-
Notifications
You must be signed in to change notification settings - Fork 69
/
conformance.ts
259 lines (240 loc) · 8.12 KB
/
conformance.ts
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2021-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import type { Writable } from "node:stream";
import {
create,
fromBinary,
fromJsonString,
type DescMessage,
type Message,
type MessageShape,
toBinary,
toJsonString,
createRegistry,
} from "@bufbuild/protobuf";
import {
type ConformanceRequest,
type ConformanceResponse,
ConformanceRequestSchema,
ConformanceResponseSchema,
FailureSetSchema,
TestCategory,
WireFormat,
} from "./gen/conformance/conformance_pb.js";
import { file_google_protobuf_test_messages_proto3 } from "./gen/google/protobuf/test_messages_proto3_pb.js";
import { file_google_protobuf_test_messages_proto2 } from "./gen/google/protobuf/test_messages_proto2_pb.js";
import { file_google_protobuf_test_messages_edition2023 } from "./gen/google/protobuf/test_messages_edition2023_pb.js";
import { file_google_protobuf_test_messages_proto2_editions } from "./gen/google/protobuf/test_messages_proto2_editions_pb.js";
import { file_google_protobuf_test_messages_proto3_editions } from "./gen/google/protobuf/test_messages_proto3_editions_pb.js";
import {
file_google_protobuf_any,
file_google_protobuf_duration,
file_google_protobuf_field_mask,
file_google_protobuf_struct,
file_google_protobuf_timestamp,
file_google_protobuf_wrappers,
} from "@bufbuild/protobuf/wkt";
const registry = createRegistry(
file_google_protobuf_test_messages_proto2,
file_google_protobuf_test_messages_proto3,
file_google_protobuf_test_messages_edition2023,
file_google_protobuf_test_messages_proto2_editions,
file_google_protobuf_test_messages_proto3_editions,
file_google_protobuf_any,
file_google_protobuf_struct,
file_google_protobuf_field_mask,
file_google_protobuf_timestamp,
file_google_protobuf_duration,
file_google_protobuf_wrappers,
);
void main();
async function main() {
let testCount = 0;
try {
const requests = readMessages(process.stdin, ConformanceRequestSchema);
const responses = processMessages(
requests,
(request: ConformanceRequest): ConformanceResponse => {
testCount += 1;
return create(ConformanceResponseSchema, {
result: test(request),
});
},
);
await writeMessages(process.stdout, ConformanceResponseSchema, responses);
} catch (e) {
process.stderr.write(
`conformance.ts: exiting after ${testCount} tests: ${String(e)}`,
() => process.exit(1),
);
}
}
function test(request: ConformanceRequest): ConformanceResponse["result"] {
if (request.messageType === FailureSetSchema.typeName) {
// > 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 = create(FailureSetSchema);
return {
case: "protobufPayload",
value: toBinary(FailureSetSchema, failureSet),
};
}
const payloadType = registry.getMessage(request.messageType);
if (!payloadType) {
return {
case: "runtimeError",
value: `unknown request message type ${request.messageType}`,
};
}
let payload: Message;
try {
switch (request.payload.case) {
case "protobufPayload":
payload = fromBinary(payloadType, request.payload.value);
break;
case "jsonPayload":
payload = fromJsonString(payloadType, request.payload.value, {
ignoreUnknownFields:
request.testCategory ===
TestCategory.JSON_IGNORE_UNKNOWN_PARSING_TEST,
registry,
});
break;
default:
// We use a failure list instead of skipping, because that is more transparent.
return {
case: "runtimeError",
value: `${request.payload.case ?? "?"} 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 { case: "parseError", value: String(err) };
}
try {
switch (request.requestedOutputFormat) {
case WireFormat.PROTOBUF:
return {
case: "protobufPayload",
value: toBinary(payloadType, payload),
};
case WireFormat.JSON:
return {
case: "jsonPayload",
value: toJsonString(payloadType, payload, {
registry,
}),
};
case WireFormat.JSPB:
return { case: "skipped", value: "JSPB not supported." };
case WireFormat.TEXT_FORMAT:
return { case: "skipped", value: "Text format not supported." };
default:
return {
case: "runtimeError",
value: `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 { case: "serializeError", value: String(err) };
}
}
// Reads length-prefixed messages from a stream.
async function* readMessages<Desc extends DescMessage>(
stream: AsyncIterable<Uint8Array>,
messageDesc: Desc,
): AsyncIterable<MessageShape<Desc>> {
// append chunk to buffer, returning updated buffer
function append(buffer: Uint8Array, chunk: Uint8Array): Uint8Array {
const n = new Uint8Array(buffer.byteLength + chunk.byteLength);
n.set(buffer);
n.set(chunk, buffer.byteLength);
return n;
}
let buffer = new Uint8Array(0);
for await (const chunk of stream) {
buffer = append(buffer, chunk);
for (;;) {
if (buffer.byteLength < 4) {
// size is incomplete, buffer more data
break;
}
const size = new DataView(buffer.buffer).getInt32(0, true);
if (buffer.byteLength < 4 + size) {
// message is incomplete, buffer more data
break;
}
yield fromBinary(messageDesc, buffer.subarray(4, 4 + size));
buffer = buffer.subarray(4 + size);
}
}
if (buffer.byteLength > 0) {
throw new Error("incomplete data");
}
}
// Returns a new iterable that processes each element of the input.
function processMessages<I, O>(
requests: AsyncIterable<I>,
processor: (req: I) => O,
): AsyncIterable<O> {
const source = requests[Symbol.asyncIterator]();
return {
[Symbol.asyncIterator]() {
return {
async next() {
const s = await source.next();
if (s.done === true) {
return {
done: true,
value: undefined,
};
}
return {
done: false,
value: processor(s.value),
};
},
};
},
};
}
// Writes length-prefixed messages to a stream.
async function writeMessages<Desc extends DescMessage>(
stream: Writable,
messageDesc: Desc,
messages:
| AsyncIterable<MessageShape<Desc>>
| (() => AsyncIterable<MessageShape<Desc>>),
) {
const input = typeof messages == "function" ? messages() : messages;
for await (const message of input) {
const bytes = toBinary(messageDesc, message);
await new Promise<void>((resolve, reject) => {
const lengthBytes = new Uint8Array(4);
new DataView(lengthBytes.buffer).setInt32(0, bytes.length, true);
stream.write(lengthBytes, (err) => (err ? reject(err) : resolve()));
});
await new Promise<void>((resolve, reject) => {
stream.write(bytes, (err) => (err ? reject(err) : resolve()));
});
}
}