1package magna
2
3import (
4 "testing"
5)
6
7func TestDecodeDomain(t *testing.T) {
8 buf := []byte{
9 0x03, 0x63, 0x6f, 0x6d, 0x00,
10 }
11
12 domain, offset, err := decode_domain(buf, 0)
13 assert_eq(t, "com", domain)
14 assert_eq(t, 5, offset)
15 assert_no_error(t, err)
16}
17
18func TestDecodeDomainWithCompression(t *testing.T) {
19 buf := []byte{
20 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x01, 0x63, 0xC0, 0x00,
21 }
22
23 domain, offset, err := decode_domain(buf, 5)
24 assert_eq(t, "c.com", domain)
25 assert_eq(t, 9, offset)
26 assert_no_error(t, err)
27}
28
29func TestDecodeDomainWithCompressionLoop(t *testing.T) {
30 buf := []byte{
31 0x03, 0x63, 0x6f, 0x6d, 0xC0, 0x00,
32 }
33
34 domain, offset, err := decode_domain(buf, 0)
35 assert_eq(t, "", domain)
36 assert_eq(t, 6, offset)
37 assert_error(t, err)
38}
39
40func FuzzDecodeDomain(f *testing.F) {
41 testcases := [][]byte{
42 {
43 0x03, 0x63, 0x6f, 0x6d, 0x00,
44 },
45 {
46 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x01, 0x63, 0xC0, 0x00,
47 },
48 {
49 0x03, 0x63, 0x6f, 0x6d, 0xC0, 0x00,
50 },
51 }
52 for _, tc := range testcases {
53 f.Add(tc) // Use f.Add to provide a seed corpus
54 }
55 f.Fuzz(func(t *testing.T, msg []byte) {
56 decode_domain(msg, 0)
57 })
58}