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