|
7 | 7 | #include "common.h" |
8 | 8 |
|
9 | 9 | typedef struct { |
10 | | - char* hex; |
| 10 | + char* in; |
11 | 11 | char* expected; |
| 12 | + ERROR status; |
12 | 13 | } Test ; |
13 | 14 |
|
14 | | -Test tests[] = { |
15 | | - {"01", "\x01"}, |
16 | | - {NULL, NULL} |
17 | | -}; |
18 | 15 |
|
19 | | -int main(){ |
20 | | - for (int i = 0; tests[i].hex != NULL; i++){ |
| 16 | + |
| 17 | + |
| 18 | +int test_utils_fromHex(void){ |
| 19 | + Test tests[] = { |
| 20 | + {"00", "\x00", OK}, |
| 21 | + {"01", "\x01", OK}, |
| 22 | + {"ff", "\xff", OK}, |
| 23 | + {"000x", "", BAD_INPUT}, |
| 24 | + {"000", "", BAD_INPUT}, |
| 25 | + {"0102030405060708090a0b0c0d0e0f10", "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10", OK}, |
| 26 | + {NULL, NULL} |
| 27 | + }; |
| 28 | + printf("TESTING: fromHex()\n"); |
| 29 | + for (int i = 0; tests[i].in != NULL; i++){ |
21 | 30 | ERROR status = OK; |
| 31 | + const bytes in = (const bytes) tests[i].in; |
| 32 | + const bytes expected = (const bytes) tests[i].expected; |
| 33 | + ERROR expected_status = tests[i].status; |
22 | 34 | char buffer[256] = {0}; |
23 | 35 | int len = 0; |
24 | 36 |
|
25 | | - status = fromHex(tests[i].hex, strlen(tests[i].hex), buffer, 256, &len); |
26 | | - if(status != OK){ |
27 | | - log_error(status); exit(1); |
| 37 | + status = fromHex(in, strlen(in), (bytes) buffer, 256, &len); |
| 38 | + |
| 39 | + if(status != expected_status){ |
| 40 | + printf(" FAIL: h(%s) unexpected status (got=%s, expected=%s)\n", in, error_string(status), error_string(expected_status)); |
| 41 | + |
| 42 | + }else if((strcmp(expected, buffer) !=0) || (strlen(expected) != strlen(buffer))){ |
| 43 | + printf(" FAIL: h(%s)\n", in); |
| 44 | + printf(" expected: x(");print_hex(expected);printf(")%lu\n", strlen(expected)); |
| 45 | + printf(" got: x(");print_hex((const bytes) buffer);printf(")%lu \n",strlen(buffer)); |
| 46 | + }else{ |
| 47 | + printf(" pass: h(%s)\n", in); |
28 | 48 | } |
29 | | - printf("fromHex(%s) -> x(", tests[i].hex);print_hex(buffer);printf(")\n"); |
| 49 | + |
30 | 50 | } |
31 | 51 | return 0; |
32 | 52 | } |
| 53 | + |
| 54 | +int main(){ |
| 55 | + return test_utils_fromHex(); |
| 56 | +} |
0 commit comments