github.com/gopacket/gopacket@v1.1.0/layers/udp_test.go (about)

     1  // Copyright 2012, Google, Inc. All rights reserved.
     2  // Copyright 2009-2011 Andreas Krennmair. All rights reserved.
     3  //
     4  // Use of this source code is governed by a BSD-style license
     5  // that can be found in the LICENSE file in the root of the source
     6  // tree.
     7  
     8  package layers
     9  
    10  import (
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/gopacket/gopacket"
    15  )
    16  
    17  // testUDPPacketDNS is the packet:
    18  //
    19  //	10:33:07.883637 IP 172.16.255.1.53 > 172.29.20.15.35181: 47320 7/0/0 MX ALT2.ASPMX.L.GOOGLE.com. 20, MX ASPMX2.GOOGLEMAIL.com. 30, MX ASPMX3.GOOGLEMAIL.com. 30, MX ASPMX4.GOOGLEMAIL.com. 30, MX ASPMX5.GOOGLEMAIL.com. 30, MX ASPMX.L.GOOGLE.com. 10, MX ALT1.ASPMX.L.GOOGLE.com. 20 (202)
    20  //	   0x0000:  24be 0527 0b17 001f cab3 75c0 0800 4500  $..'......u...E.
    21  //	   0x0010:  00e6 68cf 0000 3f11 a6f9 ac10 ff01 ac1d  ..h...?.........
    22  //	   0x0020:  140f 0035 896d 00d2 754a b8d8 8180 0001  ...5.m..uJ......
    23  //	   0x0030:  0007 0000 0000 0478 6b63 6403 636f 6d00  .......xkcd.com.
    24  //	   0x0040:  000f 0001 c00c 000f 0001 0000 0258 0018  .............X..
    25  //	   0x0050:  0014 0441 4c54 3205 4153 504d 5801 4c06  ...ALT2.ASPMX.L.
    26  //	   0x0060:  474f 4f47 4c45 c011 c00c 000f 0001 0000  GOOGLE..........
    27  //	   0x0070:  0258 0016 001e 0641 5350 4d58 320a 474f  .X.....ASPMX2.GO
    28  //	   0x0080:  4f47 4c45 4d41 494c c011 c00c 000f 0001  OGLEMAIL........
    29  //	   0x0090:  0000 0258 000b 001e 0641 5350 4d58 33c0  ...X.....ASPMX3.
    30  //	   0x00a0:  53c0 0c00 0f00 0100 0002 5800 0b00 1e06  S.........X.....
    31  //	   0x00b0:  4153 504d 5834 c053 c00c 000f 0001 0000  ASPMX4.S........
    32  //	   0x00c0:  0258 000b 001e 0641 5350 4d58 35c0 53c0  .X.....ASPMX5.S.
    33  //	   0x00d0:  0c00 0f00 0100 0002 5800 0400 0ac0 2dc0  ........X.....-.
    34  //	   0x00e0:  0c00 0f00 0100 0002 5800 0900 1404 414c  ........X.....AL
    35  //	   0x00f0:  5431 c02d                                T1.-
    36  //
    37  // Packet generated by doing DNS query for 'xkcd.com'
    38  var testUDPPacketDNS = []byte{
    39  	0x24, 0xbe, 0x05, 0x27, 0x0b, 0x17, 0x00, 0x1f, 0xca, 0xb3, 0x75, 0xc0, 0x08, 0x00, 0x45, 0x00,
    40  	0x00, 0xe6, 0x68, 0xcf, 0x00, 0x00, 0x3f, 0x11, 0xa6, 0xf9, 0xac, 0x10, 0xff, 0x01, 0xac, 0x1d,
    41  	0x14, 0x0f, 0x00, 0x35, 0x89, 0x6d, 0x00, 0xd2, 0x75, 0x4a, 0xb8, 0xd8, 0x81, 0x80, 0x00, 0x01,
    42  	0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x04, 0x78, 0x6b, 0x63, 0x64, 0x03, 0x63, 0x6f, 0x6d, 0x00,
    43  	0x00, 0x0f, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x00, 0x02, 0x58, 0x00, 0x18,
    44  	0x00, 0x14, 0x04, 0x41, 0x4c, 0x54, 0x32, 0x05, 0x41, 0x53, 0x50, 0x4d, 0x58, 0x01, 0x4c, 0x06,
    45  	0x47, 0x4f, 0x4f, 0x47, 0x4c, 0x45, 0xc0, 0x11, 0xc0, 0x0c, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x00,
    46  	0x02, 0x58, 0x00, 0x16, 0x00, 0x1e, 0x06, 0x41, 0x53, 0x50, 0x4d, 0x58, 0x32, 0x0a, 0x47, 0x4f,
    47  	0x4f, 0x47, 0x4c, 0x45, 0x4d, 0x41, 0x49, 0x4c, 0xc0, 0x11, 0xc0, 0x0c, 0x00, 0x0f, 0x00, 0x01,
    48  	0x00, 0x00, 0x02, 0x58, 0x00, 0x0b, 0x00, 0x1e, 0x06, 0x41, 0x53, 0x50, 0x4d, 0x58, 0x33, 0xc0,
    49  	0x53, 0xc0, 0x0c, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x00, 0x02, 0x58, 0x00, 0x0b, 0x00, 0x1e, 0x06,
    50  	0x41, 0x53, 0x50, 0x4d, 0x58, 0x34, 0xc0, 0x53, 0xc0, 0x0c, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x00,
    51  	0x02, 0x58, 0x00, 0x0b, 0x00, 0x1e, 0x06, 0x41, 0x53, 0x50, 0x4d, 0x58, 0x35, 0xc0, 0x53, 0xc0,
    52  	0x0c, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x00, 0x02, 0x58, 0x00, 0x04, 0x00, 0x0a, 0xc0, 0x2d, 0xc0,
    53  	0x0c, 0x00, 0x0f, 0x00, 0x01, 0x00, 0x00, 0x02, 0x58, 0x00, 0x09, 0x00, 0x14, 0x04, 0x41, 0x4c,
    54  	0x54, 0x31, 0xc0, 0x2d,
    55  }
    56  
    57  func TestUDPPacketDNS(t *testing.T) {
    58  	p := gopacket.NewPacket(testUDPPacketDNS, LinkTypeEthernet, gopacket.Default)
    59  	if p.ErrorLayer() != nil {
    60  		t.Error("Failed to decode packet:", p.ErrorLayer().Error())
    61  	}
    62  	checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4, LayerTypeUDP, LayerTypeDNS}, t)
    63  	if got, ok := p.TransportLayer().(*UDP); ok {
    64  		want := &UDP{
    65  			BaseLayer: BaseLayer{
    66  				Contents: []byte{0x0, 0x35, 0x89, 0x6d, 0x0, 0xd2, 0x75, 0x4a},
    67  				Payload: []byte{0xb8, 0xd8, 0x81, 0x80, 0x0, 0x1, 0x0,
    68  					0x7, 0x0, 0x0, 0x0, 0x0, 0x4, 0x78, 0x6b, 0x63, 0x64, 0x3, 0x63, 0x6f,
    69  					0x6d, 0x0, 0x0, 0xf, 0x0, 0x1, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0,
    70  					0x2, 0x58, 0x0, 0x18, 0x0, 0x14, 0x4, 0x41, 0x4c, 0x54, 0x32, 0x5, 0x41,
    71  					0x53, 0x50, 0x4d, 0x58, 0x1, 0x4c, 0x6, 0x47, 0x4f, 0x4f, 0x47, 0x4c,
    72  					0x45, 0xc0, 0x11, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2, 0x58, 0x0,
    73  					0x16, 0x0, 0x1e, 0x6, 0x41, 0x53, 0x50, 0x4d, 0x58, 0x32, 0xa, 0x47, 0x4f,
    74  					0x4f, 0x47, 0x4c, 0x45, 0x4d, 0x41, 0x49, 0x4c, 0xc0, 0x11, 0xc0, 0xc,
    75  					0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2, 0x58, 0x0, 0xb, 0x0, 0x1e, 0x6, 0x41,
    76  					0x53, 0x50, 0x4d, 0x58, 0x33, 0xc0, 0x53, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1,
    77  					0x0, 0x0, 0x2, 0x58, 0x0, 0xb, 0x0, 0x1e, 0x6, 0x41, 0x53, 0x50, 0x4d,
    78  					0x58, 0x34, 0xc0, 0x53, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2,
    79  					0x58, 0x0, 0xb, 0x0, 0x1e, 0x6, 0x41, 0x53, 0x50, 0x4d, 0x58, 0x35, 0xc0,
    80  					0x53, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2, 0x58, 0x0, 0x4, 0x0,
    81  					0xa, 0xc0, 0x2d, 0xc0, 0xc, 0x0, 0xf, 0x0, 0x1, 0x0, 0x0, 0x2, 0x58, 0x0,
    82  					0x9, 0x0, 0x14, 0x4, 0x41, 0x4c, 0x54, 0x31, 0xc0, 0x2d},
    83  			},
    84  			SrcPort:  53,
    85  			DstPort:  35181,
    86  			Length:   210,
    87  			Checksum: 30026,
    88  			sPort:    []byte{0x0, 0x35},
    89  			dPort:    []byte{0x89, 0x6d},
    90  		}
    91  		if !reflect.DeepEqual(got, want) {
    92  			t.Errorf("UDP packet mismatch:\ngot  :\n%#v\n\nwant :\n%#v\n\n", got, want)
    93  		}
    94  	} else {
    95  		t.Error("Transport layer packet not UDP")
    96  	}
    97  }
    98  
    99  func loadDNS(dnspacket []byte, t *testing.T) *DNS {
   100  	p := gopacket.NewPacket(dnspacket, LinkTypeEthernet, gopacket.Default)
   101  	if p.ErrorLayer() != nil {
   102  		t.Error("Failed to decode packet:", p.ErrorLayer().Error())
   103  	}
   104  	checkLayers(p, []gopacket.LayerType{LayerTypeEthernet, LayerTypeIPv4,
   105  		LayerTypeUDP, LayerTypeDNS}, t)
   106  
   107  	dnsL := p.Layer(LayerTypeDNS)
   108  	if dnsL == nil {
   109  		t.Error("No DNS Layer found")
   110  	}
   111  
   112  	dns, ok := dnsL.(*DNS)
   113  	if !ok {
   114  		return nil
   115  	}
   116  	return dns
   117  }
   118  
   119  var testDNSQueryA = []byte{
   120  	0xfe, 0x54, 0x00, 0x3e, 0x00, 0x96, 0x52, 0x54, /* .T.>..RT */
   121  	0x00, 0xbd, 0x1c, 0x70, 0x08, 0x00, 0x45, 0x00, /* ...p..E. */
   122  	0x00, 0x3c, 0x22, 0xe0, 0x00, 0x00, 0x40, 0x11, /* .<"...@. */
   123  	0xe2, 0x38, 0xc0, 0xa8, 0x7a, 0x46, 0xc0, 0xa8, /* .8..zF.. */
   124  	0x7a, 0x01, 0xc3, 0x35, 0x00, 0x35, 0x00, 0x28, /* z..5.5.( */
   125  	0x75, 0xd2, 0x52, 0x41, 0x01, 0x00, 0x00, 0x01, /* u.RA.... */
   126  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x77, /* .......w */
   127  	0x77, 0x77, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, /* ww.googl */
   128  	0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, /* e.com... */
   129  	0x00, 0x01, /* .. */
   130  }
   131  
   132  func TestDNSQueryA(t *testing.T) {
   133  	dns := loadDNS(testDNSQueryA, t)
   134  	if dns == nil {
   135  		t.Fatal("Failed to get a pointer to DNS struct")
   136  	}
   137  
   138  	if uint16(len(dns.Questions)) != dns.QDCount {
   139  		t.Fatal("Invalid query decoding, not the right number of questions")
   140  	}
   141  
   142  	if string(dns.Questions[0].Name) != "www.google.com" {
   143  		t.Errorf("Invalid query decoding, expecting 'www.google.com', got '%s'",
   144  			dns.Questions[0].Name)
   145  	}
   146  	if dns.Questions[0].Class != DNSClassIN {
   147  		t.Errorf("Invalid query decoding, expecting Class IN, got '%d'",
   148  			dns.Questions[0].Class)
   149  	}
   150  
   151  	if dns.Questions[0].Type != DNSTypeA {
   152  		t.Errorf("Invalid query decoding, expecting Type A, got '%d'",
   153  			dns.Questions[0].Type)
   154  	}
   155  }
   156  
   157  var testDNSRRA = []byte{
   158  	0x52, 0x54, 0x00, 0xbd, 0x1c, 0x70, 0xfe, 0x54, /* RT...p.T */
   159  	0x00, 0x3e, 0x00, 0x96, 0x08, 0x00, 0x45, 0x00, /* .>....E. */
   160  	0x01, 0x24, 0x00, 0x00, 0x40, 0x00, 0x40, 0x11, /* .$..@.@. */
   161  	0xc4, 0x30, 0xc0, 0xa8, 0x7a, 0x01, 0xc0, 0xa8, /* .0..z... */
   162  	0x7a, 0x46, 0x00, 0x35, 0xc3, 0x35, 0x01, 0x10, /* zF.5.5.. */
   163  	0x76, 0xba, 0x52, 0x41, 0x81, 0x80, 0x00, 0x01, /* v.RA.... */
   164  	0x00, 0x06, 0x00, 0x04, 0x00, 0x04, 0x03, 0x77, /* .......w */
   165  	0x77, 0x77, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, /* ww.googl */
   166  	0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, /* e.com... */
   167  	0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, /* ........ */
   168  	0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0x4a, 0x7d, /* ...,..J} */
   169  	0xc3, 0x67, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, /* .g...... */
   170  	0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0x4a, 0x7d, /* ...,..J} */
   171  	0xc3, 0x68, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, /* .h...... */
   172  	0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0x4a, 0x7d, /* ...,..J} */
   173  	0xc3, 0x69, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, /* .i...... */
   174  	0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0x4a, 0x7d, /* ...,..J} */
   175  	0xc3, 0x6a, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, /* .j...... */
   176  	0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0x4a, 0x7d, /* ...,..J} */
   177  	0xc3, 0x93, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, /* ........ */
   178  	0x00, 0x00, 0x01, 0x2c, 0x00, 0x04, 0x4a, 0x7d, /* ...,..J} */
   179  	0xc3, 0x63, 0xc0, 0x10, 0x00, 0x02, 0x00, 0x01, /* .c...... */
   180  	0x00, 0x02, 0xa3, 0x00, 0x00, 0x06, 0x03, 0x6e, /* .......n */
   181  	0x73, 0x32, 0xc0, 0x10, 0xc0, 0x10, 0x00, 0x02, /* s2...... */
   182  	0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 0x00, 0x06, /* ........ */
   183  	0x03, 0x6e, 0x73, 0x33, 0xc0, 0x10, 0xc0, 0x10, /* .ns3.... */
   184  	0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */
   185  	0x00, 0x06, 0x03, 0x6e, 0x73, 0x31, 0xc0, 0x10, /* ...ns1.. */
   186  	0xc0, 0x10, 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, /* ........ */
   187  	0xa3, 0x00, 0x00, 0x06, 0x03, 0x6e, 0x73, 0x34, /* .....ns4 */
   188  	0xc0, 0x10, 0xc0, 0xb0, 0x00, 0x01, 0x00, 0x01, /* ........ */
   189  	0x00, 0x02, 0xa3, 0x00, 0x00, 0x04, 0xd8, 0xef, /* ........ */
   190  	0x20, 0x0a, 0xc0, 0x8c, 0x00, 0x01, 0x00, 0x01, /*  ....... */
   191  	0x00, 0x02, 0xa3, 0x00, 0x00, 0x04, 0xd8, 0xef, /* ........ */
   192  	0x22, 0x0a, 0xc0, 0x9e, 0x00, 0x01, 0x00, 0x01, /* "....... */
   193  	0x00, 0x02, 0xa3, 0x00, 0x00, 0x04, 0xd8, 0xef, /* ........ */
   194  	0x24, 0x0a, 0xc0, 0xc2, 0x00, 0x01, 0x00, 0x01, /* $....... */
   195  	0x00, 0x02, 0xa3, 0x00, 0x00, 0x04, 0xd8, 0xef, /* ........ */
   196  	0x26, 0x0a, /* &. */
   197  }
   198  
   199  func TestDNSRRA(t *testing.T) {
   200  	dns := loadDNS(testDNSRRA, t)
   201  	if dns == nil {
   202  		t.Fatal("Failed to get a pointer to DNS struct")
   203  		return
   204  	}
   205  
   206  	if uint16(len(dns.Questions)) != dns.QDCount {
   207  		t.Fatal("Invalid query decoding, not the right number of questions")
   208  	} else if uint16(len(dns.Answers)) != dns.ANCount {
   209  		t.Fatal("Invalid query decoding, not the right number of answers")
   210  	} else if uint16(len(dns.Authorities)) != dns.NSCount {
   211  		t.Fatal("Invalid query decoding, not the right number of authorities")
   212  	} else if uint16(len(dns.Additionals)) != dns.ARCount {
   213  		t.Fatal("Invalid query decoding, not the right number of additionals info")
   214  	}
   215  
   216  	if string(dns.Questions[0].Name) != "www.google.com" {
   217  		t.Errorf("Invalid query decoding, expecting 'www.google.com', got '%s'",
   218  			dns.Questions[0].Name)
   219  	}
   220  	if string(dns.Answers[0].Name) != "www.google.com" {
   221  		t.Errorf("Invalid query decoding, expecting 'www.google.com', got '%d'",
   222  			dns.Questions[0].Class)
   223  	}
   224  	if dns.Answers[0].Class != DNSClassIN {
   225  		t.Errorf("Invalid query decoding, expecting Class IN, got '%d'",
   226  			dns.Questions[0].Class)
   227  	}
   228  	if dns.Answers[0].Type != DNSTypeA {
   229  		t.Errorf("Invalid query decoding, expecting Type A, got '%d'",
   230  			dns.Questions[0].Type)
   231  	}
   232  	if !dns.Answers[0].IP.Equal([]byte{74, 125, 195, 103}) {
   233  		t.Errorf("Invalid query decoding, invalid IP address,"+
   234  			" expecting '74.125.195.103', got '%s'",
   235  			dns.Answers[0].IP.String())
   236  	}
   237  	if len(dns.Answers) != 6 {
   238  		t.Errorf("No correct number of answers, expecting 6, go '%d'",
   239  			len(dns.Answers))
   240  	}
   241  	if len(dns.Authorities) != 4 {
   242  		t.Errorf("No correct number of answers, expecting 4, go '%d'",
   243  			len(dns.Answers))
   244  	}
   245  	if len(dns.Additionals) != 4 {
   246  		t.Errorf("No correct number of answers, expecting 4, go '%d'",
   247  			len(dns.Answers))
   248  	} else {
   249  		for i, want := range []string{
   250  			"ns1.google.com",
   251  			"ns2.google.com",
   252  			"ns3.google.com",
   253  			"ns4.google.com",
   254  		} {
   255  			if got := string(dns.Additionals[i].Name); got != want {
   256  				t.Errorf("got %q want %q", got, want)
   257  			}
   258  		}
   259  	}
   260  }
   261  
   262  var testDNSAAAA = []byte{
   263  	0x52, 0x54, 0x00, 0xbd, 0x1c, 0x70, 0xfe, 0x54, /* RT...p.T */
   264  	0x00, 0x3e, 0x00, 0x96, 0x08, 0x00, 0x45, 0x00, /* .>....E. */
   265  	0x00, 0xe0, 0x00, 0x00, 0x40, 0x00, 0x40, 0x11, /* ....@.@. */
   266  	0xc4, 0x74, 0xc0, 0xa8, 0x7a, 0x01, 0xc0, 0xa8, /* .t..z... */
   267  	0x7a, 0x46, 0x00, 0x35, 0xdb, 0x13, 0x00, 0xcc, /* zF.5.... */
   268  	0x76, 0x76, 0xf3, 0x03, 0x81, 0x80, 0x00, 0x01, /* vv...... */
   269  	0x00, 0x01, 0x00, 0x04, 0x00, 0x04, 0x03, 0x77, /* .......w */
   270  	0x77, 0x77, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, /* ww.googl */
   271  	0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x1c, /* e.com... */
   272  	0x00, 0x01, 0xc0, 0x0c, 0x00, 0x1c, 0x00, 0x01, /* ........ */
   273  	0x00, 0x00, 0x01, 0x2c, 0x00, 0x10, 0x2a, 0x00, /* ...,..*. */
   274  	0x14, 0x50, 0x40, 0x0c, 0x0c, 0x01, 0x00, 0x00, /* .P@..... */
   275  	0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0xc0, 0x10, /* .....i.. */
   276  	0x00, 0x02, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */
   277  	0x00, 0x06, 0x03, 0x6e, 0x73, 0x34, 0xc0, 0x10, /* ...ns4.. */
   278  	0xc0, 0x10, 0x00, 0x02, 0x00, 0x01, 0x00, 0x02, /* ........ */
   279  	0xa3, 0x00, 0x00, 0x06, 0x03, 0x6e, 0x73, 0x32, /* .....ns2 */
   280  	0xc0, 0x10, 0xc0, 0x10, 0x00, 0x02, 0x00, 0x01, /* ........ */
   281  	0x00, 0x02, 0xa3, 0x00, 0x00, 0x06, 0x03, 0x6e, /* .......n */
   282  	0x73, 0x31, 0xc0, 0x10, 0xc0, 0x10, 0x00, 0x02, /* s1...... */
   283  	0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, 0x00, 0x06, /* ........ */
   284  	0x03, 0x6e, 0x73, 0x33, 0xc0, 0x10, 0xc0, 0x6c, /* .ns3...l */
   285  	0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */
   286  	0x00, 0x04, 0xd8, 0xef, 0x20, 0x0a, 0xc0, 0x5a, /* .... ..Z */
   287  	0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */
   288  	0x00, 0x04, 0xd8, 0xef, 0x22, 0x0a, 0xc0, 0x7e, /* ...."..~ */
   289  	0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */
   290  	0x00, 0x04, 0xd8, 0xef, 0x24, 0x0a, 0xc0, 0x48, /* ....$..H */
   291  	0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0xa3, 0x00, /* ........ */
   292  	0x00, 0x04, 0xd8, 0xef, 0x26, 0x0a, /* ....&. */
   293  }
   294  
   295  func TestDNSAAAA(t *testing.T) {
   296  	dns := loadDNS(testDNSAAAA, t)
   297  	if dns == nil {
   298  		t.Error("Failed to get a pointer to DNS struct")
   299  		return
   300  	}
   301  
   302  	if len(dns.Questions) != 1 {
   303  		t.Error("Invalid number of question")
   304  		return
   305  	}
   306  	if dns.Questions[0].Type != DNSTypeAAAA {
   307  		t.Errorf("Invalid question, Type is not AAAA, found %d",
   308  			dns.Questions[0].Type)
   309  	}
   310  
   311  	if len(dns.Answers) != 1 {
   312  		t.Error("Invalid number of answers")
   313  	}
   314  	if !dns.Answers[0].IP.Equal([]byte{0x2a, 0x00, 0x14, 0x50, 0x40,
   315  		0x0c, 0x0c, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69}) {
   316  		t.Error("Invalid IP address, found ",
   317  			dns.Answers[0].IP.String())
   318  	}
   319  }
   320  
   321  var testDNSMXSOA = []byte{
   322  	0x52, 0x54, 0x00, 0xbd, 0x1c, 0x70, 0xfe, 0x54, /* RT...p.T */
   323  	0x00, 0x3e, 0x00, 0x96, 0x08, 0x00, 0x45, 0x00, /* .>....E. */
   324  	0x00, 0x6e, 0x00, 0x00, 0x40, 0x00, 0x40, 0x11, /* .n..@.@. */
   325  	0xc4, 0xe6, 0xc0, 0xa8, 0x7a, 0x01, 0xc0, 0xa8, /* ....z... */
   326  	0x7a, 0x46, 0x00, 0x35, 0x9c, 0x60, 0x00, 0x5a, /* zF.5.`.Z */
   327  	0x76, 0x04, 0xfc, 0x7a, 0x81, 0x80, 0x00, 0x01, /* v..z.... */
   328  	0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x03, 0x77, /* .......w */
   329  	0x77, 0x77, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, /* ww.googl */
   330  	0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x0f, /* e.com... */
   331  	0x00, 0x01, 0xc0, 0x10, 0x00, 0x06, 0x00, 0x01, /* ........ */
   332  	0x00, 0x00, 0x00, 0x3c, 0x00, 0x26, 0x03, 0x6e, /* ...<.&.n */
   333  	0x73, 0x31, 0xc0, 0x10, 0x09, 0x64, 0x6e, 0x73, /* s1...dns */
   334  	0x2d, 0x61, 0x64, 0x6d, 0x69, 0x6e, 0xc0, 0x10, /* -admin.. */
   335  	0x00, 0x17, 0x9f, 0x64, 0x00, 0x00, 0x1c, 0x20, /* ...d...  */
   336  	0x00, 0x00, 0x07, 0x08, 0x00, 0x12, 0x75, 0x00, /* ......u. */
   337  	0x00, 0x00, 0x01, 0x2c, /* ..., */
   338  }
   339  
   340  func TestDNSMXSOA(t *testing.T) {
   341  	dns := loadDNS(testDNSMXSOA, t)
   342  	if dns == nil {
   343  		t.Error("Failed to get a pointer to DNS struct")
   344  		return
   345  	}
   346  
   347  	if len(dns.Authorities) != 1 {
   348  		t.Error("Invalid number of authoritative answers, found ",
   349  			len(dns.Authorities))
   350  		return
   351  	}
   352  }
   353  
   354  func BenchmarkDecodeDNS(b *testing.B) {
   355  	for i := 0; i < b.N; i++ {
   356  		gopacket.NewPacket(testDNSQueryA, LinkTypeEthernet, gopacket.NoCopy)
   357  	}
   358  }
   359  func BenchmarkDecodeDNSLayer(b *testing.B) {
   360  	var dns DNS
   361  	for i := 0; i < b.N; i++ {
   362  		dns.DecodeFromBytes(testDNSAAAA[ /*eth*/ 14+ /*ipv4*/ 20+ /*udp*/ 8:], gopacket.NilDecodeFeedback)
   363  	}
   364  }
   365  func TestDNSDoesNotMalloc(t *testing.T) {
   366  	var dns DNS
   367  	if n := testing.AllocsPerRun(1000, func() {
   368  		if err := dns.DecodeFromBytes(testDNSAAAA[ /*eth*/ 14+ /*ipv4*/ 20+ /*udp*/ 8:], gopacket.NilDecodeFeedback); err != nil {
   369  			t.Fatal(err)
   370  		}
   371  	}); n > 0 {
   372  		t.Error(n, "mallocs decoding DNS")
   373  	}
   374  }