Home | History | Annotate | Download | only in libtest
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel (at) 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.haxx.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  ***************************************************************************/
     22 
     23 /* This test case and code is based on the bug recipe Joe Malicki provided for
     24  * bug report #1871269, fixed on Jan 14 2008 before the 7.18.0 release.
     25  */
     26 
     27 #include "test.h"
     28 
     29 #include "memdebug.h"
     30 
     31 #define POSTLEN 40960
     32 
     33 static size_t myreadfunc(void *ptr, size_t size, size_t nmemb, void *stream)
     34 {
     35   static size_t total = POSTLEN;
     36   static char buf[1024];
     37   (void)stream;
     38 
     39   memset(buf, 'A', sizeof(buf));
     40 
     41   size *= nmemb;
     42   if(size > total)
     43     size = total;
     44 
     45   if(size > sizeof(buf))
     46     size = sizeof(buf);
     47 
     48   memcpy(ptr, buf, size);
     49   total -= size;
     50   return size;
     51 }
     52 
     53 #define NUM_HEADERS 8
     54 #define SIZE_HEADERS 5000
     55 
     56 static char buf[SIZE_HEADERS + 100];
     57 
     58 int test(char *URL)
     59 {
     60   CURL *curl;
     61   CURLcode res = CURLE_FAILED_INIT;
     62   int i;
     63   struct curl_slist *headerlist = NULL, *hl;
     64 
     65   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
     66     fprintf(stderr, "curl_global_init() failed\n");
     67     return TEST_ERR_MAJOR_BAD;
     68   }
     69 
     70   curl = curl_easy_init();
     71   if(!curl) {
     72     fprintf(stderr, "curl_easy_init() failed\n");
     73     curl_global_cleanup();
     74     return TEST_ERR_MAJOR_BAD;
     75   }
     76 
     77   for(i = 0; i < NUM_HEADERS; i++) {
     78     int len = msnprintf(buf, sizeof(buf), "Header%d: ", i);
     79     memset(&buf[len], 'A', SIZE_HEADERS);
     80     buf[len + SIZE_HEADERS] = 0; /* zero terminate */
     81     hl = curl_slist_append(headerlist,  buf);
     82     if(!hl)
     83       goto test_cleanup;
     84     headerlist = hl;
     85   }
     86 
     87   hl = curl_slist_append(headerlist, "Expect: ");
     88   if(!hl)
     89     goto test_cleanup;
     90   headerlist = hl;
     91 
     92   test_setopt(curl, CURLOPT_URL, URL);
     93   test_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
     94   test_setopt(curl, CURLOPT_POST, 1L);
     95 #ifdef CURL_DOES_CONVERSIONS
     96   /* Convert the POST data to ASCII */
     97   test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
     98 #endif
     99   test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)POSTLEN);
    100   test_setopt(curl, CURLOPT_VERBOSE, 1L);
    101   test_setopt(curl, CURLOPT_HEADER, 1L);
    102   test_setopt(curl, CURLOPT_READFUNCTION, myreadfunc);
    103 
    104   res = curl_easy_perform(curl);
    105 
    106 test_cleanup:
    107 
    108   curl_easy_cleanup(curl);
    109 
    110   curl_slist_free_all(headerlist);
    111 
    112   curl_global_cleanup();
    113 
    114   return (int)res;
    115 }
    116