AtlatestRepositorysigil-quic

sigil-quic / tree / scriptsnative-tls-tests.mjs

1// Generate a native test entry from the same test bodies used by sigil test.
2// The custom-runtime CLI test harness does not forward native build-fn yet.
3// Run this generator and the resulting build only on the test machine.
4import { readFileSync, writeFileSync, mkdirSync } from 'node:fs';
5import { dirname, join, resolve } from 'node:path';
6import { fileURLToPath } from 'node:url';
7
8const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
9const output = join(root, 'build', 'native-tests');
10const source = join(output, 'src', 'sigil', 'quic', 'tests');
11mkdirSync(source, { recursive: true });
12const files = [
13 'test-tls-codec.sgl', 'test-tls-extensions.sgl',
14 'test-tls-seen.sgl',
15 'test-tls-negotiation.sgl',
16 'test-tls-certificate-policy.sgl',
17 'test-tls-retry-policy.sgl',
18 'test-tls-authentication.sgl', 'test-tls-handshake.sgl',
19 'test-tls-input-bounds.sgl',
20 'test-tls-alerts.sgl',
21 'test-frame.sgl',
22 'test-datagram-codec.sgl',
23 'test-reassembly.sgl',
24 'test-packet.sgl',
25 'test-retry-packet.sgl',
26 'test-client-retry.sgl',
27 'test-retry-token.sgl',
28 'test-server-retry.sgl',
29 'test-connection-space.sgl',
30 'test-connection.sgl',
31 'test-connection-network.sgl',
32 'test-addressed-output.sgl',
33 'test-network-confirmation.sgl',
34 'test-network-receive.sgl',
35 'test-random-ids.sgl',
36 'test-datagram.sgl',
37 'test-aead-limits.sgl',
38 'test-application-close.sgl',
39 'test-transport-errors.sgl',
40 'test-frame-work.sgl',
41 'test-close-levels.sgl',
42 'test-closing.sgl',
43 'test-api-validation.sgl',
44 'test-quic-conformance.sgl',
45 'test-ticket-validation.sgl',
46 'test-psk-fallback.sgl',
47 'test-sni-fallback.sgl',
48 'test-tls-identity.sgl',
49 'test-path-response.sgl',
50 'test-path-validation.sgl',
51 'test-path-manager.sgl',
52 'test-cid-zero.sgl',
53 'test-cid-sequence.sgl',
54 'test-local-ids.sgl',
55 'test-peer-ids.sgl',
56 'test-handler-readiness.sgl',
57 'test-early-packets.sgl',
58 'test-receive-bounds.sgl',
59 'test-handshake-probes.sgl',
60 'test-idle-timeout.sgl',
61 'test-stream.sgl',
62 'test-stream-sequence.sgl',
63 'test-endpoint.sgl',
64 'test-endpoint-admission.sgl',
65 'test-endpoint-retry.sgl',
66 'test-endpoint-network.sgl',
67 'test-address.sgl',
68 'test-endpoint-bind.sgl',
69];
70const cleanupFaults = process.argv.includes('--cleanup-faults');
71const secretCleanup = process.argv.includes('--secret-cleanup');
72const secretRetention = process.argv.includes('--secret-retention');
73const handshakeCleanup = process.argv.includes('--handshake-cleanup');
74const packetRetention = process.argv.includes('--packet-retention');
75const factoryCleanup = process.argv.includes('--factory-cleanup');
76const eventCleanup = process.argv.includes('--event-cleanup');
77const constructionCleanup = process.argv.includes('--construction-cleanup');
78const pathResponse = process.argv.includes('--path-response');
79const cidZero = process.argv.includes('--cid-zero');
80const cidSequence = process.argv.includes('--cid-sequence');
81const handlerReadiness = process.argv.includes('--handler-readiness');
82const seenBudget = process.argv.includes('--seen-budget');
83const ticketValidation = process.argv.includes('--ticket-validation');
84const negotiation = process.argv.includes('--negotiation');
85const certificatePolicy = process.argv.includes('--certificate-policy');
86const retryPolicy = process.argv.includes('--retry-policy');
87const earlyPackets = process.argv.includes('--early-packets');
88const retryPacket = process.argv.includes('--retry-packet');
89const clientRetry = process.argv.includes('--client-retry');
90const retryToken = process.argv.includes('--retry-token');
91const serverRetry = process.argv.includes('--server-retry');
92const endpointRetry = process.argv.includes('--endpoint-retry');
93const retryCleanup = process.argv.includes('--retry-cleanup');
94const datagram = process.argv.includes('--datagram');
95const pathValidation = process.argv.includes('--path-validation');
96const localIds = process.argv.includes('--local-ids');
97const peerIds = process.argv.includes('--peer-ids');
98const pathManager = process.argv.includes('--path-manager');
99const connectionNetwork = process.argv.includes('--connection-network');
100const networkIntegration = process.argv.includes('--network-integration');
101const endpointNetwork = process.argv.includes('--endpoint-network');
102const endpointBind = process.argv.includes('--endpoint-bind');
103if ([cleanupFaults, secretCleanup, secretRetention, handshakeCleanup, packetRetention, factoryCleanup, eventCleanup, constructionCleanup, pathResponse, cidZero, cidSequence, handlerReadiness, seenBudget, ticketValidation, negotiation, certificatePolicy, retryPolicy, earlyPackets, retryPacket, clientRetry, retryToken, serverRetry, endpointRetry, retryCleanup, datagram, pathValidation, localIds, peerIds, pathManager, connectionNetwork, networkIntegration, endpointNetwork, endpointBind].filter(Boolean).length > 1) {
104 throw new Error('Select one focused test suite');
106if (cleanupFaults) {
107 files.splice(0, files.length, '../scripts/cleanup-failure-tests.sgl');
109if (secretCleanup) {
110 files.splice(0, files.length, '../scripts/secret-cleanup-tests.sgl');
112if (secretRetention) {
113 files.splice(0, files.length, '../scripts/secret-retention-tests.sgl');
115if (handshakeCleanup) {
116 files.splice(0, files.length, '../scripts/handshake-cleanup-tests.sgl');
118if (packetRetention) {
119 files.splice(0, files.length, '../scripts/packet-retention-tests.sgl');
121if (factoryCleanup) {
122 files.splice(0, files.length, '../scripts/factory-cleanup-tests.sgl');
124if (eventCleanup) {
125 files.splice(0, files.length, '../scripts/event-cleanup-tests.sgl');
127if (constructionCleanup) {
128 files.splice(0, files.length, '../scripts/construction-cleanup-tests.sgl');
130if (pathResponse) {
131 files.splice(0, files.length, 'test-path-response.sgl');
133if (cidZero) {
134 files.splice(0, files.length, 'test-cid-zero.sgl');
136if (cidSequence) {
137 files.splice(0, files.length, 'test-cid-sequence.sgl');
139if (handlerReadiness) {
140 files.splice(0, files.length, 'test-handler-readiness.sgl');
142if (seenBudget) {
143 files.splice(0, files.length, 'test-tls-seen.sgl', '../scripts/seen-budget-tests.sgl');
145if (ticketValidation) {
146 files.splice(0, files.length, 'test-ticket-validation.sgl');
148if (negotiation) {
149 files.splice(0, files.length, 'test-tls-negotiation.sgl');
151if (certificatePolicy) {
152 files.splice(0, files.length, 'test-tls-certificate-policy.sgl');
154if (retryPolicy) {
155 files.splice(0, files.length, 'test-tls-retry-policy.sgl');
157if (earlyPackets) {
158 files.splice(0, files.length, 'test-early-packets.sgl');
160if (retryPacket) {
161 files.splice(0, files.length, 'test-retry-packet.sgl');
163if (clientRetry) {
164 files.splice(0, files.length, 'test-client-retry.sgl');
166if (retryToken) {
167 files.splice(0, files.length, 'test-retry-token.sgl');
169if (serverRetry) {
170 files.splice(0, files.length, 'test-server-retry.sgl');
172if (endpointRetry) {
173 files.splice(0, files.length, 'test-endpoint-retry.sgl');
175if (retryCleanup) {
176 files.splice(0, files.length, '../scripts/retry-cleanup-tests.sgl');
178if (datagram) {
179 files.splice(0, files.length, 'test-datagram-codec.sgl', 'test-datagram.sgl');
181if (pathValidation) {
182 files.splice(0, files.length, 'test-path-validation.sgl');
184if (localIds) {
185 files.splice(0, files.length, 'test-local-ids.sgl');
187if (peerIds) {
188 files.splice(0, files.length, 'test-peer-ids.sgl');
190if (pathManager) {
191 files.splice(0, files.length, 'test-path-manager.sgl');
193if (connectionNetwork) {
194 files.splice(0, files.length, 'test-connection-network.sgl');
196if (networkIntegration) {
197 files.splice(0, files.length, 'test-addressed-output.sgl', 'test-network-confirmation.sgl', 'test-network-receive.sgl', 'test-random-ids.sgl');
199if (endpointNetwork) {
200 files.splice(0, files.length, 'test-endpoint.sgl', 'test-endpoint-admission.sgl', 'test-endpoint-retry.sgl', 'test-endpoint-network.sgl');
202if (endpointBind) {
203 files.splice(0, files.length, 'test-endpoint.sgl', 'test-endpoint-admission.sgl', 'test-endpoint-retry.sgl', 'test-endpoint-network.sgl', 'test-address.sgl', 'test-endpoint-bind.sgl');
205const bodies = files.map(file => {
206 const text = readFileSync(join(root, 'test', file), 'utf8');
207 const boundary = text.indexOf('\n\n');
208 if (!text.startsWith('(import ') || boundary < 0) {
209 throw new Error(`Unexpected import header in ${file}`);
210 }
211 return text.slice(boundary + 2).trimEnd();
212});
213writeFileSync(join(source, 'native.sgl'), `(define-library (sigil quic tests native)
214 (import (sigil test)
215 (sigil math)
216 (sigil io)
217 (sigil socket)
218 (sigil process)${files.includes('test-endpoint-retry.sgl') || retryCleanup ? '\n (sigil time)' : ''}
219 (sigil crypto)
220 (sigil quic bytes)
221 (sigil quic vectors)
222 (sigil quic wire)
223 (sigil quic frame)
224 (sigil quic reassembly)
225 (sigil quic packet)${files.includes('test-retry-token.sgl') || retryCleanup ? '\n (sigil quic retry token)' : ''}
226 (sigil quic error)
227 (sigil quic connection space)
228 (sigil quic connection)${(files.includes('test-local-ids.sgl') || files.includes('test-random-ids.sgl')) ? '\n (sigil quic connection ids)' : ''}${files.includes('test-peer-ids.sgl') ? '\n (sigil quic connection peer ids)' : ''}${(files.includes('test-connection-network.sgl') || files.includes('test-network-confirmation.sgl')) ? '\n (sigil quic connection network)' : ''}
229 (sigil quic stream)
230 (sigil quic endpoint)${files.includes('test-address.sgl') || files.includes('test-endpoint-bind.sgl') ? '\n (sigil quic address)' : ''}${files.includes('test-path-validation.sgl') ? '\n (sigil quic path validation)' : ''}${files.includes('test-path-manager.sgl') ? '\n (sigil quic path manager)' : ''}
231 (sigil quic stream state)
232 (sigil quic transport parameters)
233 (sigil quic tls)
234 (sigil quic tls codec)
235 (sigil quic tls messages)
236 (sigil quic tls identity)
237 (sigil quic tls signature)
238 (sigil quic tls extensions)
239 (sigil quic tls authentication)
240 (sigil quic tls transcript)
241 (sigil quic tls vectors)${secretCleanup || secretRetention ? '\n (sigil quic tests secret spy)' : ''}${handshakeCleanup ? '\n (sigil quic tests handshake spy)' : ''}${packetRetention ? '\n (sigil quic tests packet spy)' : ''}${factoryCleanup ? '\n (sigil quic tests factory spy)' : ''}${eventCleanup ? '\n (sigil quic tests event spy)' : ''}${constructionCleanup ? '\n (sigil quic tests construction spy)' : ''}${seenBudget ? '\n (sigil quic tests seen spy)' : ''}${retryCleanup ? '\n (sigil quic tests retry spy)\n (sigil quic tests retry fixture)' : ''})
242 (export main)
243 (begin
244${bodies.map(body => body.split('\n').map(line => ' ' + line).join('\n')).join('\n\n')}
246 (define (main . args)
247 (let ((summary (run-tests-filtered (lambda (descriptor) #t))))
248 (for-each
249 (lambda (result)
250 (unless (test-result-passed? result)
251 (println "NATIVE-FAIL ~a: ~a"
252 (test-result-name result) (test-result-message result))))
253 (test-summary-results summary))
254 (println "NATIVE-TLS total=~a passed=~a failed=~a"
255 (test-summary-total summary)
256 (test-summary-passed summary)
257 (test-summary-failed summary))
258 (exit
259 (if (and (> (test-summary-total summary) 0)
260 (= (test-summary-failed summary) 0))
261 0
262 1))))))
263`);
264writeFileSync(join(output, 'package.sgl'), `(package
265 name: "sigil-quic-native-tests"
266 version: "0.1.0"
267 sigil: "0.22"
268 entry: '(sigil quic tests native)
269 dependencies: (list
270 (from-path dir: "../.." name: "sigil-quic")
271 (from-git url: "codeberg:sigil/sigil-crypto" version: "^0.18.1")
272 (from-git url: "codeberg:sigil/sigil" package: "sigil-test" version: "^0.22")
273 (from-git url: "codeberg:sigil/sigil" package: "sigil-lib" version: "^0.22")
274 (from-git url: "codeberg:sigil/sigil" package: "sigil-stdlib" version: "^0.22"))
275 configs: (list
276 (config
277 name: 'native
278 backend: 'native
279 debug?: #f
280 optimize: 2
281 bundle?: #t
282 output-dir: "build/native"))
283 bundle-name: "quic-native-tests")
284`);
285process.stdout.write(`Native test source generated in ${output}\n`);