ConformanceJava.java 3.3 KB

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