/vendor/github.com/miekg/dns/msg_test.go

https://gitlab.com/unofficial-mirrors/openshift-origin · Go · 124 lines · 110 code · 5 blank · 9 comment · 17 complexity · e23b81f527ff01965168312a5ad43ac5 MD5 · raw file

  1. package dns
  2. import (
  3. "fmt"
  4. "regexp"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. )
  9. const (
  10. maxPrintableLabel = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789x"
  11. tooLongLabel = maxPrintableLabel + "x"
  12. )
  13. var (
  14. longDomain = maxPrintableLabel[:53] + strings.TrimSuffix(
  15. strings.Join([]string{".", ".", ".", ".", "."}, maxPrintableLabel[:49]), ".")
  16. reChar = regexp.MustCompile(`.`)
  17. i = -1
  18. maxUnprintableLabel = reChar.ReplaceAllStringFunc(maxPrintableLabel, func(ch string) string {
  19. if i++; i >= 32 {
  20. i = 0
  21. }
  22. return fmt.Sprintf("\\%03d", i)
  23. })
  24. )
  25. func TestUnpackDomainName(t *testing.T) {
  26. var cases = []struct {
  27. label string
  28. input string
  29. expectedOutput string
  30. expectedError string
  31. }{
  32. {"empty domain",
  33. "\x00",
  34. ".",
  35. ""},
  36. {"long label",
  37. string(63) + maxPrintableLabel + "\x00",
  38. maxPrintableLabel + ".",
  39. ""},
  40. {"unprintable label",
  41. string(63) + regexp.MustCompile(`\\[0-9]+`).ReplaceAllStringFunc(maxUnprintableLabel,
  42. func(escape string) string {
  43. n, _ := strconv.ParseInt(escape[1:], 10, 8)
  44. return string(n)
  45. }) + "\x00",
  46. maxUnprintableLabel + ".",
  47. ""},
  48. {"long domain",
  49. string(53) + strings.Replace(longDomain, ".", string(49), -1) + "\x00",
  50. longDomain + ".",
  51. ""},
  52. {"compression pointer",
  53. // an unrealistic but functional test referencing an offset _inside_ a label
  54. "\x03foo" + "\x05\x03com\x00" + "\x07example" + "\xC0\x05",
  55. "foo.\\003com\\000.example.com.",
  56. ""},
  57. {"too long domain",
  58. string(54) + "x" + strings.Replace(longDomain, ".", string(49), -1) + "\x00",
  59. "x" + longDomain + ".",
  60. ErrLongDomain.Error()},
  61. {"too long by pointer",
  62. // a matryoshka doll name to get over 255 octets after expansion via internal pointers
  63. string([]byte{
  64. // 11 length values, first to last
  65. 40, 37, 34, 31, 28, 25, 22, 19, 16, 13, 0,
  66. // 12 filler values
  67. 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120,
  68. // 10 pointers, last to first
  69. 192, 10, 192, 9, 192, 8, 192, 7, 192, 6, 192, 5, 192, 4, 192, 3, 192, 2, 192, 1,
  70. }),
  71. "",
  72. ErrLongDomain.Error()},
  73. {"long by pointer",
  74. // a matryoshka doll name _not_ exceeding 255 octets after expansion
  75. string([]byte{
  76. // 11 length values, first to last
  77. 37, 34, 31, 28, 25, 22, 19, 16, 13, 10, 0,
  78. // 9 filler values
  79. 120, 120, 120, 120, 120, 120, 120, 120, 120,
  80. // 10 pointers, last to first
  81. 192, 10, 192, 9, 192, 8, 192, 7, 192, 6, 192, 5, 192, 4, 192, 3, 192, 2, 192, 1,
  82. }),
  83. "" +
  84. (`\"\031\028\025\022\019\016\013\010\000xxxxxxxxx` +
  85. `\192\010\192\009\192\008\192\007\192\006\192\005\192\004\192\003\192\002.`) +
  86. (`\031\028\025\022\019\016\013\010\000xxxxxxxxx` +
  87. `\192\010\192\009\192\008\192\007\192\006\192\005\192\004\192\003.`) +
  88. (`\028\025\022\019\016\013\010\000xxxxxxxxx` +
  89. `\192\010\192\009\192\008\192\007\192\006\192\005\192\004.`) +
  90. (`\025\022\019\016\013\010\000xxxxxxxxx` +
  91. `\192\010\192\009\192\008\192\007\192\006\192\005.`) +
  92. `\022\019\016\013\010\000xxxxxxxxx\192\010\192\009\192\008\192\007\192\006.` +
  93. `\019\016\013\010\000xxxxxxxxx\192\010\192\009\192\008\192\007.` +
  94. `\016\013\010\000xxxxxxxxx\192\010\192\009\192\008.` +
  95. `\013\010\000xxxxxxxxx\192\010\192\009.` +
  96. `\010\000xxxxxxxxx\192\010.` +
  97. `\000xxxxxxxxx.`,
  98. ""},
  99. {"truncated name", "\x07example\x03", "", "dns: buffer size too small"},
  100. {"non-absolute name", "\x07example\x03com", "", "dns: buffer size too small"},
  101. {"compression pointer cycle",
  102. "\x03foo" + "\x03bar" + "\x07example" + "\xC0\x04",
  103. "",
  104. "dns: too many compression pointers"},
  105. {"reserved compression pointer 0b10", "\x07example\x80", "", "dns: bad rdata"},
  106. {"reserved compression pointer 0b01", "\x07example\x40", "", "dns: bad rdata"},
  107. }
  108. for _, test := range cases {
  109. output, idx, err := UnpackDomainName([]byte(test.input), 0)
  110. if test.expectedOutput != "" && output != test.expectedOutput {
  111. t.Errorf("%s: expected %s, got %s", test.label, test.expectedOutput, output)
  112. }
  113. if test.expectedError == "" && err != nil {
  114. t.Errorf("%s: expected no error, got %d %v", test.label, idx, err)
  115. } else if test.expectedError != "" && (err == nil || err.Error() != test.expectedError) {
  116. t.Errorf("%s: expected error %s, got %d %v", test.label, test.expectedError, idx, err)
  117. }
  118. }
  119. }