forked from bufbuild/protobuf-conformance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.ts
More file actions
executable file
·225 lines (203 loc) · 7.04 KB
/
runner.ts
File metadata and controls
executable file
·225 lines (203 loc) · 7.04 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env -S npx tsx
import { readSync, writeSync } from "fs";
import * as protos from "./gen/protos_pb.js";
const { Any, Struct, Value, Int32Value, FieldMask, Duration, Timestamp } =
protos.google.protobuf;
const { TestAllTypesProto2 } = protos.protobuf_test_messages.proto2;
const { TestAllTypesProto3 } = protos.protobuf_test_messages.proto3;
const { ConformanceRequest, ConformanceResponse, FailureSet } =
protos.conformance;
type MessageType =
| protos.protobuf_test_messages.proto2.TestAllTypesProto2
| protos.protobuf_test_messages.proto3.TestAllTypesProto3
| protos.google.protobuf.Struct
| protos.google.protobuf.Value
| protos.google.protobuf.FieldMask
| protos.google.protobuf.Duration
| protos.google.protobuf.Int32Value
| protos.google.protobuf.Any
| protos.google.protobuf.Timestamp;
interface Registry {
[s: string]:
| typeof TestAllTypesProto2
| typeof TestAllTypesProto3
| typeof Struct
| typeof Value
| typeof FieldMask
| typeof Duration
| typeof Int32Value
| typeof Any
| typeof Timestamp
| undefined;
}
const registry: Registry = {
"protobuf_test_messages.proto2.TestAllTypesProto2": TestAllTypesProto2,
"protobuf_test_messages.proto3.TestAllTypesProto3": TestAllTypesProto3,
"google.protobuf.Struct": Struct,
"google.protobuf.Value": Value,
"google.protobuf.FieldMask": FieldMask,
"google.protobuf.Duration": Duration,
"google.protobuf.Int32Value": Int32Value,
"google.protobuf.Any": Any,
"google.protobuf.Timestamp": Timestamp,
};
function main() {
let testCount = 0;
try {
while (testIo(test)) {
testCount += 1;
}
} catch (e) {
process.stderr.write(
`conformance.ts: exiting after ${testCount} tests: ${String(e)}`
);
process.exit(1);
}
}
interface Result {
[s: string]: Uint8Array | string;
}
function test(request: protos.conformance.ConformanceRequest): Result {
if (request.messageType === FailureSet.name) {
// > The conformance runner will request a list of failures as the first request.
// > This will be known by message_type == "conformance.FailureSet", a conformance
// > test should return a serialized FailureSet in protobuf_payload.
const failureSet = FailureSet.create();
return {
protobufPayload: FailureSet.encode(failureSet).finish(),
};
}
const payloadType = registry[request.messageType];
if (!payloadType) {
return {
runtimeError: `unknown request message type ${request.messageType}`,
};
}
let payload: MessageType;
try {
if (request.protobufPayload) {
payload = payloadType.decode(request.protobufPayload);
} else if (
request.jsonPayload !== null &&
request.jsonPayload !== undefined &&
request.jsonPayload != ""
) {
// Note it doesn't seem ProtobufJS allows for specifying JSON options such as ignore unknown fields so this is
// unused:
// if (request.testCategory === TestCategory.JSON_IGNORE_UNKNOWN_PARSING_TEST;
// Further, we first have to parse the string payload into a JSON object and then call fromObject
payload = payloadType.fromObject(
JSON.parse(request.jsonPayload) as MessageType
);
} else {
// We use a failure list instead of skipping, because that is more transparent.
return {
runtimeError: `payload not supported`,
};
}
} catch (err) {
// > This string should be set to indicate parsing failed. The string can
// > provide more information about the parse error if it is available.
// >
// > Setting this string does not necessarily mean the testee failed the
// > test. Some of the test cases are intentionally invalid input.
return { parseError: String(err) };
}
try {
switch (request.requestedOutputFormat) {
case 1: // PROTOBUF
return {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
protobufPayload: payloadType.encode(payload as any).finish(),
};
case 2: // JSON:
return {
jsonPayload: JSON.stringify(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
payloadType.toObject(payload as any, {json: true, bytes: String})
),
};
case 3: // JSPB
return { skipped: "JSPB not supported." };
case 4: // TEXT_FORMAT
return { skipped: "Text format not supported." };
default:
return {
runtimeError: `unknown requested output format ${request.requestedOutputFormat}`,
};
}
} catch (err) {
// > If the input was successfully parsed but errors occurred when
// > serializing it to the requested output format, set the error message in
// > this field.
return { serializeError: String(err) };
}
}
// Returns true if the test ran successfully, false on legitimate EOF.
// If EOF is encountered in an unexpected place, raises IOError.
function testIo(
test: (request: protos.conformance.ConformanceRequest) => Result
): boolean {
setBlockingStdout();
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 request = ConformanceRequest.decode(serializedRequest);
const result = test(request);
const response = ConformanceResponse.create(result);
const serializedResponse = ConformanceResponse.encode(response).finish();
const responseLengthBuf = Buffer.alloc(4);
responseLengthBuf.writeInt32LE(serializedResponse.length, 0);
writeBuffer(responseLengthBuf);
writeBuffer(Buffer.from(serializedResponse));
return true;
}
// Read a buffer of N bytes from stdin.
function readBuffer(bytes: number): Buffer | "EOF" {
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: Buffer): void {
let totalWritten = 0;
while (totalWritten < buffer.length) {
totalWritten += writeSync(
1,
buffer,
totalWritten,
buffer.length - totalWritten
);
}
}
// Fixes https://github.com/timostamm/protobuf-ts/issues/134
// Node is buffering chunks to stdout, meaning that for big generated
// files the CodeGeneratorResponse will not reach protoc completely.
// To fix this, we set stdout to block using the internal private
// method setBlocking(true)
function setBlockingStdout(): void {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-explicit-any,@typescript-eslint/no-unsafe-assignment
const stdoutHandle = (process.stdout as any)._handle;
if (stdoutHandle !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
stdoutHandle.setBlocking(true);
}
}
main();