xref: /freebsd/contrib/libcbor/test/bytestring_test.c (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
1 /*
2  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
3  *
4  * libcbor is free software; you can redistribute it and/or modify
5  * it under the terms of the MIT license. See LICENSE for details.
6  */
7 
8 #include "assertions.h"
9 #include "cbor.h"
10 #include "test_allocator.h"
11 
12 cbor_item_t *bs;
13 struct cbor_load_result res;
14 
15 unsigned char data1[] = {0x40, 0xFF};
16 unsigned char data2[] = {0x41, 0xA1};
17 unsigned char data3[] = {0x58, 0x01, 0xA1, 0xFF};
18 unsigned char data4[] = {
19     0x58, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
20     0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
21     0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21,
22     0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D,
23     0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
24     0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45,
25     0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51,
26     0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D,
27     0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
28     0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
29     0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81,
30     0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D,
31     0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99,
32     0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5,
33     0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1,
34     0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD,
35     0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9,
36     0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5,
37     0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, 0xE1,
38     0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED,
39     0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9,
40     0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF};
41 unsigned char data5[] = {
42     0x59, 0x00, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
43     0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14,
44     0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
45     0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C,
46     0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
47     0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44,
48     0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
49     0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C,
50     0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
51     0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74,
52     0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80,
53     0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C,
54     0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
55     0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4,
56     0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0,
57     0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC,
58     0xBD, 0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8,
59     0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4,
60     0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0,
61     0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC,
62     0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8,
63     0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF};
64 unsigned char data6[] = {
65     0x5A, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
66     0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
67     0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,
68     0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A,
69     0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
70     0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x40, 0x41, 0x42,
71     0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E,
72     0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A,
73     0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
74     0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72,
75     0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E,
76     0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A,
77     0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
78     0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, 0xA0, 0xA1, 0xA2,
79     0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE,
80     0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA,
81     0xBB, 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6,
82     0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xD0, 0xD1, 0xD2,
83     0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE,
84     0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA,
85     0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6,
86     0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF};
87 unsigned char data7[] = {
88     0x5B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x01, 0x02,
89     0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
90     0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A,
91     0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26,
92     0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32,
93     0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E,
94     0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A,
95     0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56,
96     0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61, 0x62,
97     0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
98     0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A,
99     0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86,
100     0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92,
101     0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E,
102     0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA,
103     0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
104     0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC1, 0xC2,
105     0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE,
106     0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA,
107     0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6,
108     0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2,
109     0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE,
110     0xFF};
111 unsigned char data8[] = {
112     0x5C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0xFF, 0x00, 0x01,
113     0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D,
114     0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
115     0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25,
116     0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31,
117     0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D,
118     0x3E, 0x3F, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
119     0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55,
120     0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F, 0x60, 0x61,
121     0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D,
122     0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
123     0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85,
124     0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91,
125     0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D,
126     0x9E, 0x9F, 0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9,
127     0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5,
128     0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF, 0xC0, 0xC1,
129     0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD,
130     0xCE, 0xCF, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9,
131     0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5,
132     0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF, 0xF0, 0xF1,
133     0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD,
134     0xFE, 0xFF};
135 
136 static void test_empty_bs(void **_CBOR_UNUSED(_state)) {
137   bs = cbor_load(data1, 2, &res);
138   assert_non_null(bs);
139   assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
140   assert_true(cbor_isa_bytestring(bs));
141   assert_size_equal(cbor_bytestring_length(bs), 0);
142   assert_true(res.read == 1);
143   cbor_decref(&bs);
144   assert_null(bs);
145 }
146 
147 static void test_embedded_bs(void **_CBOR_UNUSED(_state)) {
148   bs = cbor_load(data2, 2, &res);
149   assert_non_null(bs);
150   assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
151   assert_true(cbor_isa_bytestring(bs));
152   assert_true(cbor_bytestring_length(bs) == 1);
153   assert_true(res.read == 2);
154   assert_true(*cbor_bytestring_handle(bs) == 0xA1);
155   assert_memory_equal(cbor_bytestring_handle(bs), data2 + 1, 1);
156   cbor_decref(&bs);
157   assert_null(bs);
158 }
159 
160 static void test_notenough_data(void **_CBOR_UNUSED(_state)) {
161   bs = cbor_load(data3, 2, &res);
162   assert_null(bs);
163   assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
164 }
165 
166 static void test_short_bs1(void **_CBOR_UNUSED(_state)) {
167   bs = cbor_load(data3, 4, &res);
168   assert_non_null(bs);
169   assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
170   assert_true(cbor_isa_bytestring(bs));
171   assert_true(cbor_bytestring_length(bs) == 1);
172   assert_true(res.read == 3);
173   assert_true(*cbor_bytestring_handle(bs) == 0xA1);
174   assert_memory_equal(cbor_bytestring_handle(bs), data3 + 2, 1);
175   cbor_decref(&bs);
176   assert_null(bs);
177 }
178 
179 static void test_short_bs2(void **_CBOR_UNUSED(_state)) {
180   bs = cbor_load(data4, 259, &res);
181   assert_non_null(bs);
182   assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
183   assert_true(cbor_isa_bytestring(bs));
184   assert_true(cbor_bytestring_length(bs) == 255);
185   assert_true(res.read == 257);
186   assert_memory_equal(cbor_bytestring_handle(bs), data4 + 2, 0xFF);
187   cbor_decref(&bs);
188   assert_null(bs);
189 }
190 
191 static void test_half_bs(void **_CBOR_UNUSED(_state)) {
192   bs = cbor_load(data5, 259, &res);
193   assert_non_null(bs);
194   assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
195   assert_true(cbor_isa_bytestring(bs));
196   assert_true(cbor_bytestring_length(bs) == 255);
197   assert_true(res.read == 258);
198   assert_memory_equal(cbor_bytestring_handle(bs), data5 + 3, 0xFF);
199   cbor_decref(&bs);
200   assert_null(bs);
201 }
202 
203 static void test_int_bs(void **_CBOR_UNUSED(_state)) {
204   bs = cbor_load(data6, 261, &res);
205   assert_non_null(bs);
206   assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
207   assert_true(cbor_isa_bytestring(bs));
208   assert_true(cbor_bytestring_length(bs) == 255);
209   assert_true(res.read == 260);
210   assert_memory_equal(cbor_bytestring_handle(bs), data6 + 5, 0xFF);
211   cbor_decref(&bs);
212   assert_null(bs);
213 }
214 
215 static void test_long_bs(void **_CBOR_UNUSED(_state)) {
216   bs = cbor_load(data7, 265, &res);
217   assert_non_null(bs);
218   assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
219   assert_true(cbor_isa_bytestring(bs));
220   assert_true(cbor_bytestring_length(bs) == 255);
221   assert_true(res.read == 264);
222   assert_memory_equal(cbor_bytestring_handle(bs), data7 + 9, 0xFF);
223   cbor_decref(&bs);
224   assert_null(bs);
225 }
226 
227 unsigned char data9[] = {0x5F, 0xFF};
228 
229 static void test_zero_indef(void **_CBOR_UNUSED(_state)) {
230   bs = cbor_load(data9, 2, &res);
231   assert_non_null(bs);
232   assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
233   assert_true(cbor_isa_bytestring(bs));
234   assert_true(cbor_bytestring_is_indefinite(bs));
235   assert_true(cbor_bytestring_chunk_count(bs) == 0);
236   assert_true(res.read == 2);
237   cbor_decref(&bs);
238   assert_null(bs);
239 }
240 
241 unsigned char data10[] = {0x5F, 0x58, 0x01, 0xA1, 0xFF, 0xFF};
242 
243 /*                          start |   bstring     | break| extra */
244 
245 static void test_short_indef(void **_CBOR_UNUSED(_state)) {
246   bs = cbor_load(data10, 6, &res);
247   assert_non_null(bs);
248   assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
249   assert_true(cbor_isa_bytestring(bs));
250   assert_true(cbor_bytestring_length(bs) == 0);
251   assert_true(cbor_bytestring_is_indefinite(bs));
252   assert_true(cbor_bytestring_chunk_count(bs) == 1);
253   assert_true(res.read == 5);
254   assert_true(cbor_isa_bytestring(cbor_bytestring_chunks_handle(bs)[0]));
255   assert_true(cbor_bytestring_length(cbor_bytestring_chunks_handle(bs)[0]) ==
256               1);
257   assert_true(*cbor_bytestring_handle(cbor_bytestring_chunks_handle(bs)[0]) ==
258               0xA1);
259   cbor_decref(&bs);
260   assert_null(bs);
261 }
262 
263 unsigned char data11[] = {0x5F, 0x58, 0x01, 0xA1, 0x58, 0x01, 0xA2, 0xFF, 0xFF};
264 
265 /*                          start |   bstring     |    bstring     | break|
266  * extra */
267 
268 static void test_two_indef(void **_CBOR_UNUSED(_state)) {
269   bs = cbor_load(data11, 9, &res);
270   assert_non_null(bs);
271   assert_size_equal(1, cbor_refcount(bs));
272   assert_true(cbor_typeof(bs) == CBOR_TYPE_BYTESTRING);
273   assert_true(cbor_isa_bytestring(bs));
274   assert_true(cbor_bytestring_length(bs) == 0);
275   assert_true(cbor_bytestring_is_indefinite(bs));
276   assert_true(cbor_bytestring_chunk_count(bs) == 2);
277   assert_true(res.read == 8);
278   assert_true(cbor_isa_bytestring(cbor_bytestring_chunks_handle(bs)[0]));
279   assert_true(cbor_bytestring_length(cbor_bytestring_chunks_handle(bs)[0]) ==
280               1);
281   assert_true(*cbor_bytestring_handle(cbor_bytestring_chunks_handle(bs)[0]) ==
282               0xA1);
283   assert_true(cbor_isa_bytestring(cbor_bytestring_chunks_handle(bs)[1]));
284   assert_true(cbor_bytestring_length(cbor_bytestring_chunks_handle(bs)[1]) ==
285               1);
286   assert_true(*cbor_bytestring_handle(cbor_bytestring_chunks_handle(bs)[1]) ==
287               0xA2);
288   cbor_decref(&bs);
289   assert_null(bs);
290 }
291 
292 unsigned char data12[] = {0x5F, 0x58, 0x01};
293 
294 /*                          start |   bstring - too short */
295 
296 static void test_missing_indef(void **_CBOR_UNUSED(_state)) {
297   bs = cbor_load(data12, 3, &res);
298   assert_true(res.error.code == CBOR_ERR_NOTENOUGHDATA);
299   assert_null(bs);
300 }
301 
302 static void test_inline_creation(void **_CBOR_UNUSED(_state)) {
303   bs = cbor_build_bytestring((cbor_data) "Hello!", 6);
304   assert_memory_equal(cbor_bytestring_handle(bs), "Hello!", 6);
305   cbor_decref(&bs);
306 }
307 
308 static void test_add_chunk_reallocation_overflow(void **_CBOR_UNUSED(_state)) {
309   bs = cbor_new_indefinite_bytestring();
310   cbor_item_t *chunk = cbor_build_bytestring((cbor_data) "Hello!", 6);
311   struct cbor_indefinite_string_data *metadata =
312       (struct cbor_indefinite_string_data *)bs->data;
313   // Pretend we already have many chunks allocated
314   metadata->chunk_count = SIZE_MAX;
315   metadata->chunk_capacity = SIZE_MAX;
316 
317   assert_false(cbor_bytestring_add_chunk(bs, chunk));
318   assert_size_equal(cbor_refcount(chunk), 1);
319 
320   metadata->chunk_count = 0;
321   metadata->chunk_capacity = 0;
322   cbor_decref(&chunk);
323   cbor_decref(&bs);
324 }
325 
326 static void test_bytestring_creation(void **_CBOR_UNUSED(_state)) {
327   WITH_FAILING_MALLOC({ assert_null(cbor_new_definite_bytestring()); });
328 
329   WITH_FAILING_MALLOC({ assert_null(cbor_new_indefinite_bytestring()); });
330   WITH_MOCK_MALLOC({ assert_null(cbor_new_indefinite_bytestring()); }, 2,
331                    MALLOC, MALLOC_FAIL);
332 
333   unsigned char bytes[] = {0, 0, 0xFF, 0xAB};
334 
335   WITH_FAILING_MALLOC({ assert_null(cbor_build_bytestring(bytes, 4)); });
336   WITH_MOCK_MALLOC({ assert_null(cbor_build_bytestring(bytes, 4)); }, 2, MALLOC,
337                    MALLOC_FAIL);
338 }
339 
340 static void test_bytestring_add_chunk(void **_CBOR_UNUSED(_state)) {
341   unsigned char bytes[] = {0, 0, 0xFF, 0xAB};
342   WITH_MOCK_MALLOC(
343       {
344         cbor_item_t *bytestring = cbor_new_indefinite_bytestring();
345         cbor_item_t *chunk = cbor_build_bytestring(bytes, 4);
346 
347         assert_false(cbor_bytestring_add_chunk(bytestring, chunk));
348 
349         assert_size_equal(cbor_bytestring_chunk_count(bytestring), 0);
350         assert_size_equal(
351             ((struct cbor_indefinite_string_data *)bytestring->data)
352                 ->chunk_capacity,
353             0);
354 
355         cbor_decref(&chunk);
356         cbor_decref(&bytestring);
357       },
358       5, MALLOC, MALLOC, MALLOC, MALLOC, REALLOC_FAIL);
359 }
360 
361 int main(void) {
362   const struct CMUnitTest tests[] = {
363       cmocka_unit_test(test_empty_bs),
364       cmocka_unit_test(test_embedded_bs),
365       cmocka_unit_test(test_notenough_data),
366       cmocka_unit_test(test_short_bs1),
367       cmocka_unit_test(test_short_bs2),
368       cmocka_unit_test(test_half_bs),
369       cmocka_unit_test(test_int_bs),
370       cmocka_unit_test(test_long_bs),
371       cmocka_unit_test(test_zero_indef),
372       cmocka_unit_test(test_short_indef),
373       cmocka_unit_test(test_two_indef),
374       cmocka_unit_test(test_missing_indef),
375       cmocka_unit_test(test_inline_creation),
376       cmocka_unit_test(test_add_chunk_reallocation_overflow),
377       cmocka_unit_test(test_bytestring_creation),
378       cmocka_unit_test(test_bytestring_add_chunk),
379   };
380   return cmocka_run_group_tests(tests, NULL, NULL);
381 }
382