xref: /curl/tests/libtest/lib2404.c (revision 25cbc2f7)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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 
test(char * URL)34 CURLcode test(char *URL)
35 {
36   CURLcode res = CURLE_OK;
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;
44   char *port = libtest_arg3;
45   char *address = libtest_arg2;
46 
47   (void)URL;
48 
49   msnprintf(dnsentry, sizeof(dnsentry), "localhost:%s:%s",
50             port, address);
51   printf("%s\n", dnsentry);
52   slist = curl_slist_append(slist, dnsentry);
53   if(!slist) {
54     fprintf(stderr, "curl_slist_append() failed\n");
55     goto test_cleanup;
56   }
57 
58   start_test_timing();
59 
60   global_init(CURL_GLOBAL_ALL);
61 
62   multi_init(m);
63 
64   multi_setopt(m, CURLMOPT_MAXCONNECTS, 1L);
65 
66   /* get NUM_HANDLES easy handles */
67   for(i = 0; i < NUM_HANDLES; i++) {
68     /* get an easy handle */
69     easy_init(curl[i]);
70     /* specify target */
71     msnprintf(target_url, sizeof(target_url),
72               "https://localhost:%s/path/2404%04i",
73               port, i + 1);
74     target_url[sizeof(target_url) - 1] = '\0';
75     easy_setopt(curl[i], CURLOPT_URL, target_url);
76     /* go http2 */
77     easy_setopt(curl[i], CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
78     /* no peer verify */
79     easy_setopt(curl[i], CURLOPT_SSL_VERIFYPEER, 0L);
80     easy_setopt(curl[i], CURLOPT_SSL_VERIFYHOST, 0L);
81     /* wait for first connection established to see if we can share it */
82     easy_setopt(curl[i], CURLOPT_PIPEWAIT, 1L);
83     /* go verbose */
84     easy_setopt(curl[i], CURLOPT_VERBOSE, 1L);
85     /* include headers */
86     easy_setopt(curl[i], CURLOPT_HEADER, 1L);
87 
88     easy_setopt(curl[i], CURLOPT_RESOLVE, slist);
89 
90     easy_setopt(curl[i], CURLOPT_STREAM_WEIGHT, (long)128 + i);
91   }
92 
93   fprintf(stderr, "Start at URL 0\n");
94 
95   for(i = 0; i < NUM_HANDLES; i++) {
96     /* add handle to multi */
97     multi_add_handle(m, curl[i]);
98 
99     for(;;) {
100       struct timeval interval;
101       fd_set rd, wr, exc;
102       int maxfd = -99;
103 
104       interval.tv_sec = 1;
105       interval.tv_usec = 0;
106 
107       multi_perform(m, &running);
108 
109       abort_on_test_timeout();
110 
111       if(!running)
112         break; /* done */
113 
114       FD_ZERO(&rd);
115       FD_ZERO(&wr);
116       FD_ZERO(&exc);
117 
118       multi_fdset(m, &rd, &wr, &exc, &maxfd);
119 
120       /* At this point, maxfd is guaranteed to be greater or equal than -1. */
121 
122       select_test(maxfd + 1, &rd, &wr, &exc, &interval);
123 
124       abort_on_test_timeout();
125     }
126     wait_ms(1); /* to ensure different end times */
127   }
128 
129 test_cleanup:
130 
131   /* proper cleanup sequence - type PB */
132 
133   for(i = 0; i < NUM_HANDLES; i++) {
134     curl_multi_remove_handle(m, curl[i]);
135     curl_easy_cleanup(curl[i]);
136   }
137 
138   curl_slist_free_all(slist);
139 
140   curl_multi_cleanup(m);
141   curl_global_cleanup();
142 
143   return res;
144 }
145