Debian Codegraph @

internal/errors_test.go

raw symbols SCIP
1 package internal
2
3 import (
4 "errors"
5 "os"
6 "testing"
7
8 "github.com/go-quicktest/qt"
9 )
10
11 func TestVerifierErrorWhitespace(t *testing.T) {
12 b := []byte("unreachable insn 28")
13 b = append(b,
14 0xa, // \n
15 0xd, // \r
16 0x9, // \t
17 0x20, // space
18 0, 0, // trailing NUL bytes
19 )
20
21 err := ErrorWithLog("frob", errors.New("test"), b)
22 qt.Assert(t, qt.Equals(err.Error(), "frob: test: unreachable insn 28"))
23
24 for _, log := range [][]byte{
25 nil,
26 []byte("\x00"),
27 []byte(" "),
28 } {
29 err = ErrorWithLog("frob", errors.New("test"), log)
30 qt.Assert(t, qt.Equals(err.Error(), "frob: test"), qt.Commentf("empty log %q has incorrect format", log))
31 }
32 }
33
34 func TestVerifierErrorWrapping(t *testing.T) {
35 sentinel := errors.New("bad")
36
37 ve := ErrorWithLog("frob", sentinel, nil)
38 qt.Assert(t, qt.ErrorIs(ve, sentinel), qt.Commentf("should wrap provided error"))
39
40 ve = ErrorWithLog("frob", sentinel, []byte("foo"))
41 qt.Assert(t, qt.ErrorIs(ve, sentinel), qt.Commentf("should wrap provided error"))
42 qt.Assert(t, qt.StringContains(ve.Error(), "foo"), qt.Commentf("verifier log should appear in error string"))
43 }
44
45 func TestVerifierErrorSummary(t *testing.T) {
46 // Suppress the last line containing 'processed ... insns'.
47 errno524 := readErrorFromFile(t, "testdata/errno524.log")
48 qt.Assert(t, qt.StringContains(errno524.Error(), "JIT doesn't support bpf-to-bpf calls"))
49 qt.Assert(t, qt.Not(qt.StringContains(errno524.Error(), "processed 39 insns")))
50
51 // Include the previous line if the current one starts with a tab.
52 invalidMember := readErrorFromFile(t, "testdata/invalid-member.log")
53 qt.Assert(t, qt.StringContains(invalidMember.Error(), "STRUCT task_struct size=7744 vlen=218: cpus_mask type_id=109 bitfield_size=0 bits_offset=7744 Invalid member"))
54
55 // Only include the last line.
56 issue43 := readErrorFromFile(t, "testdata/issue-43.log")
57 qt.Assert(t, qt.StringContains(issue43.Error(), "[11] FUNC helper_func2 type_id=10 vlen != 0"))
58 qt.Assert(t, qt.Not(qt.StringContains(issue43.Error(), "[10] FUNC_PROTO (anon) return=3 args=(3 arg)")))
59
60 // Include instruction that caused invalid register access.
61 invalidR0 := readErrorFromFile(t, "testdata/invalid-R0.log")
62 qt.Assert(t, qt.StringContains(invalidR0.Error(), "0: (95) exit: R0 !read_ok"))
63
64 // Include symbol that doesn't match context type.
65 invalidCtx := readErrorFromFile(t, "testdata/invalid-ctx-access.log")
66 qt.Assert(t, qt.StringContains(invalidCtx.Error(), "func '__x64_sys_recvfrom' arg0 type FWD is not a struct: invalid bpf_context access off=0 size=8"))
67 }
68
69 func readErrorFromFile(tb testing.TB, file string) *VerifierError {
70 tb.Helper()
71
72 contents, err := os.ReadFile(file)
73 if err != nil {
74 tb.Fatal("Read file:", err)
75 }
76
77 return ErrorWithLog("file", errors.New("error"), contents)
78 }