diff --git a/common/src/main/java/dev/cel/common/CelOptions.java b/common/src/main/java/dev/cel/common/CelOptions.java index 31e05de2f..d86ed64f4 100644 --- a/common/src/main/java/dev/cel/common/CelOptions.java +++ b/common/src/main/java/dev/cel/common/CelOptions.java @@ -412,7 +412,8 @@ public Builder enableHeterogeneousNumericComparisons(boolean value) { public abstract Builder enableUnsignedLongs(boolean value); /** - * Perform equality using {@code ProtoEquality} proto equality. + * Perform equality using {@code ProtoEquality} proto equality. {@code CelLiteRuntime} always + * applies this equality regardless of this option. * *

CEL's alternative implementation to the {@code Message#equals} comes with a few key * differences: diff --git a/common/src/main/java/dev/cel/common/internal/DefaultLiteDescriptorPool.java b/common/src/main/java/dev/cel/common/internal/DefaultLiteDescriptorPool.java index d28da7234..d37af5729 100644 --- a/common/src/main/java/dev/cel/common/internal/DefaultLiteDescriptorPool.java +++ b/common/src/main/java/dev/cel/common/internal/DefaultLiteDescriptorPool.java @@ -49,6 +49,8 @@ @Immutable @Internal public final class DefaultLiteDescriptorPool implements CelLiteDescriptorPool { + private static final String STRUCT_FIELDS_ENTRY = "google.protobuf.Struct.FieldsEntry"; + private final ImmutableMap protoFqnToMessageInfo; private final ImmutableMap, MessageLiteDescriptor> classToMessageInfo; @@ -163,7 +165,7 @@ private static MessageLiteDescriptor newMessageInfo(WellKnownProto wellKnownProt /* encodingType= */ EncodingType.MAP, /* protoFieldType= */ FieldLiteDescriptor.Type.MESSAGE, /* isPacked= */ false, - /* fieldProtoTypeName= */ "google.protobuf.Struct.FieldsEntry")); + /* fieldProtoTypeName= */ STRUCT_FIELDS_ENTRY)); break; case JSON_VALUE: messageBuilder = Value::newBuilder; @@ -268,6 +270,14 @@ private DefaultLiteDescriptorPool(ImmutableSet descriptors) { protoFqnMapBuilder.put(wellKnownProto.typeName(), wktMessageInfo); classMapBuilder.put(wellKnownProto.messageClass(), wktMessageInfo); } + // Struct's map entry, which `google.protobuf.Struct{fields: ...}` literals are encoded with. + protoFqnMapBuilder.put( + STRUCT_FIELDS_ENTRY, + CelLiteDescriptor.mapEntry( + /* fullName= */ STRUCT_FIELDS_ENTRY, + /* keyType= */ FieldLiteDescriptor.Type.STRING, + /* valueType= */ FieldLiteDescriptor.Type.MESSAGE, + /* valueTypeName= */ WellKnownProto.JSON_VALUE.typeName())); for (CelLiteDescriptor descriptor : descriptors) { protoFqnMapBuilder.putAll(descriptor.getProtoTypeNamesToDescriptors()); diff --git a/common/src/main/java/dev/cel/common/internal/ProtoAdapter.java b/common/src/main/java/dev/cel/common/internal/ProtoAdapter.java index b872df831..73eb3d276 100644 --- a/common/src/main/java/dev/cel/common/internal/ProtoAdapter.java +++ b/common/src/main/java/dev/cel/common/internal/ProtoAdapter.java @@ -308,23 +308,21 @@ private BidiConverter fieldToValueConverter(FieldDescriptor fieldDescriptor) { case SFIXED32: case SINT32: case INT32: - return unwrapAndConvert(INT_CONVERTER); + return unwrapAndConvert(fieldDescriptor, Long.class, INT_CONVERTER); case FIXED32: case UINT32: if (celOptions.enableUnsignedLongs()) { - return unwrapAndConvert(UNSIGNED_UINT32_CONVERTER); + return unwrapAndConvert(fieldDescriptor, UnsignedLong.class, UNSIGNED_UINT32_CONVERTER); } - return unwrapAndConvert(SIGNED_UINT32_CONVERTER); + return unwrapAndConvert(fieldDescriptor, Long.class, SIGNED_UINT32_CONVERTER); case FIXED64: case UINT64: if (celOptions.enableUnsignedLongs()) { - return unwrapAndConvert(UNSIGNED_UINT64_CONVERTER); + return unwrapAndConvert(fieldDescriptor, UnsignedLong.class, UNSIGNED_UINT64_CONVERTER); } - return BidiConverter.of( - BidiConverter.IDENTITY.forwardConverter(), - value -> BidiConverter.IDENTITY.backwardConverter().convert(maybeUnwrap(value))); + return unwrapAndCheck(fieldDescriptor, Long.class); case FLOAT: - return unwrapAndConvert(DOUBLE_CONVERTER); + return unwrapAndConvert(fieldDescriptor, Double.class, DOUBLE_CONVERTER); case BYTES: if (celOptions.evaluateCanonicalTypesToNativeValues()) { return BidiConverter.of( @@ -336,21 +334,24 @@ private BidiConverter fieldToValueConverter(FieldDescriptor fieldDescriptor) { BidiConverter.IDENTITY.forwardConverter(), value -> BidiConverter.IDENTITY.backwardConverter().convert(maybeUnwrap(value))); case DOUBLE: + return unwrapAndCheck(fieldDescriptor, Double.class); case SFIXED64: case SINT64: case INT64: + return unwrapAndCheck(fieldDescriptor, Long.class); case STRING: case BOOL: return BidiConverter.of( BidiConverter.IDENTITY.forwardConverter(), value -> BidiConverter.IDENTITY.backwardConverter().convert(maybeUnwrap(value))); case ENUM: - return BidiConverter.of( + return BidiConverter.of( value -> (long) ((EnumValueDescriptor) value).getNumber(), - number -> + value -> fieldDescriptor .getEnumType() - .findValueByNumberCreatingIfUnknown(intCheckedCast(number))); + .findValueByNumberCreatingIfUnknown( + intCheckedCast((Long) unwrapNumber(fieldDescriptor, Long.class, value)))); case MESSAGE: return BidiConverter.of( this::adaptProtoToValue, @@ -434,9 +435,31 @@ private Object maybeUnwrap(Object value) { } private BidiConverter unwrapAndConvert( - final BidiConverter original) { + FieldDescriptor fieldDescriptor, Class celType, BidiConverter original) { return BidiConverter.of( original.forwardConverter()::convert, - value -> original.backwardConverter().convert((Number) maybeUnwrap(value))); + value -> + original + .backwardConverter() + .convert((Number) unwrapNumber(fieldDescriptor, celType, value))); + } + + private BidiConverter unwrapAndCheck( + FieldDescriptor fieldDescriptor, Class celType) { + return BidiConverter.of( + BidiConverter.IDENTITY.forwardConverter(), + value -> unwrapNumber(fieldDescriptor, celType, value)); + } + + private Object unwrapNumber(FieldDescriptor fieldDescriptor, Class celType, Object value) { + Object celValue = maybeUnwrap(value); + if ((celValue instanceof Long || celValue instanceof UnsignedLong || celValue instanceof Double) + && !celType.isInstance(celValue)) { + throw new IllegalArgumentException( + String.format( + "unexpected value type for field '%s' in message '%s'", + fieldDescriptor.getName(), fieldDescriptor.getContainingType().getFullName())); + } + return celValue; } } diff --git a/common/src/main/java/dev/cel/common/internal/ProtoLiteAdapter.java b/common/src/main/java/dev/cel/common/internal/ProtoLiteAdapter.java index 4a4e2ea26..57800ae68 100644 --- a/common/src/main/java/dev/cel/common/internal/ProtoLiteAdapter.java +++ b/common/src/main/java/dev/cel/common/internal/ProtoLiteAdapter.java @@ -299,7 +299,8 @@ private MessageLite adaptValueToUint64(Object value) { throw new IllegalArgumentException("Unexpected value type: " + value); } - static int intCheckedCast(long value) { + /** Narrows {@code value} to an int32, or throws {@link CelNumericOverflowException}. */ + public static int intCheckedCast(long value) { try { return Ints.checkedCast(value); } catch (IllegalArgumentException e) { @@ -307,7 +308,8 @@ static int intCheckedCast(long value) { } } - static int unsignedIntCheckedCast(long value) { + /** Narrows {@code value} to a uint32, or throws {@link CelNumericOverflowException}. */ + public static int unsignedIntCheckedCast(long value) { try { return UnsignedInts.checkedCast(value); } catch (IllegalArgumentException e) { @@ -316,14 +318,12 @@ static int unsignedIntCheckedCast(long value) { } private static Any packAnyMessage(MessageLite msg, WellKnownProto wellKnownProto) { - return packAnyMessage(msg, wellKnownProto.typeName()); + return packAny(msg.toByteString(), wellKnownProto.typeName()); } - private static Any packAnyMessage(MessageLite msg, String typeUrl) { - return Any.newBuilder() - .setValue(msg.toByteString()) - .setTypeUrl("type.googleapis.com/" + typeUrl) - .build(); + /** Packs {@code value}, a serialized message of type {@code typeName}, into an {@link Any}. */ + public static Any packAny(ByteString value, String typeName) { + return Any.newBuilder().setValue(value).setTypeUrl("type.googleapis.com/" + typeName).build(); } private Timestamp adaptValueToProtoTimestamp(Object value) { diff --git a/common/src/main/java/dev/cel/common/values/BUILD.bazel b/common/src/main/java/dev/cel/common/values/BUILD.bazel index 53d3788bc..bfa894a46 100644 --- a/common/src/main/java/dev/cel/common/values/BUILD.bazel +++ b/common/src/main/java/dev/cel/common/values/BUILD.bazel @@ -322,6 +322,7 @@ java_library( name = "proto_message_lite_value", srcs = [ "ProtoLiteCelValueConverter.java", + "ProtoLiteEquality.java", "ProtoMessageLiteValue.java", "RawProtoMessageLiteValue.java", "WireMessageLite.java", @@ -356,6 +357,7 @@ cel_android_library( name = "proto_message_lite_value_android", srcs = [ "ProtoLiteCelValueConverter.java", + "ProtoLiteEquality.java", "ProtoMessageLiteValue.java", "RawProtoMessageLiteValue.java", "WireMessageLite.java", @@ -393,10 +395,16 @@ java_library( deps = [ ":base_proto_message_value_provider", ":proto_message_lite_value", + "//common:options", "//common/annotations", "//common/internal:cel_lite_descriptor_pool", "//common/internal:default_lite_descriptor_pool", + "//common/internal:proto_lite_adapter", + "//common/internal:proto_time_utils", + "//common/internal:well_known_proto", + "//common/values", "//common/values:base_proto_cel_value_converter", + "//common/values:cel_byte_string", "//protobuf:cel_lite_descriptor", "@maven//:com_google_errorprone_error_prone_annotations", "@maven//:com_google_guava_guava", @@ -412,10 +420,16 @@ cel_android_library( deps = [ ":base_proto_message_value_provider_android", ":proto_message_lite_value_android", + "//common:options", "//common/annotations", "//common/internal:cel_lite_descriptor_pool_android", "//common/internal:default_lite_descriptor_pool_android", + "//common/internal:proto_lite_adapter_android", + "//common/internal:proto_time_utils_android", + "//common/internal:well_known_proto_android", "//common/values:base_proto_cel_value_converter_android", + "//common/values:cel_byte_string", + "//common/values:values_android", "//protobuf:cel_lite_descriptor", "@maven//:com_google_errorprone_error_prone_annotations", "@maven_android//:com_google_guava_guava", diff --git a/common/src/main/java/dev/cel/common/values/CelValueConverter.java b/common/src/main/java/dev/cel/common/values/CelValueConverter.java index 3e00be2e6..a7ad069d2 100644 --- a/common/src/main/java/dev/cel/common/values/CelValueConverter.java +++ b/common/src/main/java/dev/cel/common/values/CelValueConverter.java @@ -221,6 +221,14 @@ public static boolean containsMapKey(Map map, Object key) { return false; } + /** + * Returns whether {@code x} and {@code y} are equal in CEL, if they're protobuf messages this + * converter knows how to compare. + */ + public Optional messageEquals(Object x, Object y) { + return Optional.empty(); + } + protected Object normalizePrimitive(Object value) { Preconditions.checkNotNull(value); diff --git a/common/src/main/java/dev/cel/common/values/CombinedCelValueConverter.java b/common/src/main/java/dev/cel/common/values/CombinedCelValueConverter.java index 46e5fc3f1..b206c65d8 100644 --- a/common/src/main/java/dev/cel/common/values/CombinedCelValueConverter.java +++ b/common/src/main/java/dev/cel/common/values/CombinedCelValueConverter.java @@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableList; import dev.cel.common.annotations.Internal; +import java.util.Optional; import org.jspecify.annotations.Nullable; /** @@ -81,4 +82,15 @@ private CombinedCelValueConverter(ImmutableList converters) { return value; } + + @Override + public Optional messageEquals(Object x, Object y) { + for (CelValueConverter converter : converters) { + Optional result = converter.messageEquals(x, y); + if (result.isPresent()) { + return result; + } + } + return Optional.empty(); + } } diff --git a/common/src/main/java/dev/cel/common/values/ProtoLiteCelValueConverter.java b/common/src/main/java/dev/cel/common/values/ProtoLiteCelValueConverter.java index 4d97cd6d4..64021c93e 100644 --- a/common/src/main/java/dev/cel/common/values/ProtoLiteCelValueConverter.java +++ b/common/src/main/java/dev/cel/common/values/ProtoLiteCelValueConverter.java @@ -62,13 +62,14 @@ public final class ProtoLiteCelValueConverter extends BaseProtoCelValueConverter { private static final String MAP_KEY_FIELD_NAME = "key"; private static final String MAP_VALUE_FIELD_NAME = "value"; - private static final int MAP_KEY_FIELD_NUMBER = 1; - private static final int MAP_VALUE_FIELD_NUMBER = 2; + static final int MAP_KEY_FIELD_NUMBER = 1; + static final int MAP_VALUE_FIELD_NUMBER = 2; // Protobuf's default recursion limit. Nested Anys are unwrapped in a loop since each payload is // parsed afresh, outside that limit. - private static final int MAX_ANY_NESTING_DEPTH = 100; + static final int MAX_ANY_NESTING_DEPTH = 100; private final CelLiteDescriptorPool descriptorPool; + private final ProtoLiteEquality protoLiteEquality; public static ProtoLiteCelValueConverter newInstance( CelLiteDescriptorPool celLiteDescriptorPool) { @@ -168,7 +169,7 @@ private Object mergeOrReadMessageField( // Unlike other WellKnownProtos (which unbox to CEL scalars/containers), FieldMask is // represented as a standard struct message so field selection (e.g., mask.paths) works. - private static boolean isStructLike(@Nullable WellKnownProto wellKnownProto) { + static boolean isStructLike(@Nullable WellKnownProto wellKnownProto) { return wellKnownProto == null || wellKnownProto == WellKnownProto.FIELD_MASK; } @@ -190,7 +191,7 @@ static MessageLite parseMessageLite(ByteString bytes, MessageLiteDescriptor desc return mergeMessageLite(descriptor.newMessageBuilder(), bytes, descriptor.getProtoTypeName()); } - private static MessageLite mergeMessageLite( + static MessageLite mergeMessageLite( MessageLite.Builder builder, ByteString bytes, String protoTypeName) { try { return builder.mergeFrom(bytes, ExtensionRegistryLite.getEmptyRegistry()).build(); @@ -215,6 +216,13 @@ public Object toRuntimeValue(Object value) { return super.toRuntimeValue(value); } + @Override + public Optional messageEquals(Object x, Object y) { + return x instanceof MessageLite && y instanceof MessageLite + ? Optional.of(protoLiteEquality.equals((MessageLite) x, (MessageLite) y)) + : Optional.empty(); + } + private Object toRuntimeValue(MessageLite msg, MessageLiteDescriptor descriptor) { WellKnownProto wellKnownProto = WellKnownProto.getByTypeName(descriptor.getProtoTypeName()).orElse(null); @@ -805,5 +813,6 @@ private static IllegalArgumentException newDecodeException(String protoTypeName, private ProtoLiteCelValueConverter(CelLiteDescriptorPool celLiteDescriptorPool) { this.descriptorPool = checkNotNull(celLiteDescriptorPool); + this.protoLiteEquality = new ProtoLiteEquality(celLiteDescriptorPool); } } diff --git a/common/src/main/java/dev/cel/common/values/ProtoLiteEquality.java b/common/src/main/java/dev/cel/common/values/ProtoLiteEquality.java new file mode 100644 index 000000000..b2d054c74 --- /dev/null +++ b/common/src/main/java/dev/cel/common/values/ProtoLiteEquality.java @@ -0,0 +1,224 @@ +// Copyright 2026 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.common.values; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; +import static dev.cel.common.values.ProtoLiteCelValueConverter.MAX_ANY_NESTING_DEPTH; + +import com.google.common.collect.ImmutableListMultimap; +import com.google.errorprone.annotations.Immutable; +import com.google.protobuf.Any; +import com.google.protobuf.ByteString; +import com.google.protobuf.CodedInputStream; +import com.google.protobuf.DoubleValue; +import com.google.protobuf.ExtensionRegistryLite; +import com.google.protobuf.FloatValue; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.ListValue; +import com.google.protobuf.MessageLite; +import com.google.protobuf.Struct; +import com.google.protobuf.Value; +import com.google.protobuf.WireFormat; +import dev.cel.common.internal.CelLiteDescriptorPool; +import dev.cel.protobuf.CelLiteDescriptor.FieldLiteDescriptor; +import dev.cel.protobuf.CelLiteDescriptor.FieldLiteDescriptor.EncodingType; +import dev.cel.protobuf.CelLiteDescriptor.MessageLiteDescriptor; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import org.jspecify.annotations.Nullable; + +/** CEL protobuf message equality for {@link MessageLite} instances. */ +@Immutable +final class ProtoLiteEquality { + + private final CelLiteDescriptorPool descriptorPool; + + boolean equals(MessageLite x, MessageLite y) { + return equals(x, y, /* depth= */ 0); + } + + private boolean equals(MessageLite x, MessageLite y, int depth) { + if (x.getClass() != y.getClass()) { + return false; + } + // Each Any payload is parsed afresh, outside protobuf's own recursion limit. + checkArgument( + depth <= MAX_ANY_NESTING_DEPTH, "Message nesting exceeds %s", MAX_ANY_NESTING_DEPTH); + MessageLiteDescriptor descriptor = descriptorPool.findDescriptor(x).orElse(null); + if (descriptor != null && descriptor.hasGetters()) { + for (FieldLiteDescriptor field : descriptor.getFieldDescriptors()) { + int number = field.getFieldNumber(); + if (isSemantic(field) + && !valueEquals( + descriptor.getFieldValue(x, number), descriptor.getFieldValue(y, number), depth)) { + return false; + } + } + } else if (x instanceof Any) { + MessageLite unpackedX = unpack((Any) x); + MessageLite unpackedY = unpack((Any) y); + if (unpackedX != null && unpackedY != null) { + return valueEquals(unpackedX, unpackedY, depth); + } + } else if (x instanceof Value) { + Value vx = (Value) x; + Value vy = (Value) y; + if (vx.getKindCase() != vy.getKindCase() + || !valueEquals(semanticValue(vx), semanticValue(vy), depth)) { + return false; + } + } else if (x instanceof Struct) { + if (!valueEquals(((Struct) x).getFieldsMap(), ((Struct) y).getFieldsMap(), depth)) { + return false; + } + } else if (x instanceof ListValue) { + if (!valueEquals(((ListValue) x).getValuesList(), ((ListValue) y).getValuesList(), depth)) { + return false; + } + } else { + // A wrapper's -0.0 is set and its 0.0 isn't, so of its doubles only NaN differs from bytes. + if (isNan(x)) { + return false; + } + // None of this message's fields was compared above, so all of them are compared by bytes. + descriptor = null; + } + // NOMUTANTS -- Messages equal in protobuf's own sense have equal bytes; this saves serializing. + return x.equals(y) + || rawFields(x.toByteString(), descriptor).equals(rawFields(y.toByteString(), descriptor)); + } + + private boolean valueEquals(@Nullable Object x, @Nullable Object y, int depth) { + if (x == null || y == null) { + return x == null && y == null; + } + if (x instanceof MessageLite) { + return equals((MessageLite) x, (MessageLite) y, depth + 1); + } + if (x instanceof Float || x instanceof Double) { + return ((Number) x).doubleValue() == ((Number) y).doubleValue(); + } + if (x instanceof List) { + List xList = (List) x; + List yList = (List) y; + if (xList.size() != yList.size()) { + return false; + } + for (int i = 0; i < xList.size(); i++) { + if (!valueEquals(xList.get(i), yList.get(i), depth)) { + return false; + } + } + return true; + } + if (x instanceof Map) { + Map xMap = (Map) x; + Map yMap = (Map) y; + if (xMap.size() != yMap.size()) { + return false; + } + for (Map.Entry entry : xMap.entrySet()) { + if (!valueEquals(entry.getValue(), yMap.get(entry.getKey()), depth)) { + return false; + } + } + return true; + } + return x.equals(y); + } + + private static @Nullable Object semanticValue(Value v) { + switch (v.getKindCase()) { + case NUMBER_VALUE: + return v.getNumberValue(); + case STRUCT_VALUE: + return v.getStructValue(); + case LIST_VALUE: + return v.getListValue(); + default: + return null; + } + } + + private @Nullable MessageLite unpack(Any any) { + String typeUrl = any.getTypeUrl(); + int slash = typeUrl.lastIndexOf('/'); + MessageLiteDescriptor descriptor = + slash < 0 ? null : descriptorPool.findDescriptor(typeUrl.substring(slash + 1)).orElse(null); + if (descriptor == null || descriptor.isMapEntry()) { + return null; + } + try { + // Partial, as in C++: a missing required field is no reason not to compare the rest. + return descriptor + .newMessageBuilder() + .mergeFrom(any.getValue(), ExtensionRegistryLite.getEmptyRegistry()) + .buildPartial(); + } catch (InvalidProtocolBufferException e) { + return null; + } + } + + private static boolean isNan(MessageLite message) { + return (message instanceof DoubleValue && Double.isNaN(((DoubleValue) message).getValue())) + || (message instanceof FloatValue && Float.isNaN(((FloatValue) message).getValue())); + } + + private static boolean isSemantic(FieldLiteDescriptor field) { + FieldLiteDescriptor.Type type = field.getProtoFieldType(); + // Not getJavaType(), which is MESSAGE for groups too, and their getters throw. + return type == FieldLiteDescriptor.Type.FLOAT + || type == FieldLiteDescriptor.Type.DOUBLE + || type == FieldLiteDescriptor.Type.MESSAGE; + } + + private static boolean isSemantic(MessageLiteDescriptor descriptor, int tag) { + FieldLiteDescriptor field = descriptor.findByFieldNumber(WireFormat.getTagFieldNumber(tag)); + if (field == null || !isSemantic(field)) { + return false; + } + int wireType = WireFormat.getTagWireType(tag); + // The parser keeps other wire types as unknown fields, and reads repeated floats and doubles + // whether packed or not. + return wireType == field.getProtoFieldType().toWireFormatFieldType().getWireType() + || (wireType == WireFormat.WIRETYPE_LENGTH_DELIMITED + && field.getEncodingType() == EncodingType.LIST); + } + + /** Groups a serialized message's fields by tag, less {@code descriptor}'s semantic ones. */ + static ImmutableListMultimap rawFields( + ByteString bytes, @Nullable MessageLiteDescriptor descriptor) { + ImmutableListMultimap.Builder fields = ImmutableListMultimap.builder(); + CodedInputStream input = bytes.newCodedInput(); + try { + for (int tag = input.readTag(); tag != 0; tag = input.readTag()) { + int start = input.getTotalBytesRead(); + input.skipField(tag); + if (descriptor == null || !isSemantic(descriptor, tag)) { + fields.put(tag, bytes.substring(start, input.getTotalBytesRead())); + } + } + } catch (IOException e) { + throw new IllegalArgumentException("Failed to decode proto message", e); + } + return fields.build(); + } + + ProtoLiteEquality(CelLiteDescriptorPool descriptorPool) { + this.descriptorPool = checkNotNull(descriptorPool); + } +} diff --git a/common/src/main/java/dev/cel/common/values/ProtoMessageLiteValueProvider.java b/common/src/main/java/dev/cel/common/values/ProtoMessageLiteValueProvider.java index 041d850a6..a7584895c 100644 --- a/common/src/main/java/dev/cel/common/values/ProtoMessageLiteValueProvider.java +++ b/common/src/main/java/dev/cel/common/values/ProtoMessageLiteValueProvider.java @@ -14,27 +14,48 @@ package dev.cel.common.values; +import static com.google.common.base.Preconditions.checkArgument; + import com.google.common.collect.ImmutableSet; +import com.google.common.primitives.UnsignedLong; import com.google.errorprone.annotations.Immutable; +import com.google.protobuf.ByteString; +import com.google.protobuf.CodedOutputStream; import com.google.protobuf.MessageLite; +import dev.cel.common.CelOptions; import dev.cel.common.annotations.Beta; import dev.cel.common.internal.CelLiteDescriptorPool; import dev.cel.common.internal.DefaultLiteDescriptorPool; +import dev.cel.common.internal.ProtoLiteAdapter; +import dev.cel.common.internal.ProtoTimeUtils; +import dev.cel.common.internal.WellKnownProto; import dev.cel.protobuf.CelLiteDescriptor; +import dev.cel.protobuf.CelLiteDescriptor.FieldLiteDescriptor; +import dev.cel.protobuf.CelLiteDescriptor.FieldLiteDescriptor.EncodingType; import dev.cel.protobuf.CelLiteDescriptor.MessageLiteDescriptor; +import java.io.IOException; +import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; /** - * {@code ProtoMessageValueProvider} constructs new instances of protobuf lite-message given its + * {@code ProtoMessageLiteValueProvider} constructs new instances of protobuf lite-message given its * fully qualified name and its fields to populate. + * + *

