github.com/Prakhar-Agarwal-byte/moby@v0.0.0-20231027092010-a14e3e8ab87e/libnetwork/types/types_test.go (about) 1 package types 2 3 import ( 4 "net" 5 "testing" 6 ) 7 8 func TestErrorConstructors(t *testing.T) { 9 var err error 10 11 err = InvalidParameterErrorf("Io ho %d uccello", 1) 12 if err.Error() != "Io ho 1 uccello" { 13 t.Fatal(err) 14 } 15 if _, ok := err.(InvalidParameterError); !ok { 16 t.Fatal(err) 17 } 18 if _, ok := err.(MaskableError); ok { 19 t.Fatal(err) 20 } 21 22 err = NotFoundErrorf("Can't find the %s", "keys") 23 if err.Error() != "Can't find the keys" { 24 t.Fatal(err) 25 } 26 if _, ok := err.(NotFoundError); !ok { 27 t.Fatal(err) 28 } 29 if _, ok := err.(MaskableError); ok { 30 t.Fatal(err) 31 } 32 33 err = ForbiddenErrorf("Can't open door %d", 2) 34 if err.Error() != "Can't open door 2" { 35 t.Fatal(err) 36 } 37 if _, ok := err.(ForbiddenError); !ok { 38 t.Fatal(err) 39 } 40 if _, ok := err.(MaskableError); ok { 41 t.Fatal(err) 42 } 43 44 err = NotImplementedErrorf("Functionality %s is not implemented", "x") 45 if err.Error() != "Functionality x is not implemented" { 46 t.Fatal(err) 47 } 48 if _, ok := err.(NotImplementedError); !ok { 49 t.Fatal(err) 50 } 51 if _, ok := err.(MaskableError); ok { 52 t.Fatal(err) 53 } 54 55 err = UnavailableErrorf("Driver %s is not available", "mh") 56 if err.Error() != "Driver mh is not available" { 57 t.Fatal(err) 58 } 59 if _, ok := err.(UnavailableError); !ok { 60 t.Fatal(err) 61 } 62 if _, ok := err.(MaskableError); ok { 63 t.Fatal(err) 64 } 65 66 err = InternalErrorf("Not sure what happened") 67 if err.Error() != "Not sure what happened" { 68 t.Fatal(err) 69 } 70 if _, ok := err.(InternalError); !ok { 71 t.Fatal(err) 72 } 73 if _, ok := err.(MaskableError); ok { 74 t.Fatal(err) 75 } 76 77 err = InternalMaskableErrorf("Minor issue, it can be ignored") 78 if err.Error() != "Minor issue, it can be ignored" { 79 t.Fatal(err) 80 } 81 if _, ok := err.(InternalError); !ok { 82 t.Fatal(err) 83 } 84 if _, ok := err.(MaskableError); !ok { 85 t.Fatal(err) 86 } 87 } 88 89 func TestCompareIPMask(t *testing.T) { 90 input := []struct { 91 ip net.IP 92 mask net.IPMask 93 is int 94 ms int 95 isErr bool 96 }{ 97 { // ip in v4Inv6 representation, mask in v4 representation 98 ip: net.IPv4(172, 28, 30, 1), 99 mask: []byte{0xff, 0xff, 0xff, 0}, 100 is: 12, 101 ms: 0, 102 }, 103 { // ip and mask in v4Inv6 representation 104 ip: net.IPv4(172, 28, 30, 2), 105 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0}, 106 is: 12, 107 ms: 12, 108 }, 109 { // ip in v4 representation, mask in v4Inv6 representation 110 ip: net.IPv4(172, 28, 30, 3)[12:], 111 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0}, 112 is: 0, 113 ms: 12, 114 }, 115 { // ip and mask in v4 representation 116 ip: net.IPv4(172, 28, 30, 4)[12:], 117 mask: []byte{0xff, 0xff, 0xff, 0}, 118 is: 0, 119 ms: 0, 120 }, 121 { // ip and mask as v6 122 ip: net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEAB:00CD"), 123 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0}, 124 is: 0, 125 ms: 0, 126 }, 127 { 128 ip: net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEAB:00CD"), 129 mask: []byte{0xff, 0xff, 0xff, 0}, 130 isErr: true, 131 }, 132 { 133 ip: net.ParseIP("173.32.4.5"), 134 mask: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0}, 135 isErr: true, 136 }, 137 { 138 ip: net.ParseIP("173.32.4.5"), 139 mask: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0}, 140 isErr: true, 141 }, 142 } 143 144 for ind, i := range input { 145 is, ms, err := compareIPMask(i.ip, i.mask) 146 if i.isErr { 147 if err == nil { 148 t.Fatalf("Incorrect error condition for element %d. is: %d, ms: %d, err: %v", ind, is, ms, err) 149 } 150 } else { 151 if i.is != is || i.ms != ms { 152 t.Fatalf("expected is: %d, ms: %d. Got is: %d, ms: %d for element %d", i.is, i.ms, is, ms, ind) 153 } 154 } 155 } 156 } 157 158 func TestUtilGetHostPartIP(t *testing.T) { 159 input := []struct { 160 ip net.IP 161 mask net.IPMask 162 host net.IP 163 err error 164 }{ 165 { // ip in v4Inv6 representation, mask in v4 representation 166 ip: net.IPv4(172, 28, 30, 1), 167 mask: []byte{0xff, 0xff, 0xff, 0}, 168 host: net.IPv4(0, 0, 0, 1), 169 }, 170 { // ip and mask in v4Inv6 representation 171 ip: net.IPv4(172, 28, 30, 2), 172 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0}, 173 host: net.IPv4(0, 0, 0, 2), 174 }, 175 { // ip in v4 representation, mask in v4Inv6 representation 176 ip: net.IPv4(172, 28, 30, 3)[12:], 177 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0}, 178 host: net.IPv4(0, 0, 0, 3)[12:], 179 }, 180 { // ip and mask in v4 representation 181 ip: net.IPv4(172, 28, 30, 4)[12:], 182 mask: []byte{0xff, 0xff, 0xff, 0}, 183 host: net.IPv4(0, 0, 0, 4)[12:], 184 }, 185 { // ip and mask as v6 186 ip: net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEAB:00CD"), 187 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0}, 188 host: net.ParseIP("0::AB:00CD"), 189 }, 190 } 191 192 for _, i := range input { 193 h, err := GetHostPartIP(i.ip, i.mask) 194 if err != nil { 195 t.Fatal(err) 196 } 197 if !i.host.Equal(h) { 198 t.Fatalf("Failed to return expected host ip. Expected: %s. Got: %s", i.host, h) 199 } 200 } 201 202 // ip as v6 and mask as v4 are not compatible 203 if _, err := GetHostPartIP(net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEAB:00CD"), []byte{0xff, 0xff, 0xff, 0}); err == nil { 204 t.Fatalf("Unexpected success") 205 } 206 // ip as v4 and non conventional mask 207 if _, err := GetHostPartIP(net.ParseIP("173.32.4.5"), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0}); err == nil { 208 t.Fatalf("Unexpected success") 209 } 210 // ip as v4 and non conventional mask 211 if _, err := GetHostPartIP(net.ParseIP("173.32.4.5"), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0}); err == nil { 212 t.Fatalf("Unexpected success") 213 } 214 } 215 216 func TestUtilGetBroadcastIP(t *testing.T) { 217 input := []struct { 218 ip net.IP 219 mask net.IPMask 220 bcast net.IP 221 err error 222 }{ 223 // ip in v4Inv6 representation, mask in v4 representation 224 { 225 ip: net.IPv4(172, 28, 30, 1), 226 mask: []byte{0xff, 0xff, 0xff, 0}, 227 bcast: net.IPv4(172, 28, 30, 255), 228 }, 229 { 230 ip: net.IPv4(10, 28, 30, 1), 231 mask: []byte{0xff, 0, 0, 0}, 232 bcast: net.IPv4(10, 255, 255, 255), 233 }, 234 // ip and mask in v4Inv6 representation 235 { 236 ip: net.IPv4(172, 28, 30, 2), 237 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0}, 238 bcast: net.IPv4(172, 28, 30, 255), 239 }, 240 { 241 ip: net.IPv4(172, 28, 30, 2), 242 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0}, 243 bcast: net.IPv4(172, 28, 255, 255), 244 }, 245 // ip in v4 representation, mask in v4Inv6 representation 246 { 247 ip: net.IPv4(172, 28, 30, 3)[12:], 248 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0}, 249 bcast: net.IPv4(172, 28, 30, 255)[12:], 250 }, 251 { 252 ip: net.IPv4(172, 28, 30, 3)[12:], 253 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0}, 254 bcast: net.IPv4(172, 255, 255, 255)[12:], 255 }, 256 // ip and mask in v4 representation 257 { 258 ip: net.IPv4(172, 28, 30, 4)[12:], 259 mask: []byte{0xff, 0xff, 0xff, 0}, 260 bcast: net.IPv4(172, 28, 30, 255)[12:], 261 }, 262 { 263 ip: net.IPv4(172, 28, 30, 4)[12:], 264 mask: []byte{0xff, 0xff, 0, 0}, 265 bcast: net.IPv4(172, 28, 255, 255)[12:], 266 }, 267 { // ip and mask as v6 268 ip: net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEAB:00CD"), 269 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0}, 270 bcast: net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEFF:FFFF"), 271 }, 272 } 273 274 for _, i := range input { 275 h, err := GetBroadcastIP(i.ip, i.mask) 276 if err != nil { 277 t.Fatal(err) 278 } 279 if !i.bcast.Equal(h) { 280 t.Fatalf("Failed to return expected host ip. Expected: %s. Got: %s", i.bcast, h) 281 } 282 } 283 284 // ip as v6 and mask as v4 are not compatible 285 if _, err := GetBroadcastIP(net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEAB:00CD"), []byte{0xff, 0xff, 0xff, 0}); err == nil { 286 t.Fatalf("Unexpected success") 287 } 288 // ip as v4 and non conventional mask 289 if _, err := GetBroadcastIP(net.ParseIP("173.32.4.5"), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0}); err == nil { 290 t.Fatalf("Unexpected success") 291 } 292 // ip as v4 and non conventional mask 293 if _, err := GetBroadcastIP(net.ParseIP("173.32.4.5"), []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0}); err == nil { 294 t.Fatalf("Unexpected success") 295 } 296 } 297 298 func TestParseCIDR(t *testing.T) { 299 input := []struct { 300 cidr string 301 ipnw *net.IPNet 302 }{ 303 {"192.168.22.44/16", &net.IPNet{IP: net.IP{192, 168, 22, 44}, Mask: net.IPMask{255, 255, 0, 0}}}, 304 {"10.10.2.0/24", &net.IPNet{IP: net.IP{10, 10, 2, 0}, Mask: net.IPMask{255, 255, 255, 0}}}, 305 {"10.0.0.100/17", &net.IPNet{IP: net.IP{10, 0, 0, 100}, Mask: net.IPMask{255, 255, 128, 0}}}, 306 } 307 308 for _, i := range input { 309 nw, err := ParseCIDR(i.cidr) 310 if err != nil { 311 t.Fatal(err) 312 } 313 if !CompareIPNet(nw, i.ipnw) { 314 t.Fatalf("network differ. Expected %v. Got: %v", i.ipnw, nw) 315 } 316 } 317 }