blob: 18b1443abe9f1ee79d78e8d137a9b4571206d0b5 [file] [log] [blame]
xf.li6c8fc1e2023-08-12 00:11:09 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 2012 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "test.h"
25
26#include <limits.h>
27#include <assert.h>
28
29#include "testutil.h"
30#include "warnless.h"
31#include "memdebug.h"
32
33#define TEST_HANG_TIMEOUT 5 * 1000
34#define MAX_EASY_HANDLES 3
35
36static int counter[MAX_EASY_HANDLES];
37static CURL *easy[MAX_EASY_HANDLES];
38static curl_socket_t sockets[MAX_EASY_HANDLES];
39static int res = 0;
40
41static size_t callback(char *ptr, size_t size, size_t nmemb, void *data)
42{
43 ssize_t idx = ((CURL **) data) - easy;
44 curl_socket_t sock;
45 long longdata;
46 CURLcode code;
47 const size_t failure = (size && nmemb) ? 0 : 1;
48 (void)ptr;
49
50 counter[idx] += (int)(size * nmemb);
51
52 /* Get socket being used for this easy handle, otherwise CURL_SOCKET_BAD */
53 code = curl_easy_getinfo(easy[idx], CURLINFO_LASTSOCKET, &longdata);
54 if(CURLE_OK != code) {
55 fprintf(stderr, "%s:%d curl_easy_getinfo() failed, "
56 "with code %d (%s)\n",
57 __FILE__, __LINE__, (int)code, curl_easy_strerror(code));
58 res = TEST_ERR_MAJOR_BAD;
59 return failure;
60 }
61 if(longdata == -1L)
62 sock = CURL_SOCKET_BAD;
63 else
64 sock = (curl_socket_t)longdata;
65
66 if(sock != CURL_SOCKET_BAD) {
67 /* Track relationship between this easy handle and the socket. */
68 if(sockets[idx] == CURL_SOCKET_BAD) {
69 /* An easy handle without previous socket, record the socket. */
70 sockets[idx] = sock;
71 }
72 else if(sock != sockets[idx]) {
73 /* An easy handle with a socket different to previously
74 tracked one, log and fail right away. Known bug #37. */
75 fprintf(stderr, "Handle %d started on socket %d and moved to %d\n",
76 curlx_sztosi(idx), (int)sockets[idx], (int)sock);
77 res = TEST_ERR_MAJOR_BAD;
78 return failure;
79 }
80 }
81 return size * nmemb;
82}
83
84enum HandleState {
85 ReadyForNewHandle,
86 NeedSocketForNewHandle,
87 NoMoreHandles
88};
89
90int test(char *url)
91{
92 CURLM *multi = NULL;
93 int running;
94 int i;
95 int num_handles = 0;
96 enum HandleState state = ReadyForNewHandle;
97 size_t urllen = strlen(url) + 4 + 1;
98 char *full_url = malloc(urllen);
99
100 start_test_timing();
101
102 if(!full_url) {
103 fprintf(stderr, "Not enough memory for full url\n");
104 return TEST_ERR_MAJOR_BAD;
105 }
106
107 for(i = 0; i < MAX_EASY_HANDLES; ++i) {
108 easy[i] = NULL;
109 sockets[i] = CURL_SOCKET_BAD;
110 }
111
112 res_global_init(CURL_GLOBAL_ALL);
113 if(res) {
114 free(full_url);
115 return res;
116 }
117
118 multi_init(multi);
119
120#ifdef USE_PIPELINING
121 multi_setopt(multi, CURLMOPT_PIPELINING, 1L);
122 multi_setopt(multi, CURLMOPT_MAX_HOST_CONNECTIONS, 5L);
123 multi_setopt(multi, CURLMOPT_MAX_TOTAL_CONNECTIONS, 10L);
124#endif
125
126 for(;;) {
127 struct timeval interval;
128 fd_set fdread;
129 fd_set fdwrite;
130 fd_set fdexcep;
131 long timeout = -99;
132 int maxfd = -99;
133 bool found_new_socket = FALSE;
134
135 /* Start a new handle if we aren't at the max */
136 if(state == ReadyForNewHandle) {
137 easy_init(easy[num_handles]);
138
139 if(num_handles % 3 == 2) {
140 msnprintf(full_url, urllen, "%s0200", url);
141 easy_setopt(easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
142 }
143 else {
144 msnprintf(full_url, urllen, "%s0100", url);
145 easy_setopt(easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
146 }
147 easy_setopt(easy[num_handles], CURLOPT_FRESH_CONNECT, 1L);
148 easy_setopt(easy[num_handles], CURLOPT_URL, full_url);
149 easy_setopt(easy[num_handles], CURLOPT_VERBOSE, 1L);
150 easy_setopt(easy[num_handles], CURLOPT_HTTPGET, 1L);
151 easy_setopt(easy[num_handles], CURLOPT_USERPWD, "testuser:testpass");
152 easy_setopt(easy[num_handles], CURLOPT_WRITEFUNCTION, callback);
153 easy_setopt(easy[num_handles], CURLOPT_WRITEDATA, easy + num_handles);
154 easy_setopt(easy[num_handles], CURLOPT_HEADER, 1L);
155
156 multi_add_handle(multi, easy[num_handles]);
157 num_handles += 1;
158 state = NeedSocketForNewHandle;
159 }
160
161 multi_perform(multi, &running);
162
163 fprintf(stderr, "%s:%d running %d state %d\n",
164 __FILE__, __LINE__, running, state);
165
166 abort_on_test_timeout();
167
168 if(!running && state == NoMoreHandles)
169 break; /* done */
170
171 FD_ZERO(&fdread);
172 FD_ZERO(&fdwrite);
173 FD_ZERO(&fdexcep);
174
175 multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
176
177 /* At this point, maxfd is guaranteed to be greater or equal than -1. */
178
179 if(state == NeedSocketForNewHandle) {
180 if(maxfd != -1 && !found_new_socket) {
181 fprintf(stderr, "Warning: socket did not open immediately for new "
182 "handle (trying again)\n");
183 continue;
184 }
185 state = num_handles < MAX_EASY_HANDLES ? ReadyForNewHandle
186 : NoMoreHandles;
187 fprintf(stderr, "%s:%d new state %d\n",
188 __FILE__, __LINE__, state);
189 }
190
191 multi_timeout(multi, &timeout);
192
193 /* At this point, timeout is guaranteed to be greater or equal than -1. */
194
195 fprintf(stderr, "%s:%d num_handles %d timeout %ld running %d\n",
196 __FILE__, __LINE__, num_handles, timeout, running);
197
198 if(timeout != -1L) {
199 int itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
200 interval.tv_sec = itimeout/1000;
201 interval.tv_usec = (itimeout%1000)*1000;
202 }
203 else {
204 interval.tv_sec = 0;
205 interval.tv_usec = 5000;
206
207 /* if there's no timeout and we get here on the last handle, we may
208 already have read the last part of the stream so waiting makes no
209 sense */
210 if(!running && num_handles == MAX_EASY_HANDLES) {
211 break;
212 }
213 }
214
215 select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &interval);
216
217 abort_on_test_timeout();
218 }
219
220test_cleanup:
221
222 /* proper cleanup sequence - type PB */
223
224 for(i = 0; i < MAX_EASY_HANDLES; i++) {
225 printf("Data connection %d: %d\n", i, counter[i]);
226 curl_multi_remove_handle(multi, easy[i]);
227 curl_easy_cleanup(easy[i]);
228 }
229
230 curl_multi_cleanup(multi);
231 curl_global_cleanup();
232
233 free(full_url);
234
235 return res;
236}