Add regress for CBB_discard_child().
authorjsing <jsing@openbsd.org>
Tue, 28 Nov 2017 16:35:05 +0000 (16:35 +0000)
committerjsing <jsing@openbsd.org>
Tue, 28 Nov 2017 16:35:05 +0000 (16:35 +0000)
Converted from BoringSSL.

regress/lib/libssl/bytestring/bytestringtest.c

index 5275269..a260ede 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: bytestringtest.c,v 1.10 2015/10/25 20:15:06 doug Exp $        */
+/*     $OpenBSD: bytestringtest.c,v 1.11 2017/11/28 16:35:05 jsing Exp $       */
 /*
  * Copyright (c) 2014, Google Inc.
  *
@@ -350,9 +350,9 @@ test_cbb_prefixed(void)
 {
        static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3,
            4, 5, 6, 5, 4, 1, 0, 1, 2};
+       CBB cbb, contents, inner_contents, inner_inner_contents;
        uint8_t *buf = NULL;
        size_t buf_len;
-       CBB cbb, contents, inner_contents, inner_inner_contents;
        int ret = 0;
 
        CHECK(CBB_init(&cbb, 0));
@@ -382,6 +382,59 @@ err:
        return ret;
 }
 
+static int
+test_cbb_discard_child(void)
+{
+       static const uint8_t kExpected[] = {
+               0xaa,
+               0,
+               1, 0xbb,
+               0, 2, 0xcc, 0xcc,
+               0, 0, 3, 0xdd, 0xdd, 0xdd,
+               1, 0xff,
+       };
+       CBB cbb, contents, inner_contents, inner_inner_contents;
+       uint8_t *buf = NULL;
+       size_t buf_len;
+       int ret = 0;
+
+       CHECK(CBB_init(&cbb, 0));
+       CHECK_GOTO(CBB_add_u8(&cbb, 0xaa));
+
+       // Discarding |cbb|'s children preserves the byte written.
+       CBB_discard_child(&cbb);
+
+       CHECK_GOTO(CBB_add_u8_length_prefixed(&cbb, &contents));
+       CHECK_GOTO(CBB_add_u8_length_prefixed(&cbb, &contents));
+       CHECK_GOTO(CBB_add_u8(&contents, 0xbb));
+       CHECK_GOTO(CBB_add_u16_length_prefixed(&cbb, &contents));
+       CHECK_GOTO(CBB_add_u16(&contents, 0xcccc));
+       CHECK_GOTO(CBB_add_u24_length_prefixed(&cbb, &contents));
+       CHECK_GOTO(CBB_add_u24(&contents, 0xdddddd));
+       CHECK_GOTO(CBB_add_u8_length_prefixed(&cbb, &contents));
+       CHECK_GOTO(CBB_add_u8(&contents, 0xff));
+       CHECK_GOTO(CBB_add_u8_length_prefixed(&contents, &inner_contents));
+       CHECK_GOTO(CBB_add_u8(&inner_contents, 0x42));
+       CHECK_GOTO(CBB_add_u16_length_prefixed(&inner_contents,
+           &inner_inner_contents));
+       CHECK_GOTO(CBB_add_u8(&inner_inner_contents, 0x99));
+
+       // Discard everything from |inner_contents| down.
+       CBB_discard_child(&contents);
+
+       CHECK_GOTO(CBB_finish(&cbb, &buf, &buf_len));
+
+       ret = (buf_len == sizeof(kExpected)
+           && memcmp(buf, kExpected, buf_len) == 0);
+
+       if (0) {
+err:
+               CBB_cleanup(&cbb);
+       }
+       free(buf);
+       return ret;
+}
+
 static int
 test_cbb_misuse(void)
 {
@@ -805,6 +858,7 @@ main(void)
        failed |= !test_cbb_basic();
        failed |= !test_cbb_fixed();
        failed |= !test_cbb_finish_child();
+       failed |= !test_cbb_discard_child();
        failed |= !test_cbb_misuse();
        failed |= !test_cbb_prefixed();
        failed |= !test_cbb_asn1();