| 1 |
/* |
| 2 |
Copyright 2025 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 |
"testing" |
| 21 |
) |
| 22 |
|
| 23 |
func TestParseIPSloppy(t *testing.T) { |
| 24 |
// See test cases in ips_test.go |
| 25 |
for _, tc := range goodTestIPs { |
| 26 |
if tc.skipParse { |
| 27 |
continue |
| 28 |
} |
| 29 |
t.Run(tc.desc, func(t *testing.T) { |
| 30 |
for i, str := range tc.strings { |
| 31 |
ip := ParseIPSloppy(str) |
| 32 |
if ip == nil { |
| 33 |
t.Errorf("expected %q to parse, but failed", str) |
| 34 |
} |
| 35 |
if !ip.Equal(tc.ips[0]) { |
| 36 |
t.Errorf("expected string %d %q to parse equal to IP %#v %q but got %#v (%q)", i+1, str, tc.ips[0], tc.ips[0].String(), ip, ip.String()) |
| 37 |
} |
| 38 |
} |
| 39 |
}) |
| 40 |
} |
| 41 |
|
| 42 |
// See test cases in ips_test.go |
| 43 |
for _, tc := range badTestIPs { |
| 44 |
if tc.skipParse { |
| 45 |
continue |
| 46 |
} |
| 47 |
t.Run(tc.desc, func(t *testing.T) { |
| 48 |
for i, ip := range tc.ips { |
| 49 |
errStr := ip.String() |
| 50 |
parsedIP := ParseIPSloppy(errStr) |
| 51 |
if parsedIP != nil { |
| 52 |
t.Errorf("expected IP %d %#v (%q) to not re-parse but got %#v (%q)", i+1, ip, errStr, parsedIP, parsedIP.String()) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
for i, str := range tc.strings { |
| 57 |
ip := ParseIPSloppy(str) |
| 58 |
if ip != nil { |
| 59 |
t.Errorf("expected string %d %q to not parse but got %#v (%q)", i+1, str, ip, ip.String()) |
| 60 |
} |
| 61 |
} |
| 62 |
}) |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
func TestParseCIDRSloppy(t *testing.T) { |
| 67 |
// See test cases in ips_test.go |
| 68 |
for _, tc := range goodTestCIDRs { |
| 69 |
if tc.skipParse { |
| 70 |
continue |
| 71 |
} |
| 72 |
t.Run(tc.desc, func(t *testing.T) { |
| 73 |
for i, str := range tc.strings { |
| 74 |
_, ipnet, err := ParseCIDRSloppy(str) |
| 75 |
if err != nil { |
| 76 |
t.Errorf("expected %q to parse, but got error %v", str, err) |
| 77 |
} |
| 78 |
if ipnet.String() != tc.ipnets[0].String() { |
| 79 |
t.Errorf("expected string %d %q to parse and re-stringify to %q but got %q", i+1, str, tc.ipnets[0].String(), ipnet.String()) |
| 80 |
} |
| 81 |
} |
| 82 |
}) |
| 83 |
} |
| 84 |
|
| 85 |
// See test cases in ips_test.go |
| 86 |
for _, tc := range badTestCIDRs { |
| 87 |
if tc.skipParse { |
| 88 |
continue |
| 89 |
} |
| 90 |
t.Run(tc.desc, func(t *testing.T) { |
| 91 |
for i, ipnet := range tc.ipnets { |
| 92 |
errStr := ipnet.String() |
| 93 |
_, parsedIPNet, err := ParseCIDRSloppy(errStr) |
| 94 |
if err == nil { |
| 95 |
t.Errorf("expected IPNet %d %q to not parse but got %#v (%q)", i+1, errStr, *parsedIPNet, parsedIPNet.String()) |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
for i, str := range tc.strings { |
| 100 |
_, ipnet, err := ParseCIDRSloppy(str) |
| 101 |
if err == nil { |
| 102 |
t.Errorf("expected string %d %q to not parse but got %#v (%q)", i+1, str, *ipnet, ipnet.String()) |
| 103 |
} |
| 104 |
} |
| 105 |
}) |
| 106 |
} |
| 107 |
} |