-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathregex_match_step_test.cc
More file actions
102 lines (93 loc) · 4.02 KB
/
Copy pathregex_match_step_test.cc
File metadata and controls
102 lines (93 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "eval/eval/regex_match_step.h"
#include "google/api/expr/v1alpha1/checked.pb.h"
#include "google/api/expr/v1alpha1/syntax.pb.h"
#include "google/protobuf/arena.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "eval/public/activation.h"
#include "eval/public/builtin_func_registrar.h"
#include "eval/public/cel_expr_builder_factory.h"
#include "eval/public/cel_options.h"
#include "internal/testing.h"
#include "parser/parser.h"
namespace google::api::expr::runtime {
namespace {
using google::api::expr::v1alpha1::CheckedExpr;
using google::api::expr::v1alpha1::Reference;
using testing::Eq;
using cel::internal::StatusIs;
Reference MakeMatchesStringOverload() {
Reference reference;
reference.add_overload_id("matches_string");
return reference;
}
TEST(RegexMatchStep, Precompiled) {
google::protobuf::Arena arena;
Activation activation;
ASSERT_OK_AND_ASSIGN(auto parsed_expr, parser::Parse("foo.matches('hello')"));
CheckedExpr checked_expr;
*checked_expr.mutable_expr() = parsed_expr.expr();
*checked_expr.mutable_source_info() = parsed_expr.source_info();
checked_expr.mutable_reference_map()->insert(
{checked_expr.expr().id(), MakeMatchesStringOverload()});
InterpreterOptions options;
options.enable_regex_precompilation = true;
auto expr_builder = CreateCelExpressionBuilder(options);
ASSERT_OK(RegisterBuiltinFunctions(expr_builder->GetRegistry(), options));
ASSERT_OK_AND_ASSIGN(auto expr,
expr_builder->CreateExpression(&checked_expr));
activation.InsertValue("foo", CelValue::CreateStringView("hello world!"));
ASSERT_OK_AND_ASSIGN(auto result, expr->Evaluate(activation, &arena));
EXPECT_TRUE(result.IsBool());
EXPECT_TRUE(result.BoolOrDie());
}
TEST(RegexMatchStep, PrecompiledInvalidRegex) {
google::protobuf::Arena arena;
Activation activation;
ASSERT_OK_AND_ASSIGN(auto parsed_expr, parser::Parse("foo.matches('(')"));
CheckedExpr checked_expr;
*checked_expr.mutable_expr() = parsed_expr.expr();
*checked_expr.mutable_source_info() = parsed_expr.source_info();
checked_expr.mutable_reference_map()->insert(
{checked_expr.expr().id(), MakeMatchesStringOverload()});
InterpreterOptions options;
options.enable_regex_precompilation = true;
auto expr_builder = CreateCelExpressionBuilder(options);
ASSERT_OK(RegisterBuiltinFunctions(expr_builder->GetRegistry(), options));
EXPECT_THAT(
expr_builder->CreateExpression(&checked_expr),
StatusIs(absl::StatusCode::kInvalidArgument, Eq("invalid_argument")));
}
TEST(RegexMatchStep, PrecompiledInvalidProgramTooLarge) {
google::protobuf::Arena arena;
Activation activation;
ASSERT_OK_AND_ASSIGN(auto parsed_expr, parser::Parse("foo.matches('hello')"));
CheckedExpr checked_expr;
*checked_expr.mutable_expr() = parsed_expr.expr();
*checked_expr.mutable_source_info() = parsed_expr.source_info();
checked_expr.mutable_reference_map()->insert(
{checked_expr.expr().id(), MakeMatchesStringOverload()});
InterpreterOptions options;
options.regex_max_program_size = 1;
options.enable_regex_precompilation = true;
auto expr_builder = CreateCelExpressionBuilder(options);
ASSERT_OK(RegisterBuiltinFunctions(expr_builder->GetRegistry(), options));
EXPECT_THAT(expr_builder->CreateExpression(&checked_expr),
StatusIs(absl::StatusCode::kInvalidArgument,
Eq("exceeded RE2 max program size")));
}
} // namespace
} // namespace google::api::expr::runtime