ConformanceJava.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. import com.google.protobuf.AbstractMessage;
  31. import com.google.protobuf.ByteString;
  32. import com.google.protobuf.CodedInputStream;
  33. import com.google.protobuf.ExtensionRegistry;
  34. import com.google.protobuf.InvalidProtocolBufferException;
  35. import com.google.protobuf.Parser;
  36. import com.google.protobuf.TextFormat;
  37. import com.google.protobuf.conformance.Conformance;
  38. import com.google.protobuf.util.JsonFormat;
  39. import com.google.protobuf.util.JsonFormat.TypeRegistry;
  40. import com.google.protobuf_test_messages.proto2.TestMessagesProto2;
  41. import com.google.protobuf_test_messages.proto2.TestMessagesProto2.TestAllTypesProto2;
  42. import com.google.protobuf_test_messages.proto3.TestMessagesProto3;
  43. import com.google.protobuf_test_messages.proto3.TestMessagesProto3.TestAllTypesProto3;
  44. import java.nio.ByteBuffer;
  45. import java.util.ArrayList;
  46. class ConformanceJava {
  47. private int testCount = 0;
  48. private TypeRegistry typeRegistry;
  49. private boolean readFromStdin(byte[] buf, int len) throws Exception {
  50. int ofs = 0;
  51. while (len > 0) {
  52. int read = System.in.read(buf, ofs, len);
  53. if (read == -1) {
  54. return false; // EOF
  55. }
  56. ofs += read;
  57. len -= read;
  58. }
  59. return true;
  60. }
  61. private void writeToStdout(byte[] buf) throws Exception {
  62. System.out.write(buf);
  63. }
  64. // Returns -1 on EOF (the actual values will always be positive).
  65. private int readLittleEndianIntFromStdin() throws Exception {
  66. byte[] buf = new byte[4];
  67. if (!readFromStdin(buf, 4)) {
  68. return -1;
  69. }
  70. return (buf[0] & 0xff)
  71. | ((buf[1] & 0xff) << 8)
  72. | ((buf[2] & 0xff) << 16)
  73. | ((buf[3] & 0xff) << 24);
  74. }
  75. private void writeLittleEndianIntToStdout(int val) throws Exception {
  76. byte[] buf = new byte[4];
  77. buf[0] = (byte) val;
  78. buf[1] = (byte) (val >> 8);
  79. buf[2] = (byte) (val >> 16);
  80. buf[3] = (byte) (val >> 24);
  81. writeToStdout(buf);
  82. }
  83. private enum BinaryDecoderType {
  84. BTYE_STRING_DECODER,
  85. BYTE_ARRAY_DECODER,
  86. ARRAY_BYTE_BUFFER_DECODER,
  87. READONLY_ARRAY_BYTE_BUFFER_DECODER,
  88. DIRECT_BYTE_BUFFER_DECODER,
  89. READONLY_DIRECT_BYTE_BUFFER_DECODER,
  90. INPUT_STREAM_DECODER;
  91. }
  92. private static class BinaryDecoder<T extends AbstractMessage> {
  93. public T decode(
  94. ByteString bytes, BinaryDecoderType type, Parser<T> parser, ExtensionRegistry extensions)
  95. throws InvalidProtocolBufferException {
  96. switch (type) {
  97. case BTYE_STRING_DECODER:
  98. case BYTE_ARRAY_DECODER:
  99. return parser.parseFrom(bytes, extensions);
  100. case ARRAY_BYTE_BUFFER_DECODER:
  101. {
  102. ByteBuffer buffer = ByteBuffer.allocate(bytes.size());
  103. bytes.copyTo(buffer);
  104. buffer.flip();
  105. return parser.parseFrom(CodedInputStream.newInstance(buffer), extensions);
  106. }
  107. case READONLY_ARRAY_BYTE_BUFFER_DECODER:
  108. {
  109. return parser.parseFrom(
  110. CodedInputStream.newInstance(bytes.asReadOnlyByteBuffer()), extensions);
  111. }
  112. case DIRECT_BYTE_BUFFER_DECODER:
  113. {
  114. ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.size());
  115. bytes.copyTo(buffer);
  116. buffer.flip();
  117. return parser.parseFrom(CodedInputStream.newInstance(buffer), extensions);
  118. }
  119. case READONLY_DIRECT_BYTE_BUFFER_DECODER:
  120. {
  121. ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.size());
  122. bytes.copyTo(buffer);
  123. buffer.flip();
  124. return parser.parseFrom(
  125. CodedInputStream.newInstance(buffer.asReadOnlyBuffer()), extensions);
  126. }
  127. case INPUT_STREAM_DECODER:
  128. {
  129. return parser.parseFrom(bytes.newInput(), extensions);
  130. }
  131. }
  132. return null;
  133. }
  134. }
  135. private <T extends AbstractMessage> T parseBinary(
  136. ByteString bytes, Parser<T> parser, ExtensionRegistry extensions)
  137. throws InvalidProtocolBufferException {
  138. ArrayList<T> messages = new ArrayList<>();
  139. ArrayList<InvalidProtocolBufferException> exceptions = new ArrayList<>();
  140. for (int i = 0; i < BinaryDecoderType.values().length; i++) {
  141. messages.add(null);
  142. exceptions.add(null);
  143. }
  144. if (messages.isEmpty()) {
  145. throw new RuntimeException("binary decoder types missing");
  146. }
  147. BinaryDecoder<T> decoder = new BinaryDecoder<>();
  148. boolean hasMessage = false;
  149. boolean hasException = false;
  150. for (int i = 0; i < BinaryDecoderType.values().length; ++i) {
  151. try {
  152. // = BinaryDecoderType.values()[i].parseProto3(bytes);
  153. messages.set(i, decoder.decode(bytes, BinaryDecoderType.values()[i], parser, extensions));
  154. hasMessage = true;
  155. } catch (InvalidProtocolBufferException e) {
  156. exceptions.set(i, e);
  157. hasException = true;
  158. }
  159. }
  160. if (hasMessage && hasException) {
  161. StringBuilder sb =
  162. new StringBuilder("Binary decoders disagreed on whether the payload was valid.\n");
  163. for (int i = 0; i < BinaryDecoderType.values().length; ++i) {
  164. sb.append(BinaryDecoderType.values()[i].name());
  165. if (messages.get(i) != null) {
  166. sb.append(" accepted the payload.\n");
  167. } else {
  168. sb.append(" rejected the payload.\n");
  169. }
  170. }
  171. throw new RuntimeException(sb.toString());
  172. }
  173. if (hasException) {
  174. // We do not check if exceptions are equal. Different implementations may return different
  175. // exception messages. Throw an arbitrary one out instead.
  176. InvalidProtocolBufferException exception = null;
  177. for (InvalidProtocolBufferException e : exceptions) {
  178. if (exception != null) {
  179. exception.addSuppressed(e);
  180. } else {
  181. exception = e;
  182. }
  183. }
  184. throw exception;
  185. }
  186. // Fast path comparing all the messages with the first message, assuming equality being
  187. // symmetric and transitive.
  188. boolean allEqual = true;
  189. for (int i = 1; i < messages.size(); ++i) {
  190. if (!messages.get(0).equals(messages.get(i))) {
  191. allEqual = false;
  192. break;
  193. }
  194. }
  195. // Slow path: compare and find out all unequal pairs.
  196. if (!allEqual) {
  197. StringBuilder sb = new StringBuilder();
  198. for (int i = 0; i < messages.size() - 1; ++i) {
  199. for (int j = i + 1; j < messages.size(); ++j) {
  200. if (!messages.get(i).equals(messages.get(j))) {
  201. sb.append(BinaryDecoderType.values()[i].name())
  202. .append(" and ")
  203. .append(BinaryDecoderType.values()[j].name())
  204. .append(" parsed the payload differently.\n");
  205. }
  206. }
  207. }
  208. throw new RuntimeException(sb.toString());
  209. }
  210. return messages.get(0);
  211. }
  212. private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest request) {
  213. AbstractMessage testMessage;
  214. String messageType = request.getMessageType();
  215. boolean isProto3 =
  216. messageType.equals("protobuf_test_messages.proto3.TestAllTypesProto3");
  217. boolean isProto2 =
  218. messageType.equals("protobuf_test_messages.proto2.TestAllTypesProto2");
  219. switch (request.getPayloadCase()) {
  220. case PROTOBUF_PAYLOAD:
  221. {
  222. if (isProto3) {
  223. try {
  224. ExtensionRegistry extensions = ExtensionRegistry.newInstance();
  225. TestMessagesProto3.registerAllExtensions(extensions);
  226. testMessage =
  227. parseBinary(
  228. request.getProtobufPayload(), TestAllTypesProto3.parser(), extensions);
  229. } catch (InvalidProtocolBufferException e) {
  230. return Conformance.ConformanceResponse.newBuilder()
  231. .setParseError(e.getMessage())
  232. .build();
  233. }
  234. } else if (isProto2) {
  235. try {
  236. ExtensionRegistry extensions = ExtensionRegistry.newInstance();
  237. TestMessagesProto2.registerAllExtensions(extensions);
  238. testMessage =
  239. parseBinary(
  240. request.getProtobufPayload(), TestAllTypesProto2.parser(), extensions);
  241. } catch (InvalidProtocolBufferException e) {
  242. return Conformance.ConformanceResponse.newBuilder()
  243. .setParseError(e.getMessage())
  244. .build();
  245. }
  246. } else {
  247. throw new IllegalArgumentException(
  248. "Protobuf request has unexpected payload type: " + messageType);
  249. }
  250. break;
  251. }
  252. case JSON_PAYLOAD:
  253. {
  254. try {
  255. JsonFormat.Parser parser = JsonFormat.parser().usingTypeRegistry(typeRegistry);
  256. if (request.getTestCategory()
  257. == Conformance.TestCategory.JSON_IGNORE_UNKNOWN_PARSING_TEST) {
  258. parser = parser.ignoringUnknownFields();
  259. }
  260. if (isProto3) {
  261. TestMessagesProto3.TestAllTypesProto3.Builder builder =
  262. TestMessagesProto3.TestAllTypesProto3.newBuilder();
  263. parser.merge(request.getJsonPayload(), builder);
  264. testMessage = builder.build();
  265. } else if (isProto2) {
  266. TestMessagesProto2.TestAllTypesProto2.Builder builder =
  267. TestMessagesProto2.TestAllTypesProto2.newBuilder();
  268. parser.merge(request.getJsonPayload(), builder);
  269. testMessage = builder.build();
  270. } else {
  271. throw new IllegalArgumentException(
  272. "Protobuf request has unexpected payload type: " + messageType);
  273. }
  274. } catch (InvalidProtocolBufferException e) {
  275. return Conformance.ConformanceResponse.newBuilder()
  276. .setParseError(e.getMessage())
  277. .build();
  278. }
  279. break;
  280. }
  281. case TEXT_PAYLOAD:
  282. {
  283. if (isProto3) {
  284. try {
  285. TestMessagesProto3.TestAllTypesProto3.Builder builder =
  286. TestMessagesProto3.TestAllTypesProto3.newBuilder();
  287. TextFormat.merge(request.getTextPayload(), builder);
  288. testMessage = builder.build();
  289. } catch (TextFormat.ParseException e) {
  290. return Conformance.ConformanceResponse.newBuilder()
  291. .setParseError(e.getMessage())
  292. .build();
  293. }
  294. } else if (isProto2) {
  295. try {
  296. TestMessagesProto2.TestAllTypesProto2.Builder builder =
  297. TestMessagesProto2.TestAllTypesProto2.newBuilder();
  298. TextFormat.merge(request.getTextPayload(), builder);
  299. testMessage = builder.build();
  300. } catch (TextFormat.ParseException e) {
  301. return Conformance.ConformanceResponse.newBuilder()
  302. .setParseError(e.getMessage())
  303. .build();
  304. }
  305. } else {
  306. throw new IllegalArgumentException(
  307. "Protobuf request has unexpected payload type: " + messageType);
  308. }
  309. break;
  310. }
  311. case PAYLOAD_NOT_SET:
  312. {
  313. throw new IllegalArgumentException("Request didn't have payload.");
  314. }
  315. default:
  316. {
  317. throw new IllegalArgumentException("Unexpected payload case.");
  318. }
  319. }
  320. switch (request.getRequestedOutputFormat()) {
  321. case UNSPECIFIED:
  322. throw new IllegalArgumentException("Unspecified output format.");
  323. case PROTOBUF:
  324. {
  325. ByteString messageString = testMessage.toByteString();
  326. return Conformance.ConformanceResponse.newBuilder()
  327. .setProtobufPayload(messageString)
  328. .build();
  329. }
  330. case JSON:
  331. try {
  332. return Conformance.ConformanceResponse.newBuilder()
  333. .setJsonPayload(
  334. JsonFormat.printer().usingTypeRegistry(typeRegistry).print(testMessage))
  335. .build();
  336. } catch (InvalidProtocolBufferException | IllegalArgumentException e) {
  337. return Conformance.ConformanceResponse.newBuilder()
  338. .setSerializeError(e.getMessage())
  339. .build();
  340. }
  341. case TEXT_FORMAT:
  342. return Conformance.ConformanceResponse.newBuilder()
  343. .setTextPayload(TextFormat.printer().printToString(testMessage))
  344. .build();
  345. default:
  346. {
  347. throw new IllegalArgumentException("Unexpected request output.");
  348. }
  349. }
  350. }
  351. private boolean doTestIo() throws Exception {
  352. int bytes = readLittleEndianIntFromStdin();
  353. if (bytes == -1) {
  354. return false; // EOF
  355. }
  356. byte[] serializedInput = new byte[bytes];
  357. if (!readFromStdin(serializedInput, bytes)) {
  358. throw new RuntimeException("Unexpected EOF from test program.");
  359. }
  360. Conformance.ConformanceRequest request =
  361. Conformance.ConformanceRequest.parseFrom(serializedInput);
  362. Conformance.ConformanceResponse response = doTest(request);
  363. byte[] serializedOutput = response.toByteArray();
  364. writeLittleEndianIntToStdout(serializedOutput.length);
  365. writeToStdout(serializedOutput);
  366. return true;
  367. }
  368. public void run() throws Exception {
  369. typeRegistry =
  370. TypeRegistry.newBuilder()
  371. .add(TestMessagesProto3.TestAllTypesProto3.getDescriptor())
  372. .build();
  373. while (doTestIo()) {
  374. this.testCount++;
  375. }
  376. System.err.println(
  377. "ConformanceJava: received EOF from test runner after " + this.testCount + " tests");
  378. }
  379. public static void main(String[] args) throws Exception {
  380. new ConformanceJava().run();
  381. }
  382. }