diff --git a/src/common.c b/src/common.c index 86e33ce7..1a95e9cb 100644 --- a/src/common.c +++ b/src/common.c @@ -819,11 +819,11 @@ url_readdata(void *ptr, size_t size, size_t nmemb, void *userdata) * * @param[in] curl CURL struct to modify. */ -static void +static CURLcode url_set_protocols(CURL *curl) { #if CURL_AT_LEAST_VERSION(7, 85, 0) - curl_easy_setopt(curl, CURLOPT_PROTOCOLS_STR, np2srv.url_protocols); + return curl_easy_setopt(curl, CURLOPT_PROTOCOLS_STR, np2srv.url_protocols); #else long proto = 0; char *ptr, *ptr2; @@ -854,7 +854,7 @@ url_set_protocols(CURL *curl) ptr = ptr2 + 1; } while (ptr2[0]); - curl_easy_setopt(curl, CURLOPT_PROTOCOLS, proto); + return curl_easy_setopt(curl, CURLOPT_PROTOCOLS, proto); #endif } @@ -871,7 +871,8 @@ url_get(const struct ly_ctx *ly_ctx, const char *url, char **url_data) { struct nc_server_reply *reply = NULL; CURL *curl; - char curl_buffer[CURL_ERROR_SIZE]; + CURLcode res; + char curl_buffer[CURL_ERROR_SIZE] = {0}; struct np_url_mem mem_data = {0}; if (!np2srv.url_protocols) { @@ -884,14 +885,26 @@ url_get(const struct ly_ctx *ly_ctx, const char *url, char **url_data) /* set up libcurl */ curl_global_init(URL_INIT_FLAGS); curl = curl_easy_init(); - url_set_protocols(curl); - curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, url_writedata); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, &mem_data); - curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_buffer); - - /* download data */ - if (curl_easy_perform(curl) != CURLE_OK) { + res = url_set_protocols(curl); + if (res == CURLE_OK) { + res = curl_easy_setopt(curl, CURLOPT_URL, url); + } + if (res == CURLE_OK) { + res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, url_writedata); + } + if (res == CURLE_OK) { + res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &mem_data); + } + if (res == CURLE_OK) { + res = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_buffer); + } + + if (res == CURLE_OK) { + /* download data */ + res = curl_easy_perform(curl); + } + + if (res != CURLE_OK) { ERR("Failed to download data (curl: %s).", curl_buffer); reply = np_reply_err_op_failed(NULL, ly_ctx, curl_buffer); goto cleanup; @@ -968,7 +981,7 @@ np_op_export_url(const struct ly_ctx *ly_ctx, const char *url, struct lyd_node * CURL *curl; struct np_url_mem mem_data; CURLcode r = 0; - char curl_buffer[CURL_ERROR_SIZE], *str_data = NULL; + char curl_buffer[CURL_ERROR_SIZE] = {0}, *str_data = NULL; struct lyd_node *config; if (!np2srv.url_protocols) { @@ -998,7 +1011,7 @@ np_op_export_url(const struct ly_ctx *ly_ctx, const char *url, struct lyd_node * /* set up libcurl */ curl_global_init(URL_INIT_FLAGS); curl = curl_easy_init(); - url_set_protocols(curl); + r = url_set_protocols(curl); if (!r) { r = curl_easy_setopt(curl, CURLOPT_URL, url); } diff --git a/tests/test_url.c b/tests/test_url.c index 47b521bd..4a5d95f2 100644 --- a/tests/test_url.c +++ b/tests/test_url.c @@ -32,6 +32,7 @@ #include "np2_test.h" #include "np2_test_config.h" +#ifdef NP2SRV_URL_FILE_PROTO static int setup_nacm_rules(void **state) { @@ -240,6 +241,7 @@ test_copy_config_into_file(void **state) /* Get file size */ fseek(file, 0, SEEK_END); size = ftell(file); + assert_true(size > 0); rewind(file); /* Allcate buffer */ @@ -331,6 +333,7 @@ test_copy_config_url2url(void **state) /* Get file size */ fseek(file, 0, SEEK_END); size = ftell(file); + assert_true(size > 0); rewind(file); /* Allcate buffer */ @@ -426,10 +429,12 @@ test_edit_config(void **state) FREE_TEST_VARS(st); } +#endif /* NP2SRV_URL_FILE_PROTO */ int main(int argc, char **argv) { +#ifdef NP2SRV_URL_FILE_PROTO const struct CMUnitTest tests[] = { cmocka_unit_test(test_validate), cmocka_unit_test_teardown(test_copy_config, teardown_data), @@ -438,6 +443,7 @@ main(int argc, char **argv) cmocka_unit_test(test_copy_config_url2url), cmocka_unit_test_teardown(test_edit_config, teardown_data), }; +#endif if (np2_is_nacm_recovery()) { puts("Running as NACM_RECOVERY_USER. Tests will not run correctly as this user bypases NACM. Skipping."); @@ -445,11 +451,13 @@ main(int argc, char **argv) } #ifndef NP2SRV_URL_FILE_PROTO + (void) argc; + (void) argv; puts("File protocol disabled, no tests to run. Skipping."); return 0; -#endif - +#else nc_verbosity(NC_VERB_WARNING); parse_arg(argc, argv); return cmocka_run_group_tests(tests, local_setup, local_teardown); +#endif }