tests/libtest/cli*: fix init/deinit, leaks, and more

- add global init and deinit where missing.
- check global init success.
- improve cleaning up on error codepaths.
- drop `CLI_ERR()` macro, that could quit.
  Also make error messages tell the reason.

Closes #19309
This commit is contained in:
Viktor Szakats 2025-10-31 17:36:27 +01:00
parent d29f14b9cf
commit 70f240b2ed
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
9 changed files with 224 additions and 127 deletions

View file

@ -85,8 +85,8 @@ static void usage_upload_pausing(const char *msg)
static CURLcode test_cli_upload_pausing(const char *URL)
{
CURL *curl;
CURLcode rc = CURLE_OK;
CURL *curl = NULL;
CURLcode result = CURLE_OK;
CURLU *cu;
struct curl_slist *resolve = NULL;
char resolve_buf[1024];
@ -126,25 +126,33 @@ static CURLcode test_cli_upload_pausing(const char *URL)
}
url = test_argv[0];
curl_global_init(CURL_GLOBAL_DEFAULT);
if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
curl_mfprintf(stderr, "curl_global_init() failed\n");
return (CURLcode)3;
}
curl_global_trace("ids,time");
cu = curl_url();
if(!cu) {
curl_mfprintf(stderr, "out of memory\n");
return (CURLcode)1;
result = (CURLcode)1;
goto cleanup;
}
if(curl_url_set(cu, CURLUPART_URL, url, 0)) {
curl_mfprintf(stderr, "not a URL: '%s'\n", url);
return (CURLcode)1;
result = (CURLcode)1;
goto cleanup;
}
if(curl_url_get(cu, CURLUPART_HOST, &host, 0)) {
curl_mfprintf(stderr, "could not get host of '%s'\n", url);
return (CURLcode)1;
result = (CURLcode)1;
goto cleanup;
}
if(curl_url_get(cu, CURLUPART_PORT, &port, 0)) {
curl_mfprintf(stderr, "could not get port of '%s'\n", url);
return (CURLcode)1;
result = (CURLcode)1;
goto cleanup;
}
memset(&resolve, 0, sizeof(resolve));
curl_msnprintf(resolve_buf, sizeof(resolve_buf)-1, "%s:%s:127.0.0.1",
@ -154,7 +162,8 @@ static CURLcode test_cli_upload_pausing(const char *URL)
curl = curl_easy_init();
if(!curl) {
curl_mfprintf(stderr, "out of memory\n");
return (CURLcode)1;
result = (CURLcode)1;
goto cleanup;
}
/* We want to use our own read function. */
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
@ -181,23 +190,25 @@ static CURLcode test_cli_upload_pausing(const char *URL)
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, cli_debug_cb) != CURLE_OK ||
curl_easy_setopt(curl, CURLOPT_RESOLVE, resolve) != CURLE_OK) {
curl_mfprintf(stderr, "something unexpected went wrong - bailing out!\n");
return (CURLcode)2;
result = (CURLcode)2;
goto cleanup;
}
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, http_version);
rc = curl_easy_perform(curl);
result = curl_easy_perform(curl);
if(curl) {
cleanup:
if(curl)
curl_easy_cleanup(curl);
}
curl_slist_free_all(resolve);
curl_free(host);
curl_free(port);
curl_url_cleanup(cu);
if(cu)
curl_url_cleanup(cu);
curl_global_cleanup();
return rc;
return result;
}