-
Notifications
You must be signed in to change notification settings - Fork 4
/
nav_test.go
374 lines (331 loc) · 16.8 KB
/
nav_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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//go:build !CGO
// +build !CGO
/*
* Copyright (c) 2022 Red Hat, Inc.
* SPDX-License-Identifier: GPL-2.0-or-later
*/
package main
import (
"nav/config"
c "nav/constants"
"database/sql"
"database/sql/driver"
"github.com/DATA-DOG/go-sqlmock"
. "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("generateOutput using sqlmock", func() {
var d *sqlMock
expectedDot := `digraph G {
rankdir="LR"
"__x64_sys_getpid"->"__task_pid_nr_ns"
"__task_pid_nr_ns"->"__rcu_read_lock"
"__task_pid_nr_ns"->"__rcu_read_unlock"
}`
d = &sqlMock{}
d.init(nil)
d.LOADsym2numValues("__x64_sys_getpid", 16, 472055, nil)
d.LOADgetEntryByIdValues(472055, 16, entry{symbol: "__x64_sys_getpid", fn: "kernel/sys.c", sourceRef: "", addressRef: "", subsys: []string{}, symId: 472055}, nil)
d.LOADgetSubsysFromSymbolNameValues("__x64_sys_getpid", 16, "", nil)
d.LOADgetSuccessorsByIdValues(472055, 16, []entry{
entry{symbol: "__fentry__", fn: "arch/x86/kernel/ftrace_64.S", sourceRef: "kernel/sys.c:892", addressRef: "0xffffffff81077570", subsys: []string{"X86 ARCHITECTURE (32-BIT AND 64-BIT)"}, symId: 501994},
entry{symbol: "__task_pid_nr_ns", fn: "kernel/pid.c", sourceRef: "kernel/sys.c:893", addressRef: "0xffffffff81077589", subsys: []string{}, symId: 472243},
}, nil)
d.LOADgetEntryByIdValues(501994, 16, entry{symbol: "__fentry__", fn: "arch/x86/kernel/ftrace_64.S", sourceRef: "", addressRef: "", subsys: []string{"X86 ARCHITECTURE (32-BIT AND 64-BIT)"}, symId: 501994}, nil)
d.LOADgetEntryByIdValues(472243, 16, entry{symbol: "__task_pid_nr_ns", fn: "kernel/pid.c", sourceRef: "", addressRef: "", subsys: []string{}, symId: 472243}, nil)
d.LOADgetSubsysFromSymbolNameValues("symbol=__task_pid_nr_ns", 16, "", nil)
d.LOADgetSuccessorsByIdValues(472243, 16, []entry{
entry{symbol: "__fentry__", fn: "arch/x86/kernel/ftrace_64.S", sourceRef: "kernel/pid.c:427", addressRef: "0xffffffff810824e0", subsys: []string{"X86 ARCHITECTURE (32-BIT AND 64-BIT)"}, symId: 501994},
entry{symbol: "__rcu_read_lock", fn: "kernel/rcu/tree_plugin.h", sourceRef: "kernel/pid.c:430", addressRef: "0xffffffff810824f7", subsys: []string{"READ-COPY UPDATE (RCU)"}, symId: 473674},
entry{symbol: "__rcu_read_unlock", fn: "kernel/rcu/tree_plugin.h", sourceRef: "kernel/pid.c:435", addressRef: "0xffffffff81082540", subsys: []string{"READ-COPY UPDATE (RCU)"}, symId: 473716},
entry{symbol: "__rcu_read_unlock", fn: "kernel/rcu/tree_plugin.h", sourceRef: "kernel/pid.c:435", addressRef: "0xffffffff81082584", subsys: []string{"READ-COPY UPDATE (RCU)"}, symId: 473716},
}, nil)
d.LOADgetEntryByIdValues(501994, 16, entry{symbol: "__fentry__", fn: "arch/x86/kernel/ftrace_64.S", sourceRef: "", addressRef: "", subsys: []string{"X86 ARCHITECTURE (32-BIT AND 64-BIT)"}, symId: 501994}, nil)
d.LOADgetEntryByIdValues(473674, 16, entry{symbol: "__rcu_read_lock", fn: "kernel/rcu/tree_plugin.h", sourceRef: "", addressRef: "", subsys: []string{"READ-COPY UPDATE (RCU)"}, symId: 473674}, nil)
d.LOADgetEntryByIdValues(473716, 16, entry{symbol: "__rcu_read_unlock", fn: "kernel/rcu/tree_plugin.h", sourceRef: "", addressRef: "", subsys: []string{"READ-COPY UPDATE (RCU)"}, symId: 473716}, nil)
d.LOADgetEntryByIdValues(473716, 16, entry{symbol: "__rcu_read_unlock", fn: "kernel/rcu/tree_plugin.h", sourceRef: "", addressRef: "", subsys: []string{"READ-COPY UPDATE (RCU)"}, symId: 473716}, nil)
d.LOADgetSubsysFromSymbolNameValues("_rcu_read_lock", 16, "READ-COPY UPDATE (RCU)", nil)
d.LOADgetSubsysFromSymbolNameValues("__rcu_read_unlock", 16, "READ-COPY UPDATE (RCU)", nil)
d.LOADsymbSubsysValues([]int{472055, 472243}, 16, "{\"FuncName\":\"__x64_sys_getpid\", \"subsystems\":[]},{\"FuncName\":\"__task_pid_nr_ns\", \"subsystems\":[]}", nil)
d.LOADgetEntryByIdValues(472055, 16, entry{symbol: "__x64_sys_getpid", fn: "kernel/sys.c", sourceRef: "", addressRef: "", subsys: []string{}, symId: 472055}, nil)
d.LOADgetEntryByIdValues(472243, 16, entry{symbol: "__task_pid_nr_ns", fn: "kernel/pid.c", sourceRef: "", addressRef: "", subsys: []string{}, symId: 472243}, nil)
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(d, &testConfig)
It("Should return syntax correct json with no error", func() {
Expect(err).To(BeNil())
Expect(dot).To(Equal(expectedDot))
})
})
Describe("generateOutput using go-sqlmock", 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
expectedDot := `digraph G {
rankdir="LR"
"__x64_sys_getpid"->"__task_pid_nr_ns"
"__task_pid_nr_ns"->"__rcu_read_lock"
"__task_pid_nr_ns"->"__rcu_read_unlock"
}`
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(dot).To(Equal(expectedDot))
})
})
Describe("main", func() {
// TODO: `nav.main` refactoring needed
})
})