Skip to content

Commit c17bfb2

Browse files
l46kokcopybara-github
authored andcommitted
Cover verifier behavior that no test exercised
PiperOrigin-RevId: 1004364292
1 parent b3df814 commit c17bfb2

3 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎verifier/src/main/java/dev/cel/verifier/CelZ3Exprs.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public static BoolExpr mkOrFlattened(Context ctx, List<BoolExpr> args) {
5252
filteredArgs.add(arg);
5353
}
5454
}
55+
// Performance only: both shortcuts are equivalent to the mkOr below.
5556
if (filteredArgs.isEmpty()) {
5657
return ctx.mkFalse();
5758
}
@@ -82,6 +83,7 @@ public static BoolExpr mkAndFlattened(Context ctx, List<BoolExpr> args) {
8283
filteredArgs.add(arg);
8384
}
8485
}
86+
// Performance only: both shortcuts are equivalent to the mkAnd below.
8587
if (filteredArgs.isEmpty()) {
8688
return ctx.mkTrue();
8789
}

‎verifier/src/test/java/dev/cel/verifier/CelPolicyVerifierImplTest.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,31 @@ public void verifyInvariants_verificationVariablesAndMultiClauseLists_verified()
499499
assertThat(results).containsExactly("ergonomic_inv", CelVerificationResult.verified());
500500
}
501501

502+
@Test
503+
public void verifyInvariants_literalListVariableBeyondUnrollLimit_verified() throws Exception {
504+
String policySource =
505+
"name: literal_list_policy\n" //
506+
+ "rule:\n" //
507+
+ " variables:\n" //
508+
+ " - xs: '[1, 2, 3, 4, 5, 6]'\n" //
509+
+ " match:\n" //
510+
+ " - output: 'true'\n" //
511+
+ "verification:\n" //
512+
+ " invariants:\n" //
513+
+ " - id: all_positive\n" //
514+
+ " assert: variables.xs.all(x, x > 0)";
515+
CelPolicy policy = PARSER.parse(policySource);
516+
CelPolicyVerifier verifier =
517+
CelPolicyVerifierFactory.newVerifier(
518+
POLICY_COMPILER,
519+
CelVerifierFactory.newVerifier(CEL).setComprehensionUnrollLimit(5).build())
520+
.build();
521+
522+
ImmutableMap<String, CelVerificationResult> results = verifier.verifyInvariants(policy);
523+
524+
assertThat(results).containsExactly("all_positive", CelVerificationResult.verified());
525+
}
526+
502527
@Test
503528
public void verifyInvariants_workloadAdmissionFlawed_violationsDetected() throws Exception {
504529
Cel extendedCel =

‎verifier/src/test/java/dev/cel/verifier/CelVerifierZ3ImplTest.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,9 @@ private enum IsAlwaysTrueTestCase {
742742
OPTIONAL_VALUE_VAR("optional.of(x).value() == x"),
743743
OPTIONAL_HAS_VALUE_VAR("optional.of(x).hasValue()"),
744744
OPTIONAL_VAR_HAS_VALUE_IMPLIES_INT("opt_var.hasValue() ? type(opt_var.value()) == int : true"),
745+
OPTIONAL_LIST_INDEX("size(int_list) > 0 ? int_list[?0].value() == int_list[0] : true"),
746+
OPTIONAL_MAP_INDEX(
747+
"'a' in string_int_map ? string_int_map[?'a'].value() == string_int_map['a'] : true"),
745748

746749
IEEE_754_PROTO_NEG_ZERO_NEQ(
747750
"TestAllTypes{single_double: -0.0} != TestAllTypes{single_double: 0.0}"),
@@ -978,6 +981,7 @@ private enum IsAlwaysTrueTestCase {
978981
"size(int_list) > 15 ? type(int_list[dyn(15)]) == int : true"),
979982
MAP_INDEX_TYPE_CONSTRAINT(
980983
"'key' in string_int_map ? type(string_int_map['key']) == int : true"),
984+
FIELD_SELECTION_TYPE_CONSTRAINT("type(test_all_types.single_int64) == int"),
981985
LITERAL_LIST_INDEX("[1, 2][0] == 1"),
982986
NESTED_LIST_VARIABLES_EQUALITY(
983987
"nested_list == [[1]] && nested_list_2 == [[1]] ? nested_list == nested_list_2 : true"),
@@ -1045,6 +1049,7 @@ public void isAlwaysTrue_withUnknownIdentifier_evaluatesToUnknown(
10451049
"x == x",
10461050
"[x] == [x]",
10471051
"{1: x} == {1: x}",
1052+
"size(int_list) == 1 ? int_list.all(i, x == x) : true",
10481053
})
10491054
String expression)
10501055
throws Exception {
@@ -1569,6 +1574,12 @@ private enum IsAlwaysTrueViolationTestCase {
15691574
"[true, 1/0].exists(x, x) == true",
15701575
"Condition is not always true\\.",
15711576
"\\(The expression fails unconditionally, regardless of input state\\)"),
1577+
// The non-negative element can't make the step false, so only an error at 0 violates it.
1578+
DYNAMIC_LIST_ALL_STEP_ERROR_PROPAGATES(
1579+
"size(int_list) == 1 && int_list[0] >= 0 ? int_list.all(x, 10 / x >= 0) : true",
1580+
"Condition is not always true\\.",
1581+
"Counterexample input:",
1582+
"int_list = \\[0\\]"),
15721583
DYNAMIC_STRING_MACRO_TYPE_MISMATCH(
15731584
"unknown_var == 1 ? !(unknown_var.contains('a') == true || unknown_var.contains('a') =="
15741585
+ " false) : true",

0 commit comments

Comments
 (0)