conformance_nodejs.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. switch (request.getPayloadCase()) {
  45. case conformance.ConformanceRequest.PayloadCase.PROTOBUF_PAYLOAD: {
  46. if (request.getMessageType() == "protobuf_test_messages.proto3.TestAllTypesProto3") {
  47. try {
  48. testMessage = test_messages_proto3.TestAllTypesProto3.deserializeBinary(
  49. request.getProtobufPayload());
  50. } catch (err) {
  51. response.setParseError(err.toString());
  52. return response;
  53. }
  54. } else if (request.getMessageType() == "protobuf_test_messages.proto2.TestAllTypesProto2"){
  55. try {
  56. testMessage = test_messages_proto2.TestAllTypesProto2.deserializeBinary(
  57. request.getProtobufPayload());
  58. } catch (err) {
  59. response.setParseError(err.toString());
  60. return response;
  61. }
  62. } else {
  63. throw "Protobuf request doesn\'t have specific payload type";
  64. }
  65. }
  66. case conformance.ConformanceRequest.PayloadCase.JSON_PAYLOAD:
  67. response.setSkipped("JSON not supported.");
  68. return response;
  69. case conformance.ConformanceRequest.PayloadCase.TEXT_PAYLOAD:
  70. response.setSkipped("Text format not supported.");
  71. return response;
  72. case conformance.ConformanceRequest.PayloadCase.PAYLOAD_NOT_SET:
  73. response.setRuntimeError("Request didn't have payload");
  74. return response;
  75. }
  76. switch (request.getRequestedOutputFormat()) {
  77. case conformance.WireFormat.UNSPECIFIED:
  78. response.setRuntimeError("Unspecified output format");
  79. return response;
  80. case conformance.WireFormat.PROTOBUF:
  81. response.setProtobufPayload(testMessage.serializeBinary());
  82. case conformance.WireFormat.JSON:
  83. response.setSkipped("JSON not supported.");
  84. return response;
  85. default:
  86. throw "Request didn't have requested output format";
  87. }
  88. } catch (err) {
  89. response.setRuntimeError(err.toString());
  90. }
  91. return response;
  92. }
  93. function onEof(totalRead) {
  94. if (totalRead == 0) {
  95. return undefined;
  96. } else {
  97. throw "conformance_nodejs: premature EOF on stdin.";
  98. }
  99. }
  100. // Utility function to read a buffer of N bytes.
  101. function readBuffer(bytes) {
  102. var buf = new Buffer(bytes);
  103. var totalRead = 0;
  104. while (totalRead < bytes) {
  105. var read = 0;
  106. try {
  107. read = fs.readSync(process.stdin.fd, buf, totalRead, bytes - totalRead);
  108. } catch (e) {
  109. if (e.code == 'EOF') {
  110. return onEof(totalRead)
  111. } else if (e.code == 'EAGAIN') {
  112. } else {
  113. throw "conformance_nodejs: Error reading from stdin." + e;
  114. }
  115. }
  116. totalRead += read;
  117. }
  118. return buf;
  119. }
  120. function writeBuffer(buffer) {
  121. var totalWritten = 0;
  122. while (totalWritten < buffer.length) {
  123. totalWritten += fs.writeSync(
  124. process.stdout.fd, buffer, totalWritten, buffer.length - totalWritten);
  125. }
  126. }
  127. // Returns true if the test ran successfully, false on legitimate EOF.
  128. // If EOF is encountered in an unexpected place, raises IOError.
  129. function doTestIo() {
  130. var lengthBuf = readBuffer(4);
  131. if (!lengthBuf) {
  132. return false;
  133. }
  134. var length = lengthBuf.readInt32LE(0);
  135. var serializedRequest = readBuffer(length);
  136. if (!serializedRequest) {
  137. throw "conformance_nodejs: Failed to read request.";
  138. }
  139. serializedRequest = new Uint8Array(serializedRequest);
  140. var request =
  141. conformance.ConformanceRequest.deserializeBinary(serializedRequest);
  142. var response = doTest(request);
  143. var serializedResponse = response.serializeBinary();
  144. lengthBuf = new Buffer(4);
  145. lengthBuf.writeInt32LE(serializedResponse.length, 0);
  146. writeBuffer(lengthBuf);
  147. writeBuffer(new Buffer(serializedResponse));
  148. testCount += 1
  149. return true;
  150. }
  151. while (true) {
  152. if (!doTestIo()) {
  153. console.error('conformance_nodejs: received EOF from test runner ' +
  154. "after " + testCount + " tests, exiting")
  155. break;
  156. }
  157. }