conformance_nodejs.js 5.5 KB

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