net_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. "net"
  19. "reflect"
  20. "testing"
  21. "github.com/davecgh/go-spew/spew"
  22. )
  23. func TestParseNetlist(t *testing.T) {
  24. var tests = []struct {
  25. input string
  26. wantErr error
  27. wantList *Netlist
  28. }{
  29. {
  30. input: "",
  31. wantList: &Netlist{},
  32. },
  33. {
  34. input: "127.0.0.0/8",
  35. wantErr: nil,
  36. wantList: &Netlist{{IP: net.IP{127, 0, 0, 0}, Mask: net.CIDRMask(8, 32)}},
  37. },
  38. {
  39. input: "127.0.0.0/44",
  40. wantErr: &net.ParseError{Type: "CIDR address", Text: "127.0.0.0/44"},
  41. },
  42. {
  43. input: "127.0.0.0/16, 23.23.23.23/24,",
  44. wantList: &Netlist{
  45. {IP: net.IP{127, 0, 0, 0}, Mask: net.CIDRMask(16, 32)},
  46. {IP: net.IP{23, 23, 23, 0}, Mask: net.CIDRMask(24, 32)},
  47. },
  48. },
  49. }
  50. for _, test := range tests {
  51. l, err := ParseNetlist(test.input)
  52. if !reflect.DeepEqual(err, test.wantErr) {
  53. t.Errorf("%q: got error %q, want %q", test.input, err, test.wantErr)
  54. continue
  55. }
  56. if !reflect.DeepEqual(l, test.wantList) {
  57. spew.Dump(l)
  58. spew.Dump(test.wantList)
  59. t.Errorf("%q: got %v, want %v", test.input, l, test.wantList)
  60. }
  61. }
  62. }
  63. func TestNilNetListContains(t *testing.T) {
  64. var list *Netlist
  65. checkContains(t, list.Contains, nil, []string{"1.2.3.4"})
  66. }
  67. func TestIsLAN(t *testing.T) {
  68. checkContains(t, IsLAN,
  69. []string{ // included
  70. "0.0.0.0",
  71. "0.2.0.8",
  72. "127.0.0.1",
  73. "10.0.1.1",
  74. "10.22.0.3",
  75. "172.31.252.251",
  76. "192.168.1.4",
  77. "fe80::f4a1:8eff:fec5:9d9d",
  78. "febf::ab32:2233",
  79. "fc00::4",
  80. },
  81. []string{ // excluded
  82. "192.0.2.1",
  83. "1.0.0.0",
  84. "172.32.0.1",
  85. "fec0::2233",
  86. },
  87. )
  88. }
  89. func TestIsSpecialNetwork(t *testing.T) {
  90. checkContains(t, IsSpecialNetwork,
  91. []string{ // included
  92. "192.0.2.1",
  93. "192.0.2.44",
  94. "2001:db8:85a3:8d3:1319:8a2e:370:7348",
  95. "255.255.255.255",
  96. "224.0.0.22", // IPv4 multicast
  97. "ff05::1:3", // IPv6 multicast
  98. },
  99. []string{ // excluded
  100. "192.0.3.1",
  101. "1.0.0.0",
  102. "172.32.0.1",
  103. "fec0::2233",
  104. },
  105. )
  106. }
  107. func checkContains(t *testing.T, fn func(net.IP) bool, inc, exc []string) {
  108. for _, s := range inc {
  109. if !fn(parseIP(s)) {
  110. t.Error("returned false for included address", s)
  111. }
  112. }
  113. for _, s := range exc {
  114. if fn(parseIP(s)) {
  115. t.Error("returned true for excluded address", s)
  116. }
  117. }
  118. }
  119. func parseIP(s string) net.IP {
  120. ip := net.ParseIP(s)
  121. if ip == nil {
  122. panic("invalid " + s)
  123. }
  124. return ip
  125. }
  126. func TestCheckRelayIP(t *testing.T) {
  127. tests := []struct {
  128. sender, addr string
  129. want error
  130. }{
  131. {"127.0.0.1", "0.0.0.0", errUnspecified},
  132. {"192.168.0.1", "0.0.0.0", errUnspecified},
  133. {"23.55.1.242", "0.0.0.0", errUnspecified},
  134. {"127.0.0.1", "255.255.255.255", errSpecial},
  135. {"192.168.0.1", "255.255.255.255", errSpecial},
  136. {"23.55.1.242", "255.255.255.255", errSpecial},
  137. {"192.168.0.1", "127.0.2.19", errLoopback},
  138. {"23.55.1.242", "192.168.0.1", errLAN},
  139. {"127.0.0.1", "127.0.2.19", nil},
  140. {"127.0.0.1", "192.168.0.1", nil},
  141. {"127.0.0.1", "23.55.1.242", nil},
  142. {"192.168.0.1", "192.168.0.1", nil},
  143. {"192.168.0.1", "23.55.1.242", nil},
  144. {"23.55.1.242", "23.55.1.242", nil},
  145. }
  146. for _, test := range tests {
  147. err := CheckRelayIP(parseIP(test.sender), parseIP(test.addr))
  148. if err != test.want {
  149. t.Errorf("%s from %s: got %q, want %q", test.addr, test.sender, err, test.want)
  150. }
  151. }
  152. }
  153. func BenchmarkCheckRelayIP(b *testing.B) {
  154. sender := parseIP("23.55.1.242")
  155. addr := parseIP("23.55.1.2")
  156. for i := 0; i < b.N; i++ {
  157. CheckRelayIP(sender, addr)
  158. }
  159. }