Skip to content

Commit

Permalink
Issue #36: The cUnit module test was updated:
Browse files Browse the repository at this point in the history
    - rename private test function
    - global counter of started modules was added to crypt module
  • Loading branch information
kirill-scherba committed Aug 9, 2015
1 parent 40386eb commit ca40758
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 23 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@
/tests/teonet_tst
/lib/confuse/.deps/
/tests/rt_udp
/tests/test_net_tr_udp
/tests/test_net_tr_udp
/tests/test_teonet
9 changes: 4 additions & 5 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ uninstall-local:
run: app/teovpn
app/teovpn just-make --port=9057 --show_peers

test: tests/test_net_tr_udp
tests/test_net_tr_udp
# tests/teonet_tst
# sudo tests/teonet_tst test-01 --show_peers

build-tests: tests/test_teonet

test: tests/test_teonet
tests/test_teonet
20 changes: 14 additions & 6 deletions src/crypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include "ev_mgr.h"
#include "utils/rlutil.h"

// Number of modules started
int num_crypt_module = 0;

/**
* Module initialize
*
Expand All @@ -37,9 +40,12 @@ ksnCryptClass *ksnCryptInit(void *ke) {
kcr->blocksize = BLOCK_SIZE;

// Initialize the library
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
OPENSSL_config(NULL);
if(!num_crypt_module) {
ERR_load_crypto_strings();
OpenSSL_add_all_algorithms();
OPENSSL_config(NULL);
}
num_crypt_module++;

return kcr;
}
Expand All @@ -51,10 +57,12 @@ ksnCryptClass *ksnCryptInit(void *ke) {
*/
void ksnCryptDestroy(ksnCryptClass *kcr) {


// Clean up
EVP_cleanup();
ERR_free_strings();
if(num_crypt_module == 1) {
EVP_cleanup();
ERR_free_strings();
}
num_crypt_module--;

free(kcr);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ if TEO_THREAD
LIBS += -pthread
endif

bin_PROGRAMS = teonet_tst test_net_tr_udp
bin_PROGRAMS = teonet_tst test_teonet

teonet_tst_SOURCES = teonet_tst.c
test_net_tr_udp_SOURCES = test_net_tr-udp.c
test_teonet_SOURCES = test_teonet.c test_net_tr-udp.c test_crypt.c
4 changes: 2 additions & 2 deletions tests/teonet_tst.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void event_cb(ksnetEvMgrClass *ke, ksnetEvMgrEvents event, void *data,
}
}

void test1() {
void test_2_1() {
printf("teonet test 1\n");
//printf("%%TEST_FAILED%% time=0 testname=test1 (teonet) message=error message sample\n");
}
Expand Down Expand Up @@ -89,7 +89,7 @@ int main(int argc, char** argv) {
printf("%%SUITE_STARTED%%\n");

printf("%%TEST_STARTED%% test1 (teonet)\n");
test1();
test_2_1();
printf("%%TEST_FINISHED%% time=0 test1 (teonet) \n");

printf("%%TEST_STARTED%% test2 (teonet)\n");
Expand Down
48 changes: 48 additions & 0 deletions tests/test_crypt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* File: test_crypt.c
* Author: Kirill Scherba <kirill@scherba.ru>
*
* Crypt module test
*
* Created on Aug 7, 2015, 9:31:12 PM
*/

#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>

#include "ev_mgr.h"
#include "crypt.h"

extern CU_pSuite pSuite; // Test global variable

extern int num_crypt_module; // Teonet crypt module global variable

/**
* Test Initialize/Destroy Crypt module
*/
void test_1_1() {

ksnCryptClass *kcr;
CU_ASSERT_PTR_NOT_NULL_FATAL((kcr = ksnCryptInit(NULL)));
CU_ASSERT(num_crypt_module == 1);
ksnCryptDestroy(kcr);
CU_ASSERT(num_crypt_module == 0);
}

/**
* Add Crypt suite tests
*
* @return
*/
int add_suite_1_tests(void) {

// Add the tests to the suite
if ((NULL == CU_add_test(pSuite, "Initialize/Destroy Crypt module", test_1_1))
) {
CU_cleanup_registry();
return CU_get_error();
}

return 0;
}
19 changes: 12 additions & 7 deletions tests/test_net_tr-udp.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extern CU_pSuite pSuite;
/**
* Test pblHeap functions
*/
void test1() {
void test_2_1() {

// Create Receive Heap
rh_data *rh_d;
Expand Down Expand Up @@ -72,7 +72,7 @@ void test1() {
/**
* Test Initialize/Destroy TR-UDP module
*/
void test2() {
void test_2_2() {

ksnTRUDPClass *tu;
CU_ASSERT_PTR_NOT_NULL_FATAL((tu = ksnTRUDPInit(NULL)));
Expand All @@ -83,7 +83,7 @@ void test2() {
/**
* Test TR-UDP utility functions
*/
void test3() {
void test_2_3() {

// Test constants and variables
const char *tst_key = "127.0.0.1:1327";
Expand Down Expand Up @@ -131,12 +131,17 @@ void test3() {
CU_ASSERT_STRING_EQUAL_FATAL(key, tst_key);
}

int add_suite1_tests(void) {
/**
* Add TR-UDP suite tests
*
* @return
*/
int add_suite_2_tests(void) {

// Add the tests to the suite
if ((NULL == CU_add_test(pSuite, "pblHeap functions", test1)) ||
(NULL == CU_add_test(pSuite, "Initialize/Destroy TR-UDP module", test2)) ||
(NULL == CU_add_test(pSuite, "TR-UDP utility functions", test3))
if ((NULL == CU_add_test(pSuite, "pblHeap functions", test_2_1)) ||
(NULL == CU_add_test(pSuite, "Initialize/Destroy TR-UDP module", test_2_2)) ||
(NULL == CU_add_test(pSuite, "TR-UDP utility functions", test_2_3))
) {
CU_cleanup_registry();
return CU_get_error();
Expand Down
67 changes: 67 additions & 0 deletions tests/test_teonet.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* File: test_net_rt_udp.c
* Author: Kirill Scherba <kirill@scherba.ru>
*
* TR-UDP module test
*
* Created on Aug 7, 2015, 9:31:12 PM
*/

#include <stdio.h>
#include <stdlib.h>
#include <CUnit/Basic.h>

#include "ev_mgr.h"

// Modules functions
int add_suite_1_tests(void);
int add_suite_2_tests(void);

// Global variables
CU_pSuite pSuite = NULL;


/*
* CUnit Test
*/

int init_suite(void) {
return 0;
}

int clean_suite(void) {
return 0;
}

int main() {

KSN_SET_TEST_MODE(1);

// Initialize the CUnit test registry
if (CUE_SUCCESS != CU_initialize_registry())
return CU_get_error();

// Add a suite to the registry
pSuite = CU_add_suite("Teonet library Crypt module", init_suite, clean_suite);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
add_suite_1_tests();

// Add a suite to the registry
pSuite = CU_add_suite("Teonet library TR-UDP module", init_suite, clean_suite);
if (NULL == pSuite) {
CU_cleanup_registry();
return CU_get_error();
}
add_suite_2_tests();

/* Run all tests using the CUnit Basic interface */
CU_basic_set_mode(CU_BRM_VERBOSE);
//CU_list_tests_to_file();
CU_basic_run_tests();
//CU_console_run_tests();
CU_cleanup_registry();
return CU_get_error();
}

0 comments on commit ca40758

Please sign in to comment.