xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 2013 - 2022, Linus Nielsen Feltzing <linus@haxx.se> |
| 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 "testutil.h" |
| 27 | #include "warnless.h" |
| 28 | #include "memdebug.h" |
| 29 | |
| 30 | #define TEST_HANG_TIMEOUT 60 * 1000 |
| 31 | |
| 32 | #define NUM_HANDLES 4 |
| 33 | |
| 34 | int test(char *URL) |
| 35 | { |
| 36 | int res = 0; |
| 37 | CURL *curl[NUM_HANDLES] = {0}; |
| 38 | int running; |
| 39 | CURLM *m = NULL; |
| 40 | int i; |
| 41 | char target_url[256]; |
| 42 | char dnsentry[256]; |
| 43 | struct curl_slist *slist = NULL, *slist2; |
| 44 | char *port = libtest_arg3; |
| 45 | char *address = libtest_arg2; |
| 46 | |
| 47 | (void)URL; |
| 48 | |
| 49 | /* Create fake DNS entries for serverX.example.com for all handles */ |
| 50 | for(i = 0; i < NUM_HANDLES; i++) { |
| 51 | msnprintf(dnsentry, sizeof(dnsentry), "server%d.example.com:%s:%s", |
| 52 | i + 1, port, address); |
| 53 | printf("%s\n", dnsentry); |
| 54 | slist2 = curl_slist_append(slist, dnsentry); |
| 55 | if(!slist2) { |
| 56 | fprintf(stderr, "curl_slist_append() failed\n"); |
| 57 | goto test_cleanup; |
| 58 | } |
| 59 | slist = slist2; |
| 60 | } |
| 61 | |
| 62 | start_test_timing(); |
| 63 | |
| 64 | global_init(CURL_GLOBAL_ALL); |
| 65 | |
| 66 | multi_init(m); |
| 67 | |
| 68 | multi_setopt(m, CURLMOPT_MAXCONNECTS, 3L); |
| 69 | |
| 70 | /* get NUM_HANDLES easy handles */ |
| 71 | for(i = 0; i < NUM_HANDLES; i++) { |
| 72 | /* get an easy handle */ |
| 73 | easy_init(curl[i]); |
| 74 | /* specify target */ |
| 75 | msnprintf(target_url, sizeof(target_url), |
| 76 | "http://server%d.example.com:%s/path/1506%04i", |
| 77 | i + 1, port, i + 1); |
| 78 | target_url[sizeof(target_url) - 1] = '\0'; |
| 79 | easy_setopt(curl[i], CURLOPT_URL, target_url); |
| 80 | /* go verbose */ |
| 81 | easy_setopt(curl[i], CURLOPT_VERBOSE, 1L); |
| 82 | /* include headers */ |
| 83 | easy_setopt(curl[i], CURLOPT_HEADER, 1L); |
| 84 | |
| 85 | easy_setopt(curl[i], CURLOPT_RESOLVE, slist); |
| 86 | } |
| 87 | |
| 88 | fprintf(stderr, "Start at URL 0\n"); |
| 89 | |
| 90 | for(i = 0; i < NUM_HANDLES; i++) { |
| 91 | /* add handle to multi */ |
| 92 | multi_add_handle(m, curl[i]); |
| 93 | |
| 94 | for(;;) { |
| 95 | struct timeval interval; |
| 96 | fd_set rd, wr, exc; |
| 97 | int maxfd = -99; |
| 98 | |
| 99 | interval.tv_sec = 1; |
| 100 | interval.tv_usec = 0; |
| 101 | |
| 102 | multi_perform(m, &running); |
| 103 | |
| 104 | abort_on_test_timeout(); |
| 105 | |
| 106 | if(!running) |
| 107 | break; /* done */ |
| 108 | |
| 109 | FD_ZERO(&rd); |
| 110 | FD_ZERO(&wr); |
| 111 | FD_ZERO(&exc); |
| 112 | |
| 113 | multi_fdset(m, &rd, &wr, &exc, &maxfd); |
| 114 | |
| 115 | /* At this point, maxfd is guaranteed to be greater or equal than -1. */ |
| 116 | |
| 117 | select_test(maxfd + 1, &rd, &wr, &exc, &interval); |
| 118 | |
| 119 | abort_on_test_timeout(); |
| 120 | } |
| 121 | wait_ms(1); /* to ensure different end times */ |
| 122 | } |
| 123 | |
| 124 | test_cleanup: |
| 125 | |
| 126 | /* proper cleanup sequence - type PB */ |
| 127 | |
| 128 | for(i = 0; i < NUM_HANDLES; i++) { |
| 129 | curl_multi_remove_handle(m, curl[i]); |
| 130 | curl_easy_cleanup(curl[i]); |
| 131 | } |
| 132 | |
| 133 | curl_slist_free_all(slist); |
| 134 | |
| 135 | curl_multi_cleanup(m); |
| 136 | curl_global_cleanup(); |
| 137 | |
| 138 | return res; |
| 139 | } |