-
Notifications
You must be signed in to change notification settings - Fork 4
/
nav_cgo_test.go
335 lines (292 loc) · 12.5 KB
/
nav_cgo_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//go:build CGO
// +build CGO
/*
* Copyright (c) 2022 Red Hat, Inc.
* SPDX-License-Identifier: GPL-2.0-or-later
*/
package main
import (
"nav/config"
"database/sql"
"database/sql/driver"
"github.com/DATA-DOG/go-sqlmock"
c "nav/constants"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Nav Tests", func() {
Describe("opt2num", func() {
When("Using a valid options key", func() {
It("Should return the correct value for graphOnly", func() {
Expect(opt2num("graphOnly")).To(Equal(c.GraphOnly))
})
It("Should return the correct value for jsonOutputPlain", func() {
Expect(opt2num("jsonOutputPlain")).To(Equal(c.JsonOutputPlain))
})
It("Should return the correct value for jsonOutputB64", func() {
Expect(opt2num("jsonOutputB64")).To(Equal(c.JsonOutputB64))
})
It("Should return the correct value for jsonOutputGZB64", func() {
Expect(opt2num("jsonOutputGZB64")).To(Equal(c.JsonOutputGZB64))
})
})
When("Using an invalid options key", func() {
It("Should return 0", func() {
Expect(opt2num("invalidKey")).To(Equal(c.InvalidOutput))
})
})
})
Describe("decorateLine", func() {
var l string
var r string
var adjm []adjM
BeforeEach(func() {
l = "lsys"
r = "rsys"
adjm = []adjM{
{
l: node{
subsys: "lsys",
symbol: "lsym",
sourceRef: "lsource",
addressRef: "laddr",
},
r: node{
subsys: "rsys",
symbol: "rsym",
sourceRef: "rsource",
addressRef: "raddr",
},
},
}
})
When("Subsystems match", func() {
It("Should return a list of all matched subsystems", func() {
actual := decorateLine(l, r, adjm)
expected := " [label=\"rsym([raddr]rsource),\\n\"]"
Expect(actual).To(Equal(expected))
})
It("Should ignore duplicated entries", func() {
duplicated := adjM{
l: node{
subsys: "lsys",
symbol: "lsym",
sourceRef: "lsource",
addressRef: "laddr",
},
r: node{
subsys: "rsys",
symbol: "rsym",
sourceRef: "rsource",
addressRef: "raddr",
},
}
adjm = append(adjm, duplicated)
actual := decorateLine(l, r, adjm)
expected := " [label=\"rsym([raddr]rsource),\\n\"]"
Expect(actual).To(Equal(expected))
})
It("Should return more than one entry", func() {
extra := adjM{
l: node{
subsys: "lsys",
symbol: "lsym2",
sourceRef: "lsource2",
addressRef: "laddr2",
},
r: node{
subsys: "rsys",
symbol: "rsym2",
sourceRef: "rsource2",
addressRef: "raddr2",
},
}
adjm = append(adjm, extra)
actual := decorateLine(l, r, adjm)
expected := " [label=\"rsym([raddr]rsource),\\nrsym2([raddr2]rsource2),\\n\"]"
Expect(actual).To(Equal(expected))
})
})
When("Subsystems do not match", func() {
It("Should return an empty list if using an empty slice", func() {
actual := decorateLine(l, r, []adjM{})
expected := " [label=\"\"]"
Expect(actual).To(Equal(expected))
})
It("Should return an empty list if nodes do not match", func() {
actual := decorateLine(l, "asym", adjm)
expected := " [label=\"\"]"
Expect(actual).To(Equal(expected))
})
})
})
Describe("valid_dot", func() {
var test_valid_dot string = `
digraph G {
start -> a0;
start -> b0;
end [shape=Msquare];
}
`
var test_invalid_dot string = `
dgraph G {
start a0;
end [shape=Msquare];
}
`
When("using on dot file", func() {
It("Should return true if syntax is correct", func() {
Expect(valid_dot(test_valid_dot)).To(Equal(true))
})
It("Should return false if syntax is not correct", func() {
Expect(valid_dot(test_invalid_dot)).To(Equal(false))
})
})
})
Describe("generateOutput", func() {
type mockQueries struct {
querySTR string
resultHead []string
resultValues [][]driver.Value
}
var queryTestSerie []mockQueries
var db *sql.DB
var mock sqlmock.Sqlmock
var dok *SqlDB
dok = &SqlDB{}
db, mock, _ = sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
dok.db = db
dok.cache = Cache{}
queryTestSerie = []mockQueries{}
queryTestSerie = append(queryTestSerie, mockQueries{
querySTR: "select symbol_id from symbols where symbols.symbol_name='__x64_sys_getpid' and symbols.symbol_instance_id_ref=16",
resultHead: []string{"symbol_id"},
resultValues: [][]driver.Value{{"472055"}},
})
queryTestSerie = append(queryTestSerie, mockQueries{
querySTR: "select symbol_id, symbol_name, subsys_name, file_name from (select * from symbols, files where symbols.symbol_file_ref_id=files.file_id and " +
"symbols.symbol_instance_id_ref=16) as dummy left outer join tags on dummy.symbol_file_ref_id=tags.tag_file_ref_id where symbol_id=472055 " +
"and symbol_instance_id_ref=16",
resultHead: []string{"symbol_id", "symbol_name", "subsys_name", "file_name"},
resultValues: [][]driver.Value{{"472055", "__x64_sys_getpid", "", "kernel/sys.c"}},
})
queryTestSerie = append(queryTestSerie, mockQueries{
querySTR: "select (select symbol_type from symbols where symbol_name='__x64_sys_getpid' and symbol_instance_id_ref=16) as type, subsys_name from " +
"(select count(*) as cnt, subsys_name from tags where subsys_name in (select subsys_name from symbols, tags where " +
"symbols.symbol_file_ref_id=tags.tag_file_ref_id and symbols.symbol_name='__x64_sys_getpid' and symbols.symbol_instance_id_ref=16) " +
"group by subsys_name order by cnt desc) as tbl",
resultHead: []string{"type", "subsys_name"},
resultValues: nil,
})
queryTestSerie = append(queryTestSerie, mockQueries{
querySTR: "select caller, callee, source_line, ref_addr from xrefs where caller = 472055 and xref_instance_id_ref = 16",
resultHead: []string{"caller", "callee", "source_line", "ref_addr"},
resultValues: [][]driver.Value{{"472055", "501994", "kernel/sys.c:892", "0xffffffff81077570"},
{"472055", "472243", "kernel/sys.c:893", "0xffffffff81077589"}},
})
queryTestSerie = append(queryTestSerie, mockQueries{
querySTR: "select symbol_id, symbol_name, subsys_name, file_name from (select * from symbols, files where symbols.symbol_file_ref_id=files.file_id " +
"and symbols.symbol_instance_id_ref=16) as dummy left outer join tags on dummy.symbol_file_ref_id=tags.tag_file_ref_id where " +
"symbol_id=501994 and symbol_instance_id_ref=16",
resultHead: []string{"symbol_id", "symbol_name", "subsys_name", "file_name"},
resultValues: [][]driver.Value{{"501994", "__fentry__", "X86 ARCHITECTURE (32-BIT AND 64-BIT)", "arch/x86/kernel/ftrace_64.S"}},
})
queryTestSerie = append(queryTestSerie, mockQueries{
querySTR: "select symbol_id, symbol_name, subsys_name, file_name from (select * from symbols, files where symbols.symbol_file_ref_id=files.file_id " +
"and symbols.symbol_instance_id_ref=16) as dummy left outer join tags on dummy.symbol_file_ref_id=tags.tag_file_ref_id where " +
"symbol_id=472243 and symbol_instance_id_ref=16",
resultHead: []string{"symbol_id", "symbol_name", "subsys_name", "file_name"},
resultValues: [][]driver.Value{{"472243", "__task_pid_nr_ns", "", "kernel/pid.c"}},
})
queryTestSerie = append(queryTestSerie, mockQueries{
querySTR: "select (select symbol_type from symbols where symbol_name='__task_pid_nr_ns' and symbol_instance_id_ref=16) as type, subsys_name from " +
"(select count(*) as cnt, subsys_name from tags where subsys_name in (select subsys_name from symbols, tags where " +
"symbols.symbol_file_ref_id=tags.tag_file_ref_id and symbols.symbol_name='__task_pid_nr_ns' and symbols.symbol_instance_id_ref=16) " +
"group by subsys_name order by cnt desc) as tbl",
resultHead: []string{"type", "subsys_name"},
resultValues: nil,
})
queryTestSerie = append(queryTestSerie, mockQueries{
querySTR: "select caller, callee, source_line, ref_addr from xrefs where caller = 472243 and xref_instance_id_ref = 16",
resultHead: []string{"caller", "callee", "source_line", "ref_addr"},
resultValues: [][]driver.Value{{"472243", "501994", "kernel/pid.c:427", "0xffffffff810824e0"},
{"472243", "473674", "kernel/pid.c:430", "0xffffffff810824f7"},
{"472243", "473716", "kernel/pid.c:435", "0xffffffff81082540"},
{"472243", "473716", "kernel/pid.c:435", "0xffffffff81082584"}},
})
queryTestSerie = append(queryTestSerie, mockQueries{
querySTR: "select symbol_id, symbol_name, subsys_name, file_name from (select * from symbols, files where symbols.symbol_file_ref_id=files.file_id " +
"and symbols.symbol_instance_id_ref=16) as dummy left outer join tags on dummy.symbol_file_ref_id=tags.tag_file_ref_id where " +
"symbol_id=473674 and symbol_instance_id_ref=16",
resultHead: []string{"symbol_id", "symbol_name", "subsys_name", "file_name"},
resultValues: [][]driver.Value{{"473674", "__rcu_read_lock", "READ-COPY UPDATE (RCU)", "kernel/rcu/tree_plugin.h"}},
})
queryTestSerie = append(queryTestSerie, mockQueries{
querySTR: "select symbol_id, symbol_name, subsys_name, file_name from (select * from symbols, files where symbols.symbol_file_ref_id=files.file_id " +
"and symbols.symbol_instance_id_ref=16) as dummy left outer join tags on dummy.symbol_file_ref_id=tags.tag_file_ref_id where " +
"symbol_id=473716 and symbol_instance_id_ref=16",
resultHead: []string{"symbol_id", "symbol_name", "subsys_name", "file_name"},
resultValues: [][]driver.Value{{"473716", "__rcu_read_unlock", "READ-COPY UPDATE (RCU)", "kernel/rcu/tree_plugin.h"}},
})
queryTestSerie = append(queryTestSerie, mockQueries{
querySTR: "select (select symbol_type from symbols where symbol_name='__rcu_read_lock' and symbol_instance_id_ref=16) as type, subsys_name from " +
"(select count(*) as cnt, subsys_name from tags where subsys_name in (select subsys_name from symbols, tags where " +
"symbols.symbol_file_ref_id=tags.tag_file_ref_id and symbols.symbol_name='__rcu_read_lock' and symbols.symbol_instance_id_ref=16) " +
"group by subsys_name order by cnt desc) as tbl",
resultHead: []string{"type", "subsys_name"},
resultValues: [][]driver.Value{{"direct", "READ-COPY UPDATE (RCU)"}},
})
queryTestSerie = append(queryTestSerie, mockQueries{
querySTR: "select (select symbol_type from symbols where symbol_name='__rcu_read_unlock' and symbol_instance_id_ref=16) as type, subsys_name from " +
"(select count(*) as cnt, subsys_name from tags where subsys_name in (select subsys_name from symbols, tags where " +
"symbols.symbol_file_ref_id=tags.tag_file_ref_id and symbols.symbol_name='__rcu_read_unlock' and symbols.symbol_instance_id_ref=16) " +
"group by subsys_name order by cnt desc) as tbl",
resultHead: []string{"type", "subsys_name"},
resultValues: [][]driver.Value{{"direct", "READ-COPY UPDATE (RCU)"}},
})
queryTestSerie = append(queryTestSerie, mockQueries{
querySTR: "select subsys_name from tags where tag_file_ref_id= (select symbol_file_ref_id from symbols where symbol_id=472055)",
resultHead: []string{"subsys_name"},
resultValues: nil,
})
queryTestSerie = append(queryTestSerie, mockQueries{
querySTR: "select subsys_name from tags where tag_file_ref_id= (select symbol_file_ref_id from symbols where symbol_id=472243)",
resultHead: []string{"subsys_name"},
resultValues: nil,
})
for _, a := range queryTestSerie {
rows := sqlmock.NewRows(a.resultHead)
for _, v := range a.resultValues {
rows.AddRow(v...)
}
mock.ExpectQuery(a.querySTR).WillReturnRows(rows)
}
mock.ExpectCommit()
dok.cache.entries = map[int]entry{}
dok.cache.successors = map[int][]entry{}
dok.cache.subSys = map[string]string{}
testConfig:= config.Config{
ConfValues: config.ConfValues{
DBDriver: "postgres",
DBDSN: "host=dbs.hqhome163.com port=5432 user=alessandro password=<password> dbname=kernel_bin sslmode=disable",
Symbol: "__x64_sys_getpid",
DBInstance: 16,
Mode: c.PrintAll,
ExcludedBefore: []string{"__fentry__", "__stack_chk_fail"},
ExcludedAfter: []string{"^kfree$", "^_raw_spin_lock$", "^_raw_spin_unlock$", "^panic$", "^call_rcu$", "^__call_rcu$", "__rcu_read_unlock", "__rcu_read_lock", "path_openat"},
TargetSubsys: []string{},
MaxDepth: 0, //0: no limit
Type: "graphOnly",
Graphviz: c.OText,
},
}
dot, err := generateOutput(dok, &testConfig)
It("Should return syntax correct json with no error", func() {
Expect(err).To(BeNil())
Expect(valid_dot(dot)).To(Equal(true))
})
})
Describe("main", func() {
// TODO: `nav.main` refactoring needed
})
})