|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "validator/comprehension_nesting_validator.h" |
| 16 | + |
| 17 | +#include <memory> |
| 18 | +#include <string> |
| 19 | +#include <utility> |
| 20 | + |
| 21 | +#include "absl/status/statusor.h" |
| 22 | +#include "compiler/compiler.h" |
| 23 | +#include "compiler/compiler_factory.h" |
| 24 | +#include "compiler/standard_library.h" |
| 25 | +#include "extensions/bindings_ext.h" |
| 26 | +#include "internal/testing.h" |
| 27 | +#include "internal/testing_descriptor_pool.h" |
| 28 | +#include "validator/validator.h" |
| 29 | + |
| 30 | +namespace cel { |
| 31 | +namespace { |
| 32 | + |
| 33 | +using ::testing::HasSubstr; |
| 34 | + |
| 35 | +absl::StatusOr<std::unique_ptr<Compiler>> StdLibCompiler() { |
| 36 | + CEL_ASSIGN_OR_RETURN( |
| 37 | + auto builder, |
| 38 | + NewCompilerBuilder(internal::GetSharedTestingDescriptorPool())); |
| 39 | + CEL_RETURN_IF_ERROR(builder->AddLibrary(StandardCompilerLibrary())); |
| 40 | + CEL_RETURN_IF_ERROR( |
| 41 | + builder->AddLibrary(cel::extensions::BindingsCompilerLibrary())); |
| 42 | + return builder->Build(); |
| 43 | +} |
| 44 | + |
| 45 | +struct TestCase { |
| 46 | + std::string expression; |
| 47 | + int limit; |
| 48 | + bool valid; |
| 49 | + std::string error_substr = ""; |
| 50 | +}; |
| 51 | + |
| 52 | +using ComprehensionNestingValidatorTest = testing::TestWithParam<TestCase>; |
| 53 | + |
| 54 | +TEST_P(ComprehensionNestingValidatorTest, Validate) { |
| 55 | + const auto& test_case = GetParam(); |
| 56 | + Validator validator; |
| 57 | + validator.AddValidation(ComprehensionNestingLimitValidator(test_case.limit)); |
| 58 | + |
| 59 | + ASSERT_OK_AND_ASSIGN(auto compiler, StdLibCompiler()); |
| 60 | + auto result_or = compiler->Compile(test_case.expression); |
| 61 | + if (!result_or.ok()) { |
| 62 | + GTEST_SKIP() << "Expression failed to compile: " << test_case.expression |
| 63 | + << " " << result_or.status().message(); |
| 64 | + } |
| 65 | + auto result = std::move(result_or).value(); |
| 66 | + |
| 67 | + validator.UpdateValidationResult(result); |
| 68 | + |
| 69 | + EXPECT_EQ(result.IsValid(), test_case.valid) |
| 70 | + << "Expression: " << test_case.expression |
| 71 | + << " Limit: " << test_case.limit; |
| 72 | + if (!test_case.valid) { |
| 73 | + EXPECT_THAT(result.FormatError(), HasSubstr(test_case.error_substr)); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +INSTANTIATE_TEST_SUITE_P( |
| 78 | + ComprehensionNestingValidatorTest, ComprehensionNestingValidatorTest, |
| 79 | + testing::Values( |
| 80 | + TestCase{"[1, 2].all(x, x > 0)", 1, true}, |
| 81 | + TestCase{"[1, 2].all(x, [1, 2].all(y, x > y))", 1, false, |
| 82 | + "comprehension nesting level of 2 exceeds limit of 1"}, |
| 83 | + TestCase{"[1, 2].all(x, [1, 2].all(y, x > y))", 2, true}, |
| 84 | + // Empty range comprehension (does not count) |
| 85 | + TestCase{"[].all(x, [1, 2].all(y, y > 0))", 1, true}, |
| 86 | + TestCase{"cel.bind(x, [1, 2].all(y, y > 0), [1, 2].all(z, z > 0))", 1, |
| 87 | + true}, |
| 88 | + // Nested empty range comprehensions |
| 89 | + TestCase{"[].all(x, [].all(y, true))", 0, true}, |
| 90 | + // Deeply nested mixed |
| 91 | + TestCase{"[1].all(x, [].all(y, [2].all(z, true)))", 1, false, |
| 92 | + "comprehension nesting level of 2 exceeds limit of 1"}, |
| 93 | + TestCase{"[1].all(x, [].all(y, [2].all(z, true)))", 2, true})); |
| 94 | + |
| 95 | +} // namespace |
| 96 | +} // namespace cel |
0 commit comments