lib654.c 5.1 KB

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