ConformanceJava.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. com.google.protobuf.AbstractMessage testMessage;
  214. boolean isProto3 =
  215. request.getMessageType().equals("protobuf_test_messages.proto3.TestAllTypesProto3");
  216. boolean isProto2 =
  217. request.getMessageType().equals("protobuf_test_messages.proto2.TestAllTypesProto2");
  218. switch (request.getPayloadCase()) {
  219. case PROTOBUF_PAYLOAD:
  220. {
  221. if (isProto3) {
  222. try {
  223. ExtensionRegistry extensions = ExtensionRegistry.newInstance();
  224. TestMessagesProto3.registerAllExtensions(extensions);
  225. testMessage =
  226. parseBinary(
  227. request.getProtobufPayload(), TestAllTypesProto3.parser(), extensions);
  228. } catch (InvalidProtocolBufferException e) {
  229. return Conformance.ConformanceResponse.newBuilder()
  230. .setParseError(e.getMessage())
  231. .build();
  232. }
  233. } else if (isProto2) {
  234. try {
  235. ExtensionRegistry extensions = ExtensionRegistry.newInstance();
  236. TestMessagesProto2.registerAllExtensions(extensions);
  237. testMessage =
  238. parseBinary(
  239. request.getProtobufPayload(), TestAllTypesProto2.parser(), extensions);
  240. } catch (InvalidProtocolBufferException e) {
  241. return Conformance.ConformanceResponse.newBuilder()
  242. .setParseError(e.getMessage())
  243. .build();
  244. }
  245. } else {
  246. throw new RuntimeException("Protobuf request doesn't have specific payload type.");
  247. }
  248. break;
  249. }
  250. case JSON_PAYLOAD:
  251. {
  252. try {
  253. JsonFormat.Parser parser = JsonFormat.parser().usingTypeRegistry(typeRegistry);
  254. if (request.getTestCategory()
  255. == Conformance.TestCategory.JSON_IGNORE_UNKNOWN_PARSING_TEST) {
  256. parser = parser.ignoringUnknownFields();
  257. }
  258. if (isProto3) {
  259. TestMessagesProto3.TestAllTypesProto3.Builder builder =
  260. TestMessagesProto3.TestAllTypesProto3.newBuilder();
  261. parser.merge(request.getJsonPayload(), builder);
  262. testMessage = builder.build();
  263. } else if (isProto2) {
  264. TestMessagesProto2.TestAllTypesProto2.Builder builder =
  265. TestMessagesProto2.TestAllTypesProto2.newBuilder();
  266. parser.merge(request.getJsonPayload(), builder);
  267. testMessage = builder.build();
  268. } else {
  269. throw new RuntimeException("Protobuf request doesn't have specific payload type.");
  270. }
  271. } catch (InvalidProtocolBufferException e) {
  272. return Conformance.ConformanceResponse.newBuilder()
  273. .setParseError(e.getMessage())
  274. .build();
  275. }
  276. break;
  277. }
  278. case TEXT_PAYLOAD:
  279. {
  280. if (isProto3) {
  281. try {
  282. TestMessagesProto3.TestAllTypesProto3.Builder builder =
  283. TestMessagesProto3.TestAllTypesProto3.newBuilder();
  284. TextFormat.merge(request.getTextPayload(), builder);
  285. testMessage = builder.build();
  286. } catch (TextFormat.ParseException e) {
  287. return Conformance.ConformanceResponse.newBuilder()
  288. .setParseError(e.getMessage())
  289. .build();
  290. }
  291. } else if (isProto2) {
  292. try {
  293. TestMessagesProto2.TestAllTypesProto2.Builder builder =
  294. TestMessagesProto2.TestAllTypesProto2.newBuilder();
  295. TextFormat.merge(request.getTextPayload(), builder);
  296. testMessage = builder.build();
  297. } catch (TextFormat.ParseException e) {
  298. return Conformance.ConformanceResponse.newBuilder()
  299. .setParseError(e.getMessage())
  300. .build();
  301. }
  302. } else {
  303. throw new RuntimeException("Protobuf request doesn't have specific payload type.");
  304. }
  305. break;
  306. }
  307. case PAYLOAD_NOT_SET:
  308. {
  309. throw new RuntimeException("Request didn't have payload.");
  310. }
  311. default:
  312. {
  313. throw new RuntimeException("Unexpected payload case.");
  314. }
  315. }
  316. switch (request.getRequestedOutputFormat()) {
  317. case UNSPECIFIED:
  318. throw new RuntimeException("Unspecified output format.");
  319. case PROTOBUF:
  320. {
  321. ByteString messageString = testMessage.toByteString();
  322. return Conformance.ConformanceResponse.newBuilder()
  323. .setProtobufPayload(messageString)
  324. .build();
  325. }
  326. case JSON:
  327. try {
  328. return Conformance.ConformanceResponse.newBuilder()
  329. .setJsonPayload(
  330. JsonFormat.printer().usingTypeRegistry(typeRegistry).print(testMessage))
  331. .build();
  332. } catch (InvalidProtocolBufferException | IllegalArgumentException e) {
  333. return Conformance.ConformanceResponse.newBuilder()
  334. .setSerializeError(e.getMessage())
  335. .build();
  336. }
  337. case TEXT_FORMAT:
  338. return Conformance.ConformanceResponse.newBuilder()
  339. .setTextPayload(TextFormat.printer().printToString(testMessage))
  340. .build();
  341. default:
  342. {
  343. throw new RuntimeException("Unexpected request output.");
  344. }
  345. }
  346. }
  347. private boolean doTestIo() throws Exception {
  348. int bytes = readLittleEndianIntFromStdin();
  349. if (bytes == -1) {
  350. return false; // EOF
  351. }
  352. byte[] serializedInput = new byte[bytes];
  353. if (!readFromStdin(serializedInput, bytes)) {
  354. throw new RuntimeException("Unexpected EOF from test program.");
  355. }
  356. Conformance.ConformanceRequest request =
  357. Conformance.ConformanceRequest.parseFrom(serializedInput);
  358. Conformance.ConformanceResponse response = doTest(request);
  359. byte[] serializedOutput = response.toByteArray();
  360. writeLittleEndianIntToStdout(serializedOutput.length);
  361. writeToStdout(serializedOutput);
  362. return true;
  363. }
  364. public void run() throws Exception {
  365. typeRegistry =
  366. TypeRegistry.newBuilder()
  367. .add(TestMessagesProto3.TestAllTypesProto3.getDescriptor())
  368. .build();
  369. while (doTestIo()) {
  370. this.testCount++;
  371. }
  372. System.err.println(
  373. "ConformanceJava: received EOF from test runner after " + this.testCount + " tests");
  374. }
  375. public static void main(String[] args) throws Exception {
  376. new ConformanceJava().run();
  377. }
  378. }