protocol

protocol tests
Log | Files | Refs | Submodules | README | git clone https://git.ne02ptzero.me/git/protocol

commit 098e4db013b7df916bf3a077a191775f544f3bcd
parent 24ef917b56d5b8a5bd65891a869dfaaaaed1e459
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 20 Mar 2017 16:43:33 +0100

Add(Tests): Better logs for failed tests

Diffstat:
Minc/package.h | 2++
Msrc/client_test.c | 2+-
Msrc/package.c | 24++++++++++++++++++++++++
Msrc/write_package.c | 1+
4 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/inc/package.h b/inc/package.h @@ -17,6 +17,8 @@ count += len; \ } +char *print_package(void *exp, void *ret, size_t exp_size, size_t ret_size); + /* Header */ typedef struct package_s { u8_t type; diff --git a/src/client_test.c b/src/client_test.c @@ -44,7 +44,7 @@ TEST(pkg_auth_1_read) { ret = malloc(2048); READ_TIMEOUT(sockfd, ret, 2048, 1, r_n); - TEST_ASSERT(r_n == size, "Package returned size is wrong"); + TEST_ASSERT_FMT(r_n == size, "Package returned size is wrong (%zu - %zu) %s", r_n, size, print_package(expect, ret, size, r_n)); TEST_ASSERT(memcmp(ret, expect, size) == 0, "Expected package is wrong"); pkg = read_pkg(ret); TEST_ASSERT(pkg, "Can't read package"); diff --git a/src/package.c b/src/package.c @@ -228,3 +228,27 @@ void *pkg_build_resp_cat(size_t *size, u64_t id, u64_t parent_id, const char *na list_add(pkg->payload, cat, sizeof(*cat)); return write_package(pkg, size); } + +char *print_package(void *exp, void *ret, size_t exp_size, size_t ret_size) { + char *res = NULL; + char *s_exp = exp, *s_ret = ret; + size_t max_size = (ret_size > exp_size ? ret_size : exp_size); + + res = strdup("\n> "); + for (size_t i = 0; i < exp_size; i++) { + asprintf(&res, "%s%02x ", res, s_exp[i]); + if (i % 10 == 0 && i != 0) + asprintf(&res, "%s\n ",res); + } + asprintf(&res, "%s\n> ",res); + fflush(stdout); + for (size_t i = 0; i < ret_size; i++) { + if (i < max_size && s_ret[i] != s_exp[i]) + asprintf(&res, "%s\033[0;31m%02x\033[0;37m ", res, s_ret[i]); + else + asprintf(&res, "%s%02x ", res, s_ret[i]); + if (i % 10) + asprintf(&res, "%s\n ",res); + } + return res; +} diff --git a/src/write_package.c b/src/write_package.c @@ -301,6 +301,7 @@ void *write_package(package_t *pkg, size_t *size) { *size = header_size + payload_size; ret = malloc(*size); + memcpy(header + 1, size, sizeof(*size)); assert(ret != NULL); memcpy(ret, header, header_size); memcpy(ret + header_size, payload, payload_size);