Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit test cases for parodus certs #455

Closed
wants to merge 13 commits into from
Closed
12 changes: 6 additions & 6 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,11 +74,11 @@ jobs:
run: |
gcovr --sonarqube coverage.xml -r ..

- name: Upload SonarCloud
run: |
sonar-scanner -Dsonar.host.url=https://sonarcloud.io -Dproject.settings=.sonar-project.properties -Dsonar.login=${{ secrets.SONAR_TOKEN }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
#- name: Upload SonarCloud
# run: |
# sonar-scanner -Dsonar.host.url=https://sonarcloud.io -Dproject.settings=.sonar-project.properties -Dsonar.login=${{ secrets.SONAR_TOKEN }}
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Upload Codecov.io
uses: codecov/codecov-action@eaaf4bedf32dbdc6b720b63067d99c4d77d6047d # v3.1.4
Expand Down
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"rdkconfig_generic.h": "c"
}
}
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
28 changes: 25 additions & 3 deletions src/rdkconfig_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "rdkconfig_generic.h"
#include "parodus_log.h"
Expand All @@ -31,6 +32,22 @@ 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 ) {
ParodusInfo( "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;
}

Expand All @@ -43,8 +60,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;
}
33 changes: 30 additions & 3 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -166,6 +168,8 @@ 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)
Expand All @@ -187,6 +191,8 @@ 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})
target_link_libraries (test_service_alive ${PARODUS_COMMON_LIBS} -lcurl -luuid)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
42 changes: 41 additions & 1 deletion tests/test_auth_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,42 @@ 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 */
/*----------------------------------------------------------------------------*/
Expand All @@ -428,7 +464,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);
Expand Down
6 changes: 6 additions & 0 deletions tests/test_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
66 changes: 66 additions & 0 deletions tests/test_rdkconfig_generic.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <CUnit/Basic.h>
#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;
}
5 changes: 4 additions & 1 deletion tests/test_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <assert.h>
#include <cjwt/cjwt.h>
#include <wrp-c.h>
#include <string.h>

#include "../src/token.h"
#include "../src/ParodusInternal.h"
Expand Down Expand Up @@ -630,7 +631,9 @@ void test_allow_insecure_conn ()
char *server_addr;
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;
Expand Down
Loading