Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}

Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 10 additions & 2 deletions tests/test_url.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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),
Expand All @@ -438,18 +443,21 @@ 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.");
return 0;
}

#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
}
Loading