ConformanceJava.java 17 KB

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