xref: /curl/tests/libtest/lib668.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 testdata[]= "dummy";
29 
30 struct WriteThis {
31   char *readptr;
32   curl_off_t sizeleft;
33 };
34 
read_callback(char * ptr,size_t size,size_t nmemb,void * userp)35 static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
36 {
37   struct WriteThis *pooh = (struct WriteThis *)userp;
38   size_t len = strlen(pooh->readptr);
39 
40   (void) size; /* Always 1.*/
41 
42   if(len > nmemb)
43     len = nmemb;
44   if(len) {
45     memcpy(ptr, pooh->readptr, len);
46     pooh->readptr += len;
47   }
48   return len;
49 }
50 
test(char * URL)51 CURLcode test(char *URL)
52 {
53   CURL *easy = NULL;
54   curl_mime *mime = NULL;
55   curl_mimepart *part;
56   CURLcode res = TEST_ERR_FAILURE;
57   struct WriteThis pooh1, pooh2;
58 
59   /*
60    * Check early end of part data detection.
61    */
62 
63   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
64     fprintf(stderr, "curl_global_init() failed\n");
65     return TEST_ERR_MAJOR_BAD;
66   }
67 
68   easy = curl_easy_init();
69 
70   /* First set the URL that is about to receive our POST. */
71   test_setopt(easy, CURLOPT_URL, URL);
72 
73   /* get verbose debug output please */
74   test_setopt(easy, CURLOPT_VERBOSE, 1L);
75 
76   /* include headers in the output */
77   test_setopt(easy, CURLOPT_HEADER, 1L);
78 
79   /* Prepare the callback structures. */
80   pooh1.readptr = testdata;
81   pooh1.sizeleft = (curl_off_t) strlen(testdata);
82   pooh2 = pooh1;
83 
84   /* Build the mime tree. */
85   mime = curl_mime_init(easy);
86   part = curl_mime_addpart(mime);
87   curl_mime_name(part, "field1");
88   /* Early end of data detection can be done because the data size is known. */
89   curl_mime_data_cb(part, (curl_off_t) strlen(testdata),
90                     read_callback, NULL, NULL, &pooh1);
91   part = curl_mime_addpart(mime);
92   curl_mime_name(part, "field2");
93   /* Using an undefined length forces chunked transfer and disables early
94      end of data detection for this part. */
95   curl_mime_data_cb(part, (curl_off_t) -1, read_callback, NULL, NULL, &pooh2);
96   part = curl_mime_addpart(mime);
97   curl_mime_name(part, "field3");
98   /* Regular file part sources early end of data can be detected because
99      the file size is known. In addition, and EOF test is performed. */
100   curl_mime_filedata(part, libtest_arg2);
101 
102   /* Bind mime data to its easy handle. */
103   test_setopt(easy, CURLOPT_MIMEPOST, mime);
104 
105   /* Send data. */
106   res = curl_easy_perform(easy);
107   if(res != CURLE_OK) {
108     fprintf(stderr, "curl_easy_perform() failed\n");
109   }
110 
111 test_cleanup:
112   curl_easy_cleanup(easy);
113   curl_mime_free(mime);
114   curl_global_cleanup();
115   return res;
116 }
117