lib506.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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 const char *HOSTHEADER = "Host: www.host.foo.com";
  25. static const char *JAR = "log/jar506";
  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. static int locks[3];
  37. /* lock callback */
  38. static void my_lock(CURL *handle, curl_lock_data data,
  39. curl_lock_access laccess, void *useptr)
  40. {
  41. const char *what;
  42. struct userdata *user = (struct userdata *)useptr;
  43. int locknum;
  44. (void)handle;
  45. (void)laccess;
  46. switch(data) {
  47. case CURL_LOCK_DATA_SHARE:
  48. what = "share";
  49. locknum = 0;
  50. break;
  51. case CURL_LOCK_DATA_DNS:
  52. what = "dns";
  53. locknum = 1;
  54. break;
  55. case CURL_LOCK_DATA_COOKIE:
  56. what = "cookie";
  57. locknum = 2;
  58. break;
  59. default:
  60. fprintf(stderr, "lock: no such data: %d\n", (int)data);
  61. return;
  62. }
  63. /* detect locking of locked locks */
  64. if(locks[locknum]) {
  65. printf("lock: double locked %s\n", what);
  66. return;
  67. }
  68. locks[locknum]++;
  69. printf("lock: %-6s [%s]: %d\n", what, user->text, user->counter);
  70. user->counter++;
  71. }
  72. /* unlock callback */
  73. static void my_unlock(CURL *handle, curl_lock_data data, void *useptr)
  74. {
  75. const char *what;
  76. struct userdata *user = (struct userdata *)useptr;
  77. int locknum;
  78. (void)handle;
  79. switch(data) {
  80. case CURL_LOCK_DATA_SHARE:
  81. what = "share";
  82. locknum = 0;
  83. break;
  84. case CURL_LOCK_DATA_DNS:
  85. what = "dns";
  86. locknum = 1;
  87. break;
  88. case CURL_LOCK_DATA_COOKIE:
  89. what = "cookie";
  90. locknum = 2;
  91. break;
  92. default:
  93. fprintf(stderr, "unlock: no such data: %d\n", (int)data);
  94. return;
  95. }
  96. /* detect unlocking of unlocked locks */
  97. if(!locks[locknum]) {
  98. printf("unlock: double unlocked %s\n", what);
  99. return;
  100. }
  101. locks[locknum]--;
  102. printf("unlock: %-6s [%s]: %d\n", what, user->text, user->counter);
  103. user->counter++;
  104. }
  105. /* build host entry */
  106. static struct curl_slist *sethost(struct curl_slist *headers)
  107. {
  108. (void)headers;
  109. return curl_slist_append(NULL, HOSTHEADER);
  110. }
  111. /* the dummy thread function */
  112. static void *fire(void *ptr)
  113. {
  114. CURLcode code;
  115. struct curl_slist *headers;
  116. struct Tdata *tdata = (struct Tdata*)ptr;
  117. CURL *curl;
  118. curl = curl_easy_init();
  119. if(!curl) {
  120. fprintf(stderr, "curl_easy_init() failed\n");
  121. return NULL;
  122. }
  123. headers = sethost(NULL);
  124. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  125. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  126. curl_easy_setopt(curl, CURLOPT_URL, tdata->url);
  127. printf("CURLOPT_SHARE\n");
  128. curl_easy_setopt(curl, CURLOPT_SHARE, tdata->share);
  129. printf("PERFORM\n");
  130. code = curl_easy_perform(curl);
  131. if(code) {
  132. int i = 0;
  133. fprintf(stderr, "perform url '%s' repeat %d failed, curlcode %d\n",
  134. tdata->url, i, (int)code);
  135. }
  136. printf("CLEANUP\n");
  137. curl_easy_cleanup(curl);
  138. curl_slist_free_all(headers);
  139. return NULL;
  140. }
  141. /* build request url */
  142. static char *suburl(const char *base, int i)
  143. {
  144. return curl_maprintf("%s%.4d", base, i);
  145. }
  146. /* test function */
  147. int test(char *URL)
  148. {
  149. int res;
  150. CURLSHcode scode = CURLSHE_OK;
  151. CURLcode code = CURLE_OK;
  152. char *url = NULL;
  153. struct Tdata tdata;
  154. CURL *curl;
  155. CURLSH *share;
  156. struct curl_slist *headers = NULL;
  157. struct curl_slist *cookies = NULL;
  158. struct curl_slist *next_cookie = NULL;
  159. int i;
  160. struct userdata user;
  161. user.text = "Pigs in space";
  162. user.counter = 0;
  163. printf("GLOBAL_INIT\n");
  164. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  165. fprintf(stderr, "curl_global_init() failed\n");
  166. return TEST_ERR_MAJOR_BAD;
  167. }
  168. /* prepare share */
  169. printf("SHARE_INIT\n");
  170. share = curl_share_init();
  171. if(!share) {
  172. fprintf(stderr, "curl_share_init() failed\n");
  173. curl_global_cleanup();
  174. return TEST_ERR_MAJOR_BAD;
  175. }
  176. if(CURLSHE_OK == scode) {
  177. printf("CURLSHOPT_LOCKFUNC\n");
  178. scode = curl_share_setopt(share, CURLSHOPT_LOCKFUNC, my_lock);
  179. }
  180. if(CURLSHE_OK == scode) {
  181. printf("CURLSHOPT_UNLOCKFUNC\n");
  182. scode = curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, my_unlock);
  183. }
  184. if(CURLSHE_OK == scode) {
  185. printf("CURLSHOPT_USERDATA\n");
  186. scode = curl_share_setopt(share, CURLSHOPT_USERDATA, &user);
  187. }
  188. if(CURLSHE_OK == scode) {
  189. printf("CURL_LOCK_DATA_COOKIE\n");
  190. scode = curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
  191. }
  192. if(CURLSHE_OK == scode) {
  193. printf("CURL_LOCK_DATA_DNS\n");
  194. scode = curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
  195. }
  196. if(CURLSHE_OK != scode) {
  197. fprintf(stderr, "curl_share_setopt() failed\n");
  198. curl_share_cleanup(share);
  199. curl_global_cleanup();
  200. return TEST_ERR_MAJOR_BAD;
  201. }
  202. /* initial cookie manipulation */
  203. curl = curl_easy_init();
  204. if(!curl) {
  205. fprintf(stderr, "curl_easy_init() failed\n");
  206. curl_share_cleanup(share);
  207. curl_global_cleanup();
  208. return TEST_ERR_MAJOR_BAD;
  209. }
  210. printf("CURLOPT_SHARE\n");
  211. test_setopt(curl, CURLOPT_SHARE, share);
  212. printf("CURLOPT_COOKIELIST injected_and_clobbered\n");
  213. test_setopt(curl, CURLOPT_COOKIELIST,
  214. "Set-Cookie: injected_and_clobbered=yes; "
  215. "domain=host.foo.com; expires=Sat Feb 2 11:56:27 GMT 2030");
  216. printf("CURLOPT_COOKIELIST ALL\n");
  217. test_setopt(curl, CURLOPT_COOKIELIST, "ALL");
  218. printf("CURLOPT_COOKIELIST session\n");
  219. test_setopt(curl, CURLOPT_COOKIELIST, "Set-Cookie: session=elephants");
  220. printf("CURLOPT_COOKIELIST injected\n");
  221. test_setopt(curl, CURLOPT_COOKIELIST,
  222. "Set-Cookie: injected=yes; domain=host.foo.com; "
  223. "expires=Sat Feb 2 11:56:27 GMT 2030");
  224. printf("CURLOPT_COOKIELIST SESS\n");
  225. test_setopt(curl, CURLOPT_COOKIELIST, "SESS");
  226. printf("CLEANUP\n");
  227. curl_easy_cleanup(curl);
  228. res = 0;
  229. /* start treads */
  230. for(i = 1; i <= THREADS; i++) {
  231. /* set thread data */
  232. tdata.url = suburl(URL, i); /* must be curl_free()d */
  233. tdata.share = share;
  234. /* simulate thread, direct call of "thread" function */
  235. printf("*** run %d\n",i);
  236. fire(&tdata);
  237. curl_free(tdata.url);
  238. }
  239. /* fetch a another one and save cookies */
  240. printf("*** run %d\n", i);
  241. curl = curl_easy_init();
  242. if(!curl) {
  243. fprintf(stderr, "curl_easy_init() failed\n");
  244. curl_share_cleanup(share);
  245. curl_global_cleanup();
  246. return TEST_ERR_MAJOR_BAD;
  247. }
  248. url = suburl(URL, i);
  249. headers = sethost(NULL);
  250. test_setopt(curl, CURLOPT_HTTPHEADER, headers);
  251. test_setopt(curl, CURLOPT_URL, url);
  252. printf("CURLOPT_SHARE\n");
  253. test_setopt(curl, CURLOPT_SHARE, share);
  254. printf("CURLOPT_COOKIEJAR\n");
  255. test_setopt(curl, CURLOPT_COOKIEJAR, JAR);
  256. printf("CURLOPT_COOKIELIST FLUSH\n");
  257. test_setopt(curl, CURLOPT_COOKIELIST, "FLUSH");
  258. printf("PERFORM\n");
  259. curl_easy_perform(curl);
  260. printf("CLEANUP\n");
  261. curl_easy_cleanup(curl);
  262. curl_free(url);
  263. curl_slist_free_all(headers);
  264. /* load cookies */
  265. curl = curl_easy_init();
  266. if(!curl) {
  267. fprintf(stderr, "curl_easy_init() failed\n");
  268. curl_share_cleanup(share);
  269. curl_global_cleanup();
  270. return TEST_ERR_MAJOR_BAD;
  271. }
  272. url = suburl(URL, i);
  273. headers = sethost(NULL);
  274. test_setopt(curl, CURLOPT_HTTPHEADER, headers);
  275. test_setopt(curl, CURLOPT_URL, url);
  276. printf("CURLOPT_SHARE\n");
  277. test_setopt(curl, CURLOPT_SHARE, share);
  278. printf("CURLOPT_COOKIELIST ALL\n");
  279. test_setopt(curl, CURLOPT_COOKIELIST, "ALL");
  280. printf("CURLOPT_COOKIEJAR\n");
  281. test_setopt(curl, CURLOPT_COOKIEFILE, JAR);
  282. printf("CURLOPT_COOKIELIST RELOAD\n");
  283. test_setopt(curl, CURLOPT_COOKIELIST, "RELOAD");
  284. code = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
  285. if(code != CURLE_OK) {
  286. fprintf(stderr, "curl_easy_getinfo() failed\n");
  287. res = TEST_ERR_MAJOR_BAD;
  288. goto test_cleanup;
  289. }
  290. printf("loaded cookies:\n");
  291. if(!cookies) {
  292. fprintf(stderr, " reloading cookies from '%s' failed\n", JAR);
  293. res = TEST_ERR_MAJOR_BAD;
  294. goto test_cleanup;
  295. }
  296. printf("-----------------\n");
  297. next_cookie = cookies;
  298. while(next_cookie) {
  299. printf(" %s\n", next_cookie->data);
  300. next_cookie = next_cookie->next;
  301. }
  302. printf("-----------------\n");
  303. curl_slist_free_all(cookies);
  304. /* try to free share, expect to fail because share is in use*/
  305. printf("try SHARE_CLEANUP...\n");
  306. scode = curl_share_cleanup(share);
  307. if(scode == CURLSHE_OK) {
  308. fprintf(stderr, "curl_share_cleanup succeed but error expected\n");
  309. share = NULL;
  310. }
  311. else {
  312. printf("SHARE_CLEANUP failed, correct\n");
  313. }
  314. test_cleanup:
  315. /* clean up last handle */
  316. printf("CLEANUP\n");
  317. curl_easy_cleanup(curl);
  318. curl_slist_free_all(headers);
  319. curl_free(url);
  320. /* free share */
  321. printf("SHARE_CLEANUP\n");
  322. scode = curl_share_cleanup(share);
  323. if(scode != CURLSHE_OK)
  324. fprintf(stderr, "curl_share_cleanup failed, code errno %d\n",
  325. (int)scode);
  326. printf("GLOBAL_CLEANUP\n");
  327. curl_global_cleanup();
  328. return res;
  329. }