From 25a6a99c69922162f6f47ec94cc28c6a36fb7cbe Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Sat, 9 Feb 2019 20:33:18 +0530 Subject: [PATCH] Added unit tests for themis curl changes --- README.md | 10 +- tests/test_config.c | 287 ++++++++++++++++++++++++++++++++++++---- tests/test_connection.c | 11 +- 3 files changed, 272 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index ea6e5fd..72adb3b 100644 --- a/README.md +++ b/README.md @@ -55,9 +55,9 @@ make test - /force-ipv6 -Forcefully connect parodus to ipv6 address (optional argument) -- /token-read-script -Script to get auth token for establishing secure connection (absolute path where that script is present) -optional argument +- /client-cert-path -client cert location to request auth token for establishing secure connection (absolute path where client cert is present) -optional argument -- /token-acquisition-script -Script to create new auth token for establishing secure connection (absolute path where that script is present) -optional argument +- /token-server-url -server url to request new auth token for establishing secure connection -optional argument - /crud-config-file -Config json file to store objects during create, retrieve, update and delete (CRUD) operations -optional argument @@ -82,17 +82,17 @@ make test ``` # Seshat & FEATURE_DNS_QUERY Enabled -./parodus --hw-model=TGXXX --hw-serial-number=E8GBUEXXXXXXXXX --hw-manufacturer=ARRIS --hw-mac=14cfexxxxxxx --hw-last-reboot-reason=unknown --fw-name=TG1682_DEV_master_20170512115046sdy --boot-time=1494590301 --webpa-ping-timeout=180 --webpa-interface-used=eth0 --webpa-url=somebody.net:8080 --webpa-backoff-max=9 --parodus-local-url=tcp://127.0.0.1:6666 --partner-id=comcast --ssl-cert-path=/etc/ssl/certs/ca-certificates.crt --acquire-jwt=1 --dns-txt-url=somebody.net --jwt-public-key-file=webpa-rs256.pem --jwt-algo=RS256 --seshat-url=tcp://127.0.0.1:7777 --token-read-script=/usr/ccsp/parodus/parodus_token1.sh --token-acquisition-script=/usr/ccsp/parodus/parodus_token2.sh --force-ipv4 --crud-config-file=/tmp/parodus_cfg.json +./parodus --hw-model=TGXXX --hw-serial-number=E8GBUEXXXXXXXXX --hw-manufacturer=ARRIS --hw-mac=14cfexxxxxxx --hw-last-reboot-reason=unknown --fw-name=TG1682_DEV_master_20170512115046sdy --boot-time=1494590301 --webpa-ping-timeout=180 --webpa-interface-used=eth0 --webpa-url=somebody.net:8080 --webpa-backoff-max=9 --parodus-local-url=tcp://127.0.0.1:6666 --partner-id=comcast --ssl-cert-path=/etc/ssl/certs/ca-certificates.crt --acquire-jwt=1 --dns-txt-url=somebody.net --jwt-public-key-file=webpa-rs256.pem --jwt-algo=RS256 --seshat-url=tcp://127.0.0.1:7777 --client-cert-path=/tmp/clientcert.mch --token-server-url=somebody.net:8080 --force-ipv4 --crud-config-file=/tmp/parodus_cfg.json # Seshat is not enabled -./parodus --hw-model=TGXXX --hw-serial-number=E8GBUEXXXXXXXXX --hw-manufacturer=ARRIS --hw-mac=14cfexxxxxxx --hw-last-reboot-reason=unknown --fw-name=TG1682_DEV_master_20170512115046sdy --boot-time=1494590301 --webpa-ping-timeout=180 --webpa-interface-used=eth0 --webpa-url=somebody.net:8080 --webpa-backoff-max=9 --parodus-local-url=tcp://127.0.0.1:6666 --partner-id=comcast --ssl-cert-path=/etc/ssl/certs/ca-certificates.crt --acquire-jwt=1 --dns-txt-url=somebody.net --jwt-public-key-file=webpa-rs256.pem --jwt-algo=RS256 --token-read-script=/usr/ccsp/parodus/parodus_token1.sh --token-acquisition-script=/usr/ccsp/parodus/parodus_token2.sh --force-ipv4 --crud-config-file=/tmp/parodus_cfg.json +./parodus --hw-model=TGXXX --hw-serial-number=E8GBUEXXXXXXXXX --hw-manufacturer=ARRIS --hw-mac=14cfexxxxxxx --hw-last-reboot-reason=unknown --fw-name=TG1682_DEV_master_20170512115046sdy --boot-time=1494590301 --webpa-ping-timeout=180 --webpa-interface-used=eth0 --webpa-url=somebody.net:8080 --webpa-backoff-max=9 --parodus-local-url=tcp://127.0.0.1:6666 --partner-id=comcast --ssl-cert-path=/etc/ssl/certs/ca-certificates.crt --acquire-jwt=1 --dns-txt-url=somebody.net --jwt-public-key-file=webpa-rs256.pem --jwt-algo=RS256 --client-cert-path=/tmp/clientcert.mch --token-server-url=somebody.net:8080 --force-ipv4 --crud-config-file=/tmp/parodus_cfg.json # When both Seshat & FEATURE_DNS_QUERY not Enabled -./parodus --hw-model=TGXXX --hw-serial-number=E8GBUEXXXXXXXXX --hw-manufacturer=ARRIS --hw-mac=14cfexxxxxxx --hw-last-reboot-reason=unknown --fw-name=TG1682_DEV_master_20170512115046sdy --boot-time=1494590301 --webpa-ping-timeout=180 --webpa-interface-used=eth0 --webpa-url=somebody.net:8080 --webpa-backoff-max=9 --parodus-local-url=tcp://127.0.0.1:6666 --partner-id=comcast --ssl-cert-path=/etc/ssl/certs/ca-certificates.crt --token-read-script=/usr/ccsp/parodus/parodus_token1.sh --token-acquisition-script=/usr/ccsp/parodus/parodus_token2.sh --force-ipv4 --crud-config-file=/tmp/parodus_cfg.json +./parodus --hw-model=TGXXX --hw-serial-number=E8GBUEXXXXXXXXX --hw-manufacturer=ARRIS --hw-mac=14cfexxxxxxx --hw-last-reboot-reason=unknown --fw-name=TG1682_DEV_master_20170512115046sdy --boot-time=1494590301 --webpa-ping-timeout=180 --webpa-interface-used=eth0 --webpa-url=somebody.net:8080 --webpa-backoff-max=9 --parodus-local-url=tcp://127.0.0.1:6666 --partner-id=comcast --ssl-cert-path=/etc/ssl/certs/ca-certificates.crt --client-cert-path=/tmp/clientcert.mch --token-server-url=somebody.net:8080 --force-ipv4 --crud-config-file=/tmp/parodus_cfg.json ``` diff --git a/tests/test_config.c b/tests/test_config.c index 0a7e0b0..c86eb3f 100644 --- a/tests/test_config.c +++ b/tests/test_config.c @@ -37,7 +37,7 @@ extern int parse_webpa_url (const char *full_url, char **server_addr, unsigned int *port); extern unsigned int get_algo_mask (const char *algo_str); extern unsigned int parse_num_arg (const char *arg, const char *arg_name); -extern void createNewAuthToken(char *newToken, size_t len); +extern int requestNewAuthToken(char *newToken, size_t len, int r_count); /*----------------------------------------------------------------------------*/ /* Mocks */ @@ -52,6 +52,29 @@ void create_token_script(char *fname) sprintf(command, "chmod +x %s",fname); system(command); } + +typedef void CURL; + +typedef enum { + CURLINFO_RESPONSE_CODE = 2, + CURLINFO_TOTAL_TIME +} CURLINFO; + +int curl_easy_perform(CURL *curl) +{ + UNUSED(curl); + function_called(); + return (int) mock(); +} + +int curl_easy_getinfo(CURL *curl, CURLINFO CURLINFO_RESPONSE_CODE, long response_code) +{ + UNUSED(curl); + UNUSED(CURLINFO_RESPONSE_CODE); + UNUSED(response_code); + function_called(); + return (int) mock(); +} /*----------------------------------------------------------------------------*/ /* Tests */ /*----------------------------------------------------------------------------*/ @@ -183,7 +206,10 @@ void test_parseCommandLine() #endif "--force-ipv4", "--force-ipv6", + "--boot-time-retry-wait=10", "--ssl-cert-path=/etc/ssl/certs/ca-certificates.crt", + "--client-cert-path=testcert", + "--token-server-url=https://dev.comcast.net/token", #ifdef FEATURE_DNS_QUERY "--acquire-jwt=1", "--dns-txt-url=mydns.mycom.net", @@ -211,6 +237,7 @@ void test_parseCommandLine() assert_string_equal( parodusCfg.hw_last_reboot_reason, "unknown"); assert_string_equal( parodusCfg.fw_name, "TG1682_DEV_master_2016000000sdy"); assert_int_equal( (int) parodusCfg.webpa_ping_timeout,180); + assert_int_equal( (int) parodusCfg.boot_retry_wait,10); assert_string_equal( parodusCfg.webpa_interface_used, "br0"); assert_string_equal( parodusCfg.webpa_url, "http://127.0.0.1"); assert_int_equal( (int) parodusCfg.webpa_backoff_max,0); @@ -221,18 +248,28 @@ void test_parseCommandLine() assert_string_equal( parodusCfg.seshat_url, "ipc://127.0.0.1:7777"); #endif assert_int_equal( (int) parodusCfg.flags, FLAGS_IPV6_ONLY|FLAGS_IPV4_ONLY); - sprintf(expectedToken,"secure-token-%s-%s",parodusCfg.hw_serial_number,parodusCfg.hw_mac); - getAuthToken(&parodusCfg); - set_parodus_cfg(&parodusCfg); + set_parodus_cfg(&parodusCfg); + will_return (curl_easy_perform, 0); + expect_function_calls (curl_easy_perform, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + will_return (curl_easy_getinfo, -1); + expect_function_calls (curl_easy_getinfo, 1); + getAuthToken(&parodusCfg); assert_string_equal( get_parodus_cfg()->webpa_auth_token,expectedToken); assert_string_equal( parodusCfg.cert_path,"/etc/ssl/certs/ca-certificates.crt"); + assert_string_equal( parodusCfg.client_cert_path,"testcert"); + assert_string_equal( parodusCfg.token_server_url,"https://dev.comcast.net/token"); #ifdef FEATURE_DNS_QUERY assert_int_equal( (int) parodusCfg.acquire_jwt, 1); assert_string_equal(parodusCfg.dns_txt_url, "mydns.mycom.net"); assert_int_equal( (int) parodusCfg.jwt_algo, 1024); assert_string_equal ( get_parodus_cfg()->jwt_key, jwt_key); #endif + assert_int_equal( (int) parodusCfg.boot_retry_wait, 10); assert_string_equal(parodusCfg.crud_config_file, "parodus_cfg.json"); } @@ -319,6 +356,8 @@ void test_loadParodusCfg() parStrncpy(Cfg->seshat_url, "ipc://tmp/seshat_service.url", sizeof(Cfg->seshat_url)); #endif Cfg->crud_config_file = strdup("parodus_cfg.json"); + Cfg->client_cert_path = strdup("testcert"); + Cfg->token_server_url = strdup("https://dev.comcast.net/token"); memset(&tmpcfg,0,sizeof(ParodusCfg)); loadParodusCfg(Cfg,&tmpcfg); @@ -329,6 +368,8 @@ void test_loadParodusCfg() assert_string_equal( tmpcfg.local_url, "tcp://10.0.0.1:6000"); assert_string_equal( tmpcfg.partner_id, "shaw"); assert_string_equal( tmpcfg.webpa_protocol, protocol); + assert_string_equal(tmpcfg.client_cert_path, "testcert"); + assert_string_equal(tmpcfg.token_server_url, "https://dev.comcast.net/token"); #ifdef FEATURE_DNS_QUERY assert_int_equal( (int) tmpcfg.acquire_jwt, 1); assert_string_equal(tmpcfg.dns_txt_url, "mydns"); @@ -546,32 +587,223 @@ void test_get_algo_mask () #endif } - - -void test_new_auth_token () +void getAuthToken_Null() { - char token[64]; + ParodusCfg cfg; + memset(&cfg,0,sizeof(cfg)); + parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac)); + cfg.client_cert_path = NULL; + getAuthToken(&cfg); + set_parodus_cfg(&cfg); + assert( cfg.client_cert_path == NULL); +} + +void getAuthToken_MacNull() +{ + ParodusCfg cfg; + memset(&cfg,0,sizeof(cfg)); + cfg.client_cert_path = NULL; + getAuthToken(&cfg); + set_parodus_cfg(&cfg); + assert( cfg.client_cert_path == NULL); +} + +void test_requestNewAuthToken_init_fail () +{ + char token[32]; + ParodusCfg cfg; + int output = -1; + memset(&cfg,0,sizeof(cfg)); + + cfg.token_server_url = strdup("https://dev.comcast.net/token"); + parStrncpy(cfg.cert_path , "/etc/ssl/certs/ca-certificates.crt", sizeof(cfg.cert_path)); + parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number)); + parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac)); + + set_parodus_cfg(&cfg); + + will_return (curl_easy_perform, -1); + expect_function_calls (curl_easy_perform, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + will_return (curl_easy_getinfo, -1); + expect_function_calls (curl_easy_getinfo, 1); + + requestNewAuthToken (token, sizeof(token), 2); + assert_int_equal (output, -1); + free(cfg.token_server_url); +} + + +void test_requestNewAuthToken_failure () +{ + char token[32]; + ParodusCfg cfg; + int output = -1; + memset(&cfg,0,sizeof(cfg)); + + cfg.token_server_url = strdup("https://dev.comcast.net/token"); + parStrncpy(cfg.cert_path , "/etc/ssl/certs/ca-certificates.crt", sizeof(cfg.cert_path)); + parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number)); + parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac)); + set_parodus_cfg(&cfg); + + will_return (curl_easy_perform, -1); + expect_function_calls (curl_easy_perform, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + will_return (curl_easy_getinfo, -1); + expect_function_calls (curl_easy_getinfo, 1); + + requestNewAuthToken (token, sizeof(token), 2); + assert_int_equal (output, -1); + free(cfg.token_server_url); +} + + +void test_requestNewAuthToken () +{ + char token[1024]; ParodusCfg cfg; + int output = -1; memset(&cfg,0,sizeof(cfg)); - parStrncpy (cfg.token_acquisition_script, "../../tests/return_success.bsh", - sizeof(cfg.token_acquisition_script)); - parStrncpy (cfg.token_read_script, "../../tests/return_ser_mac.bsh", - sizeof(cfg.token_read_script)); + cfg.token_server_url = strdup("https://dev.comcast.net/token"); + parStrncpy(cfg.cert_path , "/etc/ssl/certs/ca-certificates.crt", sizeof(cfg.cert_path)); + parStrncpy(cfg.webpa_interface_used , "eth0", sizeof(cfg.webpa_interface_used)); parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number)); parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac)); - set_parodus_cfg(&cfg); - createNewAuthToken (token, sizeof(token)); - assert_string_equal (token, "SER_MAC Fer23u948590 123567892366"); - - memset (token, 0, sizeof(token)); - parStrncpy (cfg.token_acquisition_script, "../../tests/return_failure.bsh", - sizeof(cfg.token_acquisition_script)); - set_parodus_cfg(&cfg); - createNewAuthToken (token, sizeof(token)); - assert_string_equal (token, ""); - + + will_return (curl_easy_perform, 0); + expect_function_calls (curl_easy_perform, 1); + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + output = requestNewAuthToken (token, sizeof(token), 1); + assert_int_equal (output, 0); + free(cfg.token_server_url); +} + +void test_getAuthToken () +{ + ParodusCfg cfg; + memset(&cfg,0,sizeof(cfg)); + + cfg.token_server_url = strdup("https://dev.comcast.net/token"); + cfg.client_cert_path = strdup("testcert"); + parStrncpy(cfg.cert_path , "/etc/ssl/certs/ca-certificates.crt", sizeof(cfg.cert_path)); + parStrncpy(cfg.webpa_interface_used , "eth0", sizeof(cfg.webpa_interface_used)); + parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number)); + parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac)); + set_parodus_cfg(&cfg); + + will_return (curl_easy_perform, -1); + expect_function_calls (curl_easy_perform, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + will_return (curl_easy_perform, -1); + expect_function_calls (curl_easy_perform, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + will_return (curl_easy_perform, 0); + expect_function_calls (curl_easy_perform, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + getAuthToken(&cfg); + + assert( cfg.webpa_auth_token != NULL); + + free(cfg.client_cert_path); + free(cfg.token_server_url); +} + +void test_getAuthTokenFailure () +{ + ParodusCfg cfg; + memset(&cfg,0,sizeof(cfg)); + + cfg.token_server_url = strdup("https://dev.comcast.net/token"); + cfg.client_cert_path = strdup("testcert"); + parStrncpy(cfg.cert_path , "/etc/ssl/certs/ca-certificates.crt", sizeof(cfg.cert_path)); + parStrncpy(cfg.webpa_interface_used , "eth0", sizeof(cfg.webpa_interface_used)); + parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number)); + parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac)); + set_parodus_cfg(&cfg); + will_return (curl_easy_perform, -1); + expect_function_calls (curl_easy_perform, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + will_return (curl_easy_perform, -1); + expect_function_calls (curl_easy_perform, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + will_return (curl_easy_perform, -1); + expect_function_calls (curl_easy_perform, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + will_return (curl_easy_getinfo, 0); + expect_function_calls (curl_easy_getinfo, 1); + + getAuthToken(&cfg); + + assert_string_equal( cfg.webpa_auth_token, ""); + + free(cfg.client_cert_path); + free(cfg.token_server_url); +} + +void test_write_callback_fn () +{ + void *buffer; + size_t size = 1; + size_t nmemb =8; + int out_len=0; + struct token_data data; + data.size = 0; + buffer = strdup("response"); + + data.data = (char *) malloc(sizeof(char) * 5); + data.data[0] = '\0'; + + out_len = write_callback_fn(buffer, size, nmemb, &data); + assert_string_equal(data.data, buffer); + assert_int_equal( out_len, strlen(buffer)); + free(data.data); } /*----------------------------------------------------------------------------*/ @@ -598,7 +830,14 @@ int main(void) //cmocka_unit_test(test_parodusGitVersion), cmocka_unit_test(test_setDefaultValuesToCfg), cmocka_unit_test(err_setDefaultValuesToCfg), - cmocka_unit_test(test_new_auth_token) + cmocka_unit_test(test_requestNewAuthToken), + cmocka_unit_test(test_requestNewAuthToken_init_fail), + cmocka_unit_test(test_requestNewAuthToken_failure), + cmocka_unit_test(getAuthToken_Null), + cmocka_unit_test(getAuthToken_MacNull), + cmocka_unit_test(test_getAuthToken), + cmocka_unit_test(test_getAuthTokenFailure), + cmocka_unit_test(test_write_callback_fn), }; return cmocka_run_group_tests(tests, NULL, NULL); diff --git a/tests/test_connection.c b/tests/test_connection.c index 340a067..b14e69f 100644 --- a/tests/test_connection.c +++ b/tests/test_connection.c @@ -381,7 +381,6 @@ void test_set_extra_headers () create_connection_ctx_t ctx; ParodusCfg cfg; const char *expected_extra_headers = - "\r\nAuthorization: Bearer SER_MAC Fer23u948590 123567892366" "\r\nX-WebPA-Device-Name: mac:123567892366" "\r\nX-WebPA-Device-Protocols: wrp-0.11,getset-0.1" "\r\nUser-Agent: WebPA-1.6 (2.364s2; TG1682/ARRISGroup,Inc.;)" @@ -390,15 +389,13 @@ void test_set_extra_headers () memset(&cfg,0,sizeof(cfg)); memset (&ctx, 0, sizeof(ctx)); - parStrncpy (cfg.token_acquisition_script, "../../tests/return_success.bsh", - sizeof(cfg.token_acquisition_script)); - parStrncpy (cfg.token_read_script, "../../tests/return_ser_mac.bsh", - sizeof(cfg.token_read_script)); + cfg.client_cert_path = strdup("testcert"); parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number)); parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac)); parStrncpy(cfg.hw_model, "TG1682", sizeof(cfg.hw_model)); parStrncpy(cfg.hw_manufacturer , "ARRISGroup,Inc.", sizeof(cfg.hw_manufacturer)); parStrncpy(cfg.fw_name , "2.364s2", sizeof(cfg.fw_name)); + parStrncpy(cfg.cert_path , "/etc/ssl/certs/ca-certificates.crt", sizeof(cfg.cert_path)); parStrncpy(cfg.webpa_protocol , "WebPA-1.6", sizeof(cfg.webpa_protocol)); set_parodus_cfg(&cfg); @@ -410,9 +407,9 @@ void test_set_extra_headers () assert_string_equal (ctx.header_info.conveyHeader, "WebPA-1.6 (TG1682)"); set_extra_headers (&ctx, true); - assert_string_equal (get_parodus_cfg()->webpa_auth_token, - "SER_MAC Fer23u948590 123567892366"); assert_string_equal (ctx.extra_headers, expected_extra_headers); + if(cfg.client_cert_path !=NULL) { + free(cfg.client_cert_path); } free (ctx.extra_headers); free_header_info (&ctx.header_info);