lib506.c 9.9 KB

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