| 1 | 
         747 | 
         jeremybenn | 
         // 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 | 
          | 
          | 
                 "testing"
  | 
      
      
         | 10 | 
          | 
          | 
         )
  | 
      
      
         | 11 | 
          | 
          | 
          
  | 
      
      
         | 12 | 
          | 
          | 
         func TestDNSParseSRVReply(t *testing.T) {
  | 
      
      
         | 13 | 
          | 
          | 
                 data, err := hex.DecodeString(dnsSRVReply)
  | 
      
      
         | 14 | 
          | 
          | 
                 if err != nil {
  | 
      
      
         | 15 | 
          | 
          | 
                         t.Fatal(err)
  | 
      
      
         | 16 | 
          | 
          | 
                 }
  | 
      
      
         | 17 | 
          | 
          | 
                 msg := new(dnsMsg)
  | 
      
      
         | 18 | 
          | 
          | 
                 ok := msg.Unpack(data)
  | 
      
      
         | 19 | 
          | 
          | 
                 if !ok {
  | 
      
      
         | 20 | 
          | 
          | 
                         t.Fatalf("unpacking packet failed")
  | 
      
      
         | 21 | 
          | 
          | 
                 }
  | 
      
      
         | 22 | 
          | 
          | 
                 if g, e := len(msg.answer), 5; g != e {
  | 
      
      
         | 23 | 
          | 
          | 
                         t.Errorf("len(msg.answer) = %d; want %d", g, e)
  | 
      
      
         | 24 | 
          | 
          | 
                 }
  | 
      
      
         | 25 | 
          | 
          | 
                 for idx, rr := range msg.answer {
  | 
      
      
         | 26 | 
          | 
          | 
                         if g, e := rr.Header().Rrtype, uint16(dnsTypeSRV); g != e {
  | 
      
      
         | 27 | 
          | 
          | 
                                 t.Errorf("rr[%d].Header().Rrtype = %d; want %d", idx, g, e)
  | 
      
      
         | 28 | 
          | 
          | 
                         }
  | 
      
      
         | 29 | 
          | 
          | 
                         if _, ok := rr.(*dnsRR_SRV); !ok {
  | 
      
      
         | 30 | 
          | 
          | 
                                 t.Errorf("answer[%d] = %T; want *dnsRR_SRV", idx, rr)
  | 
      
      
         | 31 | 
          | 
          | 
                         }
  | 
      
      
         | 32 | 
          | 
          | 
                 }
  | 
      
      
         | 33 | 
          | 
          | 
                 _, addrs, err := answer("_xmpp-server._tcp.google.com.", "foo:53", msg, uint16(dnsTypeSRV))
  | 
      
      
         | 34 | 
          | 
          | 
                 if err != nil {
  | 
      
      
         | 35 | 
          | 
          | 
                         t.Fatalf("answer: %v", err)
  | 
      
      
         | 36 | 
          | 
          | 
                 }
  | 
      
      
         | 37 | 
          | 
          | 
                 if g, e := len(addrs), 5; g != e {
  | 
      
      
         | 38 | 
          | 
          | 
                         t.Errorf("len(addrs) = %d; want %d", g, e)
  | 
      
      
         | 39 | 
          | 
          | 
                         t.Logf("addrs = %#v", addrs)
  | 
      
      
         | 40 | 
          | 
          | 
                 }
  | 
      
      
         | 41 | 
          | 
          | 
         }
  | 
      
      
         | 42 | 
          | 
          | 
          
  | 
      
      
         | 43 | 
          | 
          | 
         func TestDNSParseCorruptSRVReply(t *testing.T) {
  | 
      
      
         | 44 | 
          | 
          | 
                 data, err := hex.DecodeString(dnsSRVCorruptReply)
  | 
      
      
         | 45 | 
          | 
          | 
                 if err != nil {
  | 
      
      
         | 46 | 
          | 
          | 
                         t.Fatal(err)
  | 
      
      
         | 47 | 
          | 
          | 
                 }
  | 
      
      
         | 48 | 
          | 
          | 
                 msg := new(dnsMsg)
  | 
      
      
         | 49 | 
          | 
          | 
                 ok := msg.Unpack(data)
  | 
      
      
         | 50 | 
          | 
          | 
                 if !ok {
  | 
      
      
         | 51 | 
          | 
          | 
                         t.Fatalf("unpacking packet failed")
  | 
      
      
         | 52 | 
          | 
          | 
                 }
  | 
      
      
         | 53 | 
          | 
          | 
                 if g, e := len(msg.answer), 5; g != e {
  | 
      
      
         | 54 | 
          | 
          | 
                         t.Errorf("len(msg.answer) = %d; want %d", g, e)
  | 
      
      
         | 55 | 
          | 
          | 
                 }
  | 
      
      
         | 56 | 
          | 
          | 
                 for idx, rr := range msg.answer {
  | 
      
      
         | 57 | 
          | 
          | 
                         if g, e := rr.Header().Rrtype, uint16(dnsTypeSRV); g != e {
  | 
      
      
         | 58 | 
          | 
          | 
                                 t.Errorf("rr[%d].Header().Rrtype = %d; want %d", idx, g, e)
  | 
      
      
         | 59 | 
          | 
          | 
                         }
  | 
      
      
         | 60 | 
          | 
          | 
                         if idx == 4 {
  | 
      
      
         | 61 | 
          | 
          | 
                                 if _, ok := rr.(*dnsRR_Header); !ok {
  | 
      
      
         | 62 | 
          | 
          | 
                                         t.Errorf("answer[%d] = %T; want *dnsRR_Header", idx, rr)
  | 
      
      
         | 63 | 
          | 
          | 
                                 }
  | 
      
      
         | 64 | 
          | 
          | 
                         } else {
  | 
      
      
         | 65 | 
          | 
          | 
                                 if _, ok := rr.(*dnsRR_SRV); !ok {
  | 
      
      
         | 66 | 
          | 
          | 
                                         t.Errorf("answer[%d] = %T; want *dnsRR_SRV", idx, rr)
  | 
      
      
         | 67 | 
          | 
          | 
                                 }
  | 
      
      
         | 68 | 
          | 
          | 
                         }
  | 
      
      
         | 69 | 
          | 
          | 
                 }
  | 
      
      
         | 70 | 
          | 
          | 
                 _, addrs, err := answer("_xmpp-server._tcp.google.com.", "foo:53", msg, uint16(dnsTypeSRV))
  | 
      
      
         | 71 | 
          | 
          | 
                 if err != nil {
  | 
      
      
         | 72 | 
          | 
          | 
                         t.Fatalf("answer: %v", err)
  | 
      
      
         | 73 | 
          | 
          | 
                 }
  | 
      
      
         | 74 | 
          | 
          | 
                 if g, e := len(addrs), 4; g != e {
  | 
      
      
         | 75 | 
          | 
          | 
                         t.Errorf("len(addrs) = %d; want %d", g, e)
  | 
      
      
         | 76 | 
          | 
          | 
                         t.Logf("addrs = %#v", addrs)
  | 
      
      
         | 77 | 
          | 
          | 
                 }
  | 
      
      
         | 78 | 
          | 
          | 
         }
  | 
      
      
         | 79 | 
          | 
          | 
          
  | 
      
      
         | 80 | 
          | 
          | 
         // Valid DNS SRV reply
  | 
      
      
         | 81 | 
          | 
          | 
         const dnsSRVReply = "0901818000010005000000000c5f786d70702d736572766572045f74637006676f6f67" +
  | 
      
      
         | 82 | 
          | 
          | 
                 "6c6503636f6d0000210001c00c002100010000012c00210014000014950c786d70702d" +
  | 
      
      
         | 83 | 
          | 
          | 
                 "73657276657234016c06676f6f676c6503636f6d00c00c002100010000012c00210014" +
  | 
      
      
         | 84 | 
          | 
          | 
                 "000014950c786d70702d73657276657232016c06676f6f676c6503636f6d00c00c0021" +
  | 
      
      
         | 85 | 
          | 
          | 
                 "00010000012c00210014000014950c786d70702d73657276657233016c06676f6f676c" +
  | 
      
      
         | 86 | 
          | 
          | 
                 "6503636f6d00c00c002100010000012c00200005000014950b786d70702d7365727665" +
  | 
      
      
         | 87 | 
          | 
          | 
                 "72016c06676f6f676c6503636f6d00c00c002100010000012c00210014000014950c78" +
  | 
      
      
         | 88 | 
          | 
          | 
                 "6d70702d73657276657231016c06676f6f676c6503636f6d00"
  | 
      
      
         | 89 | 
          | 
          | 
          
  | 
      
      
         | 90 | 
          | 
          | 
         // Corrupt DNS SRV reply, with its final RR having a bogus length
  | 
      
      
         | 91 | 
          | 
          | 
         // (perhaps it was truncated, or it's malicious) The mutation is the
  | 
      
      
         | 92 | 
          | 
          | 
         // capital "FF" below, instead of the proper "21".
  | 
      
      
         | 93 | 
          | 
          | 
         const dnsSRVCorruptReply = "0901818000010005000000000c5f786d70702d736572766572045f74637006676f6f67" +
  | 
      
      
         | 94 | 
          | 
          | 
                 "6c6503636f6d0000210001c00c002100010000012c00210014000014950c786d70702d" +
  | 
      
      
         | 95 | 
          | 
          | 
                 "73657276657234016c06676f6f676c6503636f6d00c00c002100010000012c00210014" +
  | 
      
      
         | 96 | 
          | 
          | 
                 "000014950c786d70702d73657276657232016c06676f6f676c6503636f6d00c00c0021" +
  | 
      
      
         | 97 | 
          | 
          | 
                 "00010000012c00210014000014950c786d70702d73657276657233016c06676f6f676c" +
  | 
      
      
         | 98 | 
          | 
          | 
                 "6503636f6d00c00c002100010000012c00200005000014950b786d70702d7365727665" +
  | 
      
      
         | 99 | 
          | 
          | 
                 "72016c06676f6f676c6503636f6d00c00c002100010000012c00FF0014000014950c78" +
  | 
      
      
         | 100 | 
          | 
          | 
                 "6d70702d73657276657231016c06676f6f676c6503636f6d00"
  |