dnsname_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2009 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. "strings"
  7. "testing"
  8. )
  9. type testCase struct {
  10. name string
  11. result bool
  12. }
  13. var tests = []testCase{
  14. // RFC2181, section 11.
  15. {"_xmpp-server._tcp.google.com", true},
  16. {"foo.com", true},
  17. {"1foo.com", true},
  18. {"26.0.0.73.com", true},
  19. {"fo-o.com", true},
  20. {"fo1o.com", true},
  21. {"foo1.com", true},
  22. {"a.b..com", false},
  23. {"a.b-.com", false},
  24. {"a.b.com-", false},
  25. {"a.b..", false},
  26. {"b.com.", true},
  27. }
  28. func getTestCases(ch chan<- testCase) {
  29. defer close(ch)
  30. var char59 = ""
  31. var char63 = ""
  32. var char64 = ""
  33. for i := 0; i < 59; i++ {
  34. char59 += "a"
  35. }
  36. char63 = char59 + "aaaa"
  37. char64 = char63 + "a"
  38. for _, tc := range tests {
  39. ch <- tc
  40. }
  41. ch <- testCase{char63 + ".com", true}
  42. ch <- testCase{char64 + ".com", false}
  43. // 255 char name is fine:
  44. ch <- testCase{char59 + "." + char63 + "." + char63 + "." +
  45. char63 + ".com",
  46. true}
  47. // 256 char name is bad:
  48. ch <- testCase{char59 + "a." + char63 + "." + char63 + "." +
  49. char63 + ".com",
  50. false}
  51. }
  52. func TestDNSNames(t *testing.T) {
  53. ch := make(chan testCase)
  54. go getTestCases(ch)
  55. for tc := range ch {
  56. if isDomainName(tc.name) != tc.result {
  57. t.Errorf("isDomainName(%v) failed: Should be %v",
  58. tc.name, tc.result)
  59. }
  60. }
  61. }
  62. func BenchmarkDNSNames(b *testing.B) {
  63. benchmarks := append(tests, []testCase{
  64. {strings.Repeat("a", 63), true},
  65. {strings.Repeat("a", 64), false},
  66. }...)
  67. for n := 0; n < b.N; n++ {
  68. for _, tc := range benchmarks {
  69. if isDomainName(tc.name) != tc.result {
  70. b.Errorf("isDomainName(%q) = %v; want %v", tc.name, !tc.result, tc.result)
  71. }
  72. }
  73. }
  74. }