-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccess_test.go
93 lines (83 loc) · 2.44 KB
/
access_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
// SPDX-License-Identifier: Apache-2.0
/* Copyright Leon Hwang */
package bice
import (
"testing"
"github.com/cilium/ebpf/asm"
"github.com/leonhwangprojects/bice/internal/test"
)
func TestAccess(t *testing.T) {
t.Run("empty options", func(t *testing.T) {
_, err := Access(AccessOptions{})
test.AssertHaveErr(t, err)
test.AssertEqual(t, err.Error(), "invalid options")
})
t.Run("failed to parse", func(t *testing.T) {
_, err := Access(AccessOptions{
Expr: "a)(test)",
Type: getSkbBtf(t),
LabelExit: labelExitFail,
})
test.AssertHaveErr(t, err)
test.AssertStrPrefix(t, err.Error(), "failed to compile expression")
})
t.Run("invalid left operand", func(t *testing.T) {
_, err := Access(AccessOptions{
Expr: "a+b == c",
Type: getSkbBtf(t),
LabelExit: labelExitFail,
})
test.AssertHaveErr(t, err)
test.AssertStrPrefix(t, err.Error(), "expression is not struct/union member access")
})
t.Run("failed to convert expression to offsets", func(t *testing.T) {
_, err := Access(AccessOptions{
Expr: "skb->xxx",
Type: getSkbBtf(t),
LabelExit: labelExitFail,
})
test.AssertHaveErr(t, err)
test.AssertStrPrefix(t, err.Error(), "failed to convert expression to offsets")
})
t.Run("empty offsets", func(t *testing.T) {
_, err := Access(AccessOptions{
Expr: "skb",
Type: getSkbBtf(t),
LabelExit: labelExitFail,
})
test.AssertHaveErr(t, err)
test.AssertStrPrefix(t, err.Error(), "expr should be struct/union member access")
})
t.Run("failed to check last field", func(t *testing.T) {
_, err := Access(AccessOptions{
Expr: "skb->pkt_type",
Type: getSkbBtf(t),
LabelExit: labelExitFail,
})
test.AssertHaveErr(t, err)
test.AssertStrPrefix(t, err.Error(), "unexpected member access of bitfield")
})
t.Run("skb->len", func(t *testing.T) {
insns, err := Access(AccessOptions{
Expr: "skb->len",
Type: getSkbBtf(t),
Src: asm.R1,
Dst: asm.R3,
Insns: nil,
LabelExit: labelExitFail,
})
test.AssertNoErr(t, err)
test.AssertEqualSlice(t, insns.Insns, asm.Instructions{
asm.Mov.Reg(asm.R3, asm.R1),
asm.Add.Imm(asm.R3, 112),
asm.Mov.Imm(asm.R2, 8),
asm.Mov.Reg(asm.R1, asm.R10),
asm.Add.Imm(asm.R1, -8),
asm.FnProbeReadKernel.Call(),
asm.LoadMem(asm.R3, asm.RFP, -8, asm.DWord),
asm.LSh.Imm(asm.R3, 32),
asm.RSh.Imm(asm.R3, 32),
})
test.AssertEqual(t, insns.LabelUsed, false)
})
}