From ee9d7684c657699b3bcb7c7ef2cfa4fe5bd411e0 Mon Sep 17 00:00:00 2001 From: shilpa24balaji Date: Wed, 23 May 2018 17:40:04 -0700 Subject: [PATCH] Revert "Remove static char arrays from ParodusCfg structure. " --- src/ParodusInternal.c | 31 +++-- src/config.c | 247 +++++++++++++--------------------- src/config.h | 52 +++---- src/connection.c | 78 +++++------ src/main.c | 1 - src/partners_check.c | 2 +- tests/CMakeLists.txt | 1 - tests/simple_connection.c | 25 ++-- tests/test_ParodusInternal.c | 23 ++-- tests/test_config.c | 162 ++++++++-------------- tests/test_conn_interface.c | 23 ++-- tests/test_createConnection.c | 36 ++--- tests/test_partners_check.c | 18 +-- tests/test_seshat_interface.c | 7 +- tests/test_token.c | 29 ++-- 15 files changed, 286 insertions(+), 449 deletions(-) diff --git a/src/ParodusInternal.c b/src/ParodusInternal.c index 5a13bf0..d9a4d6b 100644 --- a/src/ParodusInternal.c +++ b/src/ParodusInternal.c @@ -36,43 +36,42 @@ char* getWebpaConveyHeader() int encodedDataSize = 1024; char * reconnect_reason = get_global_reconnect_reason(); int i =0, j=0; - ParodusCfg *cfg = get_parodus_cfg(); - if(cfg->hw_model && strlen(cfg->hw_model)!=0) + if(strlen(get_parodus_cfg()->hw_model)!=0) { - cJSON_AddStringToObject(response, HW_MODELNAME, cfg->hw_model); + cJSON_AddStringToObject(response, HW_MODELNAME, get_parodus_cfg()->hw_model); } - if(cfg->hw_serial_number && strlen(cfg->hw_serial_number)!=0) + if(strlen(get_parodus_cfg()->hw_serial_number)!=0) { - cJSON_AddStringToObject(response, HW_SERIALNUMBER, cfg->hw_serial_number); + cJSON_AddStringToObject(response, HW_SERIALNUMBER, get_parodus_cfg()->hw_serial_number); } - if(cfg->hw_manufacturer && strlen(cfg->hw_manufacturer)!=0) + if(strlen(get_parodus_cfg()->hw_manufacturer)!=0) { - cJSON_AddStringToObject(response, HW_MANUFACTURER, cfg->hw_manufacturer); + cJSON_AddStringToObject(response, HW_MANUFACTURER, get_parodus_cfg()->hw_manufacturer); } - if(cfg->fw_name && strlen(cfg->fw_name)!=0) + if(strlen(get_parodus_cfg()->fw_name)!=0) { - cJSON_AddStringToObject(response, FIRMWARE_NAME, cfg->fw_name); + cJSON_AddStringToObject(response, FIRMWARE_NAME, get_parodus_cfg()->fw_name); } - cJSON_AddNumberToObject(response, BOOT_TIME, cfg->boot_time); + cJSON_AddNumberToObject(response, BOOT_TIME, get_parodus_cfg()->boot_time); - if(cfg->webpa_protocol && strlen(cfg->webpa_protocol)!=0) + if(strlen(get_parodus_cfg()->webpa_protocol)!=0) { - cJSON_AddStringToObject(response, WEBPA_PROTOCOL, cfg->webpa_protocol); + cJSON_AddStringToObject(response, WEBPA_PROTOCOL, get_parodus_cfg()->webpa_protocol); } - if(cfg->webpa_interface_used && strlen(cfg->webpa_interface_used)!=0) + if(strlen(get_parodus_cfg()->webpa_interface_used)!=0) { - cJSON_AddStringToObject(response, WEBPA_INTERFACE, cfg->webpa_interface_used); + cJSON_AddStringToObject(response, WEBPA_INTERFACE, get_parodus_cfg()->webpa_interface_used); } - if(cfg->hw_last_reboot_reason && strlen(cfg->hw_last_reboot_reason)!=0) + if(strlen(get_parodus_cfg()->hw_last_reboot_reason)!=0) { - cJSON_AddStringToObject(response, HW_LAST_REBOOT_REASON, cfg->hw_last_reboot_reason); + cJSON_AddStringToObject(response, HW_LAST_REBOOT_REASON, get_parodus_cfg()->hw_last_reboot_reason); } else { diff --git a/src/config.c b/src/config.c index 8ab8483..1971c93 100644 --- a/src/config.c +++ b/src/config.c @@ -28,7 +28,6 @@ #include #define MAX_BUF_SIZE 128 -#define PROTOCOL_STR_LEN 1024 /*----------------------------------------------------------------------------*/ /* File Scoped Variables */ @@ -38,7 +37,6 @@ static ParodusCfg parodusCfg; static unsigned int rsa_algorithms = (1<flags = 0; + parStrncpy (cfg->webpa_url, "", sizeof(cfg->webpa_url)); cfg->acquire_jwt = 0; cfg->jwt_algo = 0; + parStrncpy (cfg->jwt_key, "", sizeof(cfg->jwt_key)); optind = 1; /* We need this if parseCommandLine is called again */ - int option_index = 0;/* getopt_long stores the option index here. */ while (1) { + + /* getopt_long stores the option index here. */ + int option_index = 0; c = getopt_long (argc, argv, "m:s:f:d:r:n:b:u:t:o:i:l:p:e:D:j:a:k:c:T:J:46", long_options, &option_index); @@ -344,45 +329,41 @@ int parseCommandLine(int argc,char **argv,ParodusCfg * cfg) switch (c) { case 'm': - cfg->hw_model = strdup (optarg); + parStrncpy(cfg->hw_model, optarg,sizeof(cfg->hw_model)); ParodusInfo("hw-model is %s\n",cfg->hw_model); break; case 's': - cfg->hw_serial_number = strdup (optarg); + parStrncpy(cfg->hw_serial_number,optarg,sizeof(cfg->hw_serial_number)); ParodusInfo("hw_serial_number is %s\n",cfg->hw_serial_number); break; case 'f': - cfg->hw_manufacturer = strdup (optarg); + parStrncpy(cfg->hw_manufacturer, optarg,sizeof(cfg->hw_manufacturer)); ParodusInfo("hw_manufacturer is %s\n",cfg->hw_manufacturer); break; case 'd': - { - char mac[16]; - if (parse_mac_address (&mac[0], optarg) == 0) { - cfg->hw_mac = strdup(mac); - ParodusInfo ("hw_mac is %s\n",cfg->hw_mac); - } else { - ParodusError ("Bad mac address %s\n", optarg); - return -1; - } - } + if (parse_mac_address (cfg->hw_mac, optarg) == 0) { + ParodusInfo ("hw_mac is %s\n",cfg->hw_mac); + } else { + ParodusError ("Bad mac address %s\n", optarg); + return -1; + } break; #ifdef ENABLE_SESHAT case 'e': - cfg->seshat_url = strdup (optarg); + parStrncpy(cfg->seshat_url, optarg,sizeof(cfg->seshat_url)); ParodusInfo("seshat_url is %s\n",cfg->seshat_url); break; #endif case 'r': - cfg->hw_last_reboot_reason = strdup (optarg); + 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); break; case 'n': - cfg->fw_name = strdup (optarg); + parStrncpy(cfg->fw_name, optarg,sizeof(cfg->fw_name)); ParodusInfo("fw_name is %s\n",cfg->fw_name); break; @@ -394,7 +375,7 @@ int parseCommandLine(int argc,char **argv,ParodusCfg * cfg) break; case 'u': - cfg->webpa_url = strdup (optarg); + parStrncpy(cfg->webpa_url, optarg,sizeof(cfg->webpa_url)); if (server_is_http (cfg->webpa_url, NULL) < 0) { ParodusError ("Bad webpa url %s\n", optarg); return -1; @@ -417,25 +398,19 @@ int parseCommandLine(int argc,char **argv,ParodusCfg * cfg) break; case 'i': - cfg->webpa_interface_used = strdup (optarg); + parStrncpy(cfg->webpa_interface_used, optarg,sizeof(cfg->webpa_interface_used)); ParodusInfo("webpa_interface_used is %s\n",cfg->webpa_interface_used); break; case 'l': - if (NULL != cfg->local_url) {// free default url - free(cfg->local_url); - } - cfg->local_url = strdup (optarg); + parStrncpy(cfg->local_url, optarg,sizeof(cfg->local_url)); ParodusInfo("parodus local_url is %s\n",cfg->local_url); break; case 'D': // like 'fabric' or 'test' // this parameter is used, along with the hw_mac parameter // to create the dns txt record id - if (NULL != cfg->dns_txt_url) {// free default url - free(cfg->dns_txt_url); - } - cfg->dns_txt_url = strdup (optarg); + parStrncpy(cfg->dns_txt_url, optarg,sizeof(cfg->dns_txt_url)); ParodusInfo("parodus dns-txt-url is %s\n",cfg->dns_txt_url); break; @@ -456,22 +431,16 @@ int parseCommandLine(int argc,char **argv,ParodusCfg * cfg) ParodusInfo("jwt_algo is %u\n",cfg->jwt_algo); break; case 'k': - if (NULL != cfg->jwt_key) {// free default jwt_key - free(cfg->jwt_key); - } - read_key_from_file (optarg, &cfg->jwt_key); + read_key_from_file (optarg, cfg->jwt_key, sizeof(cfg->jwt_key)); ParodusInfo("jwt_key is %s\n",cfg->jwt_key); break; case 'p': - cfg->partner_id = strdup (optarg); + parStrncpy(cfg->partner_id, optarg,sizeof(cfg->partner_id)); ParodusInfo("partner_id is %s\n",cfg->partner_id); break; case 'c': - if (NULL != cfg->cert_path) {// free default cert_path - free(cfg->cert_path); - } - cfg->cert_path = strdup (optarg); + parStrncpy(cfg->cert_path, optarg,sizeof(cfg->cert_path)); ParodusInfo("cert_path is %s\n",cfg->cert_path); break; @@ -486,16 +455,16 @@ int parseCommandLine(int argc,char **argv,ParodusCfg * cfg) break; case 'J': - cfg->token_acquisition_script = strdup (optarg); + parStrncpy(cfg->token_acquisition_script, optarg,sizeof(cfg->token_acquisition_script)); break; case 'T': - cfg->token_read_script = strdup(optarg); + parStrncpy(cfg->token_read_script, optarg,sizeof(cfg->token_read_script)); break; case '?': - ParodusError("Unrecognized option %s Aborting ...\n", argv[optind]); - return -1; + /* getopt_long already printed an error message. */ + break; default: ParodusError("Enter Valid commands..\n"); @@ -503,7 +472,7 @@ int parseCommandLine(int argc,char **argv,ParodusCfg * cfg) } } - if (cfg->webpa_url && (0 == strlen (cfg->webpa_url))) { + if (0 == strlen (cfg->webpa_url)) { ParodusError ("Missing webpa url argument\n"); return -1; } @@ -513,13 +482,11 @@ int parseCommandLine(int argc,char **argv,ParodusCfg * cfg) ParodusError ("Missing jwt algorithm argument\n"); return -1; } - - if (cfg->jwt_algo & rsa_algorithms) { - if (!cfg->jwt_key || (0 == strlen (cfg->jwt_key)) ) { - ParodusError ("Missing jwt public key file argument\n"); - return -1; - } - } + if ((0 != (cfg->jwt_algo & rsa_algorithms)) && + (0 == strlen (cfg->jwt_key)) ) { + ParodusError ("Missing jwt public key file argument\n"); + return -1; + } } @@ -585,7 +552,7 @@ void getAuthToken(ParodusCfg *cfg) else { ParodusInfo("update cfg->webpa_auth_token in success case\n"); - cfg->webpa_auth_token = strdup(output); + parStrncpy(cfg->webpa_auth_token, output, sizeof(cfg->webpa_auth_token)); } } else @@ -596,7 +563,6 @@ void getAuthToken(ParodusCfg *cfg) void setDefaultValuesToCfg(ParodusCfg *cfg) { - char protocol_str[PROTOCOL_STR_LEN]; if(cfg == NULL) { ParodusError("cfg is NULL\n"); @@ -604,128 +570,125 @@ void setDefaultValuesToCfg(ParodusCfg *cfg) } ParodusInfo("Setting default values to parodusCfg\n"); - cfg->local_url = strdup (PARODUS_UPSTREAM); + parStrncpy(cfg->local_url, PARODUS_UPSTREAM, sizeof(cfg->local_url)); cfg->acquire_jwt = 0; - cfg->dns_txt_url = strdup (DNS_TXT_URL); + parStrncpy(cfg->dns_txt_url, DNS_TXT_URL, sizeof(cfg->dns_txt_url)); - cfg->jwt_key = strdup ("\0"); + parStrncpy(cfg->jwt_key, "\0", sizeof(cfg->jwt_key)); cfg->jwt_algo = 0; - cfg->cert_path = NULL; + parStrncpy(cfg->cert_path, "\0", sizeof(cfg->cert_path)); cfg->flags = 0; - cfg->webpa_path_url = strdup (WEBPA_PATH_URL); + parStrncpy(cfg->webpa_path_url, WEBPA_PATH_URL,sizeof(cfg->webpa_path_url)); - snprintf(protocol_str, PROTOCOL_STR_LEN, "%s-%s", PROTOCOL_VALUE, GIT_COMMIT_TAG); - cfg->webpa_protocol = strdup(protocol_str); + snprintf(cfg->webpa_protocol, sizeof(cfg->webpa_protocol), "%s-%s", PROTOCOL_VALUE, GIT_COMMIT_TAG); ParodusInfo(" cfg->webpa_protocol is %s\n", cfg->webpa_protocol); - cfg->webpa_uuid = strdup ("1234567-345456546"); + parStrncpy(cfg->webpa_uuid, "1234567-345456546",sizeof(cfg->webpa_uuid)); ParodusPrint("cfg->webpa_uuid is :%s\n", cfg->webpa_uuid); } void loadParodusCfg(ParodusCfg * config,ParodusCfg *cfg) { - char protocol_str[PROTOCOL_STR_LEN]; - if(config == NULL) { ParodusError("config is NULL\n"); return; } - if(config->hw_model && strlen (config->hw_model) !=0) + if(strlen (config->hw_model) !=0) { - cfg->hw_model = strdup (config->hw_model); + parStrncpy(cfg->hw_model, config->hw_model, sizeof(cfg->hw_model)); } else { ParodusPrint("hw_model is NULL. read from tmp file\n"); } - if( config->hw_serial_number && strlen(config->hw_serial_number) !=0) + if( strlen(config->hw_serial_number) !=0) { - cfg->hw_serial_number = strdup (config->hw_serial_number); + parStrncpy(cfg->hw_serial_number, config->hw_serial_number, sizeof(cfg->hw_serial_number)); } else { ParodusPrint("hw_serial_number is NULL. read from tmp file\n"); } - if(config->hw_manufacturer && strlen(config->hw_manufacturer) !=0) + if(strlen(config->hw_manufacturer) !=0) { - cfg->hw_manufacturer = strdup (config->hw_manufacturer); + parStrncpy(cfg->hw_manufacturer, config->hw_manufacturer,sizeof(cfg->hw_manufacturer)); } else { ParodusPrint("hw_manufacturer is NULL. read from tmp file\n"); } - if(config->hw_mac && strlen(config->hw_mac) !=0) + if(strlen(config->hw_mac) !=0) { - cfg->hw_mac = strdup (config->hw_mac); + parStrncpy(cfg->hw_mac, config->hw_mac,sizeof(cfg->hw_mac)); } else { ParodusPrint("hw_mac is NULL. read from tmp file\n"); } - if(config->hw_last_reboot_reason && strlen (config->hw_last_reboot_reason) !=0) + if(strlen (config->hw_last_reboot_reason) !=0) { - cfg->hw_last_reboot_reason = strdup (config->hw_last_reboot_reason); + parStrncpy(cfg->hw_last_reboot_reason, config->hw_last_reboot_reason,sizeof(cfg->hw_last_reboot_reason)); } else { ParodusPrint("hw_last_reboot_reason is NULL. read from tmp file\n"); } - if(config->fw_name && strlen(config->fw_name) !=0) + if(strlen(config->fw_name) !=0) { - cfg->fw_name = strdup (config->fw_name); + parStrncpy(cfg->fw_name, config->fw_name,sizeof(cfg->fw_name)); } else { ParodusPrint("fw_name is NULL. read from tmp file\n"); } - if( config->webpa_url && strlen(config->webpa_url) !=0) + if( strlen(config->webpa_url) !=0) { - cfg->webpa_url = strdup (config->webpa_url); + parStrncpy(cfg->webpa_url, config->webpa_url,sizeof(cfg->webpa_url)); } else { ParodusPrint("webpa_url is NULL. read from tmp file\n"); } - if(config->webpa_interface_used && strlen(config->webpa_interface_used )!=0) + if(strlen(config->webpa_interface_used )!=0) { - cfg->webpa_interface_used = strdup (config->webpa_interface_used); + parStrncpy(cfg->webpa_interface_used, config->webpa_interface_used,sizeof(cfg->webpa_interface_used)); } else { ParodusPrint("webpa_interface_used is NULL. read from tmp file\n"); } - if( config->local_url && strlen(config->local_url) !=0) + if( strlen(config->local_url) !=0) { - cfg->local_url = strdup (config->local_url); + parStrncpy(cfg->local_url, config->local_url,sizeof(cfg->local_url)); } else { ParodusInfo("parodus local_url is NULL. adding default url\n"); - cfg->local_url = strdup (PARODUS_UPSTREAM); + parStrncpy(cfg->local_url, PARODUS_UPSTREAM, sizeof(cfg->local_url)); } - if( config->partner_id && strlen(config->partner_id) !=0) + if( strlen(config->partner_id) !=0) { - cfg->partner_id = strdup (config->partner_id); + parStrncpy(cfg->partner_id, config->partner_id,sizeof(cfg->partner_id)); } else { ParodusPrint("partner_id is NULL. read from tmp file\n"); } #ifdef ENABLE_SESHAT - if( config->seshat_url && strlen(config->seshat_url) !=0) + if( strlen(config->seshat_url) !=0) { - cfg->seshat_url = strdup (config->seshat_url); + parStrncpy(cfg->seshat_url, config->seshat_url,sizeof(cfg->seshat_url)); } else { @@ -734,50 +697,50 @@ void loadParodusCfg(ParodusCfg * config,ParodusCfg *cfg) #endif cfg->acquire_jwt = config->acquire_jwt; - if( config->dns_txt_url && strlen(config->dns_txt_url) !=0) + if( strlen(config->dns_txt_url) !=0) { - cfg->dns_txt_url = strdup (config->dns_txt_url); + parStrncpy(cfg->dns_txt_url, config->dns_txt_url, sizeof(cfg->dns_txt_url)); } else { - ParodusInfo("parodus dns-txt-url is NULL. adding default\n"); - cfg->dns_txt_url = strdup (DNS_TXT_URL); + ParodusInfo("parodus dns-txt-url is NULL. adding default\n"); + parStrncpy(cfg->dns_txt_url, DNS_TXT_URL, sizeof(cfg->dns_txt_url)); } - if(config->jwt_key && strlen(config->jwt_key )!=0) + if(strlen(config->jwt_key )!=0) { - cfg->jwt_key = strdup (config->jwt_key); + parStrncpy(cfg->jwt_key, config->jwt_key,sizeof(cfg->jwt_key)); } else { - cfg->jwt_key = strdup ("\0"); + parStrncpy(cfg->jwt_key, "\0", sizeof(cfg->jwt_key)); ParodusPrint("jwt_key is NULL. set to empty\n"); } cfg->jwt_algo = config->jwt_algo; - if(config->cert_path && strlen(config->cert_path )!=0) + if(strlen(config->cert_path )!=0) { - cfg->cert_path = strdup (config->cert_path); + parStrncpy(cfg->cert_path, config->cert_path,sizeof(cfg->cert_path)); } else { - cfg->cert_path = strdup ("\0"); + parStrncpy(cfg->cert_path, "\0", sizeof(cfg->cert_path)); ParodusPrint("cert_path is NULL. set to empty\n"); } - if(config->token_acquisition_script && strlen(config->token_acquisition_script )!=0) + if(strlen(config->token_acquisition_script )!=0) { - cfg->token_acquisition_script = strdup (config->token_acquisition_script); + parStrncpy(cfg->token_acquisition_script, config->token_acquisition_script,sizeof(cfg->token_acquisition_script)); } else { ParodusPrint("token_acquisition_script is NULL. read from tmp file\n"); } - if(config->token_read_script && strlen(config->token_read_script )!=0) + if(strlen(config->token_read_script )!=0) { - cfg->token_read_script = strdup (config->token_read_script); + parStrncpy(cfg->token_read_script, config->token_read_script,sizeof(cfg->token_read_script)); } else { @@ -787,36 +750,12 @@ void loadParodusCfg(ParodusCfg * config,ParodusCfg *cfg) cfg->boot_time = config->boot_time; cfg->webpa_ping_timeout = config->webpa_ping_timeout; cfg->webpa_backoff_max = config->webpa_backoff_max; - cfg->webpa_path_url = strdup (WEBPA_PATH_URL); - snprintf(protocol_str, PROTOCOL_STR_LEN, "%s-%s", PROTOCOL_VALUE, GIT_COMMIT_TAG); - cfg->webpa_protocol = strdup (protocol_str); + parStrncpy(cfg->webpa_path_url, WEBPA_PATH_URL,sizeof(cfg->webpa_path_url)); + snprintf(cfg->webpa_protocol, sizeof(cfg->webpa_protocol), "%s-%s", PROTOCOL_VALUE, GIT_COMMIT_TAG); ParodusInfo("cfg->webpa_protocol is %s\n", cfg->webpa_protocol); - cfg->webpa_uuid = strdup ("1234567-345456546"); + parStrncpy(cfg->webpa_uuid, "1234567-345456546",sizeof(cfg->webpa_uuid)); ParodusPrint("cfg->webpa_uuid is :%s\n", cfg->webpa_uuid); + } -void clean_up_parodus_cfg(ParodusCfg *cfg) -{ - if (cfg->hw_model == NULL) free(cfg->hw_model); - if (cfg->hw_serial_number == NULL) free(cfg->hw_serial_number); - if (cfg->hw_manufacturer == NULL) free(cfg->hw_manufacturer); - if (cfg->hw_mac == NULL) free(cfg->hw_mac); - if (cfg->hw_last_reboot_reason == NULL) free(cfg->hw_last_reboot_reason); - if (cfg->fw_name == NULL) free(cfg->fw_name); - if (cfg->webpa_url == NULL) free(cfg->webpa_url); - if (cfg->webpa_path_url == NULL) free(cfg->webpa_path_url); - if (cfg->webpa_interface_used == NULL) free(cfg->webpa_interface_used); - if (cfg->webpa_protocol == NULL) free(cfg->webpa_protocol); - if (cfg->webpa_uuid == NULL) free(cfg->webpa_uuid); - if (cfg->local_url == NULL) free(cfg->local_url); - if (cfg->partner_id == NULL) free(cfg->partner_id); -#ifdef ENABLE_SESHAT - if (cfg->seshat_url == NULL) free(cfg->seshat_url); -#endif - if (cfg->dns_txt_url == NULL) free(cfg->dns_txt_url); - if (cfg->jwt_key == NULL) free(cfg->jwt_key); - if (cfg->cert_path == NULL) free(cfg->cert_path); - if (cfg->webpa_auth_token == NULL) free(cfg->webpa_auth_token); - if (cfg->token_acquisition_script == NULL) free(cfg->token_acquisition_script); - if (cfg->token_read_script == NULL) free(cfg->token_read_script); -} \ No newline at end of file + diff --git a/src/config.h b/src/config.h index ef9b086..747f6a4 100644 --- a/src/config.h +++ b/src/config.h @@ -59,41 +59,40 @@ extern "C" { #define ALLOW_NON_RSA_ALG false - /*----------------------------------------------------------------------------*/ /* Data Structures */ /*----------------------------------------------------------------------------*/ typedef struct { - char *hw_model; - char *hw_serial_number; - char *hw_manufacturer; - char *hw_mac; - char *hw_last_reboot_reason; - char *fw_name; + char hw_model[64]; + char hw_serial_number[64]; + char hw_manufacturer[64]; + char hw_mac[64]; + char hw_last_reboot_reason[64]; + char fw_name[64]; unsigned int boot_time; unsigned int webpa_ping_timeout; - char *webpa_url; - char *webpa_path_url; + char webpa_url[124]; + char webpa_path_url[124]; unsigned int webpa_backoff_max; - char *webpa_interface_used; - char *webpa_protocol; - char *webpa_uuid; + char webpa_interface_used[16]; + char webpa_protocol[32]; + char webpa_uuid[64]; unsigned int flags; - char *local_url; - char *partner_id; + char local_url[124]; + char partner_id[64]; #ifdef ENABLE_SESHAT - char *seshat_url; + char seshat_url[128]; #endif - char *dns_txt_url; + char dns_txt_url[64]; unsigned int acquire_jwt; unsigned int jwt_algo; // bit mask set for each allowed algorithm - char *jwt_key; // may be read in from a pem file - char *cert_path; - char *webpa_auth_token; - char *token_acquisition_script; - char *token_read_script; + char jwt_key[4096]; // may be read in from a pem file + char cert_path[64]; + char webpa_auth_token[4096]; + char token_acquisition_script[64]; + char token_read_script[64]; } ParodusCfg; #define FLAGS_IPV6_ONLY (1 << 0) @@ -122,17 +121,6 @@ void setDefaultValuesToCfg(ParodusCfg *cfg); void getAuthToken(ParodusCfg *cfg); // Accessor for the global config structure. ParodusCfg *get_parodus_cfg(void); - -/* - Called on program exit - */ -void clean_up_parodus_cfg(ParodusCfg *cfg); - -/* File Utility Function */ -/* Mallocs memory for the entire file, caller will have to call free(*data) */ -void read_key_from_file (const char *fname, char **data); - - void set_parodus_cfg(ParodusCfg *); char *get_token_application(void) ; diff --git a/src/connection.c b/src/connection.c index ad966fc..71d0c51 100644 --- a/src/connection.c +++ b/src/connection.c @@ -119,36 +119,32 @@ int createNopollConnection(noPollCtx *ctx) char user_agent[512]={'\0'}; char * extra_headers = NULL; unsigned int fallback = FLAGS_IPV6_ONLY; - ParodusCfg *cfg = get_parodus_cfg(); if(ctx == NULL) { return nopoll_false; } - ParodusPrint("BootTime In sec: %d\n", cfg->boot_time); - ParodusInfo("Received reboot_reason as:%s\n", cfg->hw_last_reboot_reason); + ParodusPrint("BootTime In sec: %d\n", get_parodus_cfg()->boot_time); + ParodusInfo("Received reboot_reason as:%s\n", get_parodus_cfg()->hw_last_reboot_reason); ParodusInfo("Received reconnect_reason as:%s\n", reconnect_reason); - max_retry_sleep = (int) cfg->webpa_backoff_max; + max_retry_sleep = (int) get_parodus_cfg()->webpa_backoff_max; ParodusPrint("max_retry_sleep is %d\n", max_retry_sleep ); snprintf(user_agent, sizeof(user_agent),"%s (%s; %s/%s;)", - ((cfg->webpa_protocol && (0 != strlen(cfg->webpa_protocol)) ) ? - cfg->webpa_protocol : "unknown"), - ((cfg->fw_name && (0 != strlen(cfg->fw_name))) ? cfg->fw_name : "unknown"), - ((cfg->hw_model && (0 != strlen(cfg->hw_model))) ? cfg->hw_model : "unknown"), - ((cfg->hw_manufacturer && (0 != strlen(cfg->hw_manufacturer))) ? - cfg->hw_manufacturer : "unknown") - ); + ((0 != strlen(get_parodus_cfg()->webpa_protocol)) ? get_parodus_cfg()->webpa_protocol : "unknown"), + ((0 != strlen(get_parodus_cfg()->fw_name)) ? get_parodus_cfg()->fw_name : "unknown"), + ((0 != strlen(get_parodus_cfg()->hw_model)) ? get_parodus_cfg()->hw_model : "unknown"), + ((0 != strlen(get_parodus_cfg()->hw_manufacturer)) ? get_parodus_cfg()->hw_manufacturer : "unknown")); ParodusInfo("User-Agent: %s\n",user_agent); conveyHeader = getWebpaConveyHeader(); - parStrncpy(deviceMAC, cfg->hw_mac ? cfg->hw_mac : "000000000000", sizeof(deviceMAC)); + parStrncpy(deviceMAC, get_parodus_cfg()->hw_mac,sizeof(deviceMAC)); snprintf(device_id, sizeof(device_id), "mac:%s", deviceMAC); ParodusInfo("Device_id %s\n",device_id); extra_headers = build_extra_headers( - ( (cfg->webpa_auth_token&& (0 < strlen(cfg->webpa_auth_token))) ? cfg->webpa_auth_token : NULL), + ((0 < strlen(get_parodus_cfg()->webpa_auth_token)) ? get_parodus_cfg()->webpa_auth_token : NULL), device_id, user_agent, conveyHeader ); do @@ -163,13 +159,13 @@ int createNopollConnection(noPollCtx *ctx) //retry jwt validation on query dns failure if((jwt_status == INITIAL_CJWT_RETRY) || (jwt_status == TOKEN_ERR_QUERY_DNS_FAIL)) { - allow_insecure = parse_webpa_url (cfg->webpa_url, + allow_insecure = parse_webpa_url (get_parodus_cfg()->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); if (allow_insecure < 0) return nopoll_false; // must have valid default url #ifdef FEATURE_DNS_QUERY - if (cfg->acquire_jwt) { + if (get_parodus_cfg()->acquire_jwt) { //query dns and validate JWT jwt_status = allow_insecure_conn( server_Address, (int) sizeof(server_Address), @@ -204,7 +200,7 @@ int createNopollConnection(noPollCtx *ctx) ParodusPrint("secure false\n"); noPollConnOpts * opts; opts = createConnOpts(extra_headers, false); - connection = nopoll_conn_new_opts (ctx, opts,server_Address,port,NULL,cfg->webpa_path_url,NULL,NULL);// WEBPA-787 + connection = nopoll_conn_new_opts (ctx, opts,server_Address,port,NULL,get_parodus_cfg()->webpa_path_url,NULL,NULL);// WEBPA-787 } set_global_conn(connection); @@ -215,10 +211,10 @@ int createNopollConnection(noPollCtx *ctx) ParodusError("Error connecting to server\n"); ParodusError("RDK-10037 - WebPA Connection Lost\n"); // Copy the server address from config to avoid retrying to the same failing talaria redirected node - if (cfg->acquire_jwt == 0) + if (get_parodus_cfg()->acquire_jwt == 0) { ParodusInfo("acquire_jwt is 0, retrying with config server address\n"); - allow_insecure = parse_webpa_url (cfg->webpa_url, + allow_insecure = parse_webpa_url (get_parodus_cfg()->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); } @@ -232,7 +228,7 @@ int createNopollConnection(noPollCtx *ctx) else { ParodusError("acquire_jwt is 1 & unable to get jwt_server_url, retrying with config server address\n"); - allow_insecure = parse_webpa_url (cfg->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); + allow_insecure = parse_webpa_url (get_parodus_cfg()->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); } } @@ -270,10 +266,10 @@ int createNopollConnection(noPollCtx *ctx) if (allow_insecure < 0) { ParodusError ("Invalid redirectURL\n"); - if (cfg->acquire_jwt == 0) + if (get_parodus_cfg()->acquire_jwt == 0) { ParodusInfo("acquire_jwt is 0, retrying with config server address\n"); - allow_insecure = parse_webpa_url (cfg->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); + allow_insecure = parse_webpa_url (get_parodus_cfg()->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); } else { @@ -285,7 +281,7 @@ int createNopollConnection(noPollCtx *ctx) else { ParodusError("acquire_jwt is 1 & unable to get jwt_server_url, retrying with config server address\n"); - allow_insecure = parse_webpa_url (cfg->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); + allow_insecure = parse_webpa_url (get_parodus_cfg()->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); } } @@ -300,11 +296,11 @@ int createNopollConnection(noPollCtx *ctx) ParodusError("Received Unauthorized response with status: %d\n", status); //Get new token and update auth header - if (strlen(cfg->token_acquisition_script) >0) { - createNewAuthToken(cfg->webpa_auth_token,sizeof(cfg->webpa_auth_token)); + if (strlen(get_parodus_cfg()->token_acquisition_script) >0) { + createNewAuthToken(get_parodus_cfg()->webpa_auth_token,sizeof(get_parodus_cfg()->webpa_auth_token)); } - extra_headers = build_extra_headers( (0 < strlen(cfg->webpa_auth_token) ? cfg->webpa_auth_token : NULL), + extra_headers = build_extra_headers( (0 < strlen(get_parodus_cfg()->webpa_auth_token) ? get_parodus_cfg()->webpa_auth_token : NULL), device_id, user_agent, conveyHeader ); //reset c=2 to start backoffRetryTime as retrying @@ -316,10 +312,10 @@ int createNopollConnection(noPollCtx *ctx) ParodusError("RDK-10037 - WebPA Connection Lost\n"); // Copy the server address and port from config to avoid retrying to the same failing talaria redirected node - if (cfg->acquire_jwt == 0) + if (get_parodus_cfg()->acquire_jwt == 0) { ParodusInfo("acquire_jwt is 0, retrying with config server address\n"); - allow_insecure = parse_webpa_url (cfg->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); + allow_insecure = parse_webpa_url (get_parodus_cfg()->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); } else { @@ -331,7 +327,7 @@ int createNopollConnection(noPollCtx *ctx) else { ParodusError("acquire_jwt is 1 & unable to get jwt_server_url, retrying with config server address\n"); - allow_insecure = parse_webpa_url (cfg->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); + allow_insecure = parse_webpa_url (get_parodus_cfg()->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); } } @@ -390,10 +386,10 @@ int createNopollConnection(noPollCtx *ctx) c++; // Copy the server address and port from config to avoid retrying to the same failing talaria redirected node - if (cfg->acquire_jwt == 0) + if (get_parodus_cfg()->acquire_jwt == 0) { ParodusInfo("acquire_jwt is 0, retrying with config server address\n"); - allow_insecure = parse_webpa_url (cfg->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); + allow_insecure = parse_webpa_url (get_parodus_cfg()->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); } else { @@ -405,7 +401,7 @@ int createNopollConnection(noPollCtx *ctx) else { ParodusError("acquire_jwt is 1 & unable to get jwt_server_url, retrying with config server address\n"); - allow_insecure = parse_webpa_url (cfg->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); + allow_insecure = parse_webpa_url (get_parodus_cfg()->webpa_url, server_Address, (int) sizeof(server_Address), port, (int) sizeof(port)); } } @@ -475,18 +471,16 @@ static noPollConn * nopoll_tls_common_conn (noPollCtx * ctx,char * serverAddr,c unsigned int flags = 0; noPollConnOpts * opts; noPollConn *connection = NULL; - ParodusCfg *cfg = get_parodus_cfg(); - opts = createConnOpts(extra_headers, true); - flags = cfg->flags; + flags = get_parodus_cfg()->flags; if( FLAGS_IPV4_ONLY == (FLAGS_IPV4_ONLY & flags) ) { ParodusInfo("Connecting in Ipv4 mode\n"); - connection = nopoll_conn_tls_new (ctx, opts,serverAddr,serverPort,NULL,cfg->webpa_path_url,NULL,NULL); + connection = nopoll_conn_tls_new (ctx, opts,serverAddr,serverPort,NULL,get_parodus_cfg()->webpa_path_url,NULL,NULL); } else if( FLAGS_IPV6_ONLY == (FLAGS_IPV6_ONLY & flags) ) { ParodusInfo("Connecting in Ipv6 mode\n"); - connection = nopoll_conn_tls_new6 (ctx, opts,serverAddr,serverPort,NULL,cfg->webpa_path_url,NULL,NULL); + connection = nopoll_conn_tls_new6 (ctx, opts,serverAddr,serverPort,NULL,get_parodus_cfg()->webpa_path_url,NULL,NULL); } else { connection = __internal_fallbackConn(ctx,opts,serverAddr,serverPort,extra_headers,fallback); @@ -497,12 +491,11 @@ static noPollConn * nopoll_tls_common_conn (noPollCtx * ctx,char * serverAddr,c static noPollConn * __internal_fallbackConn(noPollCtx * ctx,noPollConnOpts * opts,char * serverAddr,char *serverPort,char * extra_headers,unsigned int *fallback) { noPollConn *connection = NULL; - ParodusCfg *cfg = get_parodus_cfg(); if(FLAGS_IPV6_ONLY == (FLAGS_IPV6_IPV4 & *fallback)) { ParodusInfo("Try connecting with Ipv6 mode\n"); - connection = nopoll_conn_tls_new6 (ctx, opts,serverAddr,serverPort,NULL,cfg->webpa_path_url,NULL,NULL); + connection = nopoll_conn_tls_new6 (ctx, opts,serverAddr,serverPort,NULL,get_parodus_cfg()->webpa_path_url,NULL,NULL); } if(FLAGS_IPV4_ONLY == (FLAGS_IPV6_IPV4 & *fallback) || !nopoll_conn_is_ok (connection) ) { @@ -513,7 +506,7 @@ static noPollConn * __internal_fallbackConn(noPollCtx * ctx,noPollConnOpts * op toggleIPFlag(fallback); opts = createConnOpts(extra_headers, true); - connection = nopoll_conn_tls_new (ctx, opts,serverAddr,serverPort,NULL,cfg->webpa_path_url,NULL,NULL); + connection = nopoll_conn_tls_new (ctx, opts,serverAddr,serverPort,NULL,get_parodus_cfg()->webpa_path_url,NULL,NULL); } return connection; @@ -522,19 +515,18 @@ static noPollConn * __internal_fallbackConn(noPollCtx * ctx,noPollConnOpts * op static noPollConnOpts * createConnOpts (char * extra_headers, bool secure) { noPollConnOpts * opts; - ParodusCfg *cfg = get_parodus_cfg(); opts = nopoll_conn_opts_new (); if(secure) { - if(cfg->cert_path && (strlen(cfg->cert_path) > 0)) + if(strlen(get_parodus_cfg()->cert_path) > 0) { - nopoll_conn_opts_set_ssl_certs(opts, NULL, NULL, NULL, cfg->cert_path); + nopoll_conn_opts_set_ssl_certs(opts, NULL, NULL, NULL, get_parodus_cfg()->cert_path); } nopoll_conn_opts_ssl_peer_verify (opts, nopoll_true); nopoll_conn_opts_set_ssl_protocol (opts, NOPOLL_METHOD_TLSV1_2); } - nopoll_conn_opts_set_interface (opts,cfg->webpa_interface_used); + nopoll_conn_opts_set_interface (opts,get_parodus_cfg()->webpa_interface_used); nopoll_conn_opts_set_extra_headers (opts,extra_headers); return opts; } diff --git a/src/main.c b/src/main.c index 0631259..37a63d4 100644 --- a/src/main.c +++ b/src/main.c @@ -132,7 +132,6 @@ static void sig_handler(int sig) else { ParodusInfo("Signal %d received!\n", sig); - clean_up_parodus_cfg(get_parodus_cfg()); exit(0); } diff --git a/src/partners_check.c b/src/partners_check.c index 197913a..9df498d 100644 --- a/src/partners_check.c +++ b/src/partners_check.c @@ -44,7 +44,7 @@ int validate_partner_id(wrp_msg_t *msg, partners_t **partnerIds) int matchFlag = 0, i = 0, count = 0; ParodusPrint("********* %s ********\n",__FUNCTION__); char *partnerId = get_parodus_cfg()->partner_id; - if(partnerId == NULL || strlen(partnerId) <= 0) + if(strlen(partnerId) <= 0) { ParodusPrint("partner_id is not available to validate\n"); return 0; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ab3263b..05f0b3f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -28,7 +28,6 @@ endif (ENABLE_SESHAT) if (FEATURE_DNS_QUERY) set (PARODUS_COMMON_LIBS ${PARODUS_COMMON_LIBS} -lucresolv -lresolv) -add_definitions(-DJWT_KEY_FILE_PATH="${CMAKE_CURRENT_SOURCE_DIR}") endif (FEATURE_DNS_QUERY) if(NOT DISABLE_VALGRIND) diff --git a/tests/simple_connection.c b/tests/simple_connection.c index ce9265b..4fcf5a5 100644 --- a/tests/simple_connection.c +++ b/tests/simple_connection.c @@ -65,17 +65,18 @@ void test_set_global_conn() void test_set_parodus_cfg() { ParodusCfg cfg; - cfg.hw_model= strdup ("TG1682"); - cfg.hw_serial_number= strdup ("Fer23u948590"); - cfg.hw_manufacturer = strdup ("ARRISGroup,Inc."); - cfg.hw_mac = strdup ("123567892366"); - cfg.hw_last_reboot_reason = strdup ("unknown"); - cfg.fw_name = strdup ("2.364s2"); - cfg.webpa_path_url = strdup ("/api/v2/device"); - cfg.webpa_url = strdup ("fabric-cd.webpa.comcast.net"); - cfg.webpa_interface_used = strdup ("eth0"); - cfg.webpa_protocol = strdup ("WebPA-1.6"); - cfg.webpa_uuid = strdup ("1234567-345456546"); + + parStrncpy(cfg.hw_model, "TG1682", sizeof(cfg.hw_model)); + parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number)); + parStrncpy(cfg.hw_manufacturer , "ARRISGroup,Inc.", sizeof(cfg.hw_manufacturer)); + parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac)); + parStrncpy(cfg.hw_last_reboot_reason , "unknown", sizeof(cfg.hw_last_reboot_reason)); + parStrncpy(cfg.fw_name , "2.364s2", sizeof(cfg.fw_name)); + parStrncpy(cfg.webpa_path_url , "/api/v2/device", sizeof(cfg.webpa_path_url)); + parStrncpy(cfg.webpa_url , "fabric-cd.webpa.comcast.net", sizeof(cfg.webpa_url)); + parStrncpy(cfg.webpa_interface_used , "eth0", sizeof(cfg.webpa_interface_used)); + parStrncpy(cfg.webpa_protocol , "WebPA-1.6", sizeof(cfg.webpa_protocol)); + parStrncpy(cfg.webpa_uuid , "1234567-345456546", sizeof(cfg.webpa_uuid)); cfg.secureFlag = 1; cfg.boot_time = 423457; cfg.webpa_ping_timeout = 30; @@ -97,8 +98,6 @@ void test_set_parodus_cfg() CU_ASSERT_EQUAL(cfg.webpa_ping_timeout, get_parodus_cfg()->webpa_ping_timeout); CU_ASSERT_EQUAL(cfg.webpa_backoff_max, get_parodus_cfg()->webpa_backoff_max); CU_ASSERT_EQUAL(cfg.secureFlag, get_parodus_cfg()->secureFlag); - - clean_up_parodus_cfg(); } void test_getWebpaConveyHeader() diff --git a/tests/test_ParodusInternal.c b/tests/test_ParodusInternal.c index b7c57c1..7bcccd2 100644 --- a/tests/test_ParodusInternal.c +++ b/tests/test_ParodusInternal.c @@ -57,17 +57,17 @@ void test_getWebpaConveyHeader() { ParodusCfg cfg; memset(&cfg, 0, sizeof(ParodusCfg)); - cfg.hw_model = strdup ("TG1682"); - cfg.hw_serial_number = strdup ("Fer23u948590"); - cfg.hw_manufacturer = strdup ("ARRISGroup,Inc."); - cfg.hw_mac = strdup ("123567892366"); - cfg.hw_last_reboot_reason = strdup ("unknown"); - cfg.fw_name = strdup ("2.364s2"); - cfg.webpa_path_url = strdup ("/api/v2/device"); - cfg.webpa_url = strdup ("localhost"); - cfg.webpa_interface_used = strdup ("eth0"); - cfg.webpa_protocol = strdup ("WebPA-1.6"); - cfg.webpa_uuid = strdup ("1234567-345456546"); + parStrncpy(cfg.hw_model, "TG1682", sizeof(cfg.hw_model)); + parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number)); + parStrncpy(cfg.hw_manufacturer , "ARRISGroup,Inc.", sizeof(cfg.hw_manufacturer)); + parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac)); + parStrncpy(cfg.hw_last_reboot_reason , "unknown", sizeof(cfg.hw_last_reboot_reason)); + parStrncpy(cfg.fw_name , "2.364s2", sizeof(cfg.fw_name)); + parStrncpy(cfg.webpa_path_url , "/api/v2/device", sizeof(cfg.webpa_path_url)); + parStrncpy(cfg.webpa_url , "localhost", sizeof(cfg.webpa_url)); + parStrncpy(cfg.webpa_interface_used , "eth0", sizeof(cfg.webpa_interface_used)); + parStrncpy(cfg.webpa_protocol , "WebPA-1.6", sizeof(cfg.webpa_protocol)); + parStrncpy(cfg.webpa_uuid , "1234567-345456546", sizeof(cfg.webpa_uuid)); cfg.flags = 0; cfg.boot_time = 423457; cfg.webpa_ping_timeout = 30; @@ -80,7 +80,6 @@ void test_getWebpaConveyHeader() will_return(nopoll_base64_encode, nopoll_true); expect_function_call(nopoll_base64_encode); getWebpaConveyHeader(); - clean_up_parodus_cfg(&cfg); } void err_getWebpaConveyHeader() diff --git a/tests/test_config.c b/tests/test_config.c index 607ac8d..49fe9fa 100644 --- a/tests/test_config.c +++ b/tests/test_config.c @@ -58,20 +58,20 @@ void test_setParodusConfig() ParodusCfg cfg; memset(&cfg,0,sizeof(cfg)); - cfg.hw_model = strdup ("TG1682"); - cfg.hw_serial_number = strdup ("Fer23u948590"); - cfg.hw_manufacturer = strdup ("ARRISGroup,Inc."); - cfg.hw_mac = strdup ("123567892366"); - cfg.hw_last_reboot_reason = strdup ("unknown"); - cfg.fw_name = strdup ("2.364s2"); - cfg.webpa_path_url = strdup ("/v1"); - cfg.webpa_url = strdup ("http://127.0.0.1"); - cfg.webpa_interface_used = strdup ("eth0"); - cfg.webpa_protocol = strdup ("WebPA-1.6"); - cfg.webpa_uuid = strdup ("1234567-345456546"); - cfg.partner_id = strdup ("mycom"); + parStrncpy(cfg.hw_model, "TG1682", sizeof(cfg.hw_model)); + parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number)); + parStrncpy(cfg.hw_manufacturer , "ARRISGroup,Inc.", sizeof(cfg.hw_manufacturer)); + parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac)); + parStrncpy(cfg.hw_last_reboot_reason , "unknown", sizeof(cfg.hw_last_reboot_reason)); + parStrncpy(cfg.fw_name , "2.364s2", sizeof(cfg.fw_name)); + parStrncpy(cfg.webpa_path_url , "/v1", sizeof(cfg.webpa_path_url)); + parStrncpy(cfg.webpa_url , "http://127.0.0.1", sizeof(cfg.webpa_url)); + parStrncpy(cfg.webpa_interface_used , "eth0", sizeof(cfg.webpa_interface_used)); + parStrncpy(cfg.webpa_protocol , "WebPA-1.6", sizeof(cfg.webpa_protocol)); + parStrncpy(cfg.webpa_uuid , "1234567-345456546", sizeof(cfg.webpa_uuid)); + parStrncpy(cfg.partner_id , "mycom", sizeof(cfg.partner_id)); #ifdef ENABLE_SESHAT - cfg.seshat_url = strdup ("ipc://tmp/seshat_service.url"); + parStrncpy(cfg.seshat_url, "ipc://tmp/seshat_service.url", sizeof(cfg.seshat_url)); #endif cfg.flags = 0; cfg.boot_time = 423457; @@ -79,9 +79,9 @@ void test_setParodusConfig() cfg.webpa_backoff_max = 255; #ifdef FEATURE_DNS_QUERY cfg.acquire_jwt = 1; - cfg.dns_txt_url = strdup ("test"); + parStrncpy(cfg.dns_txt_url, "test",sizeof(cfg.dns_txt_url)); cfg.jwt_algo = 1025; - cfg.jwt_key = strdup ("key.txt"); + parStrncpy(cfg.jwt_key, "key.txt",sizeof(cfg.jwt_key)); #endif set_parodus_cfg(&cfg); @@ -111,8 +111,6 @@ void test_setParodusConfig() assert_int_equal( (int) cfg.jwt_algo, (int) temp->jwt_algo); assert_string_equal(cfg.jwt_key, temp->jwt_key); #endif - - clean_up_parodus_cfg(&cfg); } void test_getParodusConfig() @@ -120,43 +118,20 @@ void test_getParodusConfig() ParodusCfg cfg; memset(&cfg,0,sizeof(cfg)); - cfg.hw_model = strdup ("TG1682133"); + parStrncpy(cfg.hw_model, "TG1682133",sizeof(cfg.hw_model)); set_parodus_cfg(&cfg); ParodusCfg *temp = get_parodus_cfg(); assert_string_equal(cfg.hw_model, temp->hw_model); - - free(cfg.hw_model); } -#ifdef FEATURE_DNS_QUERY -const char *jwt_key_file_path = JWT_KEY_FILE_PATH; -#else -const char *jwt_key_file_path = "foo"; -#endif - -static FILE * open_output_file (const char *fname) +static int open_output_file (const char *fname) { - FILE *fd; - char *file_path; - int error; - - file_path = (char *) malloc(strlen(fname) + strlen(jwt_key_file_path) + 1); - if (NULL == file_path) { - ParodusError("open_output_file() malloc failed\n"); - } - - strcpy(file_path, jwt_key_file_path); - strcat(file_path, fname); - - errno = 0; - fd = fopen(file_path, "w+"); - error = errno; - - if (NULL == fd) + int fd = open(fname, O_WRONLY | O_CREAT, 0666); + if (fd<0) { - ParodusError ("File %s open error (%s)\n", file_path, strerror(error)); + ParodusError ("File %s open error\n", fname); abort (); } return fd; @@ -164,21 +139,17 @@ static FILE * open_output_file (const char *fname) void write_key_to_file (const char *fname, const char *buf) { - ssize_t nbytes = -1; + ssize_t nbytes; ssize_t buflen = strlen (buf); - FILE *fd = open_output_file(fname); - - if (fd) { - nbytes = fwrite(buf, sizeof(char), buflen, fd); - } - + int fd = open_output_file(fname); + nbytes = write(fd, buf, buflen); if (nbytes < 0) { ParodusError ("Write file %s error\n", fname); - fclose(fd); + close(fd); abort (); } - fclose(fd); + close(fd); ParodusInfo ("%d bytes written\n", nbytes); } @@ -214,34 +185,18 @@ void test_parseCommandLine() #ifdef FEATURE_DNS_QUERY "--acquire-jwt=1", "--dns-txt-url=mydns.mycom.net", - "--jwt-algo=RS256", - "--jwt-public-key-file=../../tests/jwt_key.tst", /* POSITION DEPENDENT */ + "--jwt-public-key-file=../../tests/jwt_key.tst", + "--jwt-algo=RS256", #endif NULL }; int argc = (sizeof (command) / sizeof (char *)) - 1; + ParodusCfg parodusCfg; - -#ifdef FEATURE_DNS_QUERY - char *file_path; - #define JWT_PUBLIC "--jwt-public-key-file=" - - file_path = (char *) malloc(strlen(JWT_PUBLIC) + strlen("/jwt_key.tst") + - strlen(jwt_key_file_path) + 1); - if (NULL == file_path) { - ParodusError("open_output_file() malloc failed\n"); - } - - strcpy(file_path, JWT_PUBLIC); - strcat(file_path, jwt_key_file_path); - strcat(file_path, "/jwt_key.tst"); - command[argc-1] = file_path; -#endif - memset(&parodusCfg,0,sizeof(parodusCfg)); #ifdef FEATURE_DNS_QUERY - write_key_to_file ("/jwt_key.tst", jwt_key); + write_key_to_file ("../../tests/jwt_key.tst", jwt_key); #endif create_token_script("/tmp/token.sh"); assert_int_equal (parseCommandLine(argc,command,&parodusCfg), 0); @@ -274,7 +229,6 @@ void test_parseCommandLine() 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); - free(file_path); #endif } @@ -338,30 +292,30 @@ void test_loadParodusCfg() Cfg = (ParodusCfg*)malloc(sizeof(ParodusCfg)); char protocol[32] = {'\0'}; - Cfg->hw_model = strdup ("TG1682"); - Cfg->hw_serial_number = strdup ("Fer23u948590"); - Cfg->hw_manufacturer = strdup ("ARRISGroup,Inc."); - Cfg->hw_mac = strdup ("123567892366"); - Cfg->hw_last_reboot_reason = strdup ("unknown"); - Cfg->fw_name = strdup ("2.364s2"); - Cfg->webpa_path_url = strdup ("/v1"); - Cfg->webpa_url = strdup ("http://127.0.0.1"); - Cfg->webpa_interface_used = strdup ("eth0"); + parStrncpy(Cfg->hw_model, "TG1682", sizeof(Cfg->hw_model)); + parStrncpy(Cfg->hw_serial_number, "Fer23u948590", sizeof(Cfg->hw_serial_number)); + parStrncpy(Cfg->hw_manufacturer , "ARRISGroup,Inc.", sizeof(Cfg->hw_manufacturer)); + parStrncpy(Cfg->hw_mac , "123567892366", sizeof(Cfg->hw_mac)); + parStrncpy(Cfg->hw_last_reboot_reason , "unknown", sizeof(Cfg->hw_last_reboot_reason)); + parStrncpy(Cfg->fw_name , "2.364s2", sizeof(Cfg->fw_name)); + parStrncpy(Cfg->webpa_path_url , "/v1", sizeof(Cfg->webpa_path_url)); + parStrncpy(Cfg->webpa_url , "http://127.0.0.1", sizeof(Cfg->webpa_url)); + parStrncpy(Cfg->webpa_interface_used , "eth0", sizeof(Cfg->webpa_interface_used)); snprintf(protocol, sizeof(protocol), "%s-%s", PROTOCOL_VALUE, GIT_COMMIT_TAG); - Cfg->webpa_protocol = strdup (protocol); - Cfg->local_url = strdup ("tcp://10.0.0.1:6000"); - Cfg->partner_id = strdup ("shaw"); + parStrncpy(Cfg->webpa_protocol , protocol, sizeof(Cfg->webpa_protocol)); + parStrncpy(Cfg->local_url , "tcp://10.0.0.1:6000", sizeof(Cfg->local_url)); + parStrncpy(Cfg->partner_id , "shaw", sizeof(Cfg->partner_id)); #ifdef FEATURE_DNS_QUERY Cfg->acquire_jwt = 1; - Cfg->dns_txt_url = strdup ("mydns"); + parStrncpy(Cfg->dns_txt_url, "mydns",sizeof(Cfg->dns_txt_url)); Cfg->jwt_algo = 1025; - Cfg->jwt_key = strdup ("AGdyuwyhwl2ow2ydsoioiygkshwdthuwd"); + parStrncpy(Cfg->jwt_key, "AGdyuwyhwl2ow2ydsoioiygkshwdthuwd",sizeof(Cfg->jwt_key)); #endif - Cfg->token_acquisition_script = strdup ("/tmp/token.sh"); - Cfg->token_read_script = strdup ("/tmp/token.sh"); - Cfg->cert_path = strdup ("/etc/ssl.crt"); + parStrncpy(Cfg->token_acquisition_script , "/tmp/token.sh", sizeof(Cfg->token_acquisition_script)); + parStrncpy(Cfg->token_read_script , "/tmp/token.sh", sizeof(Cfg->token_read_script)); + parStrncpy(Cfg->cert_path, "/etc/ssl.crt",sizeof(Cfg->cert_path)); #ifdef ENABLE_SESHAT - Cfg->seshat_url = strdup ("ipc://tmp/seshat_service.url"); + parStrncpy(Cfg->seshat_url, "ipc://tmp/seshat_service.url", sizeof(Cfg->seshat_url)); #endif memset(&tmpcfg,0,sizeof(ParodusCfg)); loadParodusCfg(Cfg,&tmpcfg); @@ -385,7 +339,6 @@ void test_loadParodusCfg() #ifdef ENABLE_SESHAT assert_string_equal(tmpcfg.seshat_url, "ipc://tmp/seshat_service.url"); #endif - clean_up_parodus_cfg(Cfg); free(Cfg); } @@ -399,12 +352,14 @@ void test_loadParodusCfgNull() loadParodusCfg(cfg,&temp); + assert_string_equal(temp.hw_model, ""); + assert_string_equal(temp.hw_serial_number, ""); + assert_string_equal(temp.hw_manufacturer, ""); assert_int_equal( (int) temp.flags,0); assert_string_equal( temp.webpa_path_url, WEBPA_PATH_URL); assert_string_equal( temp.webpa_uuid,"1234567-345456546"); assert_string_equal( temp.local_url, PARODUS_UPSTREAM); - clean_up_parodus_cfg(cfg); free(cfg); } @@ -414,20 +369,18 @@ void err_loadParodusCfg() loadParodusCfg(NULL,&cfg); } -/* This test makes no sense ;-) void test_parodusGitVersion() { FILE *fp; - char *version = (char *) malloc(256); + char version[32] = {'\0'}; char *command = "git describe --tags --always"; int n; size_t len; fp = popen(command,"r"); - memset(version, 0, 2048); - while(fgets(version, 2048, fp) !=NULL) + while(fgets(version, 32, fp) !=NULL) { len = strlen(version); - if (len > 0) + if (len > 0 && version[len-1] == '\n') { version[--len] = '\0'; } @@ -438,9 +391,7 @@ void test_parodusGitVersion() printf ("GIT_COMMIT_TAG: %s\n", GIT_COMMIT_TAG); n = strcmp( version, GIT_COMMIT_TAG); assert_int_equal(n, 0); - free(version); } -*/ void test_setDefaultValuesToCfg() { @@ -454,13 +405,10 @@ void test_setDefaultValuesToCfg() assert_string_equal(cfg->jwt_key, "\0"); assert_int_equal( (int)cfg->jwt_algo, 0); #endif - assert_true(cfg->cert_path == NULL); + assert_string_equal(cfg->cert_path, "\0"); assert_int_equal((int)cfg->flags, 0); assert_string_equal(cfg->webpa_path_url, WEBPA_PATH_URL); assert_string_equal(cfg->webpa_uuid, "1234567-345456546"); - - clean_up_parodus_cfg(cfg); - free(cfg); } void err_setDefaultValuesToCfg() @@ -563,7 +511,7 @@ int main(void) cmocka_unit_test(test_parseCommandLine), cmocka_unit_test(test_parseCommandLineNull), cmocka_unit_test(err_parseCommandLine), - // cmocka_unit_test(test_parodusGitVersion), + cmocka_unit_test(test_parodusGitVersion), cmocka_unit_test(test_setDefaultValuesToCfg), cmocka_unit_test(err_setDefaultValuesToCfg), }; diff --git a/tests/test_conn_interface.c b/tests/test_conn_interface.c index 6657b61..8e05147 100644 --- a/tests/test_conn_interface.c +++ b/tests/test_conn_interface.c @@ -241,17 +241,17 @@ void test_createSocketConnection2() noPollCtx *ctx; ParodusCfg cfg; memset(&cfg,0,sizeof(ParodusCfg)); - cfg.hw_model = strdup ("TG1682"); - cfg.hw_serial_number = strdup ("Fer23u948590"); - cfg.hw_manufacturer = strdup ("ARRISGroup,Inc."); - cfg.hw_mac = strdup ("123567892366"); - cfg.hw_last_reboot_reason = strdup ("unknown"); - cfg.fw_name = strdup ("2.364s2"); - cfg.webpa_path_url = strdup ("/v1"); - cfg.webpa_url = strdup ("localhost"); - cfg.webpa_interface_used = strdup ("eth0"); - cfg.webpa_protocol = strdup ("WebPA-1.6"); - cfg.webpa_uuid = strdup ("1234567-345456546"); + parStrncpy(cfg.hw_model, "TG1682", sizeof(cfg.hw_model)); + parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number)); + parStrncpy(cfg.hw_manufacturer , "ARRISGroup,Inc.", sizeof(cfg.hw_manufacturer)); + parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac)); + parStrncpy(cfg.hw_last_reboot_reason , "unknown", sizeof(cfg.hw_last_reboot_reason)); + parStrncpy(cfg.fw_name , "2.364s2", sizeof(cfg.fw_name)); + parStrncpy(cfg.webpa_path_url , "/v1", sizeof(cfg.webpa_path_url)); + parStrncpy(cfg.webpa_url , "localhost", sizeof(cfg.webpa_url)); + parStrncpy(cfg.webpa_interface_used , "eth0", sizeof(cfg.webpa_interface_used)); + parStrncpy(cfg.webpa_protocol , "WebPA-1.6", sizeof(cfg.webpa_protocol)); + parStrncpy(cfg.webpa_uuid , "1234567-345456546", sizeof(cfg.webpa_uuid)); cfg.webpa_ping_timeout = 1; set_parodus_cfg(&cfg); @@ -291,7 +291,6 @@ void test_createSocketConnection2() expect_function_call(nopoll_ctx_unref); expect_function_call(nopoll_cleanup_library); createSocketConnection(NULL); - clean_up_parodus_cfg(&cfg); } void err_createSocketConnection() diff --git a/tests/test_createConnection.c b/tests/test_createConnection.c index a3db6fd..7a74ff6 100644 --- a/tests/test_createConnection.c +++ b/tests/test_createConnection.c @@ -235,7 +235,7 @@ void test_createSecureConnection() #ifdef FEATURE_DNS_QUERY cfg->acquire_jwt = 1; #endif - cfg->webpa_url = strdup (SECURE_WEBPA_URL); + parStrncpy(cfg->webpa_url , SECURE_WEBPA_URL, sizeof(cfg->webpa_url)); set_parodus_cfg(cfg); assert_non_null(ctx); @@ -283,7 +283,6 @@ void test_createSecureConnection() int ret = createNopollConnection(ctx); assert_int_equal(ret, nopoll_true); - clean_up_parodus_cfg(cfg); free(cfg); if (g_jwt_server_ip !=NULL) { @@ -306,7 +305,7 @@ void test_createConnection() #ifdef FEATURE_DNS_QUERY cfg->acquire_jwt = 1; #endif - cfg->webpa_url = strdup (UNSECURE_WEBPA_URL); + parStrncpy(cfg->webpa_url , UNSECURE_WEBPA_URL, sizeof(cfg->webpa_url)); set_parodus_cfg(cfg); assert_non_null(ctx); @@ -340,7 +339,6 @@ void test_createConnection() int ret = createNopollConnection(ctx); assert_int_equal(ret, nopoll_true); - clean_up_parodus_cfg(cfg); free(cfg); if (g_jwt_server_ip !=NULL) { @@ -363,7 +361,7 @@ void test_createConnectionConnNull() #ifdef FEATURE_DNS_QUERY cfg->acquire_jwt = 1; #endif - cfg->webpa_url = strdup (SECURE_WEBPA_URL); + parStrncpy(cfg->webpa_url , SECURE_WEBPA_URL,sizeof(cfg->webpa_url)); set_parodus_cfg(cfg); assert_non_null(ctx); @@ -495,7 +493,6 @@ void test_createConnectionConnNull() expect_function_call(setMessageHandlers); createNopollConnection(ctx); - clean_up_parodus_cfg(cfg); free(cfg); if (g_jwt_server_ip !=NULL) { @@ -518,7 +515,7 @@ void test_createConnNull_JWT_NULL() #ifdef FEATURE_DNS_QUERY cfg->acquire_jwt = 1; #endif - cfg->webpa_url = strdup (SECURE_WEBPA_URL); + parStrncpy(cfg->webpa_url , SECURE_WEBPA_URL,sizeof(cfg->webpa_url)); set_parodus_cfg(cfg); assert_non_null(ctx); @@ -635,7 +632,6 @@ void test_createConnNull_JWT_NULL() expect_function_call(setMessageHandlers); createNopollConnection(ctx); - clean_up_parodus_cfg(cfg); free(cfg); if (g_jwt_server_ip !=NULL) { @@ -658,7 +654,7 @@ void test_createConnectionConnNotOk() #ifdef FEATURE_DNS_QUERY cfg->acquire_jwt = 1; #endif - cfg->webpa_url = strdup (UNSECURE_WEBPA_URL); + parStrncpy(cfg->webpa_url , UNSECURE_WEBPA_URL, sizeof(cfg->webpa_url)); set_parodus_cfg(cfg); assert_non_null(ctx); @@ -733,7 +729,6 @@ void test_createConnectionConnNotOk() int ret = createNopollConnection(ctx); assert_int_equal(ret, nopoll_true); - clean_up_parodus_cfg(cfg); free(cfg); if (g_jwt_server_ip !=NULL) { @@ -756,7 +751,7 @@ void test_createConnNotOk_JWT_NULL() #ifdef FEATURE_DNS_QUERY cfg->acquire_jwt = 1; #endif - cfg->webpa_url = strdup (UNSECURE_WEBPA_URL); + parStrncpy(cfg->webpa_url , UNSECURE_WEBPA_URL, sizeof(cfg->webpa_url)); set_parodus_cfg(cfg); assert_non_null(ctx); @@ -831,7 +826,6 @@ void test_createConnNotOk_JWT_NULL() int ret = createNopollConnection(ctx); assert_int_equal(ret, nopoll_true); - clean_up_parodus_cfg(cfg); free(cfg); if (g_jwt_server_ip !=NULL) { @@ -854,7 +848,7 @@ void test_createConnectionConnRedirect() #ifdef FEATURE_DNS_QUERY cfg->acquire_jwt = 1; #endif - cfg->webpa_url = strdup (UNSECURE_WEBPA_URL); + parStrncpy(cfg->webpa_url , UNSECURE_WEBPA_URL, sizeof(cfg->webpa_url)); set_parodus_cfg(cfg); assert_non_null(ctx); @@ -927,7 +921,6 @@ void test_createConnectionConnRedirect() int ret = createNopollConnection(ctx); assert_int_equal(ret, nopoll_true); - clean_up_parodus_cfg(cfg); free(cfg); if (g_jwt_server_ip !=NULL) { @@ -948,7 +941,7 @@ void test_createIPv4Connection() #ifdef FEATURE_DNS_QUERY cfg->acquire_jwt = 1; #endif - cfg->webpa_url = strdup (SECURE_WEBPA_URL); + parStrncpy(cfg->webpa_url , SECURE_WEBPA_URL, sizeof(cfg->webpa_url)); set_parodus_cfg(cfg); assert_non_null(ctx); @@ -981,7 +974,6 @@ void test_createIPv4Connection() int ret = createNopollConnection(ctx); assert_int_equal(ret, nopoll_true); - clean_up_parodus_cfg(cfg); free(cfg); nopoll_ctx_unref (ctx); } @@ -998,7 +990,7 @@ void test_createIPv6Connection() #ifdef FEATURE_DNS_QUERY cfg->acquire_jwt = 1; #endif - cfg->webpa_url = strdup (SECURE_WEBPA_URL); + parStrncpy(cfg->webpa_url , SECURE_WEBPA_URL, sizeof(cfg->webpa_url)); set_parodus_cfg(cfg); assert_non_null(ctx); @@ -1032,7 +1024,6 @@ void test_createIPv6Connection() int ret = createNopollConnection(ctx); assert_int_equal(ret, nopoll_true); - clean_up_parodus_cfg(cfg); free(cfg); nopoll_ctx_unref (ctx); } @@ -1050,7 +1041,7 @@ void test_createIPv6toIPv4Connection() #ifdef FEATURE_DNS_QUERY cfg->acquire_jwt = 1; #endif - cfg->webpa_url = strdup (SECURE_WEBPA_URL); + parStrncpy(cfg->webpa_url , SECURE_WEBPA_URL, sizeof(cfg->webpa_url)); set_parodus_cfg(cfg); assert_non_null(ctx); @@ -1110,7 +1101,6 @@ void test_createIPv6toIPv4Connection() int ret = createNopollConnection(ctx); assert_int_equal(ret, nopoll_true); - clean_up_parodus_cfg(cfg); free(cfg); nopoll_ctx_unref (ctx); } @@ -1127,7 +1117,7 @@ void test_createFallbackRedirectionConn() #ifdef FEATURE_DNS_QUERY cfg->acquire_jwt = 1; #endif - cfg->webpa_url = strdup (SECURE_WEBPA_URL); + parStrncpy(cfg->webpa_url , SECURE_WEBPA_URL, sizeof(cfg->webpa_url)); set_parodus_cfg(cfg); assert_non_null(ctx); @@ -1212,7 +1202,6 @@ void test_createFallbackRedirectionConn() int ret = createNopollConnection(ctx); assert_int_equal(ret, nopoll_true); - clean_up_parodus_cfg(cfg); free(cfg); nopoll_ctx_unref (ctx); } @@ -1229,7 +1218,7 @@ void test_createIPv6FallbackRedirectConn() #ifdef FEATURE_DNS_QUERY cfg->acquire_jwt = 1; #endif - cfg->webpa_url = strdup (SECURE_WEBPA_URL); + parStrncpy(cfg->webpa_url , SECURE_WEBPA_URL, sizeof(cfg->webpa_url)); set_parodus_cfg(cfg); assert_non_null(ctx); @@ -1291,7 +1280,6 @@ void test_createIPv6FallbackRedirectConn() int ret = createNopollConnection(ctx); assert_int_equal(ret, nopoll_true); - clean_up_parodus_cfg(cfg); free(cfg); nopoll_ctx_unref (ctx); } diff --git a/tests/test_partners_check.c b/tests/test_partners_check.c index fbeb844..a3ad973 100755 --- a/tests/test_partners_check.c +++ b/tests/test_partners_check.c @@ -51,13 +51,12 @@ void test_validate_partner_id_for_req() ParodusCfg cfg; memset(&cfg, 0, sizeof(ParodusCfg)); - cfg.partner_id = strdup ("comcast"); + parStrncpy(cfg.partner_id, "comcast", sizeof(cfg.partner_id)); will_return(get_parodus_cfg, (intptr_t)&cfg); expect_function_call(get_parodus_cfg); int ret = validate_partner_id(msg, NULL); assert_int_equal(ret, 1); - free(cfg.partner_id); free(msg); } @@ -69,13 +68,12 @@ void test_validate_partner_id_for_req_listNULL() ParodusCfg cfg; memset(&cfg, 0, sizeof(ParodusCfg)); - cfg.partner_id = strdup ("comcast"); + parStrncpy(cfg.partner_id, "comcast", sizeof(cfg.partner_id)); will_return(get_parodus_cfg, (intptr_t)&cfg); expect_function_call(get_parodus_cfg); int ret = validate_partner_id(msg, NULL); assert_int_equal(ret, 1); - free(cfg.partner_id); free(msg); } @@ -105,13 +103,12 @@ void err_validate_partner_id_for_req() ParodusCfg cfg; memset(&cfg, 0, sizeof(ParodusCfg)); - cfg.partner_id = strdup ("comcast"); + parStrncpy(cfg.partner_id, "comcast", sizeof(cfg.partner_id)); will_return(get_parodus_cfg, (intptr_t)&cfg); expect_function_call(get_parodus_cfg); int ret = validate_partner_id(msg, NULL); assert_int_equal(ret, -1); - free(cfg.partner_id); free(msg); } @@ -125,7 +122,7 @@ void test_validate_partner_id_for_event() ParodusCfg cfg; memset(&cfg, 0, sizeof(ParodusCfg)); - cfg.partner_id = strdup ("comcast"); + parStrncpy(cfg.partner_id, "comcast", sizeof(cfg.partner_id)); will_return(get_parodus_cfg, (intptr_t)&cfg); expect_function_call(get_parodus_cfg); @@ -134,7 +131,6 @@ void test_validate_partner_id_for_event() int ret = validate_partner_id(msg, &list); assert_int_equal(ret, 1); free(list); - free(cfg.partner_id); free(msg); } @@ -146,7 +142,7 @@ void test_validate_partner_id_for_event_listNULL() ParodusCfg cfg; memset(&cfg, 0, sizeof(ParodusCfg)); - cfg.partner_id = strdup ("comcast"); + parStrncpy(cfg.partner_id, "comcast", sizeof(cfg.partner_id)); will_return(get_parodus_cfg, (intptr_t)&cfg); expect_function_call(get_parodus_cfg); @@ -161,7 +157,6 @@ void test_validate_partner_id_for_event_listNULL() free(list->partner_ids[i]); } free(list); - free(cfg.partner_id); free(msg); } @@ -195,7 +190,7 @@ void test_validate_partner_id_for_event_withoutId() ParodusCfg cfg; memset(&cfg, 0, sizeof(ParodusCfg)); - cfg.partner_id = strdup ("comcast"); + parStrncpy(cfg.partner_id, "comcast", sizeof(cfg.partner_id)); will_return(get_parodus_cfg, (intptr_t)&cfg); expect_function_call(get_parodus_cfg); @@ -212,7 +207,6 @@ void test_validate_partner_id_for_event_withoutId() } free(list); free(msg); - free(cfg.partner_id); free(partner_ids); } diff --git a/tests/test_seshat_interface.c b/tests/test_seshat_interface.c index 67835c6..1655cd3 100644 --- a/tests/test_seshat_interface.c +++ b/tests/test_seshat_interface.c @@ -87,7 +87,7 @@ int allow_insecure_conn(void) /*----------------------------------------------------------------------------*/ void test_all_pass() { - g_config.local_url = strdup(URL); + parStrncpy(g_config.local_url, URL, sizeof(g_config.local_url)); will_return(init_lib_seshat, 0); expect_function_call(init_lib_seshat); @@ -104,7 +104,6 @@ void test_all_pass() expect_function_call(shutdown_seshat_lib); assert_true(__registerWithSeshat()); - free(g_config.local_url); memset(&g_config, '\0', sizeof(g_config)); } @@ -152,7 +151,7 @@ void test_discover_fail() void test_discover_pass_but_lru_expected_fail() { - g_config.local_url = strdup(URL); + parStrncpy(g_config.local_url, URL, sizeof(g_config.local_url)); will_return(init_lib_seshat, 0); expect_function_call(init_lib_seshat); @@ -169,8 +168,6 @@ void test_discover_pass_but_lru_expected_fail() expect_function_call(shutdown_seshat_lib); assert_false(__registerWithSeshat()); - - free(g_config.local_url); } /*----------------------------------------------------------------------------*/ diff --git a/tests/test_token.c b/tests/test_token.c index 693d60f..b425a0d 100644 --- a/tests/test_token.c +++ b/tests/test_token.c @@ -142,7 +142,6 @@ cjwt_t jwt2; // secure, payload good, but expired cjwt_t jwt3; // insecure cjwt_t jwt4; // missing endpoint - // internal functions in token.c to be tested extern int analyze_jwt (const cjwt_t *jwt, char *url_buf, int url_buflen, char *port_buf, int port_buflen); @@ -155,10 +154,10 @@ extern int get_rr_seq_num (const char *rr_ptr, int rrlen); extern int get_rr_seq_table (ns_msg *msg_handle, int num_rr_recs, rr_rec_t *seq_table); extern int assemble_jwt_from_dns (ns_msg *msg_handle, int num_rr_recs, char *jwt_ans); extern int query_dns(const char* dns_txt_record_id,char *jwt_ans); +extern void read_key_from_file (const char *fname, char *buf, size_t buflen); extern const char *get_tok (const char *src, int delim, char *result, int resultsize); extern unsigned int get_algo_mask (const char *algo_str); - int setup_test_jwts (void) { memset (&jwt1, 0, sizeof(cjwt_t)); @@ -606,11 +605,11 @@ void test_allow_insecure_conn () char port_buf[6] = "8080"; ParodusCfg *cfg = get_parodus_cfg(); - cfg->hw_mac = strdup ("aabbccddeeff"); - cfg->dns_txt_url = strdup ("test.mydns.mycom.net"); + 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; - read_key_from_file ("../../tests/pubkey4.pem", &cfg->jwt_key); + read_key_from_file ("../../tests/pubkey4.pem", cfg->jwt_key, 4096); will_return (__res_ninit, 0); expect_function_call (__res_ninit); @@ -620,8 +619,8 @@ void test_allow_insecure_conn () port_buf, sizeof(port_buf)); assert_int_equal (insecure, 0); - free(cfg->dns_txt_url); - cfg->dns_txt_url = strdup ("err5.mydns.mycom.net"); + parStrncpy (cfg->hw_mac, "aabbccddeeff", sizeof(cfg->hw_mac)); + parStrncpy (cfg->dns_txt_url, "err5.mydns.mycom.net", sizeof(cfg->dns_txt_url)); will_return (__res_ninit, 0); expect_function_call (__res_ninit); @@ -631,11 +630,10 @@ void test_allow_insecure_conn () port_buf, sizeof(port_buf)); assert_int_equal (insecure, TOKEN_ERR_QUERY_DNS_FAIL); - free(cfg->dns_txt_url); - cfg->dns_txt_url = strdup ("test.mydns.mycom.net"); + 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 = 1024; - free(cfg->jwt_key); - cfg->jwt_key = strdup ("xxxxxxxxxx"); + parStrncpy (cfg->jwt_key, "xxxxxxxxxx", sizeof(cfg->jwt_key)); will_return (__res_ninit, 0); expect_function_call (__res_ninit); @@ -645,11 +643,10 @@ void test_allow_insecure_conn () port_buf, sizeof(port_buf)); assert_int_equal (insecure, TOKEN_ERR_JWT_DECODE_FAIL); - free(cfg->dns_txt_url); - cfg->dns_txt_url = strdup ("test.mydns.mycom.net"); + 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 = 4097; - free(cfg->jwt_key); - read_key_from_file ("../../tests/pubkey4.pem", &cfg->jwt_key); + read_key_from_file ("../../tests/pubkey4.pem", cfg->jwt_key, 4096); will_return (__res_ninit, 0); expect_function_call (__res_ninit); @@ -658,7 +655,7 @@ void test_allow_insecure_conn () insecure = allow_insecure_conn (cfg->dns_txt_url, sizeof(cfg->dns_txt_url), port_buf, sizeof(port_buf)); assert_int_equal (insecure, TOKEN_ERR_ALGO_NOT_ALLOWED); - clean_up_parodus_cfg(cfg); + } void test_get_tok()