Ensure that only valid map keys are supported in AST-based `CreateStr… · google/cel-cpp@3261a58
@@ -7,6 +7,7 @@
77#include "google/protobuf/struct.pb.h"
88#include "base/testing.h"
99#include "gtest/gtest.h"
10+#include "absl/status/status.h"
1011#include "eval/eval/ident_step.h"
1112#include "eval/public/cel_attribute.h"
1213#include "eval/public/cel_builtins.h"
@@ -23,10 +24,11 @@ namespace runtime {
23242425namespace {
252627+using ::google::api::expr::v1alpha1::Expr;
28+using ::google::api::expr::v1alpha1::SourceInfo;
2629using ::google::protobuf::Struct;
27-28-using google::api::expr::v1alpha1::Expr;
29-using google::api::expr::v1alpha1::SourceInfo;
30+using testing::HasSubstr;
31+using cel_base::testing::StatusIs;
30323133using TestParamType = std::tuple<bool, bool>;
3234@@ -191,6 +193,40 @@ TEST_P(ContainerAccessStepUniformityTest, TestMapKeyAccessNotFound) {
191193ASSERT_TRUE(result.IsError());
192194}
193195196+TEST_F(ContainerAccessStepTest, TestInvalidReceiverCreateContainerAccessStep) {
197+ Expr expr;
198+auto call = expr.mutable_call_expr();
199+ call->set_function(builtin::kIndex);
200+ Expr* container_expr = call->mutable_target();
201+ container_expr->mutable_ident_expr()->set_name("container");
202+203+ Expr* key_expr = call->add_args();
204+ key_expr->mutable_ident_expr()->set_name("key");
205+206+ Expr* extra_arg = call->add_args();
207+ extra_arg->mutable_const_expr()->set_bool_value(true);
208+EXPECT_THAT(CreateContainerAccessStep(call, 0).status(),
209+StatusIs(absl::StatusCode::kInvalidArgument,
210+HasSubstr("Invalid argument count")));
211+}
212+213+TEST_F(ContainerAccessStepTest, TestInvalidGlobalCreateContainerAccessStep) {
214+ Expr expr;
215+auto call = expr.mutable_call_expr();
216+ call->set_function(builtin::kIndex);
217+ Expr* container_expr = call->add_args();
218+ container_expr->mutable_ident_expr()->set_name("container");
219+220+ Expr* key_expr = call->add_args();
221+ key_expr->mutable_ident_expr()->set_name("key");
222+223+ Expr* extra_arg = call->add_args();
224+ extra_arg->mutable_const_expr()->set_bool_value(true);
225+EXPECT_THAT(CreateContainerAccessStep(call, 0).status(),
226+StatusIs(absl::StatusCode::kInvalidArgument,
227+HasSubstr("Invalid argument count")));
228+}
229+194230TEST_F(ContainerAccessStepTest, TestListIndexAccessUnknown) {
195231 ContainerBackedListImpl cel_list({CelValue::CreateInt64(1),
196232CelValue::CreateInt64(2),
@@ -225,6 +261,25 @@ TEST_F(ContainerAccessStepTest, TestListUnknownKey) {
225261ASSERT_TRUE(result.IsUnknownSet());
226262}
227263264+TEST_F(ContainerAccessStepTest, TestMapInvalidKey) {
265+const std::string kKey0 = "testkey0";
266+const std::string kKey1 = "testkey1";
267+const std::string kKey2 = "testkey2";
268+ Struct cel_struct;
269+ (*cel_struct.mutable_fields())[kKey0].set_string_value("value0");
270+ (*cel_struct.mutable_fields())[kKey1].set_string_value("value1");
271+ (*cel_struct.mutable_fields())[kKey2].set_string_value("value2");
272+273+ CelValue result =
274+EvaluateAttribute(CelProtoWrapper::CreateMessage(&cel_struct, &arena_),
275+CelValue::CreateDouble(1.0), true, true);
276+277+ASSERT_TRUE(result.IsError());
278+EXPECT_THAT(*result.ErrorOrDie(),
279+StatusIs(absl::StatusCode::kInvalidArgument,
280+HasSubstr("Invalid map key type: 'double'")));
281+}
282+228283TEST_F(ContainerAccessStepTest, TestMapUnknownKey) {
229284const std::string kKey0 = "testkey0";
230285const std::string kKey1 = "testkey1";
@@ -250,6 +305,16 @@ TEST_F(ContainerAccessStepTest, TestUnknownContainer) {
250305ASSERT_TRUE(result.IsUnknownSet());
251306}
252307308+TEST_F(ContainerAccessStepTest, TestInvalidContainerType) {
309+ CelValue result = EvaluateAttribute(CelValue::CreateInt64(1),
310+CelValue::CreateInt64(0), true, true);
311+312+ASSERT_TRUE(result.IsError());
313+EXPECT_THAT(*result.ErrorOrDie(),
314+StatusIs(absl::StatusCode::kInvalidArgument,
315+HasSubstr("Invalid container type: 'int64_t'")));
316+}
317+253318INSTANTIATE_TEST_SUITE_P(CombinedContainerTest,
254319 ContainerAccessStepUniformityTest,
255320testing::Combine(testing::Bool(), testing::Bool()));