rehosts_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package rehosts
  2. import (
  3. "context"
  4. "strings"
  5. "testing"
  6. "github.com/coredns/coredns/plugin/pkg/dnstest"
  7. "github.com/coredns/coredns/plugin/pkg/fall"
  8. "github.com/coredns/coredns/plugin/test"
  9. "github.com/miekg/dns"
  10. )
  11. func TestLookupA(t *testing.T) {
  12. for _, tc := range hostsTestCases {
  13. m := tc.Msg()
  14. var tcFall fall.F
  15. isFall := tc.Qname == "fallthrough.owo."
  16. if isFall {
  17. tcFall = fall.Root
  18. } else {
  19. tcFall = fall.Zero
  20. }
  21. rh := Rehosts{
  22. Next: test.NextHandler(dns.RcodeNameError, nil),
  23. RehostsFile: &RehostsFile{
  24. Origins: []string{"."},
  25. records: nil,
  26. options: newOptions(),
  27. },
  28. Fall: tcFall,
  29. }
  30. rh.records = rh.parse(strings.NewReader(rehostsExample))
  31. rec := dnstest.NewRecorder(&test.ResponseWriter{})
  32. rcode, err := rh.ServeDNS(context.Background(), rec, m)
  33. if err != nil {
  34. t.Errorf("Expected no error, got %v", err)
  35. return
  36. }
  37. if isFall && tc.Rcode != rcode {
  38. t.Errorf("Expected rcode is %d, but got %d", tc.Rcode, rcode)
  39. return
  40. }
  41. if resp := rec.Msg; rec.Msg != nil {
  42. if err := test.SortAndCheck(resp, tc); err != nil {
  43. t.Error(err)
  44. }
  45. }
  46. }
  47. }
  48. var hostsTestCases = []test.Case{
  49. {
  50. Qname: "uwu.", Qtype: dns.TypeA,
  51. Answer: []dns.RR{
  52. test.A("uwu. 3600 IN A 1.2.3.4"),
  53. },
  54. },
  55. {
  56. Qname: "uwu.", Qtype: dns.TypeA,
  57. Answer: []dns.RR{
  58. test.A("uwu. 3600 IN A 1.2.3.4"),
  59. },
  60. },
  61. {
  62. Qname: "gato.", Qtype: dns.TypeAAAA,
  63. Answer: []dns.RR{
  64. test.AAAA("gato. 3600 IN AAAA ::1"),
  65. },
  66. },
  67. {
  68. Qname: "uwu.", Qtype: dns.TypeAAAA,
  69. Answer: []dns.RR{},
  70. },
  71. {
  72. Qname: "uwu.", Qtype: dns.TypeMX,
  73. Answer: []dns.RR{},
  74. },
  75. {
  76. Qname: "fallthrough.owo.", Qtype: dns.TypeAAAA,
  77. Answer: []dns.RR{}, Rcode: dns.RcodeSuccess,
  78. },
  79. }
  80. const rehostsExample = `
  81. 127.0.0.1 gato
  82. ::1 gato my-gato gato.int
  83. 1.2.3.4 uwu
  84. ::FFFF:1.2.3.4 uwu
  85. 4.3.2.1 fallthrough.owo
  86. `