Rafi Kamal | 58d4420 | 2019-11-11 17:06:56 -0800 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // https://developers.google.com/protocol-buffers/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 31 | import com.google.protobuf.AbstractMessage; |
Yilun Chong | cb95a7f | 2019-01-11 11:40:52 -0800 | [diff] [blame] | 32 | import com.google.protobuf.ByteString; |
Adam Cozzette | 5a76e63 | 2016-11-17 16:48:38 -0800 | [diff] [blame] | 33 | import com.google.protobuf.CodedInputStream; |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 34 | import com.google.protobuf.ExtensionRegistry; |
Yilun Chong | cb95a7f | 2019-01-11 11:40:52 -0800 | [diff] [blame] | 35 | import com.google.protobuf.InvalidProtocolBufferException; |
| 36 | import com.google.protobuf.Parser; |
| 37 | import com.google.protobuf.TextFormat; |
| 38 | import com.google.protobuf.conformance.Conformance; |
Adam Cozzette | 5a76e63 | 2016-11-17 16:48:38 -0800 | [diff] [blame] | 39 | import com.google.protobuf.util.JsonFormat; |
Joshua Haberman | f1ce60e | 2016-12-03 11:51:25 -0500 | [diff] [blame] | 40 | import com.google.protobuf.util.JsonFormat.TypeRegistry; |
Yilun Chong | cb95a7f | 2019-01-11 11:40:52 -0800 | [diff] [blame] | 41 | import com.google.protobuf_test_messages.proto2.TestMessagesProto2; |
| 42 | import com.google.protobuf_test_messages.proto2.TestMessagesProto2.TestAllTypesProto2; |
| 43 | import com.google.protobuf_test_messages.proto3.TestMessagesProto3; |
| 44 | import com.google.protobuf_test_messages.proto3.TestMessagesProto3.TestAllTypesProto3; |
Adam Cozzette | 5a76e63 | 2016-11-17 16:48:38 -0800 | [diff] [blame] | 45 | import java.nio.ByteBuffer; |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 46 | import java.util.ArrayList; |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 47 | |
| 48 | class ConformanceJava { |
| 49 | private int testCount = 0; |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 50 | private TypeRegistry typeRegistry; |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 51 | |
| 52 | private boolean readFromStdin(byte[] buf, int len) throws Exception { |
| 53 | int ofs = 0; |
| 54 | while (len > 0) { |
| 55 | int read = System.in.read(buf, ofs, len); |
| 56 | if (read == -1) { |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 57 | return false; // EOF |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 58 | } |
| 59 | ofs += read; |
| 60 | len -= read; |
| 61 | } |
| 62 | |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | private void writeToStdout(byte[] buf) throws Exception { |
| 67 | System.out.write(buf); |
| 68 | } |
| 69 | |
| 70 | // Returns -1 on EOF (the actual values will always be positive). |
| 71 | private int readLittleEndianIntFromStdin() throws Exception { |
| 72 | byte[] buf = new byte[4]; |
| 73 | if (!readFromStdin(buf, 4)) { |
| 74 | return -1; |
| 75 | } |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 76 | return (buf[0] & 0xff) |
| 77 | | ((buf[1] & 0xff) << 8) |
| 78 | | ((buf[2] & 0xff) << 16) |
| 79 | | ((buf[3] & 0xff) << 24); |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | private void writeLittleEndianIntToStdout(int val) throws Exception { |
| 83 | byte[] buf = new byte[4]; |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 84 | buf[0] = (byte) val; |
| 85 | buf[1] = (byte) (val >> 8); |
| 86 | buf[2] = (byte) (val >> 16); |
| 87 | buf[3] = (byte) (val >> 24); |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 88 | writeToStdout(buf); |
| 89 | } |
Xiang Dai | e479410 | 2019-02-21 11:28:50 +0800 | [diff] [blame] | 90 | |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 91 | private enum BinaryDecoderType { |
| 92 | BTYE_STRING_DECODER, |
| 93 | BYTE_ARRAY_DECODER, |
| 94 | ARRAY_BYTE_BUFFER_DECODER, |
| 95 | READONLY_ARRAY_BYTE_BUFFER_DECODER, |
| 96 | DIRECT_BYTE_BUFFER_DECODER, |
| 97 | READONLY_DIRECT_BYTE_BUFFER_DECODER, |
| 98 | INPUT_STREAM_DECODER; |
| 99 | } |
| 100 | |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 101 | private static class BinaryDecoder<T extends AbstractMessage> { |
| 102 | public T decode( |
| 103 | ByteString bytes, BinaryDecoderType type, Parser<T> parser, ExtensionRegistry extensions) |
| 104 | throws InvalidProtocolBufferException { |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 105 | switch (type) { |
Xiang Dai | e479410 | 2019-02-21 11:28:50 +0800 | [diff] [blame] | 106 | case BTYE_STRING_DECODER: |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 107 | case BYTE_ARRAY_DECODER: |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 108 | return parser.parseFrom(bytes, extensions); |
| 109 | case ARRAY_BYTE_BUFFER_DECODER: |
| 110 | { |
| 111 | ByteBuffer buffer = ByteBuffer.allocate(bytes.size()); |
| 112 | bytes.copyTo(buffer); |
| 113 | buffer.flip(); |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 114 | return parser.parseFrom(CodedInputStream.newInstance(buffer), extensions); |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 115 | } |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 116 | case READONLY_ARRAY_BYTE_BUFFER_DECODER: |
| 117 | { |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 118 | return parser.parseFrom( |
| 119 | CodedInputStream.newInstance(bytes.asReadOnlyByteBuffer()), extensions); |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 120 | } |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 121 | case DIRECT_BYTE_BUFFER_DECODER: |
| 122 | { |
| 123 | ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.size()); |
| 124 | bytes.copyTo(buffer); |
| 125 | buffer.flip(); |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 126 | return parser.parseFrom(CodedInputStream.newInstance(buffer), extensions); |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 127 | } |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 128 | case READONLY_DIRECT_BYTE_BUFFER_DECODER: |
| 129 | { |
| 130 | ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.size()); |
| 131 | bytes.copyTo(buffer); |
| 132 | buffer.flip(); |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 133 | return parser.parseFrom( |
| 134 | CodedInputStream.newInstance(buffer.asReadOnlyBuffer()), extensions); |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 135 | } |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 136 | case INPUT_STREAM_DECODER: |
| 137 | { |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 138 | return parser.parseFrom(bytes.newInput(), extensions); |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 139 | } |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 140 | } |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 141 | return null; |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 145 | private <T extends AbstractMessage> T parseBinary( |
| 146 | ByteString bytes, Parser<T> parser, ExtensionRegistry extensions) |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 147 | throws InvalidProtocolBufferException { |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 148 | ArrayList<T> messages = new ArrayList<>(); |
| 149 | ArrayList<InvalidProtocolBufferException> exceptions = new ArrayList<>(); |
Xiang Dai | e479410 | 2019-02-21 11:28:50 +0800 | [diff] [blame] | 150 | |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 151 | for (int i = 0; i < BinaryDecoderType.values().length; i++) { |
| 152 | messages.add(null); |
| 153 | exceptions.add(null); |
| 154 | } |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 155 | if (messages.isEmpty()) { |
| 156 | throw new RuntimeException("binary decoder types missing"); |
| 157 | } |
| 158 | |
| 159 | BinaryDecoder<T> decoder = new BinaryDecoder<>(); |
Yilun Chong | 696cf77 | 2017-06-28 11:32:01 -0700 | [diff] [blame] | 160 | |
| 161 | boolean hasMessage = false; |
| 162 | boolean hasException = false; |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 163 | for (int i = 0; i < BinaryDecoderType.values().length; ++i) { |
Yilun Chong | 696cf77 | 2017-06-28 11:32:01 -0700 | [diff] [blame] | 164 | try { |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 165 | // = BinaryDecoderType.values()[i].parseProto3(bytes); |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 166 | messages.set(i, decoder.decode(bytes, BinaryDecoderType.values()[i], parser, extensions)); |
Adam Cozzette | 5a76e63 | 2016-11-17 16:48:38 -0800 | [diff] [blame] | 167 | hasMessage = true; |
| 168 | } catch (InvalidProtocolBufferException e) { |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 169 | exceptions.set(i, e); |
Adam Cozzette | 5a76e63 | 2016-11-17 16:48:38 -0800 | [diff] [blame] | 170 | hasException = true; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if (hasMessage && hasException) { |
| 175 | StringBuilder sb = |
| 176 | new StringBuilder("Binary decoders disagreed on whether the payload was valid.\n"); |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 177 | for (int i = 0; i < BinaryDecoderType.values().length; ++i) { |
| 178 | sb.append(BinaryDecoderType.values()[i].name()); |
| 179 | if (messages.get(i) != null) { |
Adam Cozzette | 5a76e63 | 2016-11-17 16:48:38 -0800 | [diff] [blame] | 180 | sb.append(" accepted the payload.\n"); |
| 181 | } else { |
| 182 | sb.append(" rejected the payload.\n"); |
| 183 | } |
| 184 | } |
| 185 | throw new RuntimeException(sb.toString()); |
| 186 | } |
| 187 | |
| 188 | if (hasException) { |
| 189 | // We do not check if exceptions are equal. Different implementations may return different |
| 190 | // exception messages. Throw an arbitrary one out instead. |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 191 | InvalidProtocolBufferException exception = null; |
| 192 | for (InvalidProtocolBufferException e : exceptions) { |
| 193 | if (exception != null) { |
| 194 | exception.addSuppressed(e); |
| 195 | } else { |
| 196 | exception = e; |
| 197 | } |
| 198 | } |
| 199 | throw exception; |
Adam Cozzette | 5a76e63 | 2016-11-17 16:48:38 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // Fast path comparing all the messages with the first message, assuming equality being |
| 203 | // symmetric and transitive. |
| 204 | boolean allEqual = true; |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 205 | for (int i = 1; i < messages.size(); ++i) { |
| 206 | if (!messages.get(0).equals(messages.get(i))) { |
Adam Cozzette | 5a76e63 | 2016-11-17 16:48:38 -0800 | [diff] [blame] | 207 | allEqual = false; |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // Slow path: compare and find out all unequal pairs. |
| 213 | if (!allEqual) { |
| 214 | StringBuilder sb = new StringBuilder(); |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 215 | for (int i = 0; i < messages.size() - 1; ++i) { |
| 216 | for (int j = i + 1; j < messages.size(); ++j) { |
| 217 | if (!messages.get(i).equals(messages.get(j))) { |
| 218 | sb.append(BinaryDecoderType.values()[i].name()) |
Adam Cozzette | 5a76e63 | 2016-11-17 16:48:38 -0800 | [diff] [blame] | 219 | .append(" and ") |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 220 | .append(BinaryDecoderType.values()[j].name()) |
Adam Cozzette | 5a76e63 | 2016-11-17 16:48:38 -0800 | [diff] [blame] | 221 | .append(" parsed the payload differently.\n"); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | throw new RuntimeException(sb.toString()); |
| 226 | } |
| 227 | |
Yilun Chong | fcb9268 | 2017-06-29 14:40:47 -0700 | [diff] [blame] | 228 | return messages.get(0); |
Adam Cozzette | 5a76e63 | 2016-11-17 16:48:38 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 231 | private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest request) { |
Yilun Chong | 696cf77 | 2017-06-28 11:32:01 -0700 | [diff] [blame] | 232 | com.google.protobuf.AbstractMessage testMessage; |
Joshua Haberman | 6ed7383 | 2020-05-13 13:46:15 -0700 | [diff] [blame] | 233 | boolean isProto3 = |
| 234 | request.getMessageType().equals("protobuf_test_messages.proto3.TestAllTypesProto3"); |
| 235 | boolean isProto2 = |
| 236 | request.getMessageType().equals("protobuf_test_messages.proto2.TestAllTypesProto2"); |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 237 | |
| 238 | switch (request.getPayloadCase()) { |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 239 | case PROTOBUF_PAYLOAD: |
| 240 | { |
Joshua Haberman | 6ed7383 | 2020-05-13 13:46:15 -0700 | [diff] [blame] | 241 | if (isProto3) { |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 242 | try { |
| 243 | ExtensionRegistry extensions = ExtensionRegistry.newInstance(); |
| 244 | TestMessagesProto3.registerAllExtensions(extensions); |
| 245 | testMessage = |
| 246 | parseBinary( |
| 247 | request.getProtobufPayload(), TestAllTypesProto3.parser(), extensions); |
| 248 | } catch (InvalidProtocolBufferException e) { |
| 249 | return Conformance.ConformanceResponse.newBuilder() |
| 250 | .setParseError(e.getMessage()) |
| 251 | .build(); |
| 252 | } |
Joshua Haberman | 6ed7383 | 2020-05-13 13:46:15 -0700 | [diff] [blame] | 253 | } else if (isProto2) { |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 254 | try { |
| 255 | ExtensionRegistry extensions = ExtensionRegistry.newInstance(); |
| 256 | TestMessagesProto2.registerAllExtensions(extensions); |
| 257 | testMessage = |
| 258 | parseBinary( |
| 259 | request.getProtobufPayload(), TestAllTypesProto2.parser(), extensions); |
| 260 | } catch (InvalidProtocolBufferException e) { |
| 261 | return Conformance.ConformanceResponse.newBuilder() |
| 262 | .setParseError(e.getMessage()) |
| 263 | .build(); |
| 264 | } |
Joshua Haberman | 6ed7383 | 2020-05-13 13:46:15 -0700 | [diff] [blame] | 265 | } else { |
| 266 | throw new RuntimeException("Protobuf request doesn't have specific payload type."); |
| 267 | } |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 268 | break; |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 269 | } |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 270 | case JSON_PAYLOAD: |
| 271 | { |
Yilun Chong | d8c2501 | 2019-02-22 18:13:33 +0800 | [diff] [blame] | 272 | try { |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 273 | JsonFormat.Parser parser = JsonFormat.parser().usingTypeRegistry(typeRegistry); |
| 274 | if (request.getTestCategory() |
| 275 | == Conformance.TestCategory.JSON_IGNORE_UNKNOWN_PARSING_TEST) { |
| 276 | parser = parser.ignoringUnknownFields(); |
| 277 | } |
| 278 | if (isProto3) { |
| 279 | TestMessagesProto3.TestAllTypesProto3.Builder builder = |
| 280 | TestMessagesProto3.TestAllTypesProto3.newBuilder(); |
| 281 | parser.merge(request.getJsonPayload(), builder); |
| 282 | testMessage = builder.build(); |
| 283 | } else if (isProto2) { |
| 284 | TestMessagesProto2.TestAllTypesProto2.Builder builder = |
| 285 | TestMessagesProto2.TestAllTypesProto2.newBuilder(); |
| 286 | parser.merge(request.getJsonPayload(), builder); |
| 287 | testMessage = builder.build(); |
| 288 | } else { |
| 289 | throw new RuntimeException("Protobuf request doesn't have specific payload type."); |
| 290 | } |
| 291 | } catch (InvalidProtocolBufferException e) { |
| 292 | return Conformance.ConformanceResponse.newBuilder() |
| 293 | .setParseError(e.getMessage()) |
| 294 | .build(); |
| 295 | } |
| 296 | break; |
| 297 | } |
| 298 | case TEXT_PAYLOAD: |
| 299 | { |
| 300 | if (isProto3) { |
| 301 | try { |
| 302 | TestMessagesProto3.TestAllTypesProto3.Builder builder = |
| 303 | TestMessagesProto3.TestAllTypesProto3.newBuilder(); |
| 304 | TextFormat.merge(request.getTextPayload(), builder); |
| 305 | testMessage = builder.build(); |
| 306 | } catch (TextFormat.ParseException e) { |
Yilun Chong | d8c2501 | 2019-02-22 18:13:33 +0800 | [diff] [blame] | 307 | return Conformance.ConformanceResponse.newBuilder() |
| 308 | .setParseError(e.getMessage()) |
| 309 | .build(); |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 310 | } |
| 311 | } else if (isProto2) { |
| 312 | try { |
| 313 | TestMessagesProto2.TestAllTypesProto2.Builder builder = |
| 314 | TestMessagesProto2.TestAllTypesProto2.newBuilder(); |
| 315 | TextFormat.merge(request.getTextPayload(), builder); |
| 316 | testMessage = builder.build(); |
| 317 | } catch (TextFormat.ParseException e) { |
Yilun Chong | d8c2501 | 2019-02-22 18:13:33 +0800 | [diff] [blame] | 318 | return Conformance.ConformanceResponse.newBuilder() |
| 319 | .setParseError(e.getMessage()) |
| 320 | .build(); |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 321 | } |
| 322 | } else { |
| 323 | throw new RuntimeException("Protobuf request doesn't have specific payload type."); |
Yilun Chong | d8c2501 | 2019-02-22 18:13:33 +0800 | [diff] [blame] | 324 | } |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 325 | break; |
Yilun Chong | cb95a7f | 2019-01-11 11:40:52 -0800 | [diff] [blame] | 326 | } |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 327 | case PAYLOAD_NOT_SET: |
| 328 | { |
| 329 | throw new RuntimeException("Request didn't have payload."); |
| 330 | } |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 331 | |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 332 | default: |
| 333 | { |
| 334 | throw new RuntimeException("Unexpected payload case."); |
| 335 | } |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Josh Haberman | b0500b3 | 2015-07-10 16:36:59 -0700 | [diff] [blame] | 338 | switch (request.getRequestedOutputFormat()) { |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 339 | case UNSPECIFIED: |
| 340 | throw new RuntimeException("Unspecified output format."); |
| 341 | |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 342 | case PROTOBUF: |
| 343 | { |
| 344 | ByteString messageString = testMessage.toByteString(); |
| 345 | return Conformance.ConformanceResponse.newBuilder() |
| 346 | .setProtobufPayload(messageString) |
| 347 | .build(); |
| 348 | } |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 349 | |
| 350 | case JSON: |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 351 | try { |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 352 | return Conformance.ConformanceResponse.newBuilder() |
| 353 | .setJsonPayload( |
| 354 | JsonFormat.printer().usingTypeRegistry(typeRegistry).print(testMessage)) |
| 355 | .build(); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 356 | } catch (InvalidProtocolBufferException | IllegalArgumentException e) { |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 357 | return Conformance.ConformanceResponse.newBuilder() |
| 358 | .setSerializeError(e.getMessage()) |
| 359 | .build(); |
Feng Xiao | e841bac | 2015-12-11 17:09:20 -0800 | [diff] [blame] | 360 | } |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 361 | |
Yilun Chong | cb95a7f | 2019-01-11 11:40:52 -0800 | [diff] [blame] | 362 | case TEXT_FORMAT: |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 363 | return Conformance.ConformanceResponse.newBuilder() |
| 364 | .setTextPayload(TextFormat.printToString(testMessage)) |
| 365 | .build(); |
Yilun Chong | cb95a7f | 2019-01-11 11:40:52 -0800 | [diff] [blame] | 366 | |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 367 | default: |
| 368 | { |
| 369 | throw new RuntimeException("Unexpected request output."); |
| 370 | } |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | |
| 374 | private boolean doTestIo() throws Exception { |
| 375 | int bytes = readLittleEndianIntFromStdin(); |
| 376 | |
| 377 | if (bytes == -1) { |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 378 | return false; // EOF |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | byte[] serializedInput = new byte[bytes]; |
| 382 | |
| 383 | if (!readFromStdin(serializedInput, bytes)) { |
| 384 | throw new RuntimeException("Unexpected EOF from test program."); |
| 385 | } |
| 386 | |
| 387 | Conformance.ConformanceRequest request = |
| 388 | Conformance.ConformanceRequest.parseFrom(serializedInput); |
| 389 | Conformance.ConformanceResponse response = doTest(request); |
| 390 | byte[] serializedOutput = response.toByteArray(); |
| 391 | |
| 392 | writeLittleEndianIntToStdout(serializedOutput.length); |
| 393 | writeToStdout(serializedOutput); |
| 394 | |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | public void run() throws Exception { |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 399 | typeRegistry = |
| 400 | TypeRegistry.newBuilder() |
| 401 | .add(TestMessagesProto3.TestAllTypesProto3.getDescriptor()) |
| 402 | .build(); |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 403 | while (doTestIo()) { |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 404 | this.testCount++; |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Joshua Haberman | ce56063 | 2021-04-15 15:53:17 -0700 | [diff] [blame] | 407 | System.err.println( |
| 408 | "ConformanceJava: received EOF from test runner after " + this.testCount + " tests"); |
Josh Haberman | 420f938 | 2015-04-16 12:50:39 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | public static void main(String[] args) throws Exception { |
| 412 | new ConformanceJava().run(); |
| 413 | } |
| 414 | } |