From 101a2caa4f4185fbeb7586d119111e8fa794a4a9 Mon Sep 17 00:00:00 2001 From: Rangaiah Date: Thu, 26 Sep 2024 15:55:18 +0530 Subject: [PATCH] Added unit test cases for parodus certs --- .github/workflows/push.yml | 2 +- src/CMakeLists.txt | 2 ++ src/rdkconfig_generic.c | 27 ++++++++++++-- tests/CMakeLists.txt | 33 +++++++++++++++-- tests/test_auth_token.c | 43 +++++++++++++++++++++- tests/test_config.c | 6 ++++ tests/test_rdkconfig_generic.c | 66 ++++++++++++++++++++++++++++++++++ tests/test_token.c | 5 +++ 8 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 tests/test_rdkconfig_generic.c diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 6a09a518..adcd9e36 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -44,7 +44,7 @@ jobs: - name: CMake working-directory: build run: | - cmake .. -DBUILD_GIT:BOOL=true -DENABLE_WEBCFGBIN:BOOL=true -DINTEGRATION_TESTING:BOOL=false -DDISABLE_VALGRIND:BOOL=${DISABLE_VALGRIND} -DENABLE_SESHAT:BOOL=true -DFEATURE_DNS_QUERY:BOOL=true + cmake .. -DBUILD_GIT:BOOL=true -DENABLE_WEBCFGBIN:BOOL=true -DINTEGRATION_TESTING:BOOL=false -DDISABLE_VALGRIND:BOOL=${DISABLE_VALGRIND} -DENABLE_SESHAT:BOOL=true -DFEATURE_DNS_QUERY:BOOL=true -DPARODUS_SECERT_ENABLE:BOOL=true - name: Get rtrouted Binary diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 031e55b8..cd74d7f8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -70,7 +70,9 @@ if (ENABLE_WEBCFGBIN) target_link_libraries (parodus -lrbus) endif (ENABLE_WEBCFGBIN) +if (BUILD_YOCTO) if (PARODUS_SECERT_ENABLE) target_link_libraries (parodus -lrdkconfig -lsecure_wrapper) endif (PARODUS_SECERT_ENABLE) +endif (BUILD_YOCTO) install (TARGETS parodus DESTINATION bin) diff --git a/src/rdkconfig_generic.c b/src/rdkconfig_generic.c index e490191e..b863c4be 100644 --- a/src/rdkconfig_generic.c +++ b/src/rdkconfig_generic.c @@ -23,6 +23,7 @@ #include #include +#include #include "rdkconfig_generic.h" #include "parodus_log.h" @@ -31,6 +32,21 @@ int rdkconfig_get( uint8_t **buf, size_t *buffsize, const char *reference ) { /* This is stub function, No need implemetation */ ParodusInfo("Inside rdkconfig_get stub function.\n"); + if ( reference == NULL ) { + ParodusError( "rdkconfig_get: error, bad argument\n" ); + return RDKCONFIG_FAIL; + } + + if(strcmp(reference,"/tmp/.cfgStaticxpki") == 0) + { + *buf = strdup("xxx"); + *buffsize = 3; + } + else + { + *buf = strdup("yyy\n"); + *buffsize = 4; + } return RDKCONFIG_OK; } @@ -43,8 +59,13 @@ int rdkconfig_set( const char *reference, uint8_t *buf, size_t buffsize ) int rdkconfig_free( uint8_t **buf, size_t buffsize ) { - ParodusInfo("Inside rdkconfig_free stub function.\n"); - free(*buf ); - *buf = NULL; + ParodusInfo("Inside rdkconfig_free stub function.\n"); + if ( buf == NULL ) return RDKCONFIG_FAIL; + if ( *buf == NULL ) { + return RDKCONFIG_OK; // ok if pointer is null + } + memset( *buf, 0, buffsize ); + free( *buf ); + buf = NULL; return RDKCONFIG_OK; } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7a081072..522588e3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -131,6 +131,8 @@ set (CONN_SRC ../src/connection.c ../src/string_helpers.c ../src/mutex.c ../src/time.c ../src/config.c ../src/auth_token.c ../src/spin_thread.c ../src/heartBeat.c ../src/close_retry.c) #set(CONN_SRC ../src/connection.c ${PARODUS_COMMON_SRC}) +set(CONN_SRC ${CONN_SRC} ../src/rdkconfig_generic.c) + add_executable(test_connection test_connection.c ${CONN_SRC}) target_link_libraries (test_connection ${PARODUS_COMMON_LIBS} -lcmocka -lcurl -luuid) @@ -166,6 +168,9 @@ if (ENABLE_WEBCFGBIN) set(CLIST_SRC ${CLIST_SRC} ../src/upstream_rbus.c ../src/xmidtsend_rbus.c) endif (ENABLE_WEBCFGBIN) + +set(CLIST_SRC ${CLIST_SRC} ../src/rdkconfig_generic.c) + add_executable(test_client_list ${CLIST_SRC}) #target_link_libraries (test_client_list ${PARODUS_CONN_LIBS} ${PARODUS_COMMON_LIBS}) target_link_libraries (test_client_list ${PARODUS_COMMON_LIBS} -lcurl -luuid) @@ -186,6 +191,7 @@ endif (ENABLE_SESHAT) if (ENABLE_WEBCFGBIN) set(SVA_SRC ${SVA_SRC} ../src/upstream_rbus.c ../src/xmidtsend_rbus.c) endif (ENABLE_WEBCFGBIN) +set(SVA_SRC ${SVA_SRC} ../src/rdkconfig_generic.c) add_executable(test_service_alive ${SVA_SRC}) #target_link_libraries (test_service_alive ${PARODUS_CONN_LIBS} ${PARODUS_COMMON_LIBS}) @@ -195,7 +201,9 @@ target_link_libraries (test_service_alive ${PARODUS_COMMON_LIBS} -lcurl -luuid) # test_config #------------------------------------------------------------------------------- add_test(NAME test_config COMMAND ${MEMORY_CHECK} ./test_config) -add_executable(test_config test_config.c ../src/config.c ../src/auth_token.c ../src/string_helpers.c) + +set(CON_SRC ${CON_SRC} ../src/rdkconfig_generic.c) +add_executable(test_config test_config.c ../src/config.c ../src/auth_token.c ../src/string_helpers.c ${CON_SRC}) target_link_libraries (test_config -lcmocka -Wl,--no-as-needed -lcimplog -lcjson -lcjwt -ltrower-base64 -lssl -lcrypto -lrt -lm -lcurl -luuid @@ -205,17 +213,21 @@ target_link_libraries (test_config -lcmocka # test_auth_token #------------------------------------------------------------------------------- add_test(NAME test_auth_token COMMAND ${MEMORY_CHECK} ./test_auth_token) -add_executable(test_auth_token test_auth_token.c ../src/config.c ../src/auth_token.c ../src/string_helpers.c) +set(AUTH_SRC ${AUTH_SRC} ../src/rdkconfig_generic.c) + + +add_executable(test_auth_token test_auth_token.c ../src/config.c ../src/auth_token.c ../src/string_helpers.c ${AUTH_SRC}) target_link_libraries (test_auth_token -lcmocka -Wl,--no-as-needed -lcimplog -lcjson -lcjwt -ltrower-base64 -lssl -lcrypto -lrt -lm -lcurl -luuid ) + #------------------------------------------------------------------------------- # test_auth_token_more #------------------------------------------------------------------------------- add_test(NAME test_auth_token_more COMMAND ${MEMORY_CHECK} ./test_auth_token_more) -add_executable(test_auth_token_more test_auth_token_more.c ../src/config.c ../src/auth_token.c ../src/string_helpers.c ../src/config.c) +add_executable(test_auth_token_more test_auth_token_more.c ../src/config.c ../src/auth_token.c ../src/string_helpers.c ../src/config.c ${AUTH_SRC}) target_link_libraries (test_auth_token_more -lcmocka -Wl,--no-as-needed -lcimplog -lcjson -lcjwt -ltrower-base64 -lssl -lcrypto -lrt -lm -lcurl -luuid @@ -376,6 +388,8 @@ if (ENABLE_WEBCFGBIN) set(TOKEN_SRC ${TOKEN_SRC} ../src/upstream_rbus.c ../src/xmidtsend_rbus.c) endif (ENABLE_WEBCFGBIN) +set(TOKEN_SRC ${TOKEN_SRC} ../src/rdkconfig_generic.c) + add_executable(test_token ${TOKEN_SRC} ) #target_link_libraries (test_token ${PARODUS_COMMON_LIBS} ${PARODUS_JWT_LIBS} -lcmocka ) target_link_libraries (test_token ${PARODUS_COMMON_LIBS} -lcmocka -lcurl -luuid) @@ -422,3 +436,16 @@ add_executable(simple ${SIMPLE_SRC}) target_link_libraries (simple ${PARODUS_CONN_LIBS} ${PARODUS_COMMON_LIBS} gcov -lnopoll ) endif (INTEGRATION_TESTING) + +if (PARODUS_SECERT_ENABLE) +#------------------------------------------------------------------------------- +# test_rdkconfig_generic +#------------------------------------------------------------------------------- + +add_test(NAME test_rdkconfig_generic COMMAND ${MEMORY_CHECK} ./test_rdkconfig_generic) +set(SOURCES test_rdkconfig_generic.c ../src/rdkconfig_generic.c) +add_executable(test_rdkconfig_generic ${SOURCES}) +target_link_libraries (test_rdkconfig_generic -lcunit -lcimplog) + +target_link_libraries (test_rdkconfig_generic gcov -Wl,--no-as-needed ) +endif (PARODUS_SECERT_ENABLE) diff --git a/tests/test_auth_token.c b/tests/test_auth_token.c index 0be620ac..4ec685d3 100644 --- a/tests/test_auth_token.c +++ b/tests/test_auth_token.c @@ -412,6 +412,43 @@ void test_getAuthTokenFailure_non200 () free(cfg.token_server_url); } +#ifdef PARODUS_SECERT_ENABLE +void test_getConfigPwd () +{ + uint8_t *pPasswd = NULL; + size_t pPasswdSize = 0; + ParodusCfg cfg; + memset(&cfg,0,sizeof(cfg)); + cfg.ssl_reference_name = strdup("/tmp/.cfgStaticxpki"); + set_parodus_cfg(&cfg); + getConfigPwd(&pPasswd, &pPasswdSize); + assert_string_equal( pPasswd, "xxx"); + assert_int_equal(pPasswdSize,4); + free(pPasswd); + free(cfg.ssl_reference_name); + cfg.ssl_reference_name = strdup("/tmp/.cfgDynamicSExpki"); + set_parodus_cfg(&cfg); + getConfigPwd(&pPasswd, &pPasswdSize); + assert_string_equal( pPasswd, "yyy"); + assert_int_equal(pPasswdSize,4); + free(pPasswd); + free(cfg.ssl_reference_name); +} +void test_getConfigPwd_failure () +{ + uint8_t *pPasswd = NULL; + size_t pPasswdSize = 0; + ParodusCfg cfg; + memset(&cfg,0,sizeof(cfg)); + free(cfg.ssl_reference_name); + cfg.ssl_reference_name = NULL; + set_parodus_cfg(&cfg); + getConfigPwd(&pPasswd, &pPasswdSize); + assert_int_equal(pPasswdSize,0); + assert_null( pPasswd); +} +#endif + /*----------------------------------------------------------------------------*/ /* External Functions */ /*----------------------------------------------------------------------------*/ @@ -428,7 +465,11 @@ int main(void) cmocka_unit_test(test_getAuthToken), cmocka_unit_test(test_getAuthTokenFailure), cmocka_unit_test(test_requestNewAuthToken_non200), - cmocka_unit_test(test_getAuthTokenFailure_non200) + cmocka_unit_test(test_getAuthTokenFailure_non200), +#ifdef PARODUS_SECERT_ENABLE + cmocka_unit_test(test_getConfigPwd), + cmocka_unit_test(test_getConfigPwd_failure), +#endif }; return cmocka_run_group_tests(tests, NULL, NULL); diff --git a/tests/test_config.c b/tests/test_config.c index 3bf43713..0e33b360 100644 --- a/tests/test_config.c +++ b/tests/test_config.c @@ -203,6 +203,9 @@ void test_parseCommandLine() "--jwt-algo=RS256", #endif "--crud-config-file=parodus_cfg.json", + "--ssl-engine=NA", + "--ssl-cert-type=pem", + "--ssl-reference-name=xyz", NULL }; int argc = (sizeof (command) / sizeof (char *)) - 1; @@ -247,6 +250,9 @@ void test_parseCommandLine() #endif assert_int_equal( (int) parodusCfg.boot_retry_wait, 10); assert_string_equal(parodusCfg.crud_config_file, "parodus_cfg.json"); + assert_string_equal(parodusCfg.ssl_engine,"NA"); + assert_string_equal(parodusCfg.ssl_cert_type,"pem"); + assert_string_equal(parodusCfg.ssl_reference_name,"xyz"); } void test_parseCommandLineNull() diff --git a/tests/test_rdkconfig_generic.c b/tests/test_rdkconfig_generic.c new file mode 100644 index 00000000..99c34670 --- /dev/null +++ b/tests/test_rdkconfig_generic.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include +#include "../src/rdkconfig_generic.h" + +void test_rdkconfig_get(void) +{ + uint8_t *temp=NULL; + size_t tempSize=0; + int result = rdkconfig_get(&temp, &tempSize,NULL); + CU_ASSERT_EQUAL(result, RDKCONFIG_FAIL); + result = rdkconfig_get(&temp, &tempSize,"/tmp/.cfgStaticxpki"); + CU_ASSERT_EQUAL(result, RDKCONFIG_OK); +} + +void test_rdkconfig_set(void) +{ + int result = rdkconfig_set(NULL,NULL,0); + CU_ASSERT_EQUAL(result, RDKCONFIG_OK); +} + +void test_rdkconfig_free(void) +{ + int result = rdkconfig_free(NULL,0); + CU_ASSERT_EQUAL(result, RDKCONFIG_FAIL); + char *ptr=NULL; + ptr = strdup("hello"); + result = rdkconfig_free(&ptr,5); + CU_ASSERT_EQUAL(result, RDKCONFIG_OK); +} + +void add_suites( CU_pSuite *suite ) +{ + *suite = CU_add_suite( "tests", NULL, NULL ); + CU_add_test( *suite, "test rdkconfig_get", test_rdkconfig_get); + CU_add_test( *suite, "test rdkconfig_set", test_rdkconfig_set); + CU_add_test( *suite, "test rdkconfig_free", test_rdkconfig_free); +} + +int main( int argc, char *argv[] ) +{ + unsigned rv = 1; + CU_pSuite suite = NULL; + + (void ) argc; + (void ) argv; + + if( CUE_SUCCESS == CU_initialize_registry() ) { + add_suites( &suite ); + + if( NULL != suite ) { + CU_basic_set_mode( CU_BRM_VERBOSE ); + CU_basic_run_tests(); + printf( "\n" ); + CU_basic_show_failures( CU_get_failure_list() ); + printf( "\n\n" ); + rv = CU_get_number_of_tests_failed(); + } + + CU_cleanup_registry(); + + } + + return rv; +} diff --git a/tests/test_token.c b/tests/test_token.c index ac055519..d22770d7 100644 --- a/tests/test_token.c +++ b/tests/test_token.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "../src/token.h" #include "../src/ParodusInternal.h" @@ -631,6 +632,10 @@ void test_allow_insecure_conn () unsigned int port; ParodusCfg *cfg = get_parodus_cfg(); +#ifdef FEATURE_DNS_QUERY + cfg->record_jwt_file = strdup("xmidt-jwt-payload.json"); +#endif + parStrncpy (cfg->hw_mac, "aabbccddeeff", sizeof(cfg->hw_mac)); parStrncpy (cfg->dns_txt_url, "test.mydns.mycom.net", sizeof(cfg->dns_txt_url)); cfg->jwt_algo = 1025;