Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

Commit

Permalink
Make IPPrefix uniformly strip/ignore IPv6 zones.
Browse files Browse the repository at this point in the history
Enabler for #167.

Signed-off-by: David Anderson <[email protected]>
  • Loading branch information
danderson authored and bradfitz committed Jun 2, 2021
1 parent 0067cbf commit 5c1c0f9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
6 changes: 2 additions & 4 deletions netaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -1239,10 +1239,7 @@ func ParseIPPrefix(s string) (IPPrefix, error) {
if bits < 0 || bits > maxBits {
return IPPrefix{}, fmt.Errorf("netaddr.ParseIPPrefix(%q): prefix length out of range", s)
}
return IPPrefix{
ip: ip,
bits: uint8(bits),
}, nil
return IPPrefixFrom(ip, uint8(bits)), nil
}

// MustParseIPPrefix calls ParseIPPrefix(s) and panics on error.
Expand Down Expand Up @@ -1294,6 +1291,7 @@ func (p IPPrefix) IPNet() *net.IPNet {
// An IPv4 address will not match an IPv6 prefix.
// A v6-mapped IPv6 address will not match an IPv4 prefix.
// A zero-value IP will not match any prefix.
// ip's zone, if any, is ignored.
func (p IPPrefix) Contains(ip IP) bool {
if !p.Valid() {
return false
Expand Down
37 changes: 31 additions & 6 deletions netaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,7 +1134,6 @@ func TestIPPrefixMarshalUnmarshal(t *testing.T) {
"0.0.0.0/0",
"::/0",
"::1/128",
"fe80::1cc0:3e8c:119f:c2e1%ens18/128",
"::ffff:c000:1234/128",
"2001:db8::/32",
}
Expand Down Expand Up @@ -1163,6 +1162,26 @@ func TestIPPrefixMarshalUnmarshal(t *testing.T) {
}
}

func TestIPPrefixMarshalUnmarshalZone(t *testing.T) {
orig := `"fe80::1cc0:3e8c:119f:c2e1%ens18/128"`
unzoned := `"fe80::1cc0:3e8c:119f:c2e1/128"`

var p IPPrefix
if err := json.Unmarshal([]byte(orig), &p); err != nil {
t.Fatalf("failed to unmarshal: %v", err)
}

pb, err := json.Marshal(p)
if err != nil {
t.Fatalf("failed to marshal: %v", err)
}

back := string(pb)
if back != unzoned {
t.Errorf("Marshal = %q; want %q", back, unzoned)
}
}

func TestIPPrefixUnmarshalTextNonZero(t *testing.T) {
ip := mustIPPrefix("fe80::/64")
if err := ip.UnmarshalText([]byte("xxx")); err == nil {
Expand Down Expand Up @@ -1266,6 +1285,7 @@ func TestIPPrefix(t *testing.T) {
ip IP
bits uint8
ipNet *net.IPNet
str string
contains []IP
notContains []IP
}{
Expand Down Expand Up @@ -1348,14 +1368,15 @@ func TestIPPrefix(t *testing.T) {
},
{
prefix: "::%0/00/80",
ip: mustIP("::%0/00"),
ip: mustIP("::"),
bits: 80,
str: "::/80",
ipNet: &net.IPNet{
IP: net.ParseIP("::"), // net.IPNet drops zones
Mask: net.CIDRMask(80, 128),
},
contains: mustIPs("::%0/00"),
notContains: mustIPs("ff::%0/00"),
contains: mustIPs("::%0/00", "::%1/23"),
notContains: mustIPs("ff::%0/00", "ff::%1/23"),
},
}
for _, test := range tests {
Expand Down Expand Up @@ -1384,8 +1405,12 @@ func TestIPPrefix(t *testing.T) {
t.Errorf("contains %s", ip)
}
}
if got := prefix.String(); got != test.prefix {
t.Errorf("prefix.String()=%q, want %q", got, test.prefix)
want := test.str
if want == "" {
want = test.prefix
}
if got := prefix.String(); got != want {
t.Errorf("prefix.String()=%q, want %q", got, want)
}

testAppendToMarshal(t, prefix)
Expand Down

0 comments on commit 5c1c0f9

Please sign in to comment.