quickjs-tart

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

lib1591.c (3275B)


      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 /*
     26  * This unit test PUT http data over proxy. Proxy header will be different
     27  * from server http header
     28  */
     29 
     30 #include "first.h"
     31 
     32 #include "memdebug.h"
     33 
     34 static size_t consumed = 0;
     35 
     36 static size_t t1591_read_cb(char *ptr, size_t size, size_t nmemb, void *stream)
     37 {
     38   static const char testdata[] = "Hello Cloud!\r\n";
     39 
     40   size_t amount = nmemb * size; /* Total bytes curl wants */
     41 
     42   if(consumed == strlen(testdata)) {
     43     return 0;
     44   }
     45 
     46   if(amount > strlen(testdata) - consumed) {
     47     amount = strlen(testdata);
     48   }
     49 
     50   consumed += amount;
     51   (void)stream;
     52   memcpy(ptr, testdata, amount);
     53   return amount;
     54 }
     55 
     56 /*
     57  * carefully not leak memory on OOM
     58  */
     59 static int t1591_trailers_callback(struct curl_slist **list, void *userdata)
     60 {
     61   struct curl_slist *nlist = NULL;
     62   struct curl_slist *nlist2 = NULL;
     63   (void)userdata;
     64   nlist = curl_slist_append(*list, "my-super-awesome-trailer: trail1");
     65   if(nlist)
     66     nlist2 = curl_slist_append(nlist, "my-other-awesome-trailer: trail2");
     67   if(nlist2) {
     68     *list = nlist2;
     69     return CURL_TRAILERFUNC_OK;
     70   }
     71   else {
     72     curl_slist_free_all(nlist);
     73     return CURL_TRAILERFUNC_ABORT;
     74   }
     75 }
     76 
     77 static CURLcode test_lib1591(char *URL)
     78 {
     79   CURL *curl = NULL;
     80   CURLcode res = CURLE_FAILED_INIT;
     81   /* http and proxy header list */
     82   struct curl_slist *hhl = NULL;
     83 
     84   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
     85     curl_mfprintf(stderr, "curl_global_init() failed\n");
     86     return TEST_ERR_MAJOR_BAD;
     87   }
     88 
     89   curl = curl_easy_init();
     90   if(!curl) {
     91     curl_mfprintf(stderr, "curl_easy_init() failed\n");
     92     curl_global_cleanup();
     93     return TEST_ERR_MAJOR_BAD;
     94   }
     95 
     96   hhl = curl_slist_append(hhl, "Trailer: my-super-awesome-trailer,"
     97                                " my-other-awesome-trailer");
     98   if(!hhl) {
     99     goto test_cleanup;
    100   }
    101 
    102   test_setopt(curl, CURLOPT_URL, URL);
    103   test_setopt(curl, CURLOPT_HTTPHEADER, hhl);
    104   test_setopt(curl, CURLOPT_UPLOAD, 1L);
    105   test_setopt(curl, CURLOPT_READFUNCTION, t1591_read_cb);
    106   test_setopt(curl, CURLOPT_TRAILERFUNCTION, t1591_trailers_callback);
    107   test_setopt(curl, CURLOPT_TRAILERDATA, NULL);
    108 
    109   res = curl_easy_perform(curl);
    110 
    111 test_cleanup:
    112 
    113   curl_easy_cleanup(curl);
    114 
    115   curl_slist_free_all(hhl);
    116 
    117   curl_global_cleanup();
    118 
    119   return res;
    120 }