lib654.c 4.8 KB

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