xref: /curl/tests/libtest/lib2402.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/2402%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 
91   fprintf(stderr, "Start at URL 0\n");
92 
93   for(i = 0; i < NUM_HANDLES; i++) {
94     /* add handle to multi */
95     multi_add_handle(m, curl[i]);
96 
97     for(;;) {
98       struct timeval interval;
99       fd_set rd, wr, exc;
100       int maxfd = -99;
101 
102       interval.tv_sec = 1;
103       interval.tv_usec = 0;
104 
105       multi_perform(m, &running);
106 
107       abort_on_test_timeout();
108 
109       if(!running)
110         break; /* done */
111 
112       FD_ZERO(&rd);
113       FD_ZERO(&wr);
114       FD_ZERO(&exc);
115 
116       multi_fdset(m, &rd, &wr, &exc, &maxfd);
117 
118       /* At this point, maxfd is guaranteed to be greater or equal than -1. */
119 
120       select_test(maxfd + 1, &rd, &wr, &exc, &interval);
121 
122       abort_on_test_timeout();
123     }
124     wait_ms(1); /* to ensure different end times */
125   }
126 
127 test_cleanup:
128 
129   /* proper cleanup sequence - type PB */
130 
131   for(i = 0; i < NUM_HANDLES; i++) {
132     curl_multi_remove_handle(m, curl[i]);
133     curl_easy_cleanup(curl[i]);
134   }
135 
136   curl_slist_free_all(slist);
137 
138   curl_multi_cleanup(m);
139   curl_global_cleanup();
140 
141   return res;
142 }
143