lib586.c 6.1 KB

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