github.com/Heebron/moby@v0.0.0-20221111184709-6eab4f55faf7/libnetwork/netutils/utils_linux_test.go (about) 1 package netutils 2 3 import ( 4 "bytes" 5 "net" 6 "sort" 7 "testing" 8 9 "github.com/docker/docker/libnetwork/ipamutils" 10 "github.com/docker/docker/libnetwork/testutils" 11 "github.com/docker/docker/libnetwork/types" 12 "github.com/vishvananda/netlink" 13 ) 14 15 func TestNonOverlappingNameservers(t *testing.T) { 16 network := &net.IPNet{ 17 IP: []byte{192, 168, 0, 1}, 18 Mask: []byte{255, 255, 255, 0}, 19 } 20 nameservers := []string{ 21 "127.0.0.1/32", 22 } 23 24 if err := CheckNameserverOverlaps(nameservers, network); err != nil { 25 t.Fatal(err) 26 } 27 } 28 29 func TestOverlappingNameservers(t *testing.T) { 30 network := &net.IPNet{ 31 IP: []byte{192, 168, 0, 1}, 32 Mask: []byte{255, 255, 255, 0}, 33 } 34 nameservers := []string{ 35 "192.168.0.1/32", 36 } 37 38 if err := CheckNameserverOverlaps(nameservers, network); err == nil { 39 t.Fatalf("Expected error %s got %s", ErrNetworkOverlapsWithNameservers, err) 40 } 41 } 42 43 func TestCheckRouteOverlaps(t *testing.T) { 44 networkGetRoutesFct = func(netlink.Link, int) ([]netlink.Route, error) { 45 routesData := []string{"10.0.2.0/32", "10.0.3.0/24", "10.0.42.0/24", "172.16.42.0/24", "192.168.142.0/24"} 46 routes := []netlink.Route{} 47 for _, addr := range routesData { 48 _, netX, _ := net.ParseCIDR(addr) 49 routes = append(routes, netlink.Route{Dst: netX, Scope: netlink.SCOPE_LINK}) 50 } 51 // Add a route with a scope which should not overlap 52 _, netX, _ := net.ParseCIDR("10.0.5.0/24") 53 routes = append(routes, netlink.Route{Dst: netX, Scope: netlink.SCOPE_UNIVERSE}) 54 return routes, nil 55 } 56 defer func() { networkGetRoutesFct = nil }() 57 58 _, netX, _ := net.ParseCIDR("172.16.0.1/24") 59 if err := CheckRouteOverlaps(netX); err != nil { 60 t.Fatal(err) 61 } 62 63 _, netX, _ = net.ParseCIDR("10.0.2.0/24") 64 if err := CheckRouteOverlaps(netX); err == nil { 65 t.Fatal("10.0.2.0/24 and 10.0.2.0 should overlap but it doesn't") 66 } 67 68 _, netX, _ = net.ParseCIDR("10.0.5.0/24") 69 if err := CheckRouteOverlaps(netX); err != nil { 70 t.Fatal("10.0.5.0/24 and 10.0.5.0 with scope UNIVERSE should not overlap but it does") 71 } 72 } 73 74 func TestCheckNameserverOverlaps(t *testing.T) { 75 nameservers := []string{"10.0.2.3/32", "192.168.102.1/32"} 76 77 _, netX, _ := net.ParseCIDR("10.0.2.3/32") 78 79 if err := CheckNameserverOverlaps(nameservers, netX); err == nil { 80 t.Fatalf("%s should overlap 10.0.2.3/32 but doesn't", netX) 81 } 82 83 _, netX, _ = net.ParseCIDR("192.168.102.2/32") 84 85 if err := CheckNameserverOverlaps(nameservers, netX); err != nil { 86 t.Fatalf("%s should not overlap %v but it does", netX, nameservers) 87 } 88 } 89 90 func AssertOverlap(CIDRx string, CIDRy string, t *testing.T) { 91 _, netX, _ := net.ParseCIDR(CIDRx) 92 _, netY, _ := net.ParseCIDR(CIDRy) 93 if !NetworkOverlaps(netX, netY) { 94 t.Errorf("%v and %v should overlap", netX, netY) 95 } 96 } 97 98 func AssertNoOverlap(CIDRx string, CIDRy string, t *testing.T) { 99 _, netX, _ := net.ParseCIDR(CIDRx) 100 _, netY, _ := net.ParseCIDR(CIDRy) 101 if NetworkOverlaps(netX, netY) { 102 t.Errorf("%v and %v should not overlap", netX, netY) 103 } 104 } 105 106 func TestNetworkOverlaps(t *testing.T) { 107 //netY starts at same IP and ends within netX 108 AssertOverlap("172.16.0.1/24", "172.16.0.1/25", t) 109 //netY starts within netX and ends at same IP 110 AssertOverlap("172.16.0.1/24", "172.16.0.128/25", t) 111 //netY starts and ends within netX 112 AssertOverlap("172.16.0.1/24", "172.16.0.64/25", t) 113 //netY starts at same IP and ends outside of netX 114 AssertOverlap("172.16.0.1/24", "172.16.0.1/23", t) 115 //netY starts before and ends at same IP of netX 116 AssertOverlap("172.16.1.1/24", "172.16.0.1/23", t) 117 //netY starts before and ends outside of netX 118 AssertOverlap("172.16.1.1/24", "172.16.0.1/22", t) 119 //netY starts and ends before netX 120 AssertNoOverlap("172.16.1.1/25", "172.16.0.1/24", t) 121 //netX starts and ends before netY 122 AssertNoOverlap("172.16.1.1/25", "172.16.2.1/24", t) 123 } 124 125 func TestNetworkRange(t *testing.T) { 126 // Simple class C test 127 _, network, _ := net.ParseCIDR("192.168.0.1/24") 128 first, last := NetworkRange(network) 129 if !first.Equal(net.ParseIP("192.168.0.0")) { 130 t.Error(first.String()) 131 } 132 if !last.Equal(net.ParseIP("192.168.0.255")) { 133 t.Error(last.String()) 134 } 135 136 // Class A test 137 _, network, _ = net.ParseCIDR("10.0.0.1/8") 138 first, last = NetworkRange(network) 139 if !first.Equal(net.ParseIP("10.0.0.0")) { 140 t.Error(first.String()) 141 } 142 if !last.Equal(net.ParseIP("10.255.255.255")) { 143 t.Error(last.String()) 144 } 145 146 // Class A, random IP address 147 _, network, _ = net.ParseCIDR("10.1.2.3/8") 148 first, last = NetworkRange(network) 149 if !first.Equal(net.ParseIP("10.0.0.0")) { 150 t.Error(first.String()) 151 } 152 if !last.Equal(net.ParseIP("10.255.255.255")) { 153 t.Error(last.String()) 154 } 155 156 // 32bit mask 157 _, network, _ = net.ParseCIDR("10.1.2.3/32") 158 first, last = NetworkRange(network) 159 if !first.Equal(net.ParseIP("10.1.2.3")) { 160 t.Error(first.String()) 161 } 162 if !last.Equal(net.ParseIP("10.1.2.3")) { 163 t.Error(last.String()) 164 } 165 166 // 31bit mask 167 _, network, _ = net.ParseCIDR("10.1.2.3/31") 168 first, last = NetworkRange(network) 169 if !first.Equal(net.ParseIP("10.1.2.2")) { 170 t.Error(first.String()) 171 } 172 if !last.Equal(net.ParseIP("10.1.2.3")) { 173 t.Error(last.String()) 174 } 175 176 // 26bit mask 177 _, network, _ = net.ParseCIDR("10.1.2.3/26") 178 first, last = NetworkRange(network) 179 if !first.Equal(net.ParseIP("10.1.2.0")) { 180 t.Error(first.String()) 181 } 182 if !last.Equal(net.ParseIP("10.1.2.63")) { 183 t.Error(last.String()) 184 } 185 } 186 187 // Test veth name generation "veth"+rand (e.g.veth0f60e2c) 188 func TestGenerateRandomName(t *testing.T) { 189 name1, err := GenerateRandomName("veth", 7) 190 if err != nil { 191 t.Fatal(err) 192 } 193 // veth plus generated append equals a len of 11 194 if len(name1) != 11 { 195 t.Fatalf("Expected 11 characters, instead received %d characters", len(name1)) 196 } 197 name2, err := GenerateRandomName("veth", 7) 198 if err != nil { 199 t.Fatal(err) 200 } 201 // Fail if the random generated names equal one another 202 if name1 == name2 { 203 t.Fatalf("Expected differing values but received %s and %s", name1, name2) 204 } 205 } 206 207 // Test mac generation. 208 func TestUtilGenerateRandomMAC(t *testing.T) { 209 mac1 := GenerateRandomMAC() 210 mac2 := GenerateRandomMAC() 211 // ensure bytes are unique 212 if bytes.Equal(mac1, mac2) { 213 t.Fatalf("mac1 %s should not equal mac2 %s", mac1, mac2) 214 } 215 // existing tests check string functionality so keeping the pattern 216 if mac1.String() == mac2.String() { 217 t.Fatalf("mac1 %s should not equal mac2 %s", mac1, mac2) 218 } 219 } 220 221 func TestNetworkRequest(t *testing.T) { 222 defer testutils.SetupTestOSContext(t)() 223 224 nw, err := FindAvailableNetwork(ipamutils.PredefinedLocalScopeDefaultNetworks) 225 if err != nil { 226 t.Fatal(err) 227 } 228 229 var found bool 230 for _, exp := range ipamutils.PredefinedLocalScopeDefaultNetworks { 231 if types.CompareIPNet(exp, nw) { 232 found = true 233 break 234 } 235 } 236 237 if !found { 238 t.Fatalf("Found unexpected broad network %s", nw) 239 } 240 241 nw, err = FindAvailableNetwork(ipamutils.PredefinedGlobalScopeDefaultNetworks) 242 if err != nil { 243 t.Fatal(err) 244 } 245 246 found = false 247 for _, exp := range ipamutils.PredefinedGlobalScopeDefaultNetworks { 248 if types.CompareIPNet(exp, nw) { 249 found = true 250 break 251 } 252 } 253 254 if !found { 255 t.Fatalf("Found unexpected granular network %s", nw) 256 } 257 258 // Add iface and ssert returned address on request 259 createInterface(t, "test", "172.17.42.1/16") 260 261 _, exp, err := net.ParseCIDR("172.18.0.0/16") 262 if err != nil { 263 t.Fatal(err) 264 } 265 nw, err = FindAvailableNetwork(ipamutils.PredefinedLocalScopeDefaultNetworks) 266 if err != nil { 267 t.Fatal(err) 268 } 269 if !types.CompareIPNet(exp, nw) { 270 t.Fatalf("expected %s. got %s", exp, nw) 271 } 272 } 273 274 func TestElectInterfaceAddressMultipleAddresses(t *testing.T) { 275 defer testutils.SetupTestOSContext(t)() 276 277 nws := []string{"172.101.202.254/16", "172.102.202.254/16"} 278 createInterface(t, "test", nws...) 279 280 ipv4NwList, ipv6NwList, err := ElectInterfaceAddresses("test") 281 if err != nil { 282 t.Fatal(err) 283 } 284 285 if len(ipv4NwList) == 0 { 286 t.Fatal("unexpected empty ipv4 network addresses") 287 } 288 289 if len(ipv6NwList) == 0 { 290 t.Fatal("unexpected empty ipv6 network addresses") 291 } 292 293 nwList := []string{} 294 for _, ipv4Nw := range ipv4NwList { 295 nwList = append(nwList, ipv4Nw.String()) 296 } 297 sort.Strings(nws) 298 sort.Strings(nwList) 299 300 if len(nws) != len(nwList) { 301 t.Fatalf("expected %v. got %v", nws, nwList) 302 } 303 for i, nw := range nws { 304 if nw != nwList[i] { 305 t.Fatalf("expected %v. got %v", nw, nwList[i]) 306 } 307 } 308 } 309 310 func TestElectInterfaceAddress(t *testing.T) { 311 defer testutils.SetupTestOSContext(t)() 312 313 nws := "172.101.202.254/16" 314 createInterface(t, "test", nws) 315 316 ipv4Nw, ipv6Nw, err := ElectInterfaceAddresses("test") 317 if err != nil { 318 t.Fatal(err) 319 } 320 321 if len(ipv4Nw) == 0 { 322 t.Fatal("unexpected empty ipv4 network addresses") 323 } 324 325 if len(ipv6Nw) == 0 { 326 t.Fatal("unexpected empty ipv6 network addresses") 327 } 328 329 if nws != ipv4Nw[0].String() { 330 t.Fatalf("expected %s. got %s", nws, ipv4Nw[0]) 331 } 332 } 333 334 func createInterface(t *testing.T, name string, nws ...string) { 335 // Add interface 336 link := &netlink.Bridge{ 337 LinkAttrs: netlink.LinkAttrs{ 338 Name: "test", 339 }, 340 } 341 bips := []*net.IPNet{} 342 for _, nw := range nws { 343 bip, err := types.ParseCIDR(nw) 344 if err != nil { 345 t.Fatal(err) 346 } 347 bips = append(bips, bip) 348 } 349 if err := netlink.LinkAdd(link); err != nil { 350 t.Fatalf("Failed to create interface via netlink: %v", err) 351 } 352 for _, bip := range bips { 353 if err := netlink.AddrAdd(link, &netlink.Addr{IPNet: bip}); err != nil { 354 t.Fatal(err) 355 } 356 } 357 if err := netlink.LinkSetUp(link); err != nil { 358 t.Fatal(err) 359 } 360 }