network

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

commit 24ef917b56d5b8a5bd65891a869dfaaaaed1e459
parent 648c4bd55a4a0800627757e11541064514d950f3
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 20 Mar 2017 15:31:28 +0100

Add(Tests): Add pkg tests

Diffstat:
Minc/client_test.h | 12++++++++++++
Msrc/client_test.c | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++------------
2 files changed, 66 insertions(+), 12 deletions(-)

diff --git a/inc/client_test.h b/inc/client_test.h @@ -10,6 +10,18 @@ # include <netdb.h> # include "package.h" +# define READ_TIMEOUT(sockfd, ret, size, sec, r_n) fd_set set; \ + struct timeval timeout; \ + int rv; \ + FD_ZERO(&set); \ + FD_SET(sockfd, &set); \ + timeout.tv_sec = sec; \ + timeout.tv_usec = 0; \ + rv = select(sockfd + 1, &set, NULL, NULL, &timeout); \ + TEST_ASSERT(rv > 0, "Timeout on read"); \ + r_n = read(sockfd, ret, size); + + void begin_client_test(void); int g_port; diff --git a/src/client_test.c b/src/client_test.c @@ -37,22 +37,13 @@ TEST(pkg_auth_1_read) { void *ret, *expect; package_t *pkg; auth_t *auth; - fd_set set; - size_t r_n = 0, size, rv; - struct timeval timeout; + size_t r_n = 0, size; TEST_ASSERT(sockfd, "Server is not responding"); expect = pkg_build_auth_ack(&size, 1, 0); ret = malloc(2048); - FD_ZERO(&set); - FD_SET(sockfd, &set); - timeout.tv_sec = 1; - timeout.tv_usec = 0; - rv = select(sockfd + 1, &set, NULL, NULL, &timeout); - TEST_ASSERT(rv > 0, "Timeout on read"); - r_n = read(sockfd, ret, 2048); - + READ_TIMEOUT(sockfd, ret, 2048, 1, r_n); TEST_ASSERT(r_n == size, "Package returned size is wrong"); TEST_ASSERT(memcmp(ret, expect, size) == 0, "Expected package is wrong"); pkg = read_pkg(ret); @@ -66,16 +57,62 @@ TEST(pkg_auth_1_read) { return TEST_SUCCESS; } +TEST(pkg_auth_2_write) { + size_t size; + void *ret; + + TEST_ASSERT(sockfd, "Server is not responding"); + ret = pkg_build_auth(&size, 1, 1); + TEST_ASSERT(write(sockfd, ret, size) != 0, + "Cannot send package to the server"); + return TEST_SUCCESS; +} + +TEST(pkg_auth_3_write) { + size_t size; + void *ret; + + TEST_ASSERT(sockfd, "Server is not responding"); + ret = pkg_build_auth(&size, -1, -1); + TEST_ASSERT(write(sockfd, ret, size) != 0, + "Cannot send package to the server"); + return TEST_SUCCESS; +} + TEST(pkg_req_get_pkg_1_write) { void *ret; size_t size; TEST_ASSERT(sockfd, "Server is not responding"); - ret = pkg_build_req_get_pkg(&size, 1, PKG_STABLE, "Test", "pkg", "v45.03"); + ret = pkg_build_req_get_pkg(&size, 0, PKG_STABLE, "", "", ""); TEST_ASSERT(write(sockfd, ret, size), "Cannot send package to the server"); return TEST_SUCCESS; } +TEST(pkg_req_get_pkg_1_read) { + void *ret, *expect; + package_t *pkg; + error_pkg_t *err; + size_t r_n = 0, size; + + TEST_ASSERT(sockfd, "Server is not responding"); + expect = pkg_build_error(&size, ERR_RES_NOT_FOUND, "Package not found"); + ret = malloc(2048); + + READ_TIMEOUT(sockfd, ret, 2048, 1, r_n); + TEST_ASSERT(r_n == size, "Package returned size is wrong"); + TEST_ASSERT(memcmp(ret, expect, size) == 0, "Expected package is wrong"); + pkg = read_pkg(ret); + TEST_ASSERT(pkg, "Can't read package"); + TEST_ASSERT(pkg->type == PKG_TYPE_ERROR, "Package type is wrong"); + + err = pkg->payload->member; + TEST_ASSERT(err->error_type == ERR_RES_NOT_FOUND, "Error type is wrong"); + TEST_ASSERT(strcmp(err->err, "Package not found") == 0, "Error string is wrong"); + free(ret); + return TEST_SUCCESS; +} + TEST(cleanup) { TEST_ASSERT(sockfd, "Server is not responding"); TEST_ASSERT(close(sockfd) != -1, "Cannot close socket"); @@ -86,7 +123,12 @@ void begin_client_test(void) { reg_test("connect", connect_1); reg_test("auth", pkg_auth_1_write); reg_test("auth", pkg_auth_1_read); + reg_test("auth", pkg_auth_2_write); + reg_test("auth", pkg_auth_1_read); + reg_test("auth", pkg_auth_3_write); + reg_test("auth", pkg_auth_1_read); reg_test("get_pkg", pkg_req_get_pkg_1_write); + reg_test("get_pkg", pkg_req_get_pkg_1_read); reg_test("clean", cleanup); test_all(); test_free();