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