github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/src/net/dnsmsg_test.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  import (
     8  	"encoding/hex"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func TestDNSParseSRVReply(t *testing.T) {
    14  	data, err := hex.DecodeString(dnsSRVReply)
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	msg := new(dnsMsg)
    19  	ok := msg.Unpack(data)
    20  	if !ok {
    21  		t.Fatalf("unpacking packet failed")
    22  	}
    23  	msg.String() // exercise this code path
    24  	if g, e := len(msg.answer), 5; g != e {
    25  		t.Errorf("len(msg.answer) = %d; want %d", g, e)
    26  	}
    27  	for idx, rr := range msg.answer {
    28  		if g, e := rr.Header().Rrtype, uint16(dnsTypeSRV); g != e {
    29  			t.Errorf("rr[%d].Header().Rrtype = %d; want %d", idx, g, e)
    30  		}
    31  		if _, ok := rr.(*dnsRR_SRV); !ok {
    32  			t.Errorf("answer[%d] = %T; want *dnsRR_SRV", idx, rr)
    33  		}
    34  	}
    35  	_, addrs, err := answer("_xmpp-server._tcp.google.com.", "foo:53", msg, uint16(dnsTypeSRV))
    36  	if err != nil {
    37  		t.Fatalf("answer: %v", err)
    38  	}
    39  	if g, e := len(addrs), 5; g != e {
    40  		t.Errorf("len(addrs) = %d; want %d", g, e)
    41  		t.Logf("addrs = %#v", addrs)
    42  	}
    43  	// repack and unpack.
    44  	data2, ok := msg.Pack()
    45  	msg2 := new(dnsMsg)
    46  	msg2.Unpack(data2)
    47  	switch {
    48  	case !ok:
    49  		t.Errorf("failed to repack message")
    50  	case !reflect.DeepEqual(msg, msg2):
    51  		t.Errorf("repacked message differs from original")
    52  	}
    53  }
    54  
    55  func TestDNSParseCorruptSRVReply(t *testing.T) {
    56  	data, err := hex.DecodeString(dnsSRVCorruptReply)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  	msg := new(dnsMsg)
    61  	ok := msg.Unpack(data)
    62  	if !ok {
    63  		t.Fatalf("unpacking packet failed")
    64  	}
    65  	msg.String() // exercise this code path
    66  	if g, e := len(msg.answer), 5; g != e {
    67  		t.Errorf("len(msg.answer) = %d; want %d", g, e)
    68  	}
    69  	for idx, rr := range msg.answer {
    70  		if g, e := rr.Header().Rrtype, uint16(dnsTypeSRV); g != e {
    71  			t.Errorf("rr[%d].Header().Rrtype = %d; want %d", idx, g, e)
    72  		}
    73  		if idx == 4 {
    74  			if _, ok := rr.(*dnsRR_Header); !ok {
    75  				t.Errorf("answer[%d] = %T; want *dnsRR_Header", idx, rr)
    76  			}
    77  		} else {
    78  			if _, ok := rr.(*dnsRR_SRV); !ok {
    79  				t.Errorf("answer[%d] = %T; want *dnsRR_SRV", idx, rr)
    80  			}
    81  		}
    82  	}
    83  	_, addrs, err := answer("_xmpp-server._tcp.google.com.", "foo:53", msg, uint16(dnsTypeSRV))
    84  	if err != nil {
    85  		t.Fatalf("answer: %v", err)
    86  	}
    87  	if g, e := len(addrs), 4; g != e {
    88  		t.Errorf("len(addrs) = %d; want %d", g, e)
    89  		t.Logf("addrs = %#v", addrs)
    90  	}
    91  }
    92  
    93  // Valid DNS SRV reply
    94  const dnsSRVReply = "0901818000010005000000000c5f786d70702d736572766572045f74637006676f6f67" +
    95  	"6c6503636f6d0000210001c00c002100010000012c00210014000014950c786d70702d" +
    96  	"73657276657234016c06676f6f676c6503636f6d00c00c002100010000012c00210014" +
    97  	"000014950c786d70702d73657276657232016c06676f6f676c6503636f6d00c00c0021" +
    98  	"00010000012c00210014000014950c786d70702d73657276657233016c06676f6f676c" +
    99  	"6503636f6d00c00c002100010000012c00200005000014950b786d70702d7365727665" +
   100  	"72016c06676f6f676c6503636f6d00c00c002100010000012c00210014000014950c78" +
   101  	"6d70702d73657276657231016c06676f6f676c6503636f6d00"
   102  
   103  // Corrupt DNS SRV reply, with its final RR having a bogus length
   104  // (perhaps it was truncated, or it's malicious) The mutation is the
   105  // capital "FF" below, instead of the proper "21".
   106  const dnsSRVCorruptReply = "0901818000010005000000000c5f786d70702d736572766572045f74637006676f6f67" +
   107  	"6c6503636f6d0000210001c00c002100010000012c00210014000014950c786d70702d" +
   108  	"73657276657234016c06676f6f676c6503636f6d00c00c002100010000012c00210014" +
   109  	"000014950c786d70702d73657276657232016c06676f6f676c6503636f6d00c00c0021" +
   110  	"00010000012c00210014000014950c786d70702d73657276657233016c06676f6f676c" +
   111  	"6503636f6d00c00c002100010000012c00200005000014950b786d70702d7365727665" +
   112  	"72016c06676f6f676c6503636f6d00c00c002100010000012c00FF0014000014950c78" +
   113  	"6d70702d73657276657231016c06676f6f676c6503636f6d00"