| 1 |
/* |
| 2 |
Copyright 2018 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package net |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"net" |
| 22 |
"testing" |
| 23 |
) |
| 24 |
|
| 25 |
func TestDualStackIPs(t *testing.T) { |
| 26 |
testCases := []struct { |
| 27 |
ips []string |
| 28 |
errMessage string |
| 29 |
expectedResult bool |
| 30 |
expectError bool |
| 31 |
}{ |
| 32 |
{ |
| 33 |
ips: []string{"1.1.1.1"}, |
| 34 |
errMessage: "should fail because length is not at least 2", |
| 35 |
expectedResult: false, |
| 36 |
expectError: false, |
| 37 |
}, |
| 38 |
{ |
| 39 |
ips: []string{}, |
| 40 |
errMessage: "should fail because length is not at least 2", |
| 41 |
expectedResult: false, |
| 42 |
expectError: false, |
| 43 |
}, |
| 44 |
{ |
| 45 |
ips: []string{"1.1.1.1", "2.2.2.2", "3.3.3.3"}, |
| 46 |
errMessage: "should fail because all are v4", |
| 47 |
expectedResult: false, |
| 48 |
expectError: false, |
| 49 |
}, |
| 50 |
{ |
| 51 |
ips: []string{"fd92:20ba:ca:34f7:ffff:ffff:ffff:ffff", "fd92:20ba:ca:34f7:ffff:ffff:ffff:fff0", "fd92:20ba:ca:34f7:ffff:ffff:ffff:fff1"}, |
| 52 |
errMessage: "should fail because all are v6", |
| 53 |
expectedResult: false, |
| 54 |
expectError: false, |
| 55 |
}, |
| 56 |
{ |
| 57 |
ips: []string{"1.1.1.1", "not-a-valid-ip"}, |
| 58 |
errMessage: "should fail because 2nd ip is invalid", |
| 59 |
expectedResult: false, |
| 60 |
expectError: true, |
| 61 |
}, |
| 62 |
{ |
| 63 |
ips: []string{"not-a-valid-ip", "fd92:20ba:ca:34f7:ffff:ffff:ffff:ffff"}, |
| 64 |
errMessage: "should fail because 1st ip is invalid", |
| 65 |
expectedResult: false, |
| 66 |
expectError: true, |
| 67 |
}, |
| 68 |
{ |
| 69 |
ips: []string{"1.1.1.1", "fd92:20ba:ca:34f7:ffff:ffff:ffff:ffff"}, |
| 70 |
errMessage: "expected success, but found failure", |
| 71 |
expectedResult: true, |
| 72 |
expectError: false, |
| 73 |
}, |
| 74 |
{ |
| 75 |
ips: []string{"fd92:20ba:ca:34f7:ffff:ffff:ffff:ffff", "1.1.1.1", "fd92:20ba:ca:34f7:ffff:ffff:ffff:fff0"}, |
| 76 |
errMessage: "expected success, but found failure", |
| 77 |
expectedResult: true, |
| 78 |
expectError: false, |
| 79 |
}, |
| 80 |
{ |
| 81 |
ips: []string{"1.1.1.1", "fd92:20ba:ca:34f7:ffff:ffff:ffff:ffff", "10.0.0.0"}, |
| 82 |
errMessage: "expected success, but found failure", |
| 83 |
expectedResult: true, |
| 84 |
expectError: false, |
| 85 |
}, |
| 86 |
{ |
| 87 |
ips: []string{"fd92:20ba:ca:34f7:ffff:ffff:ffff:ffff", "1.1.1.1"}, |
| 88 |
errMessage: "expected success, but found failure", |
| 89 |
expectedResult: true, |
| 90 |
expectError: false, |
| 91 |
}, |
| 92 |
} |
| 93 |
// for each test case, test the regular func and the string func |
| 94 |
for _, tc := range testCases { |
| 95 |
dualStack, err := IsDualStackIPStrings(tc.ips) |
| 96 |
if err == nil && tc.expectError { |
| 97 |
t.Errorf("%s", tc.errMessage) |
| 98 |
continue |
| 99 |
} |
| 100 |
if err != nil && !tc.expectError { |
| 101 |
t.Errorf("failed to run test case for %v, error: %v", tc.ips, err) |
| 102 |
continue |
| 103 |
} |
| 104 |
if dualStack != tc.expectedResult { |
| 105 |
t.Errorf("%v for %v", tc.errMessage, tc.ips) |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
for _, tc := range testCases { |
| 110 |
ips := make([]net.IP, 0, len(tc.ips)) |
| 111 |
for _, ip := range tc.ips { |
| 112 |
parsedIP := ParseIPSloppy(ip) |
| 113 |
ips = append(ips, parsedIP) |
| 114 |
} |
| 115 |
dualStack, err := IsDualStackIPs(ips) |
| 116 |
if err == nil && tc.expectError { |
| 117 |
t.Errorf("%s", tc.errMessage) |
| 118 |
continue |
| 119 |
} |
| 120 |
if err != nil && !tc.expectError { |
| 121 |
t.Errorf("failed to run test case for %v, error: %v", tc.ips, err) |
| 122 |
continue |
| 123 |
} |
| 124 |
if dualStack != tc.expectedResult { |
| 125 |
t.Errorf("%v for %v", tc.errMessage, tc.ips) |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
func TestDualStackCIDRs(t *testing.T) { |
| 131 |
testCases := []struct { |
| 132 |
cidrs []string |
| 133 |
errMessage string |
| 134 |
expectedResult bool |
| 135 |
expectError bool |
| 136 |
}{ |
| 137 |
{ |
| 138 |
cidrs: []string{"10.10.10.10/8"}, |
| 139 |
errMessage: "should fail because length is not at least 2", |
| 140 |
expectedResult: false, |
| 141 |
expectError: false, |
| 142 |
}, |
| 143 |
{ |
| 144 |
cidrs: []string{}, |
| 145 |
errMessage: "should fail because length is not at least 2", |
| 146 |
expectedResult: false, |
| 147 |
expectError: false, |
| 148 |
}, |
| 149 |
{ |
| 150 |
cidrs: []string{"10.10.10.10/8", "20.20.20.20/8", "30.30.30.30/8"}, |
| 151 |
errMessage: "should fail because all cidrs are v4", |
| 152 |
expectedResult: false, |
| 153 |
expectError: false, |
| 154 |
}, |
| 155 |
{ |
| 156 |
cidrs: []string{"2000::/10", "3000::/10"}, |
| 157 |
errMessage: "should fail because all cidrs are v6", |
| 158 |
expectedResult: false, |
| 159 |
expectError: false, |
| 160 |
}, |
| 161 |
{ |
| 162 |
cidrs: []string{"10.10.10.10/8", "not-a-valid-cidr"}, |
| 163 |
errMessage: "should fail because 2nd cidr is invalid", |
| 164 |
expectedResult: false, |
| 165 |
expectError: true, |
| 166 |
}, |
| 167 |
{ |
| 168 |
cidrs: []string{"not-a-valid-ip", "2000::/10"}, |
| 169 |
errMessage: "should fail because 1st cidr is invalid", |
| 170 |
expectedResult: false, |
| 171 |
expectError: true, |
| 172 |
}, |
| 173 |
{ |
| 174 |
cidrs: []string{"10.10.10.10/8", "2000::/10"}, |
| 175 |
errMessage: "expected success, but found failure", |
| 176 |
expectedResult: true, |
| 177 |
expectError: false, |
| 178 |
}, |
| 179 |
{ |
| 180 |
cidrs: []string{"2000::/10", "10.10.10.10/8"}, |
| 181 |
errMessage: "expected success, but found failure", |
| 182 |
expectedResult: true, |
| 183 |
expectError: false, |
| 184 |
}, |
| 185 |
{ |
| 186 |
cidrs: []string{"2000::/10", "10.10.10.10/8", "3000::/10"}, |
| 187 |
errMessage: "expected success, but found failure", |
| 188 |
expectedResult: true, |
| 189 |
expectError: false, |
| 190 |
}, |
| 191 |
} |
| 192 |
|
| 193 |
// for each test case, test the regular func and the string func |
| 194 |
for _, tc := range testCases { |
| 195 |
dualStack, err := IsDualStackCIDRStrings(tc.cidrs) |
| 196 |
if err == nil && tc.expectError { |
| 197 |
t.Errorf("%s", tc.errMessage) |
| 198 |
continue |
| 199 |
} |
| 200 |
if err != nil && !tc.expectError { |
| 201 |
t.Errorf("failed to run test case for %v, error: %v", tc.cidrs, err) |
| 202 |
continue |
| 203 |
} |
| 204 |
if dualStack != tc.expectedResult { |
| 205 |
t.Errorf("%v for %v", tc.errMessage, tc.cidrs) |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
for _, tc := range testCases { |
| 210 |
cidrs := make([]*net.IPNet, 0, len(tc.cidrs)) |
| 211 |
for _, cidr := range tc.cidrs { |
| 212 |
_, parsedCIDR, _ := ParseCIDRSloppy(cidr) |
| 213 |
cidrs = append(cidrs, parsedCIDR) |
| 214 |
} |
| 215 |
|
| 216 |
dualStack, err := IsDualStackCIDRs(cidrs) |
| 217 |
if err == nil && tc.expectError { |
| 218 |
t.Errorf("%s", tc.errMessage) |
| 219 |
continue |
| 220 |
} |
| 221 |
if err != nil && !tc.expectError { |
| 222 |
t.Errorf("failed to run test case for %v, error: %v", tc.cidrs, err) |
| 223 |
continue |
| 224 |
} |
| 225 |
if dualStack != tc.expectedResult { |
| 226 |
t.Errorf("%v for %v", tc.errMessage, tc.cidrs) |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
func checkOneIPFamily(t *testing.T, ip string, expectedFamily, family IPFamily, isIPv4, isIPv6 bool) { |
| 232 |
t.Helper() |
| 233 |
if family != expectedFamily { |
| 234 |
t.Errorf("Expect %q family %q, got %q", ip, expectedFamily, family) |
| 235 |
} |
| 236 |
if isIPv4 != (expectedFamily == IPv4) { |
| 237 |
t.Errorf("Expect %q ipv4 %v, got %v", ip, expectedFamily == IPv4, isIPv6) |
| 238 |
} |
| 239 |
if isIPv6 != (expectedFamily == IPv6) { |
| 240 |
t.Errorf("Expect %q ipv6 %v, got %v", ip, expectedFamily == IPv6, isIPv6) |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
func TestIPFamilyOf(t *testing.T) { |
| 245 |
// See test cases in ips_test.go |
| 246 |
for _, tc := range goodTestIPs { |
| 247 |
if tc.skipFamily { |
| 248 |
continue |
| 249 |
} |
| 250 |
t.Run(tc.desc, func(t *testing.T) { |
| 251 |
for _, str := range tc.strings { |
| 252 |
family := IPFamilyOfString(str) |
| 253 |
isIPv4 := IsIPv4String(str) |
| 254 |
isIPv6 := IsIPv6String(str) |
| 255 |
checkOneIPFamily(t, str, tc.family, family, isIPv4, isIPv6) |
| 256 |
} |
| 257 |
for _, ip := range tc.ips { |
| 258 |
family := IPFamilyOf(ip) |
| 259 |
isIPv4 := IsIPv4(ip) |
| 260 |
isIPv6 := IsIPv6(ip) |
| 261 |
checkOneIPFamily(t, ip.String(), tc.family, family, isIPv4, isIPv6) |
| 262 |
} |
| 263 |
}) |
| 264 |
} |
| 265 |
|
| 266 |
// See test cases in ips_test.go |
| 267 |
for _, tc := range badTestIPs { |
| 268 |
if tc.skipFamily { |
| 269 |
continue |
| 270 |
} |
| 271 |
t.Run(tc.desc, func(t *testing.T) { |
| 272 |
for _, ip := range tc.ips { |
| 273 |
family := IPFamilyOf(ip) |
| 274 |
isIPv4 := IsIPv4(ip) |
| 275 |
isIPv6 := IsIPv6(ip) |
| 276 |
checkOneIPFamily(t, fmt.Sprintf("%#v", ip), IPFamilyUnknown, family, isIPv4, isIPv6) |
| 277 |
} |
| 278 |
for _, str := range tc.strings { |
| 279 |
family := IPFamilyOfString(str) |
| 280 |
isIPv4 := IsIPv4String(str) |
| 281 |
isIPv6 := IsIPv6String(str) |
| 282 |
checkOneIPFamily(t, str, IPFamilyUnknown, family, isIPv4, isIPv6) |
| 283 |
} |
| 284 |
}) |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
func TestIPFamilyOfCIDR(t *testing.T) { |
| 289 |
// See test cases in ips_test.go |
| 290 |
for _, tc := range goodTestCIDRs { |
| 291 |
if tc.skipFamily { |
| 292 |
continue |
| 293 |
} |
| 294 |
t.Run(tc.desc, func(t *testing.T) { |
| 295 |
for _, str := range tc.strings { |
| 296 |
family := IPFamilyOfCIDRString(str) |
| 297 |
isIPv4 := IsIPv4CIDRString(str) |
| 298 |
isIPv6 := IsIPv6CIDRString(str) |
| 299 |
checkOneIPFamily(t, str, tc.family, family, isIPv4, isIPv6) |
| 300 |
} |
| 301 |
for _, ipnet := range tc.ipnets { |
| 302 |
family := IPFamilyOfCIDR(ipnet) |
| 303 |
isIPv4 := IsIPv4CIDR(ipnet) |
| 304 |
isIPv6 := IsIPv6CIDR(ipnet) |
| 305 |
checkOneIPFamily(t, ipnet.String(), tc.family, family, isIPv4, isIPv6) |
| 306 |
} |
| 307 |
}) |
| 308 |
} |
| 309 |
|
| 310 |
// See test cases in ips_test.go |
| 311 |
for _, tc := range badTestCIDRs { |
| 312 |
if tc.skipFamily { |
| 313 |
continue |
| 314 |
} |
| 315 |
t.Run(tc.desc, func(t *testing.T) { |
| 316 |
for _, ipnet := range tc.ipnets { |
| 317 |
family := IPFamilyOfCIDR(ipnet) |
| 318 |
isIPv4 := IsIPv4CIDR(ipnet) |
| 319 |
isIPv6 := IsIPv6CIDR(ipnet) |
| 320 |
str := "<nil>" |
| 321 |
if ipnet != nil { |
| 322 |
str = fmt.Sprintf("%#v", *ipnet) |
| 323 |
} |
| 324 |
checkOneIPFamily(t, str, IPFamilyUnknown, family, isIPv4, isIPv6) |
| 325 |
} |
| 326 |
for _, str := range tc.strings { |
| 327 |
family := IPFamilyOfCIDRString(str) |
| 328 |
isIPv4 := IsIPv4CIDRString(str) |
| 329 |
isIPv6 := IsIPv6CIDRString(str) |
| 330 |
checkOneIPFamily(t, str, IPFamilyUnknown, family, isIPv4, isIPv6) |
| 331 |
} |
| 332 |
}) |
| 333 |
} |
| 334 |
} |