summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/regress/lib/libssl/bytestring/bytestringtest.c658
1 files changed, 337 insertions, 321 deletions
diff --git a/src/regress/lib/libssl/bytestring/bytestringtest.c b/src/regress/lib/libssl/bytestring/bytestringtest.c
index 65b638132a..d989868c85 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.6 2015/06/17 07:15:52 doug Exp $ */ 1/* $OpenBSD: bytestringtest.c,v 1.7 2015/06/23 01:20:24 doug Exp $ */
2/* 2/*
3 * Copyright (c) 2014, Google Inc. 3 * Copyright (c) 2014, Google Inc.
4 * 4 *
@@ -25,6 +25,22 @@
25/* This is from <openssl/base.h> in boringssl */ 25/* This is from <openssl/base.h> in boringssl */
26#define OPENSSL_U64(x) x##ULL 26#define OPENSSL_U64(x) x##ULL
27 27
28#define CHECK(a) do { \
29 if (!(a)) { \
30 printf("Error in %s [%s:%d]\n", __func__, __FILE__, \
31 __LINE__); \
32 return 0; \
33 } \
34} while (0)
35
36#define CHECK_GOTO(a) do { \
37 if (!(a)) { \
38 printf("Error in %s [%s:%d]\n", __func__, __FILE__, \
39 __LINE__); \
40 goto err; \
41 } \
42} while (0)
43
28static int 44static int
29test_skip(void) 45test_skip(void)
30{ 46{
@@ -32,12 +48,15 @@ test_skip(void)
32 CBS data; 48 CBS data;
33 49
34 CBS_init(&data, kData, sizeof(kData)); 50 CBS_init(&data, kData, sizeof(kData));
35 return CBS_len(&data) == 3 && 51
36 CBS_skip(&data, 1) && 52 CHECK(CBS_len(&data) == 3);
37 CBS_len(&data) == 2 && 53 CHECK(CBS_skip(&data, 1));
38 CBS_skip(&data, 2) && 54 CHECK(CBS_len(&data) == 2);
39 CBS_len(&data) == 0 && 55 CHECK(CBS_skip(&data, 2));
40 !CBS_skip(&data, 1); 56 CHECK(CBS_len(&data) == 0);
57 CHECK(!CBS_skip(&data, 1));
58
59 return 1;
41} 60}
42 61
43static int 62static int
@@ -50,15 +69,18 @@ test_get_u(void)
50 CBS data; 69 CBS data;
51 70
52 CBS_init(&data, kData, sizeof(kData)); 71 CBS_init(&data, kData, sizeof(kData));
53 return CBS_get_u8(&data, &u8) && 72
54 u8 == 1 && 73 CHECK(CBS_get_u8(&data, &u8));
55 CBS_get_u16(&data, &u16) && 74 CHECK(u8 == 1);
56 u16 == 0x203 && 75 CHECK(CBS_get_u16(&data, &u16));
57 CBS_get_u24(&data, &u32) && 76 CHECK(u16 == 0x203);
58 u32 == 0x40506 && 77 CHECK(CBS_get_u24(&data, &u32));
59 CBS_get_u32(&data, &u32) && 78 CHECK(u32 == 0x40506);
60 u32 == 0x708090a && 79 CHECK(CBS_get_u32(&data, &u32));
61 !CBS_get_u8(&data, &u8); 80 CHECK(u32 == 0x708090a);
81 CHECK(!CBS_get_u8(&data, &u8));
82
83 return 1;
62} 84}
63 85
64static int 86static int
@@ -71,18 +93,21 @@ test_get_prefixed(void)
71 CBS data, prefixed; 93 CBS data, prefixed;
72 94
73 CBS_init(&data, kData, sizeof(kData)); 95 CBS_init(&data, kData, sizeof(kData));
74 return CBS_get_u8_length_prefixed(&data, &prefixed) && 96
75 CBS_len(&prefixed) == 1 && 97 CHECK(CBS_get_u8_length_prefixed(&data, &prefixed));
76 CBS_get_u8(&prefixed, &u8) && 98 CHECK(CBS_len(&prefixed) == 1);
77 u8 == 2 && 99 CHECK(CBS_get_u8(&prefixed, &u8));
78 CBS_get_u16_length_prefixed(&data, &prefixed) && 100 CHECK(u8 == 2);
79 CBS_len(&prefixed) == 2 && 101 CHECK(CBS_get_u16_length_prefixed(&data, &prefixed));
80 CBS_get_u16(&prefixed, &u16) && 102 CHECK(CBS_len(&prefixed) == 2);
81 u16 == 0x304 && 103 CHECK(CBS_get_u16(&prefixed, &u16));
82 CBS_get_u24_length_prefixed(&data, &prefixed) && 104 CHECK(u16 == 0x304);
83 CBS_len(&prefixed) == 3 && 105 CHECK(CBS_get_u24_length_prefixed(&data, &prefixed));
84 CBS_get_u24(&prefixed, &u32) && 106 CHECK(CBS_len(&prefixed) == 3);
85 u32 == 0x30201; 107 CHECK(CBS_get_u24(&prefixed, &u32));
108 CHECK(u32 == 0x30201);
109
110 return 1;
86} 111}
87 112
88static int 113static int
@@ -94,16 +119,13 @@ test_get_prefixed_bad(void)
94 CBS data, prefixed; 119 CBS data, prefixed;
95 120
96 CBS_init(&data, kData1, sizeof(kData1)); 121 CBS_init(&data, kData1, sizeof(kData1));
97 if (CBS_get_u8_length_prefixed(&data, &prefixed)) 122 CHECK(!CBS_get_u8_length_prefixed(&data, &prefixed));
98 return 0;
99 123
100 CBS_init(&data, kData2, sizeof(kData2)); 124 CBS_init(&data, kData2, sizeof(kData2));
101 if (CBS_get_u16_length_prefixed(&data, &prefixed)) 125 CHECK(!CBS_get_u16_length_prefixed(&data, &prefixed));
102 return 0;
103 126
104 CBS_init(&data, kData3, sizeof(kData3)); 127 CBS_init(&data, kData3, sizeof(kData3));
105 if (CBS_get_u24_length_prefixed(&data, &prefixed)) 128 CHECK(!CBS_get_u24_length_prefixed(&data, &prefixed));
106 return 0;
107 129
108 return 1; 130 return 1;
109} 131}
@@ -126,99 +148,87 @@ test_get_asn1(void)
126 uint64_t value; 148 uint64_t value;
127 149
128 CBS_init(&data, kData1, sizeof(kData1)); 150 CBS_init(&data, kData1, sizeof(kData1));
129 if (CBS_peek_asn1_tag(&data, 0x1) || !CBS_peek_asn1_tag(&data, 0x30))
130 return 0;
131 151
132 if (!CBS_get_asn1(&data, &contents, 0x30) || 152 CHECK(!CBS_peek_asn1_tag(&data, 0x1));
133 CBS_len(&contents) != 2 || 153 CHECK(CBS_peek_asn1_tag(&data, 0x30));
134 memcmp(CBS_data(&contents), "\x01\x02", 2) != 0) 154
135 return 0; 155 CHECK(CBS_get_asn1(&data, &contents, 0x30));
156 CHECK(CBS_len(&contents) == 2);
157 CHECK(memcmp(CBS_data(&contents), "\x01\x02", 2) == 0);
136 158
137 CBS_init(&data, kData2, sizeof(kData2)); 159 CBS_init(&data, kData2, sizeof(kData2));
138 /* data is truncated */ 160 /* data is truncated */
139 if (CBS_get_asn1(&data, &contents, 0x30)) 161 CHECK(!CBS_get_asn1(&data, &contents, 0x30));
140 return 0;
141 162
142 CBS_init(&data, kData3, sizeof(kData3)); 163 CBS_init(&data, kData3, sizeof(kData3));
143 /* zero byte length of length */ 164 /* zero byte length of length */
144 if (CBS_get_asn1(&data, &contents, 0x30)) 165 CHECK(!CBS_get_asn1(&data, &contents, 0x30));
145 return 0;
146 166
147 CBS_init(&data, kData4, sizeof(kData4)); 167 CBS_init(&data, kData4, sizeof(kData4));
148 /* long form mistakenly used. */ 168 /* long form mistakenly used. */
149 if (CBS_get_asn1(&data, &contents, 0x30)) 169 CHECK(!CBS_get_asn1(&data, &contents, 0x30));
150 return 0;
151 170
152 CBS_init(&data, kData5, sizeof(kData5)); 171 CBS_init(&data, kData5, sizeof(kData5));
153 /* length takes too many bytes. */ 172 /* length takes too many bytes. */
154 if (CBS_get_asn1(&data, &contents, 0x30)) 173 CHECK(!CBS_get_asn1(&data, &contents, 0x30));
155 return 0;
156 174
157 CBS_init(&data, kData1, sizeof(kData1)); 175 CBS_init(&data, kData1, sizeof(kData1));
158 /* wrong tag. */ 176 /* wrong tag. */
159 if (CBS_get_asn1(&data, &contents, 0x31)) 177 CHECK(!CBS_get_asn1(&data, &contents, 0x31));
160 return 0;
161 178
162 CBS_init(&data, NULL, 0); 179 CBS_init(&data, NULL, 0);
163 /* peek at empty data. */ 180 /* peek at empty data. */
164 if (CBS_peek_asn1_tag(&data, 0x30)) 181 CHECK(!CBS_peek_asn1_tag(&data, 0x30));
165 return 0;
166 182
167 CBS_init(&data, NULL, 0); 183 CBS_init(&data, NULL, 0);
168 /* optional elements at empty data. */ 184 /* optional elements at empty data. */
169 if (!CBS_get_optional_asn1(&data, &contents, &present, 0xa0) || 185 CHECK(CBS_get_optional_asn1(&data, &contents, &present, 0xa0));
170 present || 186 CHECK(!present);
171 !CBS_get_optional_asn1_octet_string(&data, &contents, &present, 187 CHECK(CBS_get_optional_asn1_octet_string(&data, &contents, &present,
172 0xa0) || 188 0xa0));
173 present || 189 CHECK(!present);
174 CBS_len(&contents) != 0 || 190 CHECK(CBS_len(&contents) == 0);
175 !CBS_get_optional_asn1_octet_string(&data, &contents, NULL, 0xa0) || 191 CHECK(CBS_get_optional_asn1_octet_string(&data, &contents, NULL, 0xa0));
176 CBS_len(&contents) != 0 || 192 CHECK(CBS_len(&contents) == 0);
177 !CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42) || 193 CHECK(CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42));
178 value != 42) 194 CHECK(value == 42);
179 return 0;
180 195
181 CBS_init(&data, kData6, sizeof(kData6)); 196 CBS_init(&data, kData6, sizeof(kData6));
182 /* optional element. */ 197 /* optional element. */
183 if (!CBS_get_optional_asn1(&data, &contents, &present, 0xa0) || 198 CHECK(CBS_get_optional_asn1(&data, &contents, &present, 0xa0));
184 present || 199 CHECK(!present);
185 !CBS_get_optional_asn1(&data, &contents, &present, 0xa1) || 200 CHECK(CBS_get_optional_asn1(&data, &contents, &present, 0xa1));
186 !present || 201 CHECK(present);
187 CBS_len(&contents) != 3 || 202 CHECK(CBS_len(&contents) == 3);
188 memcmp(CBS_data(&contents), "\x04\x01\x01", 3) != 0) 203 CHECK(memcmp(CBS_data(&contents), "\x04\x01\x01", 3) == 0);
189 return 0;
190 204
191 CBS_init(&data, kData6, sizeof(kData6)); 205 CBS_init(&data, kData6, sizeof(kData6));
192 /* optional octet string. */ 206 /* optional octet string. */
193 if (!CBS_get_optional_asn1_octet_string(&data, &contents, &present, 207 CHECK(CBS_get_optional_asn1_octet_string(&data, &contents, &present,
194 0xa0) || 208 0xa0));
195 present || 209 CHECK(!present);
196 CBS_len(&contents) != 0 || 210 CHECK(CBS_len(&contents) == 0);
197 !CBS_get_optional_asn1_octet_string(&data, &contents, &present, 211 CHECK(CBS_get_optional_asn1_octet_string(&data, &contents, &present,
198 0xa1) || 212 0xa1));
199 !present || 213 CHECK(present);
200 CBS_len(&contents) != 1 || 214 CHECK(CBS_len(&contents) == 1);
201 CBS_data(&contents)[0] != 1) 215 CHECK(CBS_data(&contents)[0] == 1);
202 return 0;
203 216
204 CBS_init(&data, kData7, sizeof(kData7)); 217 CBS_init(&data, kData7, sizeof(kData7));
205 /* invalid optional octet string. */ 218 /* invalid optional octet string. */
206 if (CBS_get_optional_asn1_octet_string(&data, &contents, &present, 219 CHECK(!CBS_get_optional_asn1_octet_string(&data, &contents, &present,
207 0xa1)) 220 0xa1));
208 return 0;
209 221
210 CBS_init(&data, kData8, sizeof(kData8)); 222 CBS_init(&data, kData8, sizeof(kData8));
211 /* optional octet string. */ 223 /* optional octet string. */
212 if (!CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42) || 224 CHECK(CBS_get_optional_asn1_uint64(&data, &value, 0xa0, 42));
213 value != 42 || 225 CHECK(value == 42);
214 !CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42) || 226 CHECK(CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42));
215 value != 1) 227 CHECK(value == 1);
216 return 0;
217 228
218 CBS_init(&data, kData9, sizeof(kData9)); 229 CBS_init(&data, kData9, sizeof(kData9));
219 /* invalid optional integer. */ 230 /* invalid optional integer. */
220 if (CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42)) 231 CHECK(!CBS_get_optional_asn1_uint64(&data, &value, 0xa1, 42));
221 return 0;
222 232
223 return 1; 233 return 1;
224} 234}
@@ -235,22 +245,21 @@ test_get_optional_asn1_bool(void)
235 245
236 CBS_init(&data, NULL, 0); 246 CBS_init(&data, NULL, 0);
237 val = 2; 247 val = 2;
238 if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0) || val != 0) 248 CHECK(CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0));
239 return 0; 249 CHECK(val == 0);
240 250
241 CBS_init(&data, kTrue, sizeof(kTrue)); 251 CBS_init(&data, kTrue, sizeof(kTrue));
242 val = 2; 252 val = 2;
243 if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0) || val != 1) 253 CHECK(CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0));
244 return 0; 254 CHECK(val == 1);
245 255
246 CBS_init(&data, kFalse, sizeof(kFalse)); 256 CBS_init(&data, kFalse, sizeof(kFalse));
247 val = 2; 257 val = 2;
248 if (!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1) || val != 0) 258 CHECK(CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1));
249 return 0; 259 CHECK(val == 0);
250 260
251 CBS_init(&data, kInvalid, sizeof(kInvalid)); 261 CBS_init(&data, kInvalid, sizeof(kInvalid));
252 if (CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1)) 262 CHECK(!CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1));
253 return 0;
254 263
255 return 1; 264 return 1;
256} 265}
@@ -259,28 +268,31 @@ static int
259test_cbb_basic(void) 268test_cbb_basic(void)
260{ 269{
261 static const uint8_t kExpected[] = {1, 2, 3, 4, 5, 6, 7, 8}; 270 static const uint8_t kExpected[] = {1, 2, 3, 4, 5, 6, 7, 8};
262 uint8_t *buf; 271 uint8_t *buf = NULL;
263 size_t buf_len; 272 size_t buf_len;
264 int ok; 273 int ret = 0;
265 CBB cbb; 274 CBB cbb;
266 275
267 if (!CBB_init(&cbb, 100)) 276 CHECK(CBB_init(&cbb, 100));
268 return 0;
269 277
270 CBB_cleanup(&cbb); 278 CBB_cleanup(&cbb);
271 279
272 if (!CBB_init(&cbb, 0) || 280 CHECK(CBB_init(&cbb, 0));
273 !CBB_add_u8(&cbb, 1) || 281 CHECK_GOTO(CBB_add_u8(&cbb, 1));
274 !CBB_add_u16(&cbb, 0x203) || 282 CHECK_GOTO(CBB_add_u16(&cbb, 0x203));
275 !CBB_add_u24(&cbb, 0x40506) || 283 CHECK_GOTO(CBB_add_u24(&cbb, 0x40506));
276 !CBB_add_bytes(&cbb, (const uint8_t*) "\x07\x08", 2) || 284 CHECK_GOTO(CBB_add_bytes(&cbb, (const uint8_t*) "\x07\x08", 2));
277 !CBB_finish(&cbb, &buf, &buf_len)) 285 CHECK_GOTO(CBB_finish(&cbb, &buf, &buf_len));
278 return 0;
279 286
280 ok = buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) 287 ret = (buf_len == sizeof(kExpected)
281 == 0; 288 && memcmp(buf, kExpected, buf_len) == 0);
289
290 if (0) {
291err:
292 CBB_cleanup(&cbb);
293 }
282 free(buf); 294 free(buf);
283 return ok; 295 return ret;
284} 296}
285 297
286static int 298static int
@@ -288,26 +300,28 @@ test_cbb_fixed(void)
288{ 300{
289 CBB cbb; 301 CBB cbb;
290 uint8_t buf[1]; 302 uint8_t buf[1];
291 uint8_t *out_buf; 303 uint8_t *out_buf = NULL;
292 size_t out_size; 304 size_t out_size;
305 int ret = 0;
293 306
294 if (!CBB_init_fixed(&cbb, NULL, 0) || 307 CHECK(CBB_init_fixed(&cbb, NULL, 0));
295 CBB_add_u8(&cbb, 1) || 308 CHECK_GOTO(!CBB_add_u8(&cbb, 1));
296 !CBB_finish(&cbb, &out_buf, &out_size) || 309 CHECK_GOTO(CBB_finish(&cbb, &out_buf, &out_size));
297 out_buf != NULL || 310 CHECK(out_buf == NULL && out_size == 0);
298 out_size != 0)
299 return 0;
300
301 if (!CBB_init_fixed(&cbb, buf, 1) ||
302 !CBB_add_u8(&cbb, 1) ||
303 CBB_add_u8(&cbb, 2) ||
304 !CBB_finish(&cbb, &out_buf, &out_size) ||
305 out_buf != buf ||
306 out_size != 1 ||
307 buf[0] != 1)
308 return 0;
309 311
310 return 1; 312 CHECK(CBB_init_fixed(&cbb, buf, 1));
313 CHECK_GOTO(CBB_add_u8(&cbb, 1));
314 CHECK_GOTO(!CBB_add_u8(&cbb, 2));
315 CHECK_GOTO(CBB_finish(&cbb, &out_buf, &out_size));
316
317 ret = (out_buf == buf && out_size == 1 && buf[0] == 1);
318
319 if (0) {
320err:
321 CBB_cleanup(&cbb);
322 }
323
324 return ret;
311} 325}
312 326
313static int 327static int
@@ -318,15 +332,12 @@ test_cbb_finish_child(void)
318 size_t out_size; 332 size_t out_size;
319 int ret = 0; 333 int ret = 0;
320 334
321 if (!CBB_init(&cbb, 16) || 335 CHECK(CBB_init(&cbb, 16));
322 !CBB_add_u8_length_prefixed(&cbb, &child) || 336 CHECK_GOTO(CBB_add_u8_length_prefixed(&cbb, &child));
323 CBB_finish(&child, &out_buf, &out_size) || 337 CHECK_GOTO(!CBB_finish(&child, &out_buf, &out_size));
324 !CBB_finish(&cbb, &out_buf, &out_size) || 338 CHECK_GOTO(CBB_finish(&cbb, &out_buf, &out_size));
325 out_size != 1 ||
326 out_buf[0] != 0)
327 goto err;
328 339
329 ret = 1; 340 ret = (out_size == 1 && out_buf[0] == 0);
330 341
331err: 342err:
332 free(out_buf); 343 free(out_buf);
@@ -341,31 +352,33 @@ test_cbb_prefixed(void)
341 uint8_t *buf = NULL; 352 uint8_t *buf = NULL;
342 size_t buf_len; 353 size_t buf_len;
343 CBB cbb, contents, inner_contents, inner_inner_contents; 354 CBB cbb, contents, inner_contents, inner_inner_contents;
344 int ok = 0; 355 int ret = 0;
345
346 if (!CBB_init(&cbb, 0) ||
347 !CBB_add_u8_length_prefixed(&cbb, &contents) ||
348 !CBB_add_u8_length_prefixed(&cbb, &contents) ||
349 !CBB_add_u8(&contents, 1) ||
350 !CBB_add_u16_length_prefixed(&cbb, &contents) ||
351 !CBB_add_u16(&contents, 0x203) ||
352 !CBB_add_u24_length_prefixed(&cbb, &contents) ||
353 !CBB_add_u24(&contents, 0x40506) ||
354 !CBB_add_u8_length_prefixed(&cbb, &contents) ||
355 !CBB_add_u8_length_prefixed(&contents, &inner_contents) ||
356 !CBB_add_u8(&inner_contents, 1) ||
357 !CBB_add_u16_length_prefixed(&inner_contents,
358 &inner_inner_contents) ||
359 !CBB_add_u8(&inner_inner_contents, 2) ||
360 !CBB_finish(&cbb, &buf, &buf_len))
361 goto err;
362
363 ok = buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len)
364 == 0;
365 356
357 CHECK(CBB_init(&cbb, 0));
358 CHECK_GOTO(CBB_add_u8_length_prefixed(&cbb, &contents));
359 CHECK_GOTO(CBB_add_u8_length_prefixed(&cbb, &contents));
360 CHECK_GOTO(CBB_add_u8(&contents, 1));
361 CHECK_GOTO(CBB_add_u16_length_prefixed(&cbb, &contents));
362 CHECK_GOTO(CBB_add_u16(&contents, 0x203));
363 CHECK_GOTO(CBB_add_u24_length_prefixed(&cbb, &contents));
364 CHECK_GOTO(CBB_add_u24(&contents, 0x40506));
365 CHECK_GOTO(CBB_add_u8_length_prefixed(&cbb, &contents));
366 CHECK_GOTO(CBB_add_u8_length_prefixed(&contents, &inner_contents));
367 CHECK_GOTO(CBB_add_u8(&inner_contents, 1));
368 CHECK_GOTO(CBB_add_u16_length_prefixed(&inner_contents,
369 &inner_inner_contents));
370 CHECK_GOTO(CBB_add_u8(&inner_inner_contents, 2));
371 CHECK_GOTO(CBB_finish(&cbb, &buf, &buf_len));
372
373 ret = (buf_len == sizeof(kExpected)
374 && memcmp(buf, kExpected, buf_len) == 0);
375
376 if (0) {
366err: 377err:
378 CBB_cleanup(&cbb);
379 }
367 free(buf); 380 free(buf);
368 return ok; 381 return ret;
369} 382}
370 383
371static int 384static int
@@ -376,34 +389,30 @@ test_cbb_misuse(void)
376 size_t buf_len; 389 size_t buf_len;
377 int ret = 0; 390 int ret = 0;
378 391
379 if (!CBB_init(&cbb, 0) || 392 CHECK(CBB_init(&cbb, 0));
380 !CBB_add_u8_length_prefixed(&cbb, &child) || 393 CHECK_GOTO(CBB_add_u8_length_prefixed(&cbb, &child));
381 !CBB_add_u8(&child, 1) || 394 CHECK_GOTO(CBB_add_u8(&child, 1));
382 !CBB_add_u8(&cbb, 2)) 395 CHECK_GOTO(CBB_add_u8(&cbb, 2));
383 return 0;
384 396
385 /* 397 /*
386 * Since we wrote to |cbb|, |child| is now invalid and attempts to write 398 * Since we wrote to |cbb|, |child| is now invalid and attempts to write
387 * to it should fail. 399 * to it should fail.
388 */ 400 */
389 if (CBB_add_u8(&child, 1) || 401 CHECK_GOTO(!CBB_add_u8(&child, 1));
390 CBB_add_u16(&child, 1) || 402 CHECK_GOTO(!CBB_add_u16(&child, 1));
391 CBB_add_u24(&child, 1) || 403 CHECK_GOTO(!CBB_add_u24(&child, 1));
392 CBB_add_u8_length_prefixed(&child, &contents) || 404 CHECK_GOTO(!CBB_add_u8_length_prefixed(&child, &contents));
393 CBB_add_u16_length_prefixed(&child, &contents) || 405 CHECK_GOTO(!CBB_add_u16_length_prefixed(&child, &contents));
394 CBB_add_asn1(&child, &contents, 1) || 406 CHECK_GOTO(!CBB_add_asn1(&child, &contents, 1));
395 CBB_add_bytes(&child, (const uint8_t*) "a", 1)) { 407 CHECK_GOTO(!CBB_add_bytes(&child, (const uint8_t*) "a", 1));
396 fprintf(stderr, "CBB operation on invalid CBB did not fail.\n"); 408 CHECK_GOTO(CBB_finish(&cbb, &buf, &buf_len));
397 return 0; 409
398 } 410 ret = (buf_len == 3 && memcmp(buf, "\x01\x01\x02", 3) == 0);
399 411
400 if (!CBB_finish(&cbb, &buf, &buf_len) || buf_len != 3 || 412 if (0) {
401 memcmp(buf, "\x01\x01\x02", 3) != 0)
402 goto err;
403
404 ret = 1;
405
406err: 413err:
414 CBB_cleanup(&cbb);
415 }
407 free(buf); 416 free(buf);
408 return ret; 417 return ret;
409} 418}
@@ -416,70 +425,75 @@ test_cbb_asn1(void)
416 size_t buf_len; 425 size_t buf_len;
417 CBB cbb, contents, inner_contents; 426 CBB cbb, contents, inner_contents;
418 int ret = 0; 427 int ret = 0;
428 int alloc = 0;
419 429
420 if (!CBB_init(&cbb, 0) || 430 CHECK_GOTO(CBB_init(&cbb, 0));
421 !CBB_add_asn1(&cbb, &contents, 0x30) || 431 alloc = 1;
422 !CBB_add_bytes(&contents, (const uint8_t*) "\x01\x02\x03", 3) || 432 CHECK_GOTO(CBB_add_asn1(&cbb, &contents, 0x30));
423 !CBB_finish(&cbb, &buf, &buf_len)) 433 CHECK_GOTO(CBB_add_bytes(&contents, (const uint8_t*) "\x01\x02\x03",
424 goto err; 434 3));
435 CHECK_GOTO(CBB_finish(&cbb, &buf, &buf_len));
436 alloc = 0;
425 437
426 if (buf_len != sizeof(kExpected) || memcmp(buf, kExpected, buf_len) 438 CHECK_GOTO(buf_len == sizeof(kExpected));
427 != 0) 439 CHECK_GOTO(memcmp(buf, kExpected, buf_len) == 0);
428 goto err;
429 440
430 free(buf); 441 free(buf);
431 buf = NULL; 442 buf = NULL;
432 443
433 if ((test_data = malloc(100000)) == NULL) 444 CHECK_GOTO(((test_data = malloc(100000)) != NULL));
434 goto err;
435 memset(test_data, 0x42, 100000); 445 memset(test_data, 0x42, 100000);
436 446
437 if (!CBB_init(&cbb, 0) || 447 CHECK_GOTO(CBB_init(&cbb, 0));
438 !CBB_add_asn1(&cbb, &contents, 0x30) || 448 alloc = 1;
439 !CBB_add_bytes(&contents, test_data, 130) || 449 CHECK_GOTO(CBB_add_asn1(&cbb, &contents, 0x30));
440 !CBB_finish(&cbb, &buf, &buf_len)) 450 CHECK_GOTO(CBB_add_bytes(&contents, test_data, 130));
441 goto err; 451 CHECK_GOTO(CBB_finish(&cbb, &buf, &buf_len));
452 alloc = 0;
453
454 CHECK_GOTO(buf_len == 3 + 130);
455 CHECK_GOTO(memcmp(buf, "\x30\x81\x82", 3) == 0);
456 CHECK_GOTO(memcmp(buf + 3, test_data, 130) == 0);
442 457
443 if (buf_len != 3 + 130 ||
444 memcmp(buf, "\x30\x81\x82", 3) != 0 ||
445 memcmp(buf + 3, test_data, 130) != 0) {
446 goto err;
447 }
448 free(buf); 458 free(buf);
449 buf = NULL; 459 buf = NULL;
450 460
451 if (!CBB_init(&cbb, 0) || 461 CHECK_GOTO(CBB_init(&cbb, 0));
452 !CBB_add_asn1(&cbb, &contents, 0x30) || 462 alloc = 1;
453 !CBB_add_bytes(&contents, test_data, 1000) || 463 CHECK_GOTO(CBB_add_asn1(&cbb, &contents, 0x30));
454 !CBB_finish(&cbb, &buf, &buf_len)) 464 CHECK_GOTO(CBB_add_bytes(&contents, test_data, 1000));
455 goto err; 465 CHECK_GOTO(CBB_finish(&cbb, &buf, &buf_len));
466 alloc = 0;
467
468 CHECK_GOTO(buf_len == 4 + 1000);
469 CHECK_GOTO(memcmp(buf, "\x30\x82\x03\xe8", 4) == 0);
470 CHECK_GOTO(!memcmp(buf + 4, test_data, 1000));
456 471
457 if (buf_len != 4 + 1000 ||
458 memcmp(buf, "\x30\x82\x03\xe8", 4) != 0 ||
459 memcmp(buf + 4, test_data, 1000)) {
460 goto err;
461 }
462 free(buf); 472 free(buf);
463 buf = NULL; 473 buf = NULL;
464 474
465 if (!CBB_init(&cbb, 0) || 475 CHECK_GOTO(CBB_init(&cbb, 0));
466 !CBB_add_asn1(&cbb, &contents, 0x30) || 476 alloc = 1;
467 !CBB_add_asn1(&contents, &inner_contents, 0x30) || 477 CHECK_GOTO(CBB_add_asn1(&cbb, &contents, 0x30));
468 !CBB_add_bytes(&inner_contents, test_data, 100000) || 478 CHECK_GOTO(CBB_add_asn1(&contents, &inner_contents, 0x30));
469 !CBB_finish(&cbb, &buf, &buf_len)) 479 CHECK_GOTO(CBB_add_bytes(&inner_contents, test_data, 100000));
470 goto err; 480 CHECK_GOTO(CBB_finish(&cbb, &buf, &buf_len));
481 alloc = 0;
471 482
472 if (buf_len != 5 + 5 + 100000 || 483 CHECK_GOTO(buf_len == 5 + 5 + 100000);
473 memcmp(buf, "\x30\x83\x01\x86\xa5\x30\x83\x01\x86\xa0", 10) != 0 || 484 CHECK_GOTO(memcmp(buf, "\x30\x83\x01\x86\xa5\x30\x83\x01\x86\xa0", 10)
474 memcmp(buf + 10, test_data, 100000)) 485 == 0);
475 goto err; 486 CHECK_GOTO(!memcmp(buf + 10, test_data, 100000));
476 487
477 ret = 1; 488 ret = 1;
478 489
490 if (0) {
479err: 491err:
492 if (alloc)
493 CBB_cleanup(&cbb);
494 }
480 free(buf); 495 free(buf);
481 free(test_data); 496 free(test_data);
482
483 return ret; 497 return ret;
484} 498}
485 499
@@ -493,33 +507,23 @@ do_indefinite_convert(const char *name, const uint8_t *definite_expected,
493 int ret = 0; 507 int ret = 0;
494 508
495 CBS_init(&in, indefinite, indefinite_len); 509 CBS_init(&in, indefinite, indefinite_len);
496 if (!CBS_asn1_indefinite_to_definite(&in, &out, &out_len)) { 510
497 fprintf(stderr, "%s: CBS_asn1_indefinite_to_definite failed.\n", 511 CHECK_GOTO(CBS_asn1_indefinite_to_definite(&in, &out, &out_len));
498 name);
499 goto end;
500 }
501 512
502 if (out == NULL) { 513 if (out == NULL) {
503 if (indefinite_len != definite_len || 514 CHECK_GOTO(indefinite_len == definite_len);
504 memcmp(definite_expected, indefinite, indefinite_len) 515 CHECK_GOTO(memcmp(definite_expected, indefinite, indefinite_len)
505 != 0) { 516 == 0);
506 fprintf(stderr, "%s: incorrect unconverted result.\n",
507 name);
508 return 0;
509 }
510 517
511 return 1; 518 return 1;
512 } 519 }
513 520
514 if (out_len != definite_len || memcmp(out, definite_expected, 521 CHECK_GOTO(out_len == definite_len);
515 definite_len) != 0) { 522 CHECK_GOTO(memcmp(out, definite_expected, definite_len) == 0);
516 fprintf(stderr, "%s: incorrect converted result.\n", name);
517 goto end;
518 }
519 523
520 ret = 1; 524 ret = 1;
521 525
522end: 526err:
523 free(out); 527 free(out);
524 return ret; 528 return ret;
525} 529}
@@ -569,15 +573,17 @@ test_indefinite_convert(void)
569 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0, 573 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0,
570 }; 574 };
571 575
572 return do_indefinite_convert("kSimpleBER", kSimpleBER, sizeof(kSimpleBER), 576 CHECK(do_indefinite_convert("kSimpleBER", kSimpleBER, sizeof(kSimpleBER),
573 kSimpleBER, sizeof(kSimpleBER)) && 577 kSimpleBER, sizeof(kSimpleBER)));
574 do_indefinite_convert("kIndefBER", kIndefDER, sizeof(kIndefDER), kIndefBER, 578 CHECK(do_indefinite_convert("kIndefBER", kIndefDER, sizeof(kIndefDER),
575 sizeof(kIndefBER)) && 579 kIndefBER, sizeof(kIndefBER)));
576 do_indefinite_convert("kOctetStringBER", kOctetStringDER, 580 CHECK(do_indefinite_convert("kOctetStringBER", kOctetStringDER,
577 sizeof(kOctetStringDER), kOctetStringBER, 581 sizeof(kOctetStringDER), kOctetStringBER,
578 sizeof(kOctetStringBER)) && 582 sizeof(kOctetStringBER)));
579 do_indefinite_convert("kNSSBER", kNSSDER, sizeof(kNSSDER), kNSSBER, 583 CHECK(do_indefinite_convert("kNSSBER", kNSSDER, sizeof(kNSSDER), kNSSBER,
580 sizeof(kNSSBER)); 584 sizeof(kNSSBER)));
585
586 return 1;
581} 587}
582 588
583typedef struct { 589typedef struct {
@@ -619,40 +625,36 @@ static const ASN1_INVALID_UINT64_TEST kAsn1InvalidUint64Tests[] = {
619static int 625static int
620test_asn1_uint64(void) 626test_asn1_uint64(void)
621{ 627{
628 CBB cbb;
629 uint8_t *out = NULL;
622 size_t i; 630 size_t i;
631 int ret = 0;
632 int alloc = 0;
623 633
624 for (i = 0; i < sizeof(kAsn1Uint64Tests) / sizeof(kAsn1Uint64Tests[0]); 634 for (i = 0; i < sizeof(kAsn1Uint64Tests) / sizeof(kAsn1Uint64Tests[0]);
625 i++) { 635 i++) {
626 const ASN1_UINT64_TEST *test = &kAsn1Uint64Tests[i]; 636 const ASN1_UINT64_TEST *test = &kAsn1Uint64Tests[i];
627 CBS cbs; 637 CBS cbs;
628 uint64_t value; 638 uint64_t value;
629 CBB cbb;
630 uint8_t *out;
631 size_t len; 639 size_t len;
632 640
633 CBS_init(&cbs, (const uint8_t *)test->encoding, 641 CBS_init(&cbs, (const uint8_t *)test->encoding,
634 test->encoding_len); 642 test->encoding_len);
635 643
636 if (!CBS_get_asn1_uint64(&cbs, &value) || 644 CHECK(CBS_get_asn1_uint64(&cbs, &value));
637 CBS_len(&cbs) != 0 || 645 CHECK(CBS_len(&cbs) == 0);
638 value != test->value) 646 CHECK(value == test->value);
639 return 0;
640 647
641 if (!CBB_init(&cbb, 0)) 648 CHECK(CBB_init(&cbb, 0));
642 return 0; 649 alloc = 1;
650 CHECK_GOTO(CBB_add_asn1_uint64(&cbb, test->value));
651 CHECK_GOTO(CBB_finish(&cbb, &out, &len));
652 alloc = 0;
643 653
644 if (!CBB_add_asn1_uint64(&cbb, test->value) || 654 CHECK_GOTO(len == test->encoding_len);
645 !CBB_finish(&cbb, &out, &len)) { 655 CHECK_GOTO(memcmp(out, test->encoding, len) == 0);
646 CBB_cleanup(&cbb);
647 return 0;
648 }
649
650 if (len != test->encoding_len || memcmp(out, test->encoding,
651 len) != 0) {
652 free(out);
653 return 0;
654 }
655 free(out); 656 free(out);
657 out = NULL;
656 } 658 }
657 659
658 for (i = 0; i < sizeof(kAsn1InvalidUint64Tests) 660 for (i = 0; i < sizeof(kAsn1InvalidUint64Tests)
@@ -664,11 +666,19 @@ test_asn1_uint64(void)
664 666
665 CBS_init(&cbs, (const uint8_t *)test->encoding, 667 CBS_init(&cbs, (const uint8_t *)test->encoding,
666 test->encoding_len); 668 test->encoding_len);
667 if (CBS_get_asn1_uint64(&cbs, &value)) 669 CHECK(!CBS_get_asn1_uint64(&cbs, &value));
668 return 0;
669 } 670 }
670 671
671 return 1; 672 ret = 1;
673
674 if (0) {
675err:
676 if (alloc)
677 CBB_cleanup(&cbb);
678 }
679 free(out);
680
681 return ret;
672} 682}
673 683
674static int 684static int
@@ -679,27 +689,32 @@ test_offset(void)
679 CBS data; 689 CBS data;
680 690
681 CBS_init(&data, input, sizeof(input)); 691 CBS_init(&data, input, sizeof(input));
682 if (sizeof(input) != 5) 692 CHECK(sizeof(input) == 5);
683 return 0; 693 CHECK(CBS_len(&data) == 5);
684 694 CHECK(CBS_offset(&data) == 0);
685 if (!(CBS_len(&data) == 5 && CBS_offset(&data) == 0 && 695 CHECK(CBS_get_u8(&data, &v));
686 CBS_get_u8(&data, &v) && v == 1 && 696 CHECK(v == 1);
687 CBS_len(&data) == 4 && CBS_offset(&data) == 1 && 697 CHECK(CBS_len(&data) == 4);
688 CBS_skip(&data, 2) && 698 CHECK(CBS_offset(&data) == 1);
689 CBS_len(&data) == 2 && CBS_offset(&data) == 3 && 699 CHECK(CBS_skip(&data, 2));
690 CBS_get_u8(&data, &v) && v == 4 && 700 CHECK(CBS_len(&data) == 2);
691 CBS_get_u8(&data, &v) && v == 5 && 701 CHECK(CBS_offset(&data) == 3);
692 CBS_len(&data) == 0 && CBS_offset(&data) == 5 && 702 CHECK(CBS_get_u8(&data, &v));
693 !CBS_skip(&data, 1))) 703 CHECK(v == 4);
694 return 0; 704 CHECK(CBS_get_u8(&data, &v));
705 CHECK(v == 5);
706 CHECK(CBS_len(&data) == 0);
707 CHECK(CBS_offset(&data) == 5);
708 CHECK(!CBS_skip(&data, 1));
695 709
696 CBS_init(&data, input, sizeof(input)); 710 CBS_init(&data, input, sizeof(input));
697 if (!(CBS_skip(&data, 2) && 711 CHECK(CBS_skip(&data, 2));
698 CBS_len(&data) == 3 && CBS_offset(&data) == 2 && 712 CHECK(CBS_len(&data) == 3);
699 CBS_skip(&data, 3) && 713 CHECK(CBS_offset(&data) == 2);
700 CBS_len(&data) == 0 && CBS_offset(&data) == 5 && 714 CHECK(CBS_skip(&data, 3));
701 !CBS_get_u8(&data, &v))) 715 CHECK(CBS_len(&data) == 0);
702 return 0; 716 CHECK(CBS_offset(&data) == 5);
717 CHECK(!CBS_get_u8(&data, &v));
703 718
704 return 1; 719 return 1;
705} 720}
@@ -714,21 +729,20 @@ test_write_bytes(void)
714 CBS data; 729 CBS data;
715 char *tmp = NULL; 730 char *tmp = NULL;
716 731
717 if ((tmp = malloc(sizeof(input))) == NULL) { 732 CHECK_GOTO((tmp = malloc(sizeof(input))) != NULL);
718 fprintf(stderr, "failed to malloc\n");
719 goto err;
720 }
721 memset(tmp, 100, sizeof(input)); 733 memset(tmp, 100, sizeof(input));
722 734
723 CBS_init(&data, input, sizeof(input)); 735 CBS_init(&data, input, sizeof(input));
724 if (!(CBS_len(&data) == 6 && CBS_offset(&data) == 0 && 736 CHECK_GOTO(CBS_len(&data) == 6);
725 CBS_get_u8(&data, &v) && v == 102 /* f */ && 737 CHECK_GOTO(CBS_offset(&data) == 0);
726 CBS_skip(&data, 1) && 738 CHECK_GOTO(CBS_get_u8(&data, &v));
727 !CBS_skip(&data, 15) && 739 CHECK_GOTO(v == 102 /* f */);
728 CBS_write_bytes(&data, tmp, sizeof(input), &len) && 740 CHECK_GOTO(CBS_skip(&data, 1));
729 len == 4 && memcmp(input + 2, tmp, len) == 0 && 741 CHECK_GOTO(!CBS_skip(&data, 15));
730 tmp[4] == 100 && tmp[5] == 100)) 742 CHECK_GOTO(CBS_write_bytes(&data, tmp, sizeof(input), &len));
731 goto err; 743 CHECK_GOTO(len == 4);
744 CHECK_GOTO(memcmp(input + 2, tmp, len) == 0);
745 CHECK_GOTO(tmp[4] == 100 && tmp[5] == 100);
732 746
733 ret = 1; 747 ret = 1;
734 748
@@ -740,24 +754,26 @@ err:
740int 754int
741main(void) 755main(void)
742{ 756{
743 if (!test_skip() || 757 int failed = 0;
744 !test_get_u() || 758
745 !test_get_prefixed() || 759 failed |= !test_skip();
746 !test_get_prefixed_bad() || 760 failed |= !test_get_u();
747 !test_get_asn1() || 761 failed |= !test_get_prefixed();
748 !test_cbb_basic() || 762 failed |= !test_get_prefixed_bad();
749 !test_cbb_fixed() || 763 failed |= !test_get_asn1();
750 !test_cbb_finish_child() || 764 failed |= !test_cbb_basic();
751 !test_cbb_misuse() || 765 failed |= !test_cbb_fixed();
752 !test_cbb_prefixed() || 766 failed |= !test_cbb_finish_child();
753 !test_cbb_asn1() || 767 failed |= !test_cbb_misuse();
754 !test_indefinite_convert() || 768 failed |= !test_cbb_prefixed();
755 !test_asn1_uint64() || 769 failed |= !test_cbb_asn1();
756 !test_get_optional_asn1_bool() || 770 failed |= !test_indefinite_convert();
757 !test_offset() || 771 failed |= !test_asn1_uint64();
758 !test_write_bytes()) 772 failed |= !test_get_optional_asn1_bool();
759 return 1; 773 failed |= !test_offset();
760 774 failed |= !test_write_bytes();
761 printf("PASS\n"); 775
762 return 0; 776 if (!failed)
777 printf("PASS\n");
778 return failed;
763} 779}