1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, 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 ***************************************************************************/
22 #include "test.h"
23
24 #include "memdebug.h"
25
26 static char data[]=
27 #ifdef CURL_DOES_CONVERSIONS
28 /* ASCII representation with escape sequences for non-ASCII platforms */
29 "\x64\x75\x6d\x6d\x79\x0a";
30 #else
31 "dummy\n";
32 #endif
33
34 struct WriteThis {
35 char *readptr;
36 curl_off_t sizeleft;
37 int freecount;
38 };
39
free_callback(void * userp)40 static void free_callback(void *userp)
41 {
42 struct WriteThis *pooh = (struct WriteThis *) userp;
43
44 pooh->freecount++;
45 }
46
read_callback(char * ptr,size_t size,size_t nmemb,void * userp)47 static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp)
48 {
49 struct WriteThis *pooh = (struct WriteThis *)userp;
50 int eof = !*pooh->readptr;
51
52 if(size*nmemb < 1)
53 return 0;
54
55 eof = pooh->sizeleft <= 0;
56 if(!eof)
57 pooh->sizeleft--;
58
59 if(!eof) {
60 *ptr = *pooh->readptr; /* copy one single byte */
61 pooh->readptr++; /* advance pointer */
62 return 1; /* we return 1 byte at a time! */
63 }
64
65 return 0; /* no more data left to deliver */
66 }
67
test(char * URL)68 int test(char *URL)
69 {
70 CURL *easy = NULL;
71 CURL *easy2 = NULL;
72 curl_mime *mime = NULL;
73 curl_mimepart *part;
74 struct curl_slist *hdrs = NULL;
75 CURLcode result;
76 int res = TEST_ERR_FAILURE;
77 struct WriteThis pooh;
78
79 /*
80 * Check proper copy/release of mime post data bound to a duplicated
81 * easy handle.
82 */
83
84 if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
85 fprintf(stderr, "curl_global_init() failed\n");
86 return TEST_ERR_MAJOR_BAD;
87 }
88
89 easy = curl_easy_init();
90
91 /* First set the URL that is about to receive our POST. */
92 test_setopt(easy, CURLOPT_URL, URL);
93
94 /* get verbose debug output please */
95 test_setopt(easy, CURLOPT_VERBOSE, 1L);
96
97 /* include headers in the output */
98 test_setopt(easy, CURLOPT_HEADER, 1L);
99
100 /* Prepare the callback structure. */
101 pooh.readptr = data;
102 pooh.sizeleft = (curl_off_t) strlen(data);
103 pooh.freecount = 0;
104
105 /* Build the mime tree. */
106 mime = curl_mime_init(easy);
107 part = curl_mime_addpart(mime);
108 curl_mime_data(part, "hello", CURL_ZERO_TERMINATED);
109 curl_mime_name(part, "greeting");
110 curl_mime_type(part, "application/X-Greeting");
111 curl_mime_encoder(part, "base64");
112 hdrs = curl_slist_append(hdrs, "X-Test-Number: 654");
113 curl_mime_headers(part, hdrs, TRUE);
114 part = curl_mime_addpart(mime);
115 curl_mime_filedata(part, "log/file654.txt");
116 part = curl_mime_addpart(mime);
117 curl_mime_data_cb(part, (curl_off_t) -1, read_callback, NULL, free_callback,
118 &pooh);
119
120 /* Bind mime data to its easy handle. */
121 test_setopt(easy, CURLOPT_MIMEPOST, mime);
122
123 /* Duplicate the handle. */
124 easy2 = curl_easy_duphandle(easy);
125 if(!easy2) {
126 fprintf(stderr, "curl_easy_duphandle() failed\n");
127 res = TEST_ERR_FAILURE;
128 goto test_cleanup;
129 }
130
131 /* Now free the mime structure: it should unbind it from the first
132 easy handle. */
133 curl_mime_free(mime);
134 mime = NULL; /* Already cleaned up. */
135
136 /* Perform on the first handle: should not send any data. */
137 result = curl_easy_perform(easy);
138 if(result) {
139 fprintf(stderr, "curl_easy_perform(original) failed\n");
140 res = (int) result;
141 goto test_cleanup;
142 }
143
144 /* Perform on the second handle: if the bound mime structure has not been
145 duplicated properly, it should cause a valgrind error. */
146 result = curl_easy_perform(easy2);
147 if(result) {
148 fprintf(stderr, "curl_easy_perform(duplicated) failed\n");
149 res = (int) result;
150 goto test_cleanup;
151 }
152
153 /* Free the duplicated handle: it should call free_callback again.
154 If the mime copy was bad or not automatically released, valgrind
155 will signal it. */
156 curl_easy_cleanup(easy2);
157 easy2 = NULL; /* Already cleaned up. */
158
159 if(pooh.freecount != 2) {
160 fprintf(stderr, "free_callback() called %d times instead of 2\n",
161 pooh.freecount);
162 res = TEST_ERR_FAILURE;
163 goto test_cleanup;
164 }
165
166 test_cleanup:
167 curl_easy_cleanup(easy);
168 curl_easy_cleanup(easy2);
169 curl_mime_free(mime);
170 curl_global_cleanup();
171 return res;
172 }
173