github.com/erriapo/docker@v1.6.0-rc2/daemon/networkdriver/network_test.go (about) 1 package networkdriver 2 3 import ( 4 "github.com/docker/libcontainer/netlink" 5 "net" 6 "testing" 7 ) 8 9 func TestNonOverlapingNameservers(t *testing.T) { 10 network := &net.IPNet{ 11 IP: []byte{192, 168, 0, 1}, 12 Mask: []byte{255, 255, 255, 0}, 13 } 14 nameservers := []string{ 15 "127.0.0.1/32", 16 } 17 18 if err := CheckNameserverOverlaps(nameservers, network); err != nil { 19 t.Fatal(err) 20 } 21 } 22 23 func TestOverlapingNameservers(t *testing.T) { 24 network := &net.IPNet{ 25 IP: []byte{192, 168, 0, 1}, 26 Mask: []byte{255, 255, 255, 0}, 27 } 28 nameservers := []string{ 29 "192.168.0.1/32", 30 } 31 32 if err := CheckNameserverOverlaps(nameservers, network); err == nil { 33 t.Fatalf("Expected error %s got %s", ErrNetworkOverlapsWithNameservers, err) 34 } 35 } 36 37 func TestCheckRouteOverlaps(t *testing.T) { 38 orig := networkGetRoutesFct 39 defer func() { 40 networkGetRoutesFct = orig 41 }() 42 networkGetRoutesFct = func() ([]netlink.Route, error) { 43 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"} 44 45 routes := []netlink.Route{} 46 for _, addr := range routesData { 47 _, netX, _ := net.ParseCIDR(addr) 48 routes = append(routes, netlink.Route{IPNet: netX}) 49 } 50 return routes, nil 51 } 52 53 _, netX, _ := net.ParseCIDR("172.16.0.1/24") 54 if err := CheckRouteOverlaps(netX); err != nil { 55 t.Fatal(err) 56 } 57 58 _, netX, _ = net.ParseCIDR("10.0.2.0/24") 59 if err := CheckRouteOverlaps(netX); err == nil { 60 t.Fatalf("10.0.2.0/24 and 10.0.2.0 should overlap but it doesn't") 61 } 62 } 63 64 func TestCheckNameserverOverlaps(t *testing.T) { 65 nameservers := []string{"10.0.2.3/32", "192.168.102.1/32"} 66 67 _, netX, _ := net.ParseCIDR("10.0.2.3/32") 68 69 if err := CheckNameserverOverlaps(nameservers, netX); err == nil { 70 t.Fatalf("%s should overlap 10.0.2.3/32 but doesn't", netX) 71 } 72 73 _, netX, _ = net.ParseCIDR("192.168.102.2/32") 74 75 if err := CheckNameserverOverlaps(nameservers, netX); err != nil { 76 t.Fatalf("%s should not overlap %v but it does", netX, nameservers) 77 } 78 } 79 80 func AssertOverlap(CIDRx string, CIDRy string, t *testing.T) { 81 _, netX, _ := net.ParseCIDR(CIDRx) 82 _, netY, _ := net.ParseCIDR(CIDRy) 83 if !NetworkOverlaps(netX, netY) { 84 t.Errorf("%v and %v should overlap", netX, netY) 85 } 86 } 87 88 func AssertNoOverlap(CIDRx string, CIDRy string, t *testing.T) { 89 _, netX, _ := net.ParseCIDR(CIDRx) 90 _, netY, _ := net.ParseCIDR(CIDRy) 91 if NetworkOverlaps(netX, netY) { 92 t.Errorf("%v and %v should not overlap", netX, netY) 93 } 94 } 95 96 func TestNetworkOverlaps(t *testing.T) { 97 //netY starts at same IP and ends within netX 98 AssertOverlap("172.16.0.1/24", "172.16.0.1/25", t) 99 //netY starts within netX and ends at same IP 100 AssertOverlap("172.16.0.1/24", "172.16.0.128/25", t) 101 //netY starts and ends within netX 102 AssertOverlap("172.16.0.1/24", "172.16.0.64/25", t) 103 //netY starts at same IP and ends outside of netX 104 AssertOverlap("172.16.0.1/24", "172.16.0.1/23", t) 105 //netY starts before and ends at same IP of netX 106 AssertOverlap("172.16.1.1/24", "172.16.0.1/23", t) 107 //netY starts before and ends outside of netX 108 AssertOverlap("172.16.1.1/24", "172.16.0.1/22", t) 109 //netY starts and ends before netX 110 AssertNoOverlap("172.16.1.1/25", "172.16.0.1/24", t) 111 //netX starts and ends before netY 112 AssertNoOverlap("172.16.1.1/25", "172.16.2.1/24", t) 113 } 114 115 func TestNetworkRange(t *testing.T) { 116 // Simple class C test 117 _, network, _ := net.ParseCIDR("192.168.0.1/24") 118 first, last := NetworkRange(network) 119 if !first.Equal(net.ParseIP("192.168.0.0")) { 120 t.Error(first.String()) 121 } 122 if !last.Equal(net.ParseIP("192.168.0.255")) { 123 t.Error(last.String()) 124 } 125 126 // Class A test 127 _, network, _ = net.ParseCIDR("10.0.0.1/8") 128 first, last = NetworkRange(network) 129 if !first.Equal(net.ParseIP("10.0.0.0")) { 130 t.Error(first.String()) 131 } 132 if !last.Equal(net.ParseIP("10.255.255.255")) { 133 t.Error(last.String()) 134 } 135 136 // Class A, random IP address 137 _, network, _ = net.ParseCIDR("10.1.2.3/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 // 32bit mask 147 _, network, _ = net.ParseCIDR("10.1.2.3/32") 148 first, last = NetworkRange(network) 149 if !first.Equal(net.ParseIP("10.1.2.3")) { 150 t.Error(first.String()) 151 } 152 if !last.Equal(net.ParseIP("10.1.2.3")) { 153 t.Error(last.String()) 154 } 155 156 // 31bit mask 157 _, network, _ = net.ParseCIDR("10.1.2.3/31") 158 first, last = NetworkRange(network) 159 if !first.Equal(net.ParseIP("10.1.2.2")) { 160 t.Error(first.String()) 161 } 162 if !last.Equal(net.ParseIP("10.1.2.3")) { 163 t.Error(last.String()) 164 } 165 166 // 26bit mask 167 _, network, _ = net.ParseCIDR("10.1.2.3/26") 168 first, last = NetworkRange(network) 169 if !first.Equal(net.ParseIP("10.1.2.0")) { 170 t.Error(first.String()) 171 } 172 if !last.Equal(net.ParseIP("10.1.2.63")) { 173 t.Error(last.String()) 174 } 175 }