| xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1 | /*************************************************************************** | 
|  | 2 | *                                  _   _ ____  _ | 
|  | 3 | *  Project                     ___| | | |  _ \| | | 
|  | 4 | *                             / __| | | | |_) | | | 
|  | 5 | *                            | (__| |_| |  _ <| |___ | 
|  | 6 | *                             \___|\___/|_| \_\_____| | 
|  | 7 | * | 
|  | 8 | * Copyright (C) 1998 - 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 <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 | */ | 
|  | 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 | */ | 
|  | 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 + 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 | */ | 
|  | 107 | static 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 | */ | 
|  | 132 | static 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 += timeout_ms * 1000; | 
|  | 140 | } | 
|  | 141 | else { | 
|  | 142 | timeout->tv_sec = -1; | 
|  | 143 | } | 
|  | 144 | return 0; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | /** | 
|  | 148 | * Check for curl completion. | 
|  | 149 | */ | 
|  | 150 | static 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 |  | 
|  | 176 | static 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 | */ | 
|  | 192 | static 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 |  | 
|  | 204 | static 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 | */ | 
|  | 218 | static 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 |  | 
|  | 229 | int 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 | start_test_timing(); | 
|  | 242 |  | 
|  | 243 | if(!libtest_arg3) { | 
|  | 244 | fprintf(stderr, "Usage: lib582 [url] [filename] [username]\n"); | 
|  | 245 | return TEST_ERR_USAGE; | 
|  | 246 | } | 
|  | 247 |  | 
|  | 248 | hd_src = fopen(libtest_arg2, "rb"); | 
|  | 249 | if(!hd_src) { | 
|  | 250 | fprintf(stderr, "fopen() failed with error: %d (%s)\n", | 
|  | 251 | errno, strerror(errno)); | 
|  | 252 | fprintf(stderr, "Error opening file: (%s)\n", libtest_arg2); | 
|  | 253 | return TEST_ERR_FOPEN; | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | /* get the file size of the local file */ | 
|  | 257 | hd = fstat(fileno(hd_src), &file_info); | 
|  | 258 | if(hd == -1) { | 
|  | 259 | /* can't open file, bail out */ | 
|  | 260 | fprintf(stderr, "fstat() failed with error: %d (%s)\n", | 
|  | 261 | errno, strerror(errno)); | 
|  | 262 | fprintf(stderr, "ERROR: cannot open file (%s)\n", libtest_arg2); | 
|  | 263 | fclose(hd_src); | 
|  | 264 | return TEST_ERR_FSTAT; | 
|  | 265 | } | 
|  | 266 | fprintf(stderr, "Set to upload %d bytes\n", (int)file_info.st_size); | 
|  | 267 |  | 
|  | 268 | res_global_init(CURL_GLOBAL_ALL); | 
|  | 269 | if(res) { | 
|  | 270 | fclose(hd_src); | 
|  | 271 | return res; | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | easy_init(curl); | 
|  | 275 |  | 
|  | 276 | /* enable uploading */ | 
|  | 277 | easy_setopt(curl, CURLOPT_UPLOAD, 1L); | 
|  | 278 |  | 
|  | 279 | /* specify target */ | 
|  | 280 | easy_setopt(curl, CURLOPT_URL, URL); | 
|  | 281 |  | 
|  | 282 | /* go verbose */ | 
|  | 283 | easy_setopt(curl, CURLOPT_VERBOSE, 1L); | 
|  | 284 |  | 
|  | 285 | /* now specify which file to upload */ | 
|  | 286 | easy_setopt(curl, CURLOPT_READDATA, hd_src); | 
|  | 287 |  | 
|  | 288 | easy_setopt(curl, CURLOPT_USERPWD, libtest_arg3); | 
|  | 289 | easy_setopt(curl, CURLOPT_SSH_PUBLIC_KEYFILE, "curl_client_key.pub"); | 
|  | 290 | easy_setopt(curl, CURLOPT_SSH_PRIVATE_KEYFILE, "curl_client_key"); | 
|  | 291 | easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); | 
|  | 292 |  | 
|  | 293 | easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size); | 
|  | 294 |  | 
|  | 295 | multi_init(m); | 
|  | 296 |  | 
|  | 297 | multi_setopt(m, CURLMOPT_SOCKETFUNCTION, curlSocketCallback); | 
|  | 298 | multi_setopt(m, CURLMOPT_SOCKETDATA, &sockets); | 
|  | 299 |  | 
|  | 300 | multi_setopt(m, CURLMOPT_TIMERFUNCTION, curlTimerCallback); | 
|  | 301 | multi_setopt(m, CURLMOPT_TIMERDATA, &timeout); | 
|  | 302 |  | 
|  | 303 | multi_add_handle(m, curl); | 
|  | 304 |  | 
|  | 305 | while(!checkForCompletion(m, &success)) { | 
|  | 306 | fd_set readSet, writeSet; | 
|  | 307 | curl_socket_t maxFd = 0; | 
|  | 308 | struct timeval tv = {10, 0}; | 
|  | 309 |  | 
|  | 310 | FD_ZERO(&readSet); | 
|  | 311 | FD_ZERO(&writeSet); | 
|  | 312 | updateFdSet(&sockets.read, &readSet, &maxFd); | 
|  | 313 | updateFdSet(&sockets.write, &writeSet, &maxFd); | 
|  | 314 |  | 
|  | 315 | if(timeout.tv_sec != -1) { | 
|  | 316 | int usTimeout = getMicroSecondTimeout(&timeout); | 
|  | 317 | tv.tv_sec = usTimeout / 1000000; | 
|  | 318 | tv.tv_usec = usTimeout % 1000000; | 
|  | 319 | } | 
|  | 320 | else if(maxFd <= 0) { | 
|  | 321 | tv.tv_sec = 0; | 
|  | 322 | tv.tv_usec = 100000; | 
|  | 323 | } | 
|  | 324 |  | 
|  | 325 | select_test((int)maxFd, &readSet, &writeSet, NULL, &tv); | 
|  | 326 |  | 
|  | 327 | /* Check the sockets for reading / writing */ | 
|  | 328 | checkFdSet(m, &sockets.read, &readSet, CURL_CSELECT_IN, "read"); | 
|  | 329 | checkFdSet(m, &sockets.write, &writeSet, CURL_CSELECT_OUT, "write"); | 
|  | 330 |  | 
|  | 331 | if(timeout.tv_sec != -1 && getMicroSecondTimeout(&timeout) == 0) { | 
|  | 332 | /* Curl's timer has elapsed. */ | 
|  | 333 | notifyCurl(m, CURL_SOCKET_TIMEOUT, 0, "timeout"); | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | abort_on_test_timeout(); | 
|  | 337 | } | 
|  | 338 |  | 
|  | 339 | if(!success) { | 
|  | 340 | fprintf(stderr, "Error uploading file.\n"); | 
|  | 341 | res = TEST_ERR_MAJOR_BAD; | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | test_cleanup: | 
|  | 345 |  | 
|  | 346 | /* proper cleanup sequence - type PB */ | 
|  | 347 |  | 
|  | 348 | curl_multi_remove_handle(m, curl); | 
|  | 349 | curl_easy_cleanup(curl); | 
|  | 350 | curl_multi_cleanup(m); | 
|  | 351 | curl_global_cleanup(); | 
|  | 352 |  | 
|  | 353 | /* close the local file */ | 
|  | 354 | fclose(hd_src); | 
|  | 355 |  | 
|  | 356 | /* free local memory */ | 
|  | 357 | free(sockets.read.sockets); | 
|  | 358 | free(sockets.write.sockets); | 
|  | 359 |  | 
|  | 360 | return res; | 
|  | 361 | } |