xref: /curl/tests/libtest/lib2502.c (revision 17a7e12e)
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 "testtrace.h"
27 #include "testutil.h"
28 #include "warnless.h"
29 #include "memdebug.h"
30 
31 #define TEST_HANG_TIMEOUT 60 * 1000
32 
33 #define NUM_HANDLES 4
34 
test(char * URL)35 CURLcode test(char *URL)
36 {
37   CURLcode res = CURLE_OK;
38   CURL *curl[NUM_HANDLES] = {0};
39   int running;
40   CURLM *m = NULL;
41   int i;
42   char target_url[256];
43   char dnsentry[256];
44   struct curl_slist *slist = NULL;
45   char *port = libtest_arg3;
46   char *address = libtest_arg2;
47 
48   (void)URL;
49 
50   msnprintf(dnsentry, sizeof(dnsentry), "localhost:%s:%s",
51             port, address);
52   printf("%s\n", dnsentry);
53   slist = curl_slist_append(slist, dnsentry);
54   if(!slist) {
55     fprintf(stderr, "curl_slist_append() failed\n");
56     goto test_cleanup;
57   }
58 
59   start_test_timing();
60 
61   global_init(CURL_GLOBAL_ALL);
62 
63   multi_init(m);
64 
65   multi_setopt(m, CURLMOPT_MAXCONNECTS, 1L);
66 
67   /* get NUM_HANDLES easy handles */
68   for(i = 0; i < NUM_HANDLES; i++) {
69     /* get an easy handle */
70     easy_init(curl[i]);
71     /* specify target */
72     msnprintf(target_url, sizeof(target_url),
73               "https://localhost:%s/path/2502%04i",
74               port, i + 1);
75     target_url[sizeof(target_url) - 1] = '\0';
76     easy_setopt(curl[i], CURLOPT_URL, target_url);
77     /* go http2 */
78     easy_setopt(curl[i], CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_3ONLY);
79     easy_setopt(curl[i], CURLOPT_CONNECTTIMEOUT_MS, (long)5000);
80     easy_setopt(curl[i], CURLOPT_CAINFO, "./certs/EdelCurlRoot-ca.cacert");
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     libtest_debug_config.nohex = 1;
85     libtest_debug_config.tracetime = 0;
86     test_setopt(curl[i], CURLOPT_DEBUGDATA, &libtest_debug_config);
87     easy_setopt(curl[i], CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
88     easy_setopt(curl[i], CURLOPT_VERBOSE, 1L);
89     /* include headers */
90     easy_setopt(curl[i], CURLOPT_HEADER, 1L);
91 
92     easy_setopt(curl[i], CURLOPT_RESOLVE, slist);
93   }
94 
95   fprintf(stderr, "Start at URL 0\n");
96 
97   for(i = 0; i < NUM_HANDLES; i++) {
98     /* add handle to multi */
99     multi_add_handle(m, curl[i]);
100 
101     for(;;) {
102       struct timeval interval;
103       fd_set rd, wr, exc;
104       int maxfd = -99;
105 
106       interval.tv_sec = 1;
107       interval.tv_usec = 0;
108 
109       multi_perform(m, &running);
110 
111       abort_on_test_timeout();
112 
113       if(!running)
114         break; /* done */
115 
116       FD_ZERO(&rd);
117       FD_ZERO(&wr);
118       FD_ZERO(&exc);
119 
120       multi_fdset(m, &rd, &wr, &exc, &maxfd);
121 
122       /* At this point, maxfd is guaranteed to be greater or equal than -1. */
123 
124       select_test(maxfd + 1, &rd, &wr, &exc, &interval);
125 
126       abort_on_test_timeout();
127     }
128     wait_ms(1); /* to ensure different end times */
129   }
130 
131 test_cleanup:
132 
133   /* proper cleanup sequence - type PB */
134 
135   for(i = 0; i < NUM_HANDLES; i++) {
136     curl_multi_remove_handle(m, curl[i]);
137     curl_easy_cleanup(curl[i]);
138   }
139 
140   curl_slist_free_all(slist);
141 
142   curl_multi_cleanup(m);
143   curl_global_cleanup();
144 
145   return res;
146 }
147