quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

lib500.c (6678B)


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