diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c71f21..13d971c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Security: Mutual Authentication (mTLS or two way TLS) - Rename command line options for MTLS cert and Key - Update to use nanomsg v. 1.1.4 +- requestNewAuthToken will clear the token if it fails. +- request auth token on every retry, not just after 403 +- update to use nopoll v 1.0.2 - Add pause/resume heartBeatTimer ## [1.0.2] - 2019-02-08 diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f43852..d6d6d92 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,7 +85,7 @@ add_dependencies(libtrower-base64 trower-base64) ExternalProject_Add(nopoll PREFIX ${PREFIX_DIR}/nopoll GIT_REPOSITORY https://github.com/Comcast/nopoll.git - GIT_TAG "1.0.1" + GIT_TAG "1.0.2" CONFIGURE_COMMAND COMMAND /autogen.sh --prefix=${PREFIX} --includedir=${INCLUDE_DIR} --libdir=${LIBRARY_DIR} @@ -152,7 +152,7 @@ add_dependencies(libcurl curl) ExternalProject_Add(cimplog PREFIX ${CMAKE_CURRENT_BINARY_DIR}/_prefix/cimplog GIT_REPOSITORY https://github.com/Comcast/cimplog.git - GIT_TAG "1.0.1" + GIT_TAG "1.0.2" CMAKE_ARGS += -DCMAKE_INSTALL_PREFIX=${INSTALL_DIR} -DBUILD_TESTING=OFF ) add_library(libcimplog STATIC SHARED IMPORTED) diff --git a/src/auth_token.c b/src/auth_token.c index ce24df9..d785bf8 100644 --- a/src/auth_token.c +++ b/src/auth_token.c @@ -66,17 +66,11 @@ int requestNewAuthToken(char *newToken, size_t len, int r_count) struct token_data data; data.size = 0; + data.data = newToken; curl = curl_easy_init(); if(curl) { - //this memory will be dynamically grown by write call back fn as required - data.data = (char *) malloc(sizeof(char) * 1); - if(NULL == data.data) - { - ParodusError("Failed to allocate memory.\n"); - return -1; - } data.data[0] = '\0'; createCurlheader(mac_header, serial_header, uuid_header, transaction_uuid, list, &headers_list); @@ -137,11 +131,8 @@ int requestNewAuthToken(char *newToken, size_t len, int r_count) { ParodusError("curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); curl_easy_cleanup(curl); - if(data.data) - { - free(data.data); - data.data = NULL; - } + data.size = 0; + memset (data.data, 0, len); return -1; } else @@ -149,19 +140,15 @@ int requestNewAuthToken(char *newToken, size_t len, int r_count) if(response_code == 200) { ParodusInfo("cURL success\n"); - strncpy(newToken, data.data, len); } } - if(data.data) - { - free(data.data); - data.data = NULL; - } curl_easy_cleanup(curl); } else { ParodusError("curl init failure\n"); + data.size = 0; + memset (data.data, 0, len); return -1; } @@ -179,6 +166,7 @@ void getAuthToken(ParodusCfg *cfg) int status = -1; int retry_count = 0; + memset (cfg->webpa_auth_token, 0, sizeof(cfg->webpa_auth_token)); if( cfg->hw_mac != NULL && strlen(cfg->hw_mac) !=0 ) { if( cfg->client_cert_path !=NULL && strlen(cfg->client_cert_path) !=0 ) @@ -225,28 +213,23 @@ void getAuthToken(ParodusCfg *cfg) */ size_t write_callback_fn(void *buffer, size_t size, size_t nmemb, struct token_data *data) { + ParodusCfg *cfg; + size_t max_data_size = sizeof (cfg->webpa_auth_token); size_t index = data->size; size_t n = (size * nmemb); - char* tmp; - data->size += (size * nmemb); + data->size += n; - tmp = realloc(data->data, data->size + 1); /* +1 for '\0' */ - - if(tmp) { - data->data = tmp; - } else { - if(data->data) { - free(data->data); - } - ParodusError("Failed to allocate memory for data\n"); + if (data->size >= max_data_size) { + ParodusError("Auth token data overruns buffer\n"); + data->size = 0; return 0; } memcpy((data->data + index), buffer, n); data->data[data->size] = '\0'; - return size * nmemb; + return n; } /* @brief function to generate random uuid. diff --git a/src/config.c b/src/config.c index 38b789d..be09283 100644 --- a/src/config.c +++ b/src/config.c @@ -435,6 +435,7 @@ int parseCommandLine(int argc,char **argv,ParodusCfg * cfg) case 'r': parStrncpy(cfg->hw_last_reboot_reason, optarg,sizeof(cfg->hw_last_reboot_reason)); ParodusInfo("hw_last_reboot_reason is %s\n",cfg->hw_last_reboot_reason); + OnboardLog("Last reboot reason is %s\n",cfg->hw_last_reboot_reason); break; case 'n': diff --git a/src/conn_interface.c b/src/conn_interface.c index d8ecf0f..147463c 100644 --- a/src/conn_interface.c +++ b/src/conn_interface.c @@ -101,6 +101,7 @@ void createSocketConnection(void (* initKeypress)()) if(!create_conn_rtn) { ParodusError("Unrecovered error, terminating the process\n"); + OnboardLog("Unrecovered error, terminating the process\n"); abort(); } packMetaData(); @@ -144,6 +145,7 @@ void createSocketConnection(void (* initKeypress)()) { ParodusError("ping wait time > %d . Terminating the connection with WebPA server and retrying\n", webpa_ping_timeout_ms / 1000); ParodusInfo("Reconnect detected, setting Ping_Miss reason for Reconnect\n"); + OnboardLog("Reconnect detected, setting Ping_Miss reason for Reconnect\n"); set_global_reconnect_reason("Ping_Miss"); set_global_reconnect_status(true); set_close_retry(); diff --git a/src/connection.c b/src/connection.c index c7b3cd8..1321004 100644 --- a/src/connection.c +++ b/src/connection.c @@ -296,21 +296,25 @@ void set_current_server (create_connection_ctx_t *ctx) ctx->current_server = get_current_server (&ctx->server_list); } -void set_extra_headers (create_connection_ctx_t *ctx, int reauthorize) -{ - if (reauthorize && (get_parodus_cfg()->client_cert_path !=NULL && strlen(get_parodus_cfg()->client_cert_path) >0)) - { - getAuthToken(get_parodus_cfg()); - } - - ctx->extra_headers = build_extra_hdrs (&ctx->header_info); -} - void free_extra_headers (create_connection_ctx_t *ctx) { FREE_PTR_VAR (ctx->extra_headers) } +void set_extra_headers (create_connection_ctx_t *ctx) +{ + ParodusCfg * cfg = get_parodus_cfg(); + + free_extra_headers (ctx); + if ((strlen(cfg->webpa_auth_token) == 0) && + (cfg->client_cert_path != NULL) && (strlen(cfg->client_cert_path) > 0)) + { + getAuthToken(cfg); + } + + ctx->extra_headers = build_extra_hdrs (&ctx->header_info); +} + void free_connection_ctx (create_connection_ctx_t *ctx) { free_extra_headers (ctx); @@ -389,6 +393,8 @@ int nopoll_connect (create_connection_ctx_t *ctx, int is_ipv6) if((checkHostIp(server->server_addr) == -2)) { if (check_timer_expired (&ctx->connect_timer, 15*60*1000)) { ParodusError("WebPA unable to connect due to DNS resolving to 10.0.0.1 for over 15 minutes; crashing service.\n"); + OnboardLog("WebPA unable to connect due to DNS resolving to 10.0.0.1 for over 15 minutes; crashing service.\n"); + OnboardLog("Reconnect detected, setting Dns_Res_webpa_reconnect reason for Reconnect\n"); set_global_reconnect_reason("Dns_Res_webpa_reconnect"); set_global_reconnect_status(true); @@ -404,7 +410,7 @@ int nopoll_connect (create_connection_ctx_t *ctx, int is_ipv6) //-------------------------------------------------------------------- // Return codes for wait_connection_ready #define WAIT_SUCCESS 0 -#define WAIT_ACTION_RETRY 1 // if wait_status is 307, 302, 303 or 403 +#define WAIT_ACTION_RETRY 1 // if wait_status is 307, 302, 303, or 403 #define WAIT_FAIL 2 #define FREE_NON_NULL_PTR(ptr) if (NULL != ptr) free(ptr) @@ -440,10 +446,12 @@ int wait_connection_ready (create_connection_ctx_t *ctx) FREE_NON_NULL_PTR (redirectURL); if(wait_status == 403) { - ParodusError("Received Unauthorized response with status: %d\n", wait_status); - free_extra_headers (ctx); - set_extra_headers (ctx, true); - return WAIT_ACTION_RETRY; + ParodusCfg *cfg = get_parodus_cfg(); + /* clear auth token in cfg so that we will refetch auth token */ + memset (cfg->webpa_auth_token, 0, sizeof(cfg->webpa_auth_token)); + ParodusError("Received Unauthorized response with status: %d\n", wait_status); + OnboardLog("Received Unauthorized response with status: %d\n", wait_status); + return WAIT_ACTION_RETRY; } ParodusError("Client connection timeout\n"); ParodusError("RDK-10037 - WebPA Connection Lost\n"); @@ -453,9 +461,9 @@ int wait_connection_ready (create_connection_ctx_t *ctx) //-------------------------------------------------------------------- // Return codes for connect_and_wait -#define CONN_WAIT_SUCCESS 0 -#define CONN_WAIT_ACTION_RETRY 1 // if wait_status is 307, 302, 303 or 403 -#define CONN_WAIT_RETRY_DNS 2 +#define CONN_WAIT_SUCCESS 0 +#define CONN_WAIT_ACTION_RETRY 1 // if wait_status is 307, 302, 303, or 403 +#define CONN_WAIT_RETRY_DNS 2 int connect_and_wait (create_connection_ctx_t *ctx) { @@ -514,9 +522,12 @@ int keep_trying_to_connect (create_connection_ctx_t *ctx, while (true) { + set_extra_headers (ctx); + rtn = connect_and_wait (ctx); if (rtn == CONN_WAIT_SUCCESS) return true; + if (rtn == CONN_WAIT_ACTION_RETRY) // if redirected or build_headers continue; backoff_delay (backoff_timer); // 3,7,15,31 .. @@ -552,11 +563,11 @@ int createNopollConnection(noPollCtx *ctx) max_retry_count = (int) get_parodus_cfg()->webpa_backoff_max; ParodusPrint("max_retry_count is %d\n", max_retry_count ); - + + memset (&conn_ctx, 0, sizeof(create_connection_ctx_t)); conn_ctx.nopoll_ctx = ctx; init_expire_timer (&conn_ctx.connect_timer); init_header_info (&conn_ctx.header_info); - set_extra_headers (&conn_ctx, false); set_server_list_null (&conn_ctx.server_list); init_backoff_timer (&backoff_timer, max_retry_count); @@ -574,10 +585,12 @@ int createNopollConnection(noPollCtx *ctx) if(conn_ctx.current_server->allow_insecure <= 0) { ParodusInfo("Connected to server over SSL\n"); + OnboardLog("Connected to server over SSL\n"); } else { ParodusInfo("Connected to server\n"); + OnboardLog("Connected to server\n"); } get_parodus_cfg()->cloud_status = CLOUD_STATUS_ONLINE; diff --git a/src/crud_internal.c b/src/crud_internal.c index 30531fc..45459d6 100644 --- a/src/crud_internal.c +++ b/src/crud_internal.c @@ -1346,6 +1346,7 @@ static int ConnDisconnectFromCloud(char *disconn_reason) if(!close_retry) { ParodusInfo("Reconnect detected, setting reason %s for Reconnect\n", disconn_reason); + OnboardLog("Reconnect detected, setting reason %s for Reconnect\n", disconn_reason); set_global_reconnect_reason(disconn_reason); set_global_reconnect_status(true); set_close_retry(); diff --git a/src/downstream.c b/src/downstream.c index 8846fd4..952a347 100644 --- a/src/downstream.c +++ b/src/downstream.c @@ -121,6 +121,9 @@ void listenerOnMessage(void * msg, size_t msgSize) ParodusInfo("Received downstream dest as :%s and transaction_uuid :%s\n", dest, ((WRP_MSG_TYPE__REQ == msgType) ? message->u.req.transaction_uuid : ((WRP_MSG_TYPE__EVENT == msgType) ? "NA" : message->u.crud.transaction_uuid))); + OnboardLog("%s\n", + ((WRP_MSG_TYPE__REQ == msgType) ? message->u.req.transaction_uuid : + ((WRP_MSG_TYPE__EVENT == msgType) ? "NA" : message->u.crud.transaction_uuid))); free(destVal); diff --git a/src/main.c b/src/main.c index b898306..b03870c 100644 --- a/src/main.c +++ b/src/main.c @@ -83,7 +83,6 @@ int main( int argc, char **argv) abort(); } curl_global_init(CURL_GLOBAL_DEFAULT); - getAuthToken(cfg); createSocketConnection( NULL); diff --git a/src/networking.c b/src/networking.c index 75ba0c6..83a4657 100644 --- a/src/networking.c +++ b/src/networking.c @@ -70,6 +70,7 @@ int checkHostIp(char * serverIP) if (retVal != 0) { ParodusError("getaddrinfo: %s\n", gai_strerror(retVal)); + OnboardLog("getaddrinfo: %s\n", gai_strerror(retVal)); } else { diff --git a/src/nopoll_handlers.c b/src/nopoll_handlers.c index 476fdc7..9a722f0 100644 --- a/src/nopoll_handlers.c +++ b/src/nopoll_handlers.c @@ -167,12 +167,14 @@ void listenerOnCloseMessage (noPollCtx * ctx, noPollConn * conn, noPollPtr user_ if( closeStatus == 1006 && !get_global_reconnect_status()) { ParodusInfo("Reconnect detected, setting default Reconnect reason %s\n",defaultReason); + OnboardLog("Reconnect detected, setting default Reconnect reason %s\n",defaultReason); set_global_reconnect_reason(defaultReason); set_global_reconnect_status(true); } else if(!get_global_reconnect_status()) { ParodusInfo("Reconnect detected, setting Reconnect reason as Unknown\n"); + OnboardLog("Reconnect detected, setting Reconnect reason as Unknown\n"); set_global_reconnect_reason("Unknown"); } diff --git a/src/nopoll_helpers.c b/src/nopoll_helpers.c index 055a3c6..524cc67 100644 --- a/src/nopoll_helpers.c +++ b/src/nopoll_helpers.c @@ -73,6 +73,7 @@ void sendMessage(noPollConn *conn, void *msg, size_t len) else { ParodusError("Failed to send msg upstream as connection is not OK\n"); + OnboardLog("Failed to send msg upstream as connection is not OK\n"); if (connErr == 0) { getCurrentTime(connStuck_startPtr); @@ -88,6 +89,7 @@ void sendMessage(noPollConn *conn, void *msg, size_t len) if( timeDiff >= (10*60*1000)) { ParodusError("conn got stuck for over 10 minutes; crashing service.\n"); + OnboardLog("conn got stuck for over 10 minutes; crashing service.\n"); kill(getpid(),SIGTERM); } @@ -149,6 +151,7 @@ void __report_log (noPollCtx * ctx, noPollDebugLevel level, const char * log_msg if (level == NOPOLL_LEVEL_CRITICAL) { ParodusError("%s\n", log_msg ); + OnboardLog("%s\n", log_msg ); } return; } diff --git a/src/parodus_log.h b/src/parodus_log.h index 2c0ec00..6b0ee7f 100644 --- a/src/parodus_log.h +++ b/src/parodus_log.h @@ -33,3 +33,9 @@ #define ParodusError(...) cimplog_error(LOGGING_MODULE, __VA_ARGS__) #define ParodusInfo(...) cimplog_info(LOGGING_MODULE, __VA_ARGS__) #define ParodusPrint(...) cimplog_debug(LOGGING_MODULE, __VA_ARGS__) +#ifdef FEATURE_SUPPORT_ONBOARD_LOGGING +#define OnboardLog(...) onboarding_log(LOGGING_MODULE, __VA_ARGS__) +#else +#define OnboardLog(...) +#endif + diff --git a/src/partners_check.c b/src/partners_check.c index 7aedcef..2c46d0c 100644 --- a/src/partners_check.c +++ b/src/partners_check.c @@ -178,6 +178,7 @@ int validate_partner_id(wrp_msg_t *msg, partners_t **partnerIds) if(matchFlag != 1) { ParodusError("Invalid partner_id %s\n",temp); + OnboardLog("Invalid partner_id %s\n",temp); if(partnersList != NULL) { for(j=0; jcount; j++) diff --git a/src/token.c b/src/token.c index 1dfc691..8926b06 100644 --- a/src/token.c +++ b/src/token.c @@ -139,6 +139,7 @@ int analyze_jwt (const cjwt_t *jwt, char **url_buf, unsigned int *port) show_times (exp_time, cur_time); if (exp_time < cur_time) { ParodusError ("JWT has expired\n"); + OnboardLog ("JWT has expired\n"); return TOKEN_ERR_JWT_EXPIRED; } } @@ -146,6 +147,7 @@ int analyze_jwt (const cjwt_t *jwt, char **url_buf, unsigned int *port) url_buf, port); if (http_match < 0) { ParodusError ("Invalid endpoint claim in JWT\n"); + OnboardLog("Invalid endpoint claim in JWT\n"); return TOKEN_ERR_BAD_ENDPOINT; } ParodusInfo ("JWT is_http strncmp: %d\n", http_match); diff --git a/src/upstream.c b/src/upstream.c index 7b75d9c..799427e 100644 --- a/src/upstream.c +++ b/src/upstream.c @@ -589,6 +589,7 @@ void sendUpstreamMsgToServer(void **resp_bytes, size_t resp_size) else { ParodusInfo("close_retry is %d, unable to send response as connection retry is in progress\n", close_retry); + OnboardLog("close_retry is %d, unable to send response as connection retry is in progress\n", close_retry); } free(appendData); appendData =NULL; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a1195a4..9117421 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -193,6 +193,17 @@ 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) +target_link_libraries (test_auth_token_more -lcmocka + -Wl,--no-as-needed -lcimplog + -lcjson -lcjwt -ltrower-base64 -lssl -lcrypto -lrt -lm -lcurl -luuid +) + #------------------------------------------------------------------------------- # test_crud_interface #------------------------------------------------------------------------------- diff --git a/tests/test_auth_token.c b/tests/test_auth_token.c index d73d73c..38f3b04 100644 --- a/tests/test_auth_token.c +++ b/tests/test_auth_token.c @@ -40,11 +40,19 @@ typedef enum { CURLINFO_TOTAL_TIME } CURLINFO; +struct token_data test_data; + int curl_easy_perform(CURL *curl) { UNUSED(curl); + char *msg = "response"; + int rtn; + function_called(); - return (int) mock(); + rtn = (int) mock(); + if (0 == rtn) + write_callback_fn (msg, 1, strlen(msg), &test_data); + return rtn; } int curl_easy_getinfo(CURL *curl, CURLINFO CURLINFO_RESPONSE_CODE, long response_code) @@ -67,6 +75,7 @@ void getAuthToken_Null() getAuthToken(&cfg); set_parodus_cfg(&cfg); assert( cfg.client_cert_path == NULL); + assert_int_equal (0, (int) cfg.webpa_auth_token[0]); } void getAuthToken_MacNull() @@ -77,8 +86,10 @@ void getAuthToken_MacNull() getAuthToken(&cfg); set_parodus_cfg(&cfg); assert( cfg.client_cert_path == NULL); + assert_int_equal (0, (int) cfg.webpa_auth_token[0]); } +#if 0 void test_requestNewAuthToken_init_fail () { char token[32]; @@ -106,7 +117,7 @@ void test_requestNewAuthToken_init_fail () assert_int_equal (output, -1); free(cfg.token_server_url); } - +#endif void test_requestNewAuthToken_failure () { @@ -121,6 +132,8 @@ void test_requestNewAuthToken_failure () parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac)); set_parodus_cfg(&cfg); + test_data.size = 0; + test_data.data = token; will_return (curl_easy_perform, -1); expect_function_calls (curl_easy_perform, 1); @@ -132,6 +145,7 @@ void test_requestNewAuthToken_failure () requestNewAuthToken (token, sizeof(token), 2); assert_int_equal (output, -1); + assert_int_equal (0, (int) token[0]); free(cfg.token_server_url); } @@ -150,6 +164,8 @@ void test_requestNewAuthToken () parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac)); set_parodus_cfg(&cfg); + test_data.size = 0; + test_data.data = token; will_return (curl_easy_perform, 0); expect_function_calls (curl_easy_perform, 1); will_return (curl_easy_getinfo, 0); @@ -160,6 +176,7 @@ void test_requestNewAuthToken () output = requestNewAuthToken (token, sizeof(token), 1); assert_int_equal (output, 0); + assert_string_equal (token, "response"); free(cfg.token_server_url); } @@ -197,6 +214,8 @@ void test_getAuthToken () expect_function_calls (curl_easy_getinfo, 1); /* To test curl success case */ + test_data.size = 0; + test_data.data = cfg.webpa_auth_token; will_return (curl_easy_perform, 0); expect_function_calls (curl_easy_perform, 1); @@ -207,7 +226,7 @@ void test_getAuthToken () expect_function_calls (curl_easy_getinfo, 1); getAuthToken(&cfg); - + assert_string_equal (cfg.webpa_auth_token, "response"); free(cfg.client_cert_path); free(cfg.token_server_url); } @@ -224,6 +243,7 @@ void test_getAuthTokenFailure () 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); @@ -261,20 +281,35 @@ void test_getAuthTokenFailure () void test_write_callback_fn () { - void *buffer; - size_t size = 1; - size_t nmemb =8; + ParodusCfg *cfg; + size_t max_data_size = sizeof (cfg->webpa_auth_token); + char *buffer1 = "response1"; + size_t buf1len = strlen(buffer1); + char *buffer2 = "R2"; + size_t buf2len = strlen(buffer2); + char buffer3[max_data_size]; int out_len=0; struct token_data data; data.size = 0; - buffer = strdup("response"); - data.data = (char *) malloc(sizeof(char) * 5); + data.data = (char *) malloc(max_data_size); 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)); + out_len = write_callback_fn(buffer1, 1, buf1len, &data); + assert_string_equal(data.data, buffer1); + assert_int_equal (out_len, buf1len); + assert_int_equal (data.size, buf1len); + + out_len = write_callback_fn(buffer2, 1, buf2len, &data); + assert_string_equal(data.data, "response1R2"); + assert_int_equal (out_len, buf2len); + assert_int_equal (data.size, buf1len+buf2len); + + memset (buffer3, 'x', max_data_size); + out_len = write_callback_fn(buffer3, 1, max_data_size, &data); + assert_int_equal (out_len, 0); + assert_int_equal (data.size, 0); + free(data.data); } @@ -285,14 +320,14 @@ void test_write_callback_fn () int main(void) { const struct CMUnitTest tests[] = { + cmocka_unit_test(test_write_callback_fn), cmocka_unit_test(test_requestNewAuthToken), - cmocka_unit_test(test_requestNewAuthToken_init_fail), + // 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_auth_token_more.c b/tests/test_auth_token_more.c new file mode 100644 index 0000000..343164e --- /dev/null +++ b/tests/test_auth_token_more.c @@ -0,0 +1,109 @@ +/** + * Copyright 2010-2016 Comcast Cable Communications Management, LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "../src/config.h" +#include "../src/auth_token.h" +#include "../src/ParodusInternal.h" + +extern int requestNewAuthToken(char *newToken, size_t len, int r_count); + +/*----------------------------------------------------------------------------*/ +/* Mocks */ +/*----------------------------------------------------------------------------*/ +typedef void CURL; + +typedef enum { + CURLINFO_RESPONSE_CODE = 2, + CURLINFO_TOTAL_TIME +} CURLINFO; + +struct token_data test_data; + +CURL *curl_easy_init () +{ + function_called(); + return (CURL *) mock(); +} + +int curl_easy_perform(CURL *curl) +{ + UNUSED(curl); + char *msg = "response"; + int rtn; + + function_called(); + rtn = (int) mock(); + if (0 == rtn) + write_callback_fn (msg, 1, strlen(msg), &test_data); + return rtn; +} + +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 */ +/*----------------------------------------------------------------------------*/ +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_init, NULL); + expect_function_calls (curl_easy_init, 1); + + requestNewAuthToken (token, sizeof(token), 2); + assert_int_equal (output, -1); + assert_int_equal (0, (int) token[0]); + free(cfg.token_server_url); +} + +/*----------------------------------------------------------------------------*/ +/* External Functions */ +/*----------------------------------------------------------------------------*/ + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_requestNewAuthToken_init_fail) + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/tests/test_connection.c b/tests/test_connection.c index b14e69f..d144162 100644 --- a/tests/test_connection.c +++ b/tests/test_connection.c @@ -117,6 +117,8 @@ nopoll_bool nopoll_conn_wait_for_status_until_connection_ready (noPollConn * co int timeout, int *status, char ** message) { UNUSED(conn); UNUSED(timeout); + + *message = NULL; if (mock_wait_status >= 1000) { *status = mock_wait_status / 1000; mock_wait_status = mock_wait_status % 1000; @@ -375,6 +377,15 @@ void test_set_current_server() assert_ptr_equal (&ctx.server_list.defaults, ctx.current_server); } +void init_cfg_header_info (ParodusCfg *cfg) +{ + 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->webpa_protocol , "WebPA-1.6", sizeof(cfg->webpa_protocol)); +} + void test_set_extra_headers () { int rtn; @@ -391,12 +402,8 @@ void test_set_extra_headers () 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)); + init_cfg_header_info (&cfg); 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); rtn = init_header_info (&ctx.header_info); @@ -578,19 +585,12 @@ void test_nopoll_connect () // Return codes for wait_connection_ready #define WAIT_SUCCESS 0 -#define WAIT_ACTION_RETRY 1 // if wait_status is 307, 302, 303 or 403 +#define WAIT_ACTION_RETRY 1 // if wait_status is 307, 302, 303, or 403 #define WAIT_FAIL 2 void test_wait_connection_ready () { create_connection_ctx_t ctx; - ParodusCfg Cfg; - const char *expected_extra_headers = - "\r\nAuthorization: Bearer Auth---" - "\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.;)" - "\r\nX-WebPA-Convey: WebPA-1.6 (TG1682)"; memset(&ctx,0,sizeof(ctx)); set_server_list_null (&ctx.server_list); @@ -605,7 +605,7 @@ void test_wait_connection_ready () will_return (nopoll_conn_wait_for_status_until_connection_ready, nopoll_false); expect_function_call (nopoll_conn_wait_for_status_until_connection_ready); assert_int_equal (wait_connection_ready (&ctx), WAIT_FAIL); - + mock_wait_status = 503; will_return (nopoll_conn_wait_for_status_until_connection_ready, nopoll_false); expect_function_call (nopoll_conn_wait_for_status_until_connection_ready); @@ -640,30 +640,18 @@ void test_wait_connection_ready () free_server (&ctx.server_list.redirect); mock_wait_status = 403; - memset(&Cfg, 0, sizeof(ParodusCfg)); - - parStrncpy (Cfg.webpa_auth_token, "Auth---", sizeof (Cfg.webpa_auth_token)); - parStrncpy(Cfg.hw_model, "TG1682", sizeof(Cfg.hw_model)); - parStrncpy(Cfg.hw_manufacturer , "ARRISGroup,Inc.", sizeof(Cfg.hw_manufacturer)); - parStrncpy(Cfg.hw_mac , "123567892366", sizeof(Cfg.hw_mac)); - parStrncpy(Cfg.fw_name , "2.364s2", sizeof(Cfg.fw_name)); - parStrncpy(Cfg.webpa_protocol , "WebPA-1.6", sizeof(Cfg.webpa_protocol)); - set_parodus_cfg(&Cfg); - - init_header_info (&ctx.header_info); will_return (nopoll_conn_wait_for_status_until_connection_ready, nopoll_false); expect_function_call (nopoll_conn_wait_for_status_until_connection_ready); assert_int_equal (wait_connection_ready (&ctx), WAIT_ACTION_RETRY); - - assert_string_equal (ctx.extra_headers, expected_extra_headers); + free_extra_headers (&ctx); free_header_info (&ctx.header_info); } // Return codes for connect_and_wait #define CONN_WAIT_SUCCESS 0 -#define CONN_WAIT_ACTION_RETRY 1 // if wait_status is 307, 302, 303 or 403 -#define CONN_WAIT_RETRY_DNS 2 +#define CONN_WAIT_ACTION_RETRY 1 // if wait_status is 307, 302, 303, or 403 +#define CONN_WAIT_RETRY_DNS 2 void test_connect_and_wait () { @@ -815,22 +803,16 @@ void test_keep_trying () server_t test_server; backoff_timer_t backoff_timer; ParodusCfg Cfg; - char *test_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.;)" - "\r\nX-WebPA-Convey: WebPA-1.6 (TG1682)"; memset(&Cfg, 0, sizeof(ParodusCfg)); parStrncpy (Cfg.webpa_url, "http://mydns.mycom.net:8080", sizeof(Cfg.webpa_url)); + init_cfg_header_info (&Cfg); mock_wait_status = 0; memset(&ctx,0,sizeof(ctx)); ctx.nopoll_ctx = &test_nopoll_ctx; ctx.current_server = &test_server; - ctx.extra_headers = test_extra_headers; test_server.allow_insecure = 1; test_server.server_addr = "mydns.mycom.net";