]> git.ozlabs.org Git - ccan/blob - ccan/str/hex/test/run.c
str/hex: to-from hexstring conversion routines.
[ccan] / ccan / str / hex / test / run.c
1 #include <ccan/str/hex/hex.h>
2 /* Include the C files directly. */
3 #include <ccan/str/hex/hex.c>
4 #include <ccan/tap/tap.h>
5 #include <string.h>
6
7 int main(void)
8 {
9         const char teststr[] = "0123456789abcdefABCDEF";
10         const char bad_teststr[] = "0123456789abcdefABCDEF1O";
11         const unsigned char testdata[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
12                                            0xcd, 0xef, 0xAB, 0xCD, 0xEF };
13         unsigned char data[11];
14         char str[23];
15         size_t i;
16         
17         plan_tests(10 + sizeof(str));
18         
19         ok1(hex_str_size(sizeof(testdata)) == sizeof(teststr));
20         /* This gives right result with or without nul included */
21         ok1(hex_data_size(strlen(teststr)) == sizeof(testdata));
22         ok1(hex_data_size(sizeof(teststr)) == sizeof(testdata));
23
24         ok1(hex_decode(teststr, strlen(teststr), data, sizeof(data)));
25         ok1(memcmp(data, testdata, sizeof(testdata)) == 0);
26         ok1(hex_encode(testdata, sizeof(testdata), str, sizeof(str)));
27         ok1(strcmp(str, "0123456789abcdefabcdef") == 0);
28
29         /* Bad char */
30         ok1(!hex_decode(bad_teststr, strlen(bad_teststr), data, sizeof(data)));
31         /* Bad hex string len */
32         ok1(!hex_decode(teststr, strlen(teststr) - 1, data, sizeof(data)));
33         /* Bad buffer len */
34         ok1(!hex_decode(teststr, strlen(teststr), data, sizeof(data) - 1));
35
36         /* Bad deststring size. */
37         for (i = 1; i <= sizeof(str); i++)
38                 ok1(!hex_encode(testdata, sizeof(testdata), str, sizeof(str)-i));
39
40         /* This exits depending on whether all tests passed */
41         return exit_status();
42 }