zone_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package zones
  2. import (
  3. "regexp"
  4. "testing"
  5. "github.com/miekg/dns"
  6. )
  7. func TestExampleComZone(t *testing.T) {
  8. t.Log("example com")
  9. mm, err := NewMuxManager("../dns", &NilReg{})
  10. if err != nil {
  11. t.Fatalf("Loading test zones: %s", err)
  12. }
  13. ex, ok := mm.zonelist["test.example.com"]
  14. if !ok || ex == nil || ex.Labels == nil {
  15. t.Fatalf("Did not load 'test.example.com' test zone")
  16. }
  17. if mh := ex.Labels["weight"].MaxHosts; mh != 1 {
  18. t.Logf("Invalid MaxHosts, expected one got '%d'", mh)
  19. t.Fail()
  20. }
  21. // Make sure that the empty "no.bar" zone gets skipped and "bar" is used
  22. m := ex.findFirstLabel("bar", []string{"no", "europe", "@"}, []uint16{dns.TypeA})
  23. if l := len(m.Label.Records[dns.TypeA]); l != 1 {
  24. t.Logf("Unexpected number of A records: '%d'", l)
  25. t.Fail()
  26. }
  27. if m.Type != dns.TypeA {
  28. t.Fatalf("Expected qtype = A record (type %d), got type %d", dns.TypeA, m.Type)
  29. }
  30. if str := m.Label.Records[m.Type][0].RR.(*dns.A).A.String(); str != "192.168.1.2" {
  31. t.Errorf("Got A '%s', expected '%s'", str, "192.168.1.2")
  32. }
  33. m = ex.findFirstLabel("", []string{"@"}, []uint16{dns.TypeMX})
  34. Mx := m.Label.Records[dns.TypeMX]
  35. if len(Mx) != 2 {
  36. t.Errorf("Expected 2 MX records but got %d", len(Mx))
  37. }
  38. if Mx[0].RR.(*dns.MX).Mx != "mx.example.net." {
  39. t.Errorf("First MX should have been mx.example.net, but was %s", Mx[0].RR.(*dns.MX).Mx)
  40. }
  41. m = ex.findFirstLabel("", []string{"dk", "europe", "@"}, []uint16{dns.TypeMX})
  42. Mx = m.Label.Records[dns.TypeMX]
  43. if len(Mx) != 1 {
  44. t.Errorf("Got %d MX record for dk,europe,@ - expected %d", len(Mx), 1)
  45. }
  46. if Mx[0].RR.(*dns.MX).Mx != "mx-eu.example.net." {
  47. t.Errorf("First MX should have been mx-eu.example.net, but was %s", Mx[0].RR.(*dns.MX).Mx)
  48. }
  49. // // look for multiple record types
  50. m = ex.findFirstLabel("www", []string{"@"}, []uint16{dns.TypeCNAME, dns.TypeA})
  51. if m.Type != dns.TypeCNAME {
  52. t.Errorf("www should have been a CNAME, but was a %s", dns.TypeToString[m.Type])
  53. }
  54. m = ex.findFirstLabel("", []string{"@"}, []uint16{dns.TypeNS})
  55. Ns := m.Label.Records[dns.TypeNS]
  56. if len(Ns) != 2 {
  57. t.Errorf("root should have returned 2 NS records but got %d", len(Ns))
  58. }
  59. // Test that we get the expected NS records (in any order because
  60. // of the configuration format used for this zone)
  61. for i := 0; i < 2; i++ {
  62. if matched, err := regexp.MatchString("^ns[12]\\.example\\.net.$", Ns[i].RR.(*dns.NS).Ns); err != nil || !matched {
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. t.Errorf("Unexpected NS record data '%s'", Ns[i].RR.(*dns.NS).Ns)
  67. }
  68. }
  69. m = ex.findFirstLabel("", []string{"@"}, []uint16{dns.TypeSPF})
  70. Spf := m.Label.Records[dns.TypeSPF]
  71. if txt := Spf[0].RR.(*dns.SPF).Txt[0]; txt != "v=spf1 ~all" {
  72. t.Errorf("Wrong SPF data '%s'", txt)
  73. }
  74. m = ex.findFirstLabel("foo", []string{"@"}, []uint16{dns.TypeTXT})
  75. Txt := m.Label.Records[dns.TypeTXT]
  76. if txt := Txt[0].RR.(*dns.TXT).Txt[0]; txt != "this is foo" {
  77. t.Errorf("Wrong TXT data '%s'", txt)
  78. }
  79. m = ex.findFirstLabel("weight", []string{"@"}, []uint16{dns.TypeTXT})
  80. Txt = m.Label.Records[dns.TypeTXT]
  81. txts := []string{"w10000", "w1"}
  82. for i, r := range Txt {
  83. if txt := r.RR.(*dns.TXT).Txt[0]; txt != txts[i] {
  84. t.Errorf("txt record %d was '%s', expected '%s'", i, txt, txts[i])
  85. }
  86. }
  87. // verify empty labels are created
  88. m = ex.findFirstLabel("a.b.c", []string{"@"}, []uint16{dns.TypeA})
  89. if a := m.Label.Records[dns.TypeA][0].RR.(*dns.A); a.A.String() != "192.168.1.7" {
  90. t.Errorf("unexpected IP for a.b.c '%s'", a)
  91. }
  92. emptyLabels := []string{"b.c", "c"}
  93. for _, el := range emptyLabels {
  94. m = ex.findFirstLabel(el, []string{"@"}, []uint16{dns.TypeA})
  95. if len(m.Label.Records[dns.TypeA]) > 0 {
  96. t.Errorf("Unexpected A record for '%s'", el)
  97. }
  98. if m.Label.Label != el {
  99. t.Errorf("'%s' label is '%s'", el, m.Label.Label)
  100. }
  101. }
  102. //verify label is created
  103. m = ex.findFirstLabel("three.two.one", []string{"@"}, []uint16{dns.TypeA})
  104. if l := len(m.Label.Records[dns.TypeA]); l != 1 {
  105. t.Errorf("Unexpected A record count for 'three.two.one' %d, expected 1", l)
  106. }
  107. if a := m.Label.Records[dns.TypeA][0].RR.(*dns.A); a.A.String() != "192.168.1.5" {
  108. t.Errorf("unexpected IP for three.two.one '%s'", a)
  109. }
  110. el := "two.one"
  111. m = ex.findFirstLabel(el, []string{"@"}, []uint16{dns.TypeA})
  112. if len(m.Label.Records[dns.TypeA]) > 0 {
  113. t.Errorf("Unexpected A record for '%s'", el)
  114. }
  115. if m.Label.Label != el {
  116. t.Errorf("'%s' label is '%s'", el, m.Label.Label)
  117. }
  118. //verify label isn't overwritten
  119. m = ex.findFirstLabel("one", []string{"@"}, []uint16{dns.TypeA})
  120. if l := len(m.Label.Records[dns.TypeA]); l != 1 {
  121. t.Errorf("Unexpected A record count for 'one' %d, expected 1", l)
  122. }
  123. if a := m.Label.Records[dns.TypeA][0].RR.(*dns.A); a.A.String() != "192.168.1.6" {
  124. t.Errorf("unexpected IP for one '%s'", a)
  125. }
  126. }
  127. func TestExampleOrgZone(t *testing.T) {
  128. mm, err := NewMuxManager("../dns", &NilReg{})
  129. if err != nil {
  130. t.Fatalf("Loading test zones: %s", err)
  131. }
  132. ex, ok := mm.zonelist["test.example.org"]
  133. if !ok || ex == nil || ex.Labels == nil {
  134. t.Fatalf("Did not load 'test.example.org' test zone")
  135. }
  136. matches := ex.FindLabels("sub", []string{"@"}, []uint16{dns.TypeNS})
  137. if matches[0].Type != dns.TypeNS {
  138. t.Fatalf("Expected qtype = NS record (type %d), got type %d", dns.TypeNS, matches[0].Type)
  139. }
  140. Ns := matches[0].Label.Records[matches[0].Type]
  141. if l := len(Ns); l != 2 {
  142. t.Fatalf("Expected 2 NS records, got '%d'", l)
  143. }
  144. }