github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/mergeCode/libnetwork/types/types_test.go (about) 1 package types 2 3 import ( 4 "flag" 5 "net" 6 "testing" 7 ) 8 9 var runningInContainer = flag.Bool("incontainer", false, "Indicates if the test is running in a container") 10 11 func TestTransportPortConv(t *testing.T) { 12 sform := "tcp/23" 13 tp := &TransportPort{Proto: TCP, Port: uint16(23)} 14 15 if sform != tp.String() { 16 t.Fatalf("String() method failed") 17 } 18 19 rc := new(TransportPort) 20 if err := rc.FromString(sform); err != nil { 21 t.Fatal(err) 22 } 23 if !tp.Equal(rc) { 24 t.Fatalf("FromString() method failed") 25 } 26 } 27 28 func TestTransportPortBindingConv(t *testing.T) { 29 sform := "tcp/172.28.30.23:80/112.0.43.56:8001" 30 pb := &PortBinding{ 31 Proto: TCP, 32 IP: net.IPv4(172, 28, 30, 23), 33 Port: uint16(80), 34 HostIP: net.IPv4(112, 0, 43, 56), 35 HostPort: uint16(8001), 36 } 37 38 rc := new(PortBinding) 39 if err := rc.FromString(sform); err != nil { 40 t.Fatal(err) 41 } 42 if !pb.Equal(rc) { 43 t.Fatalf("FromString() method failed") 44 } 45 } 46 47 func TestErrorConstructors(t *testing.T) { 48 var err error 49 50 err = BadRequestErrorf("Io ho %d uccello", 1) 51 if err.Error() != "Io ho 1 uccello" { 52 t.Fatal(err) 53 } 54 if _, ok := err.(BadRequestError); !ok { 55 t.Fatal(err) 56 } 57 if _, ok := err.(MaskableError); ok { 58 t.Fatal(err) 59 } 60 61 err = RetryErrorf("Incy wincy %s went up the spout again", "spider") 62 if err.Error() != "Incy wincy spider went up the spout again" { 63 t.Fatal(err) 64 } 65 if _, ok := err.(RetryError); !ok { 66 t.Fatal(err) 67 } 68 if _, ok := err.(MaskableError); ok { 69 t.Fatal(err) 70 } 71 72 err = NotFoundErrorf("Can't find the %s", "keys") 73 if err.Error() != "Can't find the keys" { 74 t.Fatal(err) 75 } 76 if _, ok := err.(NotFoundError); !ok { 77 t.Fatal(err) 78 } 79 if _, ok := err.(MaskableError); ok { 80 t.Fatal(err) 81 } 82 83 err = ForbiddenErrorf("Can't open door %d", 2) 84 if err.Error() != "Can't open door 2" { 85 t.Fatal(err) 86 } 87 if _, ok := err.(ForbiddenError); !ok { 88 t.Fatal(err) 89 } 90 if _, ok := err.(MaskableError); ok { 91 t.Fatal(err) 92 } 93 94 err = NotImplementedErrorf("Functionality %s is not implemented", "x") 95 if err.Error() != "Functionality x is not implemented" { 96 t.Fatal(err) 97 } 98 if _, ok := err.(NotImplementedError); !ok { 99 t.Fatal(err) 100 } 101 if _, ok := err.(MaskableError); ok { 102 t.Fatal(err) 103 } 104 105 err = TimeoutErrorf("Process %s timed out", "abc") 106 if err.Error() != "Process abc timed out" { 107 t.Fatal(err) 108 } 109 if _, ok := err.(TimeoutError); !ok { 110 t.Fatal(err) 111 } 112 if _, ok := err.(MaskableError); ok { 113 t.Fatal(err) 114 } 115 116 err = NoServiceErrorf("Driver %s is not available", "mh") 117 if err.Error() != "Driver mh is not available" { 118 t.Fatal(err) 119 } 120 if _, ok := err.(NoServiceError); !ok { 121 t.Fatal(err) 122 } 123 if _, ok := err.(MaskableError); ok { 124 t.Fatal(err) 125 } 126 127 err = InternalErrorf("Not sure what happened") 128 if err.Error() != "Not sure what happened" { 129 t.Fatal(err) 130 } 131 if _, ok := err.(InternalError); !ok { 132 t.Fatal(err) 133 } 134 if _, ok := err.(MaskableError); ok { 135 t.Fatal(err) 136 } 137 138 err = InternalMaskableErrorf("Minor issue, it can be ignored") 139 if err.Error() != "Minor issue, it can be ignored" { 140 t.Fatal(err) 141 } 142 if _, ok := err.(InternalError); !ok { 143 t.Fatal(err) 144 } 145 if _, ok := err.(MaskableError); !ok { 146 t.Fatal(err) 147 } 148 } 149 150 func TestCompareIPMask(t *testing.T) { 151 input := []struct { 152 ip net.IP 153 mask net.IPMask 154 is int 155 ms int 156 isErr bool 157 }{ 158 { // ip in v4Inv6 representation, mask in v4 representation 159 ip: net.IPv4(172, 28, 30, 1), 160 mask: []byte{0xff, 0xff, 0xff, 0}, 161 is: 12, 162 ms: 0, 163 }, 164 { // ip and mask in v4Inv6 representation 165 ip: net.IPv4(172, 28, 30, 2), 166 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0}, 167 is: 12, 168 ms: 12, 169 }, 170 { // ip in v4 representation, mask in v4Inv6 representation 171 ip: net.IPv4(172, 28, 30, 3)[12:], 172 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0}, 173 is: 0, 174 ms: 12, 175 }, 176 { // ip and mask in v4 representation 177 ip: net.IPv4(172, 28, 30, 4)[12:], 178 mask: []byte{0xff, 0xff, 0xff, 0}, 179 is: 0, 180 ms: 0, 181 }, 182 { // ip and mask as v6 183 ip: net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEAB:00CD"), 184 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0}, 185 is: 0, 186 ms: 0, 187 }, 188 { 189 ip: net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEAB:00CD"), 190 mask: []byte{0xff, 0xff, 0xff, 0}, 191 isErr: true, 192 }, 193 { 194 ip: net.ParseIP("173.32.4.5"), 195 mask: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0}, 196 isErr: true, 197 }, 198 { 199 ip: net.ParseIP("173.32.4.5"), 200 mask: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0xff, 0}, 201 isErr: true, 202 }, 203 } 204 205 for ind, i := range input { 206 is, ms, err := compareIPMask(i.ip, i.mask) 207 if i.isErr { 208 if err == nil { 209 t.Fatalf("Incorrect error condition for element %d. is: %d, ms: %d, err: %v", ind, is, ms, err) 210 } 211 } else { 212 if i.is != is || i.ms != ms { 213 t.Fatalf("expected is: %d, ms: %d. Got is: %d, ms: %d for element %d", i.is, i.ms, is, ms, ind) 214 } 215 } 216 } 217 } 218 219 func TestUtilGetHostPortionIP(t *testing.T) { 220 input := []struct { 221 ip net.IP 222 mask net.IPMask 223 host net.IP 224 err error 225 }{ 226 { // ip in v4Inv6 representation, mask in v4 representation 227 ip: net.IPv4(172, 28, 30, 1), 228 mask: []byte{0xff, 0xff, 0xff, 0}, 229 host: net.IPv4(0, 0, 0, 1), 230 }, 231 { // ip and mask in v4Inv6 representation 232 ip: net.IPv4(172, 28, 30, 2), 233 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0}, 234 host: net.IPv4(0, 0, 0, 2), 235 }, 236 { // ip in v4 representation, mask in v4Inv6 representation 237 ip: net.IPv4(172, 28, 30, 3)[12:], 238 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0}, 239 host: net.IPv4(0, 0, 0, 3)[12:], 240 }, 241 { // ip and mask in v4 representation 242 ip: net.IPv4(172, 28, 30, 4)[12:], 243 mask: []byte{0xff, 0xff, 0xff, 0}, 244 host: net.IPv4(0, 0, 0, 4)[12:], 245 }, 246 { // ip and mask as v6 247 ip: net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEAB:00CD"), 248 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0}, 249 host: net.ParseIP("0::AB:00CD"), 250 }, 251 } 252 253 for _, i := range input { 254 h, err := GetHostPartIP(i.ip, i.mask) 255 if err != nil { 256 t.Fatal(err) 257 } 258 if !i.host.Equal(h) { 259 t.Fatalf("Failed to return expected host ip. Expected: %s. Got: %s", i.host, h) 260 } 261 } 262 263 // ip as v6 and mask as v4 are not compatible 264 if _, err := GetHostPartIP(net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEAB:00CD"), []byte{0xff, 0xff, 0xff, 0}); err == nil { 265 t.Fatalf("Unexpected success") 266 } 267 // ip as v4 and non conventional mask 268 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 { 269 t.Fatalf("Unexpected success") 270 } 271 // ip as v4 and non conventional mask 272 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 { 273 t.Fatalf("Unexpected success") 274 } 275 } 276 277 func TestUtilGetBroadcastIP(t *testing.T) { 278 input := []struct { 279 ip net.IP 280 mask net.IPMask 281 bcast net.IP 282 err error 283 }{ 284 // ip in v4Inv6 representation, mask in v4 representation 285 { 286 ip: net.IPv4(172, 28, 30, 1), 287 mask: []byte{0xff, 0xff, 0xff, 0}, 288 bcast: net.IPv4(172, 28, 30, 255), 289 }, 290 { 291 ip: net.IPv4(10, 28, 30, 1), 292 mask: []byte{0xff, 0, 0, 0}, 293 bcast: net.IPv4(10, 255, 255, 255), 294 }, 295 // ip and mask in v4Inv6 representation 296 { 297 ip: net.IPv4(172, 28, 30, 2), 298 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0}, 299 bcast: net.IPv4(172, 28, 30, 255), 300 }, 301 { 302 ip: net.IPv4(172, 28, 30, 2), 303 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0}, 304 bcast: net.IPv4(172, 28, 255, 255), 305 }, 306 // ip in v4 representation, mask in v4Inv6 representation 307 { 308 ip: net.IPv4(172, 28, 30, 3)[12:], 309 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0}, 310 bcast: net.IPv4(172, 28, 30, 255)[12:], 311 }, 312 { 313 ip: net.IPv4(172, 28, 30, 3)[12:], 314 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0}, 315 bcast: net.IPv4(172, 255, 255, 255)[12:], 316 }, 317 // ip and mask in v4 representation 318 { 319 ip: net.IPv4(172, 28, 30, 4)[12:], 320 mask: []byte{0xff, 0xff, 0xff, 0}, 321 bcast: net.IPv4(172, 28, 30, 255)[12:], 322 }, 323 { 324 ip: net.IPv4(172, 28, 30, 4)[12:], 325 mask: []byte{0xff, 0xff, 0, 0}, 326 bcast: net.IPv4(172, 28, 255, 255)[12:], 327 }, 328 { // ip and mask as v6 329 ip: net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEAB:00CD"), 330 mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0, 0, 0}, 331 bcast: net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEFF:FFFF"), 332 }, 333 } 334 335 for _, i := range input { 336 h, err := GetBroadcastIP(i.ip, i.mask) 337 if err != nil { 338 t.Fatal(err) 339 } 340 if !i.bcast.Equal(h) { 341 t.Fatalf("Failed to return expected host ip. Expected: %s. Got: %s", i.bcast, h) 342 } 343 } 344 345 // ip as v6 and mask as v4 are not compatible 346 if _, err := GetBroadcastIP(net.ParseIP("2001:DB8:2002:2001:FFFF:ABCD:EEAB:00CD"), []byte{0xff, 0xff, 0xff, 0}); err == nil { 347 t.Fatalf("Unexpected success") 348 } 349 // ip as v4 and non conventional mask 350 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 { 351 t.Fatalf("Unexpected success") 352 } 353 // ip as v4 and non conventional mask 354 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 { 355 t.Fatalf("Unexpected success") 356 } 357 } 358 359 func TestParseCIDR(t *testing.T) { 360 input := []struct { 361 cidr string 362 ipnw *net.IPNet 363 }{ 364 {"192.168.22.44/16", &net.IPNet{IP: net.IP{192, 168, 22, 44}, Mask: net.IPMask{255, 255, 0, 0}}}, 365 {"10.10.2.0/24", &net.IPNet{IP: net.IP{10, 10, 2, 0}, Mask: net.IPMask{255, 255, 255, 0}}}, 366 {"10.0.0.100/17", &net.IPNet{IP: net.IP{10, 0, 0, 100}, Mask: net.IPMask{255, 255, 128, 0}}}, 367 } 368 369 for _, i := range input { 370 nw, err := ParseCIDR(i.cidr) 371 if err != nil { 372 t.Fatal(err) 373 } 374 if !CompareIPNet(nw, i.ipnw) { 375 t.Fatalf("network differ. Expected %v. Got: %v", i.ipnw, nw) 376 } 377 } 378 }