blob: 4d96a3851e0c48ca7a1b4798ba57a50356b3cd97 [file] [log] [blame]
Josh Habermanf873d322016-06-08 12:38:15 -07001#!/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
35var conformance = require('conformance_pb');
Josh Haberman8df69f02017-03-13 15:10:53 -070036var test_messages_proto3 = require('google/protobuf/test_messages_proto3_pb');
Yilun Chong3adb0542017-06-30 17:22:32 -070037var test_messages_proto2 = require('google/protobuf/test_messages_proto2_pb');
Josh Habermanf873d322016-06-08 12:38:15 -070038var fs = require('fs');
39
40var testCount = 0;
41
42function doTest(request) {
43 var testMessage;
44 var response = new conformance.ConformanceResponse();
45
46 try {
Josh Haberman8df69f02017-03-13 15:10:53 -070047 if (request.getRequestedOutputFormat() === conformance.WireFormat.JSON) {
48 response.setSkipped("JSON not supported.");
49 return response;
50 }
51
Josh Habermanf873d322016-06-08 12:38:15 -070052 switch (request.getPayloadCase()) {
Yilun Chong18a0c2c2017-06-27 18:24:15 -070053 case conformance.ConformanceRequest.PayloadCase.PROTOBUF_PAYLOAD: {
Yilun Chong3adb0542017-06-30 17:22:32 -070054 if (request.getMessageType() == "protobuf_test_messages.proto3.TestAllTypesProto3") {
Yilun Chong18a0c2c2017-06-27 18:24:15 -070055 try {
Yilun Chong3adb0542017-06-30 17:22:32 -070056 testMessage = test_messages_proto3.TestAllTypesProto3.deserializeBinary(
Yilun Chong18a0c2c2017-06-27 18:24:15 -070057 request.getProtobufPayload());
58 } catch (err) {
59 response.setParseError(err.toString());
60 return response;
61 }
Yilun Chonga7d5be62017-06-29 15:04:50 -070062 } else if (request.getMessageType() == "protobuf_test_messages.proto2.TestAllTypesProto2"){
Yilun Chong3adb0542017-06-30 17:22:32 -070063 try {
64 testMessage = test_messages_proto2.TestAllTypesProto2.deserializeBinary(
65 request.getProtobufPayload());
66 } catch (err) {
67 response.setParseError(err.toString());
68 return response;
69 }
Yilun Chong18a0c2c2017-06-27 18:24:15 -070070 } else {
71 throw "Protobuf request doesn\'t have specific payload type";
Josh Habermanf873d322016-06-08 12:38:15 -070072 }
Yilun Chong18a0c2c2017-06-27 18:24:15 -070073 }
Josh Habermanf873d322016-06-08 12:38:15 -070074
75 case conformance.ConformanceRequest.PayloadCase.JSON_PAYLOAD:
76 response.setSkipped("JSON not supported.");
77 return response;
78
Yilun Chong6d29c222019-02-26 13:46:30 -080079 case conformance.ConformanceRequest.PayloadCase.TEXT_PAYLOAD:
80 response.setSkipped("Text format not supported.");
81 return response;
82
Josh Habermanf873d322016-06-08 12:38:15 -070083 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 Haberman8df69f02017-03-13 15:10:53 -070089 case conformance.WireFormat.UNSPECIFIED:
Josh Habermanf873d322016-06-08 12:38:15 -070090 response.setRuntimeError("Unspecified output format");
91 return response;
92
Josh Haberman8df69f02017-03-13 15:10:53 -070093 case conformance.WireFormat.PROTOBUF:
Josh Habermanf873d322016-06-08 12:38:15 -070094 response.setProtobufPayload(testMessage.serializeBinary());
95
Josh Haberman8df69f02017-03-13 15:10:53 -070096 case conformance.WireFormat.JSON:
Josh Habermanf873d322016-06-08 12:38:15 -070097 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 Haberman8df69f02017-03-13 15:10:53 -0700107 return response;
Josh Habermanf873d322016-06-08 12:38:15 -0700108}
109
110function 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.
119function readBuffer(bytes) {
120 var buf = new Buffer(bytes);
121 var totalRead = 0;
122 while (totalRead < bytes) {
Josh Haberman8df69f02017-03-13 15:10:53 -0700123 var read = 0;
Josh Habermanf873d322016-06-08 12:38:15 -0700124 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 Haberman8df69f02017-03-13 15:10:53 -0700129 } else if (e.code == 'EAGAIN') {
Josh Habermanf873d322016-06-08 12:38:15 -0700130 } else {
Josh Haberman8df69f02017-03-13 15:10:53 -0700131 throw "conformance_nodejs: Error reading from stdin." + e;
Josh Habermanf873d322016-06-08 12:38:15 -0700132 }
133 }
134
Josh Habermanf873d322016-06-08 12:38:15 -0700135 totalRead += read;
136 }
137
138 return buf;
139}
140
141function 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 Habermanf873d322016-06-08 12:38:15 -0700149// Returns true if the test ran successfully, false on legitimate EOF.
150// If EOF is encountered in an unexpected place, raises IOError.
151function 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 Haberman8df69f02017-03-13 15:10:53 -0700163 serializedRequest = new Uint8Array(serializedRequest);
Josh Habermanf873d322016-06-08 12:38:15 -0700164 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 Haberman8df69f02017-03-13 15:10:53 -0700173 writeBuffer(new Buffer(serializedResponse));
Josh Habermanf873d322016-06-08 12:38:15 -0700174
175 testCount += 1
176
177 return true;
178}
179
180while (true) {
181 if (!doTestIo()) {
Josh Haberman8df69f02017-03-13 15:10:53 -0700182 console.error('conformance_nodejs: received EOF from test runner ' +
Josh Habermanf873d322016-06-08 12:38:15 -0700183 "after " + testCount + " tests, exiting")
184 break;
185 }
186}