summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortb <>2022-12-08 18:12:39 +0000
committertb <>2022-12-08 18:12:39 +0000
commit5448e8ff0ac7d39e69ed3ae37fa4512166ccff37 (patch)
tree032adcb7c1ee72b516d6dac56937dcf8bd765388 /src
parent8f15943a96fdbea5c6ef211c183f684adcf6b697 (diff)
downloadopenbsd-5448e8ff0ac7d39e69ed3ae37fa4512166ccff37.tar.gz
openbsd-5448e8ff0ac7d39e69ed3ae37fa4512166ccff37.tar.bz2
openbsd-5448e8ff0ac7d39e69ed3ae37fa4512166ccff37.zip
bio chain test: refactor link_chains_at()
Add helper that validate the chains. This deduplicates a lot of code and makes the heart of the test much easier to read.
Diffstat (limited to 'src')
-rw-r--r--src/regress/lib/libcrypto/bio/bio_chain.c307
1 files changed, 109 insertions, 198 deletions
diff --git a/src/regress/lib/libcrypto/bio/bio_chain.c b/src/regress/lib/libcrypto/bio/bio_chain.c
index 45282843f6..1676a5f512 100644
--- a/src/regress/lib/libcrypto/bio/bio_chain.c
+++ b/src/regress/lib/libcrypto/bio/bio_chain.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bio_chain.c,v 1.2 2022/12/08 18:10:52 tb Exp $ */ 1/* $OpenBSD: bio_chain.c,v 1.3 2022/12/08 18:12:39 tb Exp $ */
2/* 2/*
3 * Copyright (c) 2022 Theo Buehler <tb@openbsd.org> 3 * Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
4 * 4 *
@@ -153,6 +153,97 @@ bio_chain_pop_test(void)
153 return failed; 153 return failed;
154} 154}
155 155
156static int
157walk_forward(BIO *start, BIO *end, size_t expected_length,
158 size_t i, size_t j, const char *fn, const char *description)
159{
160 BIO *prev, *next;
161 size_t length;
162 int ret = 0;
163
164 if (start == NULL || end == NULL)
165 goto done;
166
167 next = start;
168 length = 0;
169
170 do {
171 prev = next;
172 next = BIO_next(prev);
173 length++;
174 } while (next != NULL);
175
176 if (prev != end) {
177 fprintf(stderr, "%s case (%zu, %zu) %s has unexpected end\n",
178 fn, i, j, description);
179 goto err;
180 }
181
182 if (length != expected_length) {
183 fprintf(stderr, "%s case (%zu, %zu) %s length "
184 "(walking forward) want: %zu, got %zu\n",
185 fn, i, j, description, expected_length, length);
186 goto err;
187 }
188
189 done:
190 ret = 1;
191
192 err:
193 return ret;
194}
195
196static int
197walk_backward(BIO *start, BIO *end, size_t expected_length,
198 size_t i, size_t j, const char *fn, const char *description)
199{
200 BIO *prev, *next;
201 size_t length;
202 int ret = 0;
203
204 if (start == NULL || end == NULL)
205 goto done;
206
207 length = 0;
208 prev = end;
209 do {
210 next = prev;
211 prev = BIO_prev(prev);
212 length++;
213 } while (prev != NULL);
214
215 if (next != start) {
216 fprintf(stderr, "%s case (%zu, %zu) %s has unexpected start\n",
217 fn, i, j, description);
218 goto err;
219 }
220
221 if (length != expected_length) {
222 fprintf(stderr, "%s case (%zu, %zu) %s length "
223 "(walking backward) want: %zu, got %zu\n",
224 fn, i, j, description, expected_length, length);
225 goto err;
226 }
227
228 done:
229 ret = 1;
230
231 err:
232 return ret;
233}
234
235static int
236check_chain(BIO *start, BIO *end, size_t expected_length,
237 size_t i, size_t j, const char *fn, const char *description)
238{
239 if (!walk_forward(start, end, expected_length, i, j, fn, description))
240 return 0;
241 if (!walk_backward(start, end, expected_length, i, j, fn, description))
242 return 0;
243
244 return 1;
245}
246
156/* 247/*
157 * Link two linear chains of BIOs, A[] and B[], of length N_CHAIN_BIOS together 248 * Link two linear chains of BIOs, A[] and B[], of length N_CHAIN_BIOS together
158 * using either BIO_push(A[i], B[j]) or BIO_set_next(A[i], B[j]). 249 * using either BIO_push(A[i], B[j]) or BIO_set_next(A[i], B[j]).
@@ -198,10 +289,11 @@ bio_chain_pop_test(void)
198static int 289static int
199link_chains_at(size_t i, size_t j, int use_bio_push) 290link_chains_at(size_t i, size_t j, int use_bio_push)
200{ 291{
292 const char *fn = use_bio_push ? "BIO_push" : "BIO_set_next";
201 BIO *A[N_CHAIN_BIOS], *B[N_CHAIN_BIOS]; 293 BIO *A[N_CHAIN_BIOS], *B[N_CHAIN_BIOS];
294 BIO *new_start, *new_end, *prev;
202 BIO *oldhead_start, *oldhead_end, *oldtail_start, *oldtail_end; 295 BIO *oldhead_start, *oldhead_end, *oldtail_start, *oldtail_end;
203 BIO *prev, *next; 296 size_t k, new_length, oldhead_length, oldtail_length;
204 size_t k, length, new_chain_length, oldhead_length, oldtail_length;
205 int failed = 1; 297 int failed = 1;
206 298
207 memset(A, 0, sizeof(A)); 299 memset(A, 0, sizeof(A));
@@ -227,55 +319,32 @@ link_chains_at(size_t i, size_t j, int use_bio_push)
227 * Set our expectations. ... it's complicated. 319 * Set our expectations. ... it's complicated.
228 */ 320 */
229 321
322 new_start = A[0];
323 new_end = B[N_CHAIN_BIOS - 1];
324
230 oldhead_length = j; 325 oldhead_length = j;
231 oldhead_start = B[0]; 326 oldhead_start = B[0];
232 oldhead_end = BIO_prev(B[j]); 327 oldhead_end = BIO_prev(B[j]);
233 328
234 /* 329 /* If we push B[0] or set next to B[0], the oldhead chain is empty. */
235 * Adjust for edge case where we push B[0] or set next to B[0].
236 * The oldhead chain is then empty.
237 */
238
239 if (j == 0) { 330 if (j == 0) {
240 if (oldhead_end != NULL) {
241 fprintf(stderr, "%s: oldhead(B) not empty at start\n",
242 __func__);
243 goto err;
244 }
245
246 oldhead_length = 0; 331 oldhead_length = 0;
247 oldhead_start = NULL; 332 oldhead_start = NULL;
248 } 333 }
249 334
250 if (use_bio_push) { 335 if (use_bio_push) {
251 new_chain_length = N_CHAIN_BIOS + N_CHAIN_BIOS - j; 336 new_length = N_CHAIN_BIOS + N_CHAIN_BIOS - j;
252 337
253 /* oldtail doesn't exist in the BIO_push() case. */ 338 /* oldtail doesn't exist in the BIO_push() case. */
254 oldtail_start = NULL; 339 oldtail_start = NULL;
255 oldtail_end = NULL; 340 oldtail_end = NULL;
256 oldtail_length = 0; 341 oldtail_length = 0;
257 } else { 342 } else {
258 new_chain_length = i + 1 + N_CHAIN_BIOS - j; 343 new_length = i + 1 + N_CHAIN_BIOS - j;
259 344
260 oldtail_start = BIO_next(A[i]); 345 oldtail_start = BIO_next(A[i]);
261 oldtail_end = A[N_CHAIN_BIOS - 1]; 346 oldtail_end = A[N_CHAIN_BIOS - 1];
262 oldtail_length = N_CHAIN_BIOS - i - 1; 347 oldtail_length = N_CHAIN_BIOS - i - 1;
263
264 /*
265 * In case we push onto (or set next at) the end of A[],
266 * the oldtail chain is empty.
267 */
268 if (i == N_CHAIN_BIOS - 1) {
269 if (oldtail_start != NULL) {
270 fprintf(stderr,
271 "%s: oldtail(A) not empty at end\n",
272 __func__);
273 goto err;
274 }
275
276 oldtail_end = NULL;
277 oldtail_length = 0;
278 }
279 } 348 }
280 349
281 /* 350 /*
@@ -284,9 +353,8 @@ link_chains_at(size_t i, size_t j, int use_bio_push)
284 353
285 if (use_bio_push) { 354 if (use_bio_push) {
286 if (BIO_push(A[i], B[j]) != A[i]) { 355 if (BIO_push(A[i], B[j]) != A[i]) {
287 fprintf(stderr, 356 fprintf(stderr, "BIO_push(A[%zu], B[%zu]) != A[%zu]\n",
288 "%s: BIO_push(A[%zu], B[%zu]) != A[%zu]\n", 357 i, j, i);
289 __func__, i, j, i);
290 goto err; 358 goto err;
291 } 359 }
292 } else { 360 } else {
@@ -294,176 +362,19 @@ link_chains_at(size_t i, size_t j, int use_bio_push)
294 } 362 }
295 363
296 /* 364 /*
297 * Walk the new chain starting at A[0] forward. Check that we reach 365 * Check that all the chains match our expectations.
298 * B[N_CHAIN_BIOS - 1] and that the new chain's length is as expected.
299 */ 366 */
300 367
301 next = A[0]; 368 if (!check_chain(new_start, new_end, new_length, i, j, fn, "new chain"))
302 length = 0;
303 do {
304 prev = next;
305 next = BIO_next(prev);
306 length++;
307 } while (next != NULL);
308
309 if (prev != B[N_CHAIN_BIOS - 1]) {
310 fprintf(stderr,
311 "%s(%zu, %zu, %d): new chain doesn't end in end of B\n",
312 __func__, i, j, use_bio_push);
313 goto err;
314 }
315
316 if (length != new_chain_length) {
317 fprintf(stderr, "%s(%zu, %zu, %d) unexpected new chain length"
318 " (walking forward). want %zu, got %zu\n",
319 __func__, i, j, use_bio_push, new_chain_length, length);
320 goto err; 369 goto err;
321 }
322
323 /*
324 * Walk the new chain ending in B[N_CHAIN_BIOS - 1] backward.
325 * Check that we reach A[0] and that its length is what we expect.
326 */
327 370
328 prev = B[N_CHAIN_BIOS - 1]; 371 if (!check_chain(oldhead_start, oldhead_end, oldhead_length, i, j, fn,
329 length = 0; 372 "oldhead"))
330 do {
331 next = prev;
332 prev = BIO_prev(next);
333 length++;
334 } while (prev != NULL);
335
336 if (next != A[0]) {
337 fprintf(stderr,
338 "%s(%zu, %zu, %d): new chain doesn't start at start of A\n",
339 __func__, i, j, use_bio_push);
340 goto err; 373 goto err;
341 }
342 374
343 if (length != new_chain_length) { 375 if (!check_chain(oldtail_start, oldtail_end, oldtail_length, i, j, fn,
344 fprintf(stderr, "%s(%zu, %zu, %d) unexpected new chain length" 376 "oldtail"))
345 " (walking backward). want %zu, got %zu\n",
346 __func__, i, j, use_bio_push, new_chain_length, length);
347 goto err; 377 goto err;
348 }
349
350 if (oldhead_start != NULL) {
351
352 /*
353 * Walk the old head forward and check its length.
354 */
355
356 next = oldhead_start;
357 length = 0;
358 do {
359 prev = next;
360 next = BIO_next(prev);
361 length++;
362 } while (next != NULL);
363
364 if (prev != oldhead_end) {
365 fprintf(stderr,
366 "%s(%zu, %zu, %d): unexpected old head end\n",
367 __func__, i, j, use_bio_push);
368 goto err;
369 }
370
371 if (length != oldhead_length) {
372 fprintf(stderr,
373 "%s(%zu, %zu, %d) unexpected old head length"
374 " (walking forward). want %zu, got %zu\n",
375 __func__, i, j, use_bio_push,
376 oldhead_length, length);
377 goto err;
378 }
379
380 /*
381 * Walk the old head backward and check its length.
382 */
383
384 prev = oldhead_end;
385 length = 0;
386 do {
387 next = prev;
388 prev = BIO_prev(next);
389 length++;
390 } while (prev != NULL);
391
392 if (next != oldhead_start) {
393 fprintf(stderr,
394 "%s(%zu, %zu, %d): unexpected old head start\n",
395 __func__, i, j, use_bio_push);
396 goto err;
397 }
398
399 if (length != oldhead_length) {
400 fprintf(stderr,
401 "%s(%zu, %zu, %d) unexpected old head length"
402 " (walking backward). want %zu, got %zu\n",
403 __func__, i, j, use_bio_push,
404 oldhead_length, length);
405 goto err;
406 }
407 }
408
409 if (oldtail_start != NULL) {
410
411 /*
412 * Walk the old tail forward and check its length.
413 */
414
415 next = oldtail_start;
416 length = 0;
417 do {
418 prev = next;
419 next = BIO_next(prev);
420 length++;
421 } while (next != NULL);
422
423 if (prev != oldtail_end) {
424 fprintf(stderr,
425 "%s(%zu, %zu, %d): unexpected old tail end\n",
426 __func__, i, j, use_bio_push);
427 goto err;
428 }
429
430 if (length != oldtail_length) {
431 fprintf(stderr,
432 "%s(%zu, %zu, %d) unexpected old tail length"
433 " (walking forward). want %zu, got %zu\n",
434 __func__, i, j, use_bio_push,
435 oldtail_length, length);
436 goto err;
437 }
438
439 /*
440 * Walk the old tail backward and check its length.
441 */
442
443 prev = oldtail_end;
444 length = 0;
445 do {
446 next = prev;
447 prev = BIO_prev(next);
448 length++;
449 } while (prev != NULL);
450
451 if (next != oldtail_start) {
452 fprintf(stderr,
453 "%s(%zu, %zu, %d): unexpected old tail start\n",
454 __func__, i, j, use_bio_push);
455 goto err;
456 }
457
458 if (length != oldtail_length) {
459 fprintf(stderr,
460 "%s(%zu, %zu, %d) unexpected old tail length"
461 " (walking backward). want %zu, got %zu\n",
462 __func__, i, j, use_bio_push,
463 oldtail_length, length);
464 goto err;
465 }
466 }
467 378
468 /* 379 /*
469 * All sanity checks passed. We can now free the our chains 380 * All sanity checks passed. We can now free the our chains