diff --git a/packages/atauth/include/atauth/atactivate_arg_parser.h b/packages/atauth/include/atauth/atactivate_arg_parser.h index 7747edbd..74e7522f 100644 --- a/packages/atauth/include/atauth/atactivate_arg_parser.h +++ b/packages/atauth/include/atauth/atactivate_arg_parser.h @@ -11,17 +11,18 @@ * * @param argc The number of arguments * @param argv The array of arguments - * @param atsign pointer to store the atsign value - * @param cram_secret pointer to store the cram_secret value - * @param otp OTP fetched from otp verb handler - * @param atkeys_fp pointer to store the file path of the atkeys - * @param app_name - * @param device_name - * @param namespaces - * @param root_host pointer to store the root host server address + * @param atsign pointer to store the parsed atsign value + * @param cram_secret pointer to store the parsed cram_secret value + * @param otp OTP pointer to store the parsed OTP/SPP value (accepts OTP fetched from OTP verb) + * @param atkeys_fp pointer to store the parsed file path of the atkeys + * @param app_name pointer to store the parsed app_name for current enrollment + * @param device_name pointer to store the parsed device_name for current enrollment + * @param namespaces pointert to store the parsed namespaces list for current enrollment + * @param root_host pointer to store the parsed root server host + * @param root_port pointer to store the parsed root server port * @return int 0 on success, non-zero on error */ int atactivate_parse_args(int argc, char *argv[], char **atsign, char **cram_secret, char **otp, char **atkeys_fp, - char **app_name, char **device_name, char **namespaces, char **root_host); + char **app_name, char **device_name, char **namespaces, char **root_host, int *root_port); #endif // ATACTIVATE_ARG_PARSER_H diff --git a/packages/atauth/src/atactivate.c b/packages/atauth/src/atactivate.c index 645835f5..232b56ef 100644 --- a/packages/atauth/src/atactivate.c +++ b/packages/atauth/src/atactivate.c @@ -21,6 +21,7 @@ int main(int argc, char *argv[]) { int ret = 0; char *atsign_temp = NULL, *cram_secret = NULL, *root_host = NULL, *atkeys_fp = NULL, *otp = NULL; + int root_port = 0; char enrollment_id[ENROLL_ID_MAX_LEN]; char status[ATCOMMONS_ENROLL_STATUS_STRING_MAX_LEN]; @@ -137,7 +138,7 @@ int main(int argc, char *argv[]) { * 1. Parse args */ if ((ret = atactivate_parse_args(argc, argv, &atsign_temp, &cram_secret, &otp, &atkeys_fp, NULL, NULL, NULL, - &root_host)) != 0) { + &root_host, &root_port)) != 0) { goto exit; } diff --git a/packages/atauth/src/atactivate_arg_parser.c b/packages/atauth/src/atactivate_arg_parser.c index 198cb022..f4e64106 100644 --- a/packages/atauth/src/atactivate_arg_parser.c +++ b/packages/atauth/src/atactivate_arg_parser.c @@ -1,29 +1,23 @@ #include "atauth/atactivate_arg_parser.h" +#include #include #include #include #include #include -#define DEFAULT_ROOT_SERVER "root.atsign.org" -#define DEFAULT_ROOT_PORT 64 +int parse_root_domain(const char *root_domain_string, char **root_host, int *root_port); -/// ToDO: add impl to read the root server FQDN then parse it. Currently only accepts root host, cannot parse root port int atactivate_parse_args(const int argc, char *argv[], char **atsign, char **cram_secret, char **otp, char **atkeys_fp, - char **app_name, char **device_name, char **namespaces, char **root_host) { - int ret = 0; - int opt; - - // Initialize defaults - *root_host = malloc(sizeof(char) * strlen(DEFAULT_ROOT_SERVER) + 1); - if (*root_host == NULL) { - fprintf(stderr, "Memory allocation failed for root_host\n"); - return -1; - } - strcpy(*root_host, DEFAULT_ROOT_SERVER); + char **app_name, char **device_name, char **namespaces, char **root_host, int *root_port) { + int ret = 0, opt = 0; + char *root_fqdn = NULL; + const char *usage = "Usage: \n\tActivate: \t./atactivate -a atsign -c cram-secret [-k path_to_store_keysfile] [-r root-domain]" + "\n\n\tNew enrollment: ./at_auth_cli -a atsign -s otp/spp -p app_name -d device_name -n " + "namespaces(\"wavi:rw,buzz:r\") [-k path_to_store_keysfile] [-r root-domain]\n"; // Parse command-line arguments - while ((opt = getopt(argc, argv, "a:c:k:o:p:d:n:r:vh")) != -1) { + while ((opt = getopt(argc, argv, "a:c:k:s:p:d:n:r:vh")) != -1) { switch (opt) { case 'a': *atsign = malloc(sizeof(char) * strlen(optarg) + 1); @@ -56,12 +50,12 @@ int atactivate_parse_args(const int argc, char *argv[], char **atsign, char **cr } strcpy(*atkeys_fp, optarg); break; - case 'o': + case 's': if (otp == NULL) break; *otp = malloc(sizeof(char) * strlen(optarg)); if (*otp == NULL) { - fprintf(stderr, "Memory allocation failed for atkeys file path\n"); + fprintf(stderr, "Memory allocation failed for OTP\n"); ret = -1; goto exit; } @@ -70,7 +64,7 @@ int atactivate_parse_args(const int argc, char *argv[], char **atsign, char **cr case 'p': if (app_name == NULL) break; - *app_name = realloc(*root_host, sizeof(char) * strlen(optarg) + 1); + *app_name = malloc(sizeof(char) * strlen(optarg) + 1); if (*app_name == NULL) { fprintf(stderr, "Memory reallocation failed for app_name\n"); ret = -1; @@ -81,7 +75,7 @@ int atactivate_parse_args(const int argc, char *argv[], char **atsign, char **cr case 'd': if (device_name == NULL) break; - *device_name = realloc(*device_name, sizeof(char) * strlen(optarg) + 1); + *device_name = malloc(sizeof(char) * strlen(optarg) + 1); if (*device_name == NULL) { fprintf(stderr, "Memory reallocation failed for device_name\n"); ret = -1; @@ -92,7 +86,7 @@ int atactivate_parse_args(const int argc, char *argv[], char **atsign, char **cr case 'n': if (namespaces == NULL) break; - *namespaces = realloc(*namespaces, sizeof(char) * strlen(optarg) + 1); + *namespaces = malloc(sizeof(char) * strlen(optarg) + 1); if (*namespaces == NULL) { fprintf(stderr, "Memory reallocation failed for namespaces\n"); ret = -1; @@ -101,39 +95,58 @@ int atactivate_parse_args(const int argc, char *argv[], char **atsign, char **cr strcpy(*namespaces, optarg); break; case 'r': - *root_host = realloc(*root_host, sizeof(char) * strlen(optarg) + 1); - if (*root_host == NULL) { - fprintf(stderr, "Memory reallocation failed for root_host\n"); + root_fqdn = malloc(sizeof(char) * strlen(optarg) + 1); + if (root_fqdn == NULL) { + fprintf(stderr, "Memory allocation failed for root_host\n"); ret = -1; goto exit; } - strcpy(*root_host, optarg); + strcpy(root_fqdn, optarg); break; case 'v': atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_DEBUG); break; case 'h': - fprintf(stderr, "Usage: %s -a atsign -c cram-secret -o otp [-r root-server] [-p port]\n", argv[0]); - exit(0); // force exit to display usage + fprintf(stdout, usage); + ret = 0; + goto exit; default: - fprintf(stderr, "Usage: %s -a atsign -c cram-secret -o otp [-r root-server] [-p port]\n", argv[0]); + fprintf(stderr, usage); ret = -1; goto exit; } } + // set default root server address if not provided through CLI + if (root_fqdn == NULL || parse_root_domain(root_fqdn, root_host, root_port) != 0) { + *root_host = strdup(ATCLIENT_ATDIRECTORY_PRODUCTION_HOST); + *root_port = ATCLIENT_ATDIRECTORY_PRODUCTION_PORT; + } + if (atsign == NULL) { fprintf(stderr, "Error: -a (atsign) is mandatory.\n"); - fprintf(stderr, "Usage: %s -a atsign -c cram-secret -o otp [-r root-server] [-p port]\n", argv[0]); + fprintf(stderr, usage); ret = 1; } if (cram_secret == NULL && otp == NULL) { fprintf(stderr, "Cannot proceed without either of CRAM secret or enroll OTP.\n"); - fprintf(stderr, "Usage: %s -a atsign -c cram-secret -o otp [-r root-server] [-p port]\n", argv[0]); + fprintf(stderr, usage); ret = 1; } exit: return ret; } + +int parse_root_domain(const char *root_domain_string, char **root_host, int *root_port) { + if(root_domain_string == NULL) { + return 1; + } + *root_host = strdup(strtok((char *)root_domain_string, ":")); + *root_port = atoi(strtok(NULL, ":")); + if(*root_host == NULL || root_port == NULL) { + return 1; + } + return 0; +} diff --git a/packages/atauth/src/auth_cli.c b/packages/atauth/src/auth_cli.c index 170709b0..1165647f 100644 --- a/packages/atauth/src/auth_cli.c +++ b/packages/atauth/src/auth_cli.c @@ -27,7 +27,8 @@ int create_new_atserver_connection(atclient *ctx, const char *atsign, const atcl int atauth_validate_args(const char *otp, const char *app_name, const char *device_name, const char *namespaces_str); int main(int argc, char *argv[]) { - int ret = 0; + atlogger_set_logging_level(ATLOGGER_LOGGING_LEVEL_INFO); + int ret = 0, root_port = 0; char *atsign_temp = NULL, *root_host = NULL, *atkeys_fp = NULL, *otp = NULL, *app_name = NULL, *device_name = NULL, *namespaces_str = NULL; @@ -66,7 +67,7 @@ int main(int argc, char *argv[]) { * 1. Parse + validate command-line arguments */ if ((ret = atactivate_parse_args(argc, argv, &atsign_temp, NULL, &otp, &atkeys_fp, &app_name, &device_name, - &namespaces_str, &root_host)) != 0) { + &namespaces_str, &root_host, &root_port)) != 0) { goto exit; } @@ -110,7 +111,7 @@ int main(int argc, char *argv[]) { atclient_atkeys_set_pkam_private_key_base64(&atkeys, (const char *)pkam_private_key_base64, strlen((const char *)pkam_private_key_base64)); - // 2.1.2 populate the pkam public/private key bytes in the atkeys struct by parsing the base64 formats + // 2.1.2 populate the pkam public/private key bytes in the atkeys struct by parsing the base64 encoded keys atclient_atkeys_populate_pkam_public_key(&atkeys, (const char *)pkam_public_key_base64, strlen((const char *)pkam_public_key_base64)); atclient_atkeys_populate_pkam_private_key(&atkeys, (const char *)pkam_private_key_base64, @@ -120,14 +121,14 @@ int main(int argc, char *argv[]) { atclient_authenticate_options opts; atclient_authenticate_options_init(&opts); atclient_authenticate_options_set_atdirectory_host(&opts, root_host); + atclient_authenticate_options_set_atdirectory_port(&opts, root_port); atclient at_client; atclient_init(&at_client); - atclient_set_atsign(&at_client, atsign); // 2.2.1 Start new connection if ((ret = create_new_atserver_connection(&at_client, atsign, &opts)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "create_new_atserver_connection: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "create_new_atserver_connection: %d\n", ret); goto pkam_pub_keys_exit; } @@ -190,10 +191,11 @@ int main(int argc, char *argv[]) { // 2.5.1 base64 encode the encrypted APKAM symmetric key size_t encrypted_apkam_symmetric_key_base64_len = 0; - if ((ret = atchops_base64_encode( - (unsigned char *)encrypted_apkam_symmetric_key_bytes, sizeof(unsigned char) * rsa_2048_ciphertext_size, - (unsigned char *)encrypted_apkam_symmetric_key_base64, sizeof(unsigned char) * base64_encoded_rsa2048_ciphertext_size, - &encrypted_apkam_symmetric_key_base64_len)) != 0) { + if ((ret = atchops_base64_encode((unsigned char *)encrypted_apkam_symmetric_key_bytes, + sizeof(unsigned char) * rsa_2048_ciphertext_size, + (unsigned char *)encrypted_apkam_symmetric_key_base64, + sizeof(unsigned char) * base64_encoded_rsa2048_ciphertext_size, + &encrypted_apkam_symmetric_key_base64_len)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Failed base64 encoding encrypted_apkam_symmetric_key | atchops_base64_encode: %d\n", ret); goto enc_pub_key_exit; @@ -204,6 +206,10 @@ int main(int argc, char *argv[]) { */ // 3.1 Initialize and populate enrollment params structs atcommons_enroll_namespace_list_t *ns_list = malloc(sizeof(atcommons_enroll_namespace_list_t)); + if (ns_list == NULL) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Could not allocate memory for namespace list\n"); + goto enc_pub_key_exit; + } // 3.1.1 parse namespace list string passed through command-line args if ((ret = atcommons_enroll_namespace_list_from_string(&ns_list, namespaces_str)) != 0) { @@ -504,27 +510,23 @@ int is_enrollment_denied(const char *err_msg) { int create_new_atserver_connection(atclient *ctx, const char *atsign, const atclient_authenticate_options *options) { char *atserver_host = NULL; int atserver_port = 0, ret = 0; - if (options != NULL) { - if (atclient_authenticate_options_is_atdirectory_host_initialized(options) && - atclient_authenticate_options_is_atdirectory_port_initialized(options)) { - atserver_host = options->atdirectory_host; - atserver_port = options->atdirectory_port; - } - } if (atserver_host == NULL || atserver_port == 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, - "Missing atServer host or port. Using production atDirectory to look up atServer host and port\n"); - if ((ret = atclient_utils_find_atserver_address(ATCLIENT_ATDIRECTORY_PRODUCTION_HOST, - ATCLIENT_ATDIRECTORY_PRODUCTION_PORT, atsign, &atserver_host, - &atserver_port)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_utils_find_atserver_address: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_INFO, "Fetching secondary server address for atsign: %s\n", atsign); + if ((ret = atclient_utils_find_atserver_address(options->atdirectory_host, options->atdirectory_port, atsign, + &atserver_host, &atserver_port)) != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atclient_utils_find_atserver_address: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, + "Could not fetch secondary address for atsign: %s on root directory: %s:%d\n", atsign, + options->atdirectory_host, options->atdirectory_port); goto exit; } } if ((ret = atclient_start_atserver_connection(ctx, atserver_host, atserver_port)) != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_start_atserver_connection: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_DEBUG, "atclient_start_atserver_connection: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "Could not connect to secondary server at %s:%d\n", atserver_host, + atserver_port); } exit: { return ret; } diff --git a/packages/atauth/src/send_enroll_request.c b/packages/atauth/src/send_enroll_request.c index f8105bd5..164c0e19 100644 --- a/packages/atauth/src/send_enroll_request.c +++ b/packages/atauth/src/send_enroll_request.c @@ -19,7 +19,7 @@ int atauth_validate_send_enroll_request_arguments(const atclient *client, const int atauth_send_enroll_request(atclient *client, const atcommons_enroll_params_t *ep, char *enroll_id, char *enroll_status) { int ret = 0; - const size_t recv_size = 100; // to hold the response for enroll request + const size_t recv_size = 300; // to hold the response for enroll request unsigned char recv[recv_size]; char *recv_trimmed = NULL; size_t recv_len; diff --git a/packages/atclient/include/atclient/constants.h b/packages/atclient/include/atclient/constants.h index 7b7e5a82..ada5a654 100755 --- a/packages/atclient/include/atclient/constants.h +++ b/packages/atclient/include/atclient/constants.h @@ -20,10 +20,6 @@ extern "C" { #define ATCLIENT_ERR_AT0015_KEY_NOT_FOUND -0x1980 -// default param values for ATCLIENT PKAM AUTHENTICATE OPTIONS -#define ATCLIENT_DEFAULT_AT_DIRECTORY_HOST "root.atsign.org" -#define ATCLIENT_DEFAULT_AT_DIRECTORY_PORT 64 - #define ATCLIENT_DATA_TOKEN "data:" #define ATCLIENT_CRAM_PREFIX "cram" diff --git a/packages/atclient/include/atclient/request_options.h b/packages/atclient/include/atclient/request_options.h index 3da9718a..9a968941 100644 --- a/packages/atclient/include/atclient/request_options.h +++ b/packages/atclient/include/atclient/request_options.h @@ -32,10 +32,8 @@ extern "C" { #define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INDEX 0 #define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INDEX 0 -#define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_SHOULD_AUTH_INDEX 0 #define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_BYPASS_CACHE_INITIALIZED (VALUE_INITIALIZED << 0) #define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_STORE_ATKEY_METADATA_INITIALIZED (VALUE_INITIALIZED << 1) -#define ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_SHOULD_AUTH_INITIALIZED (VALUE_INITIALIZED << 2) #define ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_REGEX_INDEX 0 #define ATCLIENT_GET_ATKEYS_REQUEST_OPTIONS_SHOW_HIDDEN_INDEX 0 @@ -108,7 +106,6 @@ typedef struct atclient_get_shared_key_request_options { typedef struct atclient_get_public_key_request_options { bool bypass_cache; bool store_atkey_metadata; - bool should_auth; uint8_t _initialized_fields[1]; } atclient_get_public_key_request_options; @@ -233,12 +230,6 @@ int atclient_get_public_key_request_options_set_store_atkey_metadata(atclient_ge void atclient_get_public_key_request_options_unset_store_atkey_metadata( atclient_get_public_key_request_options *options); - bool atclient_get_public_key_request_options_is_should_auth_initialized( - const atclient_get_public_key_request_options *options); - int atclient_get_public_key_request_options_set_should_auth(atclient_get_public_key_request_options *options, - const bool should_auth); - void atclient_get_public_key_request_options_unset_should_auth(atclient_get_public_key_request_options *options); - /* * 3. Delete */ diff --git a/packages/atclient/src/atclient_get_public_key.c b/packages/atclient/src/atclient_get_public_key.c index 56a1c357..a87be362 100755 --- a/packages/atclient/src/atclient_get_public_key.c +++ b/packages/atclient/src/atclient_get_public_key.c @@ -48,6 +48,7 @@ int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char **va char *atkey_str_without_public = NULL; char *ptr = strstr(atkey_str, "public:"); + if (ptr != NULL) { atkey_str_without_public = ptr + strlen("public:"); } else { @@ -56,16 +57,13 @@ int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char **va goto exit; } - bool bypass_cache = false; - bool should_auth = true; - if (request_options != NULL) { - bypass_cache = atclient_get_public_key_request_options_is_bypass_cache_initialized(request_options) && - request_options->bypass_cache; - should_auth = atclient_get_public_key_request_options_is_should_auth_initialized(request_options) && - request_options->should_auth; - } + const bool bypass_cache = atclient_get_public_key_request_options_is_bypass_cache_initialized(request_options) && + request_options->bypass_cache; + // use plookup verb if atclient instance is authenticated (or) lookup verb otherwise + // if the atsign var is set in the atclient instance, that is considered authenticated + const bool authenticated_lookup = atclient->atsign == NULL ? false : true; + char *verb = authenticated_lookup ? "plookup" : "lookup"; - char *verb = should_auth ? "plookup" : "lookup"; const size_t lookup_cmd_size = strlen(verb) + strlen(":all:\r\n") + (bypass_cache ? strlen("bypassCache:true:") : 0) + strlen(atkey_str_without_public) + 1; @@ -132,7 +130,7 @@ int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char **va metadata_str = cJSON_Print(metadata); - if ((ret = atclient_atkey_metadata_from_json_str(&(atkey->metadata), metadata_str)) != 0) { + if ((ret = atclient_atkey_metadata_from_json_str(&atkey->metadata, metadata_str)) != 0) { atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atclient_atkey_metadata_from_json_str: %d\n", ret); goto exit; } @@ -149,7 +147,6 @@ int atclient_get_public_key(atclient *atclient, atclient_atkey *atkey, char **va } ret = 0; - goto exit; exit: { if (root != NULL) { cJSON_Delete(root); @@ -177,12 +174,6 @@ static int atclient_get_public_key_validate_arguments(const atclient *atclient, goto exit; } - if (!atclient_is_atsign_initialized(atclient)) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atsign not initialized\n"); - goto exit; - } - if (atkey == NULL) { ret = 1; atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atkey is NULL\n"); diff --git a/packages/atclient/src/request_options.c b/packages/atclient/src/request_options.c index aa6818d7..6eb8c250 100644 --- a/packages/atclient/src/request_options.c +++ b/packages/atclient/src/request_options.c @@ -1249,101 +1249,6 @@ void atclient_get_public_key_request_options_unset_store_atkey_metadata( atclient_get_public_key_request_options_set_store_atkey_metadata_initialized(options, false); } -bool atclient_get_public_key_request_options_is_should_auth_initialized( - const atclient_get_public_key_request_options *options) { - /* - * 1. Validate arguments - */ - if (options == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_public_key_request_options_is_should_auth_initialized: " - "Invalid arguments\n"); - return false; - } - - /* - * 2. Check if the bypass cache is initialized - */ - return options->_initialized_fields[ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_SHOULD_AUTH_INDEX] & - ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_SHOULD_AUTH_INITIALIZED; -} - -static void -atclient_get_public_key_request_options_set_should_auth_initialized(atclient_get_public_key_request_options *options, - const bool initialized) { - /* - * 1. Validate arguments - */ - if (options == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_public_key_request_options_set_should_auth_initialized: " - "Invalid arguments\n"); - return; - } - - /* - * 2. Set the bypass cache initialized - */ - if (initialized) { - options->_initialized_fields[ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_SHOULD_AUTH_INDEX] |= - ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_SHOULD_AUTH_INITIALIZED; - } else { - options->_initialized_fields[ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_SHOULD_AUTH_INDEX] &= - ~ATCLIENT_GET_PUBLIC_KEY_REQUEST_OPTIONS_SHOULD_AUTH_INITIALIZED; - } -} - -int atclient_get_public_key_request_options_set_should_auth(atclient_get_public_key_request_options *options, - const bool should_auth) { - int ret = 1; - - /* - * 1. Validate arguments - */ - if (options == NULL) { - ret = 1; - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_public_key_request_options_set_should_auth: " - "Invalid arguments\n"); - return ret; - } - - /* - * 2. Unset the bypass cache, if necessary - */ - if (atclient_get_public_key_request_options_is_should_auth_initialized(options)) { - atclient_get_public_key_request_options_unset_should_auth(options); - } - - /* - * 3. Set the bypass cache - */ - options->should_auth = should_auth; - atclient_get_public_key_request_options_set_should_auth_initialized(options, true); - - ret = 0; - goto exit; -exit: { return ret; } -} - -void atclient_get_public_key_request_options_unset_should_auth(atclient_get_public_key_request_options *options) { - /* - * 1. Validate arguments - */ - if (options == NULL) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, - "atclient_get_public_key_request_options_unset_bypass_cache: " - "Invalid arguments\n"); - return; - } - - /* - * 2. Unset the bypass cache - */ - options->should_auth = true; - atclient_get_public_key_request_options_set_bypass_cache_initialized(options, false); -} - void atclient_delete_request_options_init(atclient_delete_request_options *options) { /* * 1. Validate arguments diff --git a/packages/atcommons/tests/at_expect.c b/packages/atcommons/tests/at_expect.c index 8d4b314b..0359cdfd 100644 --- a/packages/atcommons/tests/at_expect.c +++ b/packages/atcommons/tests/at_expect.c @@ -6,7 +6,8 @@ int atcommons_string_expect(char *actual, char *expected) { int ret = strcmp(actual, expected); if (ret != 0) { - printf("test failed\nexpected: %s\n*actual*: %s\n", expected, actual); + printf("test failed | ret: %d\nexpected: %s\n*actual*: %s\n", ret, expected, actual); + printf("expected_len: %lu \t actual_len: %lu", strlen(expected), strlen(actual)); } return ret; diff --git a/packages/atcommons/tests/test_enroll_command_builder.c b/packages/atcommons/tests/test_enroll_command_builder.c index df390cd9..443bc0ad 100644 --- a/packages/atcommons/tests/test_enroll_command_builder.c +++ b/packages/atcommons/tests/test_enroll_command_builder.c @@ -10,7 +10,7 @@ int main() { int ret = 1; char expected_string[] = "enroll:request:{\"appName\":\"test-app\",\"deviceName\":\"test-device\",\"otp\":\"XYZABC\"," - "\"namespaces\":{\"namespace1\":\"rw\",\"namespace2\":\"r\"}}"; + "\"namespaces\":{\"namespace1\":\"rw\",\"namespace2\":\"r\"}}\r\n"; atcommons_enroll_namespace_t namespace; namespace.name = "namespace1"; namespace.access = "rw"; diff --git a/packages/atcommons/tests/test_enroll_namespace_utils.c b/packages/atcommons/tests/test_enroll_namespace_utils.c index 13636dbf..e981870f 100644 --- a/packages/atcommons/tests/test_enroll_namespace_utils.c +++ b/packages/atcommons/tests/test_enroll_namespace_utils.c @@ -14,15 +14,18 @@ int test_enroll_namespace_list_from_string(); int main() { int ret = test_enroll_namespace_to_json(); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "%s failed\n", "test_enroll_namespace_to_json"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "%s failed\n", "test_enroll_namespace_to_json: %d", ret); return ret; } ret = test_enroll_namespace_list_to_json(); if(ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "%s failed\n", "test_enroll_namespace_list_to_json"); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "%s failed\n", "test_enroll_namespace_list_to_json: %d", ret); return ret; } ret = test_enroll_namespace_list_from_string(); + if(ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "%s failed\n", "test_enroll_namespace_list_from_string: %d", ret); + } return ret; } @@ -94,16 +97,28 @@ int test_enroll_namespace_list_to_json() { int test_enroll_namespace_list_from_string() { char *nsl_str_1 = "ns1:rw,ns2:r"; char *nsl_str_2 = "ns3:rw"; + char *nsl_invalid_str_1 = "ns4"; + char *nsl_invalid_str_2 = "ns5:"; atcommons_enroll_namespace_list_t *nsl = malloc(sizeof(atcommons_enroll_namespace_list_t)); int ret = atcommons_enroll_namespace_list_from_string(&nsl, nsl_str_1); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atcommons_enroll_namespace_list_from_string: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atcommons_enroll_namespace_list_from_string(ns string 1): %d\n", ret); return ret; } ret = atcommons_enroll_namespace_list_from_string(&nsl, nsl_str_2); if (ret != 0) { - atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atcommons_enroll_namespace_list_from_string: %d\n", ret); + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atcommons_enroll_namespace_list_from_string(ns string 2): %d\n", ret); + return ret; + } + ret = atcommons_enroll_namespace_list_from_string(&nsl, nsl_invalid_str_1); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atcommons_enroll_namespace_list_from_string(ns string 3): %d\n", ret); + return ret; + } + ret = atcommons_enroll_namespace_list_from_string(&nsl, nsl_invalid_str_2); + if (ret != 0) { + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "atcommons_enroll_namespace_list_from_string(ns string 4): %d\n", ret); return ret; } @@ -125,5 +140,17 @@ int test_enroll_namespace_list_from_string() { return ret; } + // following two test cases are negative. Them being NULL is expected behaviour + if(nsl->namespaces[3]->name != NULL || nsl->namespaces[3]->access != NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_enroll_namespace_list_from_string case 4: failed\n"); + return ret; + } + if(nsl->namespaces[4]->name != NULL || nsl->namespaces[4]->access != NULL) { + ret = 1; + atlogger_log(TAG, ATLOGGER_LOGGING_LEVEL_ERROR, "test_enroll_namespace_list_from_string case 5: failed\n"); + return ret; + } + return ret; } \ No newline at end of file