lib586.c 6.0 KB

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