| 1 |
package btf |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/go-quicktest/qt" |
| 9 |
) |
| 10 |
|
| 11 |
func TestStringTable(t *testing.T) { |
| 12 |
const in = "\x00one\x00two\x00" |
| 13 |
const splitIn = "three\x00four\x00" |
| 14 |
|
| 15 |
st, err := readStringTable(strings.NewReader(in), nil) |
| 16 |
if err != nil { |
| 17 |
t.Fatal(err) |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
split, err := readStringTable(strings.NewReader(splitIn), st) |
| 22 |
if err != nil { |
| 23 |
t.Fatal(err) |
| 24 |
} |
| 25 |
|
| 26 |
testcases := []struct { |
| 27 |
offset uint32 |
| 28 |
want string |
| 29 |
}{ |
| 30 |
{0, ""}, |
| 31 |
{1, "one"}, |
| 32 |
{5, "two"}, |
| 33 |
{9, "three"}, |
| 34 |
{15, "four"}, |
| 35 |
} |
| 36 |
|
| 37 |
for _, tc := range testcases { |
| 38 |
have, err := split.Lookup(tc.offset) |
| 39 |
if err != nil { |
| 40 |
t.Errorf("Offset %d: %s", tc.offset, err) |
| 41 |
continue |
| 42 |
} |
| 43 |
|
| 44 |
if have != tc.want { |
| 45 |
t.Errorf("Offset %d: want %s but have %s", tc.offset, tc.want, have) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
if _, err := st.Lookup(2); err == nil { |
| 50 |
t.Error("No error when using offset pointing into middle of string") |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
_, err = readStringTable(strings.NewReader("\x00one"), nil) |
| 55 |
if err == nil { |
| 56 |
t.Fatal("Accepted non-terminated string") |
| 57 |
} |
| 58 |
|
| 59 |
_, err = readStringTable(strings.NewReader("one\x00"), nil) |
| 60 |
if err == nil { |
| 61 |
t.Fatal("Accepted non-empty first item") |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
func TestEmptyStringTable(t *testing.T) { |
| 66 |
empty, err := newStringTable(nil, nil) |
| 67 |
qt.Assert(t, qt.IsNil(err)) |
| 68 |
str, err := empty.Lookup(0) |
| 69 |
qt.Assert(t, qt.IsNil(err), qt.Commentf("Can't lookup empty string")) |
| 70 |
qt.Assert(t, qt.Equals(str, ""), qt.Commentf("Empty string lookup returned %q", str)) |
| 71 |
_, err = empty.Lookup(1) |
| 72 |
qt.Assert(t, qt.IsNotNil(err)) |
| 73 |
} |
| 74 |
|
| 75 |
func TestStringTableBuilder(t *testing.T) { |
| 76 |
stb := newStringTableBuilder(0) |
| 77 |
|
| 78 |
_, err := readStringTable(bytes.NewReader(stb.AppendEncoded(nil)), nil) |
| 79 |
qt.Assert(t, qt.IsNil(err), qt.Commentf("Can't parse string table")) |
| 80 |
|
| 81 |
_, err = stb.Add("foo\x00bar") |
| 82 |
qt.Assert(t, qt.IsNotNil(err)) |
| 83 |
|
| 84 |
empty, err := stb.Add("") |
| 85 |
qt.Assert(t, qt.IsNil(err)) |
| 86 |
qt.Assert(t, qt.Equals(empty, 0), qt.Commentf("The empty string is not at index 0")) |
| 87 |
|
| 88 |
foo1, _ := stb.Add("foo") |
| 89 |
foo2, _ := stb.Add("foo") |
| 90 |
qt.Assert(t, qt.Equals(foo1, foo2), qt.Commentf("Adding the same string returns different offsets")) |
| 91 |
|
| 92 |
table := stb.AppendEncoded(nil) |
| 93 |
if n := bytes.Count(table, []byte("foo")); n != 1 { |
| 94 |
t.Fatalf("Marshalled string table contains foo %d times instead of once", n) |
| 95 |
} |
| 96 |
|
| 97 |
_, err = readStringTable(bytes.NewReader(table), nil) |
| 98 |
qt.Assert(t, qt.IsNil(err), qt.Commentf("Can't parse string table")) |
| 99 |
} |
| 100 |
|
| 101 |
func BenchmarkStringTableZeroLookup(b *testing.B) { |
| 102 |
strings := vmlinuxTestdataSpec(b).strings |
| 103 |
|
| 104 |
for b.Loop() { |
| 105 |
s, err := strings.Lookup(0) |
| 106 |
if err != nil { |
| 107 |
b.Fatal(err) |
| 108 |
} |
| 109 |
if s != "" { |
| 110 |
b.Fatal("0 is not the empty string") |
| 111 |
} |
| 112 |
} |
| 113 |
} |