quickjs-tart

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

lib650.c (6697B)


      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 /* This test attempts to use all form API features that are not
     29  * used elsewhere.
     30  */
     31 
     32 /* curl_formget callback to count characters. */
     33 static size_t count_chars(void *userp, const char *buf, size_t len)
     34 {
     35   size_t *pcounter = (size_t *) userp;
     36 
     37   (void) buf;
     38   *pcounter += len;
     39   return len;
     40 }
     41 
     42 static CURLcode test_lib650(char *URL)
     43 {
     44   CURL *curl = NULL;
     45   CURLcode res = TEST_ERR_MAJOR_BAD;
     46   CURLFORMcode formrc;
     47   struct curl_slist *headers, *headers2 = NULL;
     48   struct curl_httppost *formpost = NULL;
     49   struct curl_httppost *lastptr = NULL;
     50   struct curl_forms formarray[3];
     51   size_t formlength = 0;
     52   char flbuf[32];
     53   long contentlength = 0;
     54 
     55   static const char testname[] = "fieldname";
     56   static char testdata[] =
     57     "this is what we post to the silly web server";
     58 
     59   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
     60     curl_mfprintf(stderr, "curl_global_init() failed\n");
     61     return TEST_ERR_MAJOR_BAD;
     62   }
     63 
     64   /* Check proper name and data copying, as well as headers. */
     65   headers = curl_slist_append(NULL, "X-customheader-1: Header 1 data");
     66   if(!headers) {
     67     goto test_cleanup;
     68   }
     69   headers2 = curl_slist_append(headers, "X-customheader-2: Header 2 data");
     70   if(!headers2) {
     71     goto test_cleanup;
     72   }
     73   headers = headers2;
     74   headers2 = curl_slist_append(headers, "Content-Type: text/plain");
     75   if(!headers2) {
     76     goto test_cleanup;
     77   }
     78   headers = headers2;
     79   formrc = curl_formadd(&formpost, &lastptr,
     80                         CURLFORM_COPYNAME, &testname,
     81                         CURLFORM_COPYCONTENTS, &testdata,
     82                         CURLFORM_CONTENTHEADER, headers,
     83                         CURLFORM_END);
     84   if(formrc) {
     85     curl_mprintf("curl_formadd(1) = %d\n", (int) formrc);
     86     goto test_cleanup;
     87   }
     88 
     89   contentlength = (long)(strlen(testdata) - 1);
     90 
     91   /* Use a form array for the non-copy test. */
     92   formarray[0].option = CURLFORM_PTRCONTENTS;
     93   formarray[0].value = testdata;
     94   formarray[1].option = CURLFORM_CONTENTSLENGTH;
     95   formarray[1].value = (char *)(size_t)contentlength;
     96   formarray[2].option = CURLFORM_END;
     97   formarray[2].value = NULL;
     98   formrc = curl_formadd(&formpost,
     99                         &lastptr,
    100                         CURLFORM_PTRNAME, testname,
    101                         CURLFORM_NAMELENGTH, strlen(testname) - 1,
    102                         CURLFORM_ARRAY, formarray,
    103                         CURLFORM_FILENAME, "remotefile.txt",
    104                         CURLFORM_END);
    105 
    106   if(formrc) {
    107     curl_mprintf("curl_formadd(2) = %d\n", (int) formrc);
    108     goto test_cleanup;
    109   }
    110 
    111   /* Now change in-memory data to affect CURLOPT_PTRCONTENTS value.
    112      Copied values (first field) must not be affected.
    113      CURLOPT_PTRNAME actually copies the name thus we do not test this here. */
    114   testdata[0]++;
    115 
    116   /* Check multi-files and content type propagation. */
    117   formrc = curl_formadd(&formpost,
    118                         &lastptr,
    119                         CURLFORM_COPYNAME, "multifile",
    120                         CURLFORM_FILE, libtest_arg2,    /* Set in first.c. */
    121                         CURLFORM_FILE, libtest_arg2,
    122                         CURLFORM_CONTENTTYPE, "text/whatever",
    123                         CURLFORM_FILE, libtest_arg2,
    124                         CURLFORM_END);
    125 
    126   if(formrc) {
    127     curl_mprintf("curl_formadd(3) = %d\n", (int) formrc);
    128     goto test_cleanup;
    129   }
    130 
    131   /* Check data from file content. */
    132   formrc = curl_formadd(&formpost,
    133                         &lastptr,
    134                         CURLFORM_COPYNAME, "filecontents",
    135                         CURLFORM_FILECONTENT, libtest_arg2,
    136                         CURLFORM_END);
    137   if(formrc) {
    138     curl_mprintf("curl_formadd(4) = %d\n", (int) formrc);
    139     goto test_cleanup;
    140   }
    141 
    142   /* Measure the current form length.
    143    * This is done before including stdin data because we want to reuse it
    144    * and stdin cannot be rewound.
    145    */
    146   curl_formget(formpost, (void *) &formlength, count_chars);
    147 
    148   /* Include length in data for external check. */
    149   curl_msnprintf(flbuf, sizeof(flbuf), "%lu", (unsigned long) formlength);
    150 
    151   formrc = curl_formadd(&formpost,
    152                         &lastptr,
    153                         CURLFORM_COPYNAME, "formlength",
    154                         CURLFORM_COPYCONTENTS, &flbuf,
    155                         CURLFORM_END);
    156 
    157   if(formrc) {
    158     curl_mprintf("curl_formadd(5) = %d\n", (int) formrc);
    159     goto test_cleanup;
    160   }
    161 
    162   /* Check stdin (may be problematic on some platforms). */
    163   formrc = curl_formadd(&formpost,
    164                         &lastptr,
    165                         CURLFORM_COPYNAME, "standardinput",
    166                         CURLFORM_FILE, "-",
    167                         CURLFORM_END);
    168 
    169   if(formrc) {
    170     curl_mprintf("curl_formadd(6) = %d\n", (int) formrc);
    171     goto test_cleanup;
    172   }
    173 
    174   curl = curl_easy_init();
    175   if(!curl) {
    176     curl_mfprintf(stderr, "curl_easy_init() failed\n");
    177     goto test_cleanup;
    178   }
    179 
    180   /* First set the URL that is about to receive our POST. */
    181   test_setopt(curl, CURLOPT_URL, URL);
    182 
    183   /* send a multi-part formpost */
    184   test_setopt(curl, CURLOPT_HTTPPOST, formpost);
    185 
    186   /* get verbose debug output please */
    187   test_setopt(curl, CURLOPT_VERBOSE, 1L);
    188 
    189   test_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    190   test_setopt(curl, CURLOPT_POSTREDIR, (long)CURL_REDIR_POST_301);
    191 
    192   /* include headers in the output */
    193   test_setopt(curl, CURLOPT_HEADER, 1L);
    194 
    195   /* Perform the request, res will get the return code */
    196   res = curl_easy_perform(curl);
    197 
    198 test_cleanup:
    199 
    200   /* always cleanup */
    201   curl_easy_cleanup(curl);
    202 
    203   /* now cleanup the formpost chain */
    204   curl_formfree(formpost);
    205   curl_slist_free_all(headers);
    206 
    207   curl_global_cleanup();
    208 
    209   return res;
    210 }