hosts_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "reflect"
  7. "strings"
  8. "testing"
  9. )
  10. type staticHostEntry struct {
  11. in string
  12. out []string
  13. }
  14. var lookupStaticHostTests = []struct {
  15. name string
  16. ents []staticHostEntry
  17. }{
  18. {
  19. "testdata/hosts",
  20. []staticHostEntry{
  21. {"odin", []string{"127.0.0.2", "127.0.0.3", "::2"}},
  22. {"thor", []string{"127.1.1.1"}},
  23. {"ullr", []string{"127.1.1.2"}},
  24. {"ullrhost", []string{"127.1.1.2"}},
  25. {"localhost", []string{"fe80::1%lo0"}},
  26. },
  27. },
  28. {
  29. "testdata/singleline-hosts", // see golang.org/issue/6646
  30. []staticHostEntry{
  31. {"odin", []string{"127.0.0.2"}},
  32. },
  33. },
  34. {
  35. "testdata/ipv4-hosts",
  36. []staticHostEntry{
  37. {"localhost", []string{"127.0.0.1", "127.0.0.2", "127.0.0.3"}},
  38. {"localhost.localdomain", []string{"127.0.0.3"}},
  39. },
  40. },
  41. {
  42. "testdata/ipv6-hosts", // see golang.org/issue/8996
  43. []staticHostEntry{
  44. {"localhost", []string{"::1", "fe80::1", "fe80::2%lo0", "fe80::3%lo0"}},
  45. {"localhost.localdomain", []string{"fe80::3%lo0"}},
  46. },
  47. },
  48. {
  49. "testdata/case-hosts", // see golang.org/issue/12806
  50. []staticHostEntry{
  51. {"PreserveMe", []string{"127.0.0.1", "::1"}},
  52. {"PreserveMe.local", []string{"127.0.0.1", "::1"}},
  53. },
  54. },
  55. }
  56. func TestLookupStaticHost(t *testing.T) {
  57. defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
  58. for _, tt := range lookupStaticHostTests {
  59. testHookHostsPath = tt.name
  60. for _, ent := range tt.ents {
  61. testStaticHost(t, tt.name, ent)
  62. }
  63. }
  64. }
  65. func testStaticHost(t *testing.T, hostsPath string, ent staticHostEntry) {
  66. ins := []string{ent.in, absDomainName(ent.in), strings.ToLower(ent.in), strings.ToUpper(ent.in)}
  67. for _, in := range ins {
  68. addrs := lookupStaticHost(in)
  69. if !reflect.DeepEqual(addrs, ent.out) {
  70. t.Errorf("%s, lookupStaticHost(%s) = %v; want %v", hostsPath, in, addrs, ent.out)
  71. }
  72. }
  73. }
  74. var lookupStaticAddrTests = []struct {
  75. name string
  76. ents []staticHostEntry
  77. }{
  78. {
  79. "testdata/hosts",
  80. []staticHostEntry{
  81. {"255.255.255.255", []string{"broadcasthost"}},
  82. {"127.0.0.2", []string{"odin"}},
  83. {"127.0.0.3", []string{"odin"}},
  84. {"::2", []string{"odin"}},
  85. {"127.1.1.1", []string{"thor"}},
  86. {"127.1.1.2", []string{"ullr", "ullrhost"}},
  87. {"fe80::1%lo0", []string{"localhost"}},
  88. },
  89. },
  90. {
  91. "testdata/singleline-hosts", // see golang.org/issue/6646
  92. []staticHostEntry{
  93. {"127.0.0.2", []string{"odin"}},
  94. },
  95. },
  96. {
  97. "testdata/ipv4-hosts",
  98. []staticHostEntry{
  99. {"127.0.0.1", []string{"localhost"}},
  100. {"127.0.0.2", []string{"localhost"}},
  101. {"127.0.0.3", []string{"localhost", "localhost.localdomain"}},
  102. },
  103. },
  104. {
  105. "testdata/ipv6-hosts", // see golang.org/issue/8996
  106. []staticHostEntry{
  107. {"::1", []string{"localhost"}},
  108. {"fe80::1", []string{"localhost"}},
  109. {"fe80::2%lo0", []string{"localhost"}},
  110. {"fe80::3%lo0", []string{"localhost", "localhost.localdomain"}},
  111. },
  112. },
  113. {
  114. "testdata/case-hosts", // see golang.org/issue/12806
  115. []staticHostEntry{
  116. {"127.0.0.1", []string{"PreserveMe", "PreserveMe.local"}},
  117. {"::1", []string{"PreserveMe", "PreserveMe.local"}},
  118. },
  119. },
  120. }
  121. func TestLookupStaticAddr(t *testing.T) {
  122. defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
  123. for _, tt := range lookupStaticAddrTests {
  124. testHookHostsPath = tt.name
  125. for _, ent := range tt.ents {
  126. testStaticAddr(t, tt.name, ent)
  127. }
  128. }
  129. }
  130. func testStaticAddr(t *testing.T, hostsPath string, ent staticHostEntry) {
  131. hosts := lookupStaticAddr(ent.in)
  132. for i := range ent.out {
  133. ent.out[i] = absDomainName(ent.out[i])
  134. }
  135. if !reflect.DeepEqual(hosts, ent.out) {
  136. t.Errorf("%s, lookupStaticAddr(%s) = %v; want %v", hostsPath, ent.in, hosts, ent.out)
  137. }
  138. }
  139. func TestHostCacheModification(t *testing.T) {
  140. // Ensure that programs can't modify the internals of the host cache.
  141. // See https://golang.org/issues/14212.
  142. defer func(orig string) { testHookHostsPath = orig }(testHookHostsPath)
  143. testHookHostsPath = "testdata/ipv4-hosts"
  144. ent := staticHostEntry{"localhost", []string{"127.0.0.1", "127.0.0.2", "127.0.0.3"}}
  145. testStaticHost(t, testHookHostsPath, ent)
  146. // Modify the addresses return by lookupStaticHost.
  147. addrs := lookupStaticHost(ent.in)
  148. for i := range addrs {
  149. addrs[i] += "junk"
  150. }
  151. testStaticHost(t, testHookHostsPath, ent)
  152. testHookHostsPath = "testdata/ipv6-hosts"
  153. ent = staticHostEntry{"::1", []string{"localhost"}}
  154. testStaticAddr(t, testHookHostsPath, ent)
  155. // Modify the hosts return by lookupStaticAddr.
  156. hosts := lookupStaticAddr(ent.in)
  157. for i := range hosts {
  158. hosts[i] += "junk"
  159. }
  160. testStaticAddr(t, testHookHostsPath, ent)
  161. }