lib654.c 4.7 KB

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