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