conformance_nodejs.js 6.0 KB

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