Home | History | Annotate | Download | only in libtest
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2016, 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   if((curl = curl_easy_init()) == NULL) {
     71     fprintf(stderr, "curl_easy_init() failed\n");
     72     curl_global_cleanup();
     73     return TEST_ERR_MAJOR_BAD;
     74   }
     75 
     76   for(i = 0; i < NUM_HEADERS; i++) {
     77     int len = snprintf(buf, sizeof(buf), "Header%d: ", i);
     78     memset(&buf[len], 'A', SIZE_HEADERS);
     79     buf[len + SIZE_HEADERS]=0; /* zero terminate */
     80     hl = curl_slist_append(headerlist,  buf);
     81     if(!hl)
     82       goto test_cleanup;
     83     headerlist = hl;
     84   }
     85 
     86   hl = curl_slist_append(headerlist, "Expect: ");
     87   if(!hl)
     88     goto test_cleanup;
     89   headerlist = hl;
     90 
     91   test_setopt(curl, CURLOPT_URL, URL);
     92   test_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
     93   test_setopt(curl, CURLOPT_POST, 1L);
     94 #ifdef CURL_DOES_CONVERSIONS
     95   /* Convert the POST data to ASCII */
     96   test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
     97 #endif
     98   test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)POSTLEN);
     99   test_setopt(curl, CURLOPT_VERBOSE, 1L);
    100   test_setopt(curl, CURLOPT_HEADER, 1L);
    101   test_setopt(curl, CURLOPT_READFUNCTION, myreadfunc);
    102 
    103   res = curl_easy_perform(curl);
    104 
    105 test_cleanup:
    106 
    107   curl_easy_cleanup(curl);
    108 
    109   curl_slist_free_all(headerlist);
    110 
    111   curl_global_cleanup();
    112 
    113   return (int)res;
    114 }
    115