lib654.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 testdata[]=
  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. CURLcode 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 res = TEST_ERR_FAILURE;
  62. struct WriteThis pooh;
  63. /*
  64. * Check proper copy/release of mime post data bound to a duplicated
  65. * easy handle.
  66. */
  67. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  68. fprintf(stderr, "curl_global_init() failed\n");
  69. return TEST_ERR_MAJOR_BAD;
  70. }
  71. easy = curl_easy_init();
  72. /* First set the URL that is about to receive our POST. */
  73. test_setopt(easy, CURLOPT_URL, URL);
  74. /* get verbose debug output please */
  75. test_setopt(easy, CURLOPT_VERBOSE, 1L);
  76. /* include headers in the output */
  77. test_setopt(easy, CURLOPT_HEADER, 1L);
  78. /* Prepare the callback structure. */
  79. pooh.readptr = testdata;
  80. pooh.sizeleft = (curl_off_t) strlen(testdata);
  81. pooh.freecount = 0;
  82. /* Build the mime tree. */
  83. mime = curl_mime_init(easy);
  84. part = curl_mime_addpart(mime);
  85. curl_mime_data(part, "hello", CURL_ZERO_TERMINATED);
  86. curl_mime_name(part, "greeting");
  87. curl_mime_type(part, "application/X-Greeting");
  88. curl_mime_encoder(part, "base64");
  89. hdrs = curl_slist_append(hdrs, "X-Test-Number: 654");
  90. curl_mime_headers(part, hdrs, TRUE);
  91. part = curl_mime_addpart(mime);
  92. curl_mime_filedata(part, libtest_arg2);
  93. part = curl_mime_addpart(mime);
  94. curl_mime_data_cb(part, (curl_off_t) -1, read_callback, NULL, free_callback,
  95. &pooh);
  96. /* Bind mime data to its easy handle. */
  97. test_setopt(easy, CURLOPT_MIMEPOST, mime);
  98. /* Duplicate the handle. */
  99. easy2 = curl_easy_duphandle(easy);
  100. if(!easy2) {
  101. fprintf(stderr, "curl_easy_duphandle() failed\n");
  102. res = TEST_ERR_FAILURE;
  103. goto test_cleanup;
  104. }
  105. /* Now free the mime structure: it should unbind it from the first
  106. easy handle. */
  107. curl_mime_free(mime);
  108. mime = NULL; /* Already cleaned up. */
  109. /* Perform on the first handle: should not send any data. */
  110. res = curl_easy_perform(easy);
  111. if(res != CURLE_OK) {
  112. fprintf(stderr, "curl_easy_perform(original) failed\n");
  113. goto test_cleanup;
  114. }
  115. /* Perform on the second handle: if the bound mime structure has not been
  116. duplicated properly, it should cause a valgrind error. */
  117. res = curl_easy_perform(easy2);
  118. if(res != CURLE_OK) {
  119. fprintf(stderr, "curl_easy_perform(duplicated) failed\n");
  120. goto test_cleanup;
  121. }
  122. /* Free the duplicated handle: it should call free_callback again.
  123. If the mime copy was bad or not automatically released, valgrind
  124. will signal it. */
  125. curl_easy_cleanup(easy2);
  126. easy2 = NULL; /* Already cleaned up. */
  127. if(pooh.freecount != 2) {
  128. fprintf(stderr, "free_callback() called %d times instead of 2\n",
  129. pooh.freecount);
  130. res = TEST_ERR_FAILURE;
  131. goto test_cleanup;
  132. }
  133. test_cleanup:
  134. curl_easy_cleanup(easy);
  135. curl_easy_cleanup(easy2);
  136. curl_mime_free(mime);
  137. curl_global_cleanup();
  138. return res;
  139. }