ConformanceJavaLite.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. import com.google.protobuf.conformance.Conformance;
  31. import com.google.protobuf.InvalidProtocolBufferException;
  32. class ConformanceJavaLite {
  33. private int testCount = 0;
  34. private boolean readFromStdin(byte[] buf, int len) throws Exception {
  35. int ofs = 0;
  36. while (len > 0) {
  37. int read = System.in.read(buf, ofs, len);
  38. if (read == -1) {
  39. return false; // EOF
  40. }
  41. ofs += read;
  42. len -= read;
  43. }
  44. return true;
  45. }
  46. private void writeToStdout(byte[] buf) throws Exception {
  47. System.out.write(buf);
  48. }
  49. // Returns -1 on EOF (the actual values will always be positive).
  50. private int readLittleEndianIntFromStdin() throws Exception {
  51. byte[] buf = new byte[4];
  52. if (!readFromStdin(buf, 4)) {
  53. return -1;
  54. }
  55. return (buf[0] & 0xff)
  56. | ((buf[1] & 0xff) << 8)
  57. | ((buf[2] & 0xff) << 16)
  58. | ((buf[3] & 0xff) << 24);
  59. }
  60. private void writeLittleEndianIntToStdout(int val) throws Exception {
  61. byte[] buf = new byte[4];
  62. buf[0] = (byte)val;
  63. buf[1] = (byte)(val >> 8);
  64. buf[2] = (byte)(val >> 16);
  65. buf[3] = (byte)(val >> 24);
  66. writeToStdout(buf);
  67. }
  68. private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest request) {
  69. Conformance.TestAllTypes testMessage;
  70. switch (request.getPayloadCase()) {
  71. case PROTOBUF_PAYLOAD: {
  72. try {
  73. testMessage = Conformance.TestAllTypes.parseFrom(request.getProtobufPayload());
  74. } catch (InvalidProtocolBufferException e) {
  75. return Conformance.ConformanceResponse.newBuilder().setParseError(e.getMessage()).build();
  76. }
  77. break;
  78. }
  79. case JSON_PAYLOAD: {
  80. return Conformance.ConformanceResponse.newBuilder().setSkipped(
  81. "Lite runtime does not support JSON format.").build();
  82. }
  83. case PAYLOAD_NOT_SET: {
  84. throw new RuntimeException("Request didn't have payload.");
  85. }
  86. default: {
  87. throw new RuntimeException("Unexpected payload case.");
  88. }
  89. }
  90. switch (request.getRequestedOutputFormat()) {
  91. case UNSPECIFIED:
  92. throw new RuntimeException("Unspecified output format.");
  93. case PROTOBUF:
  94. return Conformance.ConformanceResponse.newBuilder().setProtobufPayload(testMessage.toByteString()).build();
  95. case JSON:
  96. return Conformance.ConformanceResponse.newBuilder().setSkipped(
  97. "Lite runtime does not support JSON format.").build();
  98. default: {
  99. throw new RuntimeException("Unexpected request output.");
  100. }
  101. }
  102. }
  103. private boolean doTestIo() throws Exception {
  104. int bytes = readLittleEndianIntFromStdin();
  105. if (bytes == -1) {
  106. return false; // EOF
  107. }
  108. byte[] serializedInput = new byte[bytes];
  109. if (!readFromStdin(serializedInput, bytes)) {
  110. throw new RuntimeException("Unexpected EOF from test program.");
  111. }
  112. Conformance.ConformanceRequest request =
  113. Conformance.ConformanceRequest.parseFrom(serializedInput);
  114. Conformance.ConformanceResponse response = doTest(request);
  115. byte[] serializedOutput = response.toByteArray();
  116. writeLittleEndianIntToStdout(serializedOutput.length);
  117. writeToStdout(serializedOutput);
  118. return true;
  119. }
  120. public void run() throws Exception {
  121. while (doTestIo()) {
  122. this.testCount++;
  123. }
  124. System.err.println("ConformanceJavaLite: received EOF from test runner after " +
  125. this.testCount + " tests");
  126. }
  127. public static void main(String[] args) throws Exception {
  128. new ConformanceJavaLite().run();
  129. }
  130. }