protocol

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

commit cf965a5773eba57b18bc81797712fa1c0c1ab2ba
parent 695f829d27167cd6bfaaa586a9daa43a1033cbf9
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Mon, 20 Mar 2017 19:10:46 +0100

Fix(Headers): Make struct compliant with libmpm

Diffstat:
Minc/client_test.h | 1+
Minc/package.h | 22+++++++++++-----------
Msrc/client_test.c | 39+++++++++++++++++++++++++++++++++++----
Msrc/main.c | 2+-
Msrc/package.c | 48++++++++++++++++++++++++------------------------
Msrc/read_package.c | 38+++++++++++++++++++-------------------
Msrc/write_package.c | 6+++---
7 files changed, 94 insertions(+), 62 deletions(-)

diff --git a/inc/client_test.h b/inc/client_test.h @@ -10,6 +10,7 @@ # include <strings.h> # include <netdb.h> # include "package.h" +# include "../lib/inc/package.h" # define READ_TIMEOUT(sockfd, ret, size, sec, r_n) fd_set set; \ struct timeval timeout; \ diff --git a/inc/package.h b/inc/package.h @@ -1,5 +1,5 @@ -#ifndef PACKAGE_H -# define PACKAGE_H +#ifndef PROT_PACKAGE_H +# define PROT_PACKAGE_H # include <morphux.h> # include "protocol_test.h" @@ -20,17 +20,17 @@ char *print_package(void *exp, void *ret, size_t exp_size, size_t ret_size); /* Header */ -typedef struct package_s { +typedef struct prot_package_s { u8_t type; u16_t size; u8_t next_pkg_len; char *next_pkg; u8_t number; mlist_t *payload; -} SF_PACKED package_t; +} SF_PACKED prot_package_t; /* Type field of the header */ -typedef enum package_type_e { +typedef enum prot_package_type_e { PKG_TYPE_AUTH = 0x1, PKG_TYPE_AUTH_ACK = 0x2, PKG_TYPE_ERROR = 0x3, @@ -44,7 +44,7 @@ typedef enum package_type_e { PKG_TYPE_RESP_NEWS = 0x22, PKG_TYPE_RESP_CAT = 0x23, PKG_TYPE_END = 0x24 -} package_type_t; +} prot_package_type_t; /* Payloads */ @@ -92,12 +92,12 @@ typedef struct req_get_pkg_s { pkg->version_len) /* Stability of packages */ -typedef enum package_state_e { +typedef enum prot_package_state_e { PKG_STABLE = 1, PKG_UNSTABLE, PKG_DEV, PKG_END -} package_state_t; +} prot_package_state_t; typedef struct req_get_file_s { u64_t id; @@ -194,8 +194,8 @@ typedef struct resp_cat_s { # define SIZEOF_RESP_CAT(c) (sizeof(c->id) + sizeof(c->parent_id) + \ sizeof(c->name_len) + c->name_len) -void *write_package(package_t *pkg, size_t *size); -package_t *read_pkg(void *data); +void *write_package(prot_package_t *pkg, size_t *size); +prot_package_t *read_pkg(void *data); void *pkg_build_auth(size_t *size, int major_version, int minor_version); void *pkg_build_auth_ack(size_t *size, int major_version, int minor_version); void *pkg_build_error(size_t *size, error_type_t type, const char *error); @@ -216,4 +216,4 @@ void *pkg_build_resp_news(size_t *size, u64_t id, u64_t parent_id, const char *a const char *author_mail, const char *text); void *pkg_build_resp_cat(size_t *size, u64_t id, u64_t parent_id, const char *name); -#endif /* PACKAGE_H */ +#endif /* PROT_PACKAGE_H */ diff --git a/src/client_test.c b/src/client_test.c @@ -24,10 +24,10 @@ TEST(connect_1) { } TEST(connect_db) { - u8_t ret; + u8_t ret = 0; db = mpm_database_open(&ret, g_db_path); - TEST_ASSERT(ret != 0, "Cannot open the database"); + TEST_ASSERT(ret == 0, "Cannot open the database"); return TEST_SUCCESS; } @@ -44,7 +44,7 @@ TEST(pkg_auth_1_write) { TEST(pkg_auth_1_read) { void *ret, *expect; - package_t *pkg; + prot_package_t *pkg; auth_t *auth; size_t r_n = 0, size; @@ -100,7 +100,7 @@ TEST(pkg_req_get_pkg_1_write) { TEST(pkg_req_get_pkg_1_read) { void *ret, *expect; - package_t *pkg; + prot_package_t *pkg; error_pkg_t *err; size_t r_n = 0, size; @@ -122,6 +122,35 @@ TEST(pkg_req_get_pkg_1_read) { return TEST_SUCCESS; } +TEST(pkg_req_get_pkg_2_write) { + void *ret; + size_t size; + + TEST_ASSERT(sockfd, "Server is not responding"); + ret = pkg_build_req_get_pkg(&size, 1, PKG_STABLE, "", "", ""); + TEST_ASSERT(write(sockfd, ret, size), "Cannot send package to the server"); + return TEST_SUCCESS; +} + + +TEST(pkg_req_get_pkg_2_read) { + mlist_t *pkgs = NULL; + package_t *pkg; + void *ret; + size_t r_n = 0, size; + + TEST_ASSERT(sockfd, "Server is not responding"); + mpm_get_package_by_id(db, 1, &pkgs); + pkg = pkgs->member; + + ret = malloc(2048); + READ_TIMEOUT(sockfd, ret, 2048, 1, r_n); + (void)pkg; + (void)size; + (void)r_n; + return TEST_SUCCESS; +} + TEST(cleanup_co) { TEST_ASSERT(sockfd, "Server is not responding"); TEST_ASSERT(close(sockfd) != -1, "Cannot close socket"); @@ -144,6 +173,8 @@ void begin_client_test(void) { 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("get_pkg", pkg_req_get_pkg_2_write); + reg_test("get_pkg", pkg_req_get_pkg_2_read); reg_test("clean", cleanup_co); reg_test("clean", cleanup_db); test_all(); diff --git a/src/main.c b/src/main.c @@ -37,7 +37,7 @@ int main(int ac, char **av) { read_opt(ac, av, opts, &list); if (g_port == 0 || g_ip == NULL || g_db_path == NULL) - m_panic("Missing arguments."); + opt_help(opts, 1); list_for_each(list, tmp, ptr) { if (strcmp(ptr, CLIENT_CMD) == 0) begin_client_test(); diff --git a/src/package.c b/src/package.c @@ -2,12 +2,12 @@ void *pkg_build_auth(size_t *size, int major_version, int minor_version) { auth_t *auth; - package_t *pkg; + prot_package_t *pkg; auth = malloc(sizeof(auth_t)); auth->mpm_major_version = major_version; auth->mpm_minor_version = minor_version; - pkg = malloc(sizeof(package_t)); + pkg = malloc(sizeof(prot_package_t)); pkg->type = PKG_TYPE_AUTH; pkg->payload = NULL; list_add(pkg->payload, auth, sizeof(auth_t)); @@ -16,13 +16,13 @@ void *pkg_build_auth(size_t *size, int major_version, int minor_version) { void *pkg_build_auth_ack(size_t *size, int major_version, int minor_version) { auth_t *auth; - package_t *pkg; + prot_package_t *pkg; void *ret; auth = malloc(sizeof(auth_t)); auth->mpm_major_version = major_version; auth->mpm_minor_version = minor_version; - pkg = malloc(sizeof(package_t)); + pkg = malloc(sizeof(prot_package_t)); pkg->type = PKG_TYPE_AUTH_ACK; pkg->payload = NULL; list_add(pkg->payload, auth, sizeof(auth_t)); @@ -32,14 +32,14 @@ void *pkg_build_auth_ack(size_t *size, int major_version, int minor_version) { void *pkg_build_error(size_t *size, error_type_t type, const char *error) { error_pkg_t *err; - package_t *pkg; + prot_package_t *pkg; err = malloc(sizeof(error_pkg_t)); err->error_type = type; err->error_len = strlen(error); err->err = strdup(error); - pkg = malloc(sizeof(package_t)); + pkg = malloc(sizeof(prot_package_t)); pkg->type = PKG_TYPE_ERROR; pkg->payload = NULL; list_add(pkg->payload, err, sizeof(*err)); @@ -49,7 +49,7 @@ void *pkg_build_error(size_t *size, error_type_t type, const char *error) { void *pkg_build_req_get_pkg(size_t *size, u64_t id, u8_t state, const char *name, const char *category, const char *version) { req_get_pkg_t *req; - package_t *pkg = NULL; + prot_package_t *pkg = NULL; req = malloc(sizeof(req_get_pkg_t)); req->id = id; @@ -61,7 +61,7 @@ void *pkg_build_req_get_pkg(size_t *size, u64_t id, u8_t state, const char *name req->category = strdup(category); req->version = strdup(version); - pkg = malloc(sizeof(package_t)); + pkg = malloc(sizeof(prot_package_t)); pkg->type = PKG_TYPE_REQ_GET_PKG; pkg->payload = NULL; list_add(pkg->payload, req, sizeof(req_get_pkg_t)); @@ -72,14 +72,14 @@ void *pkg_build_req_get_pkg(size_t *size, u64_t id, u8_t state, const char *name void *pkg_build_req_get_file(size_t *size, u64_t id, const char *path) { req_get_file_t *file; - package_t *pkg; + prot_package_t *pkg; file = malloc(sizeof(req_get_file_t)); file->id = id; file->path_len = strlen(path); file->path = strdup(path); - pkg = malloc(sizeof(package_t)); + pkg = malloc(sizeof(prot_package_t)); pkg->type = PKG_TYPE_REQ_GET_FILE; pkg->payload = NULL; list_add(pkg->payload, file, sizeof(*file)); @@ -89,7 +89,7 @@ void *pkg_build_req_get_file(size_t *size, u64_t id, const char *path) { void *pkg_build_req_get_news(size_t *size, time_t last_request, u16_t ids_size, u64_t *ids) { req_get_news_t *news; - package_t *pkg; + prot_package_t *pkg; news = malloc(sizeof(req_get_news_t)); news->last_request = last_request; @@ -97,7 +97,7 @@ void *pkg_build_req_get_news(size_t *size, time_t last_request, u16_t ids_size, news->pkgs_ids = malloc(sizeof(*ids) * ids_size); memcpy(news->pkgs_ids, ids, sizeof(*ids) * ids_size); - pkg = malloc(sizeof(package_t)); + pkg = malloc(sizeof(prot_package_t)); pkg->type = PKG_TYPE_REQ_GET_NEWS; pkg->payload = NULL; list_add(pkg->payload, news, sizeof(*news)); @@ -106,14 +106,14 @@ void *pkg_build_req_get_news(size_t *size, time_t last_request, u16_t ids_size, void *pkg_build_req_get_cat(size_t *size, u16_t len, u64_t *a_cat) { req_get_cat_t *cat; - package_t *pkg; + prot_package_t *pkg; cat = malloc(sizeof(req_get_cat_t)); cat->cat_len = len; cat->categories = malloc(sizeof(*a_cat) * len); memcpy(cat->categories, a_cat, sizeof(*a_cat) * len); - pkg = malloc(sizeof(package_t)); + pkg = malloc(sizeof(prot_package_t)); pkg->type = PKG_TYPE_REQ_CAT; pkg->payload = NULL; list_add(pkg->payload, cat, sizeof(*cat)); @@ -122,14 +122,14 @@ void *pkg_build_req_get_cat(size_t *size, u16_t len, u64_t *a_cat) { void *pkg_build_req_get_upd(size_t *size, u64_t len, u64_t *a_pkgs) { req_get_upd_t *upd; - package_t *pkg; + prot_package_t *pkg; upd = malloc(sizeof(req_get_upd_t)); upd->pkg_len = len; upd->packages = malloc(sizeof(*a_pkgs) * len); memcpy(upd->packages, a_pkgs, sizeof(*a_pkgs) * len); - pkg = malloc(sizeof(package_t)); + pkg = malloc(sizeof(prot_package_t)); pkg->type = PKG_TYPE_REQ_UPD; pkg->payload = NULL; list_add(pkg->payload, upd, sizeof(*upd)); @@ -141,7 +141,7 @@ void *pkg_build_resp_pkg(size_t *size, u64_t id, float comp_time, const char *category, const char *version, const char *archive, const char *checksum, u16_t dep_size, u64_t *dependencies) { resp_pkg_t *pkg; - package_t *ptr; + prot_package_t *ptr; pkg = malloc(sizeof(resp_pkg_t)); pkg->id = id; @@ -163,7 +163,7 @@ void *pkg_build_resp_pkg(size_t *size, u64_t id, float comp_time, pkg->dependencies = malloc(sizeof(*dependencies) * dep_size); memcpy(pkg->dependencies, dependencies, sizeof(*dependencies) * dep_size); - ptr = malloc(sizeof(package_t)); + ptr = malloc(sizeof(prot_package_t)); ptr->type = PKG_TYPE_RESP_PKG; ptr->payload = NULL; list_add(ptr->payload, pkg, sizeof(*pkg)); @@ -173,7 +173,7 @@ void *pkg_build_resp_pkg(size_t *size, u64_t id, float comp_time, void *pkg_build_resp_file(size_t *size, u64_t id, u8_t type, u64_t parent_id, const char *path) { resp_file_t *file; - package_t *pkg; + prot_package_t *pkg; file = malloc(sizeof(resp_file_t)); file->id = id; @@ -182,7 +182,7 @@ void *pkg_build_resp_file(size_t *size, u64_t id, u8_t type, u64_t parent_id, file->path_len = strlen(path); file->path = strdup(path); - pkg = malloc(sizeof(package_t)); + pkg = malloc(sizeof(prot_package_t)); pkg->type = PKG_TYPE_RESP_FILE; pkg->payload = NULL; list_add(pkg->payload, file, sizeof(*file)); @@ -192,7 +192,7 @@ void *pkg_build_resp_file(size_t *size, u64_t id, u8_t type, u64_t parent_id, void *pkg_build_resp_news(size_t *size, u64_t id, u64_t parent_id, const char *author, const char *author_mail, const char *text) { resp_news_t *news; - package_t *pkg; + prot_package_t *pkg; news = malloc(sizeof(resp_news_t)); news->id = id; @@ -205,7 +205,7 @@ void *pkg_build_resp_news(size_t *size, u64_t id, u64_t parent_id, const char *a news->author_mail = strdup(author_mail); news->text = strdup(text); - pkg = malloc(sizeof(package_t)); + pkg = malloc(sizeof(prot_package_t)); pkg->type = PKG_TYPE_RESP_FILE; pkg->payload = NULL; list_add(pkg->payload, news, sizeof(*news)); @@ -214,7 +214,7 @@ void *pkg_build_resp_news(size_t *size, u64_t id, u64_t parent_id, const char *a void *pkg_build_resp_cat(size_t *size, u64_t id, u64_t parent_id, const char *name) { resp_cat_t *cat; - package_t *pkg; + prot_package_t *pkg; cat = malloc(sizeof(resp_cat_t)); cat->id = id; @@ -222,7 +222,7 @@ void *pkg_build_resp_cat(size_t *size, u64_t id, u64_t parent_id, const char *na cat->name_len = strlen(name); cat->name = strdup(name); - pkg = malloc(sizeof(package_t)); + pkg = malloc(sizeof(prot_package_t)); pkg->type = PKG_TYPE_RESP_CAT; pkg->payload = NULL; list_add(pkg->payload, cat, sizeof(*cat)); diff --git a/src/read_package.c b/src/read_package.c @@ -1,6 +1,6 @@ #include "package.h" -static size_t read_header(package_t *pkg, void *data) { +static size_t read_header(prot_package_t *pkg, void *data) { size_t count = 0; read_member(pkg->type); @@ -17,13 +17,13 @@ static size_t read_header(package_t *pkg, void *data) { return count; } -static bool check_type(package_t *pkg) { +static bool check_type(prot_package_t *pkg) { if (pkg->type >= PKG_TYPE_END) return false; return true; } -static size_t read_payload_auth(package_t *pkg, void *data) { +static size_t read_payload_auth(prot_package_t *pkg, void *data) { size_t count = 0; auth_t *auth; @@ -37,7 +37,7 @@ static size_t read_payload_auth(package_t *pkg, void *data) { return count; } -static size_t read_payload_auth_ack(package_t *pkg, void *data) { +static size_t read_payload_auth_ack(prot_package_t *pkg, void *data) { size_t count = 0; auth_ack_t *auth; @@ -51,7 +51,7 @@ static size_t read_payload_auth_ack(package_t *pkg, void *data) { return count; } -static size_t read_payload_error(package_t *pkg, void *data) { +static size_t read_payload_error(prot_package_t *pkg, void *data) { size_t count = 0; error_pkg_t *err; @@ -73,7 +73,7 @@ static size_t read_payload_error(package_t *pkg, void *data) { return count; } -static size_t read_payload_get_package(package_t *pkg, void *data) { +static size_t read_payload_get_package(prot_package_t *pkg, void *data) { size_t count = 0; req_get_pkg_t *req; @@ -114,7 +114,7 @@ static size_t read_payload_get_package(package_t *pkg, void *data) { return count; } -static size_t read_payload_get_file(package_t *pkg, void *data) { +static size_t read_payload_get_file(prot_package_t *pkg, void *data) { size_t count = 0; req_get_file_t *req; @@ -136,7 +136,7 @@ static size_t read_payload_get_file(package_t *pkg, void *data) { return count; } -static size_t read_payload_get_news(package_t *pkg, void *data) { +static size_t read_payload_get_news(prot_package_t *pkg, void *data) { size_t count = 0; req_get_news_t *req; @@ -159,7 +159,7 @@ static size_t read_payload_get_news(package_t *pkg, void *data) { return count; } -static size_t read_payload_get_cat(package_t *pkg, void *data) { +static size_t read_payload_get_cat(prot_package_t *pkg, void *data) { size_t count = 0; req_get_cat_t *req; @@ -181,7 +181,7 @@ static size_t read_payload_get_cat(package_t *pkg, void *data) { return count; } -static size_t read_payload_get_upd(package_t *pkg, void *data) { +static size_t read_payload_get_upd(prot_package_t *pkg, void *data) { size_t count = 0; req_get_upd_t *req; @@ -203,7 +203,7 @@ static size_t read_payload_get_upd(package_t *pkg, void *data) { return count; } -static size_t read_payload_resp_pkg(package_t *pkg, void *data) { +static size_t read_payload_resp_pkg(prot_package_t *pkg, void *data) { size_t count = 0; resp_pkg_t *resp; @@ -275,7 +275,7 @@ static size_t read_payload_resp_pkg(package_t *pkg, void *data) { return count; } -static size_t read_payload_resp_file(package_t *pkg, void *data) { +static size_t read_payload_resp_file(prot_package_t *pkg, void *data) { size_t count = 0; resp_file_t *resp; @@ -299,7 +299,7 @@ static size_t read_payload_resp_file(package_t *pkg, void *data) { return count; } -static size_t read_payload_resp_news(package_t *pkg, void *data) { +static size_t read_payload_resp_news(prot_package_t *pkg, void *data) { size_t count = 0; resp_news_t *resp; @@ -340,7 +340,7 @@ static size_t read_payload_resp_news(package_t *pkg, void *data) { return count; } -static size_t read_payload_resp_cat(package_t *pkg, void *data) { +static size_t read_payload_resp_cat(prot_package_t *pkg, void *data) { size_t count = 0; resp_cat_t *resp; @@ -363,7 +363,7 @@ static size_t read_payload_resp_cat(package_t *pkg, void *data) { return count; } -typedef size_t (*payload_callback)(package_t *, void *); +typedef size_t (*payload_callback)(prot_package_t *, void *); typedef struct callback_s { u8_t index; payload_callback fn; @@ -384,7 +384,7 @@ static const callback_t arr[] = { {PKG_TYPE_RESP_CAT, &read_payload_resp_cat} }; -static size_t read_payload(package_t *pkg, void *data) { +static size_t read_payload(prot_package_t *pkg, void *data) { size_t index; for (index = 0; index < sizeof(arr) / sizeof(arr[0]); index++) @@ -395,12 +395,12 @@ static size_t read_payload(package_t *pkg, void *data) { return 0; } -package_t *read_pkg(void *data) { - package_t *pkg; +prot_package_t *read_pkg(void *data) { + prot_package_t *pkg; size_t count; u8_t number; - pkg = malloc(sizeof(package_t)); + pkg = malloc(sizeof(prot_package_t)); assert(pkg != NULL); pkg->payload = NULL; count = read_header(pkg, data); diff --git a/src/write_package.c b/src/write_package.c @@ -1,6 +1,6 @@ #include "package.h" -static void *write_header(package_t *pkg, size_t *count) { +static void *write_header(prot_package_t *pkg, size_t *count) { void *header; *count = 0; @@ -266,7 +266,7 @@ static const callback_t arr[] = { {PKG_TYPE_RESP_CAT, &write_payload_resp_cat} }; -void *write_payload(package_t *pkg, size_t *count) { +void *write_payload(prot_package_t *pkg, size_t *count) { mlist_t *tmp; void *ptr, *payl_tmp, *ret = NULL; size_t size, index; @@ -289,7 +289,7 @@ void *write_payload(package_t *pkg, size_t *count) { return ret; } -void *write_package(package_t *pkg, size_t *size) { +void *write_package(prot_package_t *pkg, size_t *size) { void *header, *payload, *ret; size_t header_size = 0, payload_size = 0;