package middleware

import (
	"net"
	"testing"
)

func TestIPMatchesRule_LoopbackEquivalence(t *testing.T) {
	v4 := net.ParseIP("127.0.0.1")
	v6 := net.ParseIP("::1")

	if !ipMatchesRule("127.0.0.1", v6) {
		t.Fatal("::1 should match 127.0.0.1 allow rule")
	}
	if !ipMatchesRule("::1", v4) {
		t.Fatal("127.0.0.1 should match ::1 allow rule")
	}
	if !ipMatchesRule("127.0.0.0/8", v6) {
		t.Fatal("::1 should match 127.0.0.0/8 via loopback equivalence")
	}
}

func TestIPMatchesRule_CIDR(t *testing.T) {
	ip := net.ParseIP("10.0.0.5")
	if !ipMatchesRule("10.0.0.0/24", ip) {
		t.Fatal("expected CIDR match")
	}
	if ipMatchesRule("192.168.1.0/24", ip) {
		t.Fatal("expected no match")
	}
}
