blob: 5d3955f77b4490761fcb29d26adf5fd62f9b8f30 [file] [log] [blame]
Austin Schuh40c16522018-10-28 20:27:54 -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');
36var test_messages_proto3 = require('google/protobuf/test_messages_proto3_pb');
37var test_messages_proto2 = require('google/protobuf/test_messages_proto2_pb');
38var fs = require('fs');
39
40var testCount = 0;
41
42function doTest(request) {
43 var testMessage;
44 var response = new conformance.ConformanceResponse();
45
46 try {
47 if (request.getRequestedOutputFormat() === conformance.WireFormat.JSON) {
48 response.setSkipped("JSON not supported.");
49 return response;
50 }
51
52 switch (request.getPayloadCase()) {
53 case conformance.ConformanceRequest.PayloadCase.PROTOBUF_PAYLOAD: {
54 if (request.getMessageType() == "protobuf_test_messages.proto3.TestAllTypesProto3") {
55 try {
56 testMessage = test_messages_proto3.TestAllTypesProto3.deserializeBinary(
57 request.getProtobufPayload());
58 } catch (err) {
59 response.setParseError(err.toString());
60 return response;
61 }
62 } else if (request.getMessageType() == "protobuf_test_messages.proto2.TestAllTypesProto2"){
63 try {
64 testMessage = test_messages_proto2.TestAllTypesProto2.deserializeBinary(
65 request.getProtobufPayload());
66 } catch (err) {
67 response.setParseError(err.toString());
68 return response;
69 }
70 } else {
71 throw "Protobuf request doesn\'t have specific payload type";
72 }
73 }
74
75 case conformance.ConformanceRequest.PayloadCase.JSON_PAYLOAD:
76 response.setSkipped("JSON not supported.");
77 return response;
78
79 case conformance.ConformanceRequest.PayloadCase.PAYLOAD_NOT_SET:
80 response.setRuntimeError("Request didn't have payload");
81 return response;
82 }
83
84 switch (request.getRequestedOutputFormat()) {
85 case conformance.WireFormat.UNSPECIFIED:
86 response.setRuntimeError("Unspecified output format");
87 return response;
88
89 case conformance.WireFormat.PROTOBUF:
90 response.setProtobufPayload(testMessage.serializeBinary());
91
92 case conformance.WireFormat.JSON:
93 response.setSkipped("JSON not supported.");
94 return response;
95
96 default:
97 throw "Request didn't have requested output format";
98 }
99 } catch (err) {
100 response.setRuntimeError(err.toString());
101 }
102
103 return response;
104}
105
106function onEof(totalRead) {
107 if (totalRead == 0) {
108 return undefined;
109 } else {
110 throw "conformance_nodejs: premature EOF on stdin.";
111 }
112}
113
114// Utility function to read a buffer of N bytes.
115function readBuffer(bytes) {
116 var buf = new Buffer(bytes);
117 var totalRead = 0;
118 while (totalRead < bytes) {
119 var read = 0;
120 try {
121 read = fs.readSync(process.stdin.fd, buf, totalRead, bytes - totalRead);
122 } catch (e) {
123 if (e.code == 'EOF') {
124 return onEof(totalRead)
125 } else if (e.code == 'EAGAIN') {
126 } else {
127 throw "conformance_nodejs: Error reading from stdin." + e;
128 }
129 }
130
131 totalRead += read;
132 }
133
134 return buf;
135}
136
137function writeBuffer(buffer) {
138 var totalWritten = 0;
139 while (totalWritten < buffer.length) {
140 totalWritten += fs.writeSync(
141 process.stdout.fd, buffer, totalWritten, buffer.length - totalWritten);
142 }
143}
144
145// Returns true if the test ran successfully, false on legitimate EOF.
146// If EOF is encountered in an unexpected place, raises IOError.
147function doTestIo() {
148 var lengthBuf = readBuffer(4);
149 if (!lengthBuf) {
150 return false;
151 }
152
153 var length = lengthBuf.readInt32LE(0);
154 var serializedRequest = readBuffer(length);
155 if (!serializedRequest) {
156 throw "conformance_nodejs: Failed to read request.";
157 }
158
159 serializedRequest = new Uint8Array(serializedRequest);
160 var request =
161 conformance.ConformanceRequest.deserializeBinary(serializedRequest);
162 var response = doTest(request);
163
164 var serializedResponse = response.serializeBinary();
165
166 lengthBuf = new Buffer(4);
167 lengthBuf.writeInt32LE(serializedResponse.length, 0);
168 writeBuffer(lengthBuf);
169 writeBuffer(new Buffer(serializedResponse));
170
171 testCount += 1
172
173 return true;
174}
175
176while (true) {
177 if (!doTestIo()) {
178 console.error('conformance_nodejs: received EOF from test runner ' +
179 "after " + testCount + " tests, exiting")
180 break;
181 }
182}