forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_test.go
144 lines (111 loc) · 4.11 KB
/
mysql_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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package datastore
import (
"database/sql"
"fmt"
"sort"
"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"
"www.velocidex.com/golang/velociraptor/constants"
crypto_proto "www.velocidex.com/golang/velociraptor/crypto/proto"
)
type MysqlTestSuite struct {
suite.Suite
config_obj *config_proto.Config
datastore DataStore
}
func (self *MysqlTestSuite) SetupTest() {
// Drop the database to start a new test.
conn_string := fmt.Sprintf("%s:%s@tcp(%s)/",
self.config_obj.Datastore.MysqlUsername,
self.config_obj.Datastore.MysqlPassword,
self.config_obj.Datastore.MysqlServer)
db, err := sql.Open("mysql", conn_string)
assert.NoError(self.T(), err)
_, err = db.Exec(fmt.Sprintf("drop database `%v`",
self.config_obj.Datastore.MysqlDatabase))
if err != nil {
self.T().Skipf("Unable to contact mysql - skipping: %v", err)
return
}
defer db.Close()
_, err = initializeDatabase(self.config_obj)
assert.NoError(self.T(), err)
self.datastore, err = NewMySQLDataStore(self.config_obj)
assert.NoError(self.T(), err)
}
func (self MysqlTestSuite) TestSetGetSubject() {
message := &crypto_proto.GrrMessage{Source: "Server"}
urn := "/a/b/c"
err := self.datastore.SetSubject(self.config_obj, urn, message)
assert.NoError(self.T(), err)
read_message := &crypto_proto.GrrMessage{}
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 no error but an empty message
err = self.datastore.GetSubject(self.config_obj, urn+"foo", read_message)
assert.NoError(self.T(), err)
// 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.NoError(self.T(), err)
assert.Equal(self.T(), "", read_message.Source)
}
func (self MysqlTestSuite) TestListChildren() {
message := &crypto_proto.GrrMessage{Source: "Server"}
urn := "/a/b/c"
err := self.datastore.SetSubject(self.config_obj, urn+"/1", message)
assert.NoError(self.T(), err)
err = self.datastore.SetSubject(self.config_obj, urn+"/2", message)
assert.NoError(self.T(), err)
err = self.datastore.SetSubject(self.config_obj, urn+"/3", message)
assert.NoError(self.T(), err)
children, err := self.datastore.ListChildren(self.config_obj, urn, 0, 100)
assert.NoError(self.T(), err)
// ListChildren gives the full path to all children
assert.Equal(self.T(), children, []string{
"/a/b/c/1",
"/a/b/c/2",
"/a/b/c/3"})
children, err = self.datastore.ListChildren(self.config_obj, urn, 0, 2)
assert.NoError(self.T(), err)
assert.Equal(self.T(), children, []string{
"/a/b/c/1", "/a/b/c/2"})
children, err = self.datastore.ListChildren(self.config_obj, urn, 1, 2)
assert.NoError(self.T(), err)
assert.Equal(self.T(), children, []string{
"/a/b/c/2", "/a/b/c/3"})
}
func (self MysqlTestSuite) 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.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)
assert.Equal(self.T(), []string{client_id}, hits)
}
func TestMysqlDatabase(t *testing.T) {
// If a local testing mysql server is configured we can run
// this test, otherwise skip it.
config_obj, err := config.LoadConfig("test_data/mysql.config.yaml")
if err != nil {
return
}
suite.Run(t, &MysqlTestSuite{
config_obj: config_obj,
})
}