diff options
author | tb <> | 2022-12-10 10:45:39 +0000 |
---|---|---|
committer | tb <> | 2022-12-10 10:45:39 +0000 |
commit | 6123df412acd626ae176b2b3632215d8a32c598f (patch) | |
tree | 1167535c6493d2bcbe9f27d304c398b508e5f7cc | |
parent | 38a508be23cfe9bb502552eab3091e8c037cf037 (diff) | |
download | openbsd-6123df412acd626ae176b2b3632215d8a32c598f.tar.gz openbsd-6123df412acd626ae176b2b3632215d8a32c598f.tar.bz2 openbsd-6123df412acd626ae176b2b3632215d8a32c598f.zip |
bio chain test: deduplicate chain walking code
-rw-r--r-- | src/regress/lib/libcrypto/bio/bio_chain.c | 92 |
1 files changed, 40 insertions, 52 deletions
diff --git a/src/regress/lib/libcrypto/bio/bio_chain.c b/src/regress/lib/libcrypto/bio/bio_chain.c index 810395676f..de6fc3af2a 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.12 2022/12/10 10:42:13 tb Exp $ */ | 1 | /* $OpenBSD: bio_chain.c,v 1.13 2022/12/10 10:45: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 | * |
@@ -192,78 +192,66 @@ bio_chain_pop_test(void) | |||
192 | return failed; | 192 | return failed; |
193 | } | 193 | } |
194 | 194 | ||
195 | static int | 195 | static void |
196 | walk_forward(BIO *start, BIO *end, size_t expected_len, size_t i, size_t j, | 196 | walk(BIO *(*step)(BIO *), BIO *start, BIO **end, size_t *len) |
197 | const char *fn, const char *description) | ||
198 | { | 197 | { |
199 | BIO *prev, *next; | 198 | BIO *current = NULL; |
200 | size_t len; | 199 | BIO *next = start; |
201 | int ret = 0; | ||
202 | |||
203 | prev = NULL; | ||
204 | next = start; | ||
205 | len = 0; | ||
206 | 200 | ||
201 | *len = 0; | ||
207 | while (next != NULL) { | 202 | while (next != NULL) { |
208 | prev = next; | 203 | current = next; |
209 | next = BIO_next(prev); | 204 | next = step(current); |
210 | len++; | 205 | (*len)++; |
211 | } | 206 | } |
207 | *end = current; | ||
208 | } | ||
212 | 209 | ||
213 | if (prev != end) { | 210 | static int |
214 | fprintf(stderr, "%s case (%zu, %zu) %s has unexpected end\n", | 211 | walk_report(BIO *last, BIO *expected_last, size_t len, size_t expected_len, |
215 | fn, i, j, description); | 212 | size_t i, size_t j, const char *fn, const char *description, |
216 | goto err; | 213 | const char *direction, const char *last_name) |
214 | { | ||
215 | if (last != expected_last) { | ||
216 | fprintf(stderr, "%s case (%zu, %zu) %s has unexpected %s\n", | ||
217 | fn, i, j, description, last_name); | ||
218 | return 0; | ||
217 | } | 219 | } |
218 | 220 | ||
219 | if (len != expected_len) { | 221 | if (len != expected_len) { |
220 | fprintf(stderr, "%s case (%zu, %zu) %s length " | 222 | fprintf(stderr, "%s case (%zu, %zu) %s length " |
221 | "(walking forward) want: %zu, got %zu\n", | 223 | "(walking %s) want: %zu, got %zu\n", |
222 | fn, i, j, description, expected_len, len); | 224 | fn, i, j, description, direction, expected_len, len); |
223 | goto err; | 225 | return 0; |
224 | } | 226 | } |
225 | 227 | ||
226 | ret = 1; | 228 | return 1; |
227 | |||
228 | err: | ||
229 | return ret; | ||
230 | } | 229 | } |
231 | 230 | ||
232 | static int | 231 | static int |
233 | walk_backward(BIO *start, BIO *end, size_t expected_len, size_t i, size_t j, | 232 | walk_forward(BIO *start, BIO *expected_end, size_t expected_len, |
234 | const char *fn, const char *description) | 233 | size_t i, size_t j, const char *fn, const char *description) |
235 | { | 234 | { |
236 | BIO *prev, *next; | 235 | BIO *end; |
237 | size_t len; | 236 | size_t len; |
238 | int ret = 0; | ||
239 | |||
240 | next = NULL; | ||
241 | prev = end; | ||
242 | len = 0; | ||
243 | 237 | ||
244 | while (prev != NULL) { | 238 | walk(BIO_next, start, &end, &len); |
245 | next = prev; | ||
246 | prev = BIO_prev(prev); | ||
247 | len++; | ||
248 | } | ||
249 | 239 | ||
250 | if (next != start) { | 240 | return walk_report(end, expected_end, len, expected_len, |
251 | fprintf(stderr, "%s case (%zu, %zu) %s has unexpected start\n", | 241 | i, j, fn, description, "forward", "end"); |
252 | fn, i, j, description); | 242 | } |
253 | goto err; | ||
254 | } | ||
255 | 243 | ||
256 | if (len != expected_len) { | 244 | static int |
257 | fprintf(stderr, "%s case (%zu, %zu) %s length " | 245 | walk_backward(BIO *expected_start, BIO *end, size_t expected_len, |
258 | "(walking backward) want: %zu, got %zu\n", | 246 | size_t i, size_t j, const char *fn, const char *description) |
259 | fn, i, j, description, expected_len, len); | 247 | { |
260 | goto err; | 248 | BIO *start; |
261 | } | 249 | size_t len; |
262 | 250 | ||
263 | ret = 1; | 251 | walk(BIO_prev, end, &start, &len); |
264 | 252 | ||
265 | err: | 253 | return walk_report(start, expected_start, len, expected_len, |
266 | return ret; | 254 | i, j, fn, description, "backward", "start"); |
267 | } | 255 | } |
268 | 256 | ||
269 | static int | 257 | static int |