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