Skip to content

Commit b1b1dfd

Browse files
author
tb
committed
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.
1 parent 41b8e3d commit b1b1dfd

File tree

1 file changed

+109
-198
lines changed

1 file changed

+109
-198
lines changed

src/regress/lib/libcrypto/bio/bio_chain.c

Lines changed: 109 additions & 198 deletions
Original file line numberDiff line numberDiff line change
@@ -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 $ */
22
/*
33
* Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
44
*
@@ -153,6 +153,97 @@ bio_chain_pop_test(void)
153153
return failed;
154154
}
155155

156+
static int
157+
walk_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+
196+
static int
197+
walk_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+
235+
static int
236+
check_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+
156247
/*
157248
* Link two linear chains of BIOs, A[] and B[], of length N_CHAIN_BIOS together
158249
* 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)
198289
static int
199290
link_chains_at(size_t i, size_t j, int use_bio_push)
200291
{
292+
const char *fn = use_bio_push ? "BIO_push" : "BIO_set_next";
201293
BIO *A[N_CHAIN_BIOS], *B[N_CHAIN_BIOS];
294+
BIO *new_start, *new_end, *prev;
202295
BIO *oldhead_start, *oldhead_end, *oldtail_start, *oldtail_end;
203-
BIO *prev, *next;
204-
size_t k, length, new_chain_length, oldhead_length, oldtail_length;
296+
size_t k, new_length, oldhead_length, oldtail_length;
205297
int failed = 1;
206298

207299
memset(A, 0, sizeof(A));
@@ -227,55 +319,32 @@ link_chains_at(size_t i, size_t j, int use_bio_push)
227319
* Set our expectations. ... it's complicated.
228320
*/
229321

322+
new_start = A[0];
323+
new_end = B[N_CHAIN_BIOS - 1];
324+
230325
oldhead_length = j;
231326
oldhead_start = B[0];
232327
oldhead_end = BIO_prev(B[j]);
233328

234-
/*
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-
329+
/* If we push B[0] or set next to B[0], the oldhead chain is empty. */
239330
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-
246331
oldhead_length = 0;
247332
oldhead_start = NULL;
248333
}
249334

250335
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;
252337

253338
/* oldtail doesn't exist in the BIO_push() case. */
254339
oldtail_start = NULL;
255340
oldtail_end = NULL;
256341
oldtail_length = 0;
257342
} else {
258-
new_chain_length = i + 1 + N_CHAIN_BIOS - j;
343+
new_length = i + 1 + N_CHAIN_BIOS - j;
259344

260345
oldtail_start = BIO_next(A[i]);
261346
oldtail_end = A[N_CHAIN_BIOS - 1];
262347
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-
}
279348
}
280349

281350
/*
@@ -284,186 +353,28 @@ link_chains_at(size_t i, size_t j, int use_bio_push)
284353

285354
if (use_bio_push) {
286355
if (BIO_push(A[i], B[j]) != A[i]) {
287-
fprintf(stderr,
288-
"%s: BIO_push(A[%zu], B[%zu]) != A[%zu]\n",
289-
__func__, i, j, i);
356+
fprintf(stderr, "BIO_push(A[%zu], B[%zu]) != A[%zu]\n",
357+
i, j, i);
290358
goto err;
291359
}
292360
} else {
293361
BIO_set_next(A[i], B[j]);
294362
}
295363

296364
/*
297-
* Walk the new chain starting at A[0] forward. Check that we reach
298-
* B[N_CHAIN_BIOS - 1] and that the new chain's length is as expected.
365+
* Check that all the chains match our expectations.
299366
*/
300367

301-
next = A[0];
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);
368+
if (!check_chain(new_start, new_end, new_length, i, j, fn, "new chain"))
320369
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-
*/
327370

328-
prev = B[N_CHAIN_BIOS - 1];
329-
length = 0;
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);
371+
if (!check_chain(oldhead_start, oldhead_end, oldhead_length, i, j, fn,
372+
"oldhead"))
340373
goto err;
341-
}
342374

343-
if (length != new_chain_length) {
344-
fprintf(stderr, "%s(%zu, %zu, %d) unexpected new chain length"
345-
" (walking backward). want %zu, got %zu\n",
346-
__func__, i, j, use_bio_push, new_chain_length, length);
375+
if (!check_chain(oldtail_start, oldtail_end, oldtail_length, i, j, fn,
376+
"oldtail"))
347377
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-
}
467378

468379
/*
469380
* All sanity checks passed. We can now free the our chains

0 commit comments

Comments
 (0)