conformance_nodejs.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. while (totalRead < bytes) {
  90. var read = 0;
  91. try {
  92. read = fs.readSync(process.stdin.fd, buf, totalRead, bytes - totalRead);
  93. } catch (e) {
  94. if (e.code == 'EOF') {
  95. return onEof(totalRead)
  96. } else if (e.code == 'EAGAIN') {
  97. } else {
  98. throw "conformance_nodejs: Error reading from stdin." + e;
  99. }
  100. }
  101. totalRead += read;
  102. }
  103. return buf;
  104. }
  105. function writeBuffer(buffer) {
  106. var totalWritten = 0;
  107. while (totalWritten < buffer.length) {
  108. totalWritten += fs.writeSync(
  109. process.stdout.fd, buffer, totalWritten, buffer.length - totalWritten);
  110. }
  111. }
  112. // Returns true if the test ran successfully, false on legitimate EOF.
  113. // If EOF is encountered in an unexpected place, raises IOError.
  114. function doTestIo() {
  115. var lengthBuf = readBuffer(4);
  116. if (!lengthBuf) {
  117. return false;
  118. }
  119. var length = lengthBuf.readInt32LE(0);
  120. var serializedRequest = readBuffer(length);
  121. if (!serializedRequest) {
  122. throw "conformance_nodejs: Failed to read request.";
  123. }
  124. serializedRequest = new Uint8Array(serializedRequest);
  125. var request =
  126. conformance.ConformanceRequest.deserializeBinary(serializedRequest);
  127. var response = doTest(request);
  128. var serializedResponse = response.serializeBinary();
  129. lengthBuf = new Buffer(4);
  130. lengthBuf.writeInt32LE(serializedResponse.length, 0);
  131. writeBuffer(lengthBuf);
  132. writeBuffer(new Buffer(serializedResponse));
  133. testCount += 1
  134. return true;
  135. }
  136. while (true) {
  137. if (!doTestIo()) {
  138. console.error('conformance_nodejs: received EOF from test runner ' +
  139. "after " + testCount + " tests, exiting")
  140. break;
  141. }
  142. }