summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsing <>2017-11-28 16:35:05 +0000
committerjsing <>2017-11-28 16:35:05 +0000
commit4ac2f938dc4f11b0c21ddfca93b2d1dd6e4d86e5 (patch)
tree180f6f6557e24a8051e469fdcddb6cf0594ae64c
parentd092b4b25f0344f2f2a7f8c1f93097e63f193156 (diff)
downloadopenbsd-4ac2f938dc4f11b0c21ddfca93b2d1dd6e4d86e5.tar.gz
openbsd-4ac2f938dc4f11b0c21ddfca93b2d1dd6e4d86e5.tar.bz2
openbsd-4ac2f938dc4f11b0c21ddfca93b2d1dd6e4d86e5.zip
Add regress for CBB_discard_child().
Converted from BoringSSL.
-rw-r--r--src/regress/lib/libssl/bytestring/bytestringtest.c58
1 files changed, 56 insertions, 2 deletions
diff --git a/src/regress/lib/libssl/bytestring/bytestringtest.c b/src/regress/lib/libssl/bytestring/bytestringtest.c
index 5275269902..a260ede2a2 100644
--- a/src/regress/lib/libssl/bytestring/bytestringtest.c
+++ b/src/regress/lib/libssl/bytestring/bytestringtest.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bytestringtest.c,v 1.10 2015/10/25 20:15:06 doug Exp $ */ 1/* $OpenBSD: bytestringtest.c,v 1.11 2017/11/28 16:35:05 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2014, Google Inc. 3 * Copyright (c) 2014, Google Inc.
4 * 4 *
@@ -350,9 +350,9 @@ test_cbb_prefixed(void)
350{ 350{
351 static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3, 351 static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3,
352 4, 5, 6, 5, 4, 1, 0, 1, 2}; 352 4, 5, 6, 5, 4, 1, 0, 1, 2};
353 CBB cbb, contents, inner_contents, inner_inner_contents;
353 uint8_t *buf = NULL; 354 uint8_t *buf = NULL;
354 size_t buf_len; 355 size_t buf_len;
355 CBB cbb, contents, inner_contents, inner_inner_contents;
356 int ret = 0; 356 int ret = 0;
357 357
358 CHECK(CBB_init(&cbb, 0)); 358 CHECK(CBB_init(&cbb, 0));
@@ -383,6 +383,59 @@ err:
383} 383}
384 384
385static int 385static int
386test_cbb_discard_child(void)
387{
388 static const uint8_t kExpected[] = {
389 0xaa,
390 0,
391 1, 0xbb,
392 0, 2, 0xcc, 0xcc,
393 0, 0, 3, 0xdd, 0xdd, 0xdd,
394 1, 0xff,
395 };
396 CBB cbb, contents, inner_contents, inner_inner_contents;
397 uint8_t *buf = NULL;
398 size_t buf_len;
399 int ret = 0;
400
401 CHECK(CBB_init(&cbb, 0));
402 CHECK_GOTO(CBB_add_u8(&cbb, 0xaa));
403
404 // Discarding |cbb|'s children preserves the byte written.
405 CBB_discard_child(&cbb);
406
407 CHECK_GOTO(CBB_add_u8_length_prefixed(&cbb, &contents));
408 CHECK_GOTO(CBB_add_u8_length_prefixed(&cbb, &contents));
409 CHECK_GOTO(CBB_add_u8(&contents, 0xbb));
410 CHECK_GOTO(CBB_add_u16_length_prefixed(&cbb, &contents));
411 CHECK_GOTO(CBB_add_u16(&contents, 0xcccc));
412 CHECK_GOTO(CBB_add_u24_length_prefixed(&cbb, &contents));
413 CHECK_GOTO(CBB_add_u24(&contents, 0xdddddd));
414 CHECK_GOTO(CBB_add_u8_length_prefixed(&cbb, &contents));
415 CHECK_GOTO(CBB_add_u8(&contents, 0xff));
416 CHECK_GOTO(CBB_add_u8_length_prefixed(&contents, &inner_contents));
417 CHECK_GOTO(CBB_add_u8(&inner_contents, 0x42));
418 CHECK_GOTO(CBB_add_u16_length_prefixed(&inner_contents,
419 &inner_inner_contents));
420 CHECK_GOTO(CBB_add_u8(&inner_inner_contents, 0x99));
421
422 // Discard everything from |inner_contents| down.
423 CBB_discard_child(&contents);
424
425 CHECK_GOTO(CBB_finish(&cbb, &buf, &buf_len));
426
427 ret = (buf_len == sizeof(kExpected)
428 && memcmp(buf, kExpected, buf_len) == 0);
429
430 if (0) {
431err:
432 CBB_cleanup(&cbb);
433 }
434 free(buf);
435 return ret;
436}
437
438static int
386test_cbb_misuse(void) 439test_cbb_misuse(void)
387{ 440{
388 CBB cbb, child, contents; 441 CBB cbb, child, contents;
@@ -805,6 +858,7 @@ main(void)
805 failed |= !test_cbb_basic(); 858 failed |= !test_cbb_basic();
806 failed |= !test_cbb_fixed(); 859 failed |= !test_cbb_fixed();
807 failed |= !test_cbb_finish_child(); 860 failed |= !test_cbb_finish_child();
861 failed |= !test_cbb_discard_child();
808 failed |= !test_cbb_misuse(); 862 failed |= !test_cbb_misuse();
809 failed |= !test_cbb_prefixed(); 863 failed |= !test_cbb_prefixed();
810 failed |= !test_cbb_asn1(); 864 failed |= !test_cbb_asn1();