forked from inetaf/netaddr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz.go
More file actions
88 lines (80 loc) · 2.2 KB
/
Copy pathfuzz.go
File metadata and controls
88 lines (80 loc) · 2.2 KB
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
// Copyright 2020 The Inet.Af AUTHORS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build gofuzz
package netaddr
import (
"fmt"
"net"
"strings"
)
func Fuzz(b []byte) int {
s := string(b)
ip, err := ParseIP(s)
if err == nil {
s2 := ip.String()
// There's no guarantee that ip.String() will match s.
// But a round trip the other direction ought to succeed.
ip2, err := ParseIP(s2)
if err != nil {
panic(err)
}
if ip2 != ip {
fmt.Printf("ip=%#v ip2=%#v\n", ip, ip2)
panic("IP round trip identity failure")
}
if s2 != ip2.String() {
panic("IP String round trip identity failure")
}
}
// Check that we match the standard library's IP parser, modulo zones.
if !strings.Contains(s, "%") {
stdip := net.ParseIP(s)
if ip.IsZero() != (stdip == nil) {
fmt.Println("stdip=", stdip, "ip=", ip)
panic("net.ParseIP nil != ParseIP zero")
} else if !ip.IsZero() && !ip.Is4in6() && ip.String() != stdip.String() {
fmt.Println("ip=", ip, "stdip=", stdip)
panic("net.IP.String() != IP.String()")
}
}
// Check that .Next().Prior() and .Prior().Next() preserve the IP.
if !ip.IsZero() && !ip.Next().IsZero() && ip.Next().Prior() != ip {
fmt.Println("ip=", ip, ".next=", ip.Next(), ".next.prior=", ip.Next().Prior())
panic(".Next.Prior did not round trip")
}
if !ip.IsZero() && !ip.Prior().IsZero() && ip.Prior().Next() != ip {
fmt.Println("ip=", ip, ".prior=", ip.Prior(), ".prior.next=", ip.Prior().Next())
panic(".Prior.Next did not round trip")
}
port, err := ParseIPPort(s)
if err == nil {
s2 := port.String()
port2, err := ParseIPPort(s2)
if err != nil {
panic(err)
}
if port2 != port {
panic("IPPort round trip identity failure")
}
if port2.String() != s2 {
panic("IPPort String round trip identity failure")
}
}
ipp, err := ParseIPPrefix(s)
if err == nil {
s2 := ipp.String()
ipp2, err := ParseIPPrefix(s2)
if err != nil {
panic(err)
}
if ipp2 != ipp {
fmt.Printf("ipp=%#v ipp=%#v\n", ipp, ipp2)
panic("IPPrefix round trip identity failure")
}
if ipp2.String() != s2 {
panic("IPPrefix String round trip identity failure")
}
}
return 0
}