-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathkstack_test.go
95 lines (84 loc) · 2.3 KB
/
kstack_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
package decoder
import (
"bytes"
"os"
"testing"
"github.com/cloudflare/ebpf_exporter/v2/config"
"github.com/cloudflare/ebpf_exporter/v2/kallsyms"
)
func TestKStackDecoder(t *testing.T) {
cases := []struct {
in []byte
out []byte
}{
{
in: []byte{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
out: []byte(""),
},
{
in: []byte{
0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
out: []byte("one\none\ntwo"),
},
{
in: []byte{
0xab, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
out: []byte("three\ntwo\ntwo"),
},
{
in: []byte{
0xab, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
out: []byte("three\nzero"),
},
{
in: []byte{
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xac, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
out: []byte("??\nthree\n??"),
},
}
fd, err := os.CreateTemp(t.TempDir(), "kallsyms")
if err != nil {
t.Fatalf("Error creating temporary file for kallsyms: %v", err)
}
defer os.Remove(fd.Name())
_, err = fd.WriteString("0000000000000004 T one\n0000000000000006 T two\n00000000000000aa T three\n")
if err != nil {
t.Fatalf("Error writing fake kallsyms data to %q: %v", fd.Name(), err)
}
decoder, err := kallsyms.NewDecoder(fd.Name())
if err != nil {
t.Fatalf("Error creating ksym decoder for %q: %v", fd.Name(), err)
}
_, err = fd.WriteString("0000000000000002 T zero\n")
if err != nil {
t.Fatalf("Error writing additional fake kallsyms data to %q: %v", fd.Name(), err)
}
d := KStack{decoder}
for _, c := range cases {
out, err := d.Decode(c.in, config.Decoder{})
if err != nil {
t.Errorf("Error decoding %#v: %s", c.in, err)
}
if !bytes.Equal(out, c.out) {
t.Errorf("Expected %q, got %q", c.out, out)
}
}
}