xref: /curl/tests/libtest/lib500.c (revision 71cf0d1f)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
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 "memdebug.h"
28 
29 #ifdef LIB585
30 
31 static int testcounter;
32 
tst_opensocket(void * clientp,curlsocktype purpose,struct curl_sockaddr * addr)33 static curl_socket_t tst_opensocket(void *clientp,
34                                     curlsocktype purpose,
35                                     struct curl_sockaddr *addr)
36 {
37   (void)clientp;
38   (void)purpose;
39   printf("[OPEN] counter: %d\n", ++testcounter);
40   return socket(addr->family, addr->socktype, addr->protocol);
41 }
42 
tst_closesocket(void * clientp,curl_socket_t sock)43 static int tst_closesocket(void *clientp, curl_socket_t sock)
44 {
45   (void)clientp;
46   printf("[CLOSE] counter: %d\n", testcounter--);
47   return sclose(sock);
48 }
49 
setupcallbacks(CURL * curl)50 static void setupcallbacks(CURL *curl)
51 {
52   curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, tst_opensocket);
53   curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, tst_closesocket);
54   testcounter = 0;
55 }
56 
57 #else
58 #define setupcallbacks(x) Curl_nop_stmt
59 #endif
60 
61 
test(char * URL)62 CURLcode test(char *URL)
63 {
64   CURLcode res;
65   CURL *curl;
66   char *ipstr = NULL;
67 
68   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
69     fprintf(stderr, "curl_global_init() failed\n");
70     return TEST_ERR_MAJOR_BAD;
71   }
72 
73   curl = curl_easy_init();
74   if(!curl) {
75     fprintf(stderr, "curl_easy_init() failed\n");
76     curl_global_cleanup();
77     return TEST_ERR_MAJOR_BAD;
78   }
79 
80   test_setopt(curl, CURLOPT_URL, URL);
81   test_setopt(curl, CURLOPT_HEADER, 1L);
82 
83   libtest_debug_config.nohex = 1;
84   libtest_debug_config.tracetime = 1;
85   test_setopt(curl, CURLOPT_DEBUGDATA, &libtest_debug_config);
86   test_setopt(curl, CURLOPT_DEBUGFUNCTION, libtest_debug_cb);
87   test_setopt(curl, CURLOPT_VERBOSE, 1L);
88 
89   if(libtest_arg3 && !strcmp(libtest_arg3, "activeftp"))
90     test_setopt(curl, CURLOPT_FTPPORT, "-");
91 
92   setupcallbacks(curl);
93 
94   res = curl_easy_perform(curl);
95 
96   if(!res) {
97     res = curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ipstr);
98     if(libtest_arg2) {
99       FILE *moo = fopen(libtest_arg2, "wb");
100       if(moo) {
101         curl_off_t time_namelookup;
102         curl_off_t time_connect;
103         curl_off_t time_pretransfer;
104         curl_off_t time_posttransfer;
105         curl_off_t time_starttransfer;
106         curl_off_t time_total;
107         fprintf(moo, "IP %s\n", ipstr);
108         curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME_T, &time_namelookup);
109         curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME_T, &time_connect);
110         curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME_T,
111                           &time_pretransfer);
112         curl_easy_getinfo(curl, CURLINFO_POSTTRANSFER_TIME_T,
113                           &time_posttransfer);
114         curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME_T,
115                           &time_starttransfer);
116         curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &time_total);
117 
118         /* since the timing will always vary we only compare relative
119            differences between these 5 times */
120         if(time_namelookup > time_connect) {
121           fprintf(moo, "namelookup vs connect: %" CURL_FORMAT_CURL_OFF_T
122                   ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
123                   (time_namelookup / 1000000),
124                   (long)(time_namelookup % 1000000),
125                   (time_connect / 1000000), (long)(time_connect % 1000000));
126         }
127         if(time_connect > time_pretransfer) {
128           fprintf(moo, "connect vs pretransfer: %" CURL_FORMAT_CURL_OFF_T
129                   ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
130                   (time_connect / 1000000), (long)(time_connect % 1000000),
131                   (time_pretransfer / 1000000),
132                   (long)(time_pretransfer % 1000000));
133         }
134         if(time_pretransfer > time_posttransfer) {
135           fprintf(moo, "pretransfer vs posttransfer: %" CURL_FORMAT_CURL_OFF_T
136                   ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
137                   (time_pretransfer / 1000000),
138                   (long)(time_pretransfer % 1000000),
139                   (time_posttransfer / 1000000),
140                   (long)(time_posttransfer % 1000000));
141         }
142         if(time_pretransfer > time_starttransfer) {
143           fprintf(moo, "pretransfer vs starttransfer: %" CURL_FORMAT_CURL_OFF_T
144                   ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
145                   (time_pretransfer / 1000000),
146                   (long)(time_pretransfer % 1000000),
147                   (time_starttransfer / 1000000),
148                   (long)(time_starttransfer % 1000000));
149         }
150         if(time_starttransfer > time_total) {
151           fprintf(moo, "starttransfer vs total: %" CURL_FORMAT_CURL_OFF_T
152                   ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
153                   (time_starttransfer / 1000000),
154                   (long)(time_starttransfer % 1000000),
155                   (time_total / 1000000), (long)(time_total % 1000000));
156         }
157         if(time_posttransfer > time_total) {
158           fprintf(moo, "posttransfer vs total: %" CURL_FORMAT_CURL_OFF_T
159                   ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
160                   (time_posttransfer / 1000000),
161                   (long)(time_posttransfer % 1000000),
162                   (time_total / 1000000), (long)(time_total % 1000000));
163         }
164 
165         fclose(moo);
166       }
167     }
168   }
169 
170 test_cleanup:
171 
172   curl_easy_cleanup(curl);
173   curl_global_cleanup();
174 
175   return res;
176 }
177 
178 #undef setupcallbacks
179