]>
Commit | Line | Data |
---|---|---|
ed835673 HL |
1 | /* |
2 | * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use | |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | #include <openssl/ssl.h> | |
10 | #include <openssl/quic.h> | |
11 | #include <openssl/bio.h> | |
12 | #include <openssl/lhash.h> | |
13 | #include "internal/quic_tserver.h" | |
de521629 | 14 | #include "internal/quic_ssl.h" |
ed835673 | 15 | #include "testutil.h" |
a350db73 HL |
16 | #if defined(OPENSSL_THREADS) |
17 | # include "internal/thread_arch.h" | |
18 | #endif | |
ed835673 HL |
19 | |
20 | static const char *certfile, *keyfile; | |
21 | ||
a350db73 HL |
22 | #if defined(OPENSSL_THREADS) |
23 | struct child_thread_args { | |
24 | struct helper *h; | |
25 | const struct script_op *script; | |
26 | int thread_idx; | |
27 | ||
28 | CRYPTO_THREAD *t; | |
29 | CRYPTO_MUTEX *m; | |
30 | int testresult; | |
31 | int done; | |
32 | }; | |
33 | #endif | |
34 | ||
ed835673 HL |
35 | typedef struct stream_info { |
36 | const char *name; | |
37 | SSL *c_stream; | |
38 | uint64_t s_stream_id; | |
39 | } STREAM_INFO; | |
40 | ||
41 | DEFINE_LHASH_OF_EX(STREAM_INFO); | |
42 | ||
43 | struct helper { | |
44 | int s_fd; | |
45 | BIO *s_net_bio, *s_net_bio_own; | |
46 | BIO_ADDR *s_net_bio_addr; | |
47 | QUIC_TSERVER *s; | |
48 | LHASH_OF(STREAM_INFO) *s_streams; | |
49 | ||
50 | int c_fd; | |
51 | BIO *c_net_bio, *c_net_bio_own; | |
52 | SSL_CTX *c_ctx; | |
53 | SSL *c_conn; | |
54 | LHASH_OF(STREAM_INFO) *c_streams; | |
55 | ||
a350db73 HL |
56 | #if defined(OPENSSL_THREADS) |
57 | struct child_thread_args *threads; | |
58 | size_t num_threads; | |
59 | #endif | |
60 | ||
ed835673 | 61 | OSSL_TIME start_time; |
693b23e3 HL |
62 | |
63 | /* | |
64 | * This is a duration recording the amount of time we have skipped forwards | |
65 | * for testing purposes relative to the real ossl_time_now() clock. We add | |
66 | * a quantity of time to this every time we skip some time. | |
67 | */ | |
68 | CRYPTO_RWLOCK *time_lock; | |
69 | OSSL_TIME time_slip; /* protected by time_lock */ | |
70 | ||
9715e3aa | 71 | int init, blocking, check_spin_again; |
97f30fd5 | 72 | int free_order; |
ed835673 HL |
73 | }; |
74 | ||
a350db73 HL |
75 | struct helper_local { |
76 | struct helper *h; | |
77 | LHASH_OF(STREAM_INFO) *c_streams; | |
78 | int thread_idx; | |
79 | }; | |
80 | ||
ed835673 HL |
81 | struct script_op { |
82 | uint32_t op; | |
83 | const void *arg0; | |
84 | size_t arg1; | |
85 | int (*check_func)(struct helper *h, const struct script_op *op); | |
86 | const char *stream_name; | |
87 | uint64_t arg2; | |
88 | }; | |
89 | ||
90 | #define OPK_END 0 | |
91 | #define OPK_CHECK 1 | |
92 | #define OPK_C_SET_ALPN 2 | |
93 | #define OPK_C_CONNECT_WAIT 3 | |
94 | #define OPK_C_WRITE 4 | |
95 | #define OPK_S_WRITE 5 | |
96 | #define OPK_C_READ_EXPECT 6 | |
97 | #define OPK_S_READ_EXPECT 7 | |
98 | #define OPK_C_EXPECT_FIN 8 | |
99 | #define OPK_S_EXPECT_FIN 9 | |
100 | #define OPK_C_CONCLUDE 10 | |
101 | #define OPK_S_CONCLUDE 11 | |
102 | #define OPK_C_DETACH 12 | |
103 | #define OPK_C_ATTACH 13 | |
104 | #define OPK_C_NEW_STREAM 14 | |
105 | #define OPK_S_NEW_STREAM 15 | |
a350db73 | 106 | #define OPK_C_ACCEPT_STREAM_WAIT 16 |
ed835673 HL |
107 | #define OPK_C_ACCEPT_STREAM_NONE 17 |
108 | #define OPK_C_FREE_STREAM 18 | |
109 | #define OPK_C_SET_DEFAULT_STREAM_MODE 19 | |
83df44ae | 110 | #define OPK_C_SET_INCOMING_STREAM_POLICY 20 |
ed835673 HL |
111 | #define OPK_C_SHUTDOWN 21 |
112 | #define OPK_C_EXPECT_CONN_CLOSE_INFO 22 | |
113 | #define OPK_S_EXPECT_CONN_CLOSE_INFO 23 | |
114 | #define OPK_S_BIND_STREAM_ID 24 | |
115 | #define OPK_C_WAIT_FOR_DATA 25 | |
116 | #define OPK_C_WRITE_FAIL 26 | |
117 | #define OPK_S_WRITE_FAIL 27 | |
118 | #define OPK_C_READ_FAIL 28 | |
119 | #define OPK_C_STREAM_RESET 29 | |
a350db73 HL |
120 | #define OPK_S_ACCEPT_STREAM_WAIT 30 |
121 | #define OPK_NEW_THREAD 31 | |
fca44cfc HL |
122 | #define OPK_BEGIN_REPEAT 32 |
123 | #define OPK_END_REPEAT 33 | |
124 | #define OPK_S_UNBIND_STREAM_ID 34 | |
ed835673 HL |
125 | |
126 | #define EXPECT_CONN_CLOSE_APP (1U << 0) | |
127 | #define EXPECT_CONN_CLOSE_REMOTE (1U << 1) | |
128 | ||
129 | #define C_BIDI_ID(ordinal) \ | |
130 | (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_BIDI) | |
131 | #define S_BIDI_ID(ordinal) \ | |
132 | (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_BIDI) | |
133 | #define C_UNI_ID(ordinal) \ | |
134 | (((ordinal) << 2) | QUIC_STREAM_INITIATOR_CLIENT | QUIC_STREAM_DIR_UNI) | |
135 | #define S_UNI_ID(ordinal) \ | |
136 | (((ordinal) << 2) | QUIC_STREAM_INITIATOR_SERVER | QUIC_STREAM_DIR_UNI) | |
137 | ||
a350db73 HL |
138 | #define ANY_ID UINT64_MAX |
139 | ||
ed835673 HL |
140 | #define OP_END \ |
141 | {OPK_END} | |
142 | #define OP_CHECK(func, arg2) \ | |
143 | {OPK_CHECK, NULL, 0, (func), NULL, (arg2)}, | |
144 | #define OP_C_SET_ALPN(alpn) \ | |
145 | {OPK_C_SET_ALPN, (alpn), 0, NULL, NULL}, | |
146 | #define OP_C_CONNECT_WAIT() \ | |
147 | {OPK_C_CONNECT_WAIT, NULL, 0, NULL, NULL}, | |
148 | #define OP_C_WRITE(stream_name, buf, buf_len) \ | |
149 | {OPK_C_WRITE, (buf), (buf_len), NULL, #stream_name}, | |
150 | #define OP_S_WRITE(stream_name, buf, buf_len) \ | |
151 | {OPK_S_WRITE, (buf), (buf_len), NULL, #stream_name}, | |
152 | #define OP_C_READ_EXPECT(stream_name, buf, buf_len) \ | |
153 | {OPK_C_READ_EXPECT, (buf), (buf_len), NULL, #stream_name}, | |
154 | #define OP_S_READ_EXPECT(stream_name, buf, buf_len) \ | |
155 | {OPK_S_READ_EXPECT, (buf), (buf_len), NULL, #stream_name}, | |
156 | #define OP_C_EXPECT_FIN(stream_name) \ | |
157 | {OPK_C_EXPECT_FIN, NULL, 0, NULL, #stream_name}, | |
158 | #define OP_S_EXPECT_FIN(stream_name) \ | |
159 | {OPK_S_EXPECT_FIN, NULL, 0, NULL, #stream_name}, | |
160 | #define OP_C_CONCLUDE(stream_name) \ | |
161 | {OPK_C_CONCLUDE, NULL, 0, NULL, #stream_name}, | |
162 | #define OP_S_CONCLUDE(stream_name) \ | |
163 | {OPK_S_CONCLUDE, NULL, 0, NULL, #stream_name}, | |
164 | #define OP_C_DETACH(stream_name) \ | |
165 | {OPK_C_DETACH, NULL, 0, NULL, #stream_name}, | |
166 | #define OP_C_ATTACH(stream_name) \ | |
167 | {OPK_C_ATTACH, NULL, 0, NULL, #stream_name}, | |
168 | #define OP_C_NEW_STREAM_BIDI(stream_name, expect_id) \ | |
169 | {OPK_C_NEW_STREAM, NULL, 0, NULL, #stream_name, (expect_id)}, | |
170 | #define OP_C_NEW_STREAM_UNI(stream_name, expect_id) \ | |
171 | {OPK_C_NEW_STREAM, NULL, 1, NULL, #stream_name, (expect_id)}, | |
172 | #define OP_S_NEW_STREAM_BIDI(stream_name, expect_id) \ | |
173 | {OPK_S_NEW_STREAM, NULL, 0, NULL, #stream_name, (expect_id)}, | |
174 | #define OP_S_NEW_STREAM_UNI(stream_name, expect_id) \ | |
175 | {OPK_S_NEW_STREAM, NULL, 1, NULL, #stream_name, (expect_id)}, | |
a350db73 HL |
176 | #define OP_C_ACCEPT_STREAM_WAIT(stream_name) \ |
177 | {OPK_C_ACCEPT_STREAM_WAIT, NULL, 0, NULL, #stream_name}, | |
ed835673 HL |
178 | #define OP_C_ACCEPT_STREAM_NONE() \ |
179 | {OPK_C_ACCEPT_STREAM_NONE, NULL, 0, NULL, NULL}, | |
180 | #define OP_C_FREE_STREAM(stream_name) \ | |
181 | {OPK_C_FREE_STREAM, NULL, 0, NULL, #stream_name}, | |
182 | #define OP_C_SET_DEFAULT_STREAM_MODE(mode) \ | |
183 | {OPK_C_SET_DEFAULT_STREAM_MODE, NULL, (mode), NULL, NULL}, | |
83df44ae HL |
184 | #define OP_C_SET_INCOMING_STREAM_POLICY(policy) \ |
185 | {OPK_C_SET_INCOMING_STREAM_POLICY, NULL, (policy), NULL, NULL}, | |
ed835673 HL |
186 | #define OP_C_SHUTDOWN() \ |
187 | {OPK_C_SHUTDOWN, NULL, 0, NULL, NULL}, | |
188 | #define OP_C_EXPECT_CONN_CLOSE_INFO(ec, app, remote) \ | |
189 | {OPK_C_EXPECT_CONN_CLOSE_INFO, NULL, \ | |
190 | ((app) ? EXPECT_CONN_CLOSE_APP : 0) | \ | |
191 | ((remote) ? EXPECT_CONN_CLOSE_REMOTE : 0), \ | |
192 | NULL, NULL, (ec)}, | |
193 | #define OP_S_EXPECT_CONN_CLOSE_INFO(ec, app, remote) \ | |
194 | {OPK_S_EXPECT_CONN_CLOSE_INFO, NULL, \ | |
195 | ((app) ? EXPECT_CONN_CLOSE_APP : 0) | \ | |
196 | ((remote) ? EXPECT_CONN_CLOSE_REMOTE : 0), \ | |
197 | NULL, NULL, (ec)}, | |
198 | #define OP_S_BIND_STREAM_ID(stream_name, stream_id) \ | |
199 | {OPK_S_BIND_STREAM_ID, NULL, 0, NULL, #stream_name, (stream_id)}, | |
200 | #define OP_C_WAIT_FOR_DATA(stream_name) \ | |
201 | {OPK_C_WAIT_FOR_DATA, NULL, 0, NULL, #stream_name}, | |
202 | #define OP_C_WRITE_FAIL(stream_name) \ | |
203 | {OPK_C_WRITE_FAIL, NULL, 0, NULL, #stream_name}, | |
204 | #define OP_S_WRITE_FAIL(stream_name) \ | |
205 | {OPK_S_WRITE_FAIL, NULL, 0, NULL, #stream_name}, | |
206 | #define OP_C_READ_FAIL(stream_name) \ | |
207 | {OPK_C_READ_FAIL, NULL, 0, NULL, #stream_name}, | |
208 | #define OP_C_STREAM_RESET(stream_name, aec) \ | |
209 | {OPK_C_STREAM_RESET, NULL, 0, NULL, #stream_name, (aec)}, | |
a350db73 HL |
210 | #define OP_S_ACCEPT_STREAM_WAIT(stream_name) \ |
211 | {OPK_S_ACCEPT_STREAM_WAIT, NULL, 0, NULL, #stream_name}, | |
212 | #define OP_NEW_THREAD(num_threads, script) \ | |
fca44cfc HL |
213 | {OPK_NEW_THREAD, (script), (num_threads), NULL, NULL, 0 }, |
214 | #define OP_BEGIN_REPEAT(n) \ | |
215 | {OPK_BEGIN_REPEAT, NULL, (n)}, | |
216 | #define OP_END_REPEAT() \ | |
217 | {OPK_END_REPEAT}, | |
218 | #define OP_S_UNBIND_STREAM_ID(stream_name) \ | |
219 | {OPK_S_UNBIND_STREAM_ID, NULL, 0, NULL, #stream_name}, | |
ed835673 | 220 | |
693b23e3 HL |
221 | static OSSL_TIME get_time(void *arg) |
222 | { | |
223 | struct helper *h = arg; | |
224 | OSSL_TIME t; | |
225 | ||
226 | if (!TEST_true(CRYPTO_THREAD_read_lock(h->time_lock))) | |
227 | return ossl_time_zero(); | |
228 | ||
229 | t = ossl_time_add(ossl_time_now(), h->time_slip); | |
230 | ||
231 | CRYPTO_THREAD_unlock(h->time_lock); | |
232 | return t; | |
233 | } | |
234 | ||
235 | static int skip_time_ms(struct helper *h, const struct script_op *op) | |
236 | { | |
237 | if (!TEST_true(CRYPTO_THREAD_write_lock(h->time_lock))) | |
238 | return 0; | |
239 | ||
240 | h->time_slip = ossl_time_add(h->time_slip, ossl_ms2time(op->arg2)); | |
241 | ||
242 | CRYPTO_THREAD_unlock(h->time_lock); | |
243 | return 1; | |
244 | } | |
245 | ||
ed835673 HL |
246 | static int check_rejected(struct helper *h, const struct script_op *op) |
247 | { | |
248 | uint64_t stream_id = op->arg2; | |
249 | ||
9715e3aa HL |
250 | if (!ossl_quic_tserver_stream_has_peer_stop_sending(h->s, stream_id, NULL) |
251 | || !ossl_quic_tserver_stream_has_peer_reset_stream(h->s, stream_id, NULL)) { | |
252 | h->check_spin_again = 1; | |
ed835673 | 253 | return 0; |
9715e3aa | 254 | } |
ed835673 HL |
255 | |
256 | return 1; | |
257 | } | |
258 | ||
259 | static int check_stream_reset(struct helper *h, const struct script_op *op) | |
260 | { | |
261 | uint64_t stream_id = op->arg2, aec = 0; | |
262 | ||
9715e3aa HL |
263 | if (!ossl_quic_tserver_stream_has_peer_reset_stream(h->s, stream_id, &aec)) { |
264 | h->check_spin_again = 1; | |
265 | return 0; | |
266 | } | |
267 | ||
268 | return TEST_uint64_t_eq(aec, 42); | |
ed835673 HL |
269 | } |
270 | ||
271 | static int check_stream_stopped(struct helper *h, const struct script_op *op) | |
272 | { | |
273 | uint64_t stream_id = op->arg2; | |
274 | ||
9715e3aa HL |
275 | if (!ossl_quic_tserver_stream_has_peer_stop_sending(h->s, stream_id, NULL)) { |
276 | h->check_spin_again = 1; | |
277 | return 0; | |
278 | } | |
279 | ||
280 | return 1; | |
ed835673 HL |
281 | } |
282 | ||
693b23e3 HL |
283 | static int override_key_update(struct helper *h, const struct script_op *op) |
284 | { | |
285 | QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn); | |
286 | ||
287 | ossl_quic_channel_set_txku_threshold_override(ch, op->arg2); | |
288 | return 1; | |
289 | } | |
290 | ||
2525109f HL |
291 | static int trigger_key_update(struct helper *h, const struct script_op *op) |
292 | { | |
293 | if (!TEST_true(SSL_key_update(h->c_conn, SSL_KEY_UPDATE_REQUESTED))) | |
294 | return 0; | |
295 | ||
296 | return 1; | |
297 | } | |
298 | ||
693b23e3 HL |
299 | static int check_key_update_ge(struct helper *h, const struct script_op *op) |
300 | { | |
301 | QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn); | |
302 | int64_t txke = (int64_t)ossl_quic_channel_get_tx_key_epoch(ch); | |
303 | int64_t rxke = (int64_t)ossl_quic_channel_get_rx_key_epoch(ch); | |
304 | int64_t diff = txke - rxke; | |
305 | ||
306 | /* | |
307 | * TXKE must always be equal to or ahead of RXKE. | |
308 | * It can be ahead of RXKE by at most 1. | |
309 | */ | |
310 | if (!TEST_int64_t_ge(diff, 0) || !TEST_int64_t_le(diff, 1)) | |
311 | return 0; | |
312 | ||
313 | /* Caller specifies a minimum number of RXKEs which must have happened. */ | |
314 | if (!TEST_uint64_t_ge((uint64_t)rxke, op->arg2)) | |
315 | return 0; | |
316 | ||
317 | return 1; | |
318 | } | |
319 | ||
320 | static int check_key_update_lt(struct helper *h, const struct script_op *op) | |
321 | { | |
322 | QUIC_CHANNEL *ch = ossl_quic_conn_get_channel(h->c_conn); | |
323 | uint64_t txke = ossl_quic_channel_get_tx_key_epoch(ch); | |
324 | ||
325 | /* Caller specifies a maximum number of TXKEs which must have happened. */ | |
326 | if (!TEST_uint64_t_lt(txke, op->arg2)) | |
327 | return 0; | |
328 | ||
329 | return 1; | |
330 | } | |
331 | ||
ed835673 HL |
332 | static unsigned long stream_info_hash(const STREAM_INFO *info) |
333 | { | |
334 | return OPENSSL_LH_strhash(info->name); | |
335 | } | |
336 | ||
337 | static int stream_info_cmp(const STREAM_INFO *a, const STREAM_INFO *b) | |
338 | { | |
339 | return strcmp(a->name, b->name); | |
340 | } | |
341 | ||
342 | static void cleanup_stream(STREAM_INFO *info) | |
343 | { | |
344 | SSL_free(info->c_stream); | |
345 | OPENSSL_free(info); | |
346 | } | |
347 | ||
348 | static void helper_cleanup_streams(LHASH_OF(STREAM_INFO) **lh) | |
349 | { | |
350 | if (*lh == NULL) | |
351 | return; | |
352 | ||
353 | lh_STREAM_INFO_doall(*lh, cleanup_stream); | |
354 | lh_STREAM_INFO_free(*lh); | |
355 | *lh = NULL; | |
356 | } | |
357 | ||
a350db73 HL |
358 | #if defined(OPENSSL_THREADS) |
359 | static CRYPTO_THREAD_RETVAL run_script_child_thread(void *arg); | |
360 | ||
361 | static int join_threads(struct child_thread_args *threads, size_t num_threads) | |
362 | { | |
363 | int ok = 1; | |
364 | size_t i; | |
365 | CRYPTO_THREAD_RETVAL rv; | |
366 | ||
367 | for (i = 0; i < num_threads; ++i) { | |
368 | if (threads[i].t != NULL) { | |
369 | ossl_crypto_thread_native_join(threads[i].t, &rv); | |
370 | ||
371 | if (!threads[i].testresult) | |
372 | /* Do not log failure here, worker will do it. */ | |
373 | ok = 0; | |
374 | ||
375 | ossl_crypto_thread_native_clean(threads[i].t); | |
376 | threads[i].t = NULL; | |
377 | } | |
378 | ||
379 | ossl_crypto_mutex_free(&threads[i].m); | |
380 | } | |
381 | ||
382 | return ok; | |
383 | } | |
384 | #endif | |
385 | ||
ed835673 HL |
386 | static void helper_cleanup(struct helper *h) |
387 | { | |
a350db73 HL |
388 | #if defined(OPENSSL_THREADS) |
389 | join_threads(h->threads, h->num_threads); | |
390 | OPENSSL_free(h->threads); | |
391 | h->threads = NULL; | |
392 | h->num_threads = 0; | |
393 | #endif | |
394 | ||
97f30fd5 HL |
395 | if (h->free_order == 0) { |
396 | /* order 0: streams, then conn */ | |
397 | helper_cleanup_streams(&h->c_streams); | |
398 | ||
399 | SSL_free(h->c_conn); | |
400 | h->c_conn = NULL; | |
401 | } else { | |
402 | /* order 1: conn, then streams */ | |
403 | SSL_free(h->c_conn); | |
404 | h->c_conn = NULL; | |
405 | ||
406 | helper_cleanup_streams(&h->c_streams); | |
407 | } | |
ed835673 | 408 | |
97f30fd5 | 409 | helper_cleanup_streams(&h->s_streams); |
ed835673 HL |
410 | ossl_quic_tserver_free(h->s); |
411 | h->s = NULL; | |
412 | ||
413 | BIO_free(h->s_net_bio_own); | |
414 | h->s_net_bio_own = NULL; | |
415 | ||
416 | BIO_free(h->c_net_bio_own); | |
417 | h->c_net_bio_own = NULL; | |
418 | ||
419 | if (h->s_fd >= 0) { | |
420 | BIO_closesocket(h->s_fd); | |
421 | h->s_fd = -1; | |
422 | } | |
423 | ||
424 | if (h->c_fd >= 0) { | |
425 | BIO_closesocket(h->c_fd); | |
426 | h->c_fd = -1; | |
427 | } | |
428 | ||
429 | BIO_ADDR_free(h->s_net_bio_addr); | |
430 | h->s_net_bio_addr = NULL; | |
431 | ||
432 | SSL_CTX_free(h->c_ctx); | |
433 | h->c_ctx = NULL; | |
693b23e3 HL |
434 | |
435 | CRYPTO_THREAD_lock_free(h->time_lock); | |
436 | h->time_lock = NULL; | |
ed835673 HL |
437 | } |
438 | ||
97f30fd5 | 439 | static int helper_init(struct helper *h, int free_order) |
ed835673 HL |
440 | { |
441 | short port = 8186; | |
442 | struct in_addr ina = {0}; | |
443 | QUIC_TSERVER_ARGS s_args = {0}; | |
444 | ||
445 | memset(h, 0, sizeof(*h)); | |
446 | h->c_fd = -1; | |
447 | h->s_fd = -1; | |
97f30fd5 | 448 | h->free_order = free_order; |
693b23e3 HL |
449 | h->time_slip = ossl_time_zero(); |
450 | ||
451 | if (!TEST_ptr(h->time_lock = CRYPTO_THREAD_lock_new())) | |
452 | goto err; | |
ed835673 HL |
453 | |
454 | if (!TEST_ptr(h->s_streams = lh_STREAM_INFO_new(stream_info_hash, | |
455 | stream_info_cmp))) | |
456 | goto err; | |
457 | ||
458 | if (!TEST_ptr(h->c_streams = lh_STREAM_INFO_new(stream_info_hash, | |
459 | stream_info_cmp))) | |
460 | goto err; | |
461 | ||
462 | ina.s_addr = htonl(0x7f000001UL); | |
463 | ||
464 | h->s_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0); | |
465 | if (!TEST_int_ge(h->s_fd, 0)) | |
466 | goto err; | |
467 | ||
468 | if (!TEST_true(BIO_socket_nbio(h->s_fd, 1))) | |
469 | goto err; | |
470 | ||
471 | if (!TEST_ptr(h->s_net_bio_addr = BIO_ADDR_new())) | |
472 | goto err; | |
473 | ||
474 | if (!TEST_true(BIO_ADDR_rawmake(h->s_net_bio_addr, AF_INET, &ina, sizeof(ina), | |
475 | htons(port)))) | |
476 | goto err; | |
477 | ||
478 | if (!TEST_true(BIO_bind(h->s_fd, h->s_net_bio_addr, 0))) | |
479 | goto err; | |
480 | ||
481 | if (!TEST_int_gt(BIO_ADDR_rawport(h->s_net_bio_addr), 0)) | |
482 | goto err; | |
483 | ||
484 | if (!TEST_ptr(h->s_net_bio = h->s_net_bio_own = BIO_new_dgram(h->s_fd, 0))) | |
485 | goto err; | |
486 | ||
487 | if (!BIO_up_ref(h->s_net_bio)) | |
488 | goto err; | |
489 | ||
693b23e3 HL |
490 | s_args.net_rbio = h->s_net_bio; |
491 | s_args.net_wbio = h->s_net_bio; | |
37f27b91 | 492 | s_args.alpn = NULL; |
693b23e3 HL |
493 | s_args.now_cb = get_time; |
494 | s_args.now_cb_arg = h; | |
ed835673 HL |
495 | |
496 | if (!TEST_ptr(h->s = ossl_quic_tserver_new(&s_args, certfile, keyfile))) | |
497 | goto err; | |
498 | ||
499 | h->s_net_bio_own = NULL; | |
500 | ||
501 | h->c_fd = BIO_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0); | |
502 | if (!TEST_int_ge(h->c_fd, 0)) | |
503 | goto err; | |
504 | ||
505 | if (!TEST_true(BIO_socket_nbio(h->c_fd, 1))) | |
506 | goto err; | |
507 | ||
508 | if (!TEST_ptr(h->c_net_bio = h->c_net_bio_own = BIO_new_dgram(h->c_fd, 0))) | |
509 | goto err; | |
510 | ||
511 | if (!TEST_true(BIO_dgram_set_peer(h->c_net_bio, h->s_net_bio_addr))) | |
512 | goto err; | |
513 | ||
514 | ||
515 | if (!TEST_ptr(h->c_ctx = SSL_CTX_new(OSSL_QUIC_client_method()))) | |
516 | goto err; | |
517 | ||
518 | if (!TEST_ptr(h->c_conn = SSL_new(h->c_ctx))) | |
519 | goto err; | |
520 | ||
693b23e3 HL |
521 | /* Use custom time function for virtual time skip. */ |
522 | if (!TEST_true(ossl_quic_conn_set_override_now_cb(h->c_conn, get_time, h))) | |
523 | goto err; | |
524 | ||
ed835673 HL |
525 | /* Takes ownership of our reference to the BIO. */ |
526 | SSL_set0_rbio(h->c_conn, h->c_net_bio); | |
527 | h->c_net_bio_own = NULL; | |
528 | ||
529 | if (!TEST_true(BIO_up_ref(h->c_net_bio))) | |
530 | goto err; | |
531 | ||
532 | SSL_set0_wbio(h->c_conn, h->c_net_bio); | |
533 | ||
534 | if (!TEST_true(SSL_set_blocking_mode(h->c_conn, 0))) | |
535 | goto err; | |
536 | ||
537 | h->start_time = ossl_time_now(); | |
538 | h->init = 1; | |
539 | return 1; | |
540 | ||
541 | err: | |
542 | helper_cleanup(h); | |
543 | return 0; | |
544 | } | |
545 | ||
a350db73 HL |
546 | static int helper_local_init(struct helper_local *hl, struct helper *h, |
547 | int thread_idx) | |
548 | { | |
549 | hl->h = h; | |
550 | hl->c_streams = NULL; | |
551 | hl->thread_idx = thread_idx; | |
552 | ||
553 | if (!TEST_ptr(h)) | |
554 | return 0; | |
555 | ||
556 | if (thread_idx < 0) { | |
557 | hl->c_streams = h->c_streams; | |
558 | } else { | |
559 | if (!TEST_ptr(hl->c_streams = lh_STREAM_INFO_new(stream_info_hash, | |
560 | stream_info_cmp))) | |
561 | return 0; | |
562 | } | |
563 | ||
564 | return 1; | |
565 | } | |
566 | ||
567 | static void helper_local_cleanup(struct helper_local *hl) | |
568 | { | |
569 | if (hl->h == NULL) | |
570 | return; | |
571 | ||
572 | if (hl->thread_idx >= 0) | |
573 | helper_cleanup_streams(&hl->c_streams); | |
574 | ||
575 | hl->h = NULL; | |
576 | } | |
577 | ||
ed835673 HL |
578 | static STREAM_INFO *get_stream_info(LHASH_OF(STREAM_INFO) *lh, |
579 | const char *stream_name) | |
580 | { | |
581 | STREAM_INFO key, *info; | |
582 | ||
583 | if (!TEST_ptr(stream_name)) | |
584 | return NULL; | |
585 | ||
586 | if (!strcmp(stream_name, "DEFAULT")) | |
587 | return NULL; | |
588 | ||
589 | key.name = stream_name; | |
590 | info = lh_STREAM_INFO_retrieve(lh, &key); | |
591 | if (info == NULL) { | |
592 | info = OPENSSL_zalloc(sizeof(*info)); | |
593 | if (info == NULL) | |
594 | return NULL; | |
595 | ||
596 | info->name = stream_name; | |
597 | info->s_stream_id = UINT64_MAX; | |
598 | lh_STREAM_INFO_insert(lh, info); | |
599 | } | |
600 | ||
601 | return info; | |
602 | } | |
603 | ||
a350db73 HL |
604 | static int helper_local_set_c_stream(struct helper_local *hl, |
605 | const char *stream_name, | |
606 | SSL *c_stream) | |
ed835673 | 607 | { |
a350db73 | 608 | STREAM_INFO *info = get_stream_info(hl->c_streams, stream_name); |
ed835673 HL |
609 | |
610 | if (info == NULL) | |
611 | return 0; | |
612 | ||
613 | info->c_stream = c_stream; | |
614 | info->s_stream_id = UINT64_MAX; | |
615 | return 1; | |
616 | } | |
617 | ||
a350db73 HL |
618 | static SSL *helper_local_get_c_stream(struct helper_local *hl, |
619 | const char *stream_name) | |
ed835673 HL |
620 | { |
621 | STREAM_INFO *info; | |
622 | ||
623 | if (!strcmp(stream_name, "DEFAULT")) | |
a350db73 | 624 | return hl->h->c_conn; |
ed835673 | 625 | |
a350db73 | 626 | info = get_stream_info(hl->c_streams, stream_name); |
ed835673 HL |
627 | if (info == NULL) |
628 | return NULL; | |
629 | ||
630 | return info->c_stream; | |
631 | } | |
632 | ||
633 | static int | |
634 | helper_set_s_stream(struct helper *h, const char *stream_name, | |
635 | uint64_t s_stream_id) | |
636 | { | |
637 | STREAM_INFO *info; | |
638 | ||
639 | if (!strcmp(stream_name, "DEFAULT")) | |
640 | return 0; | |
641 | ||
642 | info = get_stream_info(h->s_streams, stream_name); | |
643 | if (info == NULL) | |
644 | return 0; | |
645 | ||
646 | info->c_stream = NULL; | |
647 | info->s_stream_id = s_stream_id; | |
648 | return 1; | |
649 | } | |
650 | ||
651 | static uint64_t helper_get_s_stream(struct helper *h, const char *stream_name) | |
652 | { | |
653 | STREAM_INFO *info; | |
654 | ||
655 | if (!strcmp(stream_name, "DEFAULT")) | |
656 | return UINT64_MAX; | |
657 | ||
658 | info = get_stream_info(h->s_streams, stream_name); | |
659 | if (info == NULL) | |
660 | return UINT64_MAX; | |
661 | ||
662 | return info->s_stream_id; | |
663 | } | |
664 | ||
665 | static int is_want(SSL *s, int ret) | |
666 | { | |
667 | int ec = SSL_get_error(s, ret); | |
668 | ||
669 | return ec == SSL_ERROR_WANT_READ || ec == SSL_ERROR_WANT_WRITE; | |
670 | } | |
671 | ||
a350db73 HL |
672 | static int run_script_worker(struct helper *h, const struct script_op *script, |
673 | int thread_idx) | |
ed835673 | 674 | { |
ed835673 | 675 | int testresult = 0; |
ed835673 HL |
676 | unsigned char *tmp_buf = NULL; |
677 | int connect_started = 0; | |
9715e3aa | 678 | size_t offset = 0; |
a350db73 HL |
679 | size_t op_idx = 0; |
680 | const struct script_op *op = NULL; | |
629b408c HL |
681 | int no_advance = 0, first = 1; |
682 | #if defined(OPENSSL_THREADS) | |
683 | int end_wait_warning = 0; | |
684 | #endif | |
a350db73 HL |
685 | OSSL_TIME op_start_time = ossl_time_zero(), op_deadline = ossl_time_zero(); |
686 | struct helper_local hl; | |
fca44cfc HL |
687 | #define REPEAT_SLOTS 8 |
688 | size_t repeat_stack_idx[REPEAT_SLOTS], repeat_stack_done[REPEAT_SLOTS]; | |
689 | size_t repeat_stack_limit[REPEAT_SLOTS]; | |
690 | size_t repeat_stack_len = 0; | |
ed835673 | 691 | |
a350db73 | 692 | if (!TEST_true(helper_local_init(&hl, h, thread_idx))) |
ed835673 HL |
693 | goto out; |
694 | ||
2525109f | 695 | #define SPIN_AGAIN() { OSSL_sleep(1); no_advance = 1; continue; } |
ed835673 | 696 | |
9715e3aa | 697 | for (;;) { |
a350db73 | 698 | SSL *c_tgt = h->c_conn; |
ed835673 HL |
699 | uint64_t s_stream_id = UINT64_MAX; |
700 | ||
9715e3aa HL |
701 | if (no_advance) { |
702 | no_advance = 0; | |
703 | } else { | |
704 | if (!first) | |
705 | ++op_idx; | |
706 | ||
707 | first = 0; | |
4f2d32d6 | 708 | offset = 0; |
9715e3aa HL |
709 | op_start_time = ossl_time_now(); |
710 | op_deadline = ossl_time_add(op_start_time, ossl_ms2time(2000)); | |
711 | } | |
712 | ||
713 | if (!TEST_int_le(ossl_time_compare(ossl_time_now(), op_deadline), 0)) { | |
a350db73 | 714 | TEST_error("op %zu timed out on thread %d", op_idx + 1, thread_idx); |
9715e3aa HL |
715 | goto out; |
716 | } | |
717 | ||
ed835673 HL |
718 | op = &script[op_idx]; |
719 | ||
720 | if (op->stream_name != NULL) { | |
a350db73 HL |
721 | c_tgt = helper_local_get_c_stream(&hl, op->stream_name); |
722 | if (thread_idx < 0) | |
723 | s_stream_id = helper_get_s_stream(h, op->stream_name); | |
724 | else | |
725 | s_stream_id = UINT64_MAX; | |
726 | } | |
727 | ||
7ba8f79a | 728 | if (thread_idx < 0) |
a350db73 | 729 | ossl_quic_tserver_tick(h->s); |
7ba8f79a HL |
730 | |
731 | if (thread_idx >= 0 || connect_started) | |
6084e04b | 732 | SSL_handle_events(h->c_conn); |
ed835673 | 733 | |
a350db73 HL |
734 | if (thread_idx >= 0) { |
735 | /* Only allow certain opcodes on child threads. */ | |
736 | switch (op->op) { | |
737 | case OPK_END: | |
738 | case OPK_C_ACCEPT_STREAM_WAIT: | |
739 | case OPK_C_NEW_STREAM: | |
740 | case OPK_C_READ_EXPECT: | |
741 | case OPK_C_EXPECT_FIN: | |
742 | case OPK_C_WRITE: | |
743 | case OPK_C_CONCLUDE: | |
744 | case OPK_C_FREE_STREAM: | |
fca44cfc HL |
745 | case OPK_BEGIN_REPEAT: |
746 | case OPK_END_REPEAT: | |
a350db73 HL |
747 | break; |
748 | ||
749 | default: | |
0cea6df2 MC |
750 | TEST_error("opcode %lu not allowed on child thread", |
751 | (unsigned long)op->op); | |
a350db73 HL |
752 | goto out; |
753 | } | |
754 | } | |
ed835673 HL |
755 | |
756 | switch (op->op) { | |
757 | case OPK_END: | |
fca44cfc HL |
758 | if (!TEST_size_t_eq(repeat_stack_len, 0)) |
759 | goto out; | |
760 | ||
629b408c | 761 | #if defined(OPENSSL_THREADS) |
a350db73 HL |
762 | if (thread_idx < 0) { |
763 | int done; | |
764 | size_t i; | |
765 | ||
766 | for (i = 0; i < h->num_threads; ++i) { | |
767 | if (h->threads[i].m == NULL) | |
768 | continue; | |
769 | ||
770 | ossl_crypto_mutex_lock(h->threads[i].m); | |
771 | done = h->threads[i].done; | |
772 | ossl_crypto_mutex_unlock(h->threads[i].m); | |
773 | ||
774 | if (!done) { | |
775 | if (!end_wait_warning) { | |
776 | TEST_info("still waiting for other threads to finish (%zu)", i); | |
777 | end_wait_warning = 1; | |
778 | } | |
779 | ||
780 | SPIN_AGAIN(); | |
781 | } | |
782 | } | |
783 | } | |
629b408c | 784 | #endif |
a350db73 HL |
785 | |
786 | TEST_info("script finished on thread %d", thread_idx); | |
ed835673 HL |
787 | testresult = 1; |
788 | goto out; | |
789 | ||
fca44cfc HL |
790 | case OPK_BEGIN_REPEAT: |
791 | if (!TEST_size_t_lt(repeat_stack_len, OSSL_NELEM(repeat_stack_idx))) | |
792 | goto out; | |
793 | ||
794 | if (!TEST_size_t_gt(op->arg1, 0)) | |
795 | goto out; | |
796 | ||
797 | repeat_stack_idx[repeat_stack_len] = op_idx + 1; | |
798 | repeat_stack_done[repeat_stack_len] = 0; | |
799 | repeat_stack_limit[repeat_stack_len] = op->arg1; | |
800 | ++repeat_stack_len; | |
801 | break; | |
802 | ||
803 | case OPK_END_REPEAT: | |
804 | if (!TEST_size_t_gt(repeat_stack_len, 0)) | |
805 | goto out; | |
806 | ||
807 | if (++repeat_stack_done[repeat_stack_len - 1] | |
808 | == repeat_stack_limit[repeat_stack_len - 1]) { | |
809 | --repeat_stack_len; | |
810 | } else { | |
811 | op_idx = repeat_stack_idx[repeat_stack_len - 1]; | |
812 | no_advance = 1; | |
813 | continue; | |
814 | } | |
815 | ||
816 | break; | |
817 | ||
ed835673 | 818 | case OPK_CHECK: |
9715e3aa | 819 | { |
a350db73 HL |
820 | int ok = op->check_func(h, op); |
821 | if (h->check_spin_again) { | |
822 | h->check_spin_again = 0; | |
9715e3aa HL |
823 | SPIN_AGAIN(); |
824 | } | |
ed835673 | 825 | |
9715e3aa HL |
826 | if (!TEST_true(ok)) |
827 | goto out; | |
828 | } | |
ed835673 HL |
829 | break; |
830 | ||
831 | case OPK_C_SET_ALPN: | |
832 | { | |
833 | const char *alpn = op->arg0; | |
834 | size_t alpn_len = strlen(alpn); | |
835 | ||
836 | if (!TEST_size_t_le(alpn_len, UINT8_MAX) | |
837 | || !TEST_ptr(tmp_buf = (unsigned char *)OPENSSL_malloc(alpn_len + 1))) | |
838 | goto out; | |
839 | ||
840 | memcpy(tmp_buf + 1, alpn, alpn_len); | |
841 | tmp_buf[0] = (unsigned char)alpn_len; | |
842 | ||
843 | /* 0 is the success case for SSL_set_alpn_protos(). */ | |
a350db73 | 844 | if (!TEST_false(SSL_set_alpn_protos(h->c_conn, tmp_buf, |
ed835673 HL |
845 | alpn_len + 1))) |
846 | goto out; | |
847 | ||
848 | OPENSSL_free(tmp_buf); | |
849 | tmp_buf = NULL; | |
850 | } | |
851 | break; | |
852 | ||
853 | case OPK_C_CONNECT_WAIT: | |
854 | { | |
855 | int ret; | |
856 | ||
857 | connect_started = 1; | |
858 | ||
a350db73 | 859 | ret = SSL_connect(h->c_conn); |
ed835673 | 860 | if (!TEST_true(ret == 1 |
a350db73 | 861 | || (!h->blocking && is_want(h->c_conn, ret)))) |
ed835673 HL |
862 | goto out; |
863 | ||
a350db73 | 864 | if (!h->blocking && ret != 1) |
ed835673 HL |
865 | SPIN_AGAIN(); |
866 | } | |
867 | break; | |
868 | ||
869 | case OPK_C_WRITE: | |
870 | { | |
871 | size_t bytes_written = 0; | |
872 | ||
873 | if (!TEST_ptr(c_tgt)) | |
874 | goto out; | |
875 | ||
876 | if (!TEST_true(SSL_write_ex(c_tgt, op->arg0, op->arg1, | |
877 | &bytes_written)) | |
878 | || !TEST_size_t_eq(bytes_written, op->arg1)) | |
879 | goto out; | |
880 | } | |
881 | break; | |
882 | ||
883 | case OPK_S_WRITE: | |
884 | { | |
885 | size_t bytes_written = 0; | |
886 | ||
887 | if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX)) | |
888 | goto out; | |
889 | ||
a350db73 | 890 | if (!TEST_true(ossl_quic_tserver_write(h->s, s_stream_id, |
ed835673 HL |
891 | op->arg0, op->arg1, |
892 | &bytes_written)) | |
893 | || !TEST_size_t_eq(bytes_written, op->arg1)) | |
894 | goto out; | |
895 | } | |
896 | break; | |
897 | ||
898 | case OPK_C_CONCLUDE: | |
899 | { | |
900 | if (!TEST_true(SSL_stream_conclude(c_tgt, 0))) | |
901 | goto out; | |
902 | } | |
903 | break; | |
904 | ||
905 | case OPK_S_CONCLUDE: | |
906 | { | |
907 | if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX)) | |
908 | goto out; | |
909 | ||
a350db73 | 910 | ossl_quic_tserver_conclude(h->s, s_stream_id); |
ed835673 HL |
911 | } |
912 | break; | |
913 | ||
914 | case OPK_C_WAIT_FOR_DATA: | |
915 | { | |
916 | char buf[1]; | |
917 | size_t bytes_read = 0; | |
918 | ||
919 | if (!TEST_ptr(c_tgt)) | |
920 | goto out; | |
921 | ||
922 | if (!SSL_peek_ex(c_tgt, buf, sizeof(buf), &bytes_read) | |
923 | || bytes_read == 0) | |
924 | SPIN_AGAIN(); | |
925 | } | |
926 | break; | |
927 | ||
928 | case OPK_C_READ_EXPECT: | |
929 | { | |
930 | size_t bytes_read = 0; | |
931 | ||
9715e3aa HL |
932 | if (op->arg1 > 0 && tmp_buf == NULL |
933 | && !TEST_ptr(tmp_buf = OPENSSL_malloc(op->arg1))) | |
ed835673 HL |
934 | goto out; |
935 | ||
9715e3aa HL |
936 | if (!SSL_read_ex(c_tgt, tmp_buf + offset, op->arg1 - offset, |
937 | &bytes_read)) | |
938 | SPIN_AGAIN(); | |
ed835673 | 939 | |
9715e3aa HL |
940 | if (bytes_read + offset != op->arg1) { |
941 | offset += bytes_read; | |
942 | SPIN_AGAIN(); | |
943 | } | |
944 | ||
945 | if (op->arg1 > 0 | |
946 | && !TEST_mem_eq(tmp_buf, op->arg1, op->arg0, op->arg1)) | |
ed835673 HL |
947 | goto out; |
948 | ||
949 | OPENSSL_free(tmp_buf); | |
950 | tmp_buf = NULL; | |
951 | } | |
952 | break; | |
953 | ||
954 | case OPK_S_READ_EXPECT: | |
955 | { | |
956 | size_t bytes_read = 0; | |
957 | ||
958 | if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX)) | |
959 | goto out; | |
960 | ||
9715e3aa | 961 | if (op->arg1 > 0 && tmp_buf == NULL |
ed835673 HL |
962 | && !TEST_ptr(tmp_buf = OPENSSL_malloc(op->arg1))) |
963 | goto out; | |
964 | ||
a350db73 | 965 | if (!TEST_true(ossl_quic_tserver_read(h->s, s_stream_id, |
9715e3aa HL |
966 | tmp_buf + offset, |
967 | op->arg1 - offset, | |
968 | &bytes_read))) | |
ed835673 HL |
969 | goto out; |
970 | ||
9715e3aa HL |
971 | if (bytes_read + offset != op->arg1) { |
972 | offset += bytes_read; | |
973 | SPIN_AGAIN(); | |
974 | } | |
975 | ||
ed835673 HL |
976 | if (op->arg1 > 0 |
977 | && !TEST_mem_eq(tmp_buf, op->arg1, op->arg0, op->arg1)) | |
978 | goto out; | |
979 | ||
980 | OPENSSL_free(tmp_buf); | |
981 | tmp_buf = NULL; | |
982 | } | |
983 | break; | |
984 | ||
985 | case OPK_C_EXPECT_FIN: | |
986 | { | |
987 | char buf[1]; | |
988 | size_t bytes_read = 0; | |
989 | ||
990 | if (!TEST_false(SSL_read_ex(c_tgt, buf, sizeof(buf), | |
991 | &bytes_read)) | |
9715e3aa HL |
992 | || !TEST_size_t_eq(bytes_read, 0)) |
993 | goto out; | |
994 | ||
995 | if (is_want(c_tgt, 0)) | |
996 | SPIN_AGAIN(); | |
997 | ||
998 | if (!TEST_int_eq(SSL_get_error(c_tgt, 0), | |
999 | SSL_ERROR_ZERO_RETURN)) | |
1000 | goto out; | |
ed835673 HL |
1001 | } |
1002 | break; | |
1003 | ||
1004 | case OPK_S_EXPECT_FIN: | |
1005 | { | |
9715e3aa | 1006 | if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX)) |
ed835673 | 1007 | goto out; |
9715e3aa | 1008 | |
a350db73 | 1009 | if (!ossl_quic_tserver_has_read_ended(h->s, s_stream_id)) |
9715e3aa | 1010 | SPIN_AGAIN(); |
ed835673 HL |
1011 | } |
1012 | break; | |
1013 | ||
1014 | case OPK_C_DETACH: | |
1015 | { | |
1016 | SSL *c_stream; | |
1017 | ||
1018 | if (!TEST_ptr_null(c_tgt)) | |
1019 | goto out; /* don't overwrite existing stream with same name */ | |
1020 | ||
a350db73 | 1021 | if (!TEST_ptr(c_stream = ossl_quic_detach_stream(h->c_conn))) |
ed835673 HL |
1022 | goto out; |
1023 | ||
a350db73 | 1024 | if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name, c_stream))) |
ed835673 HL |
1025 | goto out; |
1026 | } | |
1027 | break; | |
1028 | ||
1029 | case OPK_C_ATTACH: | |
1030 | { | |
1031 | if (!TEST_ptr(c_tgt)) | |
1032 | goto out; | |
1033 | ||
a350db73 | 1034 | if (!TEST_true(ossl_quic_attach_stream(h->c_conn, c_tgt))) |
ed835673 HL |
1035 | goto out; |
1036 | ||
a350db73 | 1037 | if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name, NULL))) |
ed835673 HL |
1038 | goto out; |
1039 | } | |
1040 | break; | |
1041 | ||
1042 | case OPK_C_NEW_STREAM: | |
1043 | { | |
1044 | SSL *c_stream; | |
1045 | uint64_t flags = 0; | |
1046 | ||
1047 | if (!TEST_ptr_null(c_tgt)) | |
1048 | goto out; /* don't overwrite existing stream with same name */ | |
1049 | ||
1050 | if (op->arg1 != 0) | |
1051 | flags |= SSL_STREAM_FLAG_UNI; | |
1052 | ||
a350db73 | 1053 | if (!TEST_ptr(c_stream = SSL_new_stream(h->c_conn, flags))) |
ed835673 HL |
1054 | goto out; |
1055 | ||
1056 | if (op->arg2 != UINT64_MAX | |
1057 | && !TEST_uint64_t_eq(SSL_get_stream_id(c_stream), | |
1058 | op->arg2)) | |
1059 | goto out; | |
1060 | ||
a350db73 | 1061 | if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name, c_stream))) |
ed835673 HL |
1062 | goto out; |
1063 | } | |
1064 | break; | |
1065 | ||
1066 | case OPK_S_NEW_STREAM: | |
1067 | { | |
1068 | uint64_t stream_id = UINT64_MAX; | |
1069 | ||
1070 | if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX)) | |
1071 | goto out; /* don't overwrite existing stream with same name */ | |
1072 | ||
a350db73 | 1073 | if (!TEST_true(ossl_quic_tserver_stream_new(h->s, |
ed835673 HL |
1074 | op->arg1 > 0, |
1075 | &stream_id))) | |
1076 | goto out; | |
1077 | ||
1078 | if (op->arg2 != UINT64_MAX | |
1079 | && !TEST_uint64_t_eq(stream_id, op->arg2)) | |
1080 | goto out; | |
1081 | ||
a350db73 | 1082 | if (!TEST_true(helper_set_s_stream(h, op->stream_name, |
ed835673 HL |
1083 | stream_id))) |
1084 | goto out; | |
1085 | } | |
1086 | break; | |
1087 | ||
a350db73 | 1088 | case OPK_C_ACCEPT_STREAM_WAIT: |
ed835673 HL |
1089 | { |
1090 | SSL *c_stream; | |
1091 | ||
1092 | if (!TEST_ptr_null(c_tgt)) | |
1093 | goto out; /* don't overwrite existing stream with same name */ | |
1094 | ||
a350db73 | 1095 | if ((c_stream = SSL_accept_stream(h->c_conn, 0)) == NULL) |
9715e3aa | 1096 | SPIN_AGAIN(); |
ed835673 | 1097 | |
a350db73 HL |
1098 | if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name, |
1099 | c_stream))) | |
1100 | goto out; | |
1101 | } | |
1102 | break; | |
1103 | ||
1104 | case OPK_S_ACCEPT_STREAM_WAIT: | |
1105 | { | |
1106 | uint64_t new_stream_id; | |
1107 | ||
1108 | if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX)) | |
1109 | goto out; | |
1110 | ||
1111 | new_stream_id = ossl_quic_tserver_pop_incoming_stream(h->s); | |
1112 | if (new_stream_id == UINT64_MAX) | |
1113 | SPIN_AGAIN(); | |
1114 | ||
1115 | if (!TEST_true(helper_set_s_stream(h, op->stream_name, new_stream_id))) | |
ed835673 HL |
1116 | goto out; |
1117 | } | |
1118 | break; | |
1119 | ||
1120 | case OPK_C_ACCEPT_STREAM_NONE: | |
1121 | { | |
1122 | SSL *c_stream; | |
1123 | ||
a350db73 | 1124 | if (!TEST_ptr_null(c_stream = SSL_accept_stream(h->c_conn, 0))) { |
ed835673 HL |
1125 | SSL_free(c_stream); |
1126 | goto out; | |
1127 | } | |
1128 | } | |
1129 | break; | |
1130 | ||
1131 | case OPK_C_FREE_STREAM: | |
1132 | { | |
1133 | if (!TEST_ptr(c_tgt) | |
1134 | || !TEST_true(!SSL_is_connection(c_tgt))) | |
1135 | goto out; | |
1136 | ||
a350db73 | 1137 | if (!TEST_true(helper_local_set_c_stream(&hl, op->stream_name, NULL))) |
ed835673 HL |
1138 | goto out; |
1139 | ||
1140 | SSL_free(c_tgt); | |
1141 | c_tgt = NULL; | |
1142 | } | |
1143 | break; | |
1144 | ||
1145 | case OPK_C_SET_DEFAULT_STREAM_MODE: | |
1146 | { | |
1147 | if (!TEST_ptr(c_tgt)) | |
1148 | goto out; | |
1149 | ||
1150 | if (!TEST_true(SSL_set_default_stream_mode(c_tgt, op->arg1))) | |
1151 | goto out; | |
1152 | } | |
1153 | break; | |
1154 | ||
83df44ae | 1155 | case OPK_C_SET_INCOMING_STREAM_POLICY: |
ed835673 HL |
1156 | { |
1157 | if (!TEST_ptr(c_tgt)) | |
1158 | goto out; | |
1159 | ||
83df44ae HL |
1160 | if (!TEST_true(SSL_set_incoming_stream_policy(c_tgt, |
1161 | op->arg1, 0))) | |
ed835673 HL |
1162 | goto out; |
1163 | } | |
1164 | break; | |
1165 | ||
1166 | case OPK_C_SHUTDOWN: | |
1167 | { | |
1168 | int ret; | |
1169 | ||
1170 | if (!TEST_ptr(c_tgt)) | |
1171 | goto out; | |
1172 | ||
1173 | ret = SSL_shutdown_ex(c_tgt, 0, NULL, 0); | |
1174 | if (!TEST_int_ge(ret, 0)) | |
1175 | goto out; | |
1176 | ||
1177 | } | |
1178 | break; | |
1179 | ||
1180 | case OPK_C_EXPECT_CONN_CLOSE_INFO: | |
1181 | { | |
1182 | SSL_CONN_CLOSE_INFO cc_info = {0}; | |
1183 | int expect_app = (op->arg1 & EXPECT_CONN_CLOSE_APP) != 0; | |
1184 | int expect_remote = (op->arg1 & EXPECT_CONN_CLOSE_REMOTE) != 0; | |
1185 | uint64_t error_code = op->arg2; | |
1186 | ||
1187 | if (!TEST_ptr(c_tgt)) | |
1188 | goto out; | |
1189 | ||
9715e3aa HL |
1190 | if (!SSL_get_conn_close_info(c_tgt, &cc_info, sizeof(cc_info))) |
1191 | SPIN_AGAIN(); | |
ed835673 HL |
1192 | |
1193 | if (!TEST_int_eq(expect_app, !cc_info.is_transport) | |
1194 | || !TEST_int_eq(expect_remote, !cc_info.is_local) | |
1195 | || !TEST_uint64_t_eq(error_code, cc_info.error_code)) | |
1196 | goto out; | |
1197 | } | |
1198 | break; | |
1199 | ||
1200 | case OPK_S_EXPECT_CONN_CLOSE_INFO: | |
1201 | { | |
1202 | const QUIC_TERMINATE_CAUSE *tc; | |
1203 | int expect_app = (op->arg1 & EXPECT_CONN_CLOSE_APP) != 0; | |
1204 | int expect_remote = (op->arg1 & EXPECT_CONN_CLOSE_REMOTE) != 0; | |
1205 | uint64_t error_code = op->arg2; | |
1206 | ||
a350db73 | 1207 | if (!ossl_quic_tserver_is_term_any(h->s)) |
9715e3aa | 1208 | SPIN_AGAIN(); |
ed835673 | 1209 | |
a350db73 | 1210 | if (!TEST_ptr(tc = ossl_quic_tserver_get_terminate_cause(h->s))) |
ed835673 HL |
1211 | goto out; |
1212 | ||
1213 | if (!TEST_uint64_t_eq(error_code, tc->error_code) | |
1214 | || !TEST_int_eq(expect_app, tc->app) | |
1215 | || !TEST_int_eq(expect_remote, tc->remote)) | |
1216 | goto out; | |
1217 | } | |
1218 | break; | |
1219 | ||
1220 | case OPK_S_BIND_STREAM_ID: | |
1221 | { | |
1222 | if (!TEST_uint64_t_eq(s_stream_id, UINT64_MAX)) | |
1223 | goto out; | |
1224 | ||
a350db73 | 1225 | if (!TEST_true(helper_set_s_stream(h, op->stream_name, op->arg2))) |
ed835673 HL |
1226 | goto out; |
1227 | } | |
1228 | break; | |
1229 | ||
fca44cfc HL |
1230 | case OPK_S_UNBIND_STREAM_ID: |
1231 | { | |
1232 | if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX)) | |
1233 | goto out; | |
1234 | ||
1235 | if (!TEST_true(helper_set_s_stream(h, op->stream_name, UINT64_MAX))) | |
1236 | goto out; | |
1237 | } | |
1238 | break; | |
1239 | ||
ed835673 HL |
1240 | case OPK_C_WRITE_FAIL: |
1241 | { | |
571aff4b | 1242 | size_t bytes_written = 0; |
ed835673 HL |
1243 | |
1244 | if (!TEST_ptr(c_tgt)) | |
1245 | goto out; | |
1246 | ||
1247 | if (!TEST_false(SSL_write_ex(c_tgt, "apple", 5, &bytes_written))) | |
1248 | goto out; | |
1249 | } | |
1250 | break; | |
1251 | ||
1252 | case OPK_S_WRITE_FAIL: | |
1253 | { | |
1254 | size_t bytes_written = 0; | |
1255 | ||
1256 | if (!TEST_uint64_t_ne(s_stream_id, UINT64_MAX)) | |
1257 | goto out; | |
1258 | ||
a350db73 | 1259 | if (!TEST_false(ossl_quic_tserver_write(h->s, s_stream_id, |
ed835673 HL |
1260 | (const unsigned char *)"apple", 5, |
1261 | &bytes_written))) | |
1262 | goto out; | |
1263 | } | |
1264 | break; | |
1265 | ||
1266 | case OPK_C_READ_FAIL: | |
1267 | { | |
571aff4b | 1268 | size_t bytes_read = 0; |
ed835673 HL |
1269 | char buf[1]; |
1270 | ||
1271 | if (!TEST_ptr(c_tgt)) | |
1272 | goto out; | |
1273 | ||
1274 | if (!TEST_false(SSL_read_ex(c_tgt, buf, sizeof(buf), &bytes_read))) | |
1275 | goto out; | |
1276 | } | |
1277 | break; | |
1278 | ||
1279 | case OPK_C_STREAM_RESET: | |
1280 | { | |
1281 | SSL_STREAM_RESET_ARGS args = {0}; | |
1282 | ||
1283 | if (!TEST_ptr(c_tgt)) | |
1284 | goto out; | |
1285 | ||
1286 | args.quic_error_code = op->arg2; | |
1287 | ||
1288 | if (!TEST_true(SSL_stream_reset(c_tgt, &args, sizeof(args)))) | |
1289 | goto out; | |
1290 | } | |
1291 | break; | |
1292 | ||
a350db73 HL |
1293 | case OPK_NEW_THREAD: |
1294 | { | |
1295 | #if !defined(OPENSSL_THREADS) | |
629b408c HL |
1296 | /* |
1297 | * If this test script requires threading and we do not have | |
1298 | * support for it, skip the rest of it. | |
1299 | */ | |
1300 | TEST_skip("threading not supported, skipping"); | |
1301 | testresult = 1; | |
a350db73 HL |
1302 | goto out; |
1303 | #else | |
1304 | size_t i; | |
1305 | ||
1306 | if (!TEST_ptr_null(h->threads)) { | |
1307 | TEST_error("max one NEW_THREAD operation per script"); | |
1308 | goto out; | |
1309 | } | |
1310 | ||
1311 | h->threads = OPENSSL_zalloc(op->arg1 * sizeof(struct child_thread_args)); | |
1312 | if (!TEST_ptr(h->threads)) | |
1313 | goto out; | |
1314 | ||
1315 | h->num_threads = op->arg1; | |
1316 | ||
1317 | for (i = 0; i < op->arg1; ++i) { | |
1318 | h->threads[i].h = h; | |
1319 | h->threads[i].script = op->arg0; | |
1320 | h->threads[i].thread_idx = i; | |
1321 | ||
1322 | h->threads[i].m = ossl_crypto_mutex_new(); | |
1323 | if (!TEST_ptr(h->threads[i].m)) | |
1324 | goto out; | |
1325 | ||
1326 | h->threads[i].t | |
1327 | = ossl_crypto_thread_native_start(run_script_child_thread, | |
1328 | &h->threads[i], 1); | |
1329 | if (!TEST_ptr(h->threads[i].t)) | |
1330 | goto out; | |
1331 | } | |
1332 | #endif | |
1333 | } | |
1334 | break; | |
1335 | ||
ed835673 HL |
1336 | default: |
1337 | TEST_error("unknown op"); | |
1338 | goto out; | |
1339 | } | |
1340 | } | |
1341 | ||
1342 | out: | |
fca44cfc HL |
1343 | if (!testresult) { |
1344 | size_t i; | |
1345 | ||
a350db73 HL |
1346 | TEST_error("failed at script op %zu, thread %d\n", |
1347 | op_idx + 1, thread_idx); | |
ed835673 | 1348 | |
fca44cfc HL |
1349 | for (i = 0; i < repeat_stack_len; ++i) |
1350 | TEST_info("while repeating, iteration %zu of %zu, starting at script op %zu", | |
1351 | repeat_stack_done[i], | |
1352 | repeat_stack_limit[i], | |
1353 | repeat_stack_idx[i]); | |
1354 | } | |
1355 | ||
ed835673 | 1356 | OPENSSL_free(tmp_buf); |
a350db73 HL |
1357 | helper_local_cleanup(&hl); |
1358 | return testresult; | |
1359 | } | |
1360 | ||
1361 | static int run_script(const struct script_op *script, int free_order) | |
1362 | { | |
1363 | int testresult = 0; | |
1364 | struct helper h; | |
1365 | ||
1366 | if (!TEST_true(helper_init(&h, free_order))) | |
1367 | goto out; | |
1368 | ||
1369 | if (!TEST_true(run_script_worker(&h, script, -1))) | |
1370 | goto out; | |
1371 | ||
629b408c | 1372 | #if defined(OPENSSL_THREADS) |
a350db73 HL |
1373 | if (!TEST_true(join_threads(h.threads, h.num_threads))) |
1374 | goto out; | |
629b408c | 1375 | #endif |
a350db73 HL |
1376 | |
1377 | testresult = 1; | |
1378 | out: | |
ed835673 HL |
1379 | helper_cleanup(&h); |
1380 | return testresult; | |
1381 | } | |
1382 | ||
a350db73 HL |
1383 | #if defined(OPENSSL_THREADS) |
1384 | static CRYPTO_THREAD_RETVAL run_script_child_thread(void *arg) | |
1385 | { | |
1386 | int testresult; | |
1387 | struct child_thread_args *args = arg; | |
1388 | ||
1389 | testresult = run_script_worker(args->h, args->script, | |
1390 | args->thread_idx); | |
1391 | ||
1392 | ossl_crypto_mutex_lock(args->m); | |
1393 | args->testresult = testresult; | |
1394 | args->done = 1; | |
1395 | ossl_crypto_mutex_unlock(args->m); | |
1396 | return 1; | |
1397 | } | |
1398 | #endif | |
1399 | ||
ed835673 HL |
1400 | /* 1. Simple single-stream test */ |
1401 | static const struct script_op script_1[] = { | |
1402 | OP_C_SET_ALPN ("ossltest") | |
1403 | OP_C_CONNECT_WAIT () | |
1404 | OP_C_WRITE (DEFAULT, "apple", 5) | |
1405 | OP_C_CONCLUDE (DEFAULT) | |
1406 | OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) | |
1407 | OP_S_READ_EXPECT (a, "apple", 5) | |
1408 | OP_S_EXPECT_FIN (a) | |
1409 | OP_S_WRITE (a, "orange", 6) | |
1410 | OP_S_CONCLUDE (a) | |
1411 | OP_C_READ_EXPECT (DEFAULT, "orange", 6) | |
1412 | OP_C_EXPECT_FIN (DEFAULT) | |
1413 | OP_END | |
1414 | }; | |
1415 | ||
1416 | /* 2. Multi-stream test */ | |
1417 | static const struct script_op script_2[] = { | |
1418 | OP_C_SET_ALPN ("ossltest") | |
1419 | OP_C_CONNECT_WAIT () | |
83df44ae | 1420 | OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_ACCEPT) |
ed835673 HL |
1421 | OP_C_WRITE (DEFAULT, "apple", 5) |
1422 | OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) | |
1423 | OP_S_READ_EXPECT (a, "apple", 5) | |
1424 | OP_S_WRITE (a, "orange", 6) | |
1425 | OP_C_READ_EXPECT (DEFAULT, "orange", 6) | |
1426 | ||
1427 | OP_C_NEW_STREAM_BIDI (b, C_BIDI_ID(1)) | |
1428 | OP_C_WRITE (b, "flamingo", 8) | |
1429 | OP_C_CONCLUDE (b) | |
1430 | OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1)) | |
1431 | OP_S_READ_EXPECT (b, "flamingo", 8) | |
1432 | OP_S_EXPECT_FIN (b) | |
1433 | OP_S_WRITE (b, "gargoyle", 8) | |
1434 | OP_S_CONCLUDE (b) | |
1435 | OP_C_READ_EXPECT (b, "gargoyle", 8) | |
1436 | OP_C_EXPECT_FIN (b) | |
1437 | ||
1438 | OP_C_NEW_STREAM_UNI (c, C_UNI_ID(0)) | |
1439 | OP_C_WRITE (c, "elephant", 8) | |
1440 | OP_C_CONCLUDE (c) | |
1441 | OP_S_BIND_STREAM_ID (c, C_UNI_ID(0)) | |
1442 | OP_S_READ_EXPECT (c, "elephant", 8) | |
1443 | OP_S_EXPECT_FIN (c) | |
1444 | OP_S_WRITE_FAIL (c) | |
1445 | ||
1446 | OP_C_ACCEPT_STREAM_NONE () | |
1447 | ||
1448 | OP_S_NEW_STREAM_BIDI (d, S_BIDI_ID(0)) | |
1449 | OP_S_WRITE (d, "frog", 4) | |
1450 | OP_S_CONCLUDE (d) | |
1451 | ||
a350db73 | 1452 | OP_C_ACCEPT_STREAM_WAIT (d) |
ed835673 HL |
1453 | OP_C_ACCEPT_STREAM_NONE () |
1454 | OP_C_READ_EXPECT (d, "frog", 4) | |
1455 | OP_C_EXPECT_FIN (d) | |
1456 | ||
1457 | OP_S_NEW_STREAM_BIDI (e, S_BIDI_ID(1)) | |
1458 | OP_S_WRITE (e, "mixture", 7) | |
1459 | OP_S_CONCLUDE (e) | |
1460 | ||
a350db73 | 1461 | OP_C_ACCEPT_STREAM_WAIT (e) |
ed835673 HL |
1462 | OP_C_READ_EXPECT (e, "mixture", 7) |
1463 | OP_C_EXPECT_FIN (e) | |
1464 | OP_C_WRITE (e, "ramble", 6) | |
1465 | OP_S_READ_EXPECT (e, "ramble", 6) | |
1466 | OP_C_CONCLUDE (e) | |
1467 | OP_S_EXPECT_FIN (e) | |
1468 | ||
1469 | OP_S_NEW_STREAM_UNI (f, S_UNI_ID(0)) | |
1470 | OP_S_WRITE (f, "yonder", 6) | |
1471 | OP_S_CONCLUDE (f) | |
1472 | ||
a350db73 | 1473 | OP_C_ACCEPT_STREAM_WAIT (f) |
ed835673 HL |
1474 | OP_C_ACCEPT_STREAM_NONE () |
1475 | OP_C_READ_EXPECT (f, "yonder", 6) | |
1476 | OP_C_EXPECT_FIN (f) | |
1477 | OP_C_WRITE_FAIL (f) | |
1478 | ||
83df44ae | 1479 | OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_REJECT) |
ed835673 HL |
1480 | OP_S_NEW_STREAM_BIDI (g, S_BIDI_ID(2)) |
1481 | OP_S_WRITE (g, "unseen", 6) | |
1482 | OP_S_CONCLUDE (g) | |
1483 | ||
1484 | OP_C_ACCEPT_STREAM_NONE () | |
1485 | ||
83df44ae | 1486 | OP_C_SET_INCOMING_STREAM_POLICY(SSL_INCOMING_STREAM_POLICY_AUTO) |
ed835673 HL |
1487 | OP_S_NEW_STREAM_BIDI (h, S_BIDI_ID(3)) |
1488 | OP_S_WRITE (h, "UNSEEN", 6) | |
1489 | OP_S_CONCLUDE (h) | |
1490 | ||
1491 | OP_C_ACCEPT_STREAM_NONE () | |
1492 | ||
1493 | /* | |
1494 | * Streams g, h should have been rejected, so server should have got | |
1495 | * STOP_SENDING/RESET_STREAM. | |
1496 | */ | |
1497 | OP_CHECK (check_rejected, S_BIDI_ID(2)) | |
1498 | OP_CHECK (check_rejected, S_BIDI_ID(3)) | |
1499 | ||
1500 | OP_END | |
1501 | }; | |
1502 | ||
1503 | /* 3. Default stream detach/reattach test */ | |
1504 | static const struct script_op script_3[] = { | |
1505 | OP_C_SET_ALPN ("ossltest") | |
1506 | OP_C_CONNECT_WAIT () | |
1507 | ||
1508 | OP_C_WRITE (DEFAULT, "apple", 5) | |
1509 | OP_C_DETACH (a) /* DEFAULT becomes stream 'a' */ | |
1510 | OP_C_WRITE_FAIL (DEFAULT) | |
1511 | ||
1512 | OP_C_WRITE (a, "by", 2) | |
1513 | ||
1514 | OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) | |
1515 | OP_S_READ_EXPECT (a, "appleby", 7) | |
1516 | ||
1517 | OP_S_WRITE (a, "hello", 5) | |
1518 | OP_C_READ_EXPECT (a, "hello", 5) | |
1519 | ||
1520 | OP_C_WRITE_FAIL (DEFAULT) | |
1521 | OP_C_ATTACH (a) | |
1522 | OP_C_WRITE (DEFAULT, "is here", 7) | |
1523 | OP_S_READ_EXPECT (a, "is here", 7) | |
1524 | ||
1525 | OP_C_DETACH (a) | |
1526 | OP_C_CONCLUDE (a) | |
1527 | OP_S_EXPECT_FIN (a) | |
1528 | ||
1529 | OP_END | |
1530 | }; | |
1531 | ||
1532 | /* 4. Default stream mode test */ | |
1533 | static const struct script_op script_4[] = { | |
1534 | OP_C_SET_ALPN ("ossltest") | |
1535 | OP_C_CONNECT_WAIT () | |
1536 | ||
1537 | OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) | |
1538 | OP_C_WRITE_FAIL (DEFAULT) | |
1539 | ||
1540 | OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0)) | |
1541 | OP_S_WRITE (a, "apple", 5) | |
1542 | ||
1543 | OP_C_READ_FAIL (DEFAULT) | |
1544 | ||
a350db73 | 1545 | OP_C_ACCEPT_STREAM_WAIT (a) |
ed835673 HL |
1546 | OP_C_READ_EXPECT (a, "apple", 5) |
1547 | ||
1548 | OP_C_ATTACH (a) | |
1549 | OP_C_WRITE (DEFAULT, "orange", 6) | |
1550 | OP_S_READ_EXPECT (a, "orange", 6) | |
1551 | ||
1552 | OP_END | |
1553 | }; | |
1554 | ||
1555 | /* 5. Test stream reset functionality */ | |
1556 | static const struct script_op script_5[] = { | |
1557 | OP_C_SET_ALPN ("ossltest") | |
1558 | OP_C_CONNECT_WAIT () | |
1559 | ||
1560 | OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) | |
1561 | OP_C_NEW_STREAM_BIDI (a, C_BIDI_ID(0)) | |
1562 | ||
1563 | OP_C_WRITE (a, "apple", 5) | |
1564 | OP_C_STREAM_RESET (a, 42) | |
1565 | ||
1566 | OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) | |
1567 | OP_S_READ_EXPECT (a, "apple", 5) | |
1568 | OP_CHECK (check_stream_reset, C_BIDI_ID(0)) | |
1569 | ||
1570 | OP_END | |
1571 | }; | |
1572 | ||
1573 | /* 6. Test STOP_SENDING functionality */ | |
1574 | static const struct script_op script_6[] = { | |
1575 | OP_C_SET_ALPN ("ossltest") | |
1576 | OP_C_CONNECT_WAIT () | |
1577 | ||
1578 | OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) | |
1579 | OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0)) | |
1580 | OP_S_WRITE (a, "apple", 5) | |
1581 | ||
a350db73 | 1582 | OP_C_ACCEPT_STREAM_WAIT (a) |
ed835673 HL |
1583 | OP_C_FREE_STREAM (a) |
1584 | OP_C_ACCEPT_STREAM_NONE () | |
1585 | ||
1586 | OP_CHECK (check_stream_stopped, S_BIDI_ID(0)) | |
1587 | ||
1588 | OP_END | |
1589 | }; | |
1590 | ||
1591 | /* 7. Unidirectional default stream mode test (client sends first) */ | |
1592 | static const struct script_op script_7[] = { | |
1593 | OP_C_SET_ALPN ("ossltest") | |
1594 | OP_C_CONNECT_WAIT () | |
1595 | ||
1596 | OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI) | |
1597 | OP_C_WRITE (DEFAULT, "apple", 5) | |
1598 | ||
1599 | OP_S_BIND_STREAM_ID (a, C_UNI_ID(0)) | |
1600 | OP_S_READ_EXPECT (a, "apple", 5) | |
1601 | OP_S_WRITE_FAIL (a) | |
1602 | ||
1603 | OP_END | |
1604 | }; | |
1605 | ||
1606 | /* 8. Unidirectional default stream mode test (server sends first) */ | |
1607 | static const struct script_op script_8[] = { | |
1608 | OP_C_SET_ALPN ("ossltest") | |
1609 | OP_C_CONNECT_WAIT () | |
1610 | ||
1611 | OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI) | |
1612 | OP_S_NEW_STREAM_UNI (a, S_UNI_ID(0)) | |
1613 | OP_S_WRITE (a, "apple", 5) | |
1614 | OP_C_READ_EXPECT (DEFAULT, "apple", 5) | |
1615 | OP_C_WRITE_FAIL (DEFAULT) | |
1616 | ||
1617 | OP_END | |
1618 | }; | |
1619 | ||
1620 | /* 9. Unidirectional default stream mode test (server sends first on bidi) */ | |
1621 | static const struct script_op script_9[] = { | |
1622 | OP_C_SET_ALPN ("ossltest") | |
1623 | OP_C_CONNECT_WAIT () | |
1624 | ||
1625 | OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_AUTO_UNI) | |
1626 | OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0)) | |
1627 | OP_S_WRITE (a, "apple", 5) | |
1628 | OP_C_READ_EXPECT (DEFAULT, "apple", 5) | |
1629 | OP_C_WRITE (DEFAULT, "orange", 6) | |
1630 | OP_S_READ_EXPECT (a, "orange", 6) | |
1631 | ||
1632 | OP_END | |
1633 | }; | |
1634 | ||
1635 | /* 10. Shutdown */ | |
1636 | static const struct script_op script_10[] = { | |
1637 | OP_C_SET_ALPN ("ossltest") | |
1638 | OP_C_CONNECT_WAIT () | |
1639 | ||
1640 | OP_C_WRITE (DEFAULT, "apple", 5) | |
1641 | OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) | |
1642 | OP_S_READ_EXPECT (a, "apple", 5) | |
1643 | ||
1644 | OP_C_SHUTDOWN () | |
1645 | OP_C_EXPECT_CONN_CLOSE_INFO(0, 1, 0) | |
1646 | OP_S_EXPECT_CONN_CLOSE_INFO(0, 1, 1) | |
1647 | ||
1648 | OP_END | |
1649 | }; | |
1650 | ||
fca44cfc | 1651 | /* 11. Many threads accepted on the same client connection */ |
274bb489 HL |
1652 | static const struct script_op script_11_child[] = { |
1653 | OP_C_ACCEPT_STREAM_WAIT (a) | |
1654 | OP_C_READ_EXPECT (a, "foo", 3) | |
1655 | OP_C_EXPECT_FIN (a) | |
1656 | ||
1657 | OP_END | |
1658 | }; | |
1659 | ||
1660 | static const struct script_op script_11[] = { | |
1661 | OP_C_SET_ALPN ("ossltest") | |
1662 | OP_C_CONNECT_WAIT () | |
1663 | OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) | |
1664 | ||
fca44cfc HL |
1665 | OP_NEW_THREAD (5, script_11_child) |
1666 | ||
1667 | OP_S_NEW_STREAM_BIDI (a, ANY_ID) | |
274bb489 HL |
1668 | OP_S_WRITE (a, "foo", 3) |
1669 | OP_S_CONCLUDE (a) | |
1670 | ||
fca44cfc | 1671 | OP_S_NEW_STREAM_BIDI (b, ANY_ID) |
274bb489 HL |
1672 | OP_S_WRITE (b, "foo", 3) |
1673 | OP_S_CONCLUDE (b) | |
1674 | ||
fca44cfc | 1675 | OP_S_NEW_STREAM_BIDI (c, ANY_ID) |
274bb489 HL |
1676 | OP_S_WRITE (c, "foo", 3) |
1677 | OP_S_CONCLUDE (c) | |
1678 | ||
fca44cfc | 1679 | OP_S_NEW_STREAM_BIDI (d, ANY_ID) |
274bb489 HL |
1680 | OP_S_WRITE (d, "foo", 3) |
1681 | OP_S_CONCLUDE (d) | |
1682 | ||
fca44cfc | 1683 | OP_S_NEW_STREAM_BIDI (e, ANY_ID) |
274bb489 HL |
1684 | OP_S_WRITE (e, "foo", 3) |
1685 | OP_S_CONCLUDE (e) | |
1686 | ||
274bb489 HL |
1687 | OP_END |
1688 | }; | |
1689 | ||
fca44cfc | 1690 | /* 12. Many threads initiated on the same client connection */ |
274bb489 HL |
1691 | static const struct script_op script_12_child[] = { |
1692 | OP_C_NEW_STREAM_BIDI (a, ANY_ID) | |
1693 | OP_C_WRITE (a, "foo", 3) | |
1694 | OP_C_CONCLUDE (a) | |
1695 | OP_C_FREE_STREAM (a) | |
1696 | ||
1697 | OP_END | |
1698 | }; | |
1699 | ||
1700 | static const struct script_op script_12[] = { | |
1701 | OP_C_SET_ALPN ("ossltest") | |
1702 | OP_C_CONNECT_WAIT () | |
1703 | OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) | |
1704 | ||
1705 | OP_NEW_THREAD (5, script_12_child) | |
1706 | ||
1707 | OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) | |
1708 | OP_S_READ_EXPECT (a, "foo", 3) | |
1709 | OP_S_EXPECT_FIN (a) | |
1710 | OP_S_BIND_STREAM_ID (b, C_BIDI_ID(1)) | |
1711 | OP_S_READ_EXPECT (b, "foo", 3) | |
1712 | OP_S_EXPECT_FIN (b) | |
1713 | OP_S_BIND_STREAM_ID (c, C_BIDI_ID(2)) | |
1714 | OP_S_READ_EXPECT (c, "foo", 3) | |
1715 | OP_S_EXPECT_FIN (c) | |
1716 | OP_S_BIND_STREAM_ID (d, C_BIDI_ID(3)) | |
1717 | OP_S_READ_EXPECT (d, "foo", 3) | |
1718 | OP_S_EXPECT_FIN (d) | |
1719 | OP_S_BIND_STREAM_ID (e, C_BIDI_ID(4)) | |
1720 | OP_S_READ_EXPECT (e, "foo", 3) | |
1721 | OP_S_EXPECT_FIN (e) | |
1722 | ||
1723 | OP_END | |
1724 | }; | |
1725 | ||
fca44cfc HL |
1726 | /* 13. Many threads accepted on the same client connection (stress test) */ |
1727 | static const struct script_op script_13_child[] = { | |
1728 | OP_BEGIN_REPEAT (10) | |
1729 | ||
1730 | OP_C_ACCEPT_STREAM_WAIT (a) | |
1731 | OP_C_READ_EXPECT (a, "foo", 3) | |
1732 | OP_C_EXPECT_FIN (a) | |
1733 | OP_C_FREE_STREAM (a) | |
1734 | ||
1735 | OP_END_REPEAT () | |
1736 | ||
1737 | OP_END | |
1738 | }; | |
1739 | ||
1740 | static const struct script_op script_13[] = { | |
1741 | OP_C_SET_ALPN ("ossltest") | |
1742 | OP_C_CONNECT_WAIT () | |
1743 | OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) | |
1744 | ||
1745 | OP_NEW_THREAD (5, script_13_child) | |
1746 | ||
1747 | OP_BEGIN_REPEAT (50) | |
1748 | ||
1749 | OP_S_NEW_STREAM_BIDI (a, ANY_ID) | |
1750 | OP_S_WRITE (a, "foo", 3) | |
1751 | OP_S_CONCLUDE (a) | |
1752 | OP_S_UNBIND_STREAM_ID (a) | |
1753 | ||
1754 | OP_END_REPEAT () | |
1755 | ||
1756 | OP_END | |
1757 | }; | |
1758 | ||
1759 | /* 14. Many threads initiating on the same client connection (stress test) */ | |
1760 | static const struct script_op script_14_child[] = { | |
1761 | OP_BEGIN_REPEAT (10) | |
1762 | ||
1763 | OP_C_NEW_STREAM_BIDI (a, ANY_ID) | |
1764 | OP_C_WRITE (a, "foo", 3) | |
1765 | OP_C_CONCLUDE (a) | |
1766 | OP_C_FREE_STREAM (a) | |
1767 | ||
1768 | OP_END_REPEAT () | |
1769 | ||
1770 | OP_END | |
1771 | }; | |
1772 | ||
1773 | static const struct script_op script_14[] = { | |
1774 | OP_C_SET_ALPN ("ossltest") | |
1775 | OP_C_CONNECT_WAIT () | |
1776 | OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) | |
1777 | ||
1778 | OP_NEW_THREAD (5, script_14_child) | |
1779 | ||
1780 | OP_BEGIN_REPEAT (50) | |
1781 | ||
1782 | OP_S_ACCEPT_STREAM_WAIT (a) | |
1783 | OP_S_READ_EXPECT (a, "foo", 3) | |
1784 | OP_S_EXPECT_FIN (a) | |
1785 | OP_S_UNBIND_STREAM_ID (a) | |
1786 | ||
1787 | OP_END_REPEAT () | |
1788 | ||
1789 | OP_END | |
1790 | }; | |
1791 | ||
0554f723 HL |
1792 | /* 15. Client sending large number of streams, MAX_STREAMS test */ |
1793 | static const struct script_op script_15[] = { | |
1794 | OP_C_SET_ALPN ("ossltest") | |
1795 | OP_C_CONNECT_WAIT () | |
1796 | OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) | |
1797 | ||
1798 | /* | |
1799 | * This will cause a protocol violation to be raised by the server if we are | |
1800 | * not handling the stream limit correctly on the TX side. | |
1801 | */ | |
1802 | OP_BEGIN_REPEAT (200) | |
1803 | ||
1804 | OP_C_NEW_STREAM_BIDI (a, ANY_ID) | |
1805 | OP_C_WRITE (a, "foo", 3) | |
1806 | OP_C_CONCLUDE (a) | |
1807 | OP_C_FREE_STREAM (a) | |
1808 | ||
1809 | OP_END_REPEAT () | |
1810 | ||
1811 | /* Prove the connection is still good. */ | |
1812 | OP_S_NEW_STREAM_BIDI (a, S_BIDI_ID(0)) | |
1813 | OP_S_WRITE (a, "bar", 3) | |
1814 | OP_S_CONCLUDE (a) | |
1815 | ||
1816 | OP_C_ACCEPT_STREAM_WAIT (a) | |
1817 | OP_C_READ_EXPECT (a, "bar", 3) | |
1818 | OP_C_EXPECT_FIN (a) | |
1819 | ||
1820 | /* | |
1821 | * Drain the queue of incoming streams. We should be able to get all 200 | |
1822 | * even though only 100 can be initiated at a time. | |
1823 | */ | |
1824 | OP_BEGIN_REPEAT (200) | |
1825 | ||
1826 | OP_S_ACCEPT_STREAM_WAIT (b) | |
1827 | OP_S_READ_EXPECT (b, "foo", 3) | |
1828 | OP_S_EXPECT_FIN (b) | |
1829 | OP_S_UNBIND_STREAM_ID (b) | |
1830 | ||
1831 | OP_END_REPEAT () | |
1832 | ||
1833 | OP_END | |
1834 | }; | |
1835 | ||
1836 | /* 16. Server sending large number of streams, MAX_STREAMS test */ | |
1837 | static const struct script_op script_16[] = { | |
1838 | OP_C_SET_ALPN ("ossltest") | |
1839 | OP_C_CONNECT_WAIT () | |
1840 | OP_C_SET_DEFAULT_STREAM_MODE(SSL_DEFAULT_STREAM_MODE_NONE) | |
1841 | ||
1842 | /* | |
1843 | * This will cause a protocol violation to be raised by the client if we are | |
1844 | * not handling the stream limit correctly on the TX side. | |
1845 | */ | |
1846 | OP_BEGIN_REPEAT (200) | |
1847 | ||
1848 | OP_S_NEW_STREAM_BIDI (a, ANY_ID) | |
1849 | OP_S_WRITE (a, "foo", 3) | |
1850 | OP_S_CONCLUDE (a) | |
1851 | OP_S_UNBIND_STREAM_ID (a) | |
1852 | ||
1853 | OP_END_REPEAT () | |
1854 | ||
1855 | /* Prove that the connection is still good. */ | |
1856 | OP_C_NEW_STREAM_BIDI (a, ANY_ID) | |
1857 | OP_C_WRITE (a, "bar", 3) | |
1858 | OP_C_CONCLUDE (a) | |
1859 | ||
1860 | OP_S_ACCEPT_STREAM_WAIT (b) | |
1861 | OP_S_READ_EXPECT (b, "bar", 3) | |
1862 | OP_S_EXPECT_FIN (b) | |
1863 | ||
1864 | /* Drain the queue of incoming streams. */ | |
1865 | OP_BEGIN_REPEAT (200) | |
1866 | ||
1867 | OP_C_ACCEPT_STREAM_WAIT (b) | |
1868 | OP_C_READ_EXPECT (b, "foo", 3) | |
1869 | OP_C_EXPECT_FIN (b) | |
1870 | OP_C_FREE_STREAM (b) | |
1871 | ||
1872 | OP_END_REPEAT () | |
1873 | ||
1874 | OP_END | |
1875 | }; | |
1876 | ||
693b23e3 HL |
1877 | /* 17. Key update test - unlimited */ |
1878 | static const struct script_op script_17[] = { | |
1879 | OP_C_SET_ALPN ("ossltest") | |
1880 | OP_C_CONNECT_WAIT () | |
1881 | ||
1882 | OP_C_WRITE (DEFAULT, "apple", 5) | |
1883 | ||
1884 | OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) | |
1885 | OP_S_READ_EXPECT (a, "apple", 5) | |
1886 | ||
1887 | OP_CHECK (override_key_update, 1) | |
1888 | ||
1889 | OP_BEGIN_REPEAT (200) | |
1890 | ||
1891 | OP_C_WRITE (DEFAULT, "apple", 5) | |
1892 | OP_S_READ_EXPECT (a, "apple", 5) | |
1893 | ||
1894 | /* | |
1895 | * TXKU frequency is bounded by RTT because a previous TXKU needs to be | |
1896 | * acknowledged by the peer first before another one can be begin. By | |
1897 | * waiting this long, we eliminate any such concern and ensure as many key | |
1898 | * updates as possible can occur for the purposes of this test. | |
1899 | */ | |
1900 | OP_CHECK (skip_time_ms, 100) | |
1901 | ||
1902 | OP_END_REPEAT () | |
1903 | ||
1904 | /* At least 5 RXKUs detected */ | |
1905 | OP_CHECK (check_key_update_ge, 5) | |
1906 | ||
1907 | /* | |
1908 | * Prove the connection is still healthy by sending something in both | |
1909 | * directions. | |
1910 | */ | |
1911 | OP_C_WRITE (DEFAULT, "xyzzy", 5) | |
1912 | OP_S_READ_EXPECT (a, "xyzzy", 5) | |
1913 | ||
1914 | OP_S_WRITE (a, "plugh", 5) | |
1915 | OP_C_READ_EXPECT (DEFAULT, "plugh", 5) | |
1916 | ||
1917 | OP_END | |
1918 | }; | |
1919 | ||
1920 | /* 18. Key update test - RTT-bounded */ | |
1921 | static const struct script_op script_18[] = { | |
1922 | OP_C_SET_ALPN ("ossltest") | |
1923 | OP_C_CONNECT_WAIT () | |
1924 | ||
1925 | OP_C_WRITE (DEFAULT, "apple", 5) | |
1926 | ||
1927 | OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) | |
1928 | OP_S_READ_EXPECT (a, "apple", 5) | |
1929 | ||
1930 | OP_CHECK (override_key_update, 1) | |
1931 | ||
1932 | OP_BEGIN_REPEAT (200) | |
1933 | ||
1934 | OP_C_WRITE (DEFAULT, "apple", 5) | |
1935 | OP_S_READ_EXPECT (a, "apple", 5) | |
0e1da9d7 | 1936 | OP_CHECK (skip_time_ms, 4) |
693b23e3 HL |
1937 | |
1938 | OP_END_REPEAT () | |
1939 | ||
1940 | /* | |
1941 | * This time we simulate far less time passing between writes, so there are | |
1942 | * fewer opportunities to initiate TXKUs. Note that we ask for a TXKU every | |
1943 | * 1 packet above, which is absurd; thus this ensures we only actually | |
1944 | * generate TXKUs when we are allowed to. | |
1945 | */ | |
2525109f | 1946 | OP_CHECK (check_key_update_ge, 4) |
0e1da9d7 | 1947 | OP_CHECK (check_key_update_lt, 240) |
693b23e3 HL |
1948 | |
1949 | /* | |
1950 | * Prove the connection is still healthy by sending something in both | |
1951 | * directions. | |
1952 | */ | |
1953 | OP_C_WRITE (DEFAULT, "xyzzy", 5) | |
1954 | OP_S_READ_EXPECT (a, "xyzzy", 5) | |
1955 | ||
1956 | OP_S_WRITE (a, "plugh", 5) | |
1957 | OP_C_READ_EXPECT (DEFAULT, "plugh", 5) | |
1958 | ||
1959 | OP_END | |
1960 | }; | |
1961 | ||
2525109f HL |
1962 | /* 19. Key update test - artificially triggered */ |
1963 | static const struct script_op script_19[] = { | |
1964 | OP_C_SET_ALPN ("ossltest") | |
1965 | OP_C_CONNECT_WAIT () | |
1966 | ||
1967 | OP_C_WRITE (DEFAULT, "apple", 5) | |
1968 | ||
1969 | OP_S_BIND_STREAM_ID (a, C_BIDI_ID(0)) | |
1970 | OP_S_READ_EXPECT (a, "apple", 5) | |
1971 | ||
1972 | OP_CHECK (check_key_update_lt, 1) | |
1973 | OP_CHECK (trigger_key_update, 0) | |
1974 | ||
1975 | OP_C_WRITE (DEFAULT, "orange", 6) | |
1976 | OP_S_READ_EXPECT (a, "orange", 6) | |
9289e59c | 1977 | OP_S_WRITE (a, "ok", 2) |
2525109f | 1978 | |
9289e59c | 1979 | OP_C_READ_EXPECT (DEFAULT, "ok", 2) |
2525109f HL |
1980 | OP_CHECK (check_key_update_ge, 1) |
1981 | ||
1982 | OP_END | |
1983 | }; | |
1984 | ||
ed835673 HL |
1985 | static const struct script_op *const scripts[] = { |
1986 | script_1, | |
1987 | script_2, | |
1988 | script_3, | |
1989 | script_4, | |
1990 | script_5, | |
1991 | script_6, | |
1992 | script_7, | |
1993 | script_8, | |
1994 | script_9, | |
1995 | script_10, | |
274bb489 HL |
1996 | script_11, |
1997 | script_12, | |
fca44cfc HL |
1998 | script_13, |
1999 | script_14, | |
0554f723 HL |
2000 | script_15, |
2001 | script_16, | |
693b23e3 HL |
2002 | script_17, |
2003 | script_18, | |
2525109f | 2004 | script_19, |
ed835673 HL |
2005 | }; |
2006 | ||
2007 | static int test_script(int idx) | |
2008 | { | |
97f30fd5 HL |
2009 | int script_idx = idx >> 1; |
2010 | int free_order = idx & 1; | |
2011 | ||
2012 | TEST_info("Running script %d (order=%d)", script_idx + 1, free_order); | |
2013 | return run_script(scripts[script_idx], free_order); | |
ed835673 HL |
2014 | } |
2015 | ||
2016 | OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n") | |
2017 | ||
2018 | int setup_tests(void) | |
2019 | { | |
2020 | if (!test_skip_common_options()) { | |
2021 | TEST_error("Error parsing test options\n"); | |
2022 | return 0; | |
2023 | } | |
2024 | ||
2025 | if (!TEST_ptr(certfile = test_get_argument(0)) | |
2026 | || !TEST_ptr(keyfile = test_get_argument(1))) | |
2027 | return 0; | |
2028 | ||
97f30fd5 | 2029 | ADD_ALL_TESTS(test_script, OSSL_NELEM(scripts) * 2); |
ed835673 HL |
2030 | return 1; |
2031 | } |