ConformanceJavaLite.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import com.google.protobuf.conformance.Conformance;
  2. import com.google.protobuf.InvalidProtocolBufferException;
  3. class ConformanceJavaLite {
  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] & 0xff)
  27. | ((buf[1] & 0xff) << 8)
  28. | ((buf[2] & 0xff) << 16)
  29. | ((buf[3] & 0xff) << 24);
  30. }
  31. private void writeLittleEndianIntToStdout(int val) throws Exception {
  32. byte[] buf = new byte[4];
  33. buf[0] = (byte)val;
  34. buf[1] = (byte)(val >> 8);
  35. buf[2] = (byte)(val >> 16);
  36. buf[3] = (byte)(val >> 24);
  37. writeToStdout(buf);
  38. }
  39. private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest request) {
  40. Conformance.TestAllTypes testMessage;
  41. switch (request.getPayloadCase()) {
  42. case PROTOBUF_PAYLOAD: {
  43. try {
  44. testMessage = Conformance.TestAllTypes.parseFrom(request.getProtobufPayload());
  45. } catch (InvalidProtocolBufferException e) {
  46. return Conformance.ConformanceResponse.newBuilder().setParseError(e.getMessage()).build();
  47. }
  48. break;
  49. }
  50. case JSON_PAYLOAD: {
  51. return Conformance.ConformanceResponse.newBuilder().setSkipped(
  52. "Lite runtime does not support JSON format.").build();
  53. }
  54. case PAYLOAD_NOT_SET: {
  55. throw new RuntimeException("Request didn't have payload.");
  56. }
  57. default: {
  58. throw new RuntimeException("Unexpected payload case.");
  59. }
  60. }
  61. switch (request.getRequestedOutputFormat()) {
  62. case UNSPECIFIED:
  63. throw new RuntimeException("Unspecified output format.");
  64. case PROTOBUF:
  65. return Conformance.ConformanceResponse.newBuilder().setProtobufPayload(testMessage.toByteString()).build();
  66. case JSON:
  67. return Conformance.ConformanceResponse.newBuilder().setSkipped(
  68. "Lite runtime does not support JSON format.").build();
  69. default: {
  70. throw new RuntimeException("Unexpected request output.");
  71. }
  72. }
  73. }
  74. private boolean doTestIo() throws Exception {
  75. int bytes = readLittleEndianIntFromStdin();
  76. if (bytes == -1) {
  77. return false; // EOF
  78. }
  79. byte[] serializedInput = new byte[bytes];
  80. if (!readFromStdin(serializedInput, bytes)) {
  81. throw new RuntimeException("Unexpected EOF from test program.");
  82. }
  83. Conformance.ConformanceRequest request =
  84. Conformance.ConformanceRequest.parseFrom(serializedInput);
  85. Conformance.ConformanceResponse response = doTest(request);
  86. byte[] serializedOutput = response.toByteArray();
  87. writeLittleEndianIntToStdout(serializedOutput.length);
  88. writeToStdout(serializedOutput);
  89. return true;
  90. }
  91. public void run() throws Exception {
  92. while (doTestIo()) {
  93. this.testCount++;
  94. }
  95. System.err.println("ConformanceJavaLite: received EOF from test runner after " +
  96. this.testCount + " tests");
  97. }
  98. public static void main(String[] args) throws Exception {
  99. new ConformanceJavaLite().run();
  100. }
  101. }