Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Haibo Huang | 24c77a1 | 2020-04-29 13:49:57 -0700 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al. |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 |
Elliott Hughes | 34dd5f4 | 2021-08-10 13:01:18 -0700 | [diff] [blame] | 12 | * are also available at https://curl.se/docs/copyright.html. |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 | ***************************************************************************/ |
| 22 | #include "test.h" |
| 23 | |
| 24 | #include <fcntl.h> |
| 25 | |
| 26 | #include "testutil.h" |
| 27 | #include "warnless.h" |
| 28 | #include "memdebug.h" |
| 29 | |
| 30 | #define TEST_HANG_TIMEOUT 60 * 1000 |
| 31 | |
| 32 | struct Sockets |
| 33 | { |
| 34 | curl_socket_t *sockets; |
| 35 | int count; /* number of sockets actually stored in array */ |
| 36 | int max_count; /* max number of sockets that fit in allocated array */ |
| 37 | }; |
| 38 | |
| 39 | struct ReadWriteSockets |
| 40 | { |
| 41 | struct Sockets read, write; |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * Remove a file descriptor from a sockets array. |
| 46 | */ |
Haibo Huang | ca2a802 | 2020-07-10 20:17:42 -0700 | [diff] [blame] | 47 | static void removeFd(struct Sockets *sockets, curl_socket_t fd, int mention) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 48 | { |
| 49 | int i; |
| 50 | |
| 51 | if(mention) |
| 52 | fprintf(stderr, "Remove socket fd %d\n", (int) fd); |
| 53 | |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 54 | for(i = 0; i < sockets->count; ++i) { |
| 55 | if(sockets->sockets[i] == fd) { |
| 56 | if(i < sockets->count - 1) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 57 | memmove(&sockets->sockets[i], &sockets->sockets[i + 1], |
| 58 | sizeof(curl_socket_t) * (sockets->count - (i + 1))); |
| 59 | --sockets->count; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Add a file descriptor to a sockets array. |
| 66 | */ |
Haibo Huang | ca2a802 | 2020-07-10 20:17:42 -0700 | [diff] [blame] | 67 | static void addFd(struct Sockets *sockets, curl_socket_t fd, const char *what) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 68 | { |
| 69 | /** |
| 70 | * To ensure we only have each file descriptor once, we remove it then add |
| 71 | * it again. |
| 72 | */ |
| 73 | fprintf(stderr, "Add socket fd %d for %s\n", (int) fd, what); |
| 74 | removeFd(sockets, fd, 0); |
| 75 | /* |
| 76 | * Allocate array storage when required. |
| 77 | */ |
| 78 | if(!sockets->sockets) { |
| 79 | sockets->sockets = malloc(sizeof(curl_socket_t) * 20U); |
| 80 | if(!sockets->sockets) |
| 81 | return; |
| 82 | sockets->max_count = 20; |
| 83 | } |
| 84 | else if(sockets->count + 1 > sockets->max_count) { |
| 85 | curl_socket_t *oldptr = sockets->sockets; |
| 86 | sockets->sockets = realloc(oldptr, sizeof(curl_socket_t) * |
| 87 | (sockets->max_count + 20)); |
| 88 | if(!sockets->sockets) { |
| 89 | /* cleanup in test_cleanup */ |
| 90 | sockets->sockets = oldptr; |
| 91 | return; |
| 92 | } |
| 93 | sockets->max_count += 20; |
| 94 | } |
| 95 | /* |
| 96 | * Add file descriptor to array. |
| 97 | */ |
| 98 | sockets->sockets[sockets->count] = fd; |
| 99 | ++sockets->count; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Callback invoked by curl to poll reading / writing of a socket. |
| 104 | */ |
| 105 | static int curlSocketCallback(CURL *easy, curl_socket_t s, int action, |
| 106 | void *userp, void *socketp) |
| 107 | { |
Haibo Huang | ca2a802 | 2020-07-10 20:17:42 -0700 | [diff] [blame] | 108 | struct ReadWriteSockets *sockets = userp; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 109 | |
| 110 | (void)easy; /* unused */ |
| 111 | (void)socketp; /* unused */ |
| 112 | |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 113 | if(action == CURL_POLL_IN || action == CURL_POLL_INOUT) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 114 | addFd(&sockets->read, s, "read"); |
| 115 | |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 116 | if(action == CURL_POLL_OUT || action == CURL_POLL_INOUT) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 117 | addFd(&sockets->write, s, "write"); |
| 118 | |
| 119 | if(action == CURL_POLL_REMOVE) { |
| 120 | removeFd(&sockets->read, s, 1); |
| 121 | removeFd(&sockets->write, s, 0); |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Callback invoked by curl to set a timeout. |
| 129 | */ |
| 130 | static int curlTimerCallback(CURLM *multi, long timeout_ms, void *userp) |
| 131 | { |
Haibo Huang | ca2a802 | 2020-07-10 20:17:42 -0700 | [diff] [blame] | 132 | struct timeval *timeout = userp; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 133 | |
| 134 | (void)multi; /* unused */ |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 135 | if(timeout_ms != -1) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 136 | *timeout = tutil_tvnow(); |
| 137 | timeout->tv_usec += timeout_ms * 1000; |
| 138 | } |
| 139 | else { |
| 140 | timeout->tv_sec = -1; |
| 141 | } |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Check for curl completion. |
| 147 | */ |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 148 | static int checkForCompletion(CURLM *curl, int *success) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 149 | { |
| 150 | int numMessages; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 151 | CURLMsg *message; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 152 | int result = 0; |
| 153 | *success = 0; |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 154 | while((message = curl_multi_info_read(curl, &numMessages)) != NULL) { |
| 155 | if(message->msg == CURLMSG_DONE) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 156 | result = 1; |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 157 | if(message->data.result == CURLE_OK) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 158 | *success = 1; |
| 159 | else |
| 160 | *success = 0; |
| 161 | } |
| 162 | else { |
| 163 | fprintf(stderr, "Got an unexpected message from curl: %i\n", |
| 164 | (int)message->msg); |
| 165 | result = 1; |
| 166 | *success = 0; |
| 167 | } |
| 168 | } |
| 169 | return result; |
| 170 | } |
| 171 | |
Haibo Huang | ca2a802 | 2020-07-10 20:17:42 -0700 | [diff] [blame] | 172 | static int getMicroSecondTimeout(struct timeval *timeout) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 173 | { |
| 174 | struct timeval now; |
| 175 | ssize_t result; |
| 176 | now = tutil_tvnow(); |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 177 | result = (ssize_t)((timeout->tv_sec - now.tv_sec) * 1000000 + |
| 178 | timeout->tv_usec - now.tv_usec); |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 179 | if(result < 0) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 180 | result = 0; |
| 181 | |
| 182 | return curlx_sztosi(result); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Update a fd_set with all of the sockets in use. |
| 187 | */ |
Haibo Huang | ca2a802 | 2020-07-10 20:17:42 -0700 | [diff] [blame] | 188 | static void updateFdSet(struct Sockets *sockets, fd_set* fdset, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 189 | curl_socket_t *maxFd) |
| 190 | { |
| 191 | int i; |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 192 | for(i = 0; i < sockets->count; ++i) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 193 | FD_SET(sockets->sockets[i], fdset); |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 194 | if(*maxFd < sockets->sockets[i] + 1) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 195 | *maxFd = sockets->sockets[i] + 1; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | static void notifyCurl(CURLM *curl, curl_socket_t s, int evBitmask, |
| 201 | const char *info) |
| 202 | { |
| 203 | int numhandles = 0; |
| 204 | CURLMcode result = curl_multi_socket_action(curl, s, evBitmask, &numhandles); |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 205 | if(result != CURLM_OK) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 206 | fprintf(stderr, "Curl error on %s: %i (%s)\n", |
| 207 | info, result, curl_multi_strerror(result)); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Invoke curl when a file descriptor is set. |
| 213 | */ |
| 214 | static void checkFdSet(CURLM *curl, struct Sockets *sockets, fd_set *fdset, |
| 215 | int evBitmask, const char *name) |
| 216 | { |
| 217 | int i; |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 218 | for(i = 0; i < sockets->count; ++i) { |
| 219 | if(FD_ISSET(sockets->sockets[i], fdset)) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 220 | notifyCurl(curl, sockets->sockets[i], evBitmask, name); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | int test(char *URL) |
| 226 | { |
| 227 | int res = 0; |
| 228 | CURL *curl = NULL; |
| 229 | FILE *hd_src = NULL; |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 230 | int hd; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 231 | struct_stat file_info; |
| 232 | CURLM *m = NULL; |
| 233 | struct ReadWriteSockets sockets = {{NULL, 0, 0}, {NULL, 0, 0}}; |
| 234 | struct timeval timeout = {-1, 0}; |
| 235 | int success = 0; |
| 236 | |
| 237 | start_test_timing(); |
| 238 | |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 239 | if(!libtest_arg3) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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(NULL == hd_src) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 246 | fprintf(stderr, "fopen() failed with error: %d (%s)\n", |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 247 | errno, strerror(errno)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 256 | fprintf(stderr, "fstat() failed with error: %d (%s)\n", |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 257 | errno, strerror(errno)); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 */ |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 276 | easy_setopt(curl, CURLOPT_URL, URL); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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, "curl_client_key.pub"); |
| 286 | easy_setopt(curl, CURLOPT_SSH_PRIVATE_KEYFILE, "curl_client_key"); |
Elliott Hughes | 0128fe4 | 2018-02-27 14:57:55 -0800 | [diff] [blame] | 287 | easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 | |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 301 | while(!checkForCompletion(m, &success)) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 | |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 311 | if(timeout.tv_sec != -1) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 312 | int usTimeout = getMicroSecondTimeout(&timeout); |
| 313 | tv.tv_sec = usTimeout / 1000000; |
| 314 | tv.tv_usec = usTimeout % 1000000; |
| 315 | } |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 316 | else if(maxFd <= 0) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 317 | tv.tv_sec = 0; |
| 318 | tv.tv_usec = 100000; |
| 319 | } |
| 320 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 321 | select_test((int)maxFd, &readSet, &writeSet, NULL, &tv); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 | |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 327 | if(timeout.tv_sec != -1 && getMicroSecondTimeout(&timeout) == 0) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 328 | /* Curl's timer has elapsed. */ |
| 329 | notifyCurl(m, CURL_SOCKET_TIMEOUT, 0, "timeout"); |
| 330 | } |
| 331 | |
| 332 | abort_on_test_timeout(); |
| 333 | } |
| 334 | |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 335 | if(!success) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 | } |