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
|
package btf
import (
"bytes"
"strings"
"testing"
"github.com/go-quicktest/qt"
)
func TestStringTable(t *testing.T) {
const in = "\x00one\x00two\x00"
const splitIn = "three\x00four\x00"
st, err := readStringTable(strings.NewReader(in), nil)
if err != nil {
t.Fatal(err)
}
// Parse string table of split BTF
split, err := readStringTable(strings.NewReader(splitIn), st)
if err != nil {
t.Fatal(err)
}
testcases := []struct {
offset uint32
want string
}{
{0, ""},
{1, "one"},
{5, "two"},
{9, "three"},
{15, "four"},
}
for _, tc := range testcases {
have, err := split.Lookup(tc.offset)
if err != nil {
t.Errorf("Offset %d: %s", tc.offset, err)
continue
}
if have != tc.want {
t.Errorf("Offset %d: want %s but have %s", tc.offset, tc.want, have)
}
}
if _, err := st.Lookup(2); err == nil {
t.Error("No error when using offset pointing into middle of string")
}
// Make sure we reject bogus tables
_, err = readStringTable(strings.NewReader("\x00one"), nil)
if err == nil {
t.Fatal("Accepted non-terminated string")
}
_, err = readStringTable(strings.NewReader("one\x00"), nil)
if err == nil {
t.Fatal("Accepted non-empty first item")
}
}
func TestStringTableBuilder(t *testing.T) {
stb := newStringTableBuilder(0)
_, err := readStringTable(bytes.NewReader(stb.AppendEncoded(nil)), nil)
qt.Assert(t, qt.IsNil(err), qt.Commentf("Can't parse string table"))
_, err = stb.Add("foo\x00bar")
qt.Assert(t, qt.IsNotNil(err))
empty, err := stb.Add("")
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(empty, 0), qt.Commentf("The empty string is not at index 0"))
foo1, _ := stb.Add("foo")
foo2, _ := stb.Add("foo")
qt.Assert(t, qt.Equals(foo1, foo2), qt.Commentf("Adding the same string returns different offsets"))
table := stb.AppendEncoded(nil)
if n := bytes.Count(table, []byte("foo")); n != 1 {
t.Fatalf("Marshalled string table contains foo %d times instead of once", n)
}
_, err = readStringTable(bytes.NewReader(table), nil)
qt.Assert(t, qt.IsNil(err), qt.Commentf("Can't parse string table"))
}
func BenchmarkStringTableZeroLookup(b *testing.B) {
strings := vmlinuxTestdataSpec(b).strings
b.ResetTimer()
for i := 0; i < b.N; i++ {
s, err := strings.Lookup(0)
if err != nil {
b.Fatal(err)
}
if s != "" {
b.Fatal("0 is not the empty string")
}
}
}
func newStringTable(strings ...string) *stringTable {
offsets := make([]uint32, len(strings))
var offset uint32
for i, str := range strings {
offsets[i] = offset
offset += uint32(len(str)) + 1 // account for NUL
}
return &stringTable{nil, offsets, 0, strings}
}
|