forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaths_test.go
104 lines (83 loc) · 3.01 KB
/
paths_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package paths_test
import (
"strings"
"testing"
"github.com/alecthomas/assert"
"github.com/stretchr/testify/suite"
"www.velocidex.com/golang/velociraptor/config"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto"
"www.velocidex.com/golang/velociraptor/datastore"
"www.velocidex.com/golang/velociraptor/file_store/api"
"www.velocidex.com/golang/velociraptor/file_store/memory"
"www.velocidex.com/golang/velociraptor/file_store/path_specs"
"www.velocidex.com/golang/velociraptor/paths"
)
type PathManagerTestSuite struct {
suite.Suite
config_obj *config_proto.Config
}
func (self *PathManagerTestSuite) SetupTest() {
self.config_obj = config.GetDefaultConfig()
self.config_obj.Datastore.Location = "/ds"
self.config_obj.Datastore.FilestoreDirectory = "/fs"
}
func (self *PathManagerTestSuite) TestAsClientPath() {
path_spec := path_specs.NewSafeFilestorePath("a", "b", "c").
SetType(api.PATH_TYPE_FILESTORE_JSON)
client_path := path_spec.AsClientPath()
// The client path needs to carry the extension
assert.True(self.T(), strings.HasSuffix(client_path, ".json"))
// Parse the path back into a path spec - this should restore
// the type from the extension.
new_path_spec := paths.ExtractClientPathSpec("", client_path)
assert.Equal(self.T(), new_path_spec.Type(), path_spec.Type())
assert.Equal(self.T(), new_path_spec.Components(), path_spec.Components())
}
// Use the path spec to store to the data store and figure out exactly
// where the file will be created. Return this path - this includes
// any file store escaping or path transformations.
func (self *PathManagerTestSuite) getDatastorePath(path_spec api.DSPathSpec) string {
ds := datastore.NewMemcacheDataStore(self.config_obj)
data := &crypto_proto.VeloMessage{}
ds.SetSubject(self.config_obj, path_spec, data)
results := []string{}
for _, k := range ds.Dump() {
if k.IsDir() {
continue
}
results = append(results, normalize_path(
k.AsDatastoreFilename(self.config_obj)))
}
assert.Equal(self.T(), 1, len(results))
// Check that ListChildren() returns the correct path.
children, err := ds.ListChildren(self.config_obj, path_spec.Dir())
assert.NoError(self.T(), err)
for _, child := range children {
assert.Equal(self.T(),
child.AsClientPath(),
path_spec.AsClientPath())
}
return results[0]
}
func normalize_path(filename string) string {
return strings.ReplaceAll(strings.TrimLeft(filename, "\\?"), "\\", "/")
}
// Gets the actual file store path written (including escapes)
func (self *PathManagerTestSuite) getFilestorePath(path_spec api.FSPathSpec) string {
fs := memory.NewMemoryFileStore(self.config_obj)
fs.Clear()
fd, err := fs.WriteFile(path_spec)
assert.NoError(self.T(), err)
fd.Write([]byte(""))
fd.Close()
results := []string{}
for _, k := range fs.Data.Keys() {
results = append(results, k)
}
assert.Equal(self.T(), 1, len(results))
return results[0]
}
func TestPathManagers(t *testing.T) {
suite.Run(t, &PathManagerTestSuite{})
}