-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathCelImplTest.java
More file actions
2177 lines (1981 loc) · 91.2 KB
/
CelImplTest.java
File metadata and controls
2177 lines (1981 loc) · 91.2 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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.
package dev.cel.bundle;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat;
import static dev.cel.common.CelFunctionDecl.newFunctionDeclaration;
import static dev.cel.common.CelOverloadDecl.newGlobalOverload;
import static dev.cel.common.CelOverloadDecl.newMemberOverload;
import static org.junit.Assert.assertThrows;
import dev.cel.expr.CheckedExpr;
import dev.cel.expr.Constant;
import dev.cel.expr.Decl;
import dev.cel.expr.Decl.FunctionDecl;
import dev.cel.expr.Decl.FunctionDecl.Overload;
import dev.cel.expr.Decl.IdentDecl;
import dev.cel.expr.Expr;
import dev.cel.expr.Expr.Call;
import dev.cel.expr.Expr.Ident;
import dev.cel.expr.Expr.Select;
import dev.cel.expr.ParsedExpr;
import dev.cel.expr.Reference;
import dev.cel.expr.Type;
import dev.cel.expr.Type.PrimitiveType;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import com.google.protobuf.DescriptorProtos.FileDescriptorProto;
import com.google.protobuf.DescriptorProtos.FileDescriptorSet;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.FileDescriptor;
import com.google.protobuf.Duration;
import com.google.protobuf.DynamicMessage;
import com.google.protobuf.Empty;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.FieldMask;
import com.google.protobuf.Message;
import com.google.protobuf.Struct;
import com.google.protobuf.TextFormat;
import com.google.protobuf.Timestamp;
import com.google.protobuf.TypeRegistry;
import com.google.protobuf.WrappersProto;
import com.google.rpc.context.AttributeContext;
import com.google.testing.junit.testparameterinjector.TestParameter;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
import com.google.testing.junit.testparameterinjector.TestParameters;
import dev.cel.checker.CelCheckerLegacyImpl;
import dev.cel.checker.DescriptorTypeProvider;
import dev.cel.checker.ProtoTypeMask;
import dev.cel.checker.TypeProvider;
import dev.cel.common.CelAbstractSyntaxTree;
import dev.cel.common.CelContainer;
import dev.cel.common.CelDescriptorUtil;
import dev.cel.common.CelErrorCode;
import dev.cel.common.CelIssue;
import dev.cel.common.CelOptions;
import dev.cel.common.CelProtoAbstractSyntaxTree;
import dev.cel.common.CelSourceLocation;
import dev.cel.common.CelValidationException;
import dev.cel.common.CelValidationResult;
import dev.cel.common.CelVarDecl;
import dev.cel.common.ast.CelExpr;
import dev.cel.common.ast.CelExpr.CelList;
import dev.cel.common.testing.RepeatedTestProvider;
import dev.cel.common.types.CelKind;
import dev.cel.common.types.CelProtoMessageTypes;
import dev.cel.common.types.CelProtoTypes;
import dev.cel.common.types.CelType;
import dev.cel.common.types.EnumType;
import dev.cel.common.types.ListType;
import dev.cel.common.types.MapType;
import dev.cel.common.types.OptionalType;
import dev.cel.common.types.ProtoMessageTypeProvider;
import dev.cel.common.types.SimpleType;
import dev.cel.common.types.StructTypeReference;
import dev.cel.common.values.CelByteString;
import dev.cel.common.values.NullValue;
import dev.cel.compiler.CelCompiler;
import dev.cel.compiler.CelCompilerFactory;
import dev.cel.compiler.CelCompilerImpl;
import dev.cel.expr.conformance.proto2.Proto2ExtensionScopedMessage;
import dev.cel.expr.conformance.proto2.TestAllTypesExtensions;
import dev.cel.expr.conformance.proto3.TestAllTypes;
import dev.cel.parser.CelParserImpl;
import dev.cel.parser.CelStandardMacro;
import dev.cel.runtime.CelAttribute;
import dev.cel.runtime.CelAttribute.Qualifier;
import dev.cel.runtime.CelAttributePattern;
import dev.cel.runtime.CelEvaluationException;
import dev.cel.runtime.CelEvaluationExceptionBuilder;
import dev.cel.runtime.CelFunctionBinding;
import dev.cel.runtime.CelRuntime;
import dev.cel.runtime.CelRuntime.Program;
import dev.cel.runtime.CelRuntimeFactory;
import dev.cel.runtime.CelRuntimeLegacyImpl;
import dev.cel.runtime.CelUnknownSet;
import dev.cel.runtime.CelVariableResolver;
import dev.cel.runtime.UnknownContext;
import dev.cel.testing.testdata.proto3.StandaloneGlobalEnum;
import java.time.Instant;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import org.jspecify.annotations.Nullable;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(TestParameterInjector.class)
public final class CelImplTest {
private static final Expr NOT_EXPR =
Expr.newBuilder()
.setCallExpr(
Call.newBuilder()
.setFunction("!_")
.addArgs(
Expr.newBuilder().setConstExpr(Constant.newBuilder().setBoolValue(true))))
.build();
private static final Expr NOT_NOT_NOT_EXPR =
Expr.newBuilder()
.setCallExpr(
Call.newBuilder()
.setFunction("!_")
.addArgs(
Expr.newBuilder()
.setCallExpr(Call.newBuilder().setFunction("!_").addArgs(NOT_EXPR))))
.build();
private static final Expr EXPR =
Expr.newBuilder()
.setCallExpr(
Call.newBuilder()
.setFunction("_&&_")
.addArgs(Expr.newBuilder().setConstExpr(Constant.newBuilder().setBoolValue(true)))
.addArgs(
Expr.newBuilder()
.setCallExpr(
Call.newBuilder()
.setFunction("!_")
.addArgs(
Expr.newBuilder()
.setConstExpr(
Constant.newBuilder().setBoolValue(false))))))
.build();
private static final ParsedExpr PARSED_EXPR = ParsedExpr.newBuilder().setExpr(EXPR).build();
private static final CheckedExpr CHECKED_EXPR =
CheckedExpr.newBuilder()
.setExpr(EXPR)
.putTypeMap(1L, CelProtoTypes.BOOL)
.putTypeMap(2L, CelProtoTypes.BOOL)
.putTypeMap(3L, CelProtoTypes.BOOL)
.putTypeMap(4L, CelProtoTypes.BOOL)
.putReferenceMap(2L, Reference.newBuilder().addOverloadId("logical_and").build())
.putReferenceMap(3L, Reference.newBuilder().addOverloadId("logical_not").build())
.build();
private static final ParsedExpr PARSED_HAS_EXPR =
ParsedExpr.newBuilder()
.setExpr(
Expr.newBuilder()
.setSelectExpr(
Select.newBuilder()
.setOperand(
Expr.newBuilder().setIdentExpr(Ident.newBuilder().setName("a")))
.setField("b")
.setTestOnly(true)))
.build();
private CelBuilder standardCelBuilderWithMacros() {
return CelFactory.standardCelBuilder().setStandardMacros(CelStandardMacro.STANDARD_MACROS);
}
@Test
public void build_badFileDescriptorSet() {
IllegalArgumentException e =
Assert.assertThrows(
IllegalArgumentException.class,
() ->
standardCelBuilderWithMacros()
.setContainer(CelContainer.ofName("cel.expr.conformance.proto2"))
.addFileTypes(
FileDescriptorSet.newBuilder()
.addFile(TestAllTypesExtensions.getDescriptor().getFile().toProto())
.build())
.build());
assertThat(e).hasMessageThat().contains("file descriptor set with unresolved proto file");
}
@Test
public void parse() throws Exception {
Cel cel = standardCelBuilderWithMacros().build();
assertValidationResult(cel.parse("true && !false"), PARSED_EXPR);
}
@Test
public void check() throws Exception {
Cel cel = standardCelBuilderWithMacros().setResultType(SimpleType.BOOL).build();
CelValidationResult parseResult = cel.parse("true && !false");
assertValidationResult(parseResult, PARSED_EXPR);
CelValidationResult checkResult = cel.check(parseResult.getAst());
assertValidationResult(checkResult, CHECKED_EXPR);
}
@Test
@TestParameters("{useProtoResultType: false}")
@TestParameters("{useProtoResultType: true}")
public void compile(boolean useProtoResultType) throws Exception {
CelBuilder celBuilder = standardCelBuilderWithMacros();
if (useProtoResultType) {
celBuilder.setProtoResultType(CelProtoTypes.BOOL);
} else {
celBuilder.setResultType(SimpleType.BOOL);
}
Cel cel = celBuilder.build();
assertValidationResult(cel.compile("true && !false"), CHECKED_EXPR);
}
@Test
@TestParameters("{useProtoResultType: false}")
@TestParameters("{useProtoResultType: true}")
public void compile_resultTypeCheckFailure(boolean useProtoResultType) {
CelBuilder celBuilder = standardCelBuilderWithMacros();
if (useProtoResultType) {
celBuilder.setProtoResultType(CelProtoTypes.STRING);
} else {
celBuilder.setResultType(SimpleType.STRING);
}
Cel cel = celBuilder.build();
CelValidationResult validationResult = cel.compile("true && !false");
assertThat(validationResult.hasError()).isTrue();
assertThat(validationResult.getErrorString())
.contains("expected type 'string' but found 'bool'");
}
@Test
public void compile_combinedTypeProvider() {
ProtoMessageTypeProvider celTypeProvider =
new ProtoMessageTypeProvider(ImmutableList.of(AttributeContext.getDescriptor()));
Cel cel =
standardCelBuilderWithMacros()
.setContainer(CelContainer.ofName("google"))
.setTypeProvider(celTypeProvider)
.addMessageTypes(com.google.type.Expr.getDescriptor())
.addProtoTypeMasks(
ImmutableList.of(ProtoTypeMask.ofAllFields("google.rpc.context.AttributeContext")))
.addVar("condition", StructTypeReference.create("google.type.Expr"))
.setProtoResultType(CelProtoTypes.BOOL)
.build();
CelValidationResult result =
cel.compile("type.Expr{expression: \"'hello'\"}.expression == condition.expression");
assertThat(result.getErrorString()).isEmpty();
}
@Test
public void compile_customTypeProvider() {
ProtoMessageTypeProvider celTypeProvider =
new ProtoMessageTypeProvider(
ImmutableList.of(
AttributeContext.getDescriptor(), com.google.type.Expr.getDescriptor()));
Cel cel =
standardCelBuilderWithMacros()
.setContainer(CelContainer.ofName("google"))
.setTypeProvider(celTypeProvider)
.addVar("condition", StructTypeReference.create("google.type.Expr"))
.setResultType(SimpleType.BOOL)
.build();
CelValidationResult result =
cel.compile("type.Expr{expression: \"'hello'\"}.expression == condition.expression");
assertThat(result.getErrorString()).isEmpty();
}
@Test
public void compile_customTypesWithAliasingCombinedProviders() throws Exception {
// The custom type provider sets up an alias from "Condition" to "google.type.Expr".
// However, the first type resolution from the alias to the qualified type name won't be
// sufficient as future checks will expect the resolved alias to also be a type.
TypeProvider customTypeProvider =
aliasingProvider(
ImmutableMap.of("Condition", CelProtoTypes.createMessage("google.type.Expr")));
// The registration of the aliasing TypeProvider and the google.type.Expr descriptor
// ensures that once the alias is resolved, the additional details about the Expr type
// are discoverable.
//
// The custom type factory is then necessary to ensure that the Condition type listed
// in the AST can be resolved to the appropriate message builder instance.
Cel cel =
standardCelBuilderWithMacros()
.setTypeProvider(customTypeProvider)
.addMessageTypes(com.google.type.Expr.getDescriptor())
.setTypeFactory(
(typeName) ->
typeName.equals("Condition") ? com.google.type.Expr.newBuilder() : null)
.setResultType(StructTypeReference.create("google.type.Expr"))
.build();
CelValidationResult result = cel.compile("Condition{expression: \"'hello'\"}");
assertThat(result.getErrorString()).isEmpty();
CelRuntime.Program program = cel.createProgram(result.getAst());
assertThat(program.eval())
.isEqualTo(com.google.type.Expr.newBuilder().setExpression("'hello'").build());
}
@Test
public void compile_customTypesWithAliasingSelfContainedProvider() throws Exception {
// The custom type provider sets up an alias from "Condition" to "google.type.Expr".
TypeProvider customTypeProvider =
aliasingProvider(
ImmutableMap.of(
"Condition",
CelProtoTypes.createMessage("google.type.Expr"),
"google.type.Expr",
CelProtoTypes.createMessage("google.type.Expr")));
// The registration of the aliasing TypeProvider and the google.type.Expr descriptor
// ensures that once the alias is resolved, the additional details about the Expr type
// are discoverable.
//
// The custom type factory is then necessary to ensure that the Condition type listed
// in the AST can be resolved to the appropriate message builder instance.
Cel cel =
standardCelBuilderWithMacros()
.setTypeProvider(customTypeProvider)
.setTypeFactory(
(typeName) ->
typeName.equals("Condition") ? com.google.type.Expr.newBuilder() : null)
.setResultType(StructTypeReference.create("google.type.Expr"))
.build();
CelValidationResult result = cel.compile("Condition{expression: \"'hello'\"}");
assertThat(result.getErrorString()).isEmpty();
CelRuntime.Program program = cel.createProgram(result.getAst());
assertThat(program.eval())
.isEqualTo(com.google.type.Expr.newBuilder().setExpression("'hello'").build());
}
@Test
public void program_setTypeFactoryOnAnyPackedMessage_fieldSelectionSuccess() throws Exception {
// Arrange
CelCompiler celCompiler =
CelCompilerFactory.standardCelCompilerBuilder()
.addVar("input", StructTypeReference.create("google.type.Expr"))
.addMessageTypes(com.google.type.Expr.getDescriptor())
.setResultType(SimpleType.STRING)
.build();
CelAbstractSyntaxTree ast = celCompiler.compile("input.expression").getAst();
CelRuntime celRuntime =
CelRuntimeFactory.standardCelRuntimeBuilder()
.setTypeFactory(
(typeName) ->
typeName.equals("google.type.Expr") ? com.google.type.Expr.newBuilder() : null)
.build();
CelRuntime.Program program = celRuntime.createProgram(ast);
Message exprMessage = com.google.type.Expr.newBuilder().setExpression("test").build();
// Act
Object evaluatedResult1 = program.eval(ImmutableMap.of("input", exprMessage));
Object evaluatedResult2 = program.eval(ImmutableMap.of("input", Any.pack(exprMessage)));
// Assert
assertThat(evaluatedResult1).isEqualTo("test");
assertThat(evaluatedResult2).isEqualTo("test");
}
@Test
public void program_setTypeFactoryOnAnyPackedMessage_messageConstructionSucceeds()
throws Exception {
// Arrange
CelCompiler celCompiler =
CelCompilerFactory.standardCelCompilerBuilder()
.addVar("input", StructTypeReference.create("google.type.Expr"))
.addMessageTypes(com.google.type.Expr.getDescriptor())
.build();
CelAbstractSyntaxTree ast = celCompiler.compile("input").getAst();
CelRuntime celRuntime =
CelRuntimeFactory.standardCelRuntimeBuilder()
.setTypeFactory(
(typeName) ->
typeName.equals("google.type.Expr") ? com.google.type.Expr.newBuilder() : null)
.build();
CelRuntime.Program program = celRuntime.createProgram(ast);
Message exprMessage = com.google.type.Expr.newBuilder().setExpression("test").build();
// Act
Object evaluatedResult1 = program.eval(ImmutableMap.of("input", exprMessage));
Object evaluatedResult2 = program.eval(ImmutableMap.of("input", Any.pack(exprMessage)));
// Assert
assertThat(evaluatedResult1).isEqualTo(exprMessage);
assertThat(evaluatedResult2).isEqualTo(exprMessage);
}
@Test
@SuppressWarnings("unused") // testRunIndex name retained for test result readability
public void program_concurrentMessageConstruction_succeeds(
@TestParameter(valuesProvider = RepeatedTestProvider.class) int testRunIndex)
throws Exception {
// Arrange
int threadCount = 10;
Cel cel =
standardCelBuilderWithMacros()
.setContainer(CelContainer.ofName("google.rpc.context.AttributeContext"))
.addFileTypes(
Any.getDescriptor().getFile(),
Duration.getDescriptor().getFile(),
Struct.getDescriptor().getFile(),
Timestamp.getDescriptor().getFile(),
AttributeContext.getDescriptor().getFile())
.setResultType(
StructTypeReference.create("google.rpc.context.AttributeContext.Resource"))
.build();
CelRuntime.Program program =
cel.createProgram(cel.compile("Resource{name: \"'hello'\"}").getAst());
ExecutorService executor =
MoreExecutors.getExitingExecutorService(
(ThreadPoolExecutor) Executors.newFixedThreadPool(threadCount));
// Act
List<Future<AttributeContext.Resource>> futures = new ArrayList<>();
for (int i = 0; i < threadCount; i++) {
futures.add(executor.submit(() -> (AttributeContext.Resource) program.eval()));
}
// Assert
AttributeContext.Resource expectedResult =
AttributeContext.Resource.newBuilder().setName("'hello'").build();
for (Future<AttributeContext.Resource> future : futures) {
assertThat(future.get()).isEqualTo(expectedResult);
}
}
@Test
public void compile_syntaxFailure() throws Exception {
Cel cel = standardCelBuilderWithMacros().build();
CelValidationResult result = cel.compile("|| false");
assertThat(result.hasError()).isTrue();
assertThat(result.getErrors())
.containsExactly(
CelIssue.formatError(
1,
0,
"extraneous input '||' expecting {'[', '{', '(', '.', '-', '!', 'true', 'false',"
+ " 'null', NUM_FLOAT, NUM_INT, NUM_UINT, STRING, BYTES, IDENTIFIER}"));
assertThat(result.getErrorString())
.isEqualTo(
"ERROR: <input>:1:1: extraneous input '||' expecting {'[', '{', '(', '.', '-', '!',"
+ " 'true', 'false', 'null', NUM_FLOAT, NUM_INT, NUM_UINT, STRING, BYTES,"
+ " IDENTIFIER}\n"
+ " | || false\n"
+ " | ^");
}
@Test
public void compile_typeCheckFailure() {
Cel cel = standardCelBuilderWithMacros().build();
CelValidationResult syntaxErrorResult = cel.compile("variable");
assertThat(syntaxErrorResult.hasError()).isTrue();
assertThat(syntaxErrorResult.getErrors())
.containsExactly(
CelIssue.formatError(
/* exprId= */ 1L,
CelSourceLocation.of(1, 0),
"undeclared reference to 'variable' (in container '')"));
assertThat(syntaxErrorResult.getErrorString())
.isEqualTo(
"ERROR: <input>:1:1: undeclared reference to 'variable' (in container '')\n"
+ " | variable\n"
+ " | ^");
}
@Test
public void compile_withOptionalTypes() throws Exception {
Cel cel =
CelFactory.standardCelBuilder()
.setOptions(CelOptions.current().enableOptionalSyntax(true).build())
.addVar("a", OptionalType.create(SimpleType.STRING))
.build();
CelAbstractSyntaxTree ast = cel.compile("[?a]").getAst();
CelList list = ast.getExpr().list();
assertThat(list.optionalIndices()).containsExactly(0);
assertThat(list.elements()).containsExactly(CelExpr.ofIdent(2, "a"));
}
@Test
public void compile_overlappingVarsFailure() {
Cel cel =
standardCelBuilderWithMacros()
.addDeclarations(
Decl.newBuilder()
.setName("variable")
.setIdent(IdentDecl.newBuilder().setType(CelProtoTypes.STRING))
.build())
.addDeclarations(
Decl.newBuilder()
.setName("variable")
.setIdent(
IdentDecl.newBuilder()
.setType(CelProtoTypes.createList(CelProtoTypes.STRING)))
.build())
.setResultType(SimpleType.BOOL)
.build();
CelValidationException e =
Assert.assertThrows(
CelValidationException.class, () -> cel.compile("variable == 'hello'").getAst());
assertThat(e).hasMessageThat().contains("variable");
}
@Test
public void program() throws Exception {
Cel cel = standardCelBuilderWithMacros().setResultType(SimpleType.BOOL).build();
CelRuntime.Program program = cel.createProgram(cel.compile("true && !false").getAst());
assertThat(program.eval()).isEqualTo(true);
}
@Test
public void program_withVars() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.addDeclarations(
Decl.newBuilder()
.setName("variable")
.setIdent(IdentDecl.newBuilder().setType(CelProtoTypes.STRING))
.build())
.setResultType(SimpleType.BOOL)
.build();
CelRuntime.Program program = cel.createProgram(cel.compile("variable == 'hello'").getAst());
assertThat(program.eval(ImmutableMap.of("variable", "hello"))).isEqualTo(true);
}
@Test
public void program_withProtoVars() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.addMessageTypes(AttributeContext.getDescriptor())
.addProtoTypeMasks(
ProtoTypeMask.of(
"google.rpc.context.AttributeContext",
FieldMask.newBuilder().addPaths("resource.*").build())
.withFieldsAsVariableDeclarations())
.setResultType(SimpleType.BOOL)
.build();
CelRuntime.Program program =
cel.createProgram(
cel.compile("resource.name == 'secure' && resource.type == 'compute.vm'").getAst());
assertThat(
program.eval(
AttributeContext.newBuilder()
.setResource(
AttributeContext.Resource.newBuilder()
.setName("secure")
.setType("compute.vm"))
.build()))
.isEqualTo(true);
}
@Test
public void program_withAllFieldsHidden_emptyMessageConstructionSuccess() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.addMessageTypes(AttributeContext.getDescriptor())
.setContainer(CelContainer.ofName("google.rpc.context.AttributeContext"))
.addProtoTypeMasks(
ProtoTypeMask.ofAllFieldsHidden("google.rpc.context.AttributeContext"))
.build();
assertThat(cel.createProgram(cel.compile("AttributeContext{}").getAst()).eval())
.isEqualTo(AttributeContext.getDefaultInstance());
}
@Test
public void compile_withAllFieldsHidden_selectHiddenField_throws() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.addMessageTypes(AttributeContext.getDescriptor())
.setContainer(CelContainer.ofName("google.rpc.context.AttributeContext"))
.addProtoTypeMasks(
ProtoTypeMask.ofAllFieldsHidden("google.rpc.context.AttributeContext"))
.build();
CelValidationException e =
assertThrows(
CelValidationException.class,
() -> cel.compile("AttributeContext{ request: AttributeContext.Request{} }").getAst());
assertThat(e).hasMessageThat().contains("undefined field 'request'");
}
@Test
public void compile_withAllFieldsHidden_selectHiddenFieldOnVar_throws() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.addMessageTypes(AttributeContext.getDescriptor())
.setContainer(CelContainer.ofName("google.rpc.context.AttributeContext"))
.addProtoTypeMasks(
ProtoTypeMask.ofAllFieldsHidden("google.rpc.context.AttributeContext"))
.addVar("attr_ctx", StructTypeReference.create("google.rpc.context.AttributeContext"))
.build();
CelValidationException e =
assertThrows(CelValidationException.class, () -> cel.compile("attr_ctx.source").getAst());
assertThat(e).hasMessageThat().contains("undefined field 'source'");
}
@Test
public void program_withNestedRestrictedProtoVars() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.addMessageTypes(AttributeContext.getDescriptor())
.addProtoTypeMasks(
ProtoTypeMask.of(
"google.rpc.context.AttributeContext",
FieldMask.newBuilder().addPaths("resource.type").build())
.withFieldsAsVariableDeclarations())
.setResultType(SimpleType.BOOL)
.build();
CelValidationException e =
assertThrows(
CelValidationException.class,
() -> cel.compile("{1:resource}[1].name == 'secure'").getAst());
assertThat(e).hasMessageThat().contains("undefined field 'name'");
}
@Test
public void program_withFunctions() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.addDeclarations(
ImmutableList.of(
Decl.newBuilder()
.setName("one")
.setIdent(IdentDecl.newBuilder().setType(CelProtoTypes.BOOL))
.build(),
Decl.newBuilder()
.setName("two")
.setIdent(IdentDecl.newBuilder().setType(CelProtoTypes.BOOL))
.build(),
Decl.newBuilder()
.setName("any")
.setFunction(
FunctionDecl.newBuilder()
.addOverloads(
Overload.newBuilder()
.setOverloadId("any_bool")
.addParams(CelProtoTypes.BOOL)
.setResultType(CelProtoTypes.BOOL))
.addOverloads(
Overload.newBuilder()
.setOverloadId("any_bool_bool")
.addParams(CelProtoTypes.BOOL)
.addParams(CelProtoTypes.BOOL)
.setResultType(CelProtoTypes.BOOL))
.addOverloads(
Overload.newBuilder()
.setOverloadId("any_bool_bool_bool")
.addParams(CelProtoTypes.BOOL)
.addParams(CelProtoTypes.BOOL)
.addParams(CelProtoTypes.BOOL)
.setResultType(CelProtoTypes.BOOL)))
.build()))
.addFunctionBindings(CelFunctionBinding.from("any_bool", Boolean.class, (arg) -> arg))
.addFunctionBindings(
ImmutableList.of(
CelFunctionBinding.from(
"any_bool_bool",
Boolean.class,
Boolean.class,
(arg1, arg2) -> (boolean) arg1 || (boolean) arg2),
CelFunctionBinding.from(
"any_bool_bool_bool",
ImmutableList.of(Boolean.class, Boolean.class, Boolean.class),
(args) -> (boolean) args[0] || (boolean) args[1] || (boolean) args[2])))
.setResultType(SimpleType.BOOL)
.build();
CelRuntime.Program program =
cel.createProgram(
cel.compile("any(true) && any(false, one) && any(false, two, false)").getAst());
assertThat(program.eval(ImmutableMap.of("one", true, "two", true))).isEqualTo(true);
}
@Test
public void program_withThrowingFunction() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.addDeclarations(
Decl.newBuilder()
.setName("throws")
.setFunction(
FunctionDecl.newBuilder()
.addOverloads(
Overload.newBuilder()
.setOverloadId("throws")
.setResultType(CelProtoTypes.BOOL)))
.build())
.addFunctionBindings(
CelFunctionBinding.from(
"throws",
ImmutableList.of(),
(args) -> {
throw new CelEvaluationException("this method always throws");
}))
.setResultType(SimpleType.BOOL)
.build();
CelRuntime.Program program = cel.createProgram(cel.compile("throws()").getAst());
CelEvaluationException e = Assert.assertThrows(CelEvaluationException.class, program::eval);
assertThat(e).hasMessageThat().contains("this method always throws");
}
@Test
public void program_withThrowingFunctionShortcircuited() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.addDeclarations(
Decl.newBuilder()
.setName("throws")
.setFunction(
FunctionDecl.newBuilder()
.addOverloads(
Overload.newBuilder()
.setOverloadId("throws")
.setResultType(CelProtoTypes.BOOL)))
.build())
.addFunctionBindings(
CelFunctionBinding.from(
"throws",
ImmutableList.of(),
(args) -> {
throw CelEvaluationExceptionBuilder.newBuilder("this method always throws")
.setCause(new RuntimeException("reason"))
.build();
}))
.setResultType(SimpleType.BOOL)
.build();
CelRuntime.Program program = cel.createProgram(cel.compile("throws() || true").getAst());
assertThat(program.eval()).isEqualTo(true);
}
@Test
public void program_simpleStructTypeReference() throws Exception {
CelCompiler celCompiler =
CelCompilerFactory.standardCelCompilerBuilder()
.addVar("test", StructTypeReference.create(Expr.getDescriptor().getFullName()))
.addMessageTypes(Expr.getDescriptor())
.setResultType(SimpleType.BOOL)
.build();
CelRuntime celRuntime = CelRuntimeFactory.standardCelRuntimeBuilder().build();
CelRuntime.Program program =
celRuntime.createProgram(celCompiler.compile("test.id == 2").getAst());
Object evaluatedResult =
program.eval(ImmutableMap.of("test", Expr.newBuilder().setId(2).build()));
assertThat(evaluatedResult).isEqualTo(true);
}
@Test
public void program_messageConstruction() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.setContainer(CelContainer.ofName("google.type"))
.addMessageTypes(com.google.type.Expr.getDescriptor())
.setResultType(StructTypeReference.create("google.type.Expr"))
.setStandardEnvironmentEnabled(false)
.build();
CelRuntime.Program program =
cel.createProgram(cel.compile("type.Expr{expression: \"'hello'\"}").getAst());
assertThat(program.eval())
.isEqualTo(com.google.type.Expr.newBuilder().setExpression("'hello'").build());
}
@Test
public void program_duplicateTypeDescriptor() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.addMessageTypes(Timestamp.getDescriptor())
.addMessageTypes(ImmutableList.of(Timestamp.getDescriptor()))
.setContainer(CelContainer.ofName("google"))
.setResultType(SimpleType.TIMESTAMP)
.build();
CelRuntime.Program program =
cel.createProgram(cel.compile("protobuf.Timestamp{seconds: 12}").getAst());
assertThat(program.eval()).isEqualTo(Instant.ofEpochSecond(12));
}
@Test
public void program_hermeticDescriptors_wellKnownProtobuf() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.addMessageTypes(Timestamp.getDescriptor())
.setContainer(CelContainer.ofName("google"))
.setResultType(SimpleType.TIMESTAMP)
.build();
CelRuntime.Program program =
cel.createProgram(cel.compile("protobuf.Timestamp{seconds: 12}").getAst());
assertThat(program.eval()).isEqualTo(Instant.ofEpochSecond(12));
}
@Test
public void program_partialMessageTypes() throws Exception {
String packageName = CheckedExpr.getDescriptor().getFile().getPackage();
Cel cel =
standardCelBuilderWithMacros()
.addFileTypes(CheckedExpr.getDescriptor().getFile())
// Disabling the resolution of type dependencies can be risky as message types which
// are expected to be available in an imported file may not be present if the type
// is not referenced in a field within the provided file descriptors.
//
// In this test 'Expr' is defined in syntax.proto, but the descriptor provided is
// defined in checked.proto. Because the `Expr` type is referenced within a message
// field of the CheckedExpr, it is available for use.
.setOptions(CelOptions.current().resolveTypeDependencies(false).build())
.setContainer(CelContainer.ofName(packageName))
.setResultType(StructTypeReference.create(packageName + ".Expr"))
.build();
CelRuntime.Program program = cel.createProgram(cel.compile("Expr{}").getAst());
assertThat(program.eval()).isEqualTo(Expr.getDefaultInstance());
}
@Test
public void program_partialMessageTypeFailure() {
String packageName = CheckedExpr.getDescriptor().getFile().getPackage();
Cel cel =
standardCelBuilderWithMacros()
.addFileTypes(CheckedExpr.getDescriptor().getFile())
// In this test 'ParsedExpr' is defined in syntax.proto, but the descriptor provided is
// defined in checked.proto. Because the `ParsedExpr` type is not referenced, it is not
// available for use within CEL when deep type resolution is disabled.
.setOptions(CelOptions.current().resolveTypeDependencies(false).build())
.setContainer(CelContainer.ofName(packageName))
.setResultType(StructTypeReference.create(packageName + ".ParsedExpr"))
.build();
CelValidationException e =
Assert.assertThrows(
CelValidationException.class, () -> cel.compile("ParsedExpr{}").getAst());
assertThat(e).hasMessageThat().contains("undeclared reference to 'ParsedExpr'");
}
@Test
public void program_deepTypeResolution() throws Exception {
String packageName = CheckedExpr.getDescriptor().getFile().getPackage();
Cel cel =
standardCelBuilderWithMacros()
.addFileTypes(CheckedExpr.getDescriptor().getFile())
// In this test 'ParsedExpr' is defined in syntax.proto, but the descriptor provided is
// defined in checked.proto. Because deep type dependency resolution is enabled, the
// `ParsedExpr` may be used within CEL.
.setOptions(CelOptions.current().resolveTypeDependencies(true).build())
.setContainer(CelContainer.ofName(packageName))
.setResultType(StructTypeReference.create(packageName + ".ParsedExpr"))
.build();
CelRuntime.Program program = cel.createProgram(cel.compile("ParsedExpr{}").getAst());
assertThat(program.eval()).isEqualTo(ParsedExpr.getDefaultInstance());
}
@Test
public void program_deepTypeResolutionEnabledForRuntime_success() throws Exception {
String packageName = CheckedExpr.getDescriptor().getFile().getPackage();
CelCompiler celCompiler =
CelCompilerFactory.standardCelCompilerBuilder()
.addFileTypes(ParsedExpr.getDescriptor().getFile())
.setResultType(StructTypeReference.create(packageName + ".ParsedExpr"))
.setContainer(CelContainer.ofName(packageName))
.build();
CelAbstractSyntaxTree ast = celCompiler.compile("ParsedExpr{}").getAst();
CelRuntime celRuntime =
CelRuntimeFactory.standardCelRuntimeBuilder()
.addFileTypes(CheckedExpr.getDescriptor().getFile())
.setOptions(CelOptions.current().resolveTypeDependencies(true).build())
.build();
CelRuntime.Program program = celRuntime.createProgram(ast);
// 'ParsedExpr' is defined in syntax.proto but the descriptor provided to the runtime is from
// 'checked.proto'.
// 'ParsedExpr' is transitively available for use because deep type resolution is enabled.
assertThat(program.eval()).isEqualTo(ParsedExpr.getDefaultInstance());
}
@Test
public void program_deepTypeResolutionDisabledForRuntime_fails() throws Exception {
String packageName = CheckedExpr.getDescriptor().getFile().getPackage();
CelCompiler celCompiler =
CelCompilerFactory.standardCelCompilerBuilder()
.addFileTypes(CheckedExpr.getDescriptor().getFile())
.setOptions(CelOptions.current().resolveTypeDependencies(true).build())
.setResultType(StructTypeReference.create(packageName + ".ParsedExpr"))
.setContainer(CelContainer.ofName(packageName))
.build();
// 'ParsedExpr' is defined in syntax.proto but the descriptor provided is from 'checked.proto'.
// 'ParsedExpr' is transitively available for use because deep type resolution is enabled.
CelAbstractSyntaxTree ast = celCompiler.compile("ParsedExpr{}").getAst();
CelRuntime celRuntime =
CelRuntimeFactory.standardCelRuntimeBuilder()
.addFileTypes(CheckedExpr.getDescriptor().getFile())
.setOptions(CelOptions.current().resolveTypeDependencies(false).build())
.build();
CelRuntime.Program program = celRuntime.createProgram(ast);
// In this case, linked types are disabled so the same descriptors
// provided to the CelCompiler must also be provided into the runtime.
// As deep type resolution is disabled, 'ParsedExpr' is not available for use in runtime so an
// error is thrown.
CelEvaluationException e = Assert.assertThrows(CelEvaluationException.class, program::eval);
assertThat(e)
.hasMessageThat()
.contains(String.format("cannot resolve '%s.ParsedExpr' as a message", packageName));
}
@Test
@SuppressWarnings("deprecation") // Test for existing deprecated method setTypeProvider
public void program_typeProvider() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.setTypeProvider(
new DescriptorTypeProvider(ImmutableList.of(Timestamp.getDescriptor())))
.setContainer(CelContainer.ofName("google"))
.setResultType(SimpleType.TIMESTAMP)
.build();
CelRuntime.Program program =
cel.createProgram(cel.compile("protobuf.Timestamp{seconds: 12}").getAst());
assertThat(program.eval()).isEqualTo(Instant.ofEpochSecond(12));
}
@Test
public void program_protoActivation() throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.addMessageTypes(AttributeContext.getDescriptor())
.addDeclarations(
Decl.newBuilder()
.setName("resource")
.setIdent(
IdentDecl.newBuilder()
.setType(
CelProtoTypes.createMessage(
"google.rpc.context.AttributeContext.Resource")))
.build())
.setResultType(SimpleType.STRING)
.build();
CelRuntime.Program program = cel.createProgram(cel.compile("resource.name").getAst());
assertThat(
program.eval(
AttributeContext.newBuilder()
.setResource(AttributeContext.Resource.newBuilder().setName("test/name"))
.build()))
.isEqualTo("test/name");
}
@Test
@TestParameters("{resolveTypeDependencies: false}")
@TestParameters("{resolveTypeDependencies: true}")
public void program_enumTypeDirectResolution(boolean resolveTypeDependencies) throws Exception {
Cel cel =
standardCelBuilderWithMacros()
.addFileTypes(StandaloneGlobalEnum.getDescriptor().getFile())
.setOptions(
CelOptions.current().resolveTypeDependencies(resolveTypeDependencies).build())
.setContainer(
CelContainer.ofName("dev.cel.testing.testdata.proto3.StandaloneGlobalEnum"))
.setResultType(SimpleType.BOOL)
.build();
// Providing an enum proto file directly should not cause an error
// regardless of the resolveTypeDependencies settings
StandaloneGlobalEnum testEnum = StandaloneGlobalEnum.SGAR;
CelRuntime.Program program =
cel.createProgram(
cel.compile(String.format("%s == %d", testEnum, testEnum.getNumber())).getAst());
assertThat(program.eval()).isEqualTo(true);