Skip to content
Snippets Groups Projects
Commit f0bd741f authored by armatusmiles's avatar armatusmiles
Browse files

Refactor tests

parent 232fbcf7
No related branches found
No related tags found
No related merge requests found
[submodule "libdap-test"]
path = libdap-test
url = https://github.com/kelvinblockchain/libdap-test
...@@ -37,29 +37,26 @@ ...@@ -37,29 +37,26 @@
#define DAP_PROTOCOL_VERSION 21 #define DAP_PROTOCOL_VERSION 21
#if defined(__GNUC__) ||defined (__clang__) #if defined(__GNUC__) ||defined (__clang__)
#define DAP_ALIGN_PACKED __attribute__((aligned(1),packed)) #define DAP_ALIGN_PACKED __attribute__((aligned(1),packed))
#endif #endif
enum log_level{L_CRITICAL=5,L_ERROR=4, L_WARNING=3,L_NOTICE=2,L_INFO=1,L_DEBUG=0}; enum log_level{L_CRITICAL=5,L_ERROR=4, L_WARNING=3,L_NOTICE=2,L_INFO=1,L_DEBUG=0};
//extern enum log_level log_level;
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
int dap_common_init( const char * a_log_file ); int dap_common_init( const char * a_log_file );
void dap_common_deinit(); void dap_common_deinit(void);
void _log_it(const char * log_tag, enum log_level, const char * format,...); void _log_it(const char * log_tag, enum log_level, const char * format,...);
void _vlog_it(const char * log_tag, enum log_level, const char * format, va_list ap ); void _vlog_it(const char * log_tag, enum log_level, const char * format, va_list ap );
#define log_it(_log_level,...) _log_it(LOG_TAG,_log_level,##__VA_ARGS__) #define log_it(_log_level,...) _log_it(LOG_TAG,_log_level,##__VA_ARGS__)
#define vlog_it(a_log_level,a_format,a_ap) _vlog_it(LOG_TAG,a_log_level,a_format,a_ap) #define vlog_it(a_log_level,a_format,a_ap) _vlog_it(LOG_TAG,a_log_level,a_format,a_ap)
const char * log_error(); const char * log_error(void);
void set_log_level(enum log_level ll); void set_log_level(enum log_level ll);
#ifdef __GNUC__ #ifdef __GNUC__
...@@ -71,8 +68,8 @@ char *strndup(const char *s, size_t n); ...@@ -71,8 +68,8 @@ char *strndup(const char *s, size_t n);
#endif #endif
int time_to_rfc822(char * out, size_t out_size_max, time_t t); int time_to_rfc822(char * out, size_t out_size_max, time_t t);
int get_select_breaker(); int get_select_breaker(void);
int send_select_break(); int send_select_break(void);
char * exec_with_ret(const char * a_cmd); char * exec_with_ret(const char * a_cmd);
char * exec_with_ret_multistring(const char * a_cmd); char * exec_with_ret_multistring(const char * a_cmd);
char * random_string_create(size_t a_length); char * random_string_create(size_t a_length);
......
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
project(libdap-test) project(test)
add_subdirectory(libdap-test)
add_subdirectory(core) add_subdirectory(core)
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
project(core-test) project(core_test)
file(GLOB SRC *.h *.c) file(GLOB SRCS *.c)
file(GLOB HRDS *.c)
add_executable(${PROJECT_NAME} ${SRC}) add_executable(${PROJECT_NAME} ${SRCS} ${HRDS})
target_link_libraries(${PROJECT_NAME} dap_core) target_link_libraries(core_test dap_test dap_core)
add_test( add_test(
NAME core-test NAME core-test
COMMAND core-test COMMAND core_test
) )
...@@ -4,10 +4,11 @@ void test_put_int() { ...@@ -4,10 +4,11 @@ void test_put_int() {
const int INT_VAL = 10; const int INT_VAL = 10;
const char * EXPECTED_RESULT = "10"; const char * EXPECTED_RESULT = "10";
char * result_arr = itoa(INT_VAL); char * result_arr = itoa(INT_VAL);
assert(strcmp(result_arr, EXPECTED_RESULT) == 0 && "test_put_int failed"); dap_assert(strcmp(result_arr, EXPECTED_RESULT) == 0,
"Check string result from itoa");
} }
void dap_common_test_run() { void dap_common_test_run() {
printf("Start running dap_common_test\n"); dap_print_module_name("dap_common");
test_put_int(); test_put_int();
} }
#pragma once #pragma once
#include "dap_test.h"
#include "dap_common.h" #include "dap_common.h"
#include "assert.h"
#include "stdbool.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
extern void dap_common_test_run(void); extern void dap_common_test_run(void);
...@@ -27,8 +27,7 @@ static dap_config_t * config; ...@@ -27,8 +27,7 @@ static dap_config_t * config;
void create_test_config_file() { void create_test_config_file() {
config_file = fopen(testconfigName, "w+"); config_file = fopen(testconfigName, "w+");
assert(config_file != NULL && dap_assert(config_file != NULL, "Create config file");
"Can't create config file");
fwrite(config_data, sizeof(char), fwrite(config_data, sizeof(char),
strlen(config_data), config_file); strlen(config_data), config_file);
...@@ -46,43 +45,43 @@ void init_test_case() { ...@@ -46,43 +45,43 @@ void init_test_case() {
} }
void cleanup_test_case() { void cleanup_test_case() {
assert(remove("test_dap_config.cfg") == 0 && dap_assert(remove("test_dap_config.cfg") == 0,
"Error remove config file"); "Remove config file");
dap_config_close(config); dap_config_close(config);
} }
void test_config_open_fail() { void test_config_open_fail() {
assert(dap_config_open("RandomNeverExistName") == NULL dap_assert(dap_config_open("RandomNeverExistName") == NULL,
&& "configOpenFail failed"); "Try open not exists config file");
} }
void test_get_int() { void test_get_int() {
int32_t resultTTL = dap_config_get_item_int32(config, int32_t resultTTL = dap_config_get_item_int32(config,
"server_options", "server_options",
"TTL_session_key"); "TTL_session_key");
assert(resultTTL == 600 && "get_int failed"); dap_assert(resultTTL == 600, "Get int from config");
} }
void test_get_double() { void test_get_double() {
double timeout = dap_config_get_item_double(config, double timeout = dap_config_get_item_double(config,
"server_options", "server_options",
"timeout"); "timeout");
assert(timeout == 1.0 && "test_get_double failed"); dap_assert(timeout == 1.0, "Get double from config");
} }
void test_get_bool() { void test_get_bool() {
bool rBool = dap_config_get_item_bool(config, "server_options", "vpn_enable"); bool rBool = dap_config_get_item_bool(config, "server_options", "vpn_enable");
assert(rBool == true && "test_get_bool failed"); dap_assert(rBool == true, "Get bool from config");
rBool = dap_config_get_item_bool(config, "server_options", "proxy_enable"); rBool = dap_config_get_item_bool(config, "server_options", "proxy_enable");
assert(rBool == false && "test_get_bool failed"); dap_assert(rBool == false, "Get bool from config");
} }
void test_array_str() { void test_array_str() {
uint16_t arraySize; uint16_t arraySize;
char ** result_arr = dap_config_get_array_str(config, "server_options", "str_arr", &arraySize); char ** result_arr = dap_config_get_array_str(config, "server_options", "str_arr", &arraySize);
assert(result_arr != NULL && "test_array_str failed, result_arr is NULL"); dap_assert(result_arr != NULL, "Get array str from config");
assert(arraySize == STR_ARR_LEN); dap_assert(arraySize == STR_ARR_LEN, "Check array length");
for(uint i = 0; i < arraySize; i++) { for(uint i = 0; i < arraySize; i++) {
assert(strcmp(result_arr[i], str_add_test_case[i]) == 0 && "test_array_str failed"); assert(strcmp(result_arr[i], str_add_test_case[i]) == 0 && "test_array_str failed");
...@@ -93,19 +92,20 @@ void test_array_int() { ...@@ -93,19 +92,20 @@ void test_array_int() {
uint16_t arraySize; uint16_t arraySize;
char ** result_arr = dap_config_get_array_str(config, "server_options", "int_arr", &arraySize); char ** result_arr = dap_config_get_array_str(config, "server_options", "int_arr", &arraySize);
assert(result_arr != NULL && "test_array_str failed, result_arr is NULL"); dap_assert(result_arr != NULL, "Get array int");
assert(arraySize == INT_ARR_LEN && "test_array_str failed, arraySize is not equal INT_ARR_LEN"); dap_assert(arraySize == INT_ARR_LEN, "Check array int length");
dap_test_msg("Testing array int values.");
for(uint i = 0; i < arraySize; i++) { for(uint i = 0; i < arraySize; i++) {
assert(atoi(result_arr[i]) == int_arr_test_cases[i] && "test_array_int failed"); dap_assert_PIF(atoi(result_arr[i]) == int_arr_test_cases[i], "Check array int");
} }
} }
void dap_config_tests_run() { void dap_config_tests_run() {
printf("Start running dap_config_tests\n"); dap_print_module_name("dap_config");
init_test_case();
init_test_case();
test_config_open_fail(); test_config_open_fail();
test_get_int(); test_get_int();
test_get_bool(); test_get_bool();
......
#pragma once #pragma once
#include "dap_test.h"
#include "dap_config.h" #include "dap_config.h"
#include "assert.h"
#include "stdbool.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
extern void dap_config_tests_run(void); extern void dap_config_tests_run(void);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment