test(cli/handlers): add unit tests for CLI handlers
- Add unit tests for handlers - Update sources.mk to remove test_parse.c from test sources - Fix in handle_size to set packet_size instead of count
This commit is contained in:
parent
88b65cc62e
commit
f6af6473db
14 changed files with 173 additions and 30 deletions
|
|
@ -20,7 +20,6 @@ SRCS = $(SRC_DIR)/main.c \
|
|||
|
||||
TESTS_DIR = tests
|
||||
TESTS = $(TESTS_DIR)/test_main.c \
|
||||
$(TESTS_DIR)/cli/test_parse.c \
|
||||
$(TESTS_DIR)/cli/handlers/test_handle_count.c \
|
||||
$(TESTS_DIR)/cli/handlers/test_handle_flood.c \
|
||||
$(TESTS_DIR)/cli/handlers/test_handle_help.c \
|
||||
|
|
|
|||
|
|
@ -16,6 +16,6 @@ cli_handle_size(const char *arg, t_ping_config *config)
|
|||
return CLI_ERROR;
|
||||
}
|
||||
|
||||
config->count = size;
|
||||
config->packet_size = size;
|
||||
return CLI_SUCCESS;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,23 @@
|
|||
#include <criterion/criterion.h>
|
||||
#include "cli.h"
|
||||
|
||||
Test(dummy_test_handle_count, always_pass)
|
||||
/* Test 1: basic test */
|
||||
Test(cli_handle_count, valid_number)
|
||||
{
|
||||
cr_assert(1, "");
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "42";
|
||||
int ret = cli_handle_count(arg, &config);
|
||||
|
||||
cr_assert_eq(ret, CLI_SUCCESS);
|
||||
cr_assert_eq(config.count, 42);
|
||||
}
|
||||
|
||||
/* Test 2: invalid number */
|
||||
Test(cli_handle_count, invalid_number)
|
||||
{
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "-42";
|
||||
int ret = cli_handle_count(arg, &config);
|
||||
|
||||
cr_assert_eq(ret, CLI_ERROR);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,14 @@
|
|||
#include "cli.h"
|
||||
#include "ft_ping_flags.h"
|
||||
#include <criterion/criterion.h>
|
||||
|
||||
Test(dummy_test_handle_flood, always_pass)
|
||||
/* Test 1: test flag*/
|
||||
Test(handle_flood, flag_test)
|
||||
{
|
||||
cr_assert(1, "");
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "";
|
||||
const int ret = cli_handle_flood(arg, &config);
|
||||
|
||||
cr_assert(HAS_FLAG(config.flags, FLAG_FLOOD));
|
||||
cr_assert_eq(ret, CLI_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,12 @@
|
|||
#include <criterion/criterion.h>
|
||||
#include "cli.h"
|
||||
|
||||
Test(dummy_test_handle_help, always_pass)
|
||||
/* Test 1: exit code test */
|
||||
Test(handle_help, exit_code)
|
||||
{
|
||||
cr_assert(1, "");
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "";
|
||||
int ret = cli_handle_help(arg, &config);
|
||||
|
||||
cr_assert_eq(ret, CLI_EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,23 @@
|
|||
#include <criterion/criterion.h>
|
||||
#include "cli.h"
|
||||
|
||||
Test(dummy_test_handle_interval, always_pass)
|
||||
/* Test 1: basic test */
|
||||
Test(cli_handle_interval, valid_number)
|
||||
{
|
||||
cr_assert(1, "");
|
||||
t_ping_config config = {0};
|
||||
const char *arg = ".42";
|
||||
int ret = cli_handle_interval(arg, &config);
|
||||
|
||||
cr_assert_eq(ret, CLI_SUCCESS);
|
||||
cr_assert_float_eq(config.interval, .42, .001);
|
||||
}
|
||||
|
||||
/* Test 2: invalid number */
|
||||
Test(cli_handle_interval, invalid_number)
|
||||
{
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "-42";
|
||||
int ret = cli_handle_interval(arg, &config);
|
||||
|
||||
cr_assert_eq(ret, CLI_ERROR);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,14 @@
|
|||
#include "cli.h"
|
||||
#include "ft_ping_flags.h"
|
||||
#include <criterion/criterion.h>
|
||||
|
||||
Test(dummy_test_handle_quiet, always_pass)
|
||||
/* Test 1: test flag*/
|
||||
Test(handle_quiet, flag_test)
|
||||
{
|
||||
cr_assert(1, "");
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "";
|
||||
const int ret = cli_handle_quiet(arg, &config);
|
||||
|
||||
cr_assert(HAS_FLAG(config.flags, FLAG_QUIET));
|
||||
cr_assert_eq(ret, CLI_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,33 @@
|
|||
#include <criterion/criterion.h>
|
||||
#include "cli.h"
|
||||
|
||||
Test(dummy_test_handle_size, always_pass)
|
||||
/* Test 1: basic test */
|
||||
Test(cli_handle_size, valid_number)
|
||||
{
|
||||
cr_assert(1, "");
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "65527";
|
||||
int ret = cli_handle_size(arg, &config);
|
||||
|
||||
cr_assert_eq(ret, CLI_SUCCESS);
|
||||
cr_assert_eq(config.packet_size, 65527);
|
||||
}
|
||||
|
||||
/* Test 2: invalid number */
|
||||
Test(cli_handle_size, invalid_number)
|
||||
{
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "-42";
|
||||
int ret = cli_handle_size(arg, &config);
|
||||
|
||||
cr_assert_eq(ret, CLI_ERROR);
|
||||
}
|
||||
|
||||
/* Test 3: max test */
|
||||
Test(cli_handle_size, limit_test)
|
||||
{
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "65528";
|
||||
int ret = cli_handle_size(arg, &config);
|
||||
|
||||
cr_assert_eq(ret, CLI_ERROR);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,23 @@
|
|||
#include <criterion/criterion.h>
|
||||
#include "cli.h"
|
||||
|
||||
Test(dummy_test_handle_timeout, always_pass)
|
||||
/* Test 1: basic test */
|
||||
Test(cli_handle_timeout, valid_number)
|
||||
{
|
||||
cr_assert(1, "");
|
||||
t_ping_config config = {0};
|
||||
const char *arg = ".42";
|
||||
int ret = cli_handle_timeout(arg, &config);
|
||||
|
||||
cr_assert_eq(ret, CLI_SUCCESS);
|
||||
cr_assert_float_eq(config.timeout, .42, .001);
|
||||
}
|
||||
|
||||
/* Test 2: invalid number */
|
||||
Test(cli_handle_timeout, invalid_number)
|
||||
{
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "-42";
|
||||
int ret = cli_handle_timeout(arg, &config);
|
||||
|
||||
cr_assert_eq(ret, CLI_ERROR);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,33 @@
|
|||
#include <criterion/criterion.h>
|
||||
#include "cli.h"
|
||||
|
||||
Test(dummy_test_handle_ttl, always_pass)
|
||||
/* Test 1: basic test */
|
||||
Test(cli_handle_ttl, valid_number)
|
||||
{
|
||||
cr_assert(1, "");
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "255";
|
||||
int ret = cli_handle_ttl(arg, &config);
|
||||
|
||||
cr_assert_eq(ret, CLI_SUCCESS);
|
||||
cr_assert_eq(config.ttl, 255);
|
||||
}
|
||||
|
||||
/* Test 2: invalid number */
|
||||
Test(cli_handle_ttl, invalid_number)
|
||||
{
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "-1";
|
||||
int ret = cli_handle_ttl(arg, &config);
|
||||
|
||||
cr_assert_eq(ret, CLI_ERROR);
|
||||
}
|
||||
|
||||
/* Test 3: max test */
|
||||
Test(cli_handle_ttl, limit_test)
|
||||
{
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "256";
|
||||
int ret = cli_handle_ttl(arg, &config);
|
||||
|
||||
cr_assert_eq(ret, CLI_ERROR);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,14 @@
|
|||
#include "cli.h"
|
||||
#include "ft_ping_flags.h"
|
||||
#include <criterion/criterion.h>
|
||||
|
||||
Test(dummy_test_handle_verbose, always_pass)
|
||||
/* Test 1: test flag*/
|
||||
Test(handle_verbose, flag_test)
|
||||
{
|
||||
cr_assert(1, "");
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "";
|
||||
const int ret = cli_handle_verbose(arg, &config);
|
||||
|
||||
cr_assert(HAS_FLAG(config.flags, FLAG_VERBOSE));
|
||||
cr_assert_eq(ret, CLI_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,12 @@
|
|||
#include <criterion/criterion.h>
|
||||
#include "cli.h"
|
||||
|
||||
Test(dummy_test_handle_version, always_pass)
|
||||
/* Test 1: exit code test */
|
||||
Test(handle_version, exit_code)
|
||||
{
|
||||
cr_assert(1, "");
|
||||
t_ping_config config = {0};
|
||||
const char *arg = "";
|
||||
int ret = cli_handle_version(arg, &config);
|
||||
|
||||
cr_assert_eq(ret, CLI_EXIT_SUCCESS);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,17 @@
|
|||
#include "cli.h"
|
||||
#include <criterion/criterion.h>
|
||||
#include <stddef.h>
|
||||
|
||||
Test(dummy_test_handler_map, always_pass)
|
||||
/* Test 1: g_options is null terminated */
|
||||
Test(_test_handler_map, always_pass)
|
||||
{
|
||||
cr_assert(1, "");
|
||||
size_t i = 0;
|
||||
for (i = 0; NULL != g_options[i].description; ++i);
|
||||
|
||||
cr_assert(g_options[i].description == NULL);
|
||||
cr_assert(g_options[i].arg_type == 0);
|
||||
cr_assert(g_options[i].handler == NULL);
|
||||
cr_assert(g_options[i].has_arg == 0);
|
||||
cr_assert(g_options[i].long_opt == NULL);
|
||||
cr_assert(g_options[i].short_opt == 0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +0,0 @@
|
|||
#include <criterion/criterion.h>
|
||||
|
||||
Test(dummy_test_parse, always_pass)
|
||||
{
|
||||
cr_assert(1, "");
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue