Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index/netip.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NetIPAddrString(s string) (Key, error) {

func NetIPPrefix(prefix netip.Prefix) Key {
// Use the 16-byte form plus bits to have a constant-size key.
addrBytes := prefix.Addr().As16()
addrBytes := prefix.Masked().Addr().As16()
return append(addrBytes[:], uint8(prefix.Bits()))
}

Expand Down
22 changes: 22 additions & 0 deletions index/netip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package index

import (
"net/netip"
"testing"
)

func TestNetIPPrefix(t *testing.T) {
k1 := NetIPPrefix(netip.MustParsePrefix("10.0.0.100/8"))
Copy link
Copy Markdown

@derailed derailed Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be good to set this up as a table test so we can add more tests in the future and also test non equality.

k2 := NetIPPrefix(netip.MustParsePrefix("10.0.0.0/8"))

if !k1.Equal(k2) {
t.Errorf("expected netip prefix keys to use canonicalized CIDR")
}

k1s, _ := NetIPPrefixString("10.0.0.100/8")
k2s, _ := NetIPPrefixString("10.0.0.0/8")

if !k1s.Equal(k2s) {
t.Errorf("expected netip prefix string keys to use canonicalized CIDR")
}
}