Sandy Zhang | 6dd2176 | 2021-09-13 17:56:05 +0000 | [diff] [blame] | 1 | #!/usr/bin/env node |
Joshua Haberman | e5c570b | 2021-09-09 08:21:42 -0700 | [diff] [blame] | 2 | // Protocol Buffers - Google's data interchange format |
| 3 | // Copyright 2008 Google Inc. All rights reserved. |
| 4 | // https://developers.google.com/protocol-buffers/ |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 32 | var conformance = require('conformance_pb'); |
Josh Haberman | 8df69f0 | 2017-03-13 15:10:53 -0700 | [diff] [blame] | 33 | var test_messages_proto3 = require('google/protobuf/test_messages_proto3_pb'); |
Yilun Chong | 3adb054 | 2017-06-30 17:22:32 -0700 | [diff] [blame] | 34 | var test_messages_proto2 = require('google/protobuf/test_messages_proto2_pb'); |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 35 | var fs = require('fs'); |
| 36 | |
| 37 | var testCount = 0; |
| 38 | |
| 39 | function doTest(request) { |
| 40 | var testMessage; |
| 41 | var response = new conformance.ConformanceResponse(); |
| 42 | |
| 43 | try { |
Josh Haberman | 8df69f0 | 2017-03-13 15:10:53 -0700 | [diff] [blame] | 44 | if (request.getRequestedOutputFormat() === conformance.WireFormat.JSON) { |
| 45 | response.setSkipped("JSON not supported."); |
| 46 | return response; |
| 47 | } |
| 48 | |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 49 | switch (request.getPayloadCase()) { |
Yilun Chong | 18a0c2c | 2017-06-27 18:24:15 -0700 | [diff] [blame] | 50 | case conformance.ConformanceRequest.PayloadCase.PROTOBUF_PAYLOAD: { |
Yilun Chong | 3adb054 | 2017-06-30 17:22:32 -0700 | [diff] [blame] | 51 | if (request.getMessageType() == "protobuf_test_messages.proto3.TestAllTypesProto3") { |
Yilun Chong | 18a0c2c | 2017-06-27 18:24:15 -0700 | [diff] [blame] | 52 | try { |
Yilun Chong | 3adb054 | 2017-06-30 17:22:32 -0700 | [diff] [blame] | 53 | testMessage = test_messages_proto3.TestAllTypesProto3.deserializeBinary( |
Yilun Chong | 18a0c2c | 2017-06-27 18:24:15 -0700 | [diff] [blame] | 54 | request.getProtobufPayload()); |
| 55 | } catch (err) { |
| 56 | response.setParseError(err.toString()); |
| 57 | return response; |
| 58 | } |
Yilun Chong | a7d5be6 | 2017-06-29 15:04:50 -0700 | [diff] [blame] | 59 | } else if (request.getMessageType() == "protobuf_test_messages.proto2.TestAllTypesProto2"){ |
Yilun Chong | 3adb054 | 2017-06-30 17:22:32 -0700 | [diff] [blame] | 60 | try { |
| 61 | testMessage = test_messages_proto2.TestAllTypesProto2.deserializeBinary( |
| 62 | request.getProtobufPayload()); |
| 63 | } catch (err) { |
| 64 | response.setParseError(err.toString()); |
| 65 | return response; |
| 66 | } |
Yilun Chong | 18a0c2c | 2017-06-27 18:24:15 -0700 | [diff] [blame] | 67 | } else { |
| 68 | throw "Protobuf request doesn\'t have specific payload type"; |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 69 | } |
Yilun Chong | 18a0c2c | 2017-06-27 18:24:15 -0700 | [diff] [blame] | 70 | } |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 71 | |
| 72 | case conformance.ConformanceRequest.PayloadCase.JSON_PAYLOAD: |
| 73 | response.setSkipped("JSON not supported."); |
| 74 | return response; |
| 75 | |
Yilun Chong | 6d29c22 | 2019-02-26 13:46:30 -0800 | [diff] [blame] | 76 | case conformance.ConformanceRequest.PayloadCase.TEXT_PAYLOAD: |
| 77 | response.setSkipped("Text format not supported."); |
| 78 | return response; |
| 79 | |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 80 | case conformance.ConformanceRequest.PayloadCase.PAYLOAD_NOT_SET: |
| 81 | response.setRuntimeError("Request didn't have payload"); |
| 82 | return response; |
| 83 | } |
| 84 | |
| 85 | switch (request.getRequestedOutputFormat()) { |
Josh Haberman | 8df69f0 | 2017-03-13 15:10:53 -0700 | [diff] [blame] | 86 | case conformance.WireFormat.UNSPECIFIED: |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 87 | response.setRuntimeError("Unspecified output format"); |
| 88 | return response; |
| 89 | |
Josh Haberman | 8df69f0 | 2017-03-13 15:10:53 -0700 | [diff] [blame] | 90 | case conformance.WireFormat.PROTOBUF: |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 91 | response.setProtobufPayload(testMessage.serializeBinary()); |
| 92 | |
Josh Haberman | 8df69f0 | 2017-03-13 15:10:53 -0700 | [diff] [blame] | 93 | case conformance.WireFormat.JSON: |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 94 | response.setSkipped("JSON not supported."); |
| 95 | return response; |
| 96 | |
| 97 | default: |
| 98 | throw "Request didn't have requested output format"; |
| 99 | } |
| 100 | } catch (err) { |
| 101 | response.setRuntimeError(err.toString()); |
| 102 | } |
| 103 | |
Josh Haberman | 8df69f0 | 2017-03-13 15:10:53 -0700 | [diff] [blame] | 104 | return response; |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | function onEof(totalRead) { |
| 108 | if (totalRead == 0) { |
| 109 | return undefined; |
| 110 | } else { |
| 111 | throw "conformance_nodejs: premature EOF on stdin."; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Utility function to read a buffer of N bytes. |
| 116 | function readBuffer(bytes) { |
| 117 | var buf = new Buffer(bytes); |
| 118 | var totalRead = 0; |
| 119 | while (totalRead < bytes) { |
Josh Haberman | 8df69f0 | 2017-03-13 15:10:53 -0700 | [diff] [blame] | 120 | var read = 0; |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 121 | try { |
| 122 | read = fs.readSync(process.stdin.fd, buf, totalRead, bytes - totalRead); |
| 123 | } catch (e) { |
| 124 | if (e.code == 'EOF') { |
| 125 | return onEof(totalRead) |
Josh Haberman | 8df69f0 | 2017-03-13 15:10:53 -0700 | [diff] [blame] | 126 | } else if (e.code == 'EAGAIN') { |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 127 | } else { |
Josh Haberman | 8df69f0 | 2017-03-13 15:10:53 -0700 | [diff] [blame] | 128 | throw "conformance_nodejs: Error reading from stdin." + e; |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 132 | totalRead += read; |
| 133 | } |
| 134 | |
| 135 | return buf; |
| 136 | } |
| 137 | |
| 138 | function writeBuffer(buffer) { |
| 139 | var totalWritten = 0; |
| 140 | while (totalWritten < buffer.length) { |
| 141 | totalWritten += fs.writeSync( |
| 142 | process.stdout.fd, buffer, totalWritten, buffer.length - totalWritten); |
| 143 | } |
| 144 | } |
| 145 | |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 146 | // Returns true if the test ran successfully, false on legitimate EOF. |
| 147 | // If EOF is encountered in an unexpected place, raises IOError. |
| 148 | function doTestIo() { |
| 149 | var lengthBuf = readBuffer(4); |
| 150 | if (!lengthBuf) { |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | var length = lengthBuf.readInt32LE(0); |
| 155 | var serializedRequest = readBuffer(length); |
| 156 | if (!serializedRequest) { |
| 157 | throw "conformance_nodejs: Failed to read request."; |
| 158 | } |
| 159 | |
Josh Haberman | 8df69f0 | 2017-03-13 15:10:53 -0700 | [diff] [blame] | 160 | serializedRequest = new Uint8Array(serializedRequest); |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 161 | var request = |
| 162 | conformance.ConformanceRequest.deserializeBinary(serializedRequest); |
| 163 | var response = doTest(request); |
| 164 | |
| 165 | var serializedResponse = response.serializeBinary(); |
| 166 | |
| 167 | lengthBuf = new Buffer(4); |
| 168 | lengthBuf.writeInt32LE(serializedResponse.length, 0); |
| 169 | writeBuffer(lengthBuf); |
Josh Haberman | 8df69f0 | 2017-03-13 15:10:53 -0700 | [diff] [blame] | 170 | writeBuffer(new Buffer(serializedResponse)); |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 171 | |
| 172 | testCount += 1 |
| 173 | |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | while (true) { |
| 178 | if (!doTestIo()) { |
Josh Haberman | 8df69f0 | 2017-03-13 15:10:53 -0700 | [diff] [blame] | 179 | console.error('conformance_nodejs: received EOF from test runner ' + |
Josh Haberman | f873d32 | 2016-06-08 12:38:15 -0700 | [diff] [blame] | 180 | "after " + testCount + " tests, exiting") |
| 181 | break; |
| 182 | } |
| 183 | } |