forked from bufbuild/protobuf-conformance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.js
More file actions
executable file
·175 lines (154 loc) · 5.1 KB
/
runner.js
File metadata and controls
executable file
·175 lines (154 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env -S node
// This file is mostly copied from the conformance testee files located at:
// https://github.com/protocolbuffers/protobuf-javascript/blob/main/experimental/runtime/kernel/conformance/
// - conformance_testee.js
// - conformance_testee_runner_node.js
//
// The main differences between this file and the above are:
// - The above files were combined into this one file
// - The test runners in the protobuf-javascript repo use handwritten code instead of generated code for the Protobuf
// files used in the test. These tests generate the code from the Protobuf files using protoc-gen-js.
// - This file uses CommonJS instead of the Closure Compiler
// - err.toString was converted to String(err)
// - Any unsupported input formats (JSON, TextFormat) that were skipped in the original test runner files have been
// modified to instead fail the tests just for transparency. Output formats that were skipped were left as skipped
// to match Protobuf-ES.
const {
ConformanceRequest,
ConformanceResponse,
WireFormat,
} = require("./gen/conformance/conformance_pb.js");
const {
TestAllTypesProto2,
} = require("./gen/google/protobuf/test_messages_proto2_pb.js");
const {
TestAllTypesProto3,
} = require("./gen/google/protobuf/test_messages_proto3_pb.js");
const { readSync, writeSync } = require("fs");
function readBuffer(bytes) {
const buf = Buffer.alloc(bytes);
let read = 0;
try {
read = readSync(0, buf, 0, bytes, null);
} catch (e) {
throw `failed to read from stdin: ${String(e)}`;
}
if (read !== bytes) {
if (read === 0) {
return "EOF";
}
throw "premature EOF on stdin.";
}
return buf;
}
// Write a buffer to stdout.
function writeBuffer(buffer) {
let totalWritten = 0;
while (totalWritten < buffer.length) {
totalWritten += writeSync(
1,
buffer,
totalWritten,
buffer.length - totalWritten
);
}
}
function doTest(request) {
const response = new ConformanceResponse(new ArrayBuffer(0));
if (
request.getPayloadCase() === ConformanceRequest.PayloadCase.JSON_PAYLOAD
) {
response.setRuntimeError("Json is not supported as input format.");
return response;
}
if (
request.getPayloadCase() === ConformanceRequest.PayloadCase.TEXT_PAYLOAD
) {
response.setRuntimeError("Text format is not supported as input format.");
return response;
}
if (
request.getPayloadCase() === ConformanceRequest.PayloadCase.PAYLOAD_NOT_SET
) {
response.setRuntimeError("Request didn't have payload.");
return response;
}
if (
request.getPayloadCase() !== ConformanceRequest.PayloadCase.PROTOBUF_PAYLOAD
) {
throw new Error("Request didn't have accepted input format.");
}
if (request.getRequestedOutputFormat() === WireFormat.JSON) {
response.setSkipped("Json is not supported as output format.");
return response;
}
if (request.getRequestedOutputFormat() === WireFormat.TEXT_FORMAT) {
response.setSkipped("Text format is not supported as output format.");
return response;
}
if (request.getRequestedOutputFormat() === WireFormat.TEXT_FORMAT) {
response.setRuntimeError("Unspecified output format");
return response;
}
if (request.getRequestedOutputFormat() !== WireFormat.PROTOBUF) {
throw new Error("Request didn't have accepted output format.");
}
if (request.getMessageType() === "conformance.FailureSet") {
response.setProtobufPayload(new ArrayBuffer(0));
} else if (
request.getMessageType() ===
"protobuf_test_messages.proto2.TestAllTypesProto2"
) {
try {
const testMessage = TestAllTypesProto2.deserializeBinary(
request.getProtobufPayload()
);
response.setProtobufPayload(testMessage.serializeBinary());
} catch (err) {
response.setParseError(String(err));
}
} else if (
request.getMessageType() ===
"protobuf_test_messages.proto3.TestAllTypesProto3"
) {
try {
const testMessage = TestAllTypesProto3.deserializeBinary(
request.getProtobufPayload()
);
response.setProtobufPayload(testMessage.serializeBinary());
} catch (err) {
response.setParseError(String(err));
}
} else {
throw new Error(
`Payload message not supported: ${request.getMessageType()}.`
);
}
return response;
}
function runConformanceTest() {
const requestLengthBuf = readBuffer(4);
if (requestLengthBuf === "EOF") {
return false;
}
const requestLength = requestLengthBuf.readInt32LE(0);
const serializedRequest = readBuffer(requestLength);
if (serializedRequest === "EOF") {
throw "Failed to read request.";
}
const array = new Uint8Array(serializedRequest);
const request = ConformanceRequest.deserializeBinary(array.buffer);
const response = doTest(request);
const serializedResponse = response.serializeBinary();
const responseLengthBuf = Buffer.alloc(4);
responseLengthBuf.writeInt32LE(serializedResponse.byteLength, 0);
writeBuffer(responseLengthBuf);
writeBuffer(Buffer.from(serializedResponse));
return true;
}
// eslint-disable-next-line no-constant-condition
while (true) {
if (!runConformanceTest()) {
break;
}
}