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 <fcntl.h>
27
28 #include "testutil.h"
29 #include "warnless.h"
30 #include "memdebug.h"
31
32 #define TEST_HANG_TIMEOUT 60 * 1000
33
34 struct Sockets
35 {
36 curl_socket_t *sockets;
37 int count; /* number of sockets actually stored in array */
38 int max_count; /* max number of sockets that fit in allocated array */
39 };
40
41 struct ReadWriteSockets
42 {
43 struct Sockets read, write;
44 };
45
46 /**
47 * Remove a file descriptor from a sockets array.
48 */
removeFd(struct Sockets * sockets,curl_socket_t fd,int mention)49 static void removeFd(struct Sockets *sockets, curl_socket_t fd, int mention)
50 {
51 int i;
52
53 if(mention)
54 fprintf(stderr, "Remove socket fd %d\n", (int) fd);
55
56 for(i = 0; i < sockets->count; ++i) {
57 if(sockets->sockets[i] == fd) {
58 if(i < sockets->count - 1)
59 memmove(&sockets->sockets[i], &sockets->sockets[i + 1],
60 sizeof(curl_socket_t) * (sockets->count - (i + 1)));
61 --sockets->count;
62 }
63 }
64 }
65
66 /**
67 * Add a file descriptor to a sockets array.
68 */
addFd(struct Sockets * sockets,curl_socket_t fd,const char * what)69 static void addFd(struct Sockets *sockets, curl_socket_t fd, const char *what)
70 {
71 /**
72 * To ensure we only have each file descriptor once, we remove it then add
73 * it again.
74 */
75 fprintf(stderr, "Add socket fd %d for %s\n", (int) fd, what);
76 removeFd(sockets, fd, 0);
77 /*
78 * Allocate array storage when required.
79 */
80 if(!sockets->sockets) {
81 sockets->sockets = malloc(sizeof(curl_socket_t) * 20U);
82 if(!sockets->sockets)
83 return;
84 sockets->max_count = 20;
85 }
86 else if(sockets->count >= sockets->max_count) {
87 /* this can't happen in normal cases */
88 fprintf(stderr, "too many file handles error\n");
89 exit(2);
90 }
91 /*
92 * Add file descriptor to array.
93 */
94 sockets->sockets[sockets->count] = fd;
95 ++sockets->count;
96 }
97
98 /**
99 * Callback invoked by curl to poll reading / writing of a socket.
100 */
curlSocketCallback(CURL * easy,curl_socket_t s,int action,void * userp,void * socketp)101 static int curlSocketCallback(CURL *easy, curl_socket_t s, int action,
102 void *userp, void *socketp)
103 {
104 struct ReadWriteSockets *sockets = userp;
105
106 (void)easy; /* unused */
107 (void)socketp; /* unused */
108
109 if(action == CURL_POLL_IN || action == CURL_POLL_INOUT)
110 addFd(&sockets->read, s, "read");
111
112 if(action == CURL_POLL_OUT || action == CURL_POLL_INOUT)
113 addFd(&sockets->write, s, "write");
114
115 if(action == CURL_POLL_REMOVE) {
116 removeFd(&sockets->read, s, 1);
117 removeFd(&sockets->write, s, 0);
118 }
119
120 return 0;
121 }
122
123 /**
124 * Callback invoked by curl to set a timeout.
125 */
curlTimerCallback(CURLM * multi,long timeout_ms,void * userp)126 static int curlTimerCallback(CURLM *multi, long timeout_ms, void *userp)
127 {
128 struct timeval *timeout = userp;
129
130 (void)multi; /* unused */
131 if(timeout_ms != -1) {
132 *timeout = tutil_tvnow();
133 timeout->tv_usec += (int)timeout_ms * 1000;
134 }
135 else {
136 timeout->tv_sec = -1;
137 }
138 return 0;
139 }
140
141 /**
142 * Check for curl completion.
143 */
checkForCompletion(CURLM * curl,int * success)144 static int checkForCompletion(CURLM *curl, int *success)
145 {
146 int result = 0;
147 *success = 0;
148 while(1) {
149 int numMessages;
150 CURLMsg *message = curl_multi_info_read(curl, &numMessages);
151 if(!message)
152 break;
153 if(message->msg == CURLMSG_DONE) {
154 result = 1;
155 if(message->data.result == CURLE_OK)
156 *success = 1;
157 else
158 *success = 0;
159 }
160 else {
161 fprintf(stderr, "Got an unexpected message from curl: %i\n",
162 (int)message->msg);
163 result = 1;
164 *success = 0;
165 }
166 }
167 return result;
168 }
169
getMicroSecondTimeout(struct timeval * timeout)170 static int getMicroSecondTimeout(struct timeval *timeout)
171 {
172 struct timeval now;
173 ssize_t result;
174 now = tutil_tvnow();
175 result = (ssize_t)((timeout->tv_sec - now.tv_sec) * 1000000 +
176 timeout->tv_usec - now.tv_usec);
177 if(result < 0)
178 result = 0;
179
180 return curlx_sztosi(result);
181 }
182
183 /**
184 * Update a fd_set with all of the sockets in use.
185 */
updateFdSet(struct Sockets * sockets,fd_set * fdset,curl_socket_t * maxFd)186 static void updateFdSet(struct Sockets *sockets, fd_set* fdset,
187 curl_socket_t *maxFd)
188 {
189 int i;
190 for(i = 0; i < sockets->count; ++i) {
191 FD_SET(sockets->sockets[i], fdset);
192 if(*maxFd < sockets->sockets[i] + 1) {
193 *maxFd = sockets->sockets[i] + 1;
194 }
195 }
196 }
197
notifyCurl(CURLM * curl,curl_socket_t s,int evBitmask,const char * info)198 static void notifyCurl(CURLM *curl, curl_socket_t s, int evBitmask,
199 const char *info)
200 {
201 int numhandles = 0;
202 CURLMcode result = curl_multi_socket_action(curl, s, evBitmask, &numhandles);
203 if(result != CURLM_OK) {
204 fprintf(stderr, "Curl error on %s: %i (%s)\n",
205 info, result, curl_multi_strerror(result));
206 }
207 }
208
209 /**
210 * Invoke curl when a file descriptor is set.
211 */
checkFdSet(CURLM * curl,struct Sockets * sockets,fd_set * fdset,int evBitmask,const char * name)212 static void checkFdSet(CURLM *curl, struct Sockets *sockets, fd_set *fdset,
213 int evBitmask, const char *name)
214 {
215 int i;
216 for(i = 0; i < sockets->count; ++i) {
217 if(FD_ISSET(sockets->sockets[i], fdset)) {
218 notifyCurl(curl, sockets->sockets[i], evBitmask, name);
219 }
220 }
221 }
222
test(char * URL)223 CURLcode test(char *URL)
224 {
225 CURLcode res = CURLE_OK;
226 CURL *curl = NULL;
227 FILE *hd_src = NULL;
228 int hd;
229 struct_stat file_info;
230 CURLM *m = NULL;
231 struct ReadWriteSockets sockets = {{NULL, 0, 0}, {NULL, 0, 0}};
232 struct timeval timeout = {-1, 0};
233 int success = 0;
234
235 assert(test_argc >= 5);
236
237 start_test_timing();
238
239 if(!libtest_arg3) {
240 fprintf(stderr, "Usage: lib582 [url] [filename] [username]\n");
241 return TEST_ERR_USAGE;
242 }
243
244 hd_src = fopen(libtest_arg2, "rb");
245 if(!hd_src) {
246 fprintf(stderr, "fopen() failed with error: %d (%s)\n",
247 errno, strerror(errno));
248 fprintf(stderr, "Error opening file: (%s)\n", libtest_arg2);
249 return TEST_ERR_FOPEN;
250 }
251
252 /* get the file size of the local file */
253 hd = fstat(fileno(hd_src), &file_info);
254 if(hd == -1) {
255 /* can't open file, bail out */
256 fprintf(stderr, "fstat() failed with error: %d (%s)\n",
257 errno, strerror(errno));
258 fprintf(stderr, "ERROR: cannot open file (%s)\n", libtest_arg2);
259 fclose(hd_src);
260 return TEST_ERR_FSTAT;
261 }
262 fprintf(stderr, "Set to upload %d bytes\n", (int)file_info.st_size);
263
264 res_global_init(CURL_GLOBAL_ALL);
265 if(res) {
266 fclose(hd_src);
267 return res;
268 }
269
270 easy_init(curl);
271
272 /* enable uploading */
273 easy_setopt(curl, CURLOPT_UPLOAD, 1L);
274
275 /* specify target */
276 easy_setopt(curl, CURLOPT_URL, URL);
277
278 /* go verbose */
279 easy_setopt(curl, CURLOPT_VERBOSE, 1L);
280
281 /* now specify which file to upload */
282 easy_setopt(curl, CURLOPT_READDATA, hd_src);
283
284 easy_setopt(curl, CURLOPT_USERPWD, libtest_arg3);
285 easy_setopt(curl, CURLOPT_SSH_PUBLIC_KEYFILE, test_argv[4]);
286 easy_setopt(curl, CURLOPT_SSH_PRIVATE_KEYFILE, test_argv[5]);
287 easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
288
289 easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
290
291 multi_init(m);
292
293 multi_setopt(m, CURLMOPT_SOCKETFUNCTION, curlSocketCallback);
294 multi_setopt(m, CURLMOPT_SOCKETDATA, &sockets);
295
296 multi_setopt(m, CURLMOPT_TIMERFUNCTION, curlTimerCallback);
297 multi_setopt(m, CURLMOPT_TIMERDATA, &timeout);
298
299 multi_add_handle(m, curl);
300
301 while(!checkForCompletion(m, &success)) {
302 fd_set readSet, writeSet;
303 curl_socket_t maxFd = 0;
304 struct timeval tv = {10, 0};
305
306 FD_ZERO(&readSet);
307 FD_ZERO(&writeSet);
308 updateFdSet(&sockets.read, &readSet, &maxFd);
309 updateFdSet(&sockets.write, &writeSet, &maxFd);
310
311 if(timeout.tv_sec != -1) {
312 int usTimeout = getMicroSecondTimeout(&timeout);
313 tv.tv_sec = usTimeout / 1000000;
314 tv.tv_usec = usTimeout % 1000000;
315 }
316 else if(maxFd <= 0) {
317 tv.tv_sec = 0;
318 tv.tv_usec = 100000;
319 }
320
321 select_test((int)maxFd, &readSet, &writeSet, NULL, &tv);
322
323 /* Check the sockets for reading / writing */
324 checkFdSet(m, &sockets.read, &readSet, CURL_CSELECT_IN, "read");
325 checkFdSet(m, &sockets.write, &writeSet, CURL_CSELECT_OUT, "write");
326
327 if(timeout.tv_sec != -1 && getMicroSecondTimeout(&timeout) == 0) {
328 /* Curl's timer has elapsed. */
329 notifyCurl(m, CURL_SOCKET_TIMEOUT, 0, "timeout");
330 }
331
332 abort_on_test_timeout();
333 }
334
335 if(!success) {
336 fprintf(stderr, "Error uploading file.\n");
337 res = TEST_ERR_MAJOR_BAD;
338 }
339
340 test_cleanup:
341
342 /* proper cleanup sequence - type PB */
343
344 curl_multi_remove_handle(m, curl);
345 curl_easy_cleanup(curl);
346 curl_multi_cleanup(m);
347 curl_global_cleanup();
348
349 /* close the local file */
350 fclose(hd_src);
351
352 /* free local memory */
353 free(sockets.read.sockets);
354 free(sockets.write.sockets);
355
356 return res;
357 }
358