conformance_nodejs.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.PAYLOAD_NOT_SET:
  72. response.setRuntimeError("Request didn't have payload");
  73. return response;
  74. }
  75. switch (request.getRequestedOutputFormat()) {
  76. case conformance.WireFormat.UNSPECIFIED:
  77. response.setRuntimeError("Unspecified output format");
  78. return response;
  79. case conformance.WireFormat.PROTOBUF:
  80. response.setProtobufPayload(testMessage.serializeBinary());
  81. case conformance.WireFormat.JSON:
  82. response.setSkipped("JSON not supported.");
  83. return response;
  84. default:
  85. throw "Request didn't have requested output format";
  86. }
  87. } catch (err) {
  88. response.setRuntimeError(err.toString());
  89. }
  90. return response;
  91. }
  92. function onEof(totalRead) {
  93. if (totalRead == 0) {
  94. return undefined;
  95. } else {
  96. throw "conformance_nodejs: premature EOF on stdin.";
  97. }
  98. }
  99. // Utility function to read a buffer of N bytes.
  100. function readBuffer(bytes) {
  101. var buf = new Buffer(bytes);
  102. var totalRead = 0;
  103. while (totalRead < bytes) {
  104. var read = 0;
  105. try {
  106. read = fs.readSync(process.stdin.fd, buf, totalRead, bytes - totalRead);
  107. } catch (e) {
  108. if (e.code == 'EOF') {
  109. return onEof(totalRead)
  110. } else if (e.code == 'EAGAIN') {
  111. } else {
  112. throw "conformance_nodejs: Error reading from stdin." + e;
  113. }
  114. }
  115. totalRead += read;
  116. }
  117. return buf;
  118. }
  119. function writeBuffer(buffer) {
  120. var totalWritten = 0;
  121. while (totalWritten < buffer.length) {
  122. totalWritten += fs.writeSync(
  123. process.stdout.fd, buffer, totalWritten, buffer.length - totalWritten);
  124. }
  125. }
  126. // Returns true if the test ran successfully, false on legitimate EOF.
  127. // If EOF is encountered in an unexpected place, raises IOError.
  128. function doTestIo() {
  129. var lengthBuf = readBuffer(4);
  130. if (!lengthBuf) {
  131. return false;
  132. }
  133. var length = lengthBuf.readInt32LE(0);
  134. var serializedRequest = readBuffer(length);
  135. if (!serializedRequest) {
  136. throw "conformance_nodejs: Failed to read request.";
  137. }
  138. serializedRequest = new Uint8Array(serializedRequest);
  139. var request =
  140. conformance.ConformanceRequest.deserializeBinary(serializedRequest);
  141. var response = doTest(request);
  142. var serializedResponse = response.serializeBinary();
  143. lengthBuf = new Buffer(4);
  144. lengthBuf.writeInt32LE(serializedResponse.length, 0);
  145. writeBuffer(lengthBuf);
  146. writeBuffer(new Buffer(serializedResponse));
  147. testCount += 1
  148. return true;
  149. }
  150. while (true) {
  151. if (!doTestIo()) {
  152. console.error('conformance_nodejs: received EOF from test runner ' +
  153. "after " + testCount + " tests, exiting")
  154. break;
  155. }
  156. }