A number outside a closed (proto2) enum's values is kept as an unknown field, so the field + * reads back as its default. */ @Immutable @Beta public class ProtoMessageLiteValueProvider extends BaseProtoMessageValueProvider { + // Smaller than CodedOutputStream's default of 4KB, as most messages and map entries are small. + // A typical expression rarely exceeds 100 bytes. + private static final int OUTPUT_BUFFER_SIZE = 256; + private final CelLiteDescriptorPool descriptorPool; private final ProtoLiteCelValueConverter protoLiteCelValueConverter; + private final ProtoLiteAdapter protoLiteAdapter; @Override public BaseProtoCelValueConverter protoCelValueConverter() { @@ -44,35 +65,249 @@ public BaseProtoCelValueConverter protoCelValueConverter() { @Override public Optional newValue(String structType, Map fields) { MessageLiteDescriptor descriptor = descriptorPool.findDescriptor(structType).orElse(null); - if (descriptor == null) { + if (descriptor == null || descriptor.isMapEntry()) { return Optional.empty(); } - if (!fields.isEmpty()) { - // TODO: Add support for this - throw new UnsupportedOperationException( - "Message creation with prepopulated fields is not supported yet."); + // Encoded for the generated message's parser to read, so CelLiteDescriptor needs no setters. + ByteString.Output bytes = ByteString.newOutput(); + CodedOutputStream output = CodedOutputStream.newInstance(bytes, OUTPUT_BUFFER_SIZE); + try { + for (Map.Entry field : fields.entrySet()) { + FieldLiteDescriptor fd = descriptor.findByFieldName(field.getKey()); + if (fd == null) { + throw new IllegalArgumentException( + String.format( + "field '%s' is not declared in message '%s'", field.getKey(), structType)); + } + try { + writeField(output, fd, field.getValue()); + } catch (ClassCastException e) { + throw new IllegalArgumentException( + String.format( + "unexpected value type for field '%s' in message '%s'", + field.getKey(), structType), + e); + } + } + output.flush(); + } catch (IOException e) { + // Unreachable, as the output is in memory. + throw new AssertionError(e); } - - MessageLite message = descriptor.newMessageBuilder().build(); + // Building, unlike parsing, fails on a missing proto2 required field. + MessageLite message = + ProtoLiteCelValueConverter.mergeMessageLite( + descriptor.newMessageBuilder(), bytes.toByteString(), structType); + ProtoTimeUtils.validateWellKnownProto(message); return Optional.of(protoLiteCelValueConverter.toRuntimeValue(message)); } + private void writeField(CodedOutputStream output, FieldLiteDescriptor fd, Object value) + throws IOException { + if (value instanceof NullValue) { + String typeName = fd.getFieldProtoTypeName(); + if (fd.getEncodingType() != EncodingType.SINGULAR + || fd.getProtoFieldType() != FieldLiteDescriptor.Type.MESSAGE + || typeName.equals(WellKnownProto.JSON_STRUCT_VALUE.typeName()) + || typeName.equals(WellKnownProto.JSON_LIST_VALUE.typeName())) { + throw new IllegalArgumentException( + String.format("null cannot be assigned to field '%s'", fd.getFieldName())); + } + if (!isAnyOrJsonValue(typeName)) { + return; + } + } + switch (fd.getEncodingType()) { + case SINGULAR: + writeValue(output, fd, value); + break; + case LIST: + // Unpacked, which parsers accept for packed fields too. + boolean keepsNulls = isAnyOrJsonValue(fd.getFieldProtoTypeName()); + for (Object element : (List) value) { + if (keepsNulls || !(element instanceof NullValue)) { + writeValue(output, fd, element); + } + } + break; + case MAP: + writeMap(output, fd, (Map) value); + break; + } + } + + private void writeMap(CodedOutputStream output, FieldLiteDescriptor fd, Map map) + throws IOException { + MessageLiteDescriptor entry = descriptorPool.getDescriptorOrThrow(fd.getFieldProtoTypeName()); + FieldLiteDescriptor keyFd = + entry.getByFieldNumberOrThrow(ProtoLiteCelValueConverter.MAP_KEY_FIELD_NUMBER); + FieldLiteDescriptor valueFd = + entry.getByFieldNumberOrThrow(ProtoLiteCelValueConverter.MAP_VALUE_FIELD_NUMBER); + boolean keepsNulls = isAnyOrJsonValue(valueFd.getFieldProtoTypeName()); + // One stream encodes every entry, as each new one allocates a buffer. + ByteString.Output entryBytes = ByteString.newOutput(); + CodedOutputStream entryOutput = CodedOutputStream.newInstance(entryBytes, OUTPUT_BUFFER_SIZE); + for (Map.Entry mapEntry : map.entrySet()) { + if (keepsNulls || !(mapEntry.getValue() instanceof NullValue)) { + writeValue(entryOutput, keyFd, mapEntry.getKey()); + writeValue(entryOutput, valueFd, mapEntry.getValue()); + entryOutput.flush(); + output.writeBytes(fd.getFieldNumber(), entryBytes.toByteString()); + entryBytes.reset(); + } + } + } + + private void writeValue(CodedOutputStream output, FieldLiteDescriptor fd, Object value) + throws IOException { + int number = fd.getFieldNumber(); + switch (fd.getProtoFieldType()) { + case MESSAGE: + writeMessage(output, number, fd.getFieldProtoTypeName(), value); + break; + case DOUBLE: + output.writeDouble(number, (Double) value); + break; + case FLOAT: + output.writeFloat(number, ((Double) value).floatValue()); + break; + case INT64: + output.writeInt64(number, (Long) value); + break; + case SINT64: + output.writeSInt64(number, (Long) value); + break; + case SFIXED64: + output.writeSFixed64(number, (Long) value); + break; + case INT32: + case ENUM: + output.writeInt32(number, ProtoLiteAdapter.intCheckedCast((Long) value)); + break; + case SINT32: + output.writeSInt32(number, ProtoLiteAdapter.intCheckedCast((Long) value)); + break; + case SFIXED32: + output.writeSFixed32(number, ProtoLiteAdapter.intCheckedCast((Long) value)); + break; + case UINT64: + output.writeUInt64(number, ((UnsignedLong) value).longValue()); + break; + case FIXED64: + output.writeFixed64(number, ((UnsignedLong) value).longValue()); + break; + case UINT32: + output.writeUInt32( + number, ProtoLiteAdapter.unsignedIntCheckedCast(((UnsignedLong) value).longValue())); + break; + case FIXED32: + output.writeFixed32( + number, ProtoLiteAdapter.unsignedIntCheckedCast(((UnsignedLong) value).longValue())); + break; + case BOOL: + output.writeBool(number, (Boolean) value); + break; + case STRING: + output.writeString(number, (String) value); + break; + case BYTES: + output.writeByteArray(number, ((CelByteString) value).toByteArray()); + break; + default: + throw new UnsupportedOperationException("Groups are not supported"); + } + } + + // MessageLite values are serialized straight into the output, without an intermediate ByteString. + private void writeMessage( + CodedOutputStream output, int number, String protoTypeName, Object value) throws IOException { + WellKnownProto wellKnownProto = WellKnownProto.getByTypeName(protoTypeName).orElse(null); + if (ProtoLiteCelValueConverter.isStructLike(wellKnownProto)) { + // Fails closed on a message of another type, which only dyn or an unchecked AST can assign. + String valueTypeName = protoTypeNameOf(value); + checkArgument( + valueTypeName.equals(protoTypeName), + "expected a message of type '%s', got '%s'", + protoTypeName, + valueTypeName); + if (value instanceof WireMessageLite) { + output.writeBytes(number, ((WireMessageLite) value).toByteString()); + } else { + output.writeMessage(number, (MessageLite) value); + } + return; + } + switch (wellKnownProto) { + case ANY_VALUE: + if (!(value instanceof MessageLite || value instanceof WireMessageLite)) { + output.writeMessage(number, protoLiteAdapter.adaptValueToAny(value)); + return; + } + String valueTypeName = protoTypeNameOf(value); + checkArgument( + !valueTypeName.equals(RawProtoMessageLiteValue.UNKNOWN_MESSAGE_TYPE_NAME), + "cannot pack a message of unknown type into google.protobuf.Any"); + output.writeMessage(number, ProtoLiteAdapter.packAny(toByteString(value), valueTypeName)); + return; + case EMPTY: + // Values of google.protobuf.Empty are empty maps in CEL. + checkArgument( + ((Map) value).isEmpty(), "expected an empty map for google.protobuf.Empty"); + output.writeBytes(number, ByteString.EMPTY); + return; + default: + output.writeMessage( + number, protoLiteAdapter.adaptValueToWellKnownProto(value, wellKnownProto)); + } + } + + private String protoTypeNameOf(Object message) { + if (message instanceof WireMessageLite) { + return ((WireMessageLite) message).protoTypeName(); + } + return descriptorPool + .findDescriptor((MessageLite) message) + .map(MessageLiteDescriptor::getProtoTypeName) + .orElse(RawProtoMessageLiteValue.UNKNOWN_MESSAGE_TYPE_NAME); + } + + private static ByteString toByteString(Object message) { + return message instanceof WireMessageLite + ? ((WireMessageLite) message).toByteString() + : ((MessageLite) message).toByteString(); + } + + // Only Any and Value encode JSON null; for other message types, null is dropped. + private static boolean isAnyOrJsonValue(String protoTypeName) { + return protoTypeName.equals(WellKnownProto.ANY_VALUE.typeName()) + || protoTypeName.equals(WellKnownProto.JSON_VALUE.typeName()); + } + public static ProtoMessageLiteValueProvider newInstance(CelLiteDescriptor... descriptors) { return newInstance(ImmutableSet.copyOf(descriptors)); } public static ProtoMessageLiteValueProvider newInstance(Set descriptors) { + return newInstance(CelOptions.DEFAULT, descriptors); + } + + public static ProtoMessageLiteValueProvider newInstance( + CelOptions celOptions, Set descriptors) { DefaultLiteDescriptorPool descriptorPool = DefaultLiteDescriptorPool.newInstance(ImmutableSet.copyOf(descriptors)); ProtoLiteCelValueConverter protoLiteCelValueConverter = ProtoLiteCelValueConverter.newInstance(descriptorPool); - return new ProtoMessageLiteValueProvider(protoLiteCelValueConverter, descriptorPool); + return new ProtoMessageLiteValueProvider( + protoLiteCelValueConverter, descriptorPool, celOptions); } private ProtoMessageLiteValueProvider( - ProtoLiteCelValueConverter protoLiteCelValueConverter, CelLiteDescriptorPool descriptorPool) { + ProtoLiteCelValueConverter protoLiteCelValueConverter, + CelLiteDescriptorPool descriptorPool, + CelOptions celOptions) { this.protoLiteCelValueConverter = protoLiteCelValueConverter; this.descriptorPool = descriptorPool; + this.protoLiteAdapter = new ProtoLiteAdapter(celOptions); } } diff --git a/common/src/main/java/dev/cel/common/values/RawProtoMessageLiteValue.java b/common/src/main/java/dev/cel/common/values/RawProtoMessageLiteValue.java index 81643d46c..9660567c5 100644 --- a/common/src/main/java/dev/cel/common/values/RawProtoMessageLiteValue.java +++ b/common/src/main/java/dev/cel/common/values/RawProtoMessageLiteValue.java @@ -18,6 +18,7 @@ import com.google.auto.value.AutoValue; import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableListMultimap; import com.google.errorprone.annotations.Immutable; import com.google.protobuf.ByteString; import com.google.protobuf.CodedInputStream; @@ -25,6 +26,7 @@ import dev.cel.common.types.CelType; import dev.cel.common.types.StructTypeReference; import java.util.Locale; +import java.util.Objects; import java.util.Optional; /** @@ -41,7 +43,7 @@ abstract class RawProtoMessageLiteValue extends StructValue implements OptimizedSelectable, WireMessageLite { - private static final String UNKNOWN_MESSAGE_TYPE_NAME = "cel.@unknownMessage"; + static final String UNKNOWN_MESSAGE_TYPE_NAME = "cel.@unknownMessage"; @Override public abstract ByteString toByteString(); @@ -73,13 +75,20 @@ public WireMessageLite value() { @Override public final boolean equals(Object other) { - // TODO: Support message equality - throw new UnsupportedOperationException("Message equality is not supported"); + if (!(other instanceof RawProtoMessageLiteValue)) { + return false; + } + RawProtoMessageLiteValue that = (RawProtoMessageLiteValue) other; + return protoTypeName().equals(that.protoTypeName()) && rawFields().equals(that.rawFields()); } @Override public final int hashCode() { - throw new UnsupportedOperationException("Message equality is not supported"); + return Objects.hash(protoTypeName(), rawFields()); + } + + private ImmutableListMultimap rawFields() { + return ProtoLiteEquality.rawFields(toByteString(), /* descriptor= */ null); } @Override diff --git a/common/src/test/java/dev/cel/common/internal/ProtoAdapterTest.java b/common/src/test/java/dev/cel/common/internal/ProtoAdapterTest.java index be7e4f042..279787537 100644 --- a/common/src/test/java/dev/cel/common/internal/ProtoAdapterTest.java +++ b/common/src/test/java/dev/cel/common/internal/ProtoAdapterTest.java @@ -239,6 +239,22 @@ public void adaptValueToProto_unsignedLongsDisabled_acceptsSignedUintWrappers() .isEqualTo(UInt64Value.of(1)); } + @Test + @SuppressWarnings("deprecation") // Signed uints are only enabled through a deprecated option. + public void adaptValueToFieldType_unsignedLongsDisabled_acceptsSignedUints() { + ProtoAdapter protoAdapter = + new ProtoAdapter(DYNAMIC_PROTO, CelOptions.current().enableUnsignedLongs(false).build()); + + assertThat( + protoAdapter.adaptValueToFieldType( + UInt32Value.getDescriptor().findFieldByName("value"), 1L)) + .hasValue(1); + assertThat( + protoAdapter.adaptValueToFieldType( + UInt64Value.getDescriptor().findFieldByName("value"), 1L)) + .hasValue(1L); + } + @Test public void adaptValueToProto_asymmetricJsonConversion() { ProtoAdapter protoAdapter = new ProtoAdapter(DYNAMIC_PROTO, CelOptions.DEFAULT); diff --git a/common/src/test/java/dev/cel/common/values/BUILD.bazel b/common/src/test/java/dev/cel/common/values/BUILD.bazel index 3d7f61df1..b4467ec1d 100644 --- a/common/src/test/java/dev/cel/common/values/BUILD.bazel +++ b/common/src/test/java/dev/cel/common/values/BUILD.bazel @@ -18,11 +18,13 @@ java_library( "//common:options", "//common/exceptions:attribute_not_found", "//common/exceptions:invalid_argument", + "//common/exceptions:numeric_overflow", "//common/internal:cel_descriptor_pools", "//common/internal:cel_lite_descriptor_pool", "//common/internal:default_lite_descriptor_pool", "//common/internal:default_message_factory", "//common/internal:dynamic_proto", + "//common/internal:proto_equality", "//common/internal:proto_message_factory", "//common/internal:proto_time_utils", "//common/types", @@ -49,6 +51,7 @@ java_library( "@maven//:com_google_guava_guava_testlib", "@maven//:com_google_protobuf_protobuf_java", "@maven//:com_google_testparameterinjector_test_parameter_injector", + "@maven//:com_google_truth_extensions_truth_proto_extension", "@maven//:junit_junit", "@maven_android//:com_google_protobuf_protobuf_javalite", ], diff --git a/common/src/test/java/dev/cel/common/values/CelValueConverterTest.java b/common/src/test/java/dev/cel/common/values/CelValueConverterTest.java index 75d9182b3..83ce0cc36 100644 --- a/common/src/test/java/dev/cel/common/values/CelValueConverterTest.java +++ b/common/src/test/java/dev/cel/common/values/CelValueConverterTest.java @@ -235,4 +235,9 @@ public void maybeUnwrap_listWithNullElement_throws() { assertThat(e).hasMessageThat().isEqualTo("List element cannot be null at index: 1"); } + + @Test + public void messageEquals_returnsEmpty() { + assertThat(CEL_VALUE_CONVERTER.messageEquals("a", "b")).isEmpty(); + } } diff --git a/common/src/test/java/dev/cel/common/values/CombinedCelValueConverterTest.java b/common/src/test/java/dev/cel/common/values/CombinedCelValueConverterTest.java index 8574587bc..ca4bc24c4 100644 --- a/common/src/test/java/dev/cel/common/values/CombinedCelValueConverterTest.java +++ b/common/src/test/java/dev/cel/common/values/CombinedCelValueConverterTest.java @@ -66,6 +66,31 @@ public void combinedCelValueProvider_returnsCombinedConverter() { assertThat(combinedConverter.toRuntimeValue("target2")).isEqualTo("replacement2"); } + @Test + public void messageEquals_delegatesInOrder() { + CelValueConverter first = + new CelValueConverter() { + @Override + public Optional messageEquals(Object x, Object y) { + return x.equals("handledByFirst") ? Optional.of(true) : Optional.empty(); + } + }; + CelValueConverter second = + new CelValueConverter() { + @Override + public Optional messageEquals(Object x, Object y) { + return x.equals("handledByFirst") || x.equals("handledBySecond") + ? Optional.of(false) + : Optional.empty(); + } + }; + CelValueConverter combined = CombinedCelValueConverter.combine(ImmutableList.of(first, second)); + + assertThat(combined.messageEquals("handledByFirst", "other")).hasValue(true); + assertThat(combined.messageEquals("handledBySecond", "other")).hasValue(false); + assertThat(combined.messageEquals("unhandled", "unhandled")).isEmpty(); + } + private static class CustomConverter extends CelValueConverter { private final String target; private final String replacement; diff --git a/common/src/test/java/dev/cel/common/values/ProtoLiteCelValueConverterTest.java b/common/src/test/java/dev/cel/common/values/ProtoLiteCelValueConverterTest.java index 7cb9bef36..6aaee01bd 100644 --- a/common/src/test/java/dev/cel/common/values/ProtoLiteCelValueConverterTest.java +++ b/common/src/test/java/dev/cel/common/values/ProtoLiteCelValueConverterTest.java @@ -868,6 +868,18 @@ public void selectByFieldNumber_unknownClosedEnumValueInMemory_readsAsUnset() th assertThat(wireValue).isEqualTo(99L); } + @Test + public void messageEquals_messages_comparesWithProtoLiteEquality() { + TestAllTypes nanMsg = TestAllTypes.newBuilder().setSingleDouble(Double.NaN).build(); + TestAllTypes intMsg = TestAllTypes.newBuilder().setSingleInt32(1).build(); + TestAllTypes equalIntMsg = TestAllTypes.newBuilder().setSingleInt32(1).build(); + + assertThat(ALL_TYPES_CONVERTER.messageEquals(nanMsg, nanMsg)).hasValue(false); + assertThat(ALL_TYPES_CONVERTER.messageEquals(intMsg, equalIntMsg)).hasValue(true); + assertThat(ALL_TYPES_CONVERTER.messageEquals(intMsg, TestAllTypes.newBuilder())).isEmpty(); + assertThat(ALL_TYPES_CONVERTER.messageEquals(TestAllTypes.newBuilder(), intMsg)).isEmpty(); + } + // The value read, or the class of the exception thrown reading it. private static Object outcome(Supplier read) { try { diff --git a/common/src/test/java/dev/cel/common/values/ProtoLiteEqualityTest.java b/common/src/test/java/dev/cel/common/values/ProtoLiteEqualityTest.java new file mode 100644 index 000000000..5acdbca14 --- /dev/null +++ b/common/src/test/java/dev/cel/common/values/ProtoLiteEqualityTest.java @@ -0,0 +1,736 @@ +// Copyright 2026 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.common.values; + +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.assertThrows; + +import com.google.protobuf.Any; +import com.google.protobuf.ByteString; +import com.google.protobuf.CodedOutputStream; +import com.google.protobuf.DoubleValue; +import com.google.protobuf.ExtensionRegistryLite; +import com.google.protobuf.FloatValue; +import com.google.protobuf.Int32Value; +import com.google.protobuf.Int64Value; +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.ListValue; +import com.google.protobuf.Message; +import com.google.protobuf.NullValue; +import com.google.protobuf.Struct; +import com.google.protobuf.Value; +import com.google.testing.junit.testparameterinjector.TestParameter; +import com.google.testing.junit.testparameterinjector.TestParameterInjector; +import dev.cel.common.CelDescriptorUtil; +import dev.cel.common.internal.CelLiteDescriptorPool; +import dev.cel.common.internal.DefaultDescriptorPool; +import dev.cel.common.internal.DefaultLiteDescriptorPool; +import dev.cel.common.internal.DefaultMessageFactory; +import dev.cel.common.internal.DynamicProto; +import dev.cel.common.internal.ProtoEquality; +import dev.cel.common.internal.ProtoTimeUtils; +import dev.cel.expr.conformance.proto2.TestAllTypes.NestedGroup; +import dev.cel.expr.conformance.proto2.TestRequired; +import dev.cel.expr.conformance.proto2.TestRequiredCelDescriptor; +import dev.cel.expr.conformance.proto3.NestedTestAllTypes; +import dev.cel.expr.conformance.proto3.NestedTestAllTypesCelDescriptor; +import dev.cel.expr.conformance.proto3.TestAllTypes; +import dev.cel.expr.conformance.proto3.TestAllTypes.NestedEnum; +import dev.cel.expr.conformance.proto3.TestAllTypes.NestedMessage; +import dev.cel.expr.conformance.proto3.TestAllTypesCelDescriptor; +import dev.cel.protobuf.CelLiteDescriptor.MessageLiteDescriptor; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.time.Duration; +import java.time.Instant; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(TestParameterInjector.class) +public final class ProtoLiteEqualityTest { + + private static final CelLiteDescriptorPool DESCRIPTOR_POOL = + DefaultLiteDescriptorPool.newInstance( + TestAllTypesCelDescriptor.getDescriptor(), + NestedTestAllTypesCelDescriptor.getDescriptor(), + dev.cel.expr.conformance.proto2.TestAllTypesCelDescriptor.getDescriptor(), + dev.cel.expr.conformance.proto2.NestedTestAllTypesCelDescriptor.getDescriptor(), + TestRequiredCelDescriptor.getDescriptor()); + + private static final ProtoLiteEquality PROTO_LITE_EQUALITY = + new ProtoLiteEquality(DESCRIPTOR_POOL); + + private static final ProtoEquality PROTO_EQUALITY = + new ProtoEquality( + DynamicProto.create( + DefaultMessageFactory.create( + DefaultDescriptorPool.create( + CelDescriptorUtil.getAllDescriptorsFromFileDescriptor( + TestAllTypes.getDescriptor().getFile(), + dev.cel.expr.conformance.proto2.TestAllTypes.getDescriptor() + .getFile()))))); + + @Test + public void equals_matchesProtoEqualityOracle(@TestParameter OracleTestCase testCase) { + assertThat(PROTO_LITE_EQUALITY.equals(testCase.x, testCase.y)).isEqualTo(testCase.expected); + assertThat(PROTO_LITE_EQUALITY.equals(testCase.y, testCase.x)).isEqualTo(testCase.expected); + assertThat(PROTO_EQUALITY.equals(testCase.x, testCase.y)).isEqualTo(testCase.expected); + assertThat(PROTO_EQUALITY.equals(testCase.y, testCase.x)).isEqualTo(testCase.expected); + } + + @Test + public void equals_anyDivergentCases(@TestParameter AnyDivergentTestCase testCase) { + assertThat(PROTO_LITE_EQUALITY.equals(testCase.x, testCase.y)).isEqualTo(testCase.expected); + assertThat(PROTO_LITE_EQUALITY.equals(testCase.y, testCase.x)).isEqualTo(testCase.expected); + } + + @Test + public void equals_closedEnumMapEntryOutOfRange_divergesFromFullProto() { + dev.cel.expr.conformance.proto2.TestAllTypes withUnknownMapEnum = + parseProto2(out -> + out.writeBytes( + 83, + encode(entry -> { + entry.writeInt32(1, 1); + entry.writeEnum(2, 999); + }))); + dev.cel.expr.conformance.proto2.TestAllTypes empty = + dev.cel.expr.conformance.proto2.TestAllTypes.getDefaultInstance(); + + assertThat(PROTO_LITE_EQUALITY.equals(withUnknownMapEnum, empty)).isTrue(); + assertThat(PROTO_EQUALITY.equals(withUnknownMapEnum, empty)).isFalse(); + } + + @Test + public void equals_nestingDepthAtLimit_succeeds() { + NestedTestAllTypes left = nestedProto3Message(/* childCount= */ 99, /* leafInt= */ 1); + NestedTestAllTypes right = nestedProto3Message(/* childCount= */ 99, /* leafInt= */ 1); + + assertThat(PROTO_LITE_EQUALITY.equals(left, right)).isTrue(); + } + + @Test + public void equals_nestingDepthExceedsLimit_throwsIllegalArgumentException() { + NestedTestAllTypes left = nestedProto3Message(/* childCount= */ 100, /* leafInt= */ 1); + NestedTestAllTypes right = nestedProto3Message(/* childCount= */ 100, /* leafInt= */ 2); + + IllegalArgumentException thrown = + assertThrows(IllegalArgumentException.class, () -> PROTO_LITE_EQUALITY.equals(left, right)); + assertThat(thrown).hasMessageThat().isEqualTo("Message nesting exceeds 100"); + } + + @Test + public void equals_listValueDepthExceedsLimit_throwsIllegalArgumentException() { + Value left = nestedListValue(/* listDepth= */ 51, /* leaf= */ -0.0); + Value right = nestedListValue(/* listDepth= */ 51, /* leaf= */ 0.0); + + IllegalArgumentException thrown = + assertThrows(IllegalArgumentException.class, () -> PROTO_LITE_EQUALITY.equals(left, right)); + assertThat(thrown).hasMessageThat().isEqualTo("Message nesting exceeds 100"); + } + + @Test + public void equals_anyDepthExceedsLimit_throwsIllegalArgumentException() { + Any left = nestedAny(/* anyDepth= */ 101, /* leafInt= */ 1); + Any right = nestedAny(/* anyDepth= */ 101, /* leafInt= */ 2); + + IllegalArgumentException thrown = + assertThrows(IllegalArgumentException.class, () -> PROTO_LITE_EQUALITY.equals(left, right)); + assertThat(thrown).hasMessageThat().isEqualTo("Message nesting exceeds 100"); + } + + @Test + public void equals_unregisteredMessage_comparesByRawFields() { + ProtoLiteEquality emptyPoolEquality = + new ProtoLiteEquality(DefaultLiteDescriptorPool.newInstance()); + TestAllTypes nan1 = TestAllTypes.newBuilder().setSingleDouble(Double.NaN).build(); + TestAllTypes nan2 = TestAllTypes.newBuilder().setSingleDouble(Double.NaN).build(); + dev.cel.expr.conformance.proto2.TestAllTypes minusZero = + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder().setSingleDouble(-0.0).build(); + dev.cel.expr.conformance.proto2.TestAllTypes plusZero = + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder().setSingleDouble(0.0).build(); + + assertThat(emptyPoolEquality.equals(nan1, nan2)).isTrue(); + assertThat(emptyPoolEquality.equals(minusZero, plusZero)).isFalse(); + } + + @Test + public void rawFields_reorderedTagsAndSemanticFiltering() { + MessageLiteDescriptor descriptor = + DESCRIPTOR_POOL.findDescriptor(TestAllTypes.getDefaultInstance()).get(); + ByteString wireOrder1 = + encode(out -> { + out.writeInt32(1, 42); + out.writeDouble(12, -0.0); + out.writeInt32(998, 1); + out.writeInt32(999, 2); + }); + ByteString wireOrder2 = + encode(out -> { + out.writeInt32(999, 2); + out.writeDouble(12, 99.5); + out.writeInt32(998, 1); + out.writeInt32(1, 42); + }); + + assertThat(ProtoLiteEquality.rawFields(wireOrder1, descriptor)) + .isEqualTo(ProtoLiteEquality.rawFields(wireOrder2, descriptor)); + } + + @Test + public void rawFields_malformedWireBytes_throwsIllegalArgumentException() { + ByteString malformed = ByteString.copyFromUtf8("test"); + + assertThrows( + IllegalArgumentException.class, + () -> ProtoLiteEquality.rawFields(malformed, /* descriptor= */ null)); + } + + private interface WireEncoder { + void encode(CodedOutputStream out) throws IOException; + } + + private static ByteString encode(WireEncoder encoder) { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + CodedOutputStream out = CodedOutputStream.newInstance(bytes); + try { + encoder.encode(out); + out.flush(); + } catch (IOException e) { + throw new AssertionError(e); + } + return ByteString.copyFrom(bytes.toByteArray()); + } + + private static TestAllTypes parseProto3(WireEncoder encoder) { + try { + return TestAllTypes.parseFrom(encode(encoder), ExtensionRegistryLite.getEmptyRegistry()); + } catch (InvalidProtocolBufferException e) { + throw new AssertionError(e); + } + } + + private static dev.cel.expr.conformance.proto2.TestAllTypes parseProto2(WireEncoder encoder) { + try { + return dev.cel.expr.conformance.proto2.TestAllTypes.parseFrom( + encode(encoder), ExtensionRegistryLite.getEmptyRegistry()); + } catch (InvalidProtocolBufferException e) { + throw new AssertionError(e); + } + } + + private static DoubleValue parseDoubleValue(WireEncoder encoder) { + try { + return DoubleValue.parseFrom(encode(encoder), ExtensionRegistryLite.getEmptyRegistry()); + } catch (InvalidProtocolBufferException e) { + throw new AssertionError(e); + } + } + + private static ByteString mapInt32StringEntry(int key, String value) { + return encode(out -> { + out.writeInt32(1, key); + out.writeString(2, value); + }); + } + + private static Value nestedListValue(int listDepth, double leaf) { + Value current = Value.newBuilder().setNumberValue(leaf).build(); + for (int i = 0; i < listDepth; i++) { + current = Value.newBuilder().setListValue(ListValue.newBuilder().addValues(current)).build(); + } + return current; + } + + private static NestedTestAllTypes nestedProto3Message(int childCount, int leafInt) { + NestedTestAllTypes current = + NestedTestAllTypes.newBuilder() + .setPayload(TestAllTypes.newBuilder().setSingleInt32(leafInt)) + .build(); + for (int i = 0; i < childCount; i++) { + current = NestedTestAllTypes.newBuilder().setChild(current).build(); + } + return current; + } + + private static Any nestedAny(int anyDepth, int leafInt) { + Any current = Any.pack(TestAllTypes.newBuilder().setSingleInt32(leafInt).build()); + for (int i = 1; i < anyDepth; i++) { + current = Any.pack(current); + } + return current; + } + + private static dev.cel.expr.conformance.proto2.TestAllTypes populatedProto2Scalars( + double singleDouble) { + return dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .setSingleInt32(-10) + .setSingleInt64(-20L) + .setSingleUint32(30) + .setSingleUint64(40L) + .setSingleSint32(-50) + .setSingleSint64(-60L) + .setSingleFixed32(70) + .setSingleFixed64(80L) + .setSingleSfixed32(-90) + .setSingleSfixed64(-100L) + .setSingleFloat(1.25f) + .setSingleDouble(singleDouble) + .setSingleBool(false) + .setSingleString("hello") + .setSingleBytes(ByteString.copyFromUtf8("bytes")) + .setStandaloneEnum(dev.cel.expr.conformance.proto2.TestAllTypes.NestedEnum.BAZ) + .setSingleDuration(ProtoTimeUtils.toProtoDuration(Duration.ofSeconds(12, 345))) + .setSingleTimestamp(ProtoTimeUtils.toProtoTimestamp(Instant.ofEpochSecond(1000L, 500))) + .build(); + } + + @SuppressWarnings("ImmutableEnumChecker") // Test only + private enum AnyDivergentTestCase { + UNKNOWN_TYPE_URLS_DIFFER( + Any.newBuilder() + .setTypeUrl("type.googleapis.com/unknown.TypeA") + .setValue(encode(out -> out.writeInt32(1, 7))) + .build(), + Any.newBuilder() + .setTypeUrl("type.googleapis.com/unknown.TypeB") + .setValue(encode(out -> out.writeInt32(1, 7))) + .build(), + false), + MALFORMED_TYPE_URLS_WITHOUT_SLASH_DIFFER( + Any.newBuilder().setTypeUrl("no_slash_a").setValue(ByteString.EMPTY).build(), + Any.newBuilder().setTypeUrl("no_slash_b").setValue(ByteString.EMPTY).build(), + false), + MALFORMED_TYPE_URL_WITHOUT_SLASH_DOES_NOT_UNPACK( + Any.newBuilder() + .setTypeUrl("cel.expr.conformance.proto2.TestAllTypes") + .setValue( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .setSingleDouble(-0.0) + .build() + .toByteString()) + .build(), + Any.newBuilder() + .setTypeUrl("cel.expr.conformance.proto2.TestAllTypes") + .setValue( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .setSingleDouble(0.0) + .build() + .toByteString()) + .build(), + false), + UNPACKABLE_VS_UNKNOWN_TYPE_URL( + Any.pack(TestAllTypes.newBuilder().setSingleInt32(7).build()), + Any.newBuilder() + .setTypeUrl("type.googleapis.com/unknown.TypeA") + .setValue(encode(out -> out.writeInt32(1, 7))) + .build(), + false), + CORRUPT_PAYLOAD_SAME_BYTES( + Any.newBuilder() + .setTypeUrl("type.googleapis.com/cel.expr.conformance.proto3.TestAllTypes") + .setValue(ByteString.copyFromUtf8("test")) + .build(), + Any.newBuilder() + .setTypeUrl("type.googleapis.com/cel.expr.conformance.proto3.TestAllTypes") + .setValue(ByteString.copyFromUtf8("test")) + .build(), + true), + CORRUPT_PAYLOAD_DIFFERENT_BYTES( + Any.newBuilder() + .setTypeUrl("type.googleapis.com/cel.expr.conformance.proto3.TestAllTypes") + .setValue(ByteString.copyFromUtf8("test")) + .build(), + Any.newBuilder() + .setTypeUrl("type.googleapis.com/cel.expr.conformance.proto3.TestAllTypes") + .setValue(ByteString.copyFromUtf8("other")) + .build(), + false), + UNINITIALIZED_REQUIRED_FIELD_PARTIAL_UNPACK( + Any.newBuilder() + .setTypeUrl("type.googleapis.com/" + TestRequired.getDescriptor().getFullName()) + .setValue( + encode(out -> { + out.writeInt32(998, 1); + out.writeInt32(999, 2); + })) + .build(), + Any.newBuilder() + .setTypeUrl("type.googleapis.com/" + TestRequired.getDescriptor().getFullName()) + .setValue( + encode(out -> { + out.writeInt32(999, 2); + out.writeInt32(998, 1); + })) + .build(), + true), + MAP_ENTRY_TYPE_URL_FALLS_BACK_TO_BYTES( + Any.newBuilder() + .setTypeUrl("type.googleapis.com/google.protobuf.Struct.FieldsEntry") + .setValue( + encode(out -> { + out.writeString(1, "k"); + out.writeBytes(2, Value.newBuilder().setNumberValue(-0.0).build().toByteString()); + })) + .build(), + Any.newBuilder() + .setTypeUrl("type.googleapis.com/google.protobuf.Struct.FieldsEntry") + .setValue( + encode(out -> { + out.writeString(1, "k"); + out.writeBytes(2, Value.newBuilder().setNumberValue(0.0).build().toByteString()); + })) + .build(), + false), + ; + + private final Any x; + private final Any y; + private final boolean expected; + + AnyDivergentTestCase(Any x, Any y, boolean expected) { + this.x = x; + this.y = y; + this.expected = expected; + } + } + + @SuppressWarnings("ImmutableEnumChecker") // Test only + private enum OracleTestCase { + NAN_DOUBLE( + TestAllTypes.newBuilder().setSingleDouble(Double.NaN).build(), + TestAllTypes.newBuilder().setSingleDouble(Double.NaN).build(), + false), + NAN_FLOAT( + TestAllTypes.newBuilder().setSingleFloat(Float.NaN).build(), + TestAllTypes.newBuilder().setSingleFloat(Float.NaN).build(), + false), + NAN_REPEATED_DOUBLE( + TestAllTypes.newBuilder().addRepeatedDouble(1.0).addRepeatedDouble(Double.NaN).build(), + TestAllTypes.newBuilder().addRepeatedDouble(1.0).addRepeatedDouble(Double.NaN).build(), + false), + NAN_MAP_DOUBLE( + TestAllTypes.newBuilder().putMapStringDouble("k", Double.NaN).build(), + TestAllTypes.newBuilder().putMapStringDouble("k", Double.NaN).build(), + false), + PROTO2_EXPLICIT_SIGNED_ZERO_DOUBLE( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder().setSingleDouble(-0.0).build(), + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder().setSingleDouble(0.0).build(), + true), + PROTO2_EXPLICIT_SIGNED_ZERO_FLOAT( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder().setSingleFloat(-0.0f).build(), + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder().setSingleFloat(0.0f).build(), + true), + PROTO3_IMPLICIT_NEGATIVE_ZERO_VS_UNSET_DOUBLE( + TestAllTypes.newBuilder().setSingleDouble(-0.0).build(), + TestAllTypes.newBuilder().setSingleDouble(0.0).build(), + false), + PROTO3_IMPLICIT_NEGATIVE_ZERO_VS_UNSET_FLOAT( + TestAllTypes.newBuilder().setSingleFloat(-0.0f).build(), + TestAllTypes.newBuilder().setSingleFloat(0.0f).build(), + false), + ALL_SCALAR_KINDS_EQUAL(populatedProto2Scalars(-0.0), populatedProto2Scalars(0.0), true), + SCALAR_INT32_DIFFERS( + TestAllTypes.newBuilder().setSingleInt32(1).build(), + TestAllTypes.newBuilder().setSingleInt32(2).build(), + false), + SCALAR_ENUM_DIFFERS( + TestAllTypes.newBuilder().setStandaloneEnum(NestedEnum.BAR).build(), + TestAllTypes.newBuilder().setStandaloneEnum(NestedEnum.BAZ).build(), + false), + PROTO2_EXPLICIT_DEFAULT_INT32_VS_UNSET( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder().setSingleInt32(-32).build(), + dev.cel.expr.conformance.proto2.TestAllTypes.getDefaultInstance(), + false), + PROTO2_EXPLICIT_DEFAULT_DOUBLE_VS_UNSET( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder().setSingleDouble(6.4).build(), + dev.cel.expr.conformance.proto2.TestAllTypes.getDefaultInstance(), + false), + PROTO3_OPTIONAL_BOOL_FALSE_VS_UNSET( + TestAllTypes.newBuilder().setOptionalBool(false).build(), + TestAllTypes.getDefaultInstance(), + false), + PROTO3_PACKED_REPEATED_SIGNED_ZERO_DOUBLES( + TestAllTypes.newBuilder().addRepeatedDouble(-0.0).addRepeatedDouble(1.5).build(), + TestAllTypes.newBuilder().addRepeatedDouble(0.0).addRepeatedDouble(1.5).build(), + true), + PROTO2_UNPACKED_REPEATED_SIGNED_ZERO_DOUBLES( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .addRepeatedDouble(-0.0) + .addRepeatedDouble(1.5) + .build(), + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .addRepeatedDouble(0.0) + .addRepeatedDouble(1.5) + .build(), + true), + PROTO3_UNPACKED_WIRE_VS_PACKED_REPEATED_DOUBLE( + parseProto3(out -> out.writeDouble(42, -0.0)), + TestAllTypes.newBuilder().addRepeatedDouble(0.0).build(), + true), + REPEATED_ORDER_DIFFERS( + TestAllTypes.newBuilder().addRepeatedInt32(1).addRepeatedInt32(2).build(), + TestAllTypes.newBuilder().addRepeatedInt32(2).addRepeatedInt32(1).build(), + false), + REPEATED_SIZE_DIFFERS( + TestAllTypes.newBuilder().addRepeatedDouble(1.0).build(), + TestAllTypes.newBuilder().addRepeatedDouble(1.0).addRepeatedDouble(2.0).build(), + false), + MAP_REORDERED_ENTRIES_EQUAL( + parseProto3(out -> { + out.writeBytes(75, mapInt32StringEntry(1, "a")); + out.writeBytes(75, mapInt32StringEntry(2, "b")); + }), + parseProto3(out -> { + out.writeBytes(75, mapInt32StringEntry(2, "b")); + out.writeBytes(75, mapInt32StringEntry(1, "a")); + }), + true), + MAP_SIGNED_ZERO_DOUBLE_VALUES( + TestAllTypes.newBuilder().putMapStringDouble("k", -0.0).build(), + TestAllTypes.newBuilder().putMapStringDouble("k", 0.0).build(), + true), + MAP_SIGNED_ZERO_FLOAT_VALUES( + TestAllTypes.newBuilder().putMapStringFloat("k", -0.0f).build(), + TestAllTypes.newBuilder().putMapStringFloat("k", 0.0f).build(), + true), + MAP_KEY_DIFFERS( + TestAllTypes.newBuilder().putMapStringDouble("k1", 0.0).build(), + TestAllTypes.newBuilder().putMapStringDouble("k2", 0.0).build(), + false), + MAP_VALUE_DIFFERS( + TestAllTypes.newBuilder().putMapStringInt32("k", 1).build(), + TestAllTypes.newBuilder().putMapStringInt32("k", 2).build(), + false), + MAP_SIZE_DIFFERS( + TestAllTypes.newBuilder().putMapStringInt32("k1", 1).build(), + TestAllTypes.newBuilder().putMapStringInt32("k1", 1).putMapStringInt32("k2", 2).build(), + false), + NESTED_MESSAGE_SIGNED_ZERO_EQUAL( + dev.cel.expr.conformance.proto2.NestedTestAllTypes.newBuilder() + .setChild( + dev.cel.expr.conformance.proto2.NestedTestAllTypes.newBuilder() + .setPayload( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .setSingleDouble(-0.0))) + .build(), + dev.cel.expr.conformance.proto2.NestedTestAllTypes.newBuilder() + .setChild( + dev.cel.expr.conformance.proto2.NestedTestAllTypes.newBuilder() + .setPayload( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .setSingleDouble(0.0))) + .build(), + true), + NESTED_MESSAGE_PRESENT_VS_ABSENT( + TestAllTypes.newBuilder() + .setSingleNestedMessage(NestedMessage.getDefaultInstance()) + .build(), + TestAllTypes.getDefaultInstance(), + false), + DOUBLE_VALUE_SIGNED_ZERO_UNEQUAL(DoubleValue.of(-0.0), DoubleValue.of(0.0), false), + FLOAT_VALUE_EQUAL(FloatValue.of(1.5f), FloatValue.of(1.5f), true), + FLOAT_VALUE_SIGNED_ZERO_UNEQUAL(FloatValue.of(-0.0f), FloatValue.of(0.0f), false), + DOUBLE_VALUE_NAN_UNEQUAL(DoubleValue.of(Double.NaN), DoubleValue.of(Double.NaN), false), + FLOAT_VALUE_NAN_UNEQUAL(FloatValue.of(Float.NaN), FloatValue.of(Float.NaN), false), + DOUBLE_VALUE_DIFFERENT_UNKNOWN_FIELDS_UNEQUAL( + parseDoubleValue(out -> { + out.writeDouble(1, 1.5); + out.writeInt32(999, 1); + }), + parseDoubleValue(out -> { + out.writeDouble(1, 1.5); + out.writeInt32(999, 2); + }), + false), + VALUE_UNSET_VS_NULL_VALUE_UNEQUAL( + Value.getDefaultInstance(), + Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build(), + false), + VALUE_ZERO_NUMBER_VS_UNSET( + Value.newBuilder().setNumberValue(0.0).build(), Value.getDefaultInstance(), false), + VALUE_STRING_VS_BOOL_UNEQUAL( + Value.newBuilder().setStringValue("true").build(), + Value.newBuilder().setBoolValue(true).build(), + false), + VALUE_STRING_DIFFERS( + Value.newBuilder().setStringValue("a").build(), + Value.newBuilder().setStringValue("b").build(), + false), + VALUE_SIGNED_ZERO_EQUAL( + Value.newBuilder().setNumberValue(-0.0).build(), + Value.newBuilder().setNumberValue(0.0).build(), + true), + VALUE_NAN_UNEQUAL( + Value.newBuilder().setNumberValue(Double.NaN).build(), + Value.newBuilder().setNumberValue(Double.NaN).build(), + false), + VALUE_STRUCT_SIGNED_ZERO_EQUAL( + Value.newBuilder() + .setStructValue( + Struct.newBuilder().putFields("a", Value.newBuilder().setNumberValue(-0.0).build())) + .build(), + Value.newBuilder() + .setStructValue( + Struct.newBuilder().putFields("a", Value.newBuilder().setNumberValue(0.0).build())) + .build(), + true), + VALUE_STRUCT_UNEQUAL( + Value.newBuilder() + .setStructValue( + Struct.newBuilder().putFields("a", Value.newBuilder().setNumberValue(1.0).build())) + .build(), + Value.newBuilder() + .setStructValue( + Struct.newBuilder().putFields("a", Value.newBuilder().setNumberValue(2.0).build())) + .build(), + false), + VALUE_LIST_UNEQUAL( + Value.newBuilder() + .setListValue(ListValue.newBuilder().addValues(Value.newBuilder().setNumberValue(1.0))) + .build(), + Value.newBuilder() + .setListValue(ListValue.newBuilder().addValues(Value.newBuilder().setNumberValue(2.0))) + .build(), + false), + STRUCT_SIGNED_ZERO_EQUAL( + Struct.newBuilder().putFields("a", Value.newBuilder().setNumberValue(-0.0).build()).build(), + Struct.newBuilder().putFields("a", Value.newBuilder().setNumberValue(0.0).build()).build(), + true), + STRUCT_NAN_UNEQUAL( + Struct.newBuilder() + .putFields("a", Value.newBuilder().setNumberValue(Double.NaN).build()) + .build(), + Struct.newBuilder() + .putFields("a", Value.newBuilder().setNumberValue(Double.NaN).build()) + .build(), + false), + LIST_VALUE_SIGNED_ZERO_EQUAL( + ListValue.newBuilder().addValues(Value.newBuilder().setNumberValue(-0.0)).build(), + ListValue.newBuilder().addValues(Value.newBuilder().setNumberValue(0.0)).build(), + true), + LIST_VALUE_NAN_UNEQUAL( + ListValue.newBuilder().addValues(Value.newBuilder().setNumberValue(Double.NaN)).build(), + ListValue.newBuilder().addValues(Value.newBuilder().setNumberValue(Double.NaN)).build(), + false), + SCALAR_VALUE_AT_NESTING_LIMIT_EQUAL( + nestedListValue(/* listDepth= */ 50, /* leaf= */ -0.0), + nestedListValue(/* listDepth= */ 50, /* leaf= */ 0.0), + true), + ANY_REENCODED_PAYLOAD_EQUAL( + Any.pack( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .setSingleDouble(-0.0) + .build()), + Any.pack( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder().setSingleDouble(0.0).build()), + true), + ANY_EMPTY_URL_PREFIX_REENCODED_EQUAL( + Any.newBuilder() + .setTypeUrl("/cel.expr.conformance.proto2.TestAllTypes") + .setValue( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .setSingleDouble(-0.0) + .build() + .toByteString()) + .build(), + Any.newBuilder() + .setTypeUrl("/cel.expr.conformance.proto2.TestAllTypes") + .setValue( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .setSingleDouble(0.0) + .build() + .toByteString()) + .build(), + true), + ANY_NAN_PAYLOAD_UNEQUAL( + Any.pack(TestAllTypes.newBuilder().setSingleDouble(Double.NaN).build()), + Any.pack(TestAllTypes.newBuilder().setSingleDouble(Double.NaN).build()), + false), + ANY_DIFFERENT_PACKED_TYPES_UNEQUAL( + Any.pack(Int32Value.of(1)), Any.pack(Int64Value.of(1)), false), + UNKNOWN_FIELDS_EQUAL( + parseProto3(out -> out.writeInt32(999, 42)), + parseProto3(out -> out.writeInt32(999, 42)), + true), + UNKNOWN_FIELDS_DIFFERENT_VALUES( + parseProto3(out -> out.writeInt32(999, 42)), + parseProto3(out -> out.writeInt32(999, 43)), + false), + UNKNOWN_FIELDS_REORDERED_WITHIN_SAME_TAG( + parseProto3(out -> { + out.writeInt32(999, 1); + out.writeInt32(999, 2); + }), + parseProto3(out -> { + out.writeInt32(999, 2); + out.writeInt32(999, 1); + }), + false), + CLOSED_ENUM_UNKNOWN_SINGULAR_EQUAL( + parseProto2(out -> out.writeEnum(24, 999)), + parseProto2(out -> out.writeEnum(24, 999)), + true), + CLOSED_ENUM_UNKNOWN_SINGULAR_VS_UNSET( + parseProto2(out -> out.writeEnum(24, 999)), + dev.cel.expr.conformance.proto2.TestAllTypes.getDefaultInstance(), + false), + CLOSED_ENUM_UNKNOWN_REPEATED_REORDERED_UNEQUAL( + parseProto2(out -> { + out.writeEnum(52, 998); + out.writeEnum(52, 999); + }), + parseProto2(out -> { + out.writeEnum(52, 999); + out.writeEnum(52, 998); + }), + false), + LENGTH_DELIMITED_WIRE_ON_SINGULAR_DOUBLE_VS_EMPTY( + parseProto3(out -> out.writeString(12, "x")), TestAllTypes.getDefaultInstance(), false), + VARINT_WIRE_ON_REPEATED_DOUBLE_VS_EMPTY( + parseProto3(out -> out.writeInt32(42, 7)), TestAllTypes.getDefaultInstance(), false), + PROTO2_GROUP_EQUAL( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .setNestedGroup(NestedGroup.newBuilder().setSingleId(1).setSingleName("a")) + .build(), + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .setNestedGroup(NestedGroup.newBuilder().setSingleId(1).setSingleName("a")) + .build(), + true), + PROTO2_GROUP_UNEQUAL( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .setNestedGroup(NestedGroup.newBuilder().setSingleId(1)) + .build(), + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .setNestedGroup(NestedGroup.newBuilder().setSingleId(2)) + .build(), + false), + DIFFERENT_MESSAGE_CLASSES( + dev.cel.expr.conformance.proto2.TestAllTypes.getDefaultInstance(), + TestAllTypes.getDefaultInstance(), + false), + ; + + private final Message x; + private final Message y; + private final boolean expected; + + OracleTestCase(Message x, Message y, boolean expected) { + this.x = x; + this.y = y; + this.expected = expected; + } + } +} diff --git a/common/src/test/java/dev/cel/common/values/ProtoMessageLiteValueProviderTest.java b/common/src/test/java/dev/cel/common/values/ProtoMessageLiteValueProviderTest.java index bbe116c80..b58324001 100644 --- a/common/src/test/java/dev/cel/common/values/ProtoMessageLiteValueProviderTest.java +++ b/common/src/test/java/dev/cel/common/values/ProtoMessageLiteValueProviderTest.java @@ -15,40 +15,336 @@ package dev.cel.common.values; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat; +import static org.junit.Assert.assertThrows; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.primitives.UnsignedLong; +import com.google.protobuf.Any; +import com.google.protobuf.ByteString; +import com.google.protobuf.Empty; +import com.google.protobuf.FieldMask; +import com.google.protobuf.Int64Value; +import com.google.protobuf.Struct; +import com.google.protobuf.UInt32Value; +import com.google.protobuf.UninitializedMessageException; +import com.google.protobuf.Value; +import com.google.testing.junit.testparameterinjector.TestParameter; import com.google.testing.junit.testparameterinjector.TestParameterInjector; +import dev.cel.common.exceptions.CelNumericOverflowException; +import dev.cel.common.internal.DefaultLiteDescriptorPool; +import dev.cel.common.internal.ProtoTimeUtils; import dev.cel.common.types.StructTypeReference; +import dev.cel.expr.conformance.proto2.TestRequiredCelDescriptor; +import dev.cel.expr.conformance.proto3.NestedTestAllTypes; import dev.cel.expr.conformance.proto3.TestAllTypes; +import dev.cel.expr.conformance.proto3.TestAllTypes.NestedEnum; +import dev.cel.expr.conformance.proto3.TestAllTypes.NestedMessage; import dev.cel.expr.conformance.proto3.TestAllTypesCelDescriptor; +import java.time.Instant; +import java.util.Map; import java.util.Optional; import org.junit.Test; import org.junit.runner.RunWith; @RunWith(TestParameterInjector.class) public class ProtoMessageLiteValueProviderTest { + private static final String TEST_ALL_TYPES = "cel.expr.conformance.proto3.TestAllTypes"; + private static final NestedMessage NESTED = NestedMessage.newBuilder().setBb(7).build(); + private static final Value JSON_NULL = + Value.newBuilder().setNullValue(com.google.protobuf.NullValue.NULL_VALUE).build(); + private static final ProtoLiteCelValueConverter EMPTY_CONVERTER = + ProtoLiteCelValueConverter.newInstance(DefaultLiteDescriptorPool.newInstance()); + private static final WireMessageLite WIRE_NESTED = + RawProtoMessageLiteValue.create( + NESTED.toByteString(), + "cel.expr.conformance.proto3.TestAllTypes.NestedMessage", + EMPTY_CONVERTER); + private static final WireMessageLite WIRE_OF_UNKNOWN_TYPE = + RawProtoMessageLiteValue.create(NESTED.toByteString(), EMPTY_CONVERTER); private static final ProtoMessageLiteValueProvider VALUE_PROVIDER = - ProtoMessageLiteValueProvider.newInstance(TestAllTypesCelDescriptor.getDescriptor()); + ProtoMessageLiteValueProvider.newInstance( + TestAllTypesCelDescriptor.getDescriptor(), TestRequiredCelDescriptor.getDescriptor()); @Test - public void newValue_unknownType_returnsEmpty() { - assertThat(VALUE_PROVIDER.newValue("unknownType", ImmutableMap.of())).isEmpty(); + public void newValue_unknownOrMapEntryType_returnsEmpty( + @TestParameter({ + "unknownType", + "google.protobuf.Struct.FieldsEntry", + "cel.expr.conformance.proto3.TestAllTypes.MapStringStringEntry" + }) + String structType) { + assertThat(VALUE_PROVIDER.newValue(structType, ImmutableMap.of())).isEmpty(); } @Test public void newValue_emptyFields_success() { - Optional value = - VALUE_PROVIDER.newValue("cel.expr.conformance.proto3.TestAllTypes", ImmutableMap.of()); + Optional value = VALUE_PROVIDER.newValue(TEST_ALL_TYPES, ImmutableMap.of()); ProtoMessageLiteValue protoMessageLiteValue = (ProtoMessageLiteValue) value.get(); assertThat(protoMessageLiteValue.value()).isEqualTo(TestAllTypes.getDefaultInstance()); assertThat(protoMessageLiteValue.isZeroValue()).isTrue(); assertThat(protoMessageLiteValue.celType()) - .isEqualTo(StructTypeReference.create("cel.expr.conformance.proto3.TestAllTypes")); + .isEqualTo(StructTypeReference.create(TEST_ALL_TYPES)); + } + + @SuppressWarnings("ImmutableEnumChecker") // Test only + private enum FieldTestCase { + INT32("single_int32", -1L, TestAllTypes.newBuilder().setSingleInt32(-1)), + INT64("single_int64", Long.MIN_VALUE, TestAllTypes.newBuilder().setSingleInt64(Long.MIN_VALUE)), + UINT32( + "single_uint32", + UnsignedLong.valueOf(4294967295L), + TestAllTypes.newBuilder().setSingleUint32(-1)), + UINT64("single_uint64", UnsignedLong.MAX_VALUE, TestAllTypes.newBuilder().setSingleUint64(-1L)), + SINT32("single_sint32", -2L, TestAllTypes.newBuilder().setSingleSint32(-2)), + SINT64("single_sint64", -3L, TestAllTypes.newBuilder().setSingleSint64(-3L)), + FIXED32( + "single_fixed32", + UnsignedLong.valueOf(4294967295L), + TestAllTypes.newBuilder().setSingleFixed32(-1)), + FIXED64( + "single_fixed64", UnsignedLong.MAX_VALUE, TestAllTypes.newBuilder().setSingleFixed64(-1L)), + SFIXED32("single_sfixed32", -4L, TestAllTypes.newBuilder().setSingleSfixed32(-4)), + SFIXED64("single_sfixed64", -5L, TestAllTypes.newBuilder().setSingleSfixed64(-5L)), + FLOAT("single_float", 1.5d, TestAllTypes.newBuilder().setSingleFloat(1.5f)), + FLOAT_OVERFLOW( + "single_float", 1.4e55d, TestAllTypes.newBuilder().setSingleFloat(Float.POSITIVE_INFINITY)), + DOUBLE("single_double", -2.5d, TestAllTypes.newBuilder().setSingleDouble(-2.5d)), + BOOL("single_bool", true, TestAllTypes.newBuilder().setSingleBool(true)), + STRING("single_string", "héllo", TestAllTypes.newBuilder().setSingleString("héllo")), + BYTES( + "single_bytes", + CelByteString.copyFromUtf8("abc"), + TestAllTypes.newBuilder().setSingleBytes(ByteString.copyFromUtf8("abc"))), + ENUM("standalone_enum", 2L, TestAllTypes.newBuilder().setStandaloneEnum(NestedEnum.BAZ)), + OPEN_ENUM_UNKNOWN_VALUE( + "standalone_enum", 99L, TestAllTypes.newBuilder().setStandaloneEnumValue(99)), + IMPLICIT_PRESENCE_DEFAULT("single_int32", 0L, TestAllTypes.newBuilder()), + EXPLICIT_PRESENCE_DEFAULT( + "optional_bool", false, TestAllTypes.newBuilder().setOptionalBool(false)), + MESSAGE( + "single_nested_message", NESTED, TestAllTypes.newBuilder().setSingleNestedMessage(NESTED)), + WIRE_MESSAGE( + "single_nested_message", + WIRE_NESTED, + TestAllTypes.newBuilder().setSingleNestedMessage(NESTED)), + NULL_MESSAGE("single_nested_message", NullValue.NULL_VALUE, TestAllTypes.newBuilder()), + NULL_VALUE( + "single_value", NullValue.NULL_VALUE, TestAllTypes.newBuilder().setSingleValue(JSON_NULL)), + NULL_ANY( + "single_any", + NullValue.NULL_VALUE, + TestAllTypes.newBuilder().setSingleAny(Any.pack(JSON_NULL))), + WRAPPER( + "single_int64_wrapper", + 5L, + TestAllTypes.newBuilder().setSingleInt64Wrapper(Int64Value.of(5))), + UINT32_WRAPPER( + "single_uint32_wrapper", + UnsignedLong.valueOf(4294967295L), + TestAllTypes.newBuilder().setSingleUint32Wrapper(UInt32Value.of(-1))), + ANY_MESSAGE("single_any", NESTED, TestAllTypes.newBuilder().setSingleAny(Any.pack(NESTED))), + ANY_WIRE_MESSAGE( + "single_any", WIRE_NESTED, TestAllTypes.newBuilder().setSingleAny(Any.pack(NESTED))), + ANY_PRIMITIVE( + "single_any", 1L, TestAllTypes.newBuilder().setSingleAny(Any.pack(Int64Value.of(1)))), + JSON_STRUCT( + "single_struct", + ImmutableMap.of("a", true), + TestAllTypes.newBuilder() + .setSingleStruct( + Struct.newBuilder().putFields("a", Value.newBuilder().setBoolValue(true).build()))), + FIELD_MASK( + "field_mask", + FieldMask.newBuilder().addPaths("a.b").build(), + TestAllTypes.newBuilder().setFieldMask(FieldMask.newBuilder().addPaths("a.b"))), + EMPTY( + "empty", ImmutableMap.of(), TestAllTypes.newBuilder().setEmpty(Empty.getDefaultInstance())), + REPEATED_SCALAR( + "repeated_int32", + ImmutableList.of(1L, -2L), + TestAllTypes.newBuilder().addRepeatedInt32(1).addRepeatedInt32(-2)), + REPEATED_MESSAGE_DROPS_NULLS( + "repeated_nested_message", + ImmutableList.of(NESTED, NullValue.NULL_VALUE), + TestAllTypes.newBuilder().addRepeatedNestedMessage(NESTED)), + REPEATED_ANY_KEEPS_NULLS( + "repeated_any", + ImmutableList.of(1L, NullValue.NULL_VALUE), + TestAllTypes.newBuilder() + .addRepeatedAny(Any.pack(Int64Value.of(1))) + .addRepeatedAny(Any.pack(JSON_NULL))), + MAP( + "map_string_message", + ImmutableMap.of("a", NESTED, "b", NestedMessage.getDefaultInstance()), + TestAllTypes.newBuilder() + .putMapStringMessage("a", NESTED) + .putMapStringMessage("b", NestedMessage.getDefaultInstance())), + MAP_DROPS_NULLS( + "map_bool_timestamp", + ImmutableMap.of(true, NullValue.NULL_VALUE, false, Instant.EPOCH), + TestAllTypes.newBuilder().putMapBoolTimestamp(false, ProtoTimeUtils.TIMESTAMP_EPOCH)), + MAP_VALUE_KEEPS_NULLS( + "map_string_value", + ImmutableMap.of("k", NullValue.NULL_VALUE), + TestAllTypes.newBuilder().putMapStringValue("k", JSON_NULL)), + ONEOF_MESSAGE_LAST( + ImmutableMap.of("oneof_bool", true, "oneof_msg", NESTED), + TestAllTypes.newBuilder().setOneofMsg(NESTED)), + ONEOF_BOOL_LAST( + ImmutableMap.of("oneof_msg", NESTED, "oneof_bool", true), + TestAllTypes.newBuilder().setOneofBool(true)); + + private final ImmutableMap fields; + private final TestAllTypes expected; + + FieldTestCase(String fieldName, Object value, TestAllTypes.Builder expected) { + this(ImmutableMap.of(fieldName, value), expected); + } + + FieldTestCase(ImmutableMap fields, TestAllTypes.Builder expected) { + this.fields = fields; + this.expected = expected.build(); + } + } + + @Test + public void newValue_fields_success(@TestParameter FieldTestCase testCase) { + TestAllTypes message = newTestAllTypes(testCase.fields); + + assertThat(message).isEqualTo(testCase.expected); + } + + @Test + public void newValue_structLiteral_returnsMap() { + ImmutableMap fields = ImmutableMap.of("fields", ImmutableMap.of("a", 1.0d)); + + assertThat(VALUE_PROVIDER.newValue("google.protobuf.Struct", fields)) + .hasValue(ImmutableMap.of("a", 1.0d)); + } + + @SuppressWarnings("ImmutableEnumChecker") // Test only + private enum InvalidFieldTestCase { + UNDECLARED_FIELD("undeclared", 1L, IllegalArgumentException.class, "is not declared"), + INT32_OVERFLOW("single_int32", 1L << 31, CelNumericOverflowException.class, "Out of range"), + SINT32_UNDERFLOW( + "single_sint32", -(1L << 31) - 1, CelNumericOverflowException.class, "Out of range"), + SFIXED32_OVERFLOW( + "single_sfixed32", 1L << 31, CelNumericOverflowException.class, "Out of range"), + UINT32_OVERFLOW( + "single_uint32", + UnsignedLong.valueOf(1L << 32), + CelNumericOverflowException.class, + "out of range"), + FIXED32_OVERFLOW( + "single_fixed32", + UnsignedLong.valueOf(1L << 32), + CelNumericOverflowException.class, + "out of range"), + NULL_SCALAR( + "single_int32", + NullValue.NULL_VALUE, + IllegalArgumentException.class, + "null cannot be assigned"), + NULL_MAP( + "map_string_string", + NullValue.NULL_VALUE, + IllegalArgumentException.class, + "null cannot be assigned"), + NULL_LIST( + "repeated_nested_message", + NullValue.NULL_VALUE, + IllegalArgumentException.class, + "null cannot be assigned"), + NULL_STRUCT( + "single_struct", + NullValue.NULL_VALUE, + IllegalArgumentException.class, + "null cannot be assigned"), + NULL_LIST_VALUE( + "list_value", + NullValue.NULL_VALUE, + IllegalArgumentException.class, + "null cannot be assigned"), + MESSAGE_OF_ANOTHER_TYPE( + "single_nested_message", + TestAllTypes.getDefaultInstance(), + IllegalArgumentException.class, + "expected a message of type"), + UNREGISTERED_MESSAGE_CLASS( + "single_nested_message", + NestedTestAllTypes.getDefaultInstance(), + IllegalArgumentException.class, + "expected a message of type"), + WIRE_MESSAGE_OF_UNKNOWN_TYPE( + "single_nested_message", + WIRE_OF_UNKNOWN_TYPE, + IllegalArgumentException.class, + "expected a message of type"), + FIELD_MASK_OF_ANOTHER_TYPE( + "field_mask", NESTED, IllegalArgumentException.class, "expected a message of type"), + ANY_OF_UNKNOWN_TYPE( + "single_any", WIRE_OF_UNKNOWN_TYPE, IllegalArgumentException.class, "unknown type"), + NON_EMPTY_MAP_AS_EMPTY( + "empty", ImmutableMap.of("a", 1L), IllegalArgumentException.class, "expected an empty map"), + SCALAR_OF_ANOTHER_TYPE( + "single_int64", "a", IllegalArgumentException.class, "unexpected value type"); + + private final String fieldName; + private final Object value; + private final Class expectedException; + private final String expectedMessage; + + InvalidFieldTestCase( + String fieldName, + Object value, + Class expectedException, + String expectedMessage) { + this.fieldName = fieldName; + this.value = value; + this.expectedException = expectedException; + this.expectedMessage = expectedMessage; + } + } + + @Test + public void newValue_invalidField_throws(@TestParameter InvalidFieldTestCase testCase) { + ImmutableMap fields = ImmutableMap.of(testCase.fieldName, testCase.value); + + Exception e = + assertThrows( + testCase.expectedException, () -> VALUE_PROVIDER.newValue(TEST_ALL_TYPES, fields)); + assertThat(e).hasMessageThat().contains(testCase.expectedMessage); + } + + @Test + public void newValue_missingRequiredField_throws() { + assertThrows( + UninitializedMessageException.class, + () -> + VALUE_PROVIDER.newValue("cel.expr.conformance.proto2.TestRequired", ImmutableMap.of())); + } + + @Test + public void newValue_outOfRangeDurationLiteral_throws() { + ImmutableMap fields = ImmutableMap.of("seconds", 315576000001L); + + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, + () -> VALUE_PROVIDER.newValue("google.protobuf.Duration", fields)); + assertThat(e).hasMessageThat().contains("is not valid"); } @Test public void getProtoLiteCelValueConverter() { assertThat(VALUE_PROVIDER.protoCelValueConverter()).isNotNull(); } + + private static TestAllTypes newTestAllTypes(Map fields) { + Object value = VALUE_PROVIDER.newValue(TEST_ALL_TYPES, fields).get(); + return (TestAllTypes) ((ProtoMessageLiteValue) value).value(); + } } diff --git a/common/src/test/java/dev/cel/common/values/ProtoMessageValueProviderTest.java b/common/src/test/java/dev/cel/common/values/ProtoMessageValueProviderTest.java index 106a6508a..00eca2635 100644 --- a/common/src/test/java/dev/cel/common/values/ProtoMessageValueProviderTest.java +++ b/common/src/test/java/dev/cel/common/values/ProtoMessageValueProviderTest.java @@ -20,6 +20,8 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.primitives.UnsignedLong; +import com.google.protobuf.UInt64Value; +import com.google.testing.junit.testparameterinjector.TestParameter; import com.google.testing.junit.testparameterinjector.TestParameterInjector; import com.google.testing.junit.testparameterinjector.TestParameters; import dev.cel.common.CelDescriptorUtil; @@ -113,8 +115,7 @@ public void newValue_createProtoMessage_unsignedLongFieldsPopulated() { VALUE_PROVIDER .newValue( TestAllTypes.getDescriptor().getFullName(), - ImmutableMap.of( - "single_uint32", 3, "single_uint64", UnsignedLong.MAX_VALUE.longValue())) + ImmutableMap.of("single_uint32", 3, "single_uint64", UnsignedLong.MAX_VALUE)) .get(); assertThat(protoMessageValue.isZeroValue()).isFalse(); @@ -194,6 +195,38 @@ public void newValue_invalidField_throws() { + " 'cel.expr.conformance.proto2.TestAllTypes'"); } + @SuppressWarnings("ImmutableEnumChecker") // Test only + private enum MismatchedNumericTypeTestCase { + INT_TO_UINT32("single_uint32", 1L), + INT_TO_FLOAT("single_float", 1L), + INT_TO_MAP_DOUBLE_VALUE("map_bool_double", ImmutableMap.of(true, 1L)), + UINT_TO_ENUM("standalone_enum", UnsignedLong.valueOf(1L)), + UINT_TO_MAP_INT32_KEY("map_int32_bool", ImmutableMap.of(UnsignedLong.valueOf(1L), true)), + UINT_WRAPPER_TO_INT64("single_int64", UInt64Value.of(1L)), + DOUBLE_TO_REPEATED_UINT64("repeated_uint64", ImmutableList.of(1.0d)); + + private final String fieldName; + private final Object value; + + MismatchedNumericTypeTestCase(String fieldName, Object value) { + this.fieldName = fieldName; + this.value = value; + } + } + + @Test + public void newValue_mismatchedNumericType_throws( + @TestParameter MismatchedNumericTypeTestCase testCase) { + String typeName = TestAllTypes.getDescriptor().getFullName(); + ImmutableMap fields = ImmutableMap.of(testCase.fieldName, testCase.value); + + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> VALUE_PROVIDER.newValue(typeName, fields)); + + assertThat(e).hasMessageThat().contains("unexpected value type for field"); + } + @Test public void newValue_onCombinedProvider() { CelValueProvider celValueProvider = (structType, fields) -> Optional.empty(); diff --git a/common/src/test/java/dev/cel/common/values/RawProtoMessageLiteValueTest.java b/common/src/test/java/dev/cel/common/values/RawProtoMessageLiteValueTest.java index f47eef35f..d9d30e34b 100644 --- a/common/src/test/java/dev/cel/common/values/RawProtoMessageLiteValueTest.java +++ b/common/src/test/java/dev/cel/common/values/RawProtoMessageLiteValueTest.java @@ -21,6 +21,7 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.primitives.UnsignedLong; +import com.google.common.testing.EqualsTester; import com.google.protobuf.ByteString; import com.google.protobuf.CodedOutputStream; import com.google.protobuf.WireFormat; @@ -101,31 +102,47 @@ public void create_emptyTypeName_normalizesToUnknownMessageTypeName() { } @Test - @SuppressWarnings("SelfEquals") // Testing that equals throws even on self-comparison - public void equals_throwsUnsupportedOperationException() { - RawProtoMessageLiteValue value1 = - RawProtoMessageLiteValue.create(ByteString.EMPTY, "custom.Message", EMPTY_CONVERTER); - RawProtoMessageLiteValue value2 = - RawProtoMessageLiteValue.create(ByteString.EMPTY, "custom.Message", EMPTY_CONVERTER); - - UnsupportedOperationException selfThrown = - assertThrows(UnsupportedOperationException.class, () -> value1.equals(value1)); - UnsupportedOperationException otherThrown = - assertThrows(UnsupportedOperationException.class, () -> value1.equals(value2)); - - assertThat(selfThrown).hasMessageThat().isEqualTo("Message equality is not supported"); - assertThat(otherThrown).hasMessageThat().isEqualTo("Message equality is not supported"); + public void equalsAndHashCode() throws Exception { + ByteString order1 = + encode(out -> { + out.writeInt64(1, 10L); + out.writeString(2, "a"); + }); + ByteString order2 = + encode(out -> { + out.writeString(2, "a"); + out.writeInt64(1, 10L); + }); + ByteString differentField = encode(out -> out.writeInt64(1, 99L)); + + new EqualsTester() + .addEqualityGroup( + RawProtoMessageLiteValue.create(order1, "custom.Message", EMPTY_CONVERTER), + RawProtoMessageLiteValue.create(order2, "custom.Message", EMPTY_CONVERTER), + RawProtoMessageLiteValue.create( + order1, "custom.Message", EMPTY_CONVERTER, /* isView= */ true)) + .addEqualityGroup( + RawProtoMessageLiteValue.create(differentField, "custom.Message", EMPTY_CONVERTER)) + .addEqualityGroup(RawProtoMessageLiteValue.create(order1, "other.Message", EMPTY_CONVERTER)) + .addEqualityGroup( + RawProtoMessageLiteValue.create(ByteString.EMPTY, EMPTY_CONVERTER), + RawProtoMessageLiteValue.create(ByteString.EMPTY, "", EMPTY_CONVERTER), + RawProtoMessageLiteValue.create( + ByteString.EMPTY, "cel.@unknownMessage", EMPTY_CONVERTER)) + .testEquals(); } @Test - public void hashCode_throwsUnsupportedOperationException() { - RawProtoMessageLiteValue value = - RawProtoMessageLiteValue.create(ByteString.EMPTY, "custom.Message", EMPTY_CONVERTER); - - UnsupportedOperationException thrown = - assertThrows(UnsupportedOperationException.class, value::hashCode); + public void equals_malformedBytes_throwsIllegalArgumentException() { + RawProtoMessageLiteValue malformed1 = + RawProtoMessageLiteValue.create( + ByteString.copyFromUtf8("test"), "custom.Message", EMPTY_CONVERTER); + RawProtoMessageLiteValue malformed2 = + RawProtoMessageLiteValue.create( + ByteString.copyFromUtf8("test"), "custom.Message", EMPTY_CONVERTER); - assertThat(thrown).hasMessageThat().isEqualTo("Message equality is not supported"); + assertThrows(IllegalArgumentException.class, () -> malformed1.equals(malformed2)); + assertThrows(IllegalArgumentException.class, malformed1::hashCode); } @Test diff --git a/conformance/src/test/java/dev/cel/conformance/BUILD.bazel b/conformance/src/test/java/dev/cel/conformance/BUILD.bazel index 1860b19cf..d51fc1c9e 100644 --- a/conformance/src/test/java/dev/cel/conformance/BUILD.bazel +++ b/conformance/src/test/java/dev/cel/conformance/BUILD.bazel @@ -199,78 +199,11 @@ _TESTS_TO_SKIP_PLANNER = [ "enums/strong_proto3", ] -_TESTS_TO_SKIP_PLANNER_SELECT_OPT = _TESTS_TO_SKIP_PLANNER + [ - # TODO: Optional field selection on proto messages is not yet supported by - # SelectOptimizer. - "optionals", -] - _TESTS_TO_SKIP_LITE_BASE = _TESTS_TO_SKIP_PLANNER + [ # Proto2 extensions are not supported in CelLiteRuntime. "proto2_ext", "proto2/extensions_has", "proto2/extensions_get", - - # TODO: Proto equality after dyn() unwrapping is not yet supported in - # CelLiteRuntime. - "comparisons/eq_literal/not_eq_dyn_proto2_msg_null,not_eq_dyn_proto3_msg_null", - "comparisons/eq_wrapper/eq_proto_different_types", - "comparisons/ne_literal/ne_proto_different_types", - - # TODO: Message creation with prepopulated fields is not yet supported in - # CelLiteRuntime. - "block_ext/basic/optional_message", - "comparisons/eq_wrapper/eq_bool,eq_bytes,eq_double,eq_float,eq_int32,eq_int64,eq_string,eq_uint32,eq_uint64", - "comparisons/eq_wrapper/eq_proto2,eq_proto3,eq_proto2_missing_fields_neq,eq_proto3_missing_fields_neq,eq_proto_nan_equal", - "comparisons/eq_wrapper/eq_proto2_any_unpack_equal,eq_proto2_any_unpack_not_equal,eq_proto2_any_unpack_bytewise_fallback_not_equal,eq_proto2_any_unpack_bytewise_fallback_equal,eq_proto3_any_unpack_equal,eq_proto3_any_unpack_not_equal,eq_proto3_any_unpack_bytewise_fallback_not_equal,eq_proto3_any_unpack_bytewise_fallback_equal", - "comparisons/ne_literal/ne_proto2,ne_proto3,ne_proto2_missing_fields_neq,ne_proto3_missing_fields_neq,ne_proto_nan_not_equal", - "comparisons/ne_literal/ne_proto2_any_unpack,ne_proto2_any_unpack_bytewise_fallback,ne_proto3_any_unpack,ne_proto3_any_unpack_bytewise_fallback", - "dynamic/int32/literal,literal_no_field_access,field_assign_proto2,field_assign_proto2_zero,field_assign_proto2_max,field_assign_proto2_min,field_assign_proto2_range,field_read_proto2,field_read_proto2_zero,field_assign_proto3,field_assign_proto3_zero,field_assign_proto3_max,field_assign_proto3_min,field_assign_proto3_range,field_read_proto3,field_read_proto3_zero", - "dynamic/int64/literal,literal_no_field_access,field_assign_proto2,field_assign_proto2_zero,field_assign_proto3,field_assign_proto3_zero", - "dynamic/uint32/literal,literal_no_field_access,field_assign_proto2,field_assign_proto2_zero,field_assign_proto2_max,field_assign_proto2_range,field_assign_proto3,field_assign_proto3_zero,field_assign_proto3_max,field_assign_proto3_range,field_read_proto2,field_read_proto2_zero", - "dynamic/uint64/literal,literal_no_field_access,field_assign_proto2,field_assign_proto2_zero,field_assign_proto3,field_assign_proto3_zero,field_read_proto2,field_read_proto2_zero", - "dynamic/float/literal,literal_not_double,literal_no_field_access,field_assign_proto2,field_assign_proto2_zero,field_assign_proto2_subnorm,field_assign_proto2_round_to_zero,field_assign_proto2_range,field_read_proto2,field_read_proto2_zero,field_assign_proto3,field_assign_proto3_zero,field_assign_proto3_subnorm,field_assign_proto3_round_to_zero,field_assign_proto3_range,field_read_proto3,field_read_proto3_zero", - "dynamic/double/literal,literal_no_field_access,field_assign_proto2,field_assign_proto2_zero,field_assign_proto2_range,field_read_proto2,field_read_proto2_zero,field_assign_proto3,field_assign_proto3_zero,field_assign_proto3_range,field_read_proto3,field_read_proto3_zero", - "dynamic/bool/literal,literal_no_field_access,field_assign_proto2,field_assign_proto2_false,field_assign_proto3,field_assign_proto3_false", - "dynamic/string/literal,literal_no_field_access,literal_unicode,field_assign_proto2,field_assign_proto2_empty,field_assign_proto3,field_assign_proto3_empty", - "dynamic/bytes/literal,literal_no_field_access,literal_unicode,field_assign_proto2,field_assign_proto2_empty,field_assign_proto3,field_assign_proto3_empty", - "dynamic/list/literal,literal_no_field_access,literal_empty,field_assign_proto2,field_assign_proto2_empty,field_read_proto2,field_read_proto2_empty,field_assign_proto3,field_assign_proto3_empty,field_read_proto3,field_read_proto3_empty", - "dynamic/struct/literal,literal_no_field_access,literal_empty,field_assign_proto2,field_assign_proto2_empty,field_assign_proto2_bad,field_read_proto2,field_read_proto2_empty,field_assign_proto3,field_assign_proto3_empty,field_assign_proto3_bad,field_read_proto3,field_read_proto3_empty", - "dynamic/value_null/literal,literal_no_field_access,field_assign_proto2,field_read_proto2,field_assign_proto3,field_read_proto3", - "dynamic/value_number/literal,literal_no_field_access,literal_zero,field_assign_proto2,field_assign_proto2_zero,field_read_proto2,field_read_proto2_zero,field_assign_proto3,field_assign_proto3_zero,field_read_proto3,field_read_proto3_zero", - "dynamic/value_string/literal,literal_no_field_access,literal_empty,field_assign_proto2,field_assign_proto2_empty,field_read_proto2,field_read_proto2_zero,field_assign_proto3,field_assign_proto3_empty,field_read_proto3,field_read_proto3_zero", - "dynamic/value_bool/literal,literal_no_field_access,literal_false,field_assign_proto2,field_assign_proto2_false,field_read_proto2,field_read_proto2_false,field_assign_proto3,field_assign_proto3_false,field_read_proto3,field_read_proto3_false", - "dynamic/value_struct/literal,literal_no_field_access,literal_empty,field_assign_proto2,field_assign_proto2_empty,field_read_proto2,field_read_proto2_empty,field_assign_proto3,field_assign_proto3_empty,field_read_proto3,field_read_proto3_empty", - "dynamic/value_list/literal,literal_no_field_access,literal_empty,field_assign_proto2,field_assign_proto2_empty,field_read_proto2,field_read_proto2_empty,field_assign_proto3,field_assign_proto3_empty,field_read_proto3,field_read_proto3_empty", - "dynamic/any/literal,literal_no_field_access,field_assign_proto2,field_read_proto2,field_assign_proto3,field_read_proto3", - "dynamic/complex", - "enums/legacy_proto2/assign_standalone_name,assign_standalone_int,assign_standalone_int_too_big,assign_standalone_int_too_neg,assign_repeated_enum,list_enum_as_list_int,enum_as_int", - "enums/legacy_proto3/assign_standalone_name,assign_standalone_int,assign_standalone_int_big,assign_standalone_int_neg,assign_standalone_int_too_big,assign_standalone_int_too_neg,assign_repeated_enum,list_enum_as_list_int,enum_as_int", - "optionals/optionals/struct_map_optindex_field,struct_map_optindex_field_nested,struct_list_optindex_field,struct_optindex_value,optional_struct_optindex_value,optional_struct_optindex_index_value", - "parse/nest/message_literal", - "parse/repeat/message_literal", - "parse/whitespace", - "parse/comments", - "parse/struct_field_names", - "proto2/literal_singular", - "proto2/literal_wellknown", - "proto2/set_null", - "proto2/quoted_fields", - "proto2/has/repeated_none_explicit,repeated_one,repeated_many,map_none_explicit,map_one_default,map_one,map_many,required,optional_set_no_default,optional_set_with_default,optional_set_to_default,optional_message_set,optional_enum_set,optional_enum_set_zero,oneof_other_set,oneof_set,oneof_set_default", - "proto3/literal_singular", - "proto3/literal_wellknown", - "proto3/set_null", - "proto3/quoted_fields", - "proto3/has/repeated_none_explicit,repeated_one,repeated_many,map_none_explicit,map_one_default,map_one,map_many,single_set,single_set_to_default,single_message_set,single_message_set_to_default,single_enum_set,single_enum_set_zero,oneof_other_set,oneof_set,oneof_set_default", - "type_deductions/complex_initializers/struct", - "type_deductions/field_access/int_field", - "wrappers", -] - -_TESTS_TO_SKIP_LITE_SELECT_OPT = _TESTS_TO_SKIP_LITE_BASE + [ - # TODO: Optional field selection on proto messages is not yet supported by - # SelectOptimizer. - "optionals", ] _TESTS_TO_SKIP_LITE = _TESTS_TO_SKIP_LITE_BASE + [ @@ -309,7 +242,7 @@ conformance_test( data = _ALL_TESTS, enable_select_optimization = True, runtime = RUNTIME.PLANNER, - skip_tests = _TESTS_TO_SKIP_PLANNER_SELECT_OPT, + skip_tests = _TESTS_TO_SKIP_PLANNER, ) conformance_test( @@ -324,5 +257,5 @@ conformance_test( data = _ALL_TESTS, enable_select_optimization = True, runtime = RUNTIME.LITE, - skip_tests = _TESTS_TO_SKIP_LITE_SELECT_OPT, + skip_tests = _TESTS_TO_SKIP_LITE_BASE, ) diff --git a/conformance/src/test/java/dev/cel/conformance/ConformanceTest.java b/conformance/src/test/java/dev/cel/conformance/ConformanceTest.java index 0ff0d63a7..646a014e1 100644 --- a/conformance/src/test/java/dev/cel/conformance/ConformanceTest.java +++ b/conformance/src/test/java/dev/cel/conformance/ConformanceTest.java @@ -27,6 +27,7 @@ import dev.cel.expr.Value; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import com.google.protobuf.Descriptors.FileDescriptor; import com.google.protobuf.ExtensionRegistry; import com.google.protobuf.TypeRegistry; @@ -132,11 +133,13 @@ public final class ConformanceTest extends Statement { private static final CelValueProvider LITE_VALUE_PROVIDER = ProtoMessageLiteValueProvider.newInstance( - dev.cel.expr.conformance.proto2.TestAllTypesCelDescriptor.getDescriptor(), - dev.cel.expr.conformance.proto2.NestedTestAllTypesCelDescriptor.getDescriptor(), - dev.cel.expr.conformance.proto2.TestRequiredCelDescriptor.getDescriptor(), - dev.cel.expr.conformance.proto3.TestAllTypesCelDescriptor.getDescriptor(), - dev.cel.expr.conformance.proto3.NestedTestAllTypesCelDescriptor.getDescriptor()); + OPTIONS, + ImmutableSet.of( + dev.cel.expr.conformance.proto2.TestAllTypesCelDescriptor.getDescriptor(), + dev.cel.expr.conformance.proto2.NestedTestAllTypesCelDescriptor.getDescriptor(), + dev.cel.expr.conformance.proto2.TestRequiredCelDescriptor.getDescriptor(), + dev.cel.expr.conformance.proto3.TestAllTypesCelDescriptor.getDescriptor(), + dev.cel.expr.conformance.proto3.NestedTestAllTypesCelDescriptor.getDescriptor())); private static final ImmutableList CONFORMANCE_FILE_DESCRIPTORS = ImmutableList.of( diff --git a/extensions/src/test/java/dev/cel/extensions/CelOptionalLibraryTest.java b/extensions/src/test/java/dev/cel/extensions/CelOptionalLibraryTest.java index a08d861f2..daf5090de 100644 --- a/extensions/src/test/java/dev/cel/extensions/CelOptionalLibraryTest.java +++ b/extensions/src/test/java/dev/cel/extensions/CelOptionalLibraryTest.java @@ -758,9 +758,6 @@ public void optionalFieldSelection_onProtoMessage_returnsOptionalValue() throws @Test public void optionalFieldSelection_onProtoMessage_listValue() throws Exception { - // TODO: Message creation with prepopulated fields is not yet supported in - // CelLiteRuntime. - assumeFalse(testMode.isLite()); TestCel cel = newCelBuilder().build(); CelAbstractSyntaxTree ast = compile( @@ -773,9 +770,6 @@ public void optionalFieldSelection_onProtoMessage_listValue() throws Exception { @Test public void optionalFieldSelection_onProtoMessage_indexValue() throws Exception { - // TODO: Message creation with prepopulated fields is not yet supported in - // CelLiteRuntime. - assumeFalse(testMode.isLite()); TestCel cel = newCelBuilder().build(); CelAbstractSyntaxTree ast = compile( @@ -1450,9 +1444,6 @@ public void optionalMessageCreation_fieldValueIsEmpty_returnsEmptyMessage() thro @Test public void optionalMessageCreation_fieldValueIsPresent_returnsMessage() throws Exception { - // TODO: Message creation with prepopulated fields is not yet supported in - // CelLiteRuntime. - assumeFalse(testMode.isLite()); TestCel cel = newCelBuilder().build(); CelAbstractSyntaxTree ast = compile(cel, "TestAllTypes{?single_double_wrapper: optional.ofNonZeroValue(5.0)}"); @@ -1490,9 +1481,6 @@ public void optionalMessageCreation_fieldValueContainsEmptyMap_returnsEmptyMessa @Test public void optionalMessageCreation_fieldValueContainsMap_returnsEmptyMessage() throws Exception { - // TODO: Message creation with prepopulated fields is not yet supported in - // CelLiteRuntime. - assumeFalse(testMode.isLite()); TestCel cel = newCelBuilder() .addVar( @@ -1614,9 +1602,6 @@ public void optionalListCreation_containsMixedTypeElements_success() throws Exce @Test public void optionalListCreation_withinProtoMessage_success() throws Exception { - // TODO: Message creation with prepopulated fields is not yet supported in - // CelLiteRuntime. - assumeFalse(testMode.isLite()); TestCel cel = newCelBuilder() .addVar("m", MapType.create(SimpleType.STRING, SimpleType.DYN)) diff --git a/protobuf/src/main/java/dev/cel/protobuf/CelLiteDescriptor.java b/protobuf/src/main/java/dev/cel/protobuf/CelLiteDescriptor.java index a99845bfd..d05b92a91 100644 --- a/protobuf/src/main/java/dev/cel/protobuf/CelLiteDescriptor.java +++ b/protobuf/src/main/java/dev/cel/protobuf/CelLiteDescriptor.java @@ -406,7 +406,7 @@ public FieldLiteDescriptor( } /** Describes map entry type {@code fullName}, whose key and value are fields 1 and 2. */ - protected static MessageLiteDescriptor mapEntry( + public static MessageLiteDescriptor mapEntry( String fullName, FieldLiteDescriptor.Type keyType, FieldLiteDescriptor.Type valueType, diff --git a/runtime/src/main/java/dev/cel/runtime/BUILD.bazel b/runtime/src/main/java/dev/cel/runtime/BUILD.bazel index 28c8d0a10..8911f9e88 100644 --- a/runtime/src/main/java/dev/cel/runtime/BUILD.bazel +++ b/runtime/src/main/java/dev/cel/runtime/BUILD.bazel @@ -375,6 +375,7 @@ java_library( "//common/annotations", "//common/exceptions:attribute_not_found", "//common/internal:comparison_functions", + "//common/values", "@maven//:com_google_errorprone_error_prone_annotations", "@maven//:com_google_guava_guava", "@maven//:com_google_protobuf_protobuf_java", @@ -392,6 +393,7 @@ cel_android_library( "//common/annotations", "//common/exceptions:attribute_not_found", "//common/internal:comparison_functions_android", + "//common/values:values_android", "@maven//:com_google_errorprone_error_prone_annotations", "@maven_android//:com_google_guava_guava", "@maven_android//:com_google_protobuf_protobuf_javalite", diff --git a/runtime/src/main/java/dev/cel/runtime/CelLiteRuntimeImpl.java b/runtime/src/main/java/dev/cel/runtime/CelLiteRuntimeImpl.java index 00eb6dcfd..7f7403271 100644 --- a/runtime/src/main/java/dev/cel/runtime/CelLiteRuntimeImpl.java +++ b/runtime/src/main/java/dev/cel/runtime/CelLiteRuntimeImpl.java @@ -188,7 +188,11 @@ public CelLiteRuntime build() { ImmutableList userFunctionBindings = ImmutableList.copyOf(customFunctionBindings.values()); RuntimeHelpers runtimeHelpers = RuntimeHelpers.create(); - RuntimeEquality runtimeEquality = RuntimeEquality.create(runtimeHelpers, celOptions); + RuntimeEquality runtimeEquality = + RuntimeEquality.create( + runtimeHelpers, + celOptions.toBuilder().enableProtoDifferencerEquality(true).build(), + celValueProvider.celValueConverter()); ImmutableSet runtimeLibs = runtimeLibrariesBuilder.build(); for (CelLiteRuntimeLibrary lib : runtimeLibs) { if (lib instanceof CelInternalLiteRuntimeLibrary) { diff --git a/runtime/src/main/java/dev/cel/runtime/RuntimeEquality.java b/runtime/src/main/java/dev/cel/runtime/RuntimeEquality.java index bc39be47f..81f828fb1 100644 --- a/runtime/src/main/java/dev/cel/runtime/RuntimeEquality.java +++ b/runtime/src/main/java/dev/cel/runtime/RuntimeEquality.java @@ -14,6 +14,8 @@ package dev.cel.runtime; +import static com.google.common.base.Preconditions.checkNotNull; + import com.google.common.primitives.UnsignedLong; import com.google.errorprone.annotations.Immutable; import com.google.protobuf.MessageLiteOrBuilder; @@ -21,6 +23,7 @@ import dev.cel.common.annotations.Internal; import dev.cel.common.exceptions.CelAttributeNotFoundException; import dev.cel.common.internal.ComparisonFunctions; +import dev.cel.common.values.CelValueConverter; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -34,11 +37,18 @@ public class RuntimeEquality { protected final RuntimeHelpers runtimeHelpers; protected final CelOptions celOptions; + private final CelValueConverter celValueConverter; public static RuntimeEquality create(RuntimeHelpers runtimeHelper, CelOptions celOptions) { return new RuntimeEquality(runtimeHelper, celOptions); } + /** Creates an equality that compares protobuf messages as {@code celValueConverter} does. */ + static RuntimeEquality create( + RuntimeHelpers runtimeHelper, CelOptions celOptions, CelValueConverter celValueConverter) { + return new RuntimeEquality(runtimeHelper, celOptions, celValueConverter); + } + // Functions // ========= @@ -171,9 +181,9 @@ public boolean objectEquals(Object x, Object y) { } boolean protoEquals(MessageLiteOrBuilder x, MessageLiteOrBuilder y) { - // TODO: Implement when CelLiteDescriptor is available - throw new UnsupportedOperationException( - "Proto Differencer equality is not supported for MessageLite."); + Optional result = celValueConverter.messageEquals(x, y); + // Only messages of one class are equal, which hashCode relies on. + return result.isPresent() ? result.get() : x.getClass() == y.getClass() && x.equals(y); } private boolean optionalEquals(Optional x, Optional y) { @@ -290,6 +300,10 @@ public int hashCode(Object object) { } return h; } + if (celOptions.enableProtoDifferencerEquality() && object instanceof MessageLiteOrBuilder) { + // Constant per class: protoEquals holds for messages that hash differently, as with -0.0/0.0. + return object.getClass().hashCode(); + } return Objects.hashCode(object); } @@ -325,7 +339,13 @@ private static boolean areRandomAccessLists(Iterable x, Iterable y) { } RuntimeEquality(RuntimeHelpers runtimeHelpers, CelOptions celOptions) { - this.runtimeHelpers = runtimeHelpers; - this.celOptions = celOptions; + this(runtimeHelpers, celOptions, CelValueConverter.getDefaultInstance()); + } + + private RuntimeEquality( + RuntimeHelpers runtimeHelpers, CelOptions celOptions, CelValueConverter celValueConverter) { + this.runtimeHelpers = checkNotNull(runtimeHelpers); + this.celOptions = checkNotNull(celOptions); + this.celValueConverter = checkNotNull(celValueConverter); } } diff --git a/runtime/src/main/java/dev/cel/runtime/RuntimeHelpers.java b/runtime/src/main/java/dev/cel/runtime/RuntimeHelpers.java index 7e4a95ddd..23419b49c 100644 --- a/runtime/src/main/java/dev/cel/runtime/RuntimeHelpers.java +++ b/runtime/src/main/java/dev/cel/runtime/RuntimeHelpers.java @@ -363,17 +363,10 @@ static Object maybeAdaptPrimitive(Object value) { } /** - * Adapts a {@code protobuf.Message} to a plain old Java object. - * - *

Well-known protobuf types (wrappers, JSON types) are unwrapped to Java native object - * representations. - * - *

If the incoming {@code obj} is of type {@code google.protobuf.Any} the object is unpacked - * and the proto within is passed to the {@code adaptProtoToValue} method again to ensure the - * message contained within the Any is properly unwrapped if it is a well-known protobuf type. + * Adapts a protobuf message to a CEL value: as is, unless a subclass unwraps well-known types. */ Object adaptProtoToValue(MessageLiteOrBuilder obj) { - throw new UnsupportedOperationException("Not implemented yet"); + return obj; } public static Optional doubleToUnsignedChecked(double v) { diff --git a/runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java b/runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java index 4a4175778..64ccaab56 100644 --- a/runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java +++ b/runtime/src/main/java/dev/cel/runtime/planner/ProgramPlanner.java @@ -52,6 +52,7 @@ import dev.cel.common.types.CelType; import dev.cel.common.types.CelTypeProvider; import dev.cel.common.types.SimpleType; +import dev.cel.common.types.StructTypeReference; import dev.cel.common.types.TypeType; import dev.cel.common.values.CelValueConverter; import dev.cel.common.values.CelValueProvider; @@ -662,13 +663,10 @@ private ResolvedFunction resolveFunction( } private CelType resolveStructType(CelExpr expr, PlannerContext ctx) { - CelType checkedType = ctx.typeMap().get(expr.id()); - if (checkedType != null) { - CelKind kind = checkedType.kind(); - // Type-checked ASTs do not need a type-provider lookup as long as it's of expected kind. - if (isValidStructKind(kind)) { - return checkedType; - } + // Checked ASTs reference the resolved message name, including for well-known types. + CelReference reference = ctx.referenceMap().get(expr.id()); + if (reference != null) { + return StructTypeReference.create(reference.name()); } CelStruct struct = expr.struct(); diff --git a/runtime/src/test/java/dev/cel/runtime/BUILD.bazel b/runtime/src/test/java/dev/cel/runtime/BUILD.bazel index fb2278015..166ae68e3 100644 --- a/runtime/src/test/java/dev/cel/runtime/BUILD.bazel +++ b/runtime/src/test/java/dev/cel/runtime/BUILD.bazel @@ -68,8 +68,10 @@ java_library( "//compiler", "//compiler:compiler_builder", "//extensions", + "//extensions:lists_runtime_library", "//extensions:optional_library", "//extensions:optional_runtime_library", + "//extensions:sets_runtime_library", "//optimizer", "//optimizer:optimizer_builder", "//optimizer/optimizers:common_subexpression_elimination", diff --git a/runtime/src/test/java/dev/cel/runtime/CelLiteInterpreterTest.java b/runtime/src/test/java/dev/cel/runtime/CelLiteInterpreterTest.java index bc0fde200..6a4167790 100644 --- a/runtime/src/test/java/dev/cel/runtime/CelLiteInterpreterTest.java +++ b/runtime/src/test/java/dev/cel/runtime/CelLiteInterpreterTest.java @@ -17,6 +17,7 @@ import com.google.testing.junit.testparameterinjector.TestParameterInjector; import dev.cel.common.CelOptions; import dev.cel.common.values.ProtoMessageLiteValueProvider; +import dev.cel.expr.conformance.proto3.NestedTestAllTypesCelDescriptor; import dev.cel.expr.conformance.proto3.TestAllTypesCelDescriptor; import dev.cel.extensions.CelOptionalLibrary; import dev.cel.testing.BaseInterpreterTest; @@ -35,7 +36,8 @@ protected CelRuntimeBuilder newBaseRuntimeBuilder(CelOptions celOptions) { .setValueProvider( ProtoMessageLiteValueProvider.newInstance( dev.cel.expr.conformance.proto2.TestAllTypesCelDescriptor.getDescriptor(), - TestAllTypesCelDescriptor.getDescriptor())) + TestAllTypesCelDescriptor.getDescriptor(), + NestedTestAllTypesCelDescriptor.getDescriptor())) .addLibraries(CelOptionalLibrary.INSTANCE) .addFileTypes(TEST_FILE_DESCRIPTORS) .addLateBoundFunctions("record") @@ -54,58 +56,6 @@ public void dynamicMessage_dynamicDescriptor() throws Exception { skipBaselineVerification(); } - // All the tests below rely on message creation with fields populated. They are excluded for time - // being until this support is added. - @Override - public void nullAssignability() throws Exception { - skipBaselineVerification(); - } - - @Override - public void wrappers() throws Exception { - skipBaselineVerification(); - } - - @Override - public void jsonConversions() { - skipBaselineVerification(); - } - - @Override - public void nestedEnums() { - skipBaselineVerification(); - } - - @Override - public void messages() throws Exception { - skipBaselineVerification(); - } - - @Override - public void packUnpackAny() { - skipBaselineVerification(); - } - - @Override - public void lists() throws Exception { - skipBaselineVerification(); - } - - @Override - public void maps() throws Exception { - skipBaselineVerification(); - } - - @Override - public void jsonValueTypes() { - skipBaselineVerification(); - } - - @Override - public void messages_error() { - skipBaselineVerification(); - } - @Override public void jsonFieldNames() { // json_name field option is not yet supported in lite runtime diff --git a/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeAndroidTest.java b/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeAndroidTest.java index f1a9f4908..25661a1d2 100644 --- a/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeAndroidTest.java +++ b/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeAndroidTest.java @@ -25,13 +25,17 @@ import com.google.protobuf.BoolValue; import com.google.protobuf.ByteString; import com.google.protobuf.BytesValue; +import com.google.protobuf.CodedOutputStream; import com.google.protobuf.DoubleValue; +import com.google.protobuf.ExtensionRegistryLite; import com.google.protobuf.FloatValue; import com.google.protobuf.Int32Value; import com.google.protobuf.Int64Value; import com.google.protobuf.StringValue; +import com.google.protobuf.Struct; import com.google.protobuf.UInt32Value; import com.google.protobuf.UInt64Value; +import com.google.protobuf.Value; import com.google.testing.junit.testparameterinjector.TestParameter; import com.google.testing.junit.testparameterinjector.TestParameterInjector; import com.google.testing.junit.testparameterinjector.TestParameters; @@ -56,6 +60,8 @@ import dev.cel.runtime.standard.EqualsOperator; import dev.cel.runtime.standard.IntFunction; import dev.cel.runtime.standard.IntFunction.IntOverload; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.time.Duration; import java.time.Instant; import java.util.List; @@ -875,6 +881,146 @@ public void eval_optionalExtension() throws Exception { assertThat(runtime.createProgram(ast).eval()).isEqualTo(1L); } + @Test + public void eval_messageCreation_returnsMessageLite() throws Exception { + CelLiteRuntime runtime = + CelLiteRuntimeFactory.newLiteRuntimeBuilder() + .setStandardFunctions(CelStandardFunctions.ALL_STANDARD_FUNCTIONS) + .setValueProvider( + ProtoMessageLiteValueProvider.newInstance( + TestAllTypesCelLiteDescriptor.getDescriptor())) + .build(); + // Expr: cel.expr.conformance.proto3.TestAllTypes{single_int32: -1, ..., single_value: ...} + CelAbstractSyntaxTree ast = readCheckedExpr("compiled_proto3_message_creation"); + + TestAllTypes result = (TestAllTypes) runtime.createProgram(ast).eval(); + + assertThat(result) + .isEqualTo( + TestAllTypes.newBuilder() + .setSingleInt32(-1) + .setSingleSint64(-2L) + .setSingleFixed32(-1) + .setSingleFloat(1.5f) + .setSingleString("foo") + .setSingleBytes(ByteString.copyFromUtf8("abc")) + .setStandaloneEnumValue(2) + .setOptionalBool(false) + .setSingleNestedMessage(TestAllTypes.NestedMessage.newBuilder().setBb(7).build()) + .addRepeatedInt64(1L) + .addRepeatedInt64(2L) + .putMapStringString("k", "v") + .setSingleInt64Wrapper(Int64Value.of(5)) + .setSingleUint32Wrapper(UInt32Value.of(-1)) + .setSingleDuration(ProtoTimeUtils.toProtoDuration(Duration.ofSeconds(1))) + .setSingleStruct( + Struct.newBuilder() + .putFields("a", Value.newBuilder().setNumberValue(1.0).build())) + .setSingleValue(Value.newBuilder().setStringValue("x").build()) + .build()); + } + + @Test + public void eval_messageEquality_reorderedUnknownFields_returnsTrue() throws Exception { + TestAllTypes left = + TestAllTypes.parseFrom( + encodeWire(out -> { + out.writeInt32(1, 42); + out.writeInt32(998, 1); + out.writeInt32(999, 2); + }), + ExtensionRegistryLite.getEmptyRegistry()); + TestAllTypes right = + TestAllTypes.parseFrom( + encodeWire(out -> { + out.writeInt32(1, 42); + out.writeInt32(999, 2); + out.writeInt32(998, 1); + }), + ExtensionRegistryLite.getEmptyRegistry()); + + // Protolite's UnknownFieldSetLite.equals is order-sensitive, whereas CEL equality is not. + assertThat(left).isNotEqualTo(right); + assertThat(evalProto3MessageEquality(left, right)).isEqualTo(true); + } + + @Test + public void eval_messageEquality_doubleValueWithReorderedUnknownFields_returnsTrue() + throws Exception { + DoubleValue leftWrapper = + DoubleValue.parseFrom( + encodeWire(out -> { + out.writeDouble(1, 1.5); + out.writeInt32(998, 1); + out.writeInt32(999, 2); + }), + ExtensionRegistryLite.getEmptyRegistry()); + DoubleValue rightWrapper = + DoubleValue.parseFrom( + encodeWire(out -> { + out.writeDouble(1, 1.5); + out.writeInt32(999, 2); + out.writeInt32(998, 1); + }), + ExtensionRegistryLite.getEmptyRegistry()); + TestAllTypes left = TestAllTypes.newBuilder().setSingleDoubleWrapper(leftWrapper).build(); + TestAllTypes right = TestAllTypes.newBuilder().setSingleDoubleWrapper(rightWrapper).build(); + + assertThat(left).isNotEqualTo(right); + assertThat(evalProto3MessageEquality(left, right)).isEqualTo(true); + } + + @Test + public void eval_messageEquality_nanField_returnsFalse() throws Exception { + TestAllTypes left = TestAllTypes.newBuilder().setSingleDouble(Double.NaN).build(); + TestAllTypes right = TestAllTypes.newBuilder().setSingleDouble(Double.NaN).build(); + + assertThat(left).isEqualTo(right); + assertThat(evalProto3MessageEquality(left, right)).isEqualTo(false); + } + + @Test + public void eval_messageEquality_differentFields_returnsFalse() throws Exception { + TestAllTypes left = TestAllTypes.newBuilder().setSingleInt32(1).build(); + TestAllTypes right = TestAllTypes.newBuilder().setSingleInt32(2).build(); + + assertThat(evalProto3MessageEquality(left, right)).isEqualTo(false); + } + + private static Object evalProto3MessageEquality(TestAllTypes left, TestAllTypes right) + throws Exception { + CelLiteRuntime runtime = + CelLiteRuntimeFactory.newLiteRuntimeBuilder() + .setStandardFunctions(EqualsOperator.create()) + .setValueProvider( + ProtoMessageLiteValueProvider.newInstance( + TestAllTypesCelLiteDescriptor.getDescriptor(), + NestedTestAllTypesCelLiteDescriptor.getDescriptor())) + .build(); + // Expr: proto3.oneof_type.payload == proto3.oneof_type.child.payload + CelAbstractSyntaxTree ast = readCheckedExpr("compiled_proto3_message_equality"); + TestAllTypes root = + TestAllTypes.newBuilder() + .setOneofType( + NestedTestAllTypes.newBuilder() + .setPayload(left) + .setChild(NestedTestAllTypes.newBuilder().setPayload(right))) + .build(); + return runtime.createProgram(ast).eval(ImmutableMap.of("proto3", root)); + } + + private interface WireEncoder { + void encode(CodedOutputStream out) throws IOException; + } + + private static ByteString encodeWire(WireEncoder encoder) throws IOException { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + CodedOutputStream out = CodedOutputStream.newInstance(bytes); + encoder.encode(out); + out.flush(); + return ByteString.copyFrom(bytes.toByteArray()); + } + private enum CelOptionsTestCase { UNSIGNED_LONG_DISABLED(newBaseTestOptions().enableUnsignedLongs(false).build()), UNWRAP_WKT_DISABLED(newBaseTestOptions().unwrapWellKnownTypesOnFunctionDispatch(false).build()), diff --git a/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeTest.java b/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeTest.java index 89aed454f..edb8c14a5 100644 --- a/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeTest.java +++ b/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeTest.java @@ -21,6 +21,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.primitives.UnsignedLong; +import com.google.protobuf.Any; import com.google.protobuf.BoolValue; import com.google.protobuf.ByteString; import com.google.protobuf.BytesValue; @@ -54,6 +55,9 @@ import dev.cel.expr.conformance.proto3.TestAllTypes.NestedEnum; import dev.cel.expr.conformance.proto3.TestAllTypes.NestedMessage; import dev.cel.expr.conformance.proto3.TestAllTypesCelDescriptor; +import dev.cel.extensions.CelExtensions; +import dev.cel.extensions.CelListsRuntimeLibrary; +import dev.cel.extensions.CelSetsRuntimeLibrary; import dev.cel.parser.CelStandardMacro; import dev.cel.runtime.standard.EqualsOperator; import dev.cel.runtime.standard.TypeFunction; @@ -117,14 +121,25 @@ public void messageCreation_emptyMessage() throws Exception { @Test public void messageCreation_fieldsPopulated() throws Exception { - CelAbstractSyntaxTree ast = CEL_COMPILER.compile("TestAllTypes{single_int32: 4}").getAst(); - Program program = CEL_RUNTIME.createProgram(ast); - - CelEvaluationException e = assertThrows(CelEvaluationException.class, program::eval); + CelAbstractSyntaxTree ast = + CEL_COMPILER + .compile( + "TestAllTypes{single_int32: 4, repeated_string: ['a'], map_string_string: {'k':" + + " 'v'}, single_nested_message: TestAllTypes.NestedMessage{bb: 7}," + + " single_int64_wrapper: 5}") + .getAst(); - assertThat(e) - .hasMessageThat() - .contains("Message creation with prepopulated fields is not supported yet."); + TestAllTypes message = (TestAllTypes) CEL_RUNTIME.createProgram(ast).eval(); + + assertThat(message) + .isEqualTo( + TestAllTypes.newBuilder() + .setSingleInt32(4) + .addRepeatedString("a") + .putMapStringString("k", "v") + .setSingleNestedMessage(NestedMessage.newBuilder().setBb(7)) + .setSingleInt64Wrapper(Int64Value.of(5)) + .build()); } @Test @@ -762,4 +777,121 @@ public void eval_typeFunction_subsettable() throws Exception { assertThrows(CelEvaluationException.class, () -> runtimeWithoutType.createProgram(ast)); assertThat(e).hasMessageThat().contains("No matching overload for function 'type'"); } + + @Test + public void eval_messageEquality_defaultOptions(@TestParameter MessageEqualityTestCase testCase) + throws Exception { + CelCompiler compiler = + CEL_COMPILER + .toCompilerBuilder() + .addLibraries(CelExtensions.lists(), CelExtensions.sets()) + .addMessageTypes(dev.cel.expr.conformance.proto2.TestAllTypes.getDescriptor()) + .addVar("x", SimpleType.DYN) + .addVar("y", SimpleType.DYN) + .build(); + CelLiteRuntime runtime = + CEL_RUNTIME + .toRuntimeBuilder() + .addLibraries(CelListsRuntimeLibrary.lists(), CelSetsRuntimeLibrary.sets()) + .build(); + CelAbstractSyntaxTree ast = compiler.compile(testCase.expression).getAst(); + + Object result = runtime.createProgram(ast).eval(testCase.activation); + + assertThat(result).isEqualTo(testCase.expected); + } + + @SuppressWarnings("ImmutableEnumChecker") // Test only + private enum MessageEqualityTestCase { + EQUAL_MESSAGES( + "TestAllTypes{single_int32: 1} == TestAllTypes{single_int32: 1}", ImmutableMap.of(), true), + UNEQUAL_MESSAGES( + "TestAllTypes{single_int32: 1} != TestAllTypes{single_int32: 2}", ImmutableMap.of(), true), + MESSAGE_NOT_EQUAL_TO_NULL("dyn(TestAllTypes{}) == null", ImmutableMap.of(), false), + NAN_MESSAGE_UNEQUAL( + "TestAllTypes{single_double: double('NaN')} == TestAllTypes{single_double: double('NaN')}", + ImmutableMap.of(), + false), + NAN_MESSAGE_NOT_EQUALS_TRUE( + "TestAllTypes{single_double: double('NaN')} != TestAllTypes{single_double: double('NaN')}", + ImmutableMap.of(), + true), + PROTO2_SIGNED_ZERO_EQUAL( + "cel.expr.conformance.proto2.TestAllTypes{single_double: -0.0} == " + + "cel.expr.conformance.proto2.TestAllTypes{single_double: 0.0}", + ImmutableMap.of(), + true), + REENCODED_ANY_EQUAL( + "x == y", + ImmutableMap.of( + "x", + TestAllTypes.newBuilder() + .setSingleAny( + Any.pack( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .setSingleDouble(-0.0) + .build())) + .build(), + "y", + TestAllTypes.newBuilder() + .setSingleAny( + Any.pack( + dev.cel.expr.conformance.proto2.TestAllTypes.newBuilder() + .setSingleDouble(0.0) + .build())) + .build()), + true), + IN_LIST_SIGNED_ZERO( + "cel.expr.conformance.proto2.TestAllTypes{single_double: -0.0} in " + + "[cel.expr.conformance.proto2.TestAllTypes{single_double: 0.0}]", + ImmutableMap.of(), + true), + IN_LIST_NAN( + "TestAllTypes{single_double: double('NaN')} in " + + "[TestAllTypes{single_double: double('NaN')}]", + ImmutableMap.of(), + false), + LIST_OF_MESSAGES_EQUAL( + "[cel.expr.conformance.proto2.TestAllTypes{single_double: -0.0}] == " + + "[cel.expr.conformance.proto2.TestAllTypes{single_double: 0.0}]", + ImmutableMap.of(), + true), + MAP_OF_MESSAGES_EQUAL( + "{'k': cel.expr.conformance.proto2.TestAllTypes{single_double: -0.0}} == " + + "{'k': cel.expr.conformance.proto2.TestAllTypes{single_double: 0.0}}", + ImmutableMap.of(), + true), + LISTS_DISTINCT_SIGNED_ZERO_DEDUPLICATES( + "[cel.expr.conformance.proto2.TestAllTypes{single_double: -0.0}, " + + "cel.expr.conformance.proto2.TestAllTypes{single_double: 0.0}].distinct().size()", + ImmutableMap.of(), + 1L), + LISTS_DISTINCT_NAN_KEEPS_BOTH( + "[TestAllTypes{single_double: double('NaN')}, " + + "TestAllTypes{single_double: double('NaN')}].distinct().size()", + ImmutableMap.of(), + 2L), + SETS_CONTAINS_SIGNED_ZERO( + "sets.contains([cel.expr.conformance.proto2.TestAllTypes{single_double: 0.0}], " + + "[cel.expr.conformance.proto2.TestAllTypes{single_double: -0.0}])", + ImmutableMap.of(), + true), + SETS_CONTAINS_NAN( + "sets.contains([TestAllTypes{single_double: double('NaN')}], " + + "[TestAllTypes{single_double: double('NaN')}])", + ImmutableMap.of(), + false), + ; + + private final String expression; + private final ImmutableMap activation; + private final Object expected; + + MessageEqualityTestCase( + String expression, ImmutableMap activation, Object expected) { + this.expression = expression; + this.activation = activation; + this.expected = expected; + } + } } diff --git a/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeVersionSkewTest.java b/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeVersionSkewTest.java index 3107a23e2..5951e9708 100644 --- a/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeVersionSkewTest.java +++ b/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeVersionSkewTest.java @@ -1278,49 +1278,111 @@ public void select_negativeInt32InPopulatedMessage_evaluatesCorrectly() throws E } @Test - public void submessageEquality_withKnownSubmessageDescriptor_throwsUnsupportedOperationException( + public void submessageEquality_selfComparison_returnsTrue( + @TestParameter boolean knownSubmessageDescriptor, @TestParameter({ "msg.single_nested_message == msg.single_nested_message", "msg.repeated_nested_message == msg.repeated_nested_message" }) String expr) throws Exception { + CelLiteRuntime runtime = + knownSubmessageDescriptor ? clientRuntime : newRuntimeWithoutNestedMessageDescriptor(); CelAbstractSyntaxTree optimizedAst = serverOptimizer.optimize(serverCompiler.compile(expr).getAst()); - Program program = clientRuntime.createProgram(optimizedAst); + Program program = runtime.createProgram(optimizedAst); - CelEvaluationException thrown = - assertThrows( - CelEvaluationException.class, - () -> program.eval(ImmutableMap.of("msg", POPULATED_SERVER_MESSAGE))); + assertThat(program.eval(ImmutableMap.of("msg", POPULATED_SERVER_MESSAGE))).isEqualTo(true); + } + + @Test + @TestParameters({ + "{knownSubmessageDescriptor: true, bb: 123, expected: true}", + "{knownSubmessageDescriptor: true, bb: 999, expected: false}", + "{knownSubmessageDescriptor: false, bb: 123, expected: true}", + "{knownSubmessageDescriptor: false, bb: 999, expected: false}" + }) + public void submessageEquality_comparesSubmessages( + boolean knownSubmessageDescriptor, int bb, boolean expected) throws Exception { + CelLiteRuntime runtime = + knownSubmessageDescriptor ? clientRuntime : newRuntimeWithoutNestedMessageDescriptor(); + CelAbstractSyntaxTree optimizedAst = + serverOptimizer.optimize( + serverCompiler + .compile("msg.single_nested_message == nested_msg.payload.single_nested_message") + .getAst()); + Program program = runtime.createProgram(optimizedAst); + NestedTestAllTypes other = + NestedTestAllTypes.newBuilder() + .setPayload( + TestAllTypes.newBuilder() + .setSingleNestedMessage(NestedMessage.newBuilder().setBb(bb))) + .build(); - assertThat(thrown).hasCauseThat().isInstanceOf(UnsupportedOperationException.class); - assertThat(thrown).hasCauseThat().hasMessageThat().contains("Not implemented yet"); + assertThat(program.eval(ImmutableMap.of("msg", POPULATED_SERVER_MESSAGE, "nested_msg", other))) + .isEqualTo(expected); } @Test - public void submessageEquality_withoutSubmessageDescriptor_throwsUnsupportedOperationException( - @TestParameter({ - "msg.single_nested_message == msg.single_nested_message", - "msg.repeated_nested_message == msg.repeated_nested_message" - }) - String expr) + public void messageEquality_nanInUnknownClientField_comparesByBytes() throws Exception { + CelAbstractSyntaxTree optimizedAst = + serverOptimizer.optimize(serverCompiler.compile("msg == nested_msg.payload").getAst()); + TestAllTypes nanMsg1 = TestAllTypes.newBuilder().setSingleDouble(Double.NaN).build(); + TestAllTypes nanMsg2 = TestAllTypes.newBuilder().setSingleDouble(Double.NaN).build(); + NestedTestAllTypes nestedNan = NestedTestAllTypes.newBuilder().setPayload(nanMsg2).build(); + CelLiteRuntime v2Runtime = + CelLiteRuntimeFactory.newLiteRuntimeBuilder() + .setStandardFunctions(CelStandardFunctions.ALL_STANDARD_FUNCTIONS) + .setValueProvider( + ProtoMessageLiteValueProvider.newInstance( + TestAllTypesCelDescriptor.getDescriptor(), + NestedTestAllTypesCelDescriptor.getDescriptor())) + .setContainer(CEL_CONTAINER) + .build(); + + // single_double is absent from clientRuntime's V1 descriptor, so it falls back to wire bytes. + assertThat( + clientRuntime + .createProgram(optimizedAst) + .eval(ImmutableMap.of("msg", nanMsg1, "nested_msg", nestedNan))) + .isEqualTo(true); + assertThat( + v2Runtime + .createProgram(optimizedAst) + .eval(ImmutableMap.of("msg", nanMsg1, "nested_msg", nestedNan))) + .isEqualTo(false); + } + + @Test + public void messageEquality_selectedSubmessageVsUnregisteredRootVar_returnsFalse() throws Exception { CelLiteRuntime runtimeWithoutNestedDesc = newRuntimeWithoutNestedMessageDescriptor(); CelAbstractSyntaxTree optimizedAst = - serverOptimizer.optimize(serverCompiler.compile(expr).getAst()); - Program program = runtimeWithoutNestedDesc.createProgram(optimizedAst); + serverOptimizer.optimize( + serverCompiler.compile("msg.single_nested_message == dyn(nested_msg)").getAst()); + NestedMessage nested = NestedMessage.newBuilder().setBb(123).build(); - CelEvaluationException thrown = - assertThrows( - CelEvaluationException.class, - () -> program.eval(ImmutableMap.of("msg", POPULATED_SERVER_MESSAGE))); + assertThat( + runtimeWithoutNestedDesc + .createProgram(optimizedAst) + .eval(ImmutableMap.of("msg", POPULATED_SERVER_MESSAGE, "nested_msg", nested))) + .isEqualTo(false); + } - assertThat(thrown).hasCauseThat().isInstanceOf(UnsupportedOperationException.class); - assertThat(thrown) - .hasCauseThat() - .hasMessageThat() - .contains("Message equality is not supported"); + @Test + public void messageEquality_twoUnregisteredRootVarsWithSameWireBytes_returnsTrue() + throws Exception { + CelLiteRuntime descriptorlessRuntime = newDescriptorlessRuntime(); + CelAbstractSyntaxTree optimizedAst = + serverOptimizer.optimize(serverCompiler.compile("dyn(nested_msg) == dyn(msg)").getAst()); + NestedMessage nested = NestedMessage.newBuilder().setBb(123).build(); + TestAllTypes sameWireBytes = TestAllTypes.newBuilder().setSingleInt32(123).build(); + + assertThat( + descriptorlessRuntime + .createProgram(optimizedAst) + .eval(ImmutableMap.of("nested_msg", nested, "msg", sameWireBytes))) + .isEqualTo(true); } @Test diff --git a/runtime/src/test/java/dev/cel/runtime/RuntimeEqualityTest.java b/runtime/src/test/java/dev/cel/runtime/RuntimeEqualityTest.java index de729eb1a..e7093183a 100644 --- a/runtime/src/test/java/dev/cel/runtime/RuntimeEqualityTest.java +++ b/runtime/src/test/java/dev/cel/runtime/RuntimeEqualityTest.java @@ -15,7 +15,6 @@ package dev.cel.runtime; import static com.google.common.truth.Truth.assertThat; -import static org.junit.Assert.assertThrows; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; @@ -28,8 +27,10 @@ import dev.cel.common.internal.DefaultDescriptorPool; import dev.cel.common.internal.DefaultMessageFactory; import dev.cel.common.internal.DynamicProto; +import dev.cel.common.values.CelValueConverter; import dev.cel.common.values.ProtoMessageLiteValueProvider; import dev.cel.expr.conformance.proto2.TestAllTypes; +import dev.cel.expr.conformance.proto2.TestAllTypesCelDescriptor; import java.util.Collections; import java.util.LinkedList; import java.util.Map; @@ -167,33 +168,42 @@ public void objectEquals_nullMapValue_returnsFalse() { } @Test - public void objectEquals_protoMessageLite_throwsSymmetrically() { + public void objectEquals_protoMessageLite_fallsBackToClassAndEquals() { + class LiteStub implements MessageLiteOrBuilder { + @Override + public MessageLite getDefaultInstanceForType() { + return null; + } + + @Override + public boolean isInitialized() { + return true; + } + + @Override + public boolean equals(Object other) { + return other instanceof MessageLiteOrBuilder; + } + + @Override + public int hashCode() { + return 1; + } + } ProtoMessageRuntimeEquality protoEquality = ProtoMessageRuntimeEquality.create( DynamicProto.create(DefaultMessageFactory.create(DefaultDescriptorPool.INSTANCE)), CelOptions.current().enableProtoDifferencerEquality(true).build()); - MessageLiteOrBuilder liteOnly = - new MessageLiteOrBuilder() { - @Override - public MessageLite getDefaultInstanceForType() { - return null; - } - - @Override - public boolean isInitialized() { - return true; - } - }; + LiteStub sameClass1 = new LiteStub(); + LiteStub sameClass2 = new LiteStub(); + LiteStub subclass = new LiteStub() {}; TestAllTypes fullMessage = TestAllTypes.getDefaultInstance(); - assertThrows( - UnsupportedOperationException.class, - () -> protoEquality.objectEquals(liteOnly, fullMessage)); - assertThrows( - UnsupportedOperationException.class, - () -> protoEquality.objectEquals(fullMessage, liteOnly)); - assertThrows( - UnsupportedOperationException.class, () -> protoEquality.objectEquals(liteOnly, liteOnly)); + assertThat(protoEquality.objectEquals(sameClass1, fullMessage)).isFalse(); + assertThat(protoEquality.objectEquals(fullMessage, sameClass1)).isFalse(); + assertThat(protoEquality.objectEquals(sameClass1, sameClass2)).isTrue(); + assertThat(protoEquality.objectEquals(sameClass1, subclass)).isFalse(); + assertThat(protoEquality.hashCode(sameClass1)).isEqualTo(sameClass1.getClass().hashCode()); } @Test @@ -205,45 +215,58 @@ public void inList_negativeZero_returnsTrue() { } @Test - public void objectEquals_messageLite_throws() { + public void objectEquals_messageLite_comparesWithoutThrowing() { TestAllTypes.Builder builder = TestAllTypes.newBuilder(); TestAllTypes defaultInstance = TestAllTypes.getDefaultInstance(); + TestAllTypes populated = TestAllTypes.newBuilder().setSingleInt32(1).build(); + TestAllTypes populatedCopy = TestAllTypes.newBuilder().setSingleInt32(1).build(); + + assertThat(RUNTIME_EQUALITY.objectEquals(builder, defaultInstance)).isFalse(); + assertThat(RUNTIME_EQUALITY.objectEquals(builder, builder)).isTrue(); + assertThat(RUNTIME_EQUALITY.objectEquals("not_a_message", defaultInstance)).isFalse(); + assertThat(RUNTIME_EQUALITY.objectEquals(defaultInstance, null)).isFalse(); + assertThat(RUNTIME_EQUALITY.objectEquals(null, defaultInstance)).isFalse(); + assertThat(RUNTIME_EQUALITY.objectEquals(populated, populatedCopy)).isTrue(); + assertThat(RUNTIME_EQUALITY.objectEquals(defaultInstance, populated)).isFalse(); + assertThat(RUNTIME_EQUALITY.hashCode(populated)).isEqualTo(populated.hashCode()); + } - UnsupportedOperationException builderThrown = - assertThrows( - UnsupportedOperationException.class, - () -> RUNTIME_EQUALITY.objectEquals(builder, defaultInstance)); - UnsupportedOperationException stringThrown = - assertThrows( - UnsupportedOperationException.class, - () -> RUNTIME_EQUALITY.objectEquals("not_a_message", defaultInstance)); - - assertThat(builderThrown).hasMessageThat().isEqualTo("Not implemented yet"); - assertThat(stringThrown).hasMessageThat().isEqualTo("Not implemented yet"); + @Test + public void objectEquals_wireMessageLite_comparesByTypeAndWireFields() { + CelValueConverter converter = + ProtoMessageLiteValueProvider.newInstance().protoCelValueConverter(); + Object wireMsg1 = converter.toRuntimeValue(TestAllTypes.getDefaultInstance()); + Object wireMsg1Copy = converter.toRuntimeValue(TestAllTypes.getDefaultInstance()); + Object wireMsg2 = converter.toRuntimeValue(TestAllTypes.newBuilder().setSingleInt32(1).build()); + + assertThat(RUNTIME_EQUALITY.objectEquals(wireMsg1, wireMsg1Copy)).isTrue(); + assertThat(RUNTIME_EQUALITY.objectEquals(wireMsg1, wireMsg2)).isFalse(); + assertThat(RUNTIME_EQUALITY.objectEquals("not_a_message", wireMsg1)).isFalse(); + assertThat(RUNTIME_EQUALITY.objectEquals(wireMsg1, null)).isFalse(); + assertThat(RUNTIME_EQUALITY.hashCode(wireMsg1)) + .isEqualTo(RUNTIME_EQUALITY.hashCode(wireMsg1Copy)); } @Test - public void objectEquals_wireMessageLite_throws() { - Object wireMsg1 = - ProtoMessageLiteValueProvider.newInstance() - .protoCelValueConverter() - .toRuntimeValue(TestAllTypes.getDefaultInstance()); - Object wireMsg2 = - ProtoMessageLiteValueProvider.newInstance() - .protoCelValueConverter() - .toRuntimeValue(TestAllTypes.newBuilder().setSingleInt32(1).build()); - - UnsupportedOperationException messagesThrown = - assertThrows( - UnsupportedOperationException.class, - () -> RUNTIME_EQUALITY.objectEquals(wireMsg1, wireMsg2)); - UnsupportedOperationException stringThrown = - assertThrows( - UnsupportedOperationException.class, - () -> RUNTIME_EQUALITY.objectEquals("not_a_message", wireMsg1)); - - assertThat(messagesThrown).hasMessageThat().isEqualTo("Message equality is not supported"); - assertThat(stringThrown).hasMessageThat().isEqualTo("Message equality is not supported"); + public void objectEquals_and_hashCode_protoDifferencerWithLiteConverter() { + RuntimeEquality liteEquality = + RuntimeEquality.create( + RuntimeHelpers.create(), + CelOptions.current().enableProtoDifferencerEquality(true).build(), + ProtoMessageLiteValueProvider.newInstance(TestAllTypesCelDescriptor.getDescriptor()) + .celValueConverter()); + TestAllTypes minusZero = TestAllTypes.newBuilder().setSingleDouble(-0.0).build(); + TestAllTypes plusZero = TestAllTypes.newBuilder().setSingleDouble(0.0).build(); + TestAllTypes nan = TestAllTypes.newBuilder().setSingleDouble(Double.NaN).build(); + + assertThat(minusZero.hashCode()).isNotEqualTo(plusZero.hashCode()); + assertThat(liteEquality.objectEquals(minusZero, plusZero)).isTrue(); + assertThat(liteEquality.hashCode(minusZero)).isEqualTo(liteEquality.hashCode(plusZero)); + assertThat(liteEquality.objectEquals(nan, nan)).isFalse(); + assertThat(liteEquality.objectEquals(TestAllTypes.newBuilder(), TestAllTypes.newBuilder())) + .isFalse(); + assertThat(liteEquality.objectEquals(minusZero, "not_a_message")).isFalse(); + assertThat(liteEquality.hashCode("not_a_message")).isEqualTo("not_a_message".hashCode()); } @Test diff --git a/testing/src/main/java/dev/cel/testing/compiled/BUILD.bazel b/testing/src/main/java/dev/cel/testing/compiled/BUILD.bazel index 2c5921175..ec59b70d3 100644 --- a/testing/src/main/java/dev/cel/testing/compiled/BUILD.bazel +++ b/testing/src/main/java/dev/cel/testing/compiled/BUILD.bazel @@ -63,6 +63,8 @@ java_library( ":compiled_proto2_select_repeated_fields", ":compiled_proto2_select_wrappers", ":compiled_proto3_deep_traversal", + ":compiled_proto3_message_creation", + ":compiled_proto3_message_equality", ":compiled_proto3_select_map_fields", ":compiled_proto3_select_primitives", ":compiled_proto3_select_primitives_all_ored", @@ -106,6 +108,17 @@ compile_cel( proto_srcs = ["@cel_conformance//proto/cel/expr/conformance/proto3:test_all_types_proto"], ) +compile_cel( + name = "compiled_proto3_message_creation", + expression = "cel.expr.conformance.proto3.TestAllTypes{single_int32: -1, single_sint64: -2, single_fixed32: 4294967295u, " + + "single_float: 1.5, single_string: 'foo', single_bytes: b'abc', standalone_enum: 2, optional_bool: false, " + + "single_nested_message: cel.expr.conformance.proto3.TestAllTypes.NestedMessage{bb: 7}, repeated_int64: [1, 2], " + + "map_string_string: {'k': 'v'}, single_int64_wrapper: 5, single_uint32_wrapper: google.protobuf.UInt32Value{value: 4294967295u}, " + + "single_duration: duration('1s'), single_struct: google.protobuf.Struct{fields: {'a': 1.0}}, " + + "single_value: google.protobuf.Value{string_value: 'x'}}", + proto_srcs = ["@cel_conformance//proto/cel/expr/conformance/proto3:test_all_types_proto"], +) + compile_cel( name = "compiled_extensions", environment = "//testing/environment:all_extensions", @@ -251,6 +264,13 @@ compile_cel( proto_srcs = ["@cel_conformance//proto/cel/expr/conformance/proto3:test_all_types_proto"], ) +compile_cel( + name = "compiled_proto3_message_equality", + environment = "//testing/environment:proto3_message_variables", + expression = "proto3.oneof_type.payload == proto3.oneof_type.child.payload", + proto_srcs = ["@cel_conformance//proto/cel/expr/conformance/proto3:test_all_types_proto"], +) + compile_cel( name = "compiled_proto2_select_repeated_fields", environment = "//testing/environment:proto2_message_variables",