| 1 |
// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
|
| 6 |
#include "test_util.h" |
| 7 |
#include "../cleanup.h" |
| 8 |
#include "../hexlify.h" |
| 9 |
|
| 10 |
static const uint8_t binary[] = { |
| 11 |
0x19, 0x29, 0x1d, 0x9a, 0xc4, 0xf3, 0x4c, 0x42, 0x01, 0xee, |
| 12 |
0xdf, 0x9e, 0x8d, 0x1e, 0x59, 0x68, 0xf7, 0xd5, 0x48, 0x19, |
| 13 |
}; |
| 14 |
static const char str[] = "19291d9ac4f34c4201eedf9e8d1e5968f7d54819"; |
| 15 |
|
| 16 |
#suite hexlify |
| 17 |
|
| 18 |
#tcase hexlify |
| 19 |
|
| 20 |
#test hexlify_simple |
| 21 |
{ |
| 22 |
char out[2 * sizeof(binary) + 1]; |
| 23 |
out[sizeof(out) - 1] = '~'; |
| 24 |
hexlify(binary, sizeof(binary), out); |
| 25 |
ck_assert_mem_eq(out, str, sizeof(out) - 1); |
| 26 |
// Test that the string wasn't null-terminated. |
| 27 |
ck_assert_int_eq(out[sizeof(out) - 1], '~'); |
| 28 |
} |
| 29 |
|
| 30 |
#test ahexlify_simple |
| 31 |
{ |
| 32 |
_cleanup_free_ char *out = ahexlify(binary, sizeof(binary)); |
| 33 |
ck_assert_ptr_nonnull(out); |
| 34 |
ck_assert_str_eq(out, str); |
| 35 |
} |
| 36 |
|
| 37 |
#test unhexlify_simple |
| 38 |
{ |
| 39 |
uint8_t out[(sizeof(str) - 1) / 2]; |
| 40 |
ck_assert(unhexlify(str, sizeof(str) - 1, out)); |
| 41 |
ck_assert_mem_eq(out, binary, sizeof(binary)); |
| 42 |
} |
| 43 |
|
| 44 |
#test unhexlify_odd |
| 45 |
{ |
| 46 |
ck_assert(!unhexlify("abc", 3, (uint8_t [1]){})); |
| 47 |
} |
| 48 |
|
| 49 |
#test unhexlify_non_hex |
| 50 |
{ |
| 51 |
ck_assert(!unhexlify("foobar", 6, (uint8_t [3]){})); |
| 52 |
} |