Skip to content

Commit 401da42

Browse files
committed
Add CEL macro verifications
1 parent c81a4ab commit 401da42

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

xds/src/test/java/io/grpc/xds/internal/matchers/CelMatcherTest.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,25 @@
1717
package io.grpc.xds.internal.matchers;
1818

1919
import static com.google.common.truth.Truth.assertThat;
20+
import static org.junit.Assert.assertThrows;
2021

2122
import dev.cel.common.CelAbstractSyntaxTree;
23+
import dev.cel.common.CelErrorCode;
24+
import dev.cel.common.CelValidationException;
2225
import dev.cel.common.CelValidationResult;
2326
import dev.cel.common.types.SimpleType;
2427
import dev.cel.compiler.CelCompiler;
2528
import dev.cel.compiler.CelCompilerFactory;
29+
import dev.cel.parser.CelStandardMacro;
2630
import dev.cel.runtime.CelEvaluationException;
2731
import io.grpc.Metadata;
2832
import io.grpc.MethodDescriptor;
2933
import io.grpc.MethodDescriptor.MethodType;
3034
import io.grpc.NoopServerCall;
3135
import io.grpc.ServerCall;
36+
import io.grpc.Status;
37+
import io.grpc.Status.Code;
38+
import io.grpc.StatusRuntimeException;
3239
import io.grpc.StringMarshaller;
3340
import org.junit.Before;
3441
import org.junit.Test;
@@ -43,9 +50,10 @@ public class CelMatcherTest {
4350
CelCompilerFactory.standardCelCompilerBuilder()
4451
.addVar("request", SimpleType.ANY)
4552
.setResultType(SimpleType.BOOL)
53+
.setStandardMacros(CelStandardMacro.STANDARD_MACROS)
4654
.build();
4755
private static final CelValidationResult celProg1 =
48-
CEL_COMPILER.compile("request.method == \"POST\"");
56+
CEL_COMPILER.compile("request.method == 'POST'");
4957

5058
CelAbstractSyntaxTree ast1;
5159
CelMatcher matcher1;
@@ -92,4 +100,34 @@ public void construct() throws CelEvaluationException {
92100
public void testProgTrue() {
93101
assertThat(matcher1.test(fakeInput)).isTrue();
94102
}
103+
104+
@Test
105+
public void macros_comprehensionsDisabled() throws Exception {
106+
CelMatcher matcherWithComprehensions = newMatcher(
107+
"size(['foo', 'bar'].map(x, [request.headers[x], request.headers[x]])) == 1");
108+
109+
Status status = assertThrows(StatusRuntimeException.class,
110+
() -> matcherWithComprehensions.test(fakeInput)).getStatus();
111+
112+
assertThat(status.getCode()).isEqualTo(Code.UNKNOWN);
113+
assertThat(status.getCause()).isInstanceOf(CelEvaluationException.class);
114+
115+
// Verify CelErrorCode is ITERATION_BUDGET_EXCEEDED.
116+
CelEvaluationException cause = (CelEvaluationException) status.getCause();
117+
assertThat(cause.getErrorCode()).isEqualTo(CelErrorCode.ITERATION_BUDGET_EXCEEDED);
118+
}
119+
120+
@Test
121+
public void macros_hasEnabled() throws Exception {
122+
boolean result = newMatcher("has(request.path)").test(fakeInput);
123+
assertThat(result).isTrue();
124+
}
125+
126+
private CelMatcher newMatcher(String expr) throws CelValidationException, CelEvaluationException {
127+
return CelMatcher.create(celAst(expr));
128+
}
129+
130+
private CelAbstractSyntaxTree celAst(String expr) throws CelValidationException {
131+
return CEL_COMPILER.compile(expr).getAst();
132+
}
95133
}

0 commit comments

Comments
 (0)