xref: /curl/tests/libtest/lib651.c (revision 71cf0d1f)
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 "test.h"
25 
26 #include "memdebug.h"
27 
28 static char testbuf[17000]; /* more than 16K */
29 
test(char * URL)30 CURLcode test(char *URL)
31 {
32   CURL *curl;
33   CURLcode res = CURLE_OK;
34   CURLFORMcode formrc;
35   struct curl_httppost *formpost = NULL;
36   struct curl_httppost *lastptr = NULL;
37 
38   /* create a buffer with AAAA...BBBBB...CCCC...etc */
39   int i;
40   int size = (int)sizeof(testbuf)/1000;
41 
42   for(i = 0; i < size ; i++)
43     memset(&testbuf[i * 1000], 65 + i, 1000);
44 
45   testbuf[sizeof(testbuf)-1] = 0; /* null-terminate */
46 
47   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
48     fprintf(stderr, "curl_global_init() failed\n");
49     return TEST_ERR_MAJOR_BAD;
50   }
51 
52   CURL_IGNORE_DEPRECATION(
53     /* Check proper name and data copying. */
54     formrc = curl_formadd(&formpost, &lastptr,
55                           CURLFORM_COPYNAME, "hello",
56                           CURLFORM_COPYCONTENTS, testbuf,
57                           CURLFORM_END);
58   )
59   if(formrc)
60     printf("curl_formadd(1) = %d\n", (int) formrc);
61 
62 
63   curl = curl_easy_init();
64   if(!curl) {
65     fprintf(stderr, "curl_easy_init() failed\n");
66     CURL_IGNORE_DEPRECATION(
67       curl_formfree(formpost);
68     )
69     curl_global_cleanup();
70     return TEST_ERR_MAJOR_BAD;
71   }
72 
73   /* First set the URL that is about to receive our POST. */
74   test_setopt(curl, CURLOPT_URL, URL);
75 
76   CURL_IGNORE_DEPRECATION(
77     /* send a multi-part formpost */
78     test_setopt(curl, CURLOPT_HTTPPOST, formpost);
79   )
80 
81   /* get verbose debug output please */
82   test_setopt(curl, CURLOPT_VERBOSE, 1L);
83 
84   /* include headers in the output */
85   test_setopt(curl, CURLOPT_HEADER, 1L);
86 
87   /* Perform the request, res will get the return code */
88   res = curl_easy_perform(curl);
89 
90 test_cleanup:
91 
92   /* always cleanup */
93   curl_easy_cleanup(curl);
94 
95   CURL_IGNORE_DEPRECATION(
96     /* now cleanup the formpost chain */
97     curl_formfree(formpost);
98   )
99 
100   curl_global_cleanup();
101 
102   return res;
103 }
104