rehostsfile_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 rehosts
  5. import (
  6. "net"
  7. "strings"
  8. "testing"
  9. "github.com/coredns/coredns/plugin"
  10. )
  11. func newRehostsFile(file string) *RehostsFile {
  12. rh := &RehostsFile{
  13. Origins: []string{"."},
  14. records: nil,
  15. options: newOptions(),
  16. }
  17. rh.records = rh.parse(strings.NewReader(file))
  18. return rh
  19. }
  20. type staticHostEntry struct {
  21. in string
  22. v4 []string
  23. v6 []string
  24. }
  25. var (
  26. hosts = `
  27. #regular
  28. 127.0.0.1 uwu aoa
  29. 1234::cDEf owo
  30. 127.0.0.3 ouo
  31. # wildcard
  32. 127.0.1.1 *.owo.uwu
  33. 127.0.1.2 *.uwu
  34. # regexp
  35. 127.0.2.1 @ go+gle\.com?
  36. 127.0.2.2 @ (porn|git)hub.com
  37. `
  38. overrideHosts = `
  39. 127.0.0.1 google.com t-google.com *.my-google.us
  40. 127.0.0.2 @ .*not-google\.com
  41. 127.0.0.3 *google.com
  42. 127.0.1.1 google.com t-google.com *.my-google.us
  43. 127.0.1.2 @ .*not-google\.com
  44. 127.0.1.3 *google.com
  45. `
  46. singleLineHosts = `127.0.0.1 gato`
  47. ipv4Hosts = `
  48. 127.0.0.1 owo
  49. `
  50. ipv6Hosts = `
  51. BEba::1234 uwu
  52. `
  53. )
  54. var lookupStaticHostTests = []struct {
  55. file string
  56. ents []staticHostEntry
  57. }{
  58. {
  59. hosts,
  60. []staticHostEntry{
  61. {"rawr.", []string{}, []string{}},
  62. {"uwu.", []string{"127.0.0.1"}, []string{}},
  63. {"aoa.", []string{"127.0.0.1"}, []string{}},
  64. {"owo.", []string{}, []string{"1234::cdef"}},
  65. {"ouo.", []string{"127.0.0.3"}, []string{}},
  66. {"ucu.ouo.owo.uwu.", []string{"127.0.1.1"}, []string{}},
  67. {"ouo.owo.uwu.", []string{"127.0.1.1"}, []string{}},
  68. {"aoa.ouo.uwu.", []string{"127.0.1.2"}, []string{}},
  69. {"ouo.uwu.", []string{"127.0.1.2"}, []string{}},
  70. {"gogle.com.", []string{"127.0.2.1"}, []string{}},
  71. {"gogle.co.", []string{"127.0.2.1"}, []string{}},
  72. {"gooooooooooooooooooooooooooooogle.co.", []string{"127.0.2.1"}, []string{}},
  73. {"github.com.", []string{"127.0.2.2"}, []string{}},
  74. {"pornhub.com.", []string{"127.0.2.2"}, []string{}},
  75. },
  76. },
  77. {
  78. overrideHosts,
  79. []staticHostEntry{
  80. {"gle.com.", []string{}, []string{}},
  81. {"google.com.", []string{"127.0.0.1"}, []string{}},
  82. {"t-google.com.", []string{"127.0.0.1"}, []string{}},
  83. {"not.my-google.us.", []string{"127.0.0.1"}, []string{}},
  84. {"why-not-google.com.", []string{"127.0.0.2"}, []string{}},
  85. {"why-google.com.", []string{"127.0.0.3"}, []string{}},
  86. {"not-google.com.", []string{"127.0.0.2"}, []string{}},
  87. },
  88. },
  89. {
  90. singleLineHosts,
  91. []staticHostEntry{
  92. {"gato.", []string{"127.0.0.1"}, []string{}},
  93. },
  94. },
  95. }
  96. func TestLookupHosts(t *testing.T) {
  97. for _, tt := range lookupStaticHostTests {
  98. h := newRehostsFile(tt.file)
  99. for _, ent := range tt.ents {
  100. testHostsCases(t, ent, h)
  101. }
  102. }
  103. }
  104. func testHostsCases(t *testing.T, ent staticHostEntry, rh *RehostsFile) {
  105. ins := []string{ent.in, plugin.Name(ent.in).Normalize(), strings.ToLower(ent.in), strings.ToUpper(ent.in)}
  106. for k, in := range ins {
  107. addrsV4 := rh.LookupStaticHostV4(in)
  108. if len(addrsV4) != len(ent.v4) {
  109. t.Fatalf("%d, LookupStaticHostV4(%s) = %v; want %v", k, in, addrsV4, ent.v4)
  110. }
  111. for i, v4 := range addrsV4 {
  112. if v4.String() != ent.v4[i] {
  113. t.Fatalf("%d, LookupStaticHostV4(%s) = %v; want %v", k, in, addrsV4, ent.v4)
  114. }
  115. }
  116. addrsV6 := rh.LookupStaticHostV6(in)
  117. if len(addrsV6) != len(ent.v6) {
  118. t.Fatalf("%d, LookupStaticHostV6(%s) = %v; want %v", k, in, addrsV6, ent.v6)
  119. }
  120. for i, v6 := range addrsV6 {
  121. if v6.String() != ent.v6[i] {
  122. t.Fatalf("%d, LookupStaticHostV6(%s) = %v; want %v", k, in, addrsV6, ent.v6)
  123. }
  124. }
  125. }
  126. }
  127. func TestHostCacheModification(t *testing.T) {
  128. // Ensure that programs can't modify the internals of the host cache.
  129. // See https://github.com/golang/go/issues/14212.
  130. rh := newRehostsFile(ipv4Hosts)
  131. entry := staticHostEntry{"owo.", []string{"127.0.0.1"}, []string{}}
  132. testHostsCases(t, entry, rh)
  133. // Modify returned address
  134. addrs := rh.LookupStaticHostV4(entry.in)
  135. for i := range addrs {
  136. addrs[i] = net.IPv4zero
  137. }
  138. testHostsCases(t, entry, rh)
  139. rh = newRehostsFile(ipv6Hosts)
  140. entry = staticHostEntry{"uwu.", []string{}, []string{"beba::1234"}}
  141. testHostsCases(t, entry, rh)
  142. // Modify returned address
  143. addrs = rh.LookupStaticHostV6(entry.in)
  144. for i := range addrs {
  145. addrs[i] = net.IPv6zero
  146. }
  147. testHostsCases(t, entry, rh)
  148. }