forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastore_test.go
129 lines (98 loc) · 3.78 KB
/
datastore_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package datastore
import (
"os"
"sort"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
config_proto "www.velocidex.com/golang/velociraptor/config/proto"
"www.velocidex.com/golang/velociraptor/constants"
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto"
)
type BaseTestSuite struct {
suite.Suite
config_obj *config_proto.Config
datastore DataStore
}
func (self BaseTestSuite) TestSetGetSubject() {
message := &crypto_proto.VeloMessage{Source: "Server"}
urn := "/a/b/c"
err := self.datastore.SetSubject(self.config_obj, urn, message)
assert.NoError(self.T(), err)
read_message := &crypto_proto.VeloMessage{}
err = self.datastore.GetSubject(self.config_obj, urn, read_message)
assert.NoError(self.T(), err)
assert.Equal(self.T(), message.Source, read_message.Source)
// Not existing urn returns os.ErrNotExist error and an empty message
read_message.SessionId = "X"
err = self.datastore.GetSubject(self.config_obj, urn+"foo", read_message)
assert.Error(self.T(), err, os.ErrNotExist)
// Same for json files.
read_message.SessionId = "X"
err = self.datastore.GetSubject(
self.config_obj, urn+"foo.json", read_message)
assert.Error(self.T(), err, os.ErrNotExist)
// Delete the subject
err = self.datastore.DeleteSubject(self.config_obj, urn)
assert.NoError(self.T(), err)
// It should now be cleared
err = self.datastore.GetSubject(self.config_obj, urn, read_message)
assert.Error(self.T(), err, os.ErrNotExist)
}
func (self BaseTestSuite) TestListChildren() {
message := &crypto_proto.VeloMessage{Source: "Server"}
urn := "/a/b/c"
err := self.datastore.SetSubject(self.config_obj, urn+"/1", message)
assert.NoError(self.T(), err)
time.Sleep(10 * time.Millisecond)
err = self.datastore.SetSubject(self.config_obj, urn+"/2", message)
assert.NoError(self.T(), err)
time.Sleep(10 * time.Millisecond)
err = self.datastore.SetSubject(self.config_obj, urn+"/3", message)
assert.NoError(self.T(), err)
time.Sleep(10 * time.Millisecond)
children, err := self.datastore.ListChildren(self.config_obj, urn, 0, 100)
assert.NoError(self.T(), err)
// ListChildren gives the full path to all children
sort.Strings(children)
assert.Equal(self.T(), []string{
"/a/b/c/1",
"/a/b/c/2",
"/a/b/c/3"}, children)
children, err = self.datastore.ListChildren(self.config_obj, urn, 0, 2)
assert.NoError(self.T(), err)
assert.Equal(self.T(), []string{"/a/b/c/1", "/a/b/c/2"}, children)
children, err = self.datastore.ListChildren(self.config_obj, urn, 1, 2)
assert.NoError(self.T(), err)
assert.Equal(self.T(), []string{"/a/b/c/2", "/a/b/c/3"}, children)
visited := []string{}
self.datastore.Walk(self.config_obj, "/\"a\"/b",
func(path_name string) error {
visited = append(visited, path_name)
return nil
})
sort.Strings(visited)
assert.Equal(self.T(), []string{"/a/b/c/1", "/a/b/c/2", "/a/b/c/3"}, visited)
}
func (self BaseTestSuite) TestIndexes() {
client_id := "C.1234"
client_id_2 := "C.1235"
err := self.datastore.SetIndex(self.config_obj, constants.CLIENT_INDEX_URN,
client_id, []string{"all", client_id, "Hostname", "FQDN", "host:Foo"})
assert.NoError(self.T(), err)
err = self.datastore.SetIndex(self.config_obj, constants.CLIENT_INDEX_URN,
client_id_2, []string{"all", client_id_2, "Hostname2", "FQDN2", "host:Bar"})
assert.NoError(self.T(), err)
hits := self.datastore.SearchClients(self.config_obj, constants.CLIENT_INDEX_URN,
"all", "", 0, 100, SORT_UP)
sort.Strings(hits)
assert.Equal(self.T(), []string{client_id, client_id_2}, hits)
hits = self.datastore.SearchClients(self.config_obj, constants.CLIENT_INDEX_URN,
"*foo", "", 0, 100, SORT_UP)
assert.Equal(self.T(), []string{client_id}, hits)
}
func benchmarkSearchClient(b *testing.B,
data_store DataStore,
config_obj *config_proto.Config) {
}