a go dns packet parser
1package magna 2 3import ( 4 "testing" 5) 6 7func TestMessageDecode(t *testing.T) { 8 bytes := []byte{ 9 0x8e, 0x19, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x6e, 0x65, 10 0x77, 0x73, 0x0b, 0x79, 0x63, 0x6f, 0x6d, 0x62, 0x69, 0x6e, 0x61, 0x74, 0x6f, 0x72, 0x03, 11 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, 12 0x00, 0x00, 0x01, 0x00, 0x04, 0xd1, 0xd8, 0xe6, 0xcf, 13 } 14 15 var msg Message 16 msg.Decode(bytes) 17 assert_eq(t, uint16(0x8e19), msg.Header.ID) 18 assert_eq(t, true, msg.Header.QR) 19 assert_eq(t, OPCode(0), msg.Header.OPCode) 20 assert_eq(t, false, msg.Header.AA) 21 assert_eq(t, false, msg.Header.TC) 22 assert_eq(t, true, msg.Header.RD) 23 assert_eq(t, true, msg.Header.RA) 24 assert_eq(t, uint8(0), msg.Header.Z) 25 assert_eq(t, RCode(0), msg.Header.RCode) 26 assert_eq(t, uint16(1), msg.Header.QDCount) 27 assert_eq(t, uint16(1), msg.Header.ANCount) 28 assert_eq(t, uint16(0), msg.Header.NSCount) 29 assert_eq(t, uint16(0), msg.Header.ARCount) 30 31 assert_eq(t, 1, len(msg.Question)) 32 33 question := msg.Question[0] 34 assert_eq(t, "news.ycombinator.com", question.QName) 35 assert_eq(t, DNSType(1), question.QType) 36 assert_eq(t, DNSClass(1), question.QClass) 37 38 assert_eq(t, 1, len(msg.Answer)) 39 answer := msg.Answer[0] 40 assert_eq(t, answer.Name, "news.ycombinator.com") 41 assert_eq(t, DNSType(1), answer.RType) 42 assert_eq(t, DNSClass(1), answer.RClass) 43 assert_eq(t, uint32(1), answer.TTL) 44 assert_eq(t, uint16(4), answer.RDLength) 45} 46 47func TestMessageDecodeWithU14Offset(t *testing.T) { 48 bytes := []byte{ 49 0x0e, 0xc3, 0x80, 0x00, 0x00, 0x01, 0x00, 0x00, 50 0x00, 0x0d, 0x00, 0x0e, 0x06, 0x6e, 0x73, 0x2d, 51 0x33, 0x37, 0x32, 0x09, 0x61, 0x77, 0x73, 0x64, 52 0x6e, 0x73, 0x2d, 0x34, 0x36, 0x03, 0x63, 0x6f, 53 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x1d, 54 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 55 0x00, 0x14, 0x01, 0x61, 0x0c, 0x67, 0x74, 0x6c, 56 0x64, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 57 0x73, 0x03, 0x6e, 0x65, 0x74, 0x00, 0xc0, 0x1d, 58 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 59 0x00, 0x04, 0x01, 0x62, 0xc0, 0x34, 0xc0, 0x1d, 60 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 61 0x00, 0x04, 0x01, 0x63, 0xc0, 0x34, 0xc0, 0x1d, 62 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 63 0x00, 0x04, 0x01, 0x64, 0xc0, 0x34, 0xc0, 0x1d, 64 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 65 0x00, 0x04, 0x01, 0x65, 0xc0, 0x34, 0xc0, 0x1d, 66 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 67 0x00, 0x04, 0x01, 0x66, 0xc0, 0x34, 0xc0, 0x1d, 68 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 69 0x00, 0x04, 0x01, 0x67, 0xc0, 0x34, 0xc0, 0x1d, 70 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 71 0x00, 0x04, 0x01, 0x68, 0xc0, 0x34, 0xc0, 0x1d, 72 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 73 0x00, 0x04, 0x01, 0x69, 0xc0, 0x34, 0xc0, 0x1d, 74 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 75 0x00, 0x04, 0x01, 0x6a, 0xc0, 0x34, 0xc0, 0x1d, 76 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 77 0x00, 0x04, 0x01, 0x6b, 0xc0, 0x34, 0xc0, 0x1d, 78 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 79 0x00, 0x04, 0x01, 0x6c, 0xc0, 0x34, 0xc0, 0x1d, 80 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 81 0x00, 0x04, 0x01, 0x6d, 0xc0, 0x34, 0xc0, 0x32, 82 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 83 0x00, 0x04, 0xc0, 0x05, 0x06, 0x1e, 0xc0, 0x52, 84 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 85 0x00, 0x04, 0xc0, 0x21, 0x0e, 0x1e, 0xc0, 0x62, 86 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 87 0x00, 0x04, 0xc0, 0x1a, 0x5c, 0x1e, 0xc0, 0x72, 88 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 89 0x00, 0x04, 0xc0, 0x1f, 0x50, 0x1e, 0xc0, 0x82, 90 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 91 0x00, 0x04, 0xc0, 0x0c, 0x5e, 0x1e, 0xc0, 0x92, 92 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 93 0x00, 0x04, 0xc0, 0x23, 0x33, 0x1e, 0xc0, 0xa2, 94 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 95 0x00, 0x04, 0xc0, 0x2a, 0x5d, 0x1e, 0xc0, 0xb2, 96 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 97 0x00, 0x04, 0xc0, 0x36, 0x70, 0x1e, 0xc0, 0xc2, 98 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 99 0x00, 0x04, 0xc0, 0x2b, 0xac, 0x1e, 0xc0, 0xd2, 100 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 101 0x00, 0x04, 0xc0, 0x30, 0x4f, 0x1e, 0xc0, 0xe2, 102 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 103 0x00, 0x04, 0xc0, 0x34, 0xb2, 0x1e, 0xc0, 0xf2, 104 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 105 0x00, 0x04, 0xc0, 0x29, 0xa2, 0x1e, 0xc1, 0x02, 106 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 107 0x00, 0x04, 0xc0, 0x37, 0x53, 0x1e, 0xc0, 0x32, 108 0x00, 0x1c, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 109 0x00, 0x10, 0x20, 0x01, 0x05, 0x03, 0xa8, 0x3e, 110 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 111 0x00, 0x30, 112 } 113 114 var msg Message 115 _ = msg.Decode(bytes) 116 // assert_no_error(t, err) 117 118 // Header Section 119 assert_eq(t, uint16(0x0ec3), msg.Header.ID) 120 assert_eq(t, true, msg.Header.QR) 121 assert_eq(t, QUERY, msg.Header.OPCode) 122 assert_eq(t, false, msg.Header.AA) 123 assert_eq(t, false, msg.Header.TC) 124 assert_eq(t, false, msg.Header.RD) 125 assert_eq(t, false, msg.Header.RA) 126 assert_eq(t, uint8(0), msg.Header.Z) 127 assert_eq(t, NOERROR, msg.Header.RCode) 128 assert_eq(t, uint16(1), msg.Header.QDCount) 129 assert_eq(t, uint16(0), msg.Header.ANCount) 130 assert_eq(t, uint16(13), msg.Header.NSCount) 131 assert_eq(t, uint16(14), msg.Header.ARCount) 132 133 // Query Section 134 assert_eq(t, 1, len(msg.Question)) 135 question := msg.Question[0] 136 assert_eq(t, "ns-372.awsdns-46.com", question.QName) 137 assert_eq(t, AType, question.QType) 138 assert_eq(t, IN, question.QClass) 139 140 assert_eq(t, 13, len(msg.Authority)) 141 assert_eq(t, 14, len(msg.Additional)) 142} 143 144func TestMessageEncode(t *testing.T) { 145 bytes := []byte{ 146 0x8e, 0x19, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x6e, 0x65, 147 0x77, 0x73, 0x0b, 0x79, 0x63, 0x6f, 0x6d, 0x62, 0x69, 0x6e, 0x61, 0x74, 0x6f, 0x72, 0x03, 148 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, 149 0x00, 0x00, 0x01, 0x00, 0x04, 0xd1, 0xd8, 0xe6, 0xcf, 150 } 151 152 var msg Message 153 err := msg.Decode(bytes) 154 assert_no_error(t, err) 155 156 actual := msg.Encode() 157 for i := 0; i < len(bytes); i++ { 158 if bytes[i] != actual[i] { 159 t.Fatal(bytes, actual) 160 } 161 } 162} 163 164func FuzzDecodeMessage(f *testing.F) { 165 testcases := [][]byte{ 166 { 167 0x8e, 0x19, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x6e, 0x65, 168 0x77, 0x73, 0x0b, 0x79, 0x63, 0x6f, 0x6d, 0x62, 0x69, 0x6e, 0x61, 0x74, 0x6f, 0x72, 0x03, 169 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, 170 0x00, 0x00, 0x01, 0x00, 0x04, 0xd1, 0xd8, 0xe6, 0xcf, 171 }, 172 } 173 for _, tc := range testcases { 174 f.Add(tc) // Use f.Add to provide a seed corpus 175 } 176 f.Fuzz(func(t *testing.T, msg []byte) { 177 var m Message 178 m.Decode(msg) 179 }) 180}