|
24 | 24 | #include "absl/base/no_destructor.h" |
25 | 25 | #include "absl/log/absl_check.h" |
26 | 26 | #include "absl/status/status.h" |
| 27 | +#include "absl/status/status_matchers.h" |
27 | 28 | #include "absl/status/statusor.h" |
28 | 29 | #include "absl/strings/string_view.h" |
| 30 | +#include "base/builtins.h" |
29 | 31 | #include "common/memory.h" |
30 | 32 | #include "common/source.h" |
31 | 33 | #include "common/value.h" |
|
51 | 53 | namespace cel { |
52 | 54 | namespace { |
53 | 55 |
|
| 56 | +using ::absl_testing::StatusIs; |
54 | 57 | using ::cel::extensions::ProtobufRuntimeAdapter; |
55 | 58 | using ::cel::extensions::ProtoMemoryManagerRef; |
56 | 59 | using ::cel::test::BoolValueIs; |
@@ -544,5 +547,116 @@ TEST(StandardRuntimeTest, RuntimeIssueSupport) { |
544 | 547 | } |
545 | 548 | } |
546 | 549 |
|
| 550 | +enum class EvalStrategy { kIterative, kRecursive }; |
| 551 | + |
| 552 | +class StandardRuntimeEvalStrategyTest |
| 553 | + : public ::testing::TestWithParam<EvalStrategy> {}; |
| 554 | + |
| 555 | +// Check that calls to specialized builtins are validated. |
| 556 | +TEST_P(StandardRuntimeEvalStrategyTest, InvalidBuiltinBoolOp) { |
| 557 | + EvalStrategy eval_strategy = GetParam(); |
| 558 | + RuntimeOptions options; |
| 559 | + if (eval_strategy == EvalStrategy::kRecursive) { |
| 560 | + options.max_recursion_depth = -1; |
| 561 | + } else { |
| 562 | + options.max_recursion_depth = 0; |
| 563 | + } |
| 564 | + |
| 565 | + google::protobuf::Arena arena; |
| 566 | + |
| 567 | + ASSERT_OK_AND_ASSIGN(auto builder, |
| 568 | + CreateStandardRuntimeBuilder( |
| 569 | + google::protobuf::DescriptorPool::generated_pool(), options)); |
| 570 | + |
| 571 | + ASSERT_OK_AND_ASSIGN(auto runtime, std::move(builder).Build()); |
| 572 | + |
| 573 | + ParsedExpr expr; |
| 574 | + expr.mutable_expr()->mutable_call_expr()->set_function(cel::builtin::kOr); |
| 575 | + auto* arg = expr.mutable_expr()->mutable_call_expr()->add_args(); |
| 576 | + arg->mutable_const_expr()->set_bool_value(true); |
| 577 | + |
| 578 | + EXPECT_THAT(ProtobufRuntimeAdapter::CreateProgram(*runtime, expr), |
| 579 | + StatusIs(absl::StatusCode::kInvalidArgument)); |
| 580 | +} |
| 581 | + |
| 582 | +TEST_P(StandardRuntimeEvalStrategyTest, InvalidBuiltinTernaryOp) { |
| 583 | + EvalStrategy eval_strategy = GetParam(); |
| 584 | + RuntimeOptions options; |
| 585 | + if (eval_strategy == EvalStrategy::kRecursive) { |
| 586 | + options.max_recursion_depth = -1; |
| 587 | + } else { |
| 588 | + options.max_recursion_depth = 0; |
| 589 | + } |
| 590 | + |
| 591 | + google::protobuf::Arena arena; |
| 592 | + |
| 593 | + ASSERT_OK_AND_ASSIGN(auto builder, |
| 594 | + CreateStandardRuntimeBuilder( |
| 595 | + google::protobuf::DescriptorPool::generated_pool(), options)); |
| 596 | + |
| 597 | + ASSERT_OK_AND_ASSIGN(auto runtime, std::move(builder).Build()); |
| 598 | + |
| 599 | + ParsedExpr expr; |
| 600 | + expr.mutable_expr()->mutable_call_expr()->set_function( |
| 601 | + cel::builtin::kTernary); |
| 602 | + expr.mutable_expr() |
| 603 | + ->mutable_call_expr() |
| 604 | + ->add_args() |
| 605 | + ->mutable_const_expr() |
| 606 | + ->set_bool_value(true); |
| 607 | + expr.mutable_expr() |
| 608 | + ->mutable_call_expr() |
| 609 | + ->add_args() |
| 610 | + ->mutable_const_expr() |
| 611 | + ->set_bool_value(true); |
| 612 | + expr.mutable_expr() |
| 613 | + ->mutable_call_expr() |
| 614 | + ->add_args() |
| 615 | + ->mutable_const_expr() |
| 616 | + ->set_bool_value(true); |
| 617 | + expr.mutable_expr() |
| 618 | + ->mutable_call_expr() |
| 619 | + ->add_args() |
| 620 | + ->mutable_const_expr() |
| 621 | + ->set_bool_value(true); |
| 622 | + |
| 623 | + EXPECT_THAT(ProtobufRuntimeAdapter::CreateProgram(*runtime, expr), |
| 624 | + StatusIs(absl::StatusCode::kInvalidArgument)); |
| 625 | +} |
| 626 | + |
| 627 | +TEST_P(StandardRuntimeEvalStrategyTest, InvalidBuiltinIndex) { |
| 628 | + EvalStrategy eval_strategy = GetParam(); |
| 629 | + RuntimeOptions options; |
| 630 | + if (eval_strategy == EvalStrategy::kRecursive) { |
| 631 | + options.max_recursion_depth = -1; |
| 632 | + } else { |
| 633 | + options.max_recursion_depth = 0; |
| 634 | + } |
| 635 | + |
| 636 | + google::protobuf::Arena arena; |
| 637 | + |
| 638 | + ASSERT_OK_AND_ASSIGN(auto builder, |
| 639 | + CreateStandardRuntimeBuilder( |
| 640 | + google::protobuf::DescriptorPool::generated_pool(), options)); |
| 641 | + |
| 642 | + ASSERT_OK_AND_ASSIGN(auto runtime, std::move(builder).Build()); |
| 643 | + |
| 644 | + ParsedExpr expr; |
| 645 | + expr.mutable_expr()->mutable_call_expr()->set_function(cel::builtin::kIndex); |
| 646 | + auto* arg = expr.mutable_expr()->mutable_call_expr()->add_args(); |
| 647 | + arg->mutable_list_expr() |
| 648 | + ->add_elements() |
| 649 | + ->mutable_const_expr() |
| 650 | + ->set_int64_value(1); |
| 651 | + |
| 652 | + EXPECT_THAT(ProtobufRuntimeAdapter::CreateProgram(*runtime, expr), |
| 653 | + StatusIs(absl::StatusCode::kInvalidArgument)); |
| 654 | +} |
| 655 | + |
| 656 | +INSTANTIATE_TEST_SUITE_P(StandardRuntimeEvalStrategyTest, |
| 657 | + StandardRuntimeEvalStrategyTest, |
| 658 | + testing::Values(EvalStrategy::kIterative, |
| 659 | + EvalStrategy::kRecursive)); |
| 660 | + |
547 | 661 | } // namespace |
548 | 662 | } // namespace cel |
0 commit comments