lib654.c 4.9 KB

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