|
| 1 | +// Copyright 2023 Buf Technologies, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package build.buf.protovalidate; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | + |
| 19 | +import build.buf.validate.Violation; |
| 20 | +import com.example.noimports.validationtest.ExampleFieldConstraints; |
| 21 | +import com.example.noimports.validationtest.ExampleMessageConstraints; |
| 22 | +import com.example.noimports.validationtest.ExampleOneofConstraints; |
| 23 | +import com.google.protobuf.DescriptorProtos; |
| 24 | +import com.google.protobuf.Descriptors; |
| 25 | +import com.google.protobuf.DynamicMessage; |
| 26 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 27 | +import com.google.protobuf.Message; |
| 28 | +import java.util.LinkedHashSet; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Set; |
| 31 | +import java.util.function.Function; |
| 32 | +import java.util.stream.Collectors; |
| 33 | +import org.junit.Test; |
| 34 | + |
| 35 | +/** |
| 36 | + * This test mimics the behavior when performing validation with protovalidate on a file descriptor |
| 37 | + * set (as created by <code>protoc --retain_options --descriptor_set_out=...</code>). These |
| 38 | + * descriptor types have the protovalidate extensions as unknown fields and need to be parsed with |
| 39 | + * an extension registry for the constraints to be recognized and validated. |
| 40 | + */ |
| 41 | +public class ValidatorDynamicMessageTest { |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testFieldConstraintDynamicMessage() throws Exception { |
| 45 | + DynamicMessage.Builder messageBuilder = |
| 46 | + createMessageWithUnknownOptions(ExampleFieldConstraints.getDefaultInstance()); |
| 47 | + messageBuilder.setField( |
| 48 | + messageBuilder.getDescriptorForType().findFieldByName("regex_string_field"), "0123456789"); |
| 49 | + Violation expectedViolation = |
| 50 | + Violation.newBuilder() |
| 51 | + .setConstraintId("string.pattern") |
| 52 | + .setFieldPath("regex_string_field") |
| 53 | + .setMessage("value does not match regex pattern `^[a-z0-9]{1,9}$`") |
| 54 | + .build(); |
| 55 | + assertThat(new Validator().validate(messageBuilder.build()).getViolations()) |
| 56 | + .containsExactly(expectedViolation); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testOneofConstraintDynamicMessage() throws Exception { |
| 61 | + DynamicMessage.Builder messageBuilder = |
| 62 | + createMessageWithUnknownOptions(ExampleOneofConstraints.getDefaultInstance()); |
| 63 | + Violation expectedViolation = |
| 64 | + Violation.newBuilder() |
| 65 | + .setFieldPath("contact_info") |
| 66 | + .setConstraintId("required") |
| 67 | + .setMessage("exactly one field is required in oneof") |
| 68 | + .build(); |
| 69 | + assertThat(new Validator().validate(messageBuilder.build()).getViolations()) |
| 70 | + .containsExactly(expectedViolation); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testMessageConstraintDynamicMessage() throws Exception { |
| 75 | + DynamicMessage.Builder messageBuilder = |
| 76 | + createMessageWithUnknownOptions(ExampleMessageConstraints.getDefaultInstance()); |
| 77 | + messageBuilder.setField( |
| 78 | + messageBuilder.getDescriptorForType().findFieldByName("secondary_email"), |
| 79 | + |
| 80 | + Violation expectedViolation = |
| 81 | + Violation.newBuilder() |
| 82 | + .setConstraintId("secondary_email_depends_on_primary") |
| 83 | + .setMessage("cannot set a secondary email without setting a primary one") |
| 84 | + .build(); |
| 85 | + assertThat(new Validator().validate(messageBuilder.build()).getViolations()) |
| 86 | + .containsExactly(expectedViolation); |
| 87 | + } |
| 88 | + |
| 89 | + private static void gatherDependencies( |
| 90 | + Descriptors.FileDescriptor fd, Set<DescriptorProtos.FileDescriptorProto> dependencies) { |
| 91 | + dependencies.add(fd.toProto()); |
| 92 | + for (Descriptors.FileDescriptor dependency : fd.getDependencies()) { |
| 93 | + gatherDependencies(dependency, dependencies); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static DescriptorProtos.FileDescriptorSet createFileDescriptorSetForMessage( |
| 98 | + Descriptors.Descriptor message) { |
| 99 | + DescriptorProtos.FileDescriptorSet.Builder builder = |
| 100 | + DescriptorProtos.FileDescriptorSet.newBuilder(); |
| 101 | + Set<DescriptorProtos.FileDescriptorProto> dependencies = new LinkedHashSet<>(); |
| 102 | + gatherDependencies(message.getFile(), dependencies); |
| 103 | + builder.addAllFile(dependencies); |
| 104 | + return builder.build(); |
| 105 | + } |
| 106 | + |
| 107 | + private static Descriptors.FileDescriptor getFileDescriptor( |
| 108 | + String name, Map<String, DescriptorProtos.FileDescriptorProto> fds) |
| 109 | + throws Descriptors.DescriptorValidationException { |
| 110 | + DescriptorProtos.FileDescriptorProto fdProto = fds.get(name); |
| 111 | + if (fdProto == null) { |
| 112 | + throw new IllegalArgumentException("unable to file file descriptor proto: " + name); |
| 113 | + } |
| 114 | + Descriptors.FileDescriptor[] dependencies = |
| 115 | + new Descriptors.FileDescriptor[fdProto.getDependencyCount()]; |
| 116 | + for (int i = 0; i < fdProto.getDependencyCount(); i++) { |
| 117 | + dependencies[i] = getFileDescriptor(fdProto.getDependency(i), fds); |
| 118 | + } |
| 119 | + return Descriptors.FileDescriptor.buildFrom(fdProto, dependencies); |
| 120 | + } |
| 121 | + |
| 122 | + private static DynamicMessage.Builder createMessageWithUnknownOptions(Message message) |
| 123 | + throws InvalidProtocolBufferException, Descriptors.DescriptorValidationException { |
| 124 | + DescriptorProtos.FileDescriptorSet fds = |
| 125 | + createFileDescriptorSetForMessage(message.getDescriptorForType()); |
| 126 | + // Reparse file descriptor set from encoded form (loses known extensions). |
| 127 | + fds = DescriptorProtos.FileDescriptorSet.parseFrom(fds.toByteArray()); |
| 128 | + Map<String, DescriptorProtos.FileDescriptorProto> fdsMap = |
| 129 | + fds.getFileList().stream() |
| 130 | + .collect( |
| 131 | + Collectors.toMap( |
| 132 | + DescriptorProtos.FileDescriptorProto::getName, Function.identity())); |
| 133 | + Descriptors.FileDescriptor descriptor = |
| 134 | + getFileDescriptor(message.getDescriptorForType().getFile().getName(), fdsMap); |
| 135 | + return DynamicMessage.newBuilder( |
| 136 | + descriptor.findMessageTypeByName(message.getDescriptorForType().getName())); |
| 137 | + } |
| 138 | +} |
0 commit comments