conformance_nodejs.js 5.0 KB

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