net_test.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Copyright 2016 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package netutil
  17. import (
  18. "fmt"
  19. "net"
  20. "reflect"
  21. "testing"
  22. "testing/quick"
  23. "github.com/davecgh/go-spew/spew"
  24. )
  25. func TestParseNetlist(t *testing.T) {
  26. var tests = []struct {
  27. input string
  28. wantErr error
  29. wantList *Netlist
  30. }{
  31. {
  32. input: "",
  33. wantList: &Netlist{},
  34. },
  35. {
  36. input: "127.0.0.0/8",
  37. wantErr: nil,
  38. wantList: &Netlist{{IP: net.IP{127, 0, 0, 0}, Mask: net.CIDRMask(8, 32)}},
  39. },
  40. {
  41. input: "127.0.0.0/44",
  42. wantErr: &net.ParseError{Type: "CIDR address", Text: "127.0.0.0/44"},
  43. },
  44. {
  45. input: "127.0.0.0/16, 23.23.23.23/24,",
  46. wantList: &Netlist{
  47. {IP: net.IP{127, 0, 0, 0}, Mask: net.CIDRMask(16, 32)},
  48. {IP: net.IP{23, 23, 23, 0}, Mask: net.CIDRMask(24, 32)},
  49. },
  50. },
  51. }
  52. for _, test := range tests {
  53. l, err := ParseNetlist(test.input)
  54. if !reflect.DeepEqual(err, test.wantErr) {
  55. t.Errorf("%q: got error %q, want %q", test.input, err, test.wantErr)
  56. continue
  57. }
  58. if !reflect.DeepEqual(l, test.wantList) {
  59. spew.Dump(l)
  60. spew.Dump(test.wantList)
  61. t.Errorf("%q: got %v, want %v", test.input, l, test.wantList)
  62. }
  63. }
  64. }
  65. func TestNilNetListContains(t *testing.T) {
  66. var list *Netlist
  67. checkContains(t, list.Contains, nil, []string{"1.2.3.4"})
  68. }
  69. func TestIsLAN(t *testing.T) {
  70. checkContains(t, IsLAN,
  71. []string{ // included
  72. "0.0.0.0",
  73. "0.2.0.8",
  74. "127.0.0.1",
  75. "10.0.1.1",
  76. "10.22.0.3",
  77. "172.31.252.251",
  78. "192.168.1.4",
  79. "fe80::f4a1:8eff:fec5:9d9d",
  80. "febf::ab32:2233",
  81. "fc00::4",
  82. },
  83. []string{ // excluded
  84. "192.0.2.1",
  85. "1.0.0.0",
  86. "172.32.0.1",
  87. "fec0::2233",
  88. },
  89. )
  90. }
  91. func TestIsSpecialNetwork(t *testing.T) {
  92. checkContains(t, IsSpecialNetwork,
  93. []string{ // included
  94. "192.0.2.1",
  95. "192.0.2.44",
  96. "2001:db8:85a3:8d3:1319:8a2e:370:7348",
  97. "255.255.255.255",
  98. "224.0.0.22", // IPv4 multicast
  99. "ff05::1:3", // IPv6 multicast
  100. },
  101. []string{ // excluded
  102. "192.0.3.1",
  103. "1.0.0.0",
  104. "172.32.0.1",
  105. "fec0::2233",
  106. },
  107. )
  108. }
  109. func checkContains(t *testing.T, fn func(net.IP) bool, inc, exc []string) {
  110. for _, s := range inc {
  111. if !fn(parseIP(s)) {
  112. t.Error("returned false for included address", s)
  113. }
  114. }
  115. for _, s := range exc {
  116. if fn(parseIP(s)) {
  117. t.Error("returned true for excluded address", s)
  118. }
  119. }
  120. }
  121. func parseIP(s string) net.IP {
  122. ip := net.ParseIP(s)
  123. if ip == nil {
  124. panic("invalid " + s)
  125. }
  126. return ip
  127. }
  128. func TestCheckRelayIP(t *testing.T) {
  129. tests := []struct {
  130. sender, addr string
  131. want error
  132. }{
  133. {"127.0.0.1", "0.0.0.0", errUnspecified},
  134. {"192.168.0.1", "0.0.0.0", errUnspecified},
  135. {"23.55.1.242", "0.0.0.0", errUnspecified},
  136. {"127.0.0.1", "255.255.255.255", errSpecial},
  137. {"192.168.0.1", "255.255.255.255", errSpecial},
  138. {"23.55.1.242", "255.255.255.255", errSpecial},
  139. {"192.168.0.1", "127.0.2.19", errLoopback},
  140. {"23.55.1.242", "192.168.0.1", errLAN},
  141. {"127.0.0.1", "127.0.2.19", nil},
  142. {"127.0.0.1", "192.168.0.1", nil},
  143. {"127.0.0.1", "23.55.1.242", nil},
  144. {"192.168.0.1", "192.168.0.1", nil},
  145. {"192.168.0.1", "23.55.1.242", nil},
  146. {"23.55.1.242", "23.55.1.242", nil},
  147. }
  148. for _, test := range tests {
  149. err := CheckRelayIP(parseIP(test.sender), parseIP(test.addr))
  150. if err != test.want {
  151. t.Errorf("%s from %s: got %q, want %q", test.addr, test.sender, err, test.want)
  152. }
  153. }
  154. }
  155. func BenchmarkCheckRelayIP(b *testing.B) {
  156. sender := parseIP("23.55.1.242")
  157. addr := parseIP("23.55.1.2")
  158. for i := 0; i < b.N; i++ {
  159. CheckRelayIP(sender, addr)
  160. }
  161. }
  162. func TestSameNet(t *testing.T) {
  163. tests := []struct {
  164. ip, other string
  165. bits uint
  166. want bool
  167. }{
  168. {"0.0.0.0", "0.0.0.0", 32, true},
  169. {"0.0.0.0", "0.0.0.1", 0, true},
  170. {"0.0.0.0", "0.0.0.1", 31, true},
  171. {"0.0.0.0", "0.0.0.1", 32, false},
  172. {"0.33.0.1", "0.34.0.2", 8, true},
  173. {"0.33.0.1", "0.34.0.2", 13, true},
  174. {"0.33.0.1", "0.34.0.2", 15, false},
  175. }
  176. for _, test := range tests {
  177. if ok := SameNet(test.bits, parseIP(test.ip), parseIP(test.other)); ok != test.want {
  178. t.Errorf("SameNet(%d, %s, %s) == %t, want %t", test.bits, test.ip, test.other, ok, test.want)
  179. }
  180. }
  181. }
  182. func ExampleSameNet() {
  183. // This returns true because the IPs are in the same /24 network:
  184. fmt.Println(SameNet(24, net.IP{127, 0, 0, 1}, net.IP{127, 0, 0, 3}))
  185. // This call returns false:
  186. fmt.Println(SameNet(24, net.IP{127, 3, 0, 1}, net.IP{127, 5, 0, 3}))
  187. // Output:
  188. // true
  189. // false
  190. }
  191. func TestDistinctNetSet(t *testing.T) {
  192. ops := []struct {
  193. add, remove string
  194. fails bool
  195. }{
  196. {add: "127.0.0.1"},
  197. {add: "127.0.0.2"},
  198. {add: "127.0.0.3", fails: true},
  199. {add: "127.32.0.1"},
  200. {add: "127.32.0.2"},
  201. {add: "127.32.0.3", fails: true},
  202. {add: "127.33.0.1", fails: true},
  203. {add: "127.34.0.1"},
  204. {add: "127.34.0.2"},
  205. {add: "127.34.0.3", fails: true},
  206. // Make room for an address, then add again.
  207. {remove: "127.0.0.1"},
  208. {add: "127.0.0.3"},
  209. {add: "127.0.0.3", fails: true},
  210. }
  211. set := DistinctNetSet{Subnet: 15, Limit: 2}
  212. for _, op := range ops {
  213. var desc string
  214. if op.add != "" {
  215. desc = fmt.Sprintf("Add(%s)", op.add)
  216. if ok := set.Add(parseIP(op.add)); ok != !op.fails {
  217. t.Errorf("%s == %t, want %t", desc, ok, !op.fails)
  218. }
  219. } else {
  220. desc = fmt.Sprintf("Remove(%s)", op.remove)
  221. set.Remove(parseIP(op.remove))
  222. }
  223. t.Logf("%s: %v", desc, set)
  224. }
  225. }
  226. func TestDistinctNetSetAddRemove(t *testing.T) {
  227. cfg := &quick.Config{}
  228. fn := func(ips []net.IP) bool {
  229. s := DistinctNetSet{Limit: 3, Subnet: 2}
  230. for _, ip := range ips {
  231. s.Add(ip)
  232. }
  233. for _, ip := range ips {
  234. s.Remove(ip)
  235. }
  236. return s.Len() == 0
  237. }
  238. if err := quick.Check(fn, cfg); err != nil {
  239. t.Fatal(err)
  240. }
  241. }