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