| 1 |
package pack |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"maps" |
| 6 |
"testing" |
| 7 |
) |
| 8 |
|
| 9 |
func TestName(t *testing.T) { |
| 10 |
newmap := func(a ...any) map[string]uint16 { |
| 11 |
m := map[string]uint16{} |
| 12 |
for i := 0; i < len(a); i += 2 { |
| 13 |
m[a[i].(string)] = uint16(a[i+1].(int)) |
| 14 |
} |
| 15 |
return m |
| 16 |
} |
| 17 |
|
| 18 |
testcases := []struct { |
| 19 |
in string |
| 20 |
ok bool |
| 21 |
comp map[string]uint16 |
| 22 |
}{ |
| 23 |
{`www.this.is.an.example.org.`, true, newmap("this.is.an.example.org.", 4, "www.this.is.an.example.org.", 0, |
| 24 |
"an.example.org.", 12, "example.org.", 15, "is.an.example.org.", 9, "org.", 23)}, |
| 25 |
{`www.example.org.`, true, newmap("www.example.org.", 0, "example.org.", 4, "org.", 12)}, |
| 26 |
{`www.example.org`, false, nil}, |
| 27 |
{`org.`, true, newmap("org.", 0)}, |
| 28 |
{`.`, true, newmap()}, |
| 29 |
{`..`, false, nil}, |
| 30 |
{`.org`, false, nil}, |
| 31 |
{`www..example.org.`, false, nil}, |
| 32 |
{`www.example.org..`, false, nil}, |
| 33 |
{``, true, newmap()}, |
| 34 |
} |
| 35 |
buf := make([]byte, 256) |
| 36 |
for i, tc := range testcases { |
| 37 |
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) { |
| 38 |
comp := map[string]uint16{} |
| 39 |
_, got := Name(tc.in, buf, 0, comp, true) |
| 40 |
if (got == nil) != tc.ok { |
| 41 |
t.Errorf("expected %t for name %q: %v", tc.ok, tc.in, got) |
| 42 |
} |
| 43 |
if !tc.ok { |
| 44 |
return |
| 45 |
} |
| 46 |
if !maps.Equal(comp, tc.comp) { |
| 47 |
t.Errorf("expected compression map\n %v, got\n %v", tc.comp, comp) |
| 48 |
} |
| 49 |
}) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func BenchmarkName(b *testing.B) { |
| 54 |
buf := make([]byte, 256) |
| 55 |
s := "wwww.example.org." |
| 56 |
for b.Loop() { |
| 57 |
Name(s, buf, 0, nil, false) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
func TestMName(t *testing.T) { |
| 62 |
testcases := []struct { |
| 63 |
in string |
| 64 |
wire string // empty means MName must return an error |
| 65 |
}{ |
| 66 |
{`.`, "\x00"}, |
| 67 |
{``, "\x00"}, |
| 68 |
{`hostmaster.example.org.`, "\x0ahostmaster\x07example\x03org\x00"}, |
| 69 |
{`a\.b.example.org.`, "\x03a.b\x07example\x03org\x00"}, |
| 70 |
// The rname of the .mil SOA, which carries four escaped dots in a single label. |
| 71 |
{`DISA\.COLUMBUS\.NS\.MBX\.HOSTMASTER-DOD-NIC.MAIL.mil.`, "\x27DISA.COLUMBUS.NS.MBX.HOSTMASTER-DOD-NIC\x04MAIL\x03mil\x00"}, |
| 72 |
{`a\.b\.c.`, "\x05a.b.c\x00"}, |
| 73 |
{`\.a.example.org.`, ""}, |
| 74 |
{`.example.org.`, ""}, |
| 75 |
{`a\.`, ""}, |
| 76 |
{`a\.b.example.org`, ""}, |
| 77 |
{`a\.b..example.org.`, ""}, |
| 78 |
} |
| 79 |
buf := make([]byte, 256) |
| 80 |
for i, tc := range testcases { |
| 81 |
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) { |
| 82 |
off, err := MName(tc.in, buf, 0) |
| 83 |
if (err == nil) != (tc.wire != "") { |
| 84 |
t.Fatalf("expected error %t for mname %q, got %v", tc.wire == "", tc.in, err) |
| 85 |
} |
| 86 |
if err != nil { |
| 87 |
return |
| 88 |
} |
| 89 |
if got := string(buf[:off]); got != tc.wire { |
| 90 |
t.Errorf("expected wire %q, got %q", tc.wire, got) |
| 91 |
} |
| 92 |
}) |
| 93 |
} |
| 94 |
} |