| 1 |
package jump_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"codeberg.org/miekg/dns/internal/bin" |
| 9 |
"codeberg.org/miekg/dns/internal/jump" |
| 10 |
) |
| 11 |
|
| 12 |
func TestName(t *testing.T) { |
| 13 |
testcases := []struct { |
| 14 |
buf []byte |
| 15 |
start int |
| 16 |
off int |
| 17 |
}{ |
| 18 |
// miek.nl (4 miek 2 nl 0) |
| 19 |
{[]byte{4, 109, 105, 101, 107, 2, 110, 108, 0}, 0, 9}, |
| 20 |
// beginning of a message, ID (98, 24),... then miek.nl as question = 0 15 (mx as type) and 0 01 as |
| 21 |
// class. But then 192 12 which is a pointer to miek.nl, so lets decode that. |
| 22 |
{[]byte{98, 24, 129, 128, 0, 1, 0, 5, 0, 0, 0, 1, 4, 109, 105, 101, 107, 2, 110, 108, 0, 0, 15, 0, 1, 192, 12, 0}, 25, 27}, |
| 23 |
// Almost entire message... we are starting '192,12,0,15'; name pointer and then mx type. |
| 24 |
// 21,61 -> ttl, then 0,27 -> rdlength, 0, 5 -> mx prio, then 4,97...,5,95: alt1.aspmx.l.google.com. |
| 25 |
{[]byte{21, 33, 129, 128, 0, 1, 0, 5, 0, 0, 0, 1, 4, 109, 105, 101, 107, 2, 110, 108, 0, 0, 15, 0, 1, 192, 12, 0, 15, 0, 1, 0, 0, 21, 61, 0, 27, 0, 5, 4, 97, 108, 116, 49, 5, 97, 115, 112, 109, 120, 1, 108, 6, 103, 111, 111, 103, 108, 101, 3, 99, 111, 109, 0}, 26, 64}, |
| 26 |
// miek.nl (4 miek 2 nl), no null byte, should terminate. |
| 27 |
{[]byte{4, 109, 105, 101, 107, 2, 110, 108}, 2, 0}, |
| 28 |
} |
| 29 |
for i, tc := range testcases { |
| 30 |
t.Run(fmt.Sprintf("test %d", i), func(t *testing.T) { |
| 31 |
off := jump.Name(tc.buf, tc.start) |
| 32 |
if off != tc.off { |
| 33 |
t.Errorf("expected offset %d, got %d", tc.off, off) |
| 34 |
} |
| 35 |
}) |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func TestTo(t *testing.T) { |
| 40 |
testcases := []struct { |
| 41 |
binary string |
| 42 |
rrs int |
| 43 |
off int |
| 44 |
}{ |
| 45 |
{"dig-mx-miek.nl", 0, 25}, |
| 46 |
{"dig-mx-miek.nl", 1, 62}, |
| 47 |
{"dig-mx-miek.nl", 2, 98}, |
| 48 |
{"dig-mx-miek.nl", 3, 114}, |
| 49 |
{"dig-mx-miek.nl", 4, 135}, |
| 50 |
{"dig-mx-miek.nl", 5, 158}, // OPT RR |
| 51 |
{"dig-mx-miek.nl", 6, 0}, // overshoot |
| 52 |
} |
| 53 |
for i, tc := range testcases { |
| 54 |
t.Run(fmt.Sprintf("test %d: %s", i, tc.binary), func(t *testing.T) { |
| 55 |
buf, _ := os.ReadFile("../../testdata/" + tc.binary) |
| 56 |
off := jump.To(tc.rrs, buf) |
| 57 |
if off != tc.off { |
| 58 |
t.Errorf("expected to land on %d, got %d", tc.off, off) |
| 59 |
t.Logf("%v\n", buf[off:]) |
| 60 |
t.Log(bin.Dump(buf)) |
| 61 |
} |
| 62 |
}) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestToNoQuestion(t *testing.T) { |
| 67 |
// 2 identical MX (miek.nl. IN MX 10 mx.miek.nl.) in the answer section, qdcount is 0 |
| 68 |
// created with |
| 69 |
// m := new(dns.Msg) |
| 70 |
// mx := &dns.MX{Hdr: dns.Header{Name: "miek.nl.", Class: dns.ClassINET}, Preference: 10, Mx: "mx.miek.nl."} |
| 71 |
// m.Answer = []dns.RR{mx, mx} |
| 72 |
// m.Pack() |
| 73 |
// println(bin.Bytes(m.Data)) |
| 74 |
msg := []byte{0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 4, 109, 105, 101, 107, 2, 110, 108, 0, 0, 15, 0, 1, 0, 0, 0, 0, 0, 7, 0, 10, 2, 109, 120, 192, 12, 192, 12, 0, 15, 0, 1, 0, 0, 0, 0, 0, 4, 0, 10, 192, 33} |
| 75 |
testcases := []struct { |
| 76 |
to int |
| 77 |
off int |
| 78 |
}{ |
| 79 |
{0, 12}, |
| 80 |
{1, 38}, |
| 81 |
{2, 0}, |
| 82 |
} |
| 83 |
for i, tc := range testcases { |
| 84 |
off := jump.To(tc.to, msg) |
| 85 |
if off != tc.off { |
| 86 |
t.Errorf("test %d, expected to land on %d, got %d", i, tc.off, off) |
| 87 |
} |
| 88 |
} |
| 89 |
} |