chore: remove IPv6 special-casing (YAGNI)

Drop the explicit IPv6 early-exit in ReadAll — ParseIPv4 already rejects
non-IPv4 via Is4(). Remove IPv6-specific tests and error message wording.
This commit is contained in:
AJ ONeal 2026-04-20 09:54:04 -06:00
parent ad5d696ce6
commit 86ffa2fb23
No known key found for this signature in database
2 changed files with 1 additions and 30 deletions

View File

@ -132,7 +132,7 @@ func ParseIPv4(raw string) (ipv4net IPv4Net, err error) {
addr := ippre.Addr()
if !addr.Is4() {
return ipv4net, fmt.Errorf("IPv6 not supported: %s", raw)
return ipv4net, fmt.Errorf("not an IPv4 address: %s", raw)
}
ip4 := addr.As4()
prefix := uint8(ippre.Bits()) // 032
@ -203,11 +203,6 @@ func ReadAll(r *csv.Reader) (*Cohort, error) {
continue
}
// skip IPv6
if strings.Contains(raw, ":") {
continue
}
ipv4net, err := ParseIPv4(raw)
if err != nil {
log.Printf("skipping invalid entry: %q", raw)

View File

@ -21,7 +21,6 @@ func TestParseIPv4(t *testing.T) {
{"", true},
{"not-an-ip", true},
{"1.2.3.4/33", true},
{"::1", true}, // IPv6 not supported
}
for _, tt := range tests {
_, err := ipcohort.ParseIPv4(tt.raw)
@ -104,16 +103,6 @@ func TestContains_FailClosed(t *testing.T) {
}
}
func TestContains_IPv6NeverBlocked(t *testing.T) {
c, _ := ipcohort.Parse([]string{"1.2.3.4", "10.0.0.0/8"})
// IPv6 addresses are not stored; should return false, not panic.
if c.Contains("::1") {
t.Error("IPv6 address should not be in an IPv4-only cohort")
}
if c.Contains("2001:db8::1") {
t.Error("IPv6 address should not be in an IPv4-only cohort")
}
}
func TestContains_Empty(t *testing.T) {
c, err := ipcohort.Parse(nil)
@ -175,16 +164,3 @@ func TestLoadFiles_Merge(t *testing.T) {
}
}
func TestCSVSkipsIPv6(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "mixed.txt")
os.WriteFile(path, []byte("1.2.3.4\n::1\n2001:db8::/32\n5.6.7.8\n"), 0o644)
c, err := ipcohort.LoadFile(path)
if err != nil {
t.Fatal(err)
}
if c.Size() != 2 {
t.Errorf("Size() = %d, want 2 (IPv6 should be skipped)", c.Size())
}
}