blob: aaad0d6b313607d841ed67539a56924739befc21 [file] [log] [blame]
Anna Karas5664bc82020-07-02 10:09:47 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2
3#include <stdlib.h>
4#include <string.h>
5#include <lib.h>
6#include <stdint.h>
7#include <tests/test.h>
8
9struct hexstr_t {
10 char *str;
11 int *val;
12 size_t res;
13} hexstr[] = {
14 {.str = "A", .res = 0},
Jakub Czapigac08b6a72022-01-10 13:36:47 +000015 {.str = "AB", .val = (int[]){171}, .res = 1},
16 {.str = "277a", .val = (int[]){39, 122}, .res = 2},
17 {.str = "277ab", .val = (int[]){39, 122}, .res = 2},
18 {.str = "\n\rx1234567ijkl", .val = (int[]){18, 52, 86}, .res = 3},
19 {.str = "\nB*e/ef-", .val = (int[]){190, 239}, .res = 2},
Anna Karas5664bc82020-07-02 10:09:47 +020020};
21
22static void test_hexstrtobin(void **state)
23{
24 uint8_t *buf;
25 size_t res, len;
26
27 for (int i = 0; i < ARRAY_SIZE(hexstr); i++) {
28 len = strlen(hexstr[i].str) / 2 + 1;
29 buf = malloc(len);
30 res = hexstrtobin(hexstr[i].str, buf, len);
31
32 assert_int_equal(hexstr[i].res, res);
33
34 for (int j = 0; j < res; j++)
35 assert_int_equal(hexstr[i].val[j], buf[j]);
36
37 free(buf);
38 }
39}
40
41int main(void)
42{
43 const struct CMUnitTest tests[] = {
44 cmocka_unit_test(test_hexstrtobin),
45 };
46
Jakub Czapiga7c6081e2021-08-25 16:27:35 +020047 return cb_run_group_tests(tests, NULL, NULL);
Anna Karas5664bc82020-07-02 10:09:47 +020048}