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