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