dnsmsg_test.go 3.7 KB

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