113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci#include "test.h" 2513498266Sopenharmony_ci 2613498266Sopenharmony_ci#include <limits.h> 2713498266Sopenharmony_ci#include <assert.h> 2813498266Sopenharmony_ci 2913498266Sopenharmony_ci#include "testutil.h" 3013498266Sopenharmony_ci#include "warnless.h" 3113498266Sopenharmony_ci#include "memdebug.h" 3213498266Sopenharmony_ci 3313498266Sopenharmony_ci#define TEST_HANG_TIMEOUT 60 * 1000 3413498266Sopenharmony_ci#define MAX_EASY_HANDLES 3 3513498266Sopenharmony_ci 3613498266Sopenharmony_cistatic int counter[MAX_EASY_HANDLES]; 3713498266Sopenharmony_cistatic CURL *easy[MAX_EASY_HANDLES]; 3813498266Sopenharmony_cistatic curl_socket_t sockets[MAX_EASY_HANDLES]; 3913498266Sopenharmony_cistatic int res = 0; 4013498266Sopenharmony_ci 4113498266Sopenharmony_cistatic size_t callback(char *ptr, size_t size, size_t nmemb, void *data) 4213498266Sopenharmony_ci{ 4313498266Sopenharmony_ci ssize_t idx = ((CURL **) data) - easy; 4413498266Sopenharmony_ci curl_socket_t sock; 4513498266Sopenharmony_ci long longdata; 4613498266Sopenharmony_ci CURLcode code; 4713498266Sopenharmony_ci const size_t failure = (size && nmemb) ? 0 : 1; 4813498266Sopenharmony_ci (void)ptr; 4913498266Sopenharmony_ci 5013498266Sopenharmony_ci counter[idx] += (int)(size * nmemb); 5113498266Sopenharmony_ci 5213498266Sopenharmony_ci /* Get socket being used for this easy handle, otherwise CURL_SOCKET_BAD */ 5313498266Sopenharmony_ci CURL_IGNORE_DEPRECATION( 5413498266Sopenharmony_ci code = curl_easy_getinfo(easy[idx], CURLINFO_LASTSOCKET, &longdata); 5513498266Sopenharmony_ci ) 5613498266Sopenharmony_ci if(CURLE_OK != code) { 5713498266Sopenharmony_ci fprintf(stderr, "%s:%d curl_easy_getinfo() failed, " 5813498266Sopenharmony_ci "with code %d (%s)\n", 5913498266Sopenharmony_ci __FILE__, __LINE__, (int)code, curl_easy_strerror(code)); 6013498266Sopenharmony_ci res = TEST_ERR_MAJOR_BAD; 6113498266Sopenharmony_ci return failure; 6213498266Sopenharmony_ci } 6313498266Sopenharmony_ci if(longdata == -1L) 6413498266Sopenharmony_ci sock = CURL_SOCKET_BAD; 6513498266Sopenharmony_ci else 6613498266Sopenharmony_ci sock = (curl_socket_t)longdata; 6713498266Sopenharmony_ci 6813498266Sopenharmony_ci if(sock != CURL_SOCKET_BAD) { 6913498266Sopenharmony_ci /* Track relationship between this easy handle and the socket. */ 7013498266Sopenharmony_ci if(sockets[idx] == CURL_SOCKET_BAD) { 7113498266Sopenharmony_ci /* An easy handle without previous socket, record the socket. */ 7213498266Sopenharmony_ci sockets[idx] = sock; 7313498266Sopenharmony_ci } 7413498266Sopenharmony_ci else if(sock != sockets[idx]) { 7513498266Sopenharmony_ci /* An easy handle with a socket different to previously 7613498266Sopenharmony_ci tracked one, log and fail right away. Known bug #37. */ 7713498266Sopenharmony_ci fprintf(stderr, "Handle %d started on socket %d and moved to %d\n", 7813498266Sopenharmony_ci curlx_sztosi(idx), (int)sockets[idx], (int)sock); 7913498266Sopenharmony_ci res = TEST_ERR_MAJOR_BAD; 8013498266Sopenharmony_ci return failure; 8113498266Sopenharmony_ci } 8213498266Sopenharmony_ci } 8313498266Sopenharmony_ci return size * nmemb; 8413498266Sopenharmony_ci} 8513498266Sopenharmony_ci 8613498266Sopenharmony_cienum HandleState { 8713498266Sopenharmony_ci ReadyForNewHandle, 8813498266Sopenharmony_ci NeedSocketForNewHandle, 8913498266Sopenharmony_ci NoMoreHandles 9013498266Sopenharmony_ci}; 9113498266Sopenharmony_ci 9213498266Sopenharmony_ciint test(char *url) 9313498266Sopenharmony_ci{ 9413498266Sopenharmony_ci CURLM *multi = NULL; 9513498266Sopenharmony_ci int running; 9613498266Sopenharmony_ci int i; 9713498266Sopenharmony_ci int num_handles = 0; 9813498266Sopenharmony_ci enum HandleState state = ReadyForNewHandle; 9913498266Sopenharmony_ci size_t urllen = strlen(url) + 4 + 1; 10013498266Sopenharmony_ci char *full_url = malloc(urllen); 10113498266Sopenharmony_ci 10213498266Sopenharmony_ci start_test_timing(); 10313498266Sopenharmony_ci 10413498266Sopenharmony_ci if(!full_url) { 10513498266Sopenharmony_ci fprintf(stderr, "Not enough memory for full url\n"); 10613498266Sopenharmony_ci return TEST_ERR_MAJOR_BAD; 10713498266Sopenharmony_ci } 10813498266Sopenharmony_ci 10913498266Sopenharmony_ci for(i = 0; i < MAX_EASY_HANDLES; ++i) { 11013498266Sopenharmony_ci easy[i] = NULL; 11113498266Sopenharmony_ci sockets[i] = CURL_SOCKET_BAD; 11213498266Sopenharmony_ci } 11313498266Sopenharmony_ci 11413498266Sopenharmony_ci res_global_init(CURL_GLOBAL_ALL); 11513498266Sopenharmony_ci if(res) { 11613498266Sopenharmony_ci free(full_url); 11713498266Sopenharmony_ci return res; 11813498266Sopenharmony_ci } 11913498266Sopenharmony_ci 12013498266Sopenharmony_ci multi_init(multi); 12113498266Sopenharmony_ci 12213498266Sopenharmony_ci for(;;) { 12313498266Sopenharmony_ci struct timeval interval; 12413498266Sopenharmony_ci fd_set fdread; 12513498266Sopenharmony_ci fd_set fdwrite; 12613498266Sopenharmony_ci fd_set fdexcep; 12713498266Sopenharmony_ci long timeout = -99; 12813498266Sopenharmony_ci int maxfd = -99; 12913498266Sopenharmony_ci bool found_new_socket = FALSE; 13013498266Sopenharmony_ci 13113498266Sopenharmony_ci /* Start a new handle if we aren't at the max */ 13213498266Sopenharmony_ci if(state == ReadyForNewHandle) { 13313498266Sopenharmony_ci easy_init(easy[num_handles]); 13413498266Sopenharmony_ci 13513498266Sopenharmony_ci if(num_handles % 3 == 2) { 13613498266Sopenharmony_ci msnprintf(full_url, urllen, "%s0200", url); 13713498266Sopenharmony_ci easy_setopt(easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_NTLM); 13813498266Sopenharmony_ci } 13913498266Sopenharmony_ci else { 14013498266Sopenharmony_ci msnprintf(full_url, urllen, "%s0100", url); 14113498266Sopenharmony_ci easy_setopt(easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 14213498266Sopenharmony_ci } 14313498266Sopenharmony_ci easy_setopt(easy[num_handles], CURLOPT_FRESH_CONNECT, 1L); 14413498266Sopenharmony_ci easy_setopt(easy[num_handles], CURLOPT_URL, full_url); 14513498266Sopenharmony_ci easy_setopt(easy[num_handles], CURLOPT_VERBOSE, 1L); 14613498266Sopenharmony_ci easy_setopt(easy[num_handles], CURLOPT_HTTPGET, 1L); 14713498266Sopenharmony_ci easy_setopt(easy[num_handles], CURLOPT_USERPWD, "testuser:testpass"); 14813498266Sopenharmony_ci easy_setopt(easy[num_handles], CURLOPT_WRITEFUNCTION, callback); 14913498266Sopenharmony_ci easy_setopt(easy[num_handles], CURLOPT_WRITEDATA, easy + num_handles); 15013498266Sopenharmony_ci easy_setopt(easy[num_handles], CURLOPT_HEADER, 1L); 15113498266Sopenharmony_ci 15213498266Sopenharmony_ci multi_add_handle(multi, easy[num_handles]); 15313498266Sopenharmony_ci num_handles += 1; 15413498266Sopenharmony_ci state = NeedSocketForNewHandle; 15513498266Sopenharmony_ci } 15613498266Sopenharmony_ci 15713498266Sopenharmony_ci multi_perform(multi, &running); 15813498266Sopenharmony_ci 15913498266Sopenharmony_ci fprintf(stderr, "%s:%d running %d state %d\n", 16013498266Sopenharmony_ci __FILE__, __LINE__, running, state); 16113498266Sopenharmony_ci 16213498266Sopenharmony_ci abort_on_test_timeout(); 16313498266Sopenharmony_ci 16413498266Sopenharmony_ci if(!running && state == NoMoreHandles) 16513498266Sopenharmony_ci break; /* done */ 16613498266Sopenharmony_ci 16713498266Sopenharmony_ci FD_ZERO(&fdread); 16813498266Sopenharmony_ci FD_ZERO(&fdwrite); 16913498266Sopenharmony_ci FD_ZERO(&fdexcep); 17013498266Sopenharmony_ci 17113498266Sopenharmony_ci multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd); 17213498266Sopenharmony_ci 17313498266Sopenharmony_ci /* At this point, maxfd is guaranteed to be greater or equal than -1. */ 17413498266Sopenharmony_ci 17513498266Sopenharmony_ci if(state == NeedSocketForNewHandle) { 17613498266Sopenharmony_ci if(maxfd != -1 && !found_new_socket) { 17713498266Sopenharmony_ci fprintf(stderr, "Warning: socket did not open immediately for new " 17813498266Sopenharmony_ci "handle (trying again)\n"); 17913498266Sopenharmony_ci continue; 18013498266Sopenharmony_ci } 18113498266Sopenharmony_ci state = num_handles < MAX_EASY_HANDLES ? ReadyForNewHandle 18213498266Sopenharmony_ci : NoMoreHandles; 18313498266Sopenharmony_ci fprintf(stderr, "%s:%d new state %d\n", 18413498266Sopenharmony_ci __FILE__, __LINE__, state); 18513498266Sopenharmony_ci } 18613498266Sopenharmony_ci 18713498266Sopenharmony_ci multi_timeout(multi, &timeout); 18813498266Sopenharmony_ci 18913498266Sopenharmony_ci /* At this point, timeout is guaranteed to be greater or equal than -1. */ 19013498266Sopenharmony_ci 19113498266Sopenharmony_ci fprintf(stderr, "%s:%d num_handles %d timeout %ld running %d\n", 19213498266Sopenharmony_ci __FILE__, __LINE__, num_handles, timeout, running); 19313498266Sopenharmony_ci 19413498266Sopenharmony_ci if(timeout != -1L) { 19513498266Sopenharmony_ci int itimeout; 19613498266Sopenharmony_ci#if LONG_MAX > INT_MAX 19713498266Sopenharmony_ci itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout; 19813498266Sopenharmony_ci#else 19913498266Sopenharmony_ci itimeout = (int)timeout; 20013498266Sopenharmony_ci#endif 20113498266Sopenharmony_ci interval.tv_sec = itimeout/1000; 20213498266Sopenharmony_ci interval.tv_usec = (itimeout%1000)*1000; 20313498266Sopenharmony_ci } 20413498266Sopenharmony_ci else { 20513498266Sopenharmony_ci interval.tv_sec = 0; 20613498266Sopenharmony_ci interval.tv_usec = 5000; 20713498266Sopenharmony_ci 20813498266Sopenharmony_ci /* if there's no timeout and we get here on the last handle, we may 20913498266Sopenharmony_ci already have read the last part of the stream so waiting makes no 21013498266Sopenharmony_ci sense */ 21113498266Sopenharmony_ci if(!running && num_handles == MAX_EASY_HANDLES) { 21213498266Sopenharmony_ci break; 21313498266Sopenharmony_ci } 21413498266Sopenharmony_ci } 21513498266Sopenharmony_ci 21613498266Sopenharmony_ci select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &interval); 21713498266Sopenharmony_ci 21813498266Sopenharmony_ci abort_on_test_timeout(); 21913498266Sopenharmony_ci } 22013498266Sopenharmony_ci 22113498266Sopenharmony_citest_cleanup: 22213498266Sopenharmony_ci 22313498266Sopenharmony_ci /* proper cleanup sequence - type PB */ 22413498266Sopenharmony_ci 22513498266Sopenharmony_ci for(i = 0; i < MAX_EASY_HANDLES; i++) { 22613498266Sopenharmony_ci printf("Data connection %d: %d\n", i, counter[i]); 22713498266Sopenharmony_ci curl_multi_remove_handle(multi, easy[i]); 22813498266Sopenharmony_ci curl_easy_cleanup(easy[i]); 22913498266Sopenharmony_ci } 23013498266Sopenharmony_ci 23113498266Sopenharmony_ci curl_multi_cleanup(multi); 23213498266Sopenharmony_ci curl_global_cleanup(); 23313498266Sopenharmony_ci 23413498266Sopenharmony_ci free(full_url); 23513498266Sopenharmony_ci 23613498266Sopenharmony_ci return res; 23713498266Sopenharmony_ci} 238