xref: /curl/tests/libtest/libntlmconnect.c (revision 71cf0d1f)
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 60 * 1000
34 #define MAX_EASY_HANDLES 3
35 
36 static int ntlm_counter[MAX_EASY_HANDLES];
37 static CURL *ntlm_easy[MAX_EASY_HANDLES];
38 static curl_socket_t ntlm_sockets[MAX_EASY_HANDLES];
39 static CURLcode ntlmcb_res = CURLE_OK;
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) - ntlm_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   ntlm_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(ntlm_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     ntlmcb_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(ntlm_sockets[idx] == CURL_SOCKET_BAD) {
71       /* An easy handle without previous socket, record the socket. */
72       ntlm_sockets[idx] = sock;
73     }
74     else if(sock != ntlm_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)ntlm_sockets[idx], (int)sock);
79       ntlmcb_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 CURLcode test(char *url)
93 {
94   CURLcode res = CURLE_OK;
95   CURLM *multi = NULL;
96   int running;
97   int i;
98   int num_handles = 0;
99   enum HandleState state = ReadyForNewHandle;
100   size_t urllen = strlen(url) + 4 + 1;
101   char *full_url = malloc(urllen);
102 
103   start_test_timing();
104 
105   if(!full_url) {
106     fprintf(stderr, "Not enough memory for full url\n");
107     return TEST_ERR_MAJOR_BAD;
108   }
109 
110   for(i = 0; i < MAX_EASY_HANDLES; ++i) {
111     ntlm_easy[i] = NULL;
112     ntlm_sockets[i] = CURL_SOCKET_BAD;
113   }
114 
115   res_global_init(CURL_GLOBAL_ALL);
116   if(res) {
117     free(full_url);
118     return res;
119   }
120 
121   multi_init(multi);
122 
123   for(;;) {
124     struct timeval interval;
125     fd_set fdread;
126     fd_set fdwrite;
127     fd_set fdexcep;
128     long timeout = -99;
129     int maxfd = -99;
130     bool found_new_socket = FALSE;
131 
132     /* Start a new handle if we aren't at the max */
133     if(state == ReadyForNewHandle) {
134       easy_init(ntlm_easy[num_handles]);
135 
136       if(num_handles % 3 == 2) {
137         msnprintf(full_url, urllen, "%s0200", url);
138         easy_setopt(ntlm_easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
139       }
140       else {
141         msnprintf(full_url, urllen, "%s0100", url);
142         easy_setopt(ntlm_easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
143       }
144       easy_setopt(ntlm_easy[num_handles], CURLOPT_FRESH_CONNECT, 1L);
145       easy_setopt(ntlm_easy[num_handles], CURLOPT_URL, full_url);
146       easy_setopt(ntlm_easy[num_handles], CURLOPT_VERBOSE, 1L);
147       easy_setopt(ntlm_easy[num_handles], CURLOPT_HTTPGET, 1L);
148       easy_setopt(ntlm_easy[num_handles], CURLOPT_USERPWD,
149                   "testuser:testpass");
150       easy_setopt(ntlm_easy[num_handles], CURLOPT_WRITEFUNCTION, callback);
151       easy_setopt(ntlm_easy[num_handles], CURLOPT_WRITEDATA,
152                   ntlm_easy + num_handles);
153       easy_setopt(ntlm_easy[num_handles], CURLOPT_HEADER, 1L);
154 
155       multi_add_handle(multi, ntlm_easy[num_handles]);
156       num_handles += 1;
157       state = NeedSocketForNewHandle;
158       res = ntlmcb_res;
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;
200 #if LONG_MAX > INT_MAX
201       itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
202 #else
203       itimeout = (int)timeout;
204 #endif
205       interval.tv_sec = itimeout/1000;
206       interval.tv_usec = (itimeout%1000)*1000;
207     }
208     else {
209       interval.tv_sec = 0;
210       interval.tv_usec = 5000;
211 
212       /* if there's no timeout and we get here on the last handle, we may
213          already have read the last part of the stream so waiting makes no
214          sense */
215       if(!running && num_handles == MAX_EASY_HANDLES) {
216         break;
217       }
218     }
219 
220     select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &interval);
221 
222     abort_on_test_timeout();
223   }
224 
225 test_cleanup:
226 
227   /* proper cleanup sequence - type PB */
228 
229   for(i = 0; i < MAX_EASY_HANDLES; i++) {
230     printf("Data connection %d: %d\n", i, ntlm_counter[i]);
231     curl_multi_remove_handle(multi, ntlm_easy[i]);
232     curl_easy_cleanup(ntlm_easy[i]);
233   }
234 
235   curl_multi_cleanup(multi);
236   curl_global_cleanup();
237 
238   free(full_url);
239 
240   return res;
241 }
242