github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/network/p2p/netutil/net_test.go (about) 1 package netutil 2 3 import ( 4 "fmt" 5 "net" 6 "reflect" 7 "testing" 8 "testing/quick" 9 10 "github.com/davecgh/go-spew/spew" 11 ) 12 13 func TestParseNetlist(t *testing.T) { 14 var tests = []struct { 15 input string 16 wantErr error 17 wantList *Netlist 18 }{ 19 { 20 input: "", 21 wantList: &Netlist{}, 22 }, 23 { 24 input: "127.0.0.0/8", 25 wantErr: nil, 26 wantList: &Netlist{{IP: net.IP{127, 0, 0, 0}, Mask: net.CIDRMask(8, 32)}}, 27 }, 28 { 29 input: "127.0.0.0/44", 30 wantErr: &net.ParseError{Type: "CIDR address", Text: "127.0.0.0/44"}, 31 }, 32 { 33 input: "127.0.0.0/16, 23.23.23.23/24,", 34 wantList: &Netlist{ 35 {IP: net.IP{127, 0, 0, 0}, Mask: net.CIDRMask(16, 32)}, 36 {IP: net.IP{23, 23, 23, 0}, Mask: net.CIDRMask(24, 32)}, 37 }, 38 }, 39 } 40 41 for _, test := range tests { 42 l, err := ParseNetlist(test.input) 43 if !reflect.DeepEqual(err, test.wantErr) { 44 t.Errorf("%q: got error %q, want %q", test.input, err, test.wantErr) 45 continue 46 } 47 if !reflect.DeepEqual(l, test.wantList) { 48 spew.Dump(l) 49 spew.Dump(test.wantList) 50 t.Errorf("%q: got %v, want %v", test.input, l, test.wantList) 51 } 52 } 53 } 54 55 func TestNilNetListContains(t *testing.T) { 56 var list *Netlist 57 checkContains(t, list.Contains, nil, []string{"1.2.3.4"}) 58 } 59 60 func TestIsLAN(t *testing.T) { 61 checkContains(t, IsLAN, 62 []string{ 63 "0.0.0.0", 64 "0.2.0.8", 65 "127.0.0.1", 66 "10.0.1.1", 67 "10.22.0.3", 68 "172.31.252.251", 69 "192.168.1.4", 70 "fe80::f4a1:8eff:fec5:9d9d", 71 "febf::ab32:2233", 72 "fc00::4", 73 }, 74 []string{ 75 "192.0.2.1", 76 "1.0.0.0", 77 "172.32.0.1", 78 "fec0::2233", 79 }, 80 ) 81 } 82 83 func TestIsSpecialNetwork(t *testing.T) { 84 checkContains(t, IsSpecialNetwork, 85 []string{ 86 "192.0.2.1", 87 "192.0.2.44", 88 "2001:db8:85a3:8d3:1319:8a2e:370:7348", 89 "255.255.255.255", 90 "224.0.0.22", 91 "ff05::1:3", 92 }, 93 []string{ 94 "192.0.3.1", 95 "1.0.0.0", 96 "172.32.0.1", 97 "fec0::2233", 98 }, 99 ) 100 } 101 102 func checkContains(t *testing.T, fn func(net.IP) bool, inc, exc []string) { 103 for _, s := range inc { 104 if !fn(parseIP(s)) { 105 t.Error("returned false for included address", s) 106 } 107 } 108 for _, s := range exc { 109 if fn(parseIP(s)) { 110 t.Error("returned true for excluded address", s) 111 } 112 } 113 } 114 115 func parseIP(s string) net.IP { 116 ip := net.ParseIP(s) 117 if ip == nil { 118 panic("invalid " + s) 119 } 120 return ip 121 } 122 123 func TestCheckRelayIP(t *testing.T) { 124 tests := []struct { 125 sender, addr string 126 want error 127 }{ 128 {"127.0.0.1", "0.0.0.0", errUnspecified}, 129 {"192.168.0.1", "0.0.0.0", errUnspecified}, 130 {"23.55.1.242", "0.0.0.0", errUnspecified}, 131 {"127.0.0.1", "255.255.255.255", errSpecial}, 132 {"192.168.0.1", "255.255.255.255", errSpecial}, 133 {"23.55.1.242", "255.255.255.255", errSpecial}, 134 {"192.168.0.1", "127.0.2.19", errLoopback}, 135 {"23.55.1.242", "192.168.0.1", errLAN}, 136 137 {"127.0.0.1", "127.0.2.19", nil}, 138 {"127.0.0.1", "192.168.0.1", nil}, 139 {"127.0.0.1", "23.55.1.242", nil}, 140 {"192.168.0.1", "192.168.0.1", nil}, 141 {"192.168.0.1", "23.55.1.242", nil}, 142 {"23.55.1.242", "23.55.1.242", nil}, 143 } 144 145 for _, test := range tests { 146 err := CheckRelayIP(parseIP(test.sender), parseIP(test.addr)) 147 if err != test.want { 148 t.Errorf("%s from %s: got %q, want %q", test.addr, test.sender, err, test.want) 149 } 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 } 160 161 func TestSameNet(t *testing.T) { 162 tests := []struct { 163 ip, other string 164 bits uint 165 want bool 166 }{ 167 {"0.0.0.0", "0.0.0.0", 32, true}, 168 {"0.0.0.0", "0.0.0.1", 0, true}, 169 {"0.0.0.0", "0.0.0.1", 31, true}, 170 {"0.0.0.0", "0.0.0.1", 32, false}, 171 {"0.33.0.1", "0.34.0.2", 8, true}, 172 {"0.33.0.1", "0.34.0.2", 13, true}, 173 {"0.33.0.1", "0.34.0.2", 15, false}, 174 } 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 183 func ExampleSameNet() { 184 185 fmt.Println(SameNet(24, net.IP{127, 0, 0, 1}, net.IP{127, 0, 0, 3})) 186 187 fmt.Println(SameNet(24, net.IP{127, 3, 0, 1}, net.IP{127, 5, 0, 3})) 188 189 } 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 207 {remove: "127.0.0.1"}, 208 {add: "127.0.0.3"}, 209 {add: "127.0.0.3", fails: true}, 210 } 211 212 set := DistinctNetSet{Subnet: 15, Limit: 2} 213 for _, op := range ops { 214 var desc string 215 if op.add != "" { 216 desc = fmt.Sprintf("Add(%s)", op.add) 217 if ok := set.Add(parseIP(op.add)); ok != !op.fails { 218 t.Errorf("%s == %t, want %t", desc, ok, !op.fails) 219 } 220 } else { 221 desc = fmt.Sprintf("Remove(%s)", op.remove) 222 set.Remove(parseIP(op.remove)) 223 } 224 t.Logf("%s: %v", desc, set) 225 } 226 } 227 228 func TestDistinctNetSetAddRemove(t *testing.T) { 229 cfg := &quick.Config{} 230 fn := func(ips []net.IP) bool { 231 s := DistinctNetSet{Limit: 3, Subnet: 2} 232 for _, ip := range ips { 233 s.Add(ip) 234 } 235 for _, ip := range ips { 236 s.Remove(ip) 237 } 238 return s.Len() == 0 239 } 240 241 if err := quick.Check(fn, cfg); err != nil { 242 t.Fatal(err) 243 } 244 }