-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathend_to_end_test.cc
More file actions
308 lines (266 loc) · 8.32 KB
/
end_to_end_test.cc
File metadata and controls
308 lines (266 loc) · 8.32 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <memory>
#include <string>
#include <vector>
#include "cel/expr/syntax.pb.h"
#include "google/protobuf/struct.pb.h"
#include "google/protobuf/text_format.h"
#include "absl/status/status.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_expression.h"
#include "eval/public/cel_value.h"
#include "eval/public/structs/cel_proto_wrapper.h"
#include "eval/testutil/test_message.pb.h"
#include "internal/status_macros.h"
#include "internal/testing.h"
#include "testutil/util.h"
namespace google {
namespace api {
namespace expr {
namespace runtime {
namespace {
using ::absl_testing::StatusIs;
using ::cel::expr::Expr;
using ::cel::expr::SourceInfo;
using ::google::protobuf::Arena;
using ::google::protobuf::TextFormat;
// Simple one parameter function that records the message argument it receives.
class RecordArgFunction : public CelFunction {
public:
explicit RecordArgFunction(const std::string& name,
std::vector<CelValue>* output)
: CelFunction(
CelFunctionDescriptor{name, false, {CelValue::Type::kMessage}}),
output_(*output) {}
absl::Status Evaluate(absl::Span<const CelValue> args, CelValue* result,
google::protobuf::Arena* arena) const override {
if (args.size() != 1) {
return absl::Status(absl::StatusCode::kInvalidArgument,
"Bad arguments number");
}
output_.push_back(args.at(0));
*result = CelValue::CreateBool(true);
return absl::OkStatus();
}
std::vector<CelValue>& output_;
};
// Simple end-to-end test, which also serves as usage example.
TEST(EndToEndTest, SimpleOnePlusOne) {
// AST CEL equivalent of "1+var"
constexpr char kExpr0[] = R"(
call_expr: <
function: "_+_"
args: <
ident_expr: <
name: "var"
>
>
args: <
const_expr: <
int64_value: 1
>
>
>
)";
Expr expr;
SourceInfo source_info;
TextFormat::ParseFromString(kExpr0, &expr);
// Obtain CEL Expression builder.
std::unique_ptr<CelExpressionBuilder> builder = CreateCelExpressionBuilder();
// Builtin registration.
ASSERT_OK(RegisterBuiltinFunctions(builder->GetRegistry()));
// Create CelExpression from AST (Expr object).
ASSERT_OK_AND_ASSIGN(auto cel_expr,
builder->CreateExpression(&expr, &source_info));
Activation activation;
// Bind value to "var" parameter.
activation.InsertValue("var", CelValue::CreateInt64(1));
Arena arena;
// Run evaluation.
ASSERT_OK_AND_ASSIGN(CelValue result, cel_expr->Evaluate(activation, &arena));
ASSERT_TRUE(result.IsInt64());
EXPECT_EQ(result.Int64OrDie(), 2);
}
// Simple end-to-end test, which also serves as usage example.
TEST(EndToEndTest, EmptyStringCompare) {
// AST CEL equivalent of "var.string_value == '' && var.int64_value == 0"
constexpr char kExpr0[] = R"(
call_expr: <
function: "_&&_"
args: <
call_expr: <
function: "_==_"
args: <
select_expr: <
operand: <
ident_expr: <
name: "var"
>
>
field: "string_value"
>
>
args: <
const_expr: <
string_value: ""
>
>
>
>
args: <
call_expr: <
function: "_==_"
args: <
select_expr: <
operand: <
ident_expr: <
name: "var"
>
>
field: "int64_value"
>
>
args: <
const_expr: <
int64_value: 0
>
>
>
>
>
)";
Expr expr;
SourceInfo source_info;
TextFormat::ParseFromString(kExpr0, &expr);
// Obtain CEL Expression builder.
std::unique_ptr<CelExpressionBuilder> builder = CreateCelExpressionBuilder();
// Builtin registration.
ASSERT_OK(RegisterBuiltinFunctions(builder->GetRegistry()));
// Create CelExpression from AST (Expr object).
ASSERT_OK_AND_ASSIGN(auto cel_expr,
builder->CreateExpression(&expr, &source_info));
Activation activation;
// Bind value to "var" parameter.
constexpr char kData[] = R"(
string_value: ""
int64_value: 0
)";
TestMessage data;
TextFormat::ParseFromString(kData, &data);
Arena arena;
activation.InsertValue("var", CelProtoWrapper::CreateMessage(&data, &arena));
// Run evaluation.
ASSERT_OK_AND_ASSIGN(CelValue result, cel_expr->Evaluate(activation, &arena));
ASSERT_TRUE(result.IsBool());
EXPECT_TRUE(result.BoolOrDie());
}
TEST(EndToEndTest, NullLiteral) {
// AST CEL equivalent of "Value{null_value: NullValue.NULL_VALUE}"
constexpr char kExpr0[] = R"(
struct_expr: <
message_name: "Value"
entries: <
field_key: "null_value"
value: <
select_expr: <
operand: <
ident_expr: <
name: "NullValue"
>
>
field: "NULL_VALUE"
>
>
>
>
)";
Expr expr;
SourceInfo source_info;
TextFormat::ParseFromString(kExpr0, &expr);
// Obtain CEL Expression builder.
std::unique_ptr<CelExpressionBuilder> builder = CreateCelExpressionBuilder();
builder->set_container("google.protobuf");
// Builtin registration.
ASSERT_OK(RegisterBuiltinFunctions(builder->GetRegistry()));
// Create CelExpression from AST (Expr object).
ASSERT_OK_AND_ASSIGN(auto cel_expr,
builder->CreateExpression(&expr, &source_info));
Activation activation;
Arena arena;
// Run evaluation.
ASSERT_OK_AND_ASSIGN(CelValue result, cel_expr->Evaluate(activation, &arena));
ASSERT_TRUE(result.IsNull());
}
// Equivalent to 'RecordArg(test_message)'
constexpr char kNullMessageHandlingExpr[] = R"pb(
id: 1
call_expr: <
function: "RecordArg"
args: <
ident_expr: < name: "test_message" >
id: 2
>
>
)pb";
TEST(EndToEndTest, StrictNullHandling) {
InterpreterOptions options;
Expr expr;
ASSERT_TRUE(
google::protobuf::TextFormat::ParseFromString(kNullMessageHandlingExpr, &expr));
SourceInfo info;
auto builder = CreateCelExpressionBuilder(options);
std::vector<CelValue> extension_calls;
ASSERT_OK(builder->GetRegistry()->Register(
std::make_unique<RecordArgFunction>("RecordArg", &extension_calls)));
ASSERT_OK_AND_ASSIGN(auto expression,
builder->CreateExpression(&expr, &info));
Activation activation;
google::protobuf::Arena arena;
activation.InsertValue("test_message", CelValue::CreateNull());
ASSERT_OK_AND_ASSIGN(CelValue result,
expression->Evaluate(activation, &arena));
const CelError* result_value;
ASSERT_TRUE(result.GetValue(&result_value)) << result.DebugString();
EXPECT_THAT(*result_value,
StatusIs(absl::StatusCode::kUnknown,
testing::HasSubstr("No matching overloads")));
}
TEST(EndToEndTest, OutOfRangeDurationConstant) {
InterpreterOptions options;
options.enable_timestamp_duration_overflow_errors = true;
Expr expr;
// Duration representable in absl::Duration, but out of range for CelValue
ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(
R"(
call_expr {
function: "type"
args {
const_expr {
duration_value {
seconds: 28552639587287040
}
}
}
})",
&expr));
SourceInfo info;
auto builder = CreateCelExpressionBuilder(options);
ASSERT_OK(RegisterBuiltinFunctions(builder->GetRegistry(), options));
ASSERT_OK_AND_ASSIGN(auto expression,
builder->CreateExpression(&expr, &info));
Activation activation;
google::protobuf::Arena arena;
ASSERT_OK_AND_ASSIGN(CelValue result,
expression->Evaluate(activation, &arena));
const CelError* result_value;
ASSERT_TRUE(result.GetValue(&result_value)) << result.DebugString();
EXPECT_THAT(*result_value,
StatusIs(absl::StatusCode::kInvalidArgument,
testing::HasSubstr("Duration is out of range")));
}
} // namespace
} // namespace runtime
} // namespace expr
} // namespace api
} // namespace google