conformance_nodejs.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env node
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. var conformance = require('conformance_pb');
  32. var test_messages_proto3 = require('google/protobuf/test_messages_proto3_pb');
  33. var test_messages_proto2 = require('google/protobuf/test_messages_proto2_pb');
  34. var fs = require('fs');
  35. var testCount = 0;
  36. function doTest(request) {
  37. var testMessage;
  38. var response = new conformance.ConformanceResponse();
  39. try {
  40. if (request.getRequestedOutputFormat() == conformance.WireFormat.JSON) {
  41. response.setSkipped("JSON not supported.");
  42. return response;
  43. }
  44. if (request.getRequestedOutputFormat() ==
  45. conformance.WireFormat.TEXT_FORMAT) {
  46. response.setSkipped('Text format is not supported as output format.');
  47. return response;
  48. }
  49. switch (request.getPayloadCase()) {
  50. case conformance.ConformanceRequest.PayloadCase.PROTOBUF_PAYLOAD: {
  51. if (request.getMessageType() == "protobuf_test_messages.proto3.TestAllTypesProto3") {
  52. try {
  53. testMessage = test_messages_proto3.TestAllTypesProto3.deserializeBinary(
  54. request.getProtobufPayload());
  55. } catch (err) {
  56. response.setParseError(err.toString());
  57. return response;
  58. }
  59. } else if (request.getMessageType() == "protobuf_test_messages.proto2.TestAllTypesProto2"){
  60. try {
  61. testMessage = test_messages_proto2.TestAllTypesProto2.deserializeBinary(
  62. request.getProtobufPayload());
  63. } catch (err) {
  64. response.setParseError(err.toString());
  65. return response;
  66. }
  67. } else {
  68. throw "Protobuf request doesn\'t have specific payload type";
  69. }
  70. } break;
  71. case conformance.ConformanceRequest.PayloadCase.JSON_PAYLOAD:
  72. response.setSkipped("JSON not supported.");
  73. return response;
  74. case conformance.ConformanceRequest.PayloadCase.TEXT_PAYLOAD:
  75. response.setSkipped("Text format not supported.");
  76. return response;
  77. case conformance.ConformanceRequest.PayloadCase.PAYLOAD_NOT_SET:
  78. response.setRuntimeError("Request didn't have payload");
  79. return response;
  80. }
  81. switch (request.getRequestedOutputFormat()) {
  82. case conformance.WireFormat.UNSPECIFIED:
  83. response.setRuntimeError("Unspecified output format");
  84. return response;
  85. case conformance.WireFormat.PROTOBUF:
  86. response.setProtobufPayload(testMessage.serializeBinary());
  87. case conformance.WireFormat.JSON:
  88. response.setSkipped("JSON not supported.");
  89. return response;
  90. default:
  91. throw "Request didn't have requested output format";
  92. }
  93. } catch (err) {
  94. response.setRuntimeError(err.toString());
  95. }
  96. return response;
  97. }
  98. function onEof(totalRead) {
  99. if (totalRead == 0) {
  100. return undefined;
  101. } else {
  102. throw "conformance_nodejs: premature EOF on stdin.";
  103. }
  104. }
  105. // Utility function to read a buffer of N bytes.
  106. function readBuffer(bytes) {
  107. var buf = new Buffer(bytes);
  108. var totalRead = 0;
  109. while (totalRead < bytes) {
  110. var read = 0;
  111. try {
  112. read = fs.readSync(process.stdin.fd, buf, totalRead, bytes - totalRead);
  113. } catch (e) {
  114. if (e.code == 'EOF') {
  115. return onEof(totalRead)
  116. } else if (e.code == 'EAGAIN') {
  117. } else {
  118. throw "conformance_nodejs: Error reading from stdin." + e;
  119. }
  120. }
  121. totalRead += read;
  122. }
  123. return buf;
  124. }
  125. function writeBuffer(buffer) {
  126. var totalWritten = 0;
  127. while (totalWritten < buffer.length) {
  128. totalWritten += fs.writeSync(
  129. process.stdout.fd, buffer, totalWritten, buffer.length - totalWritten);
  130. }
  131. }
  132. // Returns true if the test ran successfully, false on legitimate EOF.
  133. // If EOF is encountered in an unexpected place, raises IOError.
  134. function doTestIo() {
  135. var lengthBuf = readBuffer(4);
  136. if (!lengthBuf) {
  137. return false;
  138. }
  139. var length = lengthBuf.readInt32LE(0);
  140. var serializedRequest = readBuffer(length);
  141. if (!serializedRequest) {
  142. throw "conformance_nodejs: Failed to read request.";
  143. }
  144. serializedRequest = new Uint8Array(serializedRequest);
  145. var request =
  146. conformance.ConformanceRequest.deserializeBinary(serializedRequest);
  147. var response = doTest(request);
  148. var serializedResponse = response.serializeBinary();
  149. lengthBuf = new Buffer(4);
  150. lengthBuf.writeInt32LE(serializedResponse.length, 0);
  151. writeBuffer(lengthBuf);
  152. writeBuffer(new Buffer(serializedResponse));
  153. testCount += 1
  154. return true;
  155. }
  156. while (true) {
  157. if (!doTestIo()) {
  158. console.error('conformance_nodejs: received EOF from test runner ' +
  159. "after " + testCount + " tests, exiting")
  160. break;
  161. }
  162. }