2
0

lib586.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #define THREADS 2
  27. /* struct containing data of a thread */
  28. struct Tdata {
  29. CURLSH *share;
  30. char *url;
  31. };
  32. struct userdata {
  33. const char *text;
  34. int counter;
  35. };
  36. /* lock callback */
  37. static void my_lock(CURL *handle, curl_lock_data data,
  38. curl_lock_access laccess, void *useptr)
  39. {
  40. const char *what;
  41. struct userdata *user = (struct userdata *)useptr;
  42. (void)handle;
  43. (void)laccess;
  44. switch(data) {
  45. case CURL_LOCK_DATA_SHARE:
  46. what = "share";
  47. break;
  48. case CURL_LOCK_DATA_DNS:
  49. what = "dns";
  50. break;
  51. case CURL_LOCK_DATA_COOKIE:
  52. what = "cookie";
  53. break;
  54. case CURL_LOCK_DATA_SSL_SESSION:
  55. what = "ssl_session";
  56. break;
  57. default:
  58. fprintf(stderr, "lock: no such data: %d\n", (int)data);
  59. return;
  60. }
  61. printf("lock: %-6s [%s]: %d\n", what, user->text, user->counter);
  62. user->counter++;
  63. }
  64. /* unlock callback */
  65. static void my_unlock(CURL *handle, curl_lock_data data, void *useptr)
  66. {
  67. const char *what;
  68. struct userdata *user = (struct userdata *)useptr;
  69. (void)handle;
  70. switch(data) {
  71. case CURL_LOCK_DATA_SHARE:
  72. what = "share";
  73. break;
  74. case CURL_LOCK_DATA_DNS:
  75. what = "dns";
  76. break;
  77. case CURL_LOCK_DATA_COOKIE:
  78. what = "cookie";
  79. break;
  80. case CURL_LOCK_DATA_SSL_SESSION:
  81. what = "ssl_session";
  82. break;
  83. default:
  84. fprintf(stderr, "unlock: no such data: %d\n", (int)data);
  85. return;
  86. }
  87. printf("unlock: %-6s [%s]: %d\n", what, user->text, user->counter);
  88. user->counter++;
  89. }
  90. /* the dummy thread function */
  91. static void *fire(void *ptr)
  92. {
  93. CURLcode code;
  94. struct Tdata *tdata = (struct Tdata*)ptr;
  95. CURL *curl;
  96. curl = curl_easy_init();
  97. if(!curl) {
  98. fprintf(stderr, "curl_easy_init() failed\n");
  99. return NULL;
  100. }
  101. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  102. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  103. curl_easy_setopt(curl, CURLOPT_URL, tdata->url);
  104. printf("CURLOPT_SHARE\n");
  105. curl_easy_setopt(curl, CURLOPT_SHARE, tdata->share);
  106. printf("PERFORM\n");
  107. code = curl_easy_perform(curl);
  108. if(code != CURLE_OK) {
  109. int i = 0;
  110. fprintf(stderr, "perform url '%s' repeat %d failed, curlcode %d\n",
  111. tdata->url, i, (int)code);
  112. }
  113. printf("CLEANUP\n");
  114. curl_easy_cleanup(curl);
  115. return NULL;
  116. }
  117. /* test function */
  118. CURLcode test(char *URL)
  119. {
  120. CURLcode res = CURLE_OK;
  121. CURLSHcode scode = CURLSHE_OK;
  122. char *url;
  123. struct Tdata tdata;
  124. CURL *curl;
  125. CURLSH *share;
  126. int i;
  127. struct userdata user;
  128. user.text = "Pigs in space";
  129. user.counter = 0;
  130. printf("GLOBAL_INIT\n");
  131. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  132. fprintf(stderr, "curl_global_init() failed\n");
  133. return TEST_ERR_MAJOR_BAD;
  134. }
  135. /* prepare share */
  136. printf("SHARE_INIT\n");
  137. share = curl_share_init();
  138. if(!share) {
  139. fprintf(stderr, "curl_share_init() failed\n");
  140. curl_global_cleanup();
  141. return TEST_ERR_MAJOR_BAD;
  142. }
  143. if(CURLSHE_OK == scode) {
  144. printf("CURLSHOPT_LOCKFUNC\n");
  145. scode = curl_share_setopt(share, CURLSHOPT_LOCKFUNC, my_lock);
  146. }
  147. if(CURLSHE_OK == scode) {
  148. printf("CURLSHOPT_UNLOCKFUNC\n");
  149. scode = curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, my_unlock);
  150. }
  151. if(CURLSHE_OK == scode) {
  152. printf("CURLSHOPT_USERDATA\n");
  153. scode = curl_share_setopt(share, CURLSHOPT_USERDATA, &user);
  154. }
  155. if(CURLSHE_OK == scode) {
  156. printf("CURL_LOCK_DATA_SSL_SESSION\n");
  157. scode = curl_share_setopt(share, CURLSHOPT_SHARE,
  158. CURL_LOCK_DATA_SSL_SESSION);
  159. }
  160. if(CURLSHE_OK != scode) {
  161. fprintf(stderr, "curl_share_setopt() failed\n");
  162. curl_share_cleanup(share);
  163. curl_global_cleanup();
  164. return TEST_ERR_MAJOR_BAD;
  165. }
  166. /* start treads */
  167. for(i = 1; i <= THREADS; i++) {
  168. /* set thread data */
  169. tdata.url = URL;
  170. tdata.share = share;
  171. /* simulate thread, direct call of "thread" function */
  172. printf("*** run %d\n",i);
  173. fire(&tdata);
  174. }
  175. /* fetch another one */
  176. printf("*** run %d\n", i);
  177. curl = curl_easy_init();
  178. if(!curl) {
  179. fprintf(stderr, "curl_easy_init() failed\n");
  180. curl_share_cleanup(share);
  181. curl_global_cleanup();
  182. return TEST_ERR_MAJOR_BAD;
  183. }
  184. url = URL;
  185. test_setopt(curl, CURLOPT_URL, url);
  186. printf("CURLOPT_SHARE\n");
  187. test_setopt(curl, CURLOPT_SHARE, share);
  188. printf("PERFORM\n");
  189. res = curl_easy_perform(curl);
  190. /* try to free share, expect to fail because share is in use */
  191. printf("try SHARE_CLEANUP...\n");
  192. scode = curl_share_cleanup(share);
  193. if(scode == CURLSHE_OK) {
  194. fprintf(stderr, "curl_share_cleanup succeed but error expected\n");
  195. share = NULL;
  196. }
  197. else {
  198. printf("SHARE_CLEANUP failed, correct\n");
  199. }
  200. test_cleanup:
  201. /* clean up last handle */
  202. printf("CLEANUP\n");
  203. curl_easy_cleanup(curl);
  204. /* free share */
  205. printf("SHARE_CLEANUP\n");
  206. scode = curl_share_cleanup(share);
  207. if(scode != CURLSHE_OK)
  208. fprintf(stderr, "curl_share_cleanup failed, code errno %d\n",
  209. (int)scode);
  210. printf("GLOBAL_CLEANUP\n");
  211. curl_global_cleanup();
  212. return res;
  213. }