ConformanceJava.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. import com.google.protobuf.ByteString;
  2. import com.google.protobuf.CodedInputStream;
  3. import com.google.protobuf.InvalidProtocolBufferException;
  4. import com.google.protobuf.conformance.Conformance;
  5. import com.google.protobuf.util.JsonFormat.TypeRegistry;
  6. import com.google.protobuf.util.JsonFormat;
  7. import java.io.IOException;
  8. import java.nio.ByteBuffer;
  9. class ConformanceJava {
  10. private int testCount = 0;
  11. private TypeRegistry typeRegistry;
  12. private boolean readFromStdin(byte[] buf, int len) throws Exception {
  13. int ofs = 0;
  14. while (len > 0) {
  15. int read = System.in.read(buf, ofs, len);
  16. if (read == -1) {
  17. return false; // EOF
  18. }
  19. ofs += read;
  20. len -= read;
  21. }
  22. return true;
  23. }
  24. private void writeToStdout(byte[] buf) throws Exception {
  25. System.out.write(buf);
  26. }
  27. // Returns -1 on EOF (the actual values will always be positive).
  28. private int readLittleEndianIntFromStdin() throws Exception {
  29. byte[] buf = new byte[4];
  30. if (!readFromStdin(buf, 4)) {
  31. return -1;
  32. }
  33. return (buf[0] & 0xff)
  34. | ((buf[1] & 0xff) << 8)
  35. | ((buf[2] & 0xff) << 16)
  36. | ((buf[3] & 0xff) << 24);
  37. }
  38. private void writeLittleEndianIntToStdout(int val) throws Exception {
  39. byte[] buf = new byte[4];
  40. buf[0] = (byte)val;
  41. buf[1] = (byte)(val >> 8);
  42. buf[2] = (byte)(val >> 16);
  43. buf[3] = (byte)(val >> 24);
  44. writeToStdout(buf);
  45. }
  46. private enum BinaryDecoder {
  47. BYTE_STRING_DECODER() {
  48. @Override
  49. public Conformance.TestAllTypes parse(ByteString bytes)
  50. throws InvalidProtocolBufferException {
  51. return Conformance.TestAllTypes.parseFrom(bytes);
  52. }
  53. },
  54. BYTE_ARRAY_DECODER() {
  55. @Override
  56. public Conformance.TestAllTypes parse(ByteString bytes)
  57. throws InvalidProtocolBufferException {
  58. return Conformance.TestAllTypes.parseFrom(bytes.toByteArray());
  59. }
  60. },
  61. ARRAY_BYTE_BUFFER_DECODER() {
  62. @Override
  63. public Conformance.TestAllTypes parse(ByteString bytes)
  64. throws InvalidProtocolBufferException {
  65. ByteBuffer buffer = ByteBuffer.allocate(bytes.size());
  66. bytes.copyTo(buffer);
  67. buffer.flip();
  68. try {
  69. return Conformance.TestAllTypes.parseFrom(CodedInputStream.newInstance(buffer));
  70. } catch (InvalidProtocolBufferException e) {
  71. throw e;
  72. } catch (IOException e) {
  73. throw new RuntimeException(
  74. "ByteString based ByteBuffer should not throw IOException.", e);
  75. }
  76. }
  77. },
  78. READONLY_ARRAY_BYTE_BUFFER_DECODER() {
  79. @Override
  80. public Conformance.TestAllTypes parse(ByteString bytes)
  81. throws InvalidProtocolBufferException {
  82. try {
  83. return Conformance.TestAllTypes.parseFrom(
  84. CodedInputStream.newInstance(bytes.asReadOnlyByteBuffer()));
  85. } catch (InvalidProtocolBufferException e) {
  86. throw e;
  87. } catch (IOException e) {
  88. throw new RuntimeException(
  89. "ByteString based ByteBuffer should not throw IOException.", e);
  90. }
  91. }
  92. },
  93. DIRECT_BYTE_BUFFER_DECODER() {
  94. @Override
  95. public Conformance.TestAllTypes parse(ByteString bytes)
  96. throws InvalidProtocolBufferException {
  97. ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.size());
  98. bytes.copyTo(buffer);
  99. buffer.flip();
  100. try {
  101. return Conformance.TestAllTypes.parseFrom(CodedInputStream.newInstance(buffer));
  102. } catch (InvalidProtocolBufferException e) {
  103. throw e;
  104. } catch (IOException e) {
  105. throw new RuntimeException(
  106. "ByteString based ByteBuffer should not throw IOException.", e);
  107. }
  108. }
  109. },
  110. READONLY_DIRECT_BYTE_BUFFER_DECODER() {
  111. @Override
  112. public Conformance.TestAllTypes parse(ByteString bytes)
  113. throws InvalidProtocolBufferException {
  114. ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.size());
  115. bytes.copyTo(buffer);
  116. buffer.flip();
  117. try {
  118. return Conformance.TestAllTypes.parseFrom(
  119. CodedInputStream.newInstance(buffer.asReadOnlyBuffer()));
  120. } catch (InvalidProtocolBufferException e) {
  121. throw e;
  122. } catch (IOException e) {
  123. throw new RuntimeException(
  124. "ByteString based ByteBuffer should not throw IOException.", e);
  125. }
  126. }
  127. },
  128. INPUT_STREAM_DECODER() {
  129. @Override
  130. public Conformance.TestAllTypes parse(ByteString bytes)
  131. throws InvalidProtocolBufferException {
  132. try {
  133. return Conformance.TestAllTypes.parseFrom(bytes.newInput());
  134. } catch (InvalidProtocolBufferException e) {
  135. throw e;
  136. } catch (IOException e) {
  137. throw new RuntimeException(
  138. "ByteString based InputStream should not throw IOException.", e);
  139. }
  140. }
  141. };
  142. public abstract Conformance.TestAllTypes parse(ByteString bytes)
  143. throws InvalidProtocolBufferException;
  144. }
  145. private Conformance.TestAllTypes parseBinary(ByteString bytes)
  146. throws InvalidProtocolBufferException {
  147. Conformance.TestAllTypes[] messages =
  148. new Conformance.TestAllTypes[BinaryDecoder.values().length];
  149. InvalidProtocolBufferException[] exceptions =
  150. new InvalidProtocolBufferException[BinaryDecoder.values().length];
  151. boolean hasMessage = false;
  152. boolean hasException = false;
  153. for (int i = 0; i < BinaryDecoder.values().length; ++i) {
  154. try {
  155. messages[i] = BinaryDecoder.values()[i].parse(bytes);
  156. hasMessage = true;
  157. } catch (InvalidProtocolBufferException e) {
  158. exceptions[i] = e;
  159. hasException = true;
  160. }
  161. }
  162. if (hasMessage && hasException) {
  163. StringBuilder sb =
  164. new StringBuilder("Binary decoders disagreed on whether the payload was valid.\n");
  165. for (int i = 0; i < BinaryDecoder.values().length; ++i) {
  166. sb.append(BinaryDecoder.values()[i].name());
  167. if (messages[i] != null) {
  168. sb.append(" accepted the payload.\n");
  169. } else {
  170. sb.append(" rejected the payload.\n");
  171. }
  172. }
  173. throw new RuntimeException(sb.toString());
  174. }
  175. if (hasException) {
  176. // We do not check if exceptions are equal. Different implementations may return different
  177. // exception messages. Throw an arbitrary one out instead.
  178. throw exceptions[0];
  179. }
  180. // Fast path comparing all the messages with the first message, assuming equality being
  181. // symmetric and transitive.
  182. boolean allEqual = true;
  183. for (int i = 1; i < messages.length; ++i) {
  184. if (!messages[0].equals(messages[i])) {
  185. allEqual = false;
  186. break;
  187. }
  188. }
  189. // Slow path: compare and find out all unequal pairs.
  190. if (!allEqual) {
  191. StringBuilder sb = new StringBuilder();
  192. for (int i = 0; i < messages.length - 1; ++i) {
  193. for (int j = i + 1; j < messages.length; ++j) {
  194. if (!messages[i].equals(messages[j])) {
  195. sb.append(BinaryDecoder.values()[i].name())
  196. .append(" and ")
  197. .append(BinaryDecoder.values()[j].name())
  198. .append(" parsed the payload differently.\n");
  199. }
  200. }
  201. }
  202. throw new RuntimeException(sb.toString());
  203. }
  204. return messages[0];
  205. }
  206. private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest request) {
  207. Conformance.TestAllTypes testMessage;
  208. switch (request.getPayloadCase()) {
  209. case PROTOBUF_PAYLOAD: {
  210. try {
  211. testMessage = parseBinary(request.getProtobufPayload());
  212. } catch (InvalidProtocolBufferException e) {
  213. return Conformance.ConformanceResponse.newBuilder().setParseError(e.getMessage()).build();
  214. }
  215. break;
  216. }
  217. case JSON_PAYLOAD: {
  218. try {
  219. Conformance.TestAllTypes.Builder builder = Conformance.TestAllTypes.newBuilder();
  220. JsonFormat.parser().usingTypeRegistry(typeRegistry)
  221. .merge(request.getJsonPayload(), builder);
  222. testMessage = builder.build();
  223. } catch (InvalidProtocolBufferException e) {
  224. return Conformance.ConformanceResponse.newBuilder().setParseError(e.getMessage()).build();
  225. }
  226. break;
  227. }
  228. case PAYLOAD_NOT_SET: {
  229. throw new RuntimeException("Request didn't have payload.");
  230. }
  231. default: {
  232. throw new RuntimeException("Unexpected payload case.");
  233. }
  234. }
  235. switch (request.getRequestedOutputFormat()) {
  236. case UNSPECIFIED:
  237. throw new RuntimeException("Unspecified output format.");
  238. case PROTOBUF:
  239. return Conformance.ConformanceResponse.newBuilder().setProtobufPayload(testMessage.toByteString()).build();
  240. case JSON:
  241. try {
  242. return Conformance.ConformanceResponse.newBuilder().setJsonPayload(
  243. JsonFormat.printer().usingTypeRegistry(typeRegistry).print(testMessage)).build();
  244. } catch (InvalidProtocolBufferException | IllegalArgumentException e) {
  245. return Conformance.ConformanceResponse.newBuilder().setSerializeError(
  246. e.getMessage()).build();
  247. }
  248. default: {
  249. throw new RuntimeException("Unexpected request output.");
  250. }
  251. }
  252. }
  253. private boolean doTestIo() throws Exception {
  254. int bytes = readLittleEndianIntFromStdin();
  255. if (bytes == -1) {
  256. return false; // EOF
  257. }
  258. byte[] serializedInput = new byte[bytes];
  259. if (!readFromStdin(serializedInput, bytes)) {
  260. throw new RuntimeException("Unexpected EOF from test program.");
  261. }
  262. Conformance.ConformanceRequest request =
  263. Conformance.ConformanceRequest.parseFrom(serializedInput);
  264. Conformance.ConformanceResponse response = doTest(request);
  265. byte[] serializedOutput = response.toByteArray();
  266. writeLittleEndianIntToStdout(serializedOutput.length);
  267. writeToStdout(serializedOutput);
  268. return true;
  269. }
  270. public void run() throws Exception {
  271. typeRegistry = TypeRegistry.newBuilder().add(
  272. Conformance.TestAllTypes.getDescriptor()).build();
  273. while (doTestIo()) {
  274. this.testCount++;
  275. }
  276. System.err.println("ConformanceJava: received EOF from test runner after " +
  277. this.testCount + " tests");
  278. }
  279. public static void main(String[] args) throws Exception {
  280. new ConformanceJava().run();
  281. }
  282. }