From 02be4010d6f09a188a83e6a3a07580608fe27ed5 Mon Sep 17 00:00:00 2001 From: Fabio Bozzo Date: Mon, 4 Nov 2024 18:50:30 +0100 Subject: [PATCH] add array quantifiers tests and tiny fix --- pkg/policy/match.go | 4 +++- pkg/policy/match_test.go | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/pkg/policy/match.go b/pkg/policy/match.go index c3862d5..2a586f5 100644 --- a/pkg/policy/match.go +++ b/pkg/policy/match.go @@ -242,7 +242,9 @@ func matchStatement(cur Statement, node ipld.Node) (_ matchResult, leafMost Stat // continue } } - return matchResultFalse, cur + + // when no elements match, return the leaf statement instead of 'cur' + return matchResultFalse, s.statement } } panic(fmt.Errorf("unimplemented statement kind: %s", cur.Kind())) diff --git a/pkg/policy/match_test.go b/pkg/policy/match_test.go index 83e0d08..56a2814 100644 --- a/pkg/policy/match_test.go +++ b/pkg/policy/match_test.go @@ -692,6 +692,56 @@ func TestPartialMatch(t *testing.T) { )[0], }, + // Array quantifiers + { + name: "all matches when every element satisfies condition", + policy: MustConstruct( + All(".numbers", Equal(".", literal.Int(1))), + ), + data: map[string]interface{}{ + "numbers": []interface{}{1, 1, 1}, + }, + expectedMatch: true, + expectedStmt: nil, + }, + { + name: "all fails when any element doesn't satisfy", + policy: MustConstruct( + All(".numbers", Equal(".", literal.Int(1))), + ), + data: map[string]interface{}{ + "numbers": []interface{}{1, 2, 1}, + }, + expectedMatch: false, + expectedStmt: MustConstruct( + Equal(".", literal.Int(1)), + )[0], + }, + { + name: "any succeeds when one element matches", + policy: MustConstruct( + Any(".numbers", Equal(".", literal.Int(2))), + ), + data: map[string]interface{}{ + "numbers": []interface{}{1, 2, 3}, + }, + expectedMatch: true, + expectedStmt: nil, + }, + { + name: "any fails when no elements match", + policy: MustConstruct( + Any(".numbers", Equal(".", literal.Int(4))), + ), + data: map[string]interface{}{ + "numbers": []interface{}{1, 2, 3}, + }, + expectedMatch: false, + expectedStmt: MustConstruct( + Equal(".", literal.Int(4)), + )[0], + }, + // Complex nested case { name: "complex nested policy",