Path: blob/main/external/curl/tests/libtest/cli_h2_pausing.c
2649 views
/***************************************************************************1* _ _ ____ _2* Project ___| | | | _ \| |3* / __| | | | |_) | |4* | (__| |_| | _ <| |___5* \___|\___/|_| \_\_____|6*7* Copyright (C) Daniel Stenberg, <[email protected]>, et al.8*9* This software is licensed as described in the file COPYING, which10* you should have received as part of this distribution. The terms11* are also available at https://curl.se/docs/copyright.html.12*13* You may opt to use, copy, modify, merge, publish, distribute and/or sell14* copies of the Software, and permit persons to whom the Software is15* furnished to do so, under the terms of the COPYING file.16*17* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY18* KIND, either express or implied.19*20* SPDX-License-Identifier: curl21*22***************************************************************************/23/* This is based on the PoC client of issue #1198224*/25#include "first.h"2627#include "testtrace.h"28#include "memdebug.h"2930static void usage_h2_pausing(const char *msg)31{32if(msg)33curl_mfprintf(stderr, "%s\n", msg);34curl_mfprintf(stderr,35"usage: [options] url\n"36" pause downloads with following options:\n"37" -V http_version (http/1.1, h2, h3) http version to use\n"38);39}4041struct handle42{43size_t idx;44int paused;45int resumed;46int errored;47int fail_write;48CURL *curl;49};5051static size_t cb(char *data, size_t size, size_t nmemb, void *clientp)52{53size_t realsize = size * nmemb;54struct handle *handle = (struct handle *) clientp;55curl_off_t totalsize;5657(void)data;58if(curl_easy_getinfo(handle->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T,59&totalsize) == CURLE_OK)60curl_mfprintf(stderr, "INFO: [%zu] write, "61"Content-Length %" CURL_FORMAT_CURL_OFF_T "\n",62handle->idx, totalsize);6364if(!handle->resumed) {65++handle->paused;66curl_mfprintf(stderr, "INFO: [%zu] write, PAUSING %d time on %zu bytes\n",67handle->idx, handle->paused, realsize);68assert(handle->paused == 1);69return CURL_WRITEFUNC_PAUSE;70}71if(handle->fail_write) {72++handle->errored;73curl_mfprintf(stderr, "INFO: [%zu] FAIL write of %zu bytes, %d time\n",74handle->idx, realsize, handle->errored);75return CURL_WRITEFUNC_ERROR;76}77curl_mfprintf(stderr, "INFO: [%zu] write, accepting %zu bytes\n",78handle->idx, realsize);79return realsize;80}8182static CURLcode test_cli_h2_pausing(const char *URL)83{84struct handle handles[2];85CURLM *multi = NULL;86int still_running = 1, msgs_left, numfds;87size_t i;88CURLMsg *msg;89int rounds = 0;90CURLcode result = CURLE_OK;91CURLU *cu;92struct curl_slist *resolve = NULL;93char resolve_buf[1024];94const char *url;95char *host = NULL, *port = NULL;96int all_paused = 0;97int resume_round = -1;98long http_version = CURL_HTTP_VERSION_2_0;99int ch;100101(void)URL;102103while((ch = cgetopt(test_argc, test_argv, "hV:")) != -1) {104switch(ch) {105case 'h':106usage_h2_pausing(NULL);107return (CURLcode)2;108case 'V': {109if(!strcmp("http/1.1", coptarg))110http_version = CURL_HTTP_VERSION_1_1;111else if(!strcmp("h2", coptarg))112http_version = CURL_HTTP_VERSION_2_0;113else if(!strcmp("h3", coptarg))114http_version = CURL_HTTP_VERSION_3ONLY;115else {116usage_h2_pausing("invalid http version");117return (CURLcode)1;118}119break;120}121default:122usage_h2_pausing("invalid option");123return (CURLcode)1;124}125}126test_argc -= coptind;127test_argv += coptind;128129if(test_argc != 1) {130curl_mfprintf(stderr, "ERROR: need URL as argument\n");131return (CURLcode)2;132}133url = test_argv[0];134135if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {136curl_mfprintf(stderr, "curl_global_init() failed\n");137return (CURLcode)3;138}139140curl_global_trace("ids,time,http/2,http/3");141142memset(handles, 0, sizeof(handles));143144cu = curl_url();145if(!cu) {146curl_mfprintf(stderr, "out of memory\n");147result = (CURLcode)1;148goto cleanup;149}150if(curl_url_set(cu, CURLUPART_URL, url, 0)) {151curl_mfprintf(stderr, "not a URL: '%s'\n", url);152result = (CURLcode)1;153goto cleanup;154}155if(curl_url_get(cu, CURLUPART_HOST, &host, 0)) {156curl_mfprintf(stderr, "could not get host of '%s'\n", url);157result = (CURLcode)1;158goto cleanup;159}160if(curl_url_get(cu, CURLUPART_PORT, &port, 0)) {161curl_mfprintf(stderr, "could not get port of '%s'\n", url);162result = (CURLcode)1;163goto cleanup;164}165memset(&resolve, 0, sizeof(resolve));166curl_msnprintf(resolve_buf, sizeof(resolve_buf)-1, "%s:%s:127.0.0.1",167host, port);168resolve = curl_slist_append(resolve, resolve_buf);169170for(i = 0; i < CURL_ARRAYSIZE(handles); i++) {171handles[i].idx = i;172handles[i].paused = 0;173handles[i].resumed = 0;174handles[i].errored = 0;175handles[i].fail_write = 1;176handles[i].curl = curl_easy_init();177if(!handles[i].curl ||178curl_easy_setopt(handles[i].curl, CURLOPT_WRITEFUNCTION, cb)179!= CURLE_OK ||180curl_easy_setopt(handles[i].curl, CURLOPT_WRITEDATA, &handles[i])181!= CURLE_OK ||182curl_easy_setopt(handles[i].curl, CURLOPT_FOLLOWLOCATION, 1L)183!= CURLE_OK ||184curl_easy_setopt(handles[i].curl, CURLOPT_VERBOSE, 1L) != CURLE_OK ||185curl_easy_setopt(handles[i].curl, CURLOPT_DEBUGFUNCTION, cli_debug_cb)186!= CURLE_OK ||187curl_easy_setopt(handles[i].curl, CURLOPT_SSL_VERIFYPEER, 0L)188!= CURLE_OK ||189curl_easy_setopt(handles[i].curl, CURLOPT_RESOLVE, resolve)190!= CURLE_OK ||191curl_easy_setopt(handles[i].curl, CURLOPT_PIPEWAIT, 1L) != CURLE_OK ||192curl_easy_setopt(handles[i].curl, CURLOPT_URL, url) != CURLE_OK) {193curl_mfprintf(stderr, "failed configuring easy handle - bailing out\n");194result = (CURLcode)2;195goto cleanup;196}197curl_easy_setopt(handles[i].curl, CURLOPT_HTTP_VERSION, http_version);198}199200multi = curl_multi_init();201if(!multi) {202curl_mfprintf(stderr, "curl_multi_init() failed - bailing out\n");203result = (CURLcode)2;204goto cleanup;205}206207for(i = 0; i < CURL_ARRAYSIZE(handles); i++) {208if(curl_multi_add_handle(multi, handles[i].curl) != CURLM_OK) {209curl_mfprintf(stderr, "curl_multi_add_handle() failed - bailing out\n");210result = (CURLcode)2;211goto cleanup;212}213}214215for(rounds = 0;; rounds++) {216curl_mfprintf(stderr, "INFO: multi_perform round %d\n", rounds);217if(curl_multi_perform(multi, &still_running) != CURLM_OK) {218curl_mfprintf(stderr, "curl_multi_perform() failed - bailing out\n");219result = (CURLcode)2;220goto cleanup;221}222223if(!still_running) {224int as_expected = 1;225curl_mfprintf(stderr, "INFO: no more handles running\n");226for(i = 0; i < CURL_ARRAYSIZE(handles); i++) {227if(!handles[i].paused) {228curl_mfprintf(stderr, "ERROR: [%zu] NOT PAUSED\n", i);229as_expected = 0;230}231else if(handles[i].paused != 1) {232curl_mfprintf(stderr, "ERROR: [%zu] PAUSED %d times!\n",233i, handles[i].paused);234as_expected = 0;235}236else if(!handles[i].resumed) {237curl_mfprintf(stderr, "ERROR: [%zu] NOT resumed!\n", i);238as_expected = 0;239}240else if(handles[i].errored != 1) {241curl_mfprintf(stderr, "ERROR: [%zu] NOT errored once, %d instead!\n",242i, handles[i].errored);243as_expected = 0;244}245}246if(!as_expected) {247curl_mfprintf(stderr, "ERROR: handles not in expected state "248"after %d rounds\n", rounds);249result = (CURLcode)1;250}251break;252}253254if(curl_multi_poll(multi, NULL, 0, 100, &numfds) != CURLM_OK) {255curl_mfprintf(stderr, "curl_multi_poll() failed - bailing out\n");256result = (CURLcode)2;257goto cleanup;258}259260/* !checksrc! disable EQUALSNULL 1 */261while((msg = curl_multi_info_read(multi, &msgs_left)) != NULL) {262if(msg->msg == CURLMSG_DONE) {263for(i = 0; i < CURL_ARRAYSIZE(handles); i++) {264if(msg->easy_handle == handles[i].curl) {265if(handles[i].paused != 1 || !handles[i].resumed) {266curl_mfprintf(stderr, "ERROR: [%zu] done, paused=%d, "267"resumed=%d, result %d - wtf?\n", i,268handles[i].paused,269handles[i].resumed, msg->data.result);270result = (CURLcode)1;271goto cleanup;272}273}274}275}276}277278/* Successfully paused? */279if(!all_paused) {280for(i = 0; i < CURL_ARRAYSIZE(handles); i++) {281if(!handles[i].paused) {282break;283}284}285all_paused = (i == CURL_ARRAYSIZE(handles));286if(all_paused) {287curl_mfprintf(stderr, "INFO: all transfers paused\n");288/* give transfer some rounds to mess things up */289resume_round = rounds + 2;290}291}292if(resume_round > 0 && rounds == resume_round) {293/* time to resume */294for(i = 0; i < CURL_ARRAYSIZE(handles); i++) {295curl_mfprintf(stderr, "INFO: [%zu] resumed\n", i);296handles[i].resumed = 1;297curl_easy_pause(handles[i].curl, CURLPAUSE_CONT);298}299}300}301302cleanup:303304for(i = 0; i < CURL_ARRAYSIZE(handles); i++) {305curl_multi_remove_handle(multi, handles[i].curl);306curl_easy_cleanup(handles[i].curl);307}308309curl_slist_free_all(resolve);310curl_free(host);311curl_free(port);312curl_url_cleanup(cu);313curl_multi_cleanup(multi);314curl_global_cleanup();315316return result;317}318319320