• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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 
36 static int counter[MAX_EASY_HANDLES];
37 static CURL *easy[MAX_EASY_HANDLES];
38 static curl_socket_t sockets[MAX_EASY_HANDLES];
39 static int res = 0;
40 
callback(char * ptr,size_t size,size_t nmemb,void * data)41 static 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   CURL_IGNORE_DEPRECATION(
54     code = curl_easy_getinfo(easy[idx], CURLINFO_LASTSOCKET, &longdata);
55   )
56   if(CURLE_OK != code) {
57     fprintf(stderr, "%s:%d curl_easy_getinfo() failed, "
58             "with code %d (%s)\n",
59             __FILE__, __LINE__, (int)code, curl_easy_strerror(code));
60     res = TEST_ERR_MAJOR_BAD;
61     return failure;
62   }
63   if(longdata == -1L)
64     sock = CURL_SOCKET_BAD;
65   else
66     sock = (curl_socket_t)longdata;
67 
68   if(sock != CURL_SOCKET_BAD) {
69     /* Track relationship between this easy handle and the socket. */
70     if(sockets[idx] == CURL_SOCKET_BAD) {
71       /* An easy handle without previous socket, record the socket. */
72       sockets[idx] = sock;
73     }
74     else if(sock != sockets[idx]) {
75       /* An easy handle with a socket different to previously
76          tracked one, log and fail right away. Known bug #37. */
77       fprintf(stderr, "Handle %d started on socket %d and moved to %d\n",
78               curlx_sztosi(idx), (int)sockets[idx], (int)sock);
79       res = TEST_ERR_MAJOR_BAD;
80       return failure;
81     }
82   }
83   return size * nmemb;
84 }
85 
86 enum HandleState {
87   ReadyForNewHandle,
88   NeedSocketForNewHandle,
89   NoMoreHandles
90 };
91 
test(char * url)92 int test(char *url)
93 {
94   CURLM *multi = NULL;
95   int running;
96   int i;
97   int num_handles = 0;
98   enum HandleState state = ReadyForNewHandle;
99   size_t urllen = strlen(url) + 4 + 1;
100   char *full_url = malloc(urllen);
101 
102   start_test_timing();
103 
104   if(!full_url) {
105     fprintf(stderr, "Not enough memory for full url\n");
106     return TEST_ERR_MAJOR_BAD;
107   }
108 
109   for(i = 0; i < MAX_EASY_HANDLES; ++i) {
110     easy[i] = NULL;
111     sockets[i] = CURL_SOCKET_BAD;
112   }
113 
114   res_global_init(CURL_GLOBAL_ALL);
115   if(res) {
116     free(full_url);
117     return res;
118   }
119 
120   multi_init(multi);
121 
122 #ifdef USE_PIPELINING
123   multi_setopt(multi, CURLMOPT_PIPELINING, 1L);
124   multi_setopt(multi, CURLMOPT_MAX_HOST_CONNECTIONS, 5L);
125   multi_setopt(multi, CURLMOPT_MAX_TOTAL_CONNECTIONS, 10L);
126 #endif
127 
128   for(;;) {
129     struct timeval interval;
130     fd_set fdread;
131     fd_set fdwrite;
132     fd_set fdexcep;
133     long timeout = -99;
134     int maxfd = -99;
135     bool found_new_socket = FALSE;
136 
137     /* Start a new handle if we aren't at the max */
138     if(state == ReadyForNewHandle) {
139       easy_init(easy[num_handles]);
140 
141       if(num_handles % 3 == 2) {
142         msnprintf(full_url, urllen, "%s0200", url);
143         easy_setopt(easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
144       }
145       else {
146         msnprintf(full_url, urllen, "%s0100", url);
147         easy_setopt(easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
148       }
149       easy_setopt(easy[num_handles], CURLOPT_FRESH_CONNECT, 1L);
150       easy_setopt(easy[num_handles], CURLOPT_URL, full_url);
151       easy_setopt(easy[num_handles], CURLOPT_VERBOSE, 1L);
152       easy_setopt(easy[num_handles], CURLOPT_HTTPGET, 1L);
153       easy_setopt(easy[num_handles], CURLOPT_USERPWD, "testuser:testpass");
154       easy_setopt(easy[num_handles], CURLOPT_WRITEFUNCTION, callback);
155       easy_setopt(easy[num_handles], CURLOPT_WRITEDATA, easy + num_handles);
156       easy_setopt(easy[num_handles], CURLOPT_HEADER, 1L);
157 
158       multi_add_handle(multi, easy[num_handles]);
159       num_handles += 1;
160       state = NeedSocketForNewHandle;
161     }
162 
163     multi_perform(multi, &running);
164 
165     fprintf(stderr, "%s:%d running %d state %d\n",
166             __FILE__, __LINE__, running, state);
167 
168     abort_on_test_timeout();
169 
170     if(!running && state == NoMoreHandles)
171       break; /* done */
172 
173     FD_ZERO(&fdread);
174     FD_ZERO(&fdwrite);
175     FD_ZERO(&fdexcep);
176 
177     multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
178 
179     /* At this point, maxfd is guaranteed to be greater or equal than -1. */
180 
181     if(state == NeedSocketForNewHandle) {
182       if(maxfd != -1 && !found_new_socket) {
183         fprintf(stderr, "Warning: socket did not open immediately for new "
184                 "handle (trying again)\n");
185         continue;
186       }
187       state = num_handles < MAX_EASY_HANDLES ? ReadyForNewHandle
188                                              : NoMoreHandles;
189       fprintf(stderr, "%s:%d new state %d\n",
190               __FILE__, __LINE__, state);
191     }
192 
193     multi_timeout(multi, &timeout);
194 
195     /* At this point, timeout is guaranteed to be greater or equal than -1. */
196 
197     fprintf(stderr, "%s:%d num_handles %d timeout %ld running %d\n",
198             __FILE__, __LINE__, num_handles, timeout, running);
199 
200     if(timeout != -1L) {
201       int itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
202       interval.tv_sec = itimeout/1000;
203       interval.tv_usec = (itimeout%1000)*1000;
204     }
205     else {
206       interval.tv_sec = 0;
207       interval.tv_usec = 5000;
208 
209       /* if there's no timeout and we get here on the last handle, we may
210          already have read the last part of the stream so waiting makes no
211          sense */
212       if(!running && num_handles == MAX_EASY_HANDLES) {
213         break;
214       }
215     }
216 
217     select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &interval);
218 
219     abort_on_test_timeout();
220   }
221 
222 test_cleanup:
223 
224   /* proper cleanup sequence - type PB */
225 
226   for(i = 0; i < MAX_EASY_HANDLES; i++) {
227     printf("Data connection %d: %d\n", i, counter[i]);
228     curl_multi_remove_handle(multi, easy[i]);
229     curl_easy_cleanup(easy[i]);
230   }
231 
232   curl_multi_cleanup(multi);
233   curl_global_cleanup();
234 
235   free(full_url);
236 
237   return res;
238 }
239