blob: 95da893f71b466864bd8d130d378a7981abf01e8 [file] [log] [blame]
Sandy Zhang6dd21762021-09-13 17:56:05 +00001#!/usr/bin/env node
Joshua Habermane5c570b2021-09-09 08:21:42 -07002// 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 Habermanf873d322016-06-08 12:38:15 -070032var conformance = require('conformance_pb');
Josh Haberman8df69f02017-03-13 15:10:53 -070033var test_messages_proto3 = require('google/protobuf/test_messages_proto3_pb');
Yilun Chong3adb0542017-06-30 17:22:32 -070034var test_messages_proto2 = require('google/protobuf/test_messages_proto2_pb');
Josh Habermanf873d322016-06-08 12:38:15 -070035var fs = require('fs');
36
37var testCount = 0;
38
39function doTest(request) {
40 var testMessage;
41 var response = new conformance.ConformanceResponse();
42
43 try {
Josh Haberman8df69f02017-03-13 15:10:53 -070044 if (request.getRequestedOutputFormat() === conformance.WireFormat.JSON) {
45 response.setSkipped("JSON not supported.");
46 return response;
47 }
48
Josh Habermanf873d322016-06-08 12:38:15 -070049 switch (request.getPayloadCase()) {
Yilun Chong18a0c2c2017-06-27 18:24:15 -070050 case conformance.ConformanceRequest.PayloadCase.PROTOBUF_PAYLOAD: {
Yilun Chong3adb0542017-06-30 17:22:32 -070051 if (request.getMessageType() == "protobuf_test_messages.proto3.TestAllTypesProto3") {
Yilun Chong18a0c2c2017-06-27 18:24:15 -070052 try {
Yilun Chong3adb0542017-06-30 17:22:32 -070053 testMessage = test_messages_proto3.TestAllTypesProto3.deserializeBinary(
Yilun Chong18a0c2c2017-06-27 18:24:15 -070054 request.getProtobufPayload());
55 } catch (err) {
56 response.setParseError(err.toString());
57 return response;
58 }
Yilun Chonga7d5be62017-06-29 15:04:50 -070059 } else if (request.getMessageType() == "protobuf_test_messages.proto2.TestAllTypesProto2"){
Yilun Chong3adb0542017-06-30 17:22:32 -070060 try {
61 testMessage = test_messages_proto2.TestAllTypesProto2.deserializeBinary(
62 request.getProtobufPayload());
63 } catch (err) {
64 response.setParseError(err.toString());
65 return response;
66 }
Yilun Chong18a0c2c2017-06-27 18:24:15 -070067 } else {
68 throw "Protobuf request doesn\'t have specific payload type";
Josh Habermanf873d322016-06-08 12:38:15 -070069 }
Yilun Chong18a0c2c2017-06-27 18:24:15 -070070 }
Josh Habermanf873d322016-06-08 12:38:15 -070071
72 case conformance.ConformanceRequest.PayloadCase.JSON_PAYLOAD:
73 response.setSkipped("JSON not supported.");
74 return response;
75
Yilun Chong6d29c222019-02-26 13:46:30 -080076 case conformance.ConformanceRequest.PayloadCase.TEXT_PAYLOAD:
77 response.setSkipped("Text format not supported.");
78 return response;
79
Josh Habermanf873d322016-06-08 12:38:15 -070080 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 Haberman8df69f02017-03-13 15:10:53 -070086 case conformance.WireFormat.UNSPECIFIED:
Josh Habermanf873d322016-06-08 12:38:15 -070087 response.setRuntimeError("Unspecified output format");
88 return response;
89
Josh Haberman8df69f02017-03-13 15:10:53 -070090 case conformance.WireFormat.PROTOBUF:
Josh Habermanf873d322016-06-08 12:38:15 -070091 response.setProtobufPayload(testMessage.serializeBinary());
92
Josh Haberman8df69f02017-03-13 15:10:53 -070093 case conformance.WireFormat.JSON:
Josh Habermanf873d322016-06-08 12:38:15 -070094 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 Haberman8df69f02017-03-13 15:10:53 -0700104 return response;
Josh Habermanf873d322016-06-08 12:38:15 -0700105}
106
107function 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.
116function readBuffer(bytes) {
117 var buf = new Buffer(bytes);
118 var totalRead = 0;
119 while (totalRead < bytes) {
Josh Haberman8df69f02017-03-13 15:10:53 -0700120 var read = 0;
Josh Habermanf873d322016-06-08 12:38:15 -0700121 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 Haberman8df69f02017-03-13 15:10:53 -0700126 } else if (e.code == 'EAGAIN') {
Josh Habermanf873d322016-06-08 12:38:15 -0700127 } else {
Josh Haberman8df69f02017-03-13 15:10:53 -0700128 throw "conformance_nodejs: Error reading from stdin." + e;
Josh Habermanf873d322016-06-08 12:38:15 -0700129 }
130 }
131
Josh Habermanf873d322016-06-08 12:38:15 -0700132 totalRead += read;
133 }
134
135 return buf;
136}
137
138function 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 Habermanf873d322016-06-08 12:38:15 -0700146// Returns true if the test ran successfully, false on legitimate EOF.
147// If EOF is encountered in an unexpected place, raises IOError.
148function 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 Haberman8df69f02017-03-13 15:10:53 -0700160 serializedRequest = new Uint8Array(serializedRequest);
Josh Habermanf873d322016-06-08 12:38:15 -0700161 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 Haberman8df69f02017-03-13 15:10:53 -0700170 writeBuffer(new Buffer(serializedResponse));
Josh Habermanf873d322016-06-08 12:38:15 -0700171
172 testCount += 1
173
174 return true;
175}
176
177while (true) {
178 if (!doTestIo()) {
Josh Haberman8df69f02017-03-13 15:10:53 -0700179 console.error('conformance_nodejs: received EOF from test runner ' +
Josh Habermanf873d322016-06-08 12:38:15 -0700180 "after " + testCount + " tests, exiting")
181 break;
182 }
183}