X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Fstr%2Fhex%2Ftest%2Frun.c;fp=ccan%2Fstr%2Fhex%2Ftest%2Frun.c;h=e4c1a6c5aa3f12f8142975165ce59c29632b4c22;hp=0000000000000000000000000000000000000000;hb=fc3a762c6629caf4af8c490c8f9ccff5ac9e5a16;hpb=64b0c5cdeea5bc00ce3b9ef89fd3aade9cb0cd2c diff --git a/ccan/str/hex/test/run.c b/ccan/str/hex/test/run.c new file mode 100644 index 00000000..e4c1a6c5 --- /dev/null +++ b/ccan/str/hex/test/run.c @@ -0,0 +1,42 @@ +#include +/* Include the C files directly. */ +#include +#include +#include + +int main(void) +{ + const char teststr[] = "0123456789abcdefABCDEF"; + const char bad_teststr[] = "0123456789abcdefABCDEF1O"; + const unsigned char testdata[] = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, + 0xcd, 0xef, 0xAB, 0xCD, 0xEF }; + unsigned char data[11]; + char str[23]; + size_t i; + + plan_tests(10 + sizeof(str)); + + ok1(hex_str_size(sizeof(testdata)) == sizeof(teststr)); + /* This gives right result with or without nul included */ + ok1(hex_data_size(strlen(teststr)) == sizeof(testdata)); + ok1(hex_data_size(sizeof(teststr)) == sizeof(testdata)); + + ok1(hex_decode(teststr, strlen(teststr), data, sizeof(data))); + ok1(memcmp(data, testdata, sizeof(testdata)) == 0); + ok1(hex_encode(testdata, sizeof(testdata), str, sizeof(str))); + ok1(strcmp(str, "0123456789abcdefabcdef") == 0); + + /* Bad char */ + ok1(!hex_decode(bad_teststr, strlen(bad_teststr), data, sizeof(data))); + /* Bad hex string len */ + ok1(!hex_decode(teststr, strlen(teststr) - 1, data, sizeof(data))); + /* Bad buffer len */ + ok1(!hex_decode(teststr, strlen(teststr), data, sizeof(data) - 1)); + + /* Bad deststring size. */ + for (i = 1; i <= sizeof(str); i++) + ok1(!hex_encode(testdata, sizeof(testdata), str, sizeof(str)-i)); + + /* This exits depending on whether all tests passed */ + return exit_status(); +}