network

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

commit b9f94a53dbf755fc32552d47c39f788126ada83c
parent 5e29d915aba8ddc9e9cfdf41b357679bb8407bea
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 15 Mar 2017 19:01:21 +0100

Work(Tests): Beginnings of tests:

Add args handling too, and a basic test auth_t, gonna rework the package constructions

Diffstat:
MMakefile | 2+-
Ainc/client_test.h | 16++++++++++++++++
Minc/package.h | 6++++--
Asrc/client_test.c | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/main.c | 37+++++++++++++++++++++++++++++--------
Msrc/read_package.c | 6+++---
Msrc/write_package.c | 2+-
7 files changed, 110 insertions(+), 15 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,6 +1,6 @@ NAME = protocol_tests CC = gcc -CFLAGS = -Wall -Werror -Wextra -I inc/ -I lib/inc/ -g -O3 +CFLAGS = -Wall -Werror -Wextra -Wno-unused-result -I inc/ -I lib/inc/ -g -O3 LFLAGS = -Llib/ -lmorphux SRCS = $(wildcard src/*.c) OBJS = $(SRCS:%.c=%.o) diff --git a/inc/client_test.h b/inc/client_test.h @@ -0,0 +1,16 @@ +#ifndef CLIENT_TEST_H +# define CLIENT_TEST_H +# include <morphux.h> +# include <stdio.h> +# include <sys/types.h> +# include <sys/socket.h> +# include <netinet/in.h> +# include <netdb.h> +# include "package.h" + +void begin_client_test(void); + +int g_port; +char *g_ip; + +#endif /* CLIENT_TEST_H */ diff --git a/inc/package.h b/inc/package.h @@ -56,11 +56,11 @@ typedef struct auth_ack_s { u8_t mpm_minor_version; } SF_PACKED auth_ack_t; -typedef struct error_s { +typedef struct error_pkg_s { u8_t error_type; u16_t error_len; char *err; -} SF_PACKED error_t; +} SF_PACKED error_pkg_t; /* Error possible types */ typedef enum error_type_e { @@ -157,4 +157,6 @@ typedef struct resp_cat_s { char *name; } SF_PACKED resp_cat_t; +void *write_package(package_t *pkg, size_t *size); + #endif /* PACKAGE_H */ diff --git a/src/client_test.c b/src/client_test.c @@ -0,0 +1,56 @@ +#include "client_test.h" + +static int sockfd; + +TEST(connect_1) { + struct sockaddr_in serv_addr; + struct hostent *server; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + TEST_ASSERT(sockfd > 0, "Cannot open socket"); + server = gethostbyname(g_ip); + TEST_ASSERT(server != NULL, "Address doesn't exist"); + bzero((char *)&serv_addr, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + bcopy((char *)server->h_addr, + (char *)&serv_addr.sin_addr.s_addr, + server->h_length); + serv_addr.sin_port = htons(g_port); + if (connect(sockfd, &serv_addr, sizeof(serv_addr)) == -1) + sockfd = 0; + TEST_ASSERT(sockfd != 0, "Cannot connect to the server"); + return TEST_SUCCESS; +} + +TEST(pkg_auth_1) { + package_t *pkg; + size_t size; + auth_t *auth; + void *ret; + + TEST_ASSERT(sockfd, "Server is not responding"); + auth = malloc(sizeof(auth_t)); + auth->mpm_major_version = 1; + auth->mpm_minor_version = 1; + pkg = malloc(sizeof(pkg)); + pkg->type = PKG_TYPE_AUTH; + pkg->next_pkg_len = 0; + list_add(pkg->payload, auth, sizeof(auth_t)); + ret = write_package(pkg, &size); + TEST_ASSERT(write(sockfd, ret, size) != 0, + "Cannot send package to the server"); + return TEST_SUCCESS; +} + +TEST(cleanup) { + TEST_ASSERT(close(sockfd) != -1, "Cannot close socket"); + return TEST_SUCCESS; +} + +void begin_client_test(void) { + reg_test("connect", connect_1); + reg_test("auth", pkg_auth_1); + reg_test("clean", cleanup); + test_all(); + test_free(); +} diff --git a/src/main.c b/src/main.c @@ -1,16 +1,37 @@ #include "protocol_test.h" #include "package.h" +#include "client_test.h" +#define CLIENT_CMD "client" +#define SERV_CMD "server" + +static void callback_port(const char *p) { + g_port = atoi(p); +} + +static void callback_ip(const char *ip) { + g_ip = strdup(ip); +} + +static const mopts_t opts[] = { + {'p', "port", "Port", true, &callback_port}, + {'a', "ip", "IP Address", true, &callback_ip}, + ARGS_EOL +}; + +static void begin_server_test(void) { + return ; +} int main(int ac, char **av) { - if (ac == 1) - m_panic("Can't do without an argument\n"); + mlist_t *list = NULL, *tmp; + char *ptr; - if (strcmp(av[1], "client") == 0) { - /* Client */ - } else if (strcmp(av[1], "server") == 0) { - /* Server */ - } else { - m_panic("Unknown argument: %s\n", av[1]); + read_opt(ac, av, opts, &list); + list_for_each(list, tmp, ptr) { + if (strcmp(ptr, CLIENT_CMD) == 0) + begin_client_test(); + else if (strcmp(ptr, SERV_CMD) == 0) + begin_server_test(); } return 0; } diff --git a/src/read_package.c b/src/read_package.c @@ -53,9 +53,9 @@ static size_t read_payload_auth_ack(package_t *pkg, void *data) { static size_t read_payload_error(package_t *pkg, void *data) { size_t count = 0; - error_t *err; + error_pkg_t *err; - err = malloc(sizeof(error_t)); + err = malloc(sizeof(error_pkg_t)); assert(err != NULL); read_member(err->error_type); @@ -69,7 +69,7 @@ static size_t read_payload_error(package_t *pkg, void *data) { err->err = NULL; } - list_add(pkg->payload, err, sizeof(error_t)); + list_add(pkg->payload, err, sizeof(error_pkg_t)); return count; } diff --git a/src/write_package.c b/src/write_package.c @@ -46,7 +46,7 @@ static void *write_payload_auth_ack(void *pkg, size_t *count) { } static void *write_payload_error(void *pkg, size_t *count) { - error_t *err = pkg; + error_pkg_t *err = pkg; void *ret; ret = malloc(sizeof(err->error_type) + sizeof(err->error_len) + err->error_len);