quickjs-tart

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

lib526.c (5074B)


      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 /*
     25  * This code sets up multiple easy handles that transfer a single file from
     26  * the same URL, in a serial manner after each other. Due to the connection
     27  * sharing within the multi handle all transfers are performed on the same
     28  * persistent connection.
     29  *
     30  * This source code is used for test526, test527 and test532 with branches
     31  * controlling the small differences.
     32  *
     33  * - test526 closes all easy handles after
     34  *   they all have transferred the file over the single connection
     35  * - test527 closes each easy handle after each single transfer.
     36  * - test532 uses only a single easy handle that is removed, reset and then
     37  *   re-added for each transfer
     38  *
     39  * Test case 526, 527 and 532 use FTP, while test 528 uses the lib526 tool but
     40  * with HTTP.
     41  */
     42 
     43 #include "first.h"
     44 
     45 #include "memdebug.h"
     46 
     47 static CURLcode test_lib526(char *URL)
     48 {
     49   CURLcode res = CURLE_OK;
     50   CURL *curl[NUM_HANDLES];
     51   int running;
     52   CURLM *m = NULL;
     53   size_t current = 0;
     54   size_t i;
     55 
     56   for(i = 0; i < CURL_ARRAYSIZE(curl); i++)
     57     curl[i] = NULL;
     58 
     59   start_test_timing();
     60 
     61   global_init(CURL_GLOBAL_ALL);
     62 
     63   /* get each easy handle */
     64   for(i = 0; i < CURL_ARRAYSIZE(curl); i++) {
     65     easy_init(curl[i]);
     66     /* specify target */
     67     easy_setopt(curl[i], CURLOPT_URL, URL);
     68     /* go verbose */
     69     easy_setopt(curl[i], CURLOPT_VERBOSE, 1L);
     70   }
     71 
     72   multi_init(m);
     73 
     74   multi_add_handle(m, curl[current]);
     75 
     76   curl_mfprintf(stderr, "Start at URL 0\n");
     77 
     78   for(;;) {
     79     struct timeval interval;
     80     fd_set rd, wr, exc;
     81     int maxfd = -99;
     82 
     83     interval.tv_sec = 1;
     84     interval.tv_usec = 0;
     85 
     86     multi_perform(m, &running);
     87 
     88     abort_on_test_timeout();
     89 
     90     if(!running) {
     91       if(testnum == 527) {
     92         /* NOTE: this code does not remove the handle from the multi handle
     93            here, which would be the nice, sane and documented way of working.
     94            This however tests that the API survives this abuse gracefully. */
     95         curl_easy_cleanup(curl[current]);
     96         curl[current] = NULL;
     97       }
     98       if(++current < CURL_ARRAYSIZE(curl)) {
     99         curl_mfprintf(stderr, "Advancing to URL %d\n", (int)current);
    100         if(testnum == 532) {
    101           /* first remove the only handle we use */
    102           curl_multi_remove_handle(m, curl[0]);
    103 
    104           /* make us reuse the same handle all the time, and try resetting
    105              the handle first too */
    106           curl_easy_reset(curl[0]);
    107           easy_setopt(curl[0], CURLOPT_URL, URL);
    108           /* go verbose */
    109           easy_setopt(curl[0], CURLOPT_VERBOSE, 1L);
    110 
    111           /* re-add it */
    112           multi_add_handle(m, curl[0]);
    113         }
    114         else {
    115           multi_add_handle(m, curl[current]);
    116         }
    117       }
    118       else {
    119         break; /* done */
    120       }
    121     }
    122 
    123     FD_ZERO(&rd);
    124     FD_ZERO(&wr);
    125     FD_ZERO(&exc);
    126 
    127     multi_fdset(m, &rd, &wr, &exc, &maxfd);
    128 
    129     /* At this point, maxfd is guaranteed to be greater or equal than -1. */
    130 
    131     select_test(maxfd + 1, &rd, &wr, &exc, &interval);
    132 
    133     abort_on_test_timeout();
    134   }
    135 
    136 test_cleanup:
    137 
    138   if((testnum == 526) || (testnum == 528)) {
    139     /* proper cleanup sequence - type PB */
    140 
    141     for(i = 0; i < CURL_ARRAYSIZE(curl); i++) {
    142       curl_multi_remove_handle(m, curl[i]);
    143       curl_easy_cleanup(curl[i]);
    144     }
    145     curl_multi_cleanup(m);
    146     curl_global_cleanup();
    147   }
    148   else if(testnum == 527) {
    149     /* Upon non-failure test flow the easy's have already been cleanup'ed. In
    150        case there is a failure we arrive here with easy's that have not been
    151        cleanup'ed yet, in this case we have to cleanup them or otherwise these
    152        will be leaked, let's use undocumented cleanup sequence - type UB */
    153 
    154     if(res != CURLE_OK)
    155       for(i = 0; i < CURL_ARRAYSIZE(curl); i++)
    156         curl_easy_cleanup(curl[i]);
    157 
    158     curl_multi_cleanup(m);
    159     curl_global_cleanup();
    160 
    161   }
    162   else if(testnum == 532) {
    163     /* undocumented cleanup sequence - type UB */
    164 
    165     for(i = 0; i < CURL_ARRAYSIZE(curl); i++)
    166       curl_easy_cleanup(curl[i]);
    167     curl_multi_cleanup(m);
    168     curl_global_cleanup();
    169   }
    170 
    171   return res;
    172 }