network

low level network subjects
Log | Files | Refs | Submodules | README | git clone https://git.ne02ptzero.me/git/network

commit 77782cf32d0b6e077c9d2c4f7cb487f49a630dd2
parent c4613ce62e8309e7a3864918fa8585b46b5d0af1
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Sun, 26 Mar 2017 22:46:43 +0200

Fix(Tests): Fix an allocation bug for test_all, and a printf bug on print package

Diffstat:
Msrc/client_test.c | 5++++-
Msrc/package.c | 14+++++++-------
2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/src/client_test.c b/src/client_test.c @@ -199,6 +199,7 @@ TEST(pkg_req_get_pkg_2_read) { TEST_ASSERT(sockfd, "Server is not responding"); + ret = malloc(2048); asprintf(&req, "SELECT * FROM pkgs WHERE id = 1"); mpm_database_exec(db, req, pkg_cb, &pkgs, &req); pkg = pkgs->member; @@ -208,9 +209,10 @@ TEST(pkg_req_get_pkg_2_read) { pkg->arch_size, pkg->state, pkg->name, pkg->category, pkg->version, pkg->description, pkg->archive, pkg->arch_hash, pkg->dependencies_arr_size, pkg->dependencies_arr); - TEST_ASSERT(ret != 0, "Server returned nothing"); + printf("\n%zu\n", size); TEST_ASSERT_FMT(memcmp(ptr, ret, size) == 0, "Expected package is wrong %s", print_package(ptr, ret, size, r_n)); + free(ret); return TEST_SUCCESS; } @@ -228,6 +230,7 @@ TEST(pkg_req_get_pkg_test_all) { ret = pkg_build_req_get_pkg(&size, pkg->id, PKG_STABLE, "", "", ""); TEST_ASSERT(write(sockfd, ret, size), "Cannot send package to the server"); free(ret); + ret = malloc(2048); READ_TIMEOUT(sockfd, ret, 2048, 1, r_n); ptr = pkg_build_resp_pkg(&size, pkg->id, pkg->sbu, pkg->inst_size, pkg->arch_size, pkg->state, pkg->name, pkg->category, pkg->version, pkg->description, diff --git a/src/package.c b/src/package.c @@ -237,20 +237,20 @@ char *print_package(void *exp, void *ret, size_t exp_size, size_t ret_size) { char *s_exp = exp, *s_ret = ret; size_t max_size = (ret_size > exp_size ? ret_size : exp_size); - res = strdup("\n> "); + 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%02X ", res, (unsigned char)s_exp[i]); + if ((i + 1) % 10 == 0 && i != 0) asprintf(&res, "%s\n ",res); } - 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]); + asprintf(&res, "%s\033[0;31m%02x\033[0;37m ", res, (unsigned char)s_ret[i]); else - asprintf(&res, "%s%02x ", res, s_ret[i]); - if (i % 10 == 0 && i != 0) + asprintf(&res, "%s%02X ", res, (unsigned char)s_ret[i]); + if ((i + 1) % 10 == 0 && i != 0) asprintf(&res, "%s\n ",res); } asprintf(&res, "%s\n ",res);