summaryrefslogtreecommitdiff
path: root/regress/lib/libssl
diff options
context:
space:
mode:
authorDoug Hogan <doug@cvs.openbsd.org>2015-06-17 07:15:53 +0000
committerDoug Hogan <doug@cvs.openbsd.org>2015-06-17 07:15:53 +0000
commit3b2e0ab3368d00a8571ed46b0d100ad5e30271c3 (patch)
treebcd3184834c4fe9f8332db13163e64d674854703 /regress/lib/libssl
parentf394691868c0818998edcf198a6828d70b1a72fa (diff)
Add tests for CBS_offset() and CBS_write_bytes().
"no problem" miod@, tweak + ok jsing@
Diffstat (limited to 'regress/lib/libssl')
-rw-r--r--regress/lib/libssl/bytestring/bytestringtest.c72
1 files changed, 70 insertions, 2 deletions
diff --git a/regress/lib/libssl/bytestring/bytestringtest.c b/regress/lib/libssl/bytestring/bytestringtest.c
index 05ca27e8b51..65b638132a5 100644
--- a/regress/lib/libssl/bytestring/bytestringtest.c
+++ b/regress/lib/libssl/bytestring/bytestringtest.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bytestringtest.c,v 1.5 2015/06/16 06:37:58 doug Exp $ */
+/* $OpenBSD: bytestringtest.c,v 1.6 2015/06/17 07:15:52 doug Exp $ */
/*
* Copyright (c) 2014, Google Inc.
*
@@ -671,6 +671,72 @@ test_asn1_uint64(void)
return 1;
}
+static int
+test_offset(void)
+{
+ uint8_t v;
+ static const uint8_t input[] = {1, 2, 3, 4, 5};
+ CBS data;
+
+ CBS_init(&data, input, sizeof(input));
+ if (sizeof(input) != 5)
+ return 0;
+
+ if (!(CBS_len(&data) == 5 && CBS_offset(&data) == 0 &&
+ CBS_get_u8(&data, &v) && v == 1 &&
+ CBS_len(&data) == 4 && CBS_offset(&data) == 1 &&
+ CBS_skip(&data, 2) &&
+ CBS_len(&data) == 2 && CBS_offset(&data) == 3 &&
+ CBS_get_u8(&data, &v) && v == 4 &&
+ CBS_get_u8(&data, &v) && v == 5 &&
+ CBS_len(&data) == 0 && CBS_offset(&data) == 5 &&
+ !CBS_skip(&data, 1)))
+ return 0;
+
+ CBS_init(&data, input, sizeof(input));
+ if (!(CBS_skip(&data, 2) &&
+ CBS_len(&data) == 3 && CBS_offset(&data) == 2 &&
+ CBS_skip(&data, 3) &&
+ CBS_len(&data) == 0 && CBS_offset(&data) == 5 &&
+ !CBS_get_u8(&data, &v)))
+ return 0;
+
+ return 1;
+}
+
+static int
+test_write_bytes(void)
+{
+ int ret = 0;
+ uint8_t v;
+ size_t len;
+ static const uint8_t input[] = {'f', 'o', 'o', 'b', 'a', 'r'};
+ CBS data;
+ char *tmp = NULL;
+
+ if ((tmp = malloc(sizeof(input))) == NULL) {
+ fprintf(stderr, "failed to malloc\n");
+ goto err;
+ }
+ memset(tmp, 100, sizeof(input));
+
+ CBS_init(&data, input, sizeof(input));
+ if (!(CBS_len(&data) == 6 && CBS_offset(&data) == 0 &&
+ CBS_get_u8(&data, &v) && v == 102 /* f */ &&
+ CBS_skip(&data, 1) &&
+ !CBS_skip(&data, 15) &&
+ CBS_write_bytes(&data, tmp, sizeof(input), &len) &&
+ len == 4 && memcmp(input + 2, tmp, len) == 0 &&
+ tmp[4] == 100 && tmp[5] == 100))
+ goto err;
+
+ ret = 1;
+
+err:
+ free(tmp);
+ return ret;
+}
+
int
main(void)
{
@@ -687,7 +753,9 @@ main(void)
!test_cbb_asn1() ||
!test_indefinite_convert() ||
!test_asn1_uint64() ||
- !test_get_optional_asn1_bool())
+ !test_get_optional_asn1_bool() ||
+ !test_offset() ||
+ !test_write_bytes())
return 1;
printf("PASS\n");