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