-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathio_test.go
More file actions
422 lines (393 loc) · 12.4 KB
/
Copy pathio_test.go
File metadata and controls
422 lines (393 loc) · 12.4 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright 2019 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
//
// http://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.
package cel
import (
"fmt"
"strings"
"testing"
"time"
"google.golang.org/protobuf/proto"
"github.com/google/cel-go/checker/decls"
celast "github.com/google/cel-go/common/ast"
"github.com/google/cel-go/common/operators"
"github.com/google/cel-go/common/types"
"github.com/google/cel-go/common/types/ref"
proto3pb "github.com/google/cel-go/test/proto3pb"
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
)
func TestRefValueToValueRoundTrip(t *testing.T) {
tests := []struct {
value any
}{
{value: types.NullValue},
{value: types.Bool(true)},
{value: types.String("abc")},
{value: types.Double(0.0)},
{value: types.Bytes(make([]byte, 0, 5))},
{value: types.Int(0)},
{value: types.Uint(0)},
{value: types.Duration{Duration: time.Hour}},
{value: types.Timestamp{Time: time.Unix(0, 0)}},
{value: types.IntType},
{value: types.NewOpaqueType("CustomType")},
{value: map[int64]int64{1: 1}},
{value: []any{true, "abc"}},
{value: &proto3pb.TestAllTypes{SingleString: "abc"}},
}
env, err := NewEnv(Types(&proto3pb.TestAllTypes{}))
if err != nil {
t.Fatalf("NewEnv() failed: %v", err)
}
for i, tst := range tests {
tc := tst
t.Run(fmt.Sprintf("[%d]%v", i, tc.value), func(t *testing.T) {
refVal := env.TypeAdapter().NativeToValue(tc.value)
val, err := RefValueToValue(refVal)
if err != nil {
t.Fatalf("RefValueToValue(%v) failed with error: %v", refVal, err)
}
actual, err := ValueToRefValue(env.TypeAdapter(), val)
if err != nil {
t.Fatalf("ValueToRefValue() failed: %v", err)
}
if refVal.Equal(actual) != types.True {
t.Errorf("got val %v, wanted %v", actual, refVal)
}
})
}
}
func TestAstToProto(t *testing.T) {
stdEnv, _ := NewEnv(Declarations(
decls.NewVar("a", decls.Dyn),
decls.NewVar("b", decls.Dyn),
))
ast, iss := stdEnv.Parse("a + b")
if iss.Err() != nil {
t.Fatalf("Parse('a + b') failed: %v", iss.Err())
}
parsed, err := AstToParsedExpr(ast)
if err != nil {
t.Fatalf("AstToParsedExpr() failed: %v", err)
}
ast2 := ParsedExprToAst(parsed)
if !proto.Equal(ast2.Expr(), ast.Expr()) {
t.Errorf("got expr %v, wanted %v", ast2, ast)
}
ast3 := ParsedExprToAstWithSource(parsed, ast.Source())
if !proto.Equal(ast3.Expr(), ast.Expr()) {
t.Errorf("got expr %v, wanted %v", ast2, ast)
}
if ast3.Source() != ast.Source() {
t.Errorf("got source %v, wanted %v", ast3.Source(), ast.Source())
}
_, err = AstToCheckedExpr(ast)
if err == nil {
t.Error("expected error converting unchecked ast")
}
ast, iss = stdEnv.Check(ast)
if iss != nil && iss.Err() != nil {
t.Fatalf("stdEnv.Check(ast) failed: %v", iss.Err())
}
checked, err := AstToCheckedExpr(ast)
if err != nil {
t.Fatalf("AstToCheckedExpr(ast) failed: %v", err)
}
ast4 := CheckedExprToAst(checked)
if !proto.Equal(ast4.Expr(), ast.Expr()) {
t.Fatalf("got ast %v, wanted %v", ast4, ast)
}
ast5, err := CheckedExprToAstWithSource(checked, ast.Source())
if err != nil {
t.Fatalf("CheckedExprToAstWithSource() failed: %v", err)
}
if !proto.Equal(ast5.Expr(), ast.Expr()) {
t.Errorf("got expr %v, wanted %v", ast5, ast)
}
if ast5.Source() != ast.Source() {
t.Errorf("got source %v, wanted %v", ast5.Source(), ast.Source())
}
}
func TestAstToString(t *testing.T) {
stdEnv, err := NewEnv()
if err != nil {
t.Fatalf("NewEnv() failed: %v", err)
}
in := "a + b - (c ? (-d + 4) : e)"
ast, iss := stdEnv.Parse(in)
if iss.Err() != nil {
t.Fatalf("stdEnv.Parse(%q) failed: %v", in, iss.Err())
}
expr, err := AstToString(ast)
if err != nil {
t.Fatalf("AstToString(ast) failed: %v", err)
}
if expr != in {
t.Errorf("got %v, wanted %v", expr, in)
}
}
func TestExprToString(t *testing.T) {
stdEnv, err := NewEnv(EnableMacroCallTracking())
if err != nil {
t.Fatalf("NewEnv() failed: %v", err)
}
in := "[a, b].filter(i, (i > 0) ? (-i + 4) : i)"
ast, iss := stdEnv.Parse(in)
if iss.Err() != nil {
t.Fatalf("stdEnv.Parse(%q) failed: %v", in, iss.Err())
}
expr, err := ExprToString(ast.NativeRep().Expr(), ast.NativeRep().SourceInfo())
if err != nil {
t.Fatalf("ExprToString(ast) failed: %v", err)
}
if expr != in {
t.Errorf("got %v, wanted %v", expr, in)
}
// Test sub-expression unparsing.
navExpr := celast.NavigateAST(ast.NativeRep())
condExpr := celast.MatchDescendants(navExpr, celast.FunctionMatcher(operators.Conditional))[0]
want := `(i > 0) ? (-i + 4) : i`
expr, err = ExprToString(condExpr, ast.NativeRep().SourceInfo())
if err != nil {
t.Fatalf("ExprToString(ast) failed: %v", err)
}
if expr != want {
t.Errorf("got %v, wanted %v", expr, want)
}
// Also passes with a nil source info, but only because the sub-expr doesn't contain macro calls.
expr, err = ExprToString(condExpr, nil)
if err != nil {
t.Fatalf("ExprToString(ast) failed: %v", err)
}
if expr != want {
t.Errorf("got %v, wanted %v", expr, want)
}
// Fails do to missing macro information.
_, err = ExprToString(ast.NativeRep().Expr(), nil)
if err == nil {
t.Error("ExprToString() succeeded, wanted error")
}
}
func TestRefValToExprValue(t *testing.T) {
tests := []struct {
name string
refVal ref.Val
expectError bool
}{
{
name: "unknown value",
refVal: types.NewUnknown(1, nil),
expectError: false,
},
{
name: "error value",
refVal: types.NewErr("test error"),
expectError: false,
},
{
name: "bool value",
refVal: types.Bool(true),
expectError: false,
},
{
name: "string value",
refVal: types.String("test"),
expectError: false,
},
{
name: "int value",
refVal: types.Int(1),
expectError: false,
},
}
for _, tst := range tests {
tc := tst
t.Run(tc.name, func(t *testing.T) {
exprVal, err := ExprValueAsProto(tc.refVal)
if tc.expectError {
if err == nil {
t.Errorf("RefValToExprValue(%v) expected error, got %v", tc.refVal, exprVal)
}
} else {
if err != nil {
t.Errorf("RefValToExprValue(%v) failed with error: %v", tc.refVal, err)
}
if exprVal == nil {
t.Errorf("RefValToExprValue(%v) expected value, got nil", tc.refVal)
}
}
})
}
}
func TestAstToStringNil(t *testing.T) {
expr, err := AstToString(nil)
if err == nil || !strings.Contains(err.Error(), "unsupported expr") {
t.Errorf("env.AstToString() got (%v, %v) wanted unsupported expr error", expr, err)
}
}
func TestAstToCheckedExprNil(t *testing.T) {
expr, err := AstToCheckedExpr(nil)
if err == nil || !strings.Contains(err.Error(), "cannot convert unchecked ast") {
t.Errorf("env.AstToCheckedExpr() got (%v, %v) wanted conversion error", expr, err)
}
}
func TestAstToParsedExprNil(t *testing.T) {
expr, err := AstToParsedExpr(nil)
if err != nil {
t.Errorf("env.AstToParsedExpr() got (%v, %v) wanted conversion error", expr, err)
}
}
func TestCheckedExprToAstConstantExpr(t *testing.T) {
stdEnv, err := NewEnv()
if err != nil {
t.Fatalf("NewEnv() failed: %v", err)
}
in := "10"
ast, iss := stdEnv.Compile(in)
if iss.Err() != nil {
t.Fatalf("stdEnv.Compile(%q) failed: %v", in, iss.Err())
}
expr, err := AstToCheckedExpr(ast)
if err != nil {
t.Fatalf("AstToCheckedExpr(ast) failed: %v", err)
}
ast2 := CheckedExprToAst(expr)
if !proto.Equal(ast2.Expr(), ast.Expr()) {
t.Fatalf("got ast %v, wanted %v", ast2, ast)
}
}
func TestCheckedExprToAstMissingInfo(t *testing.T) {
stdEnv, err := NewEnv()
if err != nil {
t.Fatalf("NewEnv() failed: %v", err)
}
in := "10"
ast, iss := stdEnv.Parse(in)
if iss.Err() != nil {
t.Fatalf("stdEnv.Compile(%q) failed: %v", in, iss.Err())
}
if ast.ResultType() != decls.Dyn {
t.Fatalf("ast.ResultType() got %v, wanted 'dyn'", ast.ResultType())
}
expr, err := AstToParsedExpr(ast)
if err != nil {
t.Fatalf("AstToParsedExpr(ast) failed: %v", err)
}
checkedExpr := &exprpb.CheckedExpr{
TypeMap: map[int64]*exprpb.Type{expr.GetExpr().GetId(): decls.Int},
Expr: expr.GetExpr(),
}
ast2 := CheckedExprToAst(checkedExpr)
if !ast2.IsChecked() {
t.Fatal("CheckedExprToAst() did not produce a 'checked' ast")
}
if ast2.ResultType() != decls.Int {
t.Fatalf("ast2.ResultType() got %v, wanted 'int'", ast.ResultType())
}
}
// deepBoolExpr builds a synthetic deeply nested proto Expr by stacking `depth` unary `!` calls
// on top of a boolean literal. A depth well above the 250 default but far below the Go stack
// limit keeps the test itself from overflowing while still exercising the depth guard.
func deepBoolExpr(depth int) *exprpb.Expr {
expr := &exprpb.Expr{
Id: 1,
ExprKind: &exprpb.Expr_ConstExpr{
ConstExpr: &exprpb.Constant{
ConstantKind: &exprpb.Constant_BoolValue{BoolValue: true},
},
},
}
for i := 0; i < depth; i++ {
expr = &exprpb.Expr{
Id: int64(i + 2),
ExprKind: &exprpb.Expr_CallExpr{
CallExpr: &exprpb.Expr_Call{
Function: operators.LogicalNot,
Args: []*exprpb.Expr{expr},
},
},
}
}
return expr
}
func TestLoadedAstDepthLimit(t *testing.T) {
env, err := NewEnv()
if err != nil {
t.Fatalf("NewEnv() failed: %v", err)
}
// Sanity check: a shallow parsed expression still checks and plans clean.
shallow, iss := env.Parse("1 + 2")
if iss.Err() != nil {
t.Fatalf("Parse('1 + 2') failed: %v", iss.Err())
}
if _, iss := env.Check(shallow); iss.Err() != nil {
t.Fatalf("Check(shallow) failed: %v", iss.Err())
}
if _, err := env.Program(shallow); err != nil {
t.Fatalf("Program(shallow) failed: %v", err)
}
const depth = 300
deepExpr := deepBoolExpr(depth)
// ParsedExprToAst flags the over-deep AST at conversion time. Because the conversion helper
// has no error return, the violation is surfaced as a normal error when the AST is later
// planned or checked, rather than recursing into a Go stack overflow.
deepParsed := ParsedExprToAst(&exprpb.ParsedExpr{Expr: deepExpr})
if _, err := env.Program(deepParsed); err == nil {
t.Errorf("Program(deepParsed) expected an error, got nil")
} else if !strings.Contains(err.Error(), "maximum expression nesting depth") {
t.Errorf("Program(deepParsed) error = %v, want it to mention 'maximum expression nesting depth'", err)
}
if _, iss := env.Check(deepParsed); iss.Err() == nil {
t.Errorf("Check(deepParsed) expected an error, got nil")
} else if !strings.Contains(iss.Err().Error(), "maximum expression nesting depth") {
t.Errorf("Check(deepParsed) error = %v, want it to mention 'maximum expression nesting depth'", iss.Err())
}
// CheckedExprToAstWithSource returns the depth error directly since it has an error return.
if _, err := CheckedExprToAstWithSource(&exprpb.CheckedExpr{Expr: deepExpr}, nil); err == nil {
t.Errorf("CheckedExprToAstWithSource(deep) expected an error, got nil")
} else if !strings.Contains(err.Error(), "maximum expression nesting depth") {
t.Errorf("CheckedExprToAstWithSource(deep) error = %v, want it to mention 'maximum expression nesting depth'", err)
}
// Embedders in full control of their AST inputs can skip the check by building the AST through
// the common/ast package directly rather than the cel conversion helpers.
nativeExpr, err := celast.ProtoToExpr(deepExpr)
if err != nil {
t.Fatalf("celast.ProtoToExpr() failed: %v", err)
}
bypass := &Ast{impl: celast.NewAST(nativeExpr, celast.NewSourceInfo(nil))}
if _, err := env.Program(bypass); err != nil {
t.Errorf("Program(bypass) built directly via common/ast failed: %v", err)
}
}
func TestExpressionNestingDepthLimitConfigRoundTrip(t *testing.T) {
env, err := NewEnv(ExpressionNestingDepthLimit(128))
if err != nil {
t.Fatalf("NewEnv(ExpressionNestingDepthLimit(128)) failed: %v", err)
}
conf, err := env.ToConfig("depth-limit")
if err != nil {
t.Fatalf("env.ToConfig() failed: %v", err)
}
found := false
for _, limit := range conf.Limits {
if limit.Name == "cel.limit.max_ast_depth" {
found = true
if limit.Value != 128 {
t.Errorf("limit %q value = %d, wanted 128", limit.Name, limit.Value)
}
}
}
if !found {
t.Errorf("env config limits %v missing 'cel.limit.max_ast_depth'", conf.Limits)
}
}