Debian Codegraph @

libgo/go/net/netip/fuzz_test.go

raw
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build ignore_due_to_generics
6
7 package netip_test
8
9 import (
10 "bytes"
11 "encoding"
12 "fmt"
13 "net"
14 . "net/netip"
15 "reflect"
16 "strings"
17 "testing"
18 )
19
20 var corpus = []string{
21 // Basic zero IPv4 address.
22 "0.0.0.0",
23 // Basic non-zero IPv4 address.
24 "192.168.140.255",
25 // IPv4 address in windows-style "print all the digits" form.
26 "010.000.015.001",
27 // IPv4 address with a silly amount of leading zeros.
28 "000001.00000002.00000003.000000004",
29 // 4-in-6 with octet with leading zero
30 "::ffff:1.2.03.4",
31 // Basic zero IPv6 address.
32 "::",
33 // Localhost IPv6.
34 "::1",
35 // Fully expanded IPv6 address.
36 "fd7a:115c:a1e0:ab12:4843:cd96:626b:430b",
37 // IPv6 with elided fields in the middle.
38 "fd7a:115c::626b:430b",
39 // IPv6 with elided fields at the end.
40 "fd7a:115c:a1e0:ab12:4843:cd96::",
41 // IPv6 with single elided field at the end.
42 "fd7a:115c:a1e0:ab12:4843:cd96:626b::",
43 "fd7a:115c:a1e0:ab12:4843:cd96:626b:0",
44 // IPv6 with single elided field in the middle.
45 "fd7a:115c:a1e0::4843:cd96:626b:430b",
46 "fd7a:115c:a1e0:0:4843:cd96:626b:430b",
47 // IPv6 with the trailing 32 bits written as IPv4 dotted decimal. (4in6)
48 "::ffff:192.168.140.255",
49 "::ffff:192.168.140.255",
50 // IPv6 with a zone specifier.
51 "fd7a:115c:a1e0:ab12:4843:cd96:626b:430b%eth0",
52 // IPv6 with dotted decimal and zone specifier.
53 "1:2::ffff:192.168.140.255%eth1",
54 "1:2::ffff:c0a8:8cff%eth1",
55 // IPv6 with capital letters.
56 "FD9E:1A04:F01D::1",
57 "fd9e:1a04:f01d::1",
58 // Empty string.
59 "",
60 // Garbage non-IP.
61 "bad",
62 // Single number. Some parsers accept this as an IPv4 address in
63 // big-endian uint32 form, but we don't.
64 "1234",
65 // IPv4 with a zone specifier.
66 "1.2.3.4%eth0",
67 // IPv4 field must have at least one digit.
68 ".1.2.3",
69 "1.2.3.",
70 "1..2.3",
71 // IPv4 address too long.
72 "1.2.3.4.5",
73 // IPv4 in dotted octal form.
74 "0300.0250.0214.0377",
75 // IPv4 in dotted hex form.
76 "0xc0.0xa8.0x8c.0xff",
77 // IPv4 in class B form.
78 "192.168.12345",
79 // IPv4 in class B form, with a small enough number to be
80 // parseable as a regular dotted decimal field.
81 "127.0.1",
82 // IPv4 in class A form.
83 "192.1234567",
84 // IPv4 in class A form, with a small enough number to be
85 // parseable as a regular dotted decimal field.
86 "127.1",
87 // IPv4 field has value >255.
88 "192.168.300.1",
89 // IPv4 with too many fields.
90 "192.168.0.1.5.6",
91 // IPv6 with not enough fields.
92 "1:2:3:4:5:6:7",
93 // IPv6 with too many fields.
94 "1:2:3:4:5:6:7:8:9",
95 // IPv6 with 8 fields and a :: expander.
96 "1:2:3:4::5:6:7:8",
97 // IPv6 with a field bigger than 2b.
98 "fe801::1",
99 // IPv6 with non-hex values in field.
100 "fe80:tail:scal:e::",
101 // IPv6 with a zone delimiter but no zone.
102 "fe80::1%",
103 // IPv6 with a zone specifier of zero.
104 "::ffff:0:0%0",
105 // IPv6 (without ellipsis) with too many fields for trailing embedded IPv4.
106 "ffff:ffff:ffff:ffff:ffff:ffff:ffff:192.168.140.255",
107 // IPv6 (with ellipsis) with too many fields for trailing embedded IPv4.
108 "ffff::ffff:ffff:ffff:ffff:ffff:ffff:192.168.140.255",
109 // IPv6 with invalid embedded IPv4.
110 "::ffff:192.168.140.bad",
111 // IPv6 with multiple ellipsis ::.
112 "fe80::1::1",
113 // IPv6 with invalid non hex/colon character.
114 "fe80:1?:1",
115 // IPv6 with truncated bytes after single colon.
116 "fe80:",
117 // AddrPort strings.
118 "1.2.3.4:51820",
119 "[fd7a:115c:a1e0:ab12:4843:cd96:626b:430b]:80",
120 "[::ffff:c000:0280]:65535",
121 "[::ffff:c000:0280%eth0]:1",
122 // Prefix strings.
123 "1.2.3.4/24",
124 "fd7a:115c:a1e0:ab12:4843:cd96:626b:430b/118",
125 "::ffff:c000:0280/96",
126 "::ffff:c000:0280%eth0/37",
127 }
128
129 func FuzzParse(f *testing.F) {
130 for _, seed := range corpus {
131 f.Add(seed)
132 }
133
134 f.Fuzz(func(t *testing.T, s string) {
135 ip, _ := ParseAddr(s)
136 checkStringParseRoundTrip(t, ip, ParseAddr)
137 checkEncoding(t, ip)
138
139 // Check that we match the net's IP parser, modulo zones.
140 if !strings.Contains(s, "%") {
141 stdip := net.ParseIP(s)
142 if !ip.IsValid() != (stdip == nil) {
143 t.Errorf("ParseAddr zero != net.ParseIP nil: ip=%q stdip=%q", ip, stdip)
144 }
145
146 if ip.IsValid() && !ip.Is4In6() {
147 buf, err := ip.MarshalText()
148 if err != nil {
149 t.Fatal(err)
150 }
151 buf2, err := stdip.MarshalText()
152 if err != nil {
153 t.Fatal(err)
154 }
155 if !bytes.Equal(buf, buf2) {
156 t.Errorf("Addr.MarshalText() != net.IP.MarshalText(): ip=%q stdip=%q", ip, stdip)
157 }
158 if ip.String() != stdip.String() {
159 t.Errorf("Addr.String() != net.IP.String(): ip=%q stdip=%q", ip, stdip)
160 }
161 if ip.IsGlobalUnicast() != stdip.IsGlobalUnicast() {
162 t.Errorf("Addr.IsGlobalUnicast() != net.IP.IsGlobalUnicast(): ip=%q stdip=%q", ip, stdip)
163 }
164 if ip.IsInterfaceLocalMulticast() != stdip.IsInterfaceLocalMulticast() {
165 t.Errorf("Addr.IsInterfaceLocalMulticast() != net.IP.IsInterfaceLocalMulticast(): ip=%q stdip=%q", ip, stdip)
166 }
167 if ip.IsLinkLocalMulticast() != stdip.IsLinkLocalMulticast() {
168 t.Errorf("Addr.IsLinkLocalMulticast() != net.IP.IsLinkLocalMulticast(): ip=%q stdip=%q", ip, stdip)
169 }
170 if ip.IsLinkLocalUnicast() != stdip.IsLinkLocalUnicast() {
171 t.Errorf("Addr.IsLinkLocalUnicast() != net.IP.IsLinkLocalUnicast(): ip=%q stdip=%q", ip, stdip)
172 }
173 if ip.IsLoopback() != stdip.IsLoopback() {
174 t.Errorf("Addr.IsLoopback() != net.IP.IsLoopback(): ip=%q stdip=%q", ip, stdip)
175 }
176 if ip.IsMulticast() != stdip.IsMulticast() {
177 t.Errorf("Addr.IsMulticast() != net.IP.IsMulticast(): ip=%q stdip=%q", ip, stdip)
178 }
179 if ip.IsPrivate() != stdip.IsPrivate() {
180 t.Errorf("Addr.IsPrivate() != net.IP.IsPrivate(): ip=%q stdip=%q", ip, stdip)
181 }
182 if ip.IsUnspecified() != stdip.IsUnspecified() {
183 t.Errorf("Addr.IsUnspecified() != net.IP.IsUnspecified(): ip=%q stdip=%q", ip, stdip)
184 }
185 }
186 }
187
188 // Check that .Next().Prev() and .Prev().Next() preserve the IP.
189 if ip.IsValid() && ip.Next().IsValid() && ip.Next().Prev() != ip {
190 t.Errorf(".Next.Prev did not round trip: ip=%q .next=%q .next.prev=%q", ip, ip.Next(), ip.Next().Prev())
191 }
192 if ip.IsValid() && ip.Prev().IsValid() && ip.Prev().Next() != ip {
193 t.Errorf(".Prev.Next did not round trip: ip=%q .prev=%q .prev.next=%q", ip, ip.Prev(), ip.Prev().Next())
194 }
195
196 port, err := ParseAddrPort(s)
197 if err == nil {
198 checkStringParseRoundTrip(t, port, ParseAddrPort)
199 checkEncoding(t, port)
200 }
201 port = AddrPortFrom(ip, 80)
202 checkStringParseRoundTrip(t, port, ParseAddrPort)
203 checkEncoding(t, port)
204
205 ipp, err := ParsePrefix(s)
206 if err == nil {
207 checkStringParseRoundTrip(t, ipp, ParsePrefix)
208 checkEncoding(t, ipp)
209 }
210 ipp = PrefixFrom(ip, 8)
211 checkStringParseRoundTrip(t, ipp, ParsePrefix)
212 checkEncoding(t, ipp)
213 })
214 }
215
216 // checkTextMarshaler checks that x's MarshalText and UnmarshalText functions round trip correctly.
217 func checkTextMarshaler(t *testing.T, x encoding.TextMarshaler) {
218 buf, err := x.MarshalText()
219 if err != nil {
220 t.Fatal(err)
221 }
222 y := reflect.New(reflect.TypeOf(x)).Interface().(encoding.TextUnmarshaler)
223 err = y.UnmarshalText(buf)
224 if err != nil {
225 t.Logf("(%v).MarshalText() = %q", x, buf)
226 t.Fatalf("(%T).UnmarshalText(%q) = %v", y, buf, err)
227 }
228 e := reflect.ValueOf(y).Elem().Interface()
229 if !reflect.DeepEqual(x, e) {
230 t.Logf("(%v).MarshalText() = %q", x, buf)
231 t.Logf("(%T).UnmarshalText(%q) = %v", y, buf, y)
232 t.Fatalf("MarshalText/UnmarshalText failed to round trip: %#v != %#v", x, e)
233 }
234 buf2, err := y.(encoding.TextMarshaler).MarshalText()
235 if err != nil {
236 t.Logf("(%v).MarshalText() = %q", x, buf)
237 t.Logf("(%T).UnmarshalText(%q) = %v", y, buf, y)
238 t.Fatalf("failed to MarshalText a second time: %v", err)
239 }
240 if !bytes.Equal(buf, buf2) {
241 t.Logf("(%v).MarshalText() = %q", x, buf)
242 t.Logf("(%T).UnmarshalText(%q) = %v", y, buf, y)
243 t.Logf("(%v).MarshalText() = %q", y, buf2)
244 t.Fatalf("second MarshalText differs from first: %q != %q", buf, buf2)
245 }
246 }
247
248 // checkBinaryMarshaler checks that x's MarshalText and UnmarshalText functions round trip correctly.
249 func checkBinaryMarshaler(t *testing.T, x encoding.BinaryMarshaler) {
250 buf, err := x.MarshalBinary()
251 if err != nil {
252 t.Fatal(err)
253 }
254 y := reflect.New(reflect.TypeOf(x)).Interface().(encoding.BinaryUnmarshaler)
255 err = y.UnmarshalBinary(buf)
256 if err != nil {
257 t.Logf("(%v).MarshalBinary() = %q", x, buf)
258 t.Fatalf("(%T).UnmarshalBinary(%q) = %v", y, buf, err)
259 }
260 e := reflect.ValueOf(y).Elem().Interface()
261 if !reflect.DeepEqual(x, e) {
262 t.Logf("(%v).MarshalBinary() = %q", x, buf)
263 t.Logf("(%T).UnmarshalBinary(%q) = %v", y, buf, y)
264 t.Fatalf("MarshalBinary/UnmarshalBinary failed to round trip: %#v != %#v", x, e)
265 }
266 buf2, err := y.(encoding.BinaryMarshaler).MarshalBinary()
267 if err != nil {
268 t.Logf("(%v).MarshalBinary() = %q", x, buf)
269 t.Logf("(%T).UnmarshalBinary(%q) = %v", y, buf, y)
270 t.Fatalf("failed to MarshalBinary a second time: %v", err)
271 }
272 if !bytes.Equal(buf, buf2) {
273 t.Logf("(%v).MarshalBinary() = %q", x, buf)
274 t.Logf("(%T).UnmarshalBinary(%q) = %v", y, buf, y)
275 t.Logf("(%v).MarshalBinary() = %q", y, buf2)
276 t.Fatalf("second MarshalBinary differs from first: %q != %q", buf, buf2)
277 }
278 }
279
280 func checkTextMarshalMatchesString(t *testing.T, x netipType) {
281 buf, err := x.MarshalText()
282 if err != nil {
283 t.Fatal(err)
284 }
285 str := x.String()
286 if string(buf) != str {
287 t.Fatalf("%v: MarshalText = %q, String = %q", x, buf, str)
288 }
289 }
290
291 type appendMarshaler interface {
292 encoding.TextMarshaler
293 AppendTo([]byte) []byte
294 }
295
296 // checkTextMarshalMatchesAppendTo checks that x's MarshalText matches x's AppendTo.
297 func checkTextMarshalMatchesAppendTo(t *testing.T, x appendMarshaler) {
298 buf, err := x.MarshalText()
299 if err != nil {
300 t.Fatal(err)
301 }
302
303 buf2 := make([]byte, 0, len(buf))
304 buf2 = x.AppendTo(buf2)
305 if !bytes.Equal(buf, buf2) {
306 t.Fatalf("%v: MarshalText = %q, AppendTo = %q", x, buf, buf2)
307 }
308 }
309
310 type netipType interface {
311 encoding.BinaryMarshaler
312 encoding.TextMarshaler
313 fmt.Stringer
314 IsValid() bool
315 }
316
317 type netipTypeCmp interface {
318 comparable
319 netipType
320 }
321
322 // checkStringParseRoundTrip checks that x's String method and the provided parse function can round trip correctly.
323 func checkStringParseRoundTrip[P netipTypeCmp](t *testing.T, x P, parse func(string) (P, error)) {
324 if !x.IsValid() {
325 // Ignore invalid values.
326 return
327 }
328
329 s := x.String()
330 y, err := parse(s)
331 if err != nil {
332 t.Fatalf("s=%q err=%v", s, err)
333 }
334 if x != y {
335 t.Fatalf("%T round trip identity failure: s=%q x=%#v y=%#v", x, s, x, y)
336 }
337 s2 := y.String()
338 if s != s2 {
339 t.Fatalf("%T String round trip identity failure: s=%#v s2=%#v", x, s, s2)
340 }
341 }
342
343 func checkEncoding(t *testing.T, x netipType) {
344 if x.IsValid() {
345 checkTextMarshaler(t, x)
346 checkBinaryMarshaler(t, x)
347 checkTextMarshalMatchesString(t, x)
348 }
349
350 if am, ok := x.(appendMarshaler); ok {
351 checkTextMarshalMatchesAppendTo(t, am)
352 }
353 }