···
"github.com/stretchr/testify/assert"
func TestMessageDecode(t *testing.T) {
-
0x8e, 0x19, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x6e, 0x65,
-
0x77, 0x73, 0x0b, 0x79, 0x63, 0x6f, 0x6d, 0x62, 0x69, 0x6e, 0x61, 0x74, 0x6f, 0x72, 0x03,
-
0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00,
-
0x00, 0x00, 0x01, 0x00, 0x04, 0xd1, 0xd8, 0xe6, 0xcf,
-
assert.Equal(t, uint16(0x8e19), msg.Header.ID)
-
assert.Equal(t, true, msg.Header.QR)
-
assert.Equal(t, OPCode(0), msg.Header.OPCode)
-
assert.Equal(t, false, msg.Header.AA)
-
assert.Equal(t, false, msg.Header.TC)
-
assert.Equal(t, true, msg.Header.RD)
-
assert.Equal(t, true, msg.Header.RA)
-
assert.Equal(t, uint8(0), msg.Header.Z)
-
assert.Equal(t, RCode(0), msg.Header.RCode)
-
assert.Equal(t, uint16(1), msg.Header.QDCount)
-
assert.Equal(t, uint16(1), msg.Header.ANCount)
-
assert.Equal(t, uint16(0), msg.Header.NSCount)
-
assert.Equal(t, uint16(0), msg.Header.ARCount)
-
assert.Equal(t, 1, len(msg.Question))
-
question := msg.Question[0]
-
assert.Equal(t, "news.ycombinator.com", question.QName)
-
assert.Equal(t, DNSType(1), question.QType)
-
assert.Equal(t, DNSClass(1), question.QClass)
-
assert.Equal(t, 1, len(msg.Answer))
-
answer := msg.Answer[0]
-
assert.Equal(t, answer.Name, "news.ycombinator.com")
-
assert.Equal(t, DNSType(1), answer.RType)
-
assert.Equal(t, DNSClass(1), answer.RClass)
-
assert.Equal(t, uint32(1), answer.TTL)
-
assert.Equal(t, uint16(4), answer.RDLength)
-
func TestMessageDecodeWithU14Offset(t *testing.T) {
-
0x0e, 0xc3, 0x80, 0x00, 0x00, 0x01, 0x00, 0x00,
-
0x00, 0x0d, 0x00, 0x0e, 0x06, 0x6e, 0x73, 0x2d,
-
0x33, 0x37, 0x32, 0x09, 0x61, 0x77, 0x73, 0x64,
-
0x6e, 0x73, 0x2d, 0x34, 0x36, 0x03, 0x63, 0x6f,
-
0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x1d,
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x14, 0x01, 0x61, 0x0c, 0x67, 0x74, 0x6c,
-
0x64, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72,
-
0x73, 0x03, 0x6e, 0x65, 0x74, 0x00, 0xc0, 0x1d,
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0x01, 0x62, 0xc0, 0x34, 0xc0, 0x1d,
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0x01, 0x63, 0xc0, 0x34, 0xc0, 0x1d,
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0x01, 0x64, 0xc0, 0x34, 0xc0, 0x1d,
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0x01, 0x65, 0xc0, 0x34, 0xc0, 0x1d,
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0x01, 0x66, 0xc0, 0x34, 0xc0, 0x1d,
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0x01, 0x67, 0xc0, 0x34, 0xc0, 0x1d,
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0x01, 0x68, 0xc0, 0x34, 0xc0, 0x1d,
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0x01, 0x69, 0xc0, 0x34, 0xc0, 0x1d,
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0x01, 0x6a, 0xc0, 0x34, 0xc0, 0x1d,
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0x01, 0x6b, 0xc0, 0x34, 0xc0, 0x1d,
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0x01, 0x6c, 0xc0, 0x34, 0xc0, 0x1d,
-
0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0x01, 0x6d, 0xc0, 0x34, 0xc0, 0x32,
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0xc0, 0x05, 0x06, 0x1e, 0xc0, 0x52,
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0xc0, 0x21, 0x0e, 0x1e, 0xc0, 0x62,
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0xc0, 0x1a, 0x5c, 0x1e, 0xc0, 0x72,
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0xc0, 0x1f, 0x50, 0x1e, 0xc0, 0x82,
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0xc0, 0x0c, 0x5e, 0x1e, 0xc0, 0x92,
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0xc0, 0x23, 0x33, 0x1e, 0xc0, 0xa2,
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0xc0, 0x2a, 0x5d, 0x1e, 0xc0, 0xb2,
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0xc0, 0x36, 0x70, 0x1e, 0xc0, 0xc2,
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0xc0, 0x2b, 0xac, 0x1e, 0xc0, 0xd2,
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0xc0, 0x30, 0x4f, 0x1e, 0xc0, 0xe2,
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0xc0, 0x34, 0xb2, 0x1e, 0xc0, 0xf2,
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0xc0, 0x29, 0xa2, 0x1e, 0xc1, 0x02,
-
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x04, 0xc0, 0x37, 0x53, 0x1e, 0xc0, 0x32,
-
0x00, 0x1c, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00,
-
0x00, 0x10, 0x20, 0x01, 0x05, 0x03, 0xa8, 0x3e,
-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
-
// assert_no_error(t, err)
-
assert.Equal(t, uint16(0x0ec3), msg.Header.ID)
-
assert.Equal(t, true, msg.Header.QR)
-
assert.Equal(t, QUERY, msg.Header.OPCode)
-
assert.Equal(t, false, msg.Header.AA)
-
assert.Equal(t, false, msg.Header.TC)
-
assert.Equal(t, false, msg.Header.RD)
-
assert.Equal(t, false, msg.Header.RA)
-
assert.Equal(t, uint8(0), msg.Header.Z)
-
assert.Equal(t, NOERROR, msg.Header.RCode)
-
assert.Equal(t, uint16(1), msg.Header.QDCount)
-
assert.Equal(t, uint16(0), msg.Header.ANCount)
-
assert.Equal(t, uint16(13), msg.Header.NSCount)
-
assert.Equal(t, uint16(14), msg.Header.ARCount)
-
assert.Equal(t, 1, len(msg.Question))
-
question := msg.Question[0]
-
assert.Equal(t, "ns-372.awsdns-46.com", question.QName)
-
assert.Equal(t, AType, question.QType)
-
assert.Equal(t, IN, question.QClass)
-
assert.Equal(t, 13, len(msg.Authority))
-
assert.Equal(t, 14, len(msg.Additional))
-
func TestMessageEncode(t *testing.T) {
-
0x8e, 0x19, 0x81, 0x80, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x04, 0x6e, 0x65,
-
0x77, 0x73, 0x0b, 0x79, 0x63, 0x6f, 0x6d, 0x62, 0x69, 0x6e, 0x61, 0x74, 0x6f, 0x72, 0x03,
-
0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00,
-
0x00, 0x00, 0x01, 0x00, 0x04, 0xd1, 0xd8, 0xe6, 0xcf,
-
err := msg.Decode(bytes)
-
assert.Equal(t, bytes, actual)
-
func TestMessageEncode2(t *testing.T) {
-
0xfc, 0xa9, 0x81, 0x80, 0x00, 0x01, 0x00, 0x05,
-
0x00, 0x00, 0x00, 0x00, 0x03, 0x6f, 0x6c, 0x64,
-
0x06, 0x72, 0x65, 0x64, 0x64, 0x69, 0x74, 0x03,
-
0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
-
0xc0, 0x0c, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00,
-
0x2a, 0x17, 0x00, 0x17, 0x06, 0x72, 0x65, 0x64,
-
0x64, 0x69, 0x74, 0x03, 0x6d, 0x61, 0x70, 0x06,
-
0x66, 0x61, 0x73, 0x74, 0x6c, 0x79, 0x03, 0x6e,
-
0x65, 0x74, 0x00, 0xc0, 0x2c, 0x00, 0x01, 0x00,
-
0x01, 0x00, 0x00, 0x00, 0x23, 0x00, 0x04, 0x97,
-
0x65, 0x01, 0x8c, 0xc0, 0x2c, 0x00, 0x01, 0x00,
-
0x01, 0x00, 0x00, 0x00, 0x23, 0x00, 0x04, 0x97,
-
0x65, 0xc1, 0x8c, 0xc0, 0x2c, 0x00, 0x01, 0x00,
-
0x01, 0x00, 0x00, 0x00, 0x23, 0x00, 0x04, 0x97,
-
0x65, 0x41, 0x8c, 0xc0, 0x2c, 0x00, 0x01, 0x00,
-
0x01, 0x00, 0x00, 0x00, 0x23, 0x00, 0x04, 0x97,
-
err := msg.Decode(bytes)
-
assert.Equal(t, bytes, actual)
func FuzzDecodeMessage(f *testing.F) {
···
for _, tc := range testcases {
-
f.Add(tc) // Use f.Add to provide a seed corpus
f.Fuzz(func(t *testing.T, msg []byte) {
···
"github.com/stretchr/testify/assert"
func TestMessageDecode(t *testing.T) {
+
name: "Valid DNS message with one question",
+
buf := new(bytes.Buffer)
+
binary.Write(buf, binary.BigEndian, uint16(1234))
+
binary.Write(buf, binary.BigEndian, uint16(0x0100))
+
binary.Write(buf, binary.BigEndian, uint16(1))
+
binary.Write(buf, binary.BigEndian, uint16(0))
+
binary.Write(buf, binary.BigEndian, uint16(0))
+
binary.Write(buf, binary.BigEndian, uint16(0))
+
buf.Write([]byte{3, 'w', 'w', 'w', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0})
+
binary.Write(buf, binary.BigEndian, uint16(1))
+
binary.Write(buf, binary.BigEndian, uint16(1))
+
QName: "www.example.com",
+
name: "Valid DNS message with one answer",
+
buf := new(bytes.Buffer)
+
binary.Write(buf, binary.BigEndian, uint16(5678))
+
binary.Write(buf, binary.BigEndian, uint16(0x8180))
+
binary.Write(buf, binary.BigEndian, uint16(0))
+
binary.Write(buf, binary.BigEndian, uint16(1))
+
binary.Write(buf, binary.BigEndian, uint16(0))
+
binary.Write(buf, binary.BigEndian, uint16(0))
+
buf.Write([]byte{3, 'w', 'w', 'w', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 3, 'c', 'o', 'm', 0})
+
binary.Write(buf, binary.BigEndian, uint16(1))
+
binary.Write(buf, binary.BigEndian, uint16(1))
+
binary.Write(buf, binary.BigEndian, uint32(3600))
+
binary.Write(buf, binary.BigEndian, uint16(4))
+
binary.Write(buf, binary.BigEndian, uint32(0x0A000001))
+
Answer: []ResourceRecord{
+
Name: "www.example.com",
+
RData: &A{net.IP([]byte{10, 0, 0, 1})},
+
name: "Invalid input - empty buffer",
+
for _, tt := range tests {
+
t.Run(tt.name, func(t *testing.T) {
+
err := m.Decode(tt.input)
+
assert.Equal(t, tt.expected.Header, m.Header)
+
assert.Equal(t, tt.expected.Question, m.Question)
+
assert.Equal(t, tt.expected.Answer, m.Answer)
+
assert.Equal(t, tt.expected.Authority, m.Authority)
+
assert.Equal(t, tt.expected.Additional, m.Additional)
func FuzzDecodeMessage(f *testing.F) {
···
for _, tc := range testcases {
f.Fuzz(func(t *testing.T, msg []byte) {