summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheo Buehler <tb@cvs.openbsd.org>2022-12-10 10:45:40 +0000
committerTheo Buehler <tb@cvs.openbsd.org>2022-12-10 10:45:40 +0000
commit1c26ef6431ef857256cf470b8f0a5a2897fb7e1f (patch)
treea38c5f323a4777828ab8b573627d53e0bbeaf010
parent1b94088266e2e098304ea1f3614767f4fbc4e72f (diff)
bio chain test: deduplicate chain walking code
-rw-r--r--regress/lib/libcrypto/bio/bio_chain.c92
1 files changed, 40 insertions, 52 deletions
diff --git a/regress/lib/libcrypto/bio/bio_chain.c b/regress/lib/libcrypto/bio/bio_chain.c
index 810395676f7..de6fc3af2a5 100644
--- a/regress/lib/libcrypto/bio/bio_chain.c
+++ b/regress/lib/libcrypto/bio/bio_chain.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bio_chain.c,v 1.12 2022/12/10 10:42:13 tb Exp $ */
+/* $OpenBSD: bio_chain.c,v 1.13 2022/12/10 10:45:39 tb Exp $ */
/*
* Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
*
@@ -192,78 +192,66 @@ bio_chain_pop_test(void)
return failed;
}
-static int
-walk_forward(BIO *start, BIO *end, size_t expected_len, size_t i, size_t j,
- const char *fn, const char *description)
+static void
+walk(BIO *(*step)(BIO *), BIO *start, BIO **end, size_t *len)
{
- BIO *prev, *next;
- size_t len;
- int ret = 0;
-
- prev = NULL;
- next = start;
- len = 0;
+ BIO *current = NULL;
+ BIO *next = start;
+ *len = 0;
while (next != NULL) {
- prev = next;
- next = BIO_next(prev);
- len++;
+ current = next;
+ next = step(current);
+ (*len)++;
}
+ *end = current;
+}
- if (prev != end) {
- fprintf(stderr, "%s case (%zu, %zu) %s has unexpected end\n",
- fn, i, j, description);
- goto err;
+static int
+walk_report(BIO *last, BIO *expected_last, size_t len, size_t expected_len,
+ size_t i, size_t j, const char *fn, const char *description,
+ const char *direction, const char *last_name)
+{
+ if (last != expected_last) {
+ fprintf(stderr, "%s case (%zu, %zu) %s has unexpected %s\n",
+ fn, i, j, description, last_name);
+ return 0;
}
if (len != expected_len) {
fprintf(stderr, "%s case (%zu, %zu) %s length "
- "(walking forward) want: %zu, got %zu\n",
- fn, i, j, description, expected_len, len);
- goto err;
+ "(walking %s) want: %zu, got %zu\n",
+ fn, i, j, description, direction, expected_len, len);
+ return 0;
}
- ret = 1;
-
- err:
- return ret;
+ return 1;
}
static int
-walk_backward(BIO *start, BIO *end, size_t expected_len, size_t i, size_t j,
- const char *fn, const char *description)
+walk_forward(BIO *start, BIO *expected_end, size_t expected_len,
+ size_t i, size_t j, const char *fn, const char *description)
{
- BIO *prev, *next;
+ BIO *end;
size_t len;
- int ret = 0;
-
- next = NULL;
- prev = end;
- len = 0;
- while (prev != NULL) {
- next = prev;
- prev = BIO_prev(prev);
- len++;
- }
+ walk(BIO_next, start, &end, &len);
- if (next != start) {
- fprintf(stderr, "%s case (%zu, %zu) %s has unexpected start\n",
- fn, i, j, description);
- goto err;
- }
+ return walk_report(end, expected_end, len, expected_len,
+ i, j, fn, description, "forward", "end");
+}
- if (len != expected_len) {
- fprintf(stderr, "%s case (%zu, %zu) %s length "
- "(walking backward) want: %zu, got %zu\n",
- fn, i, j, description, expected_len, len);
- goto err;
- }
+static int
+walk_backward(BIO *expected_start, BIO *end, size_t expected_len,
+ size_t i, size_t j, const char *fn, const char *description)
+{
+ BIO *start;
+ size_t len;
- ret = 1;
+ walk(BIO_prev, end, &start, &len);
- err:
- return ret;
+ return walk_report(start, expected_start, len, expected_len,
+ i, j, fn, description, "backward", "start");
}
static int