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