package firewall import ( "testing" ) func TestEvaluate_emptyRules(t *testing.T) { prefixes := map[string][]string{"c1": {"1.2.3.0/24"}} got := Evaluate("client1", nil, prefixes) if len(got) != 0 { t.Fatalf("expected empty blocklist, got %v", got) } } func TestEvaluate_onlyAccept(t *testing.T) { wild := (*string)(nil) rules := []Rule{{Priority: 1, Action: "accept", CommunityID: wild}} prefixes := map[string][]string{"c1": {"1.2.3.0/24"}} got := Evaluate("client1", rules, prefixes) if len(got) != 0 { t.Fatalf("accept alone must not block, got %v", got) } } func TestEvaluate_explicitBlock(t *testing.T) { cid := "c1" rules := []Rule{{Priority: 1, Action: "block", CommunityID: &cid}} prefixes := map[string][]string{"c1": {"1.2.3.0/24", "5.6.7.8/32"}, "c2": {"9.9.9.9/32"}} got := Evaluate("client1", rules, prefixes) if len(got) != 2 { t.Fatalf("expected 2 prefixes, got %v", got) } } func TestEvaluate_clientOverrideAccept(t *testing.T) { wild := (*string)(nil) cTrusted := "trusted" clientID := "srv1" rules := []Rule{ {Priority: 1, Action: "block", CommunityID: wild}, {ClientID: &clientID, Priority: 1, Action: "accept", CommunityID: &cTrusted}, } prefixes := map[string][]string{ "trusted": {"1.1.1.0/24"}, "bad": {"2.2.2.0/24"}, } got := Evaluate(clientID, rules, prefixes) if len(got) != 1 || got[0] != "2.2.2.0/24" { t.Fatalf("expected only bad community, got %v", got) } } func TestEvaluate_clientOverrideWins(t *testing.T) { cid := "c1" clientID := "srv1" rules := []Rule{ {Priority: 1, Action: "accept", CommunityID: &cid}, {ClientID: &clientID, Priority: 1, Action: "block", CommunityID: &cid}, } prefixes := map[string][]string{"c1": {"1.2.3.0/24"}} got := Evaluate(clientID, rules, prefixes) if len(got) != 1 { t.Fatalf("client override should block, got %v", got) } } func TestEvaluate_noCommunityKey(t *testing.T) { empty := "" rules := []Rule{{Priority: 1, Action: "block", CommunityID: &empty}} prefixes := map[string][]string{"": {"10.0.0.0/8"}} got := Evaluate("c", rules, prefixes) if len(got) != 1 { t.Fatalf("expected prefix without community, got %v", got) } }