quickjs-tart

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

lib1541.c (4695B)


      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 "memdebug.h"
     27 
     28 struct t1541_transfer_status {
     29   CURL *easy;
     30   int hd_count;
     31   int bd_count;
     32   CURLcode result;
     33 };
     34 
     35 #define KN(a)   a, #a
     36 
     37 static int t1541_geterr(const char *name, CURLcode val, int lineno)
     38 {
     39   curl_mprintf("CURLINFO_%s returned %d, \"%s\" on line %d\n",
     40                name, val, curl_easy_strerror(val), lineno);
     41   return (int)val;
     42 }
     43 
     44 static void report_time(const char *key, const char *where, curl_off_t time,
     45                         bool ok)
     46 {
     47   if(ok)
     48     curl_mprintf("%s on %s is OK\n", key, where);
     49   else
     50     curl_mprintf("%s on %s is WRONG: %" CURL_FORMAT_CURL_OFF_T "\n",
     51                  key, where, time);
     52 }
     53 
     54 static void check_time(CURL *easy, int key, const char *name,
     55                        const char *where)
     56 {
     57   curl_off_t tval;
     58   CURLcode res = curl_easy_getinfo(easy, (CURLINFO)key, &tval);
     59   if(res) {
     60     t1541_geterr(name, res, __LINE__);
     61   }
     62   else
     63     report_time(name, where, tval, tval > 0);
     64 }
     65 
     66 static void check_time0(CURL *easy, int key, const char *name,
     67                         const char *where)
     68 {
     69   curl_off_t tval;
     70   CURLcode res = curl_easy_getinfo(easy, (CURLINFO)key, &tval);
     71   if(res) {
     72     t1541_geterr(name, res, __LINE__);
     73   }
     74   else
     75     report_time(name, where, tval, !tval);
     76 }
     77 
     78 static size_t t1541_header_callback(char *ptr, size_t size, size_t nmemb,
     79                                     void *userp)
     80 {
     81   struct t1541_transfer_status *st = (struct t1541_transfer_status *)userp;
     82   size_t len = size * nmemb;
     83 
     84   (void)ptr;
     85   if(!st->hd_count++) {
     86     /* first header, check some CURLINFO value to be reported. See #13125 */
     87     check_time(st->easy, KN(CURLINFO_CONNECT_TIME_T), "1st header");
     88     check_time(st->easy, KN(CURLINFO_PRETRANSFER_TIME_T), "1st header");
     89     check_time(st->easy, KN(CURLINFO_STARTTRANSFER_TIME_T), "1st header");
     90     /* continuously updated */
     91     check_time(st->easy, KN(CURLINFO_TOTAL_TIME_T), "1st header");
     92     /* no SSL, must be 0 */
     93     check_time0(st->easy, KN(CURLINFO_APPCONNECT_TIME_T), "1st header");
     94     /* download not really started */
     95     check_time0(st->easy, KN(CURLINFO_SPEED_DOWNLOAD_T), "1st header");
     96   }
     97   (void)fwrite(ptr, size, nmemb, stdout);
     98   return len;
     99 }
    100 
    101 static size_t t1541_write_cb(char *ptr, size_t size, size_t nmemb, void *userp)
    102 {
    103   struct t1541_transfer_status *st = (struct t1541_transfer_status *)userp;
    104 
    105   (void)ptr;
    106   (void)st;
    107   fwrite(ptr, size, nmemb, stdout);
    108   return size * nmemb;
    109 }
    110 
    111 static CURLcode test_lib1541(char *URL)
    112 {
    113   CURL *curls = NULL;
    114   CURLcode res = CURLE_OK;
    115   struct t1541_transfer_status st;
    116 
    117   start_test_timing();
    118 
    119   memset(&st, 0, sizeof(st));
    120 
    121   global_init(CURL_GLOBAL_ALL);
    122 
    123   easy_init(curls);
    124   st.easy = curls; /* to allow callbacks access */
    125 
    126   easy_setopt(curls, CURLOPT_URL, URL);
    127   easy_setopt(curls, CURLOPT_WRITEFUNCTION, t1541_write_cb);
    128   easy_setopt(curls, CURLOPT_WRITEDATA, &st);
    129   easy_setopt(curls, CURLOPT_HEADERFUNCTION, t1541_header_callback);
    130   easy_setopt(curls, CURLOPT_HEADERDATA, &st);
    131 
    132   easy_setopt(curls, CURLOPT_NOPROGRESS, 0L);
    133 
    134   res = curl_easy_perform(curls);
    135 
    136   check_time(curls, KN(CURLINFO_CONNECT_TIME_T), "done");
    137   check_time(curls, KN(CURLINFO_PRETRANSFER_TIME_T), "done");
    138   check_time(curls, KN(CURLINFO_POSTTRANSFER_TIME_T), "done");
    139   check_time(curls, KN(CURLINFO_STARTTRANSFER_TIME_T), "done");
    140   /* no SSL, must be 0 */
    141   check_time0(curls, KN(CURLINFO_APPCONNECT_TIME_T), "done");
    142   check_time(curls, KN(CURLINFO_SPEED_DOWNLOAD_T), "done");
    143   check_time(curls, KN(CURLINFO_TOTAL_TIME_T), "done");
    144 
    145 test_cleanup:
    146 
    147   curl_easy_cleanup(curls);
    148   curl_global_cleanup();
    149 
    150   return res; /* return the final return code */
    151 }