xref: /third_party/curl/tests/libtest/lib500.c (revision 13498266)
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
31static int counter;
32
33static 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", ++counter);
40  return socket(addr->family, addr->socktype, addr->protocol);
41}
42
43static int tst_closesocket(void *clientp, curl_socket_t sock)
44{
45  (void)clientp;
46  printf("[CLOSE] counter: %d\n", counter--);
47  return sclose(sock);
48}
49
50static void setupcallbacks(CURL *curl)
51{
52  curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, tst_opensocket);
53  curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, tst_closesocket);
54  counter = 0;
55}
56
57#else
58#define setupcallbacks(x) Curl_nop_stmt
59#endif
60
61
62int 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_starttransfer;
105        curl_off_t time_total;
106        fprintf(moo, "IP %s\n", ipstr);
107        curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME_T, &time_namelookup);
108        curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME_T, &time_connect);
109        curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME_T,
110                          &time_pretransfer);
111        curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME_T,
112                          &time_starttransfer);
113        curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME_T, &time_total);
114
115        /* since the timing will always vary we only compare relative
116           differences between these 5 times */
117        if(time_namelookup > time_connect) {
118          fprintf(moo, "namelookup vs connect: %" CURL_FORMAT_CURL_OFF_T
119                  ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
120                  (time_namelookup / 1000000),
121                  (long)(time_namelookup % 1000000),
122                  (time_connect / 1000000), (long)(time_connect % 1000000));
123        }
124        if(time_connect > time_pretransfer) {
125          fprintf(moo, "connect vs pretransfer: %" CURL_FORMAT_CURL_OFF_T
126                  ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
127                  (time_connect / 1000000), (long)(time_connect % 1000000),
128                  (time_pretransfer / 1000000),
129                  (long)(time_pretransfer % 1000000));
130        }
131        if(time_pretransfer > time_starttransfer) {
132          fprintf(moo, "pretransfer vs starttransfer: %" CURL_FORMAT_CURL_OFF_T
133                  ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
134                  (time_pretransfer / 1000000),
135                  (long)(time_pretransfer % 1000000),
136                  (time_starttransfer / 1000000),
137                  (long)(time_starttransfer % 1000000));
138        }
139        if(time_starttransfer > time_total) {
140          fprintf(moo, "starttransfer vs total: %" CURL_FORMAT_CURL_OFF_T
141                  ".%06ld %" CURL_FORMAT_CURL_OFF_T ".%06ld\n",
142                  (time_starttransfer / 1000000),
143                  (long)(time_starttransfer % 1000000),
144                  (time_total / 1000000), (long)(time_total % 1000000));
145        }
146
147        fclose(moo);
148      }
149    }
150  }
151
152test_cleanup:
153
154  curl_easy_cleanup(curl);
155  curl_global_cleanup();
156
157  return (int)res;
158}
159