ConformanceJava.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import com.google.protobuf.conformance.Conformance;
  2. import com.google.protobuf.util.JsonFormat;
  3. import com.google.protobuf.util.JsonFormat.TypeRegistry;
  4. import com.google.protobuf.InvalidProtocolBufferException;
  5. class ConformanceJava {
  6. private int testCount = 0;
  7. private TypeRegistry typeRegistry;
  8. private boolean readFromStdin(byte[] buf, int len) throws Exception {
  9. int ofs = 0;
  10. while (len > 0) {
  11. int read = System.in.read(buf, ofs, len);
  12. if (read == -1) {
  13. return false; // EOF
  14. }
  15. ofs += read;
  16. len -= read;
  17. }
  18. return true;
  19. }
  20. private void writeToStdout(byte[] buf) throws Exception {
  21. System.out.write(buf);
  22. }
  23. // Returns -1 on EOF (the actual values will always be positive).
  24. private int readLittleEndianIntFromStdin() throws Exception {
  25. byte[] buf = new byte[4];
  26. if (!readFromStdin(buf, 4)) {
  27. return -1;
  28. }
  29. return (buf[0] & 0xff)
  30. | ((buf[1] & 0xff) << 8)
  31. | ((buf[2] & 0xff) << 16)
  32. | ((buf[3] & 0xff) << 24);
  33. }
  34. private void writeLittleEndianIntToStdout(int val) throws Exception {
  35. byte[] buf = new byte[4];
  36. buf[0] = (byte)val;
  37. buf[1] = (byte)(val >> 8);
  38. buf[2] = (byte)(val >> 16);
  39. buf[3] = (byte)(val >> 24);
  40. writeToStdout(buf);
  41. }
  42. private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest request) {
  43. Conformance.TestAllTypes testMessage;
  44. switch (request.getPayloadCase()) {
  45. case PROTOBUF_PAYLOAD: {
  46. try {
  47. testMessage = Conformance.TestAllTypes.parseFrom(request.getProtobufPayload());
  48. } catch (InvalidProtocolBufferException e) {
  49. return Conformance.ConformanceResponse.newBuilder().setParseError(e.getMessage()).build();
  50. }
  51. break;
  52. }
  53. case JSON_PAYLOAD: {
  54. try {
  55. Conformance.TestAllTypes.Builder builder = Conformance.TestAllTypes.newBuilder();
  56. JsonFormat.parser().usingTypeRegistry(typeRegistry)
  57. .merge(request.getJsonPayload(), builder);
  58. testMessage = builder.build();
  59. } catch (InvalidProtocolBufferException e) {
  60. return Conformance.ConformanceResponse.newBuilder().setParseError(e.getMessage()).build();
  61. }
  62. break;
  63. }
  64. case PAYLOAD_NOT_SET: {
  65. throw new RuntimeException("Request didn't have payload.");
  66. }
  67. default: {
  68. throw new RuntimeException("Unexpected payload case.");
  69. }
  70. }
  71. switch (request.getRequestedOutputFormat()) {
  72. case UNSPECIFIED:
  73. throw new RuntimeException("Unspecified output format.");
  74. case PROTOBUF:
  75. return Conformance.ConformanceResponse.newBuilder().setProtobufPayload(testMessage.toByteString()).build();
  76. case JSON:
  77. try {
  78. return Conformance.ConformanceResponse.newBuilder().setJsonPayload(
  79. JsonFormat.printer().usingTypeRegistry(typeRegistry).print(testMessage)).build();
  80. } catch (InvalidProtocolBufferException | IllegalArgumentException e) {
  81. return Conformance.ConformanceResponse.newBuilder().setSerializeError(
  82. e.getMessage()).build();
  83. }
  84. default: {
  85. throw new RuntimeException("Unexpected request output.");
  86. }
  87. }
  88. }
  89. private boolean doTestIo() throws Exception {
  90. int bytes = readLittleEndianIntFromStdin();
  91. if (bytes == -1) {
  92. return false; // EOF
  93. }
  94. byte[] serializedInput = new byte[bytes];
  95. if (!readFromStdin(serializedInput, bytes)) {
  96. throw new RuntimeException("Unexpected EOF from test program.");
  97. }
  98. Conformance.ConformanceRequest request =
  99. Conformance.ConformanceRequest.parseFrom(serializedInput);
  100. Conformance.ConformanceResponse response = doTest(request);
  101. byte[] serializedOutput = response.toByteArray();
  102. writeLittleEndianIntToStdout(serializedOutput.length);
  103. writeToStdout(serializedOutput);
  104. return true;
  105. }
  106. public void run() throws Exception {
  107. typeRegistry = TypeRegistry.newBuilder().add(
  108. Conformance.TestAllTypes.getDescriptor()).build();
  109. while (doTestIo()) {
  110. this.testCount++;
  111. }
  112. System.err.println("ConformanceJava: received EOF from test runner after " +
  113. this.testCount + " tests");
  114. }
  115. public static void main(String[] args) throws Exception {
  116. new ConformanceJava().run();
  117. }
  118. }