lib506.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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.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. curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
  128. printf("CURLOPT_SHARE\n");
  129. curl_easy_setopt(curl, CURLOPT_SHARE, tdata->share);
  130. printf("PERFORM\n");
  131. code = curl_easy_perform(curl);
  132. if(code) {
  133. int i = 0;
  134. fprintf(stderr, "perform url '%s' repeat %d failed, curlcode %d\n",
  135. tdata->url, i, (int)code);
  136. }
  137. printf("CLEANUP\n");
  138. curl_easy_cleanup(curl);
  139. curl_slist_free_all(headers);
  140. return NULL;
  141. }
  142. /* build request url */
  143. static char *suburl(const char *base, int i)
  144. {
  145. return curl_maprintf("%s%.4d", base, i);
  146. }
  147. /* test function */
  148. int test(char *URL)
  149. {
  150. int res;
  151. CURLSHcode scode = CURLSHE_OK;
  152. CURLcode code = CURLE_OK;
  153. char *url = NULL;
  154. struct Tdata tdata;
  155. CURL *curl;
  156. CURLSH *share;
  157. struct curl_slist *headers = NULL;
  158. struct curl_slist *cookies = NULL;
  159. struct curl_slist *next_cookie = NULL;
  160. int i;
  161. struct userdata user;
  162. user.text = "Pigs in space";
  163. user.counter = 0;
  164. printf("GLOBAL_INIT\n");
  165. if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
  166. fprintf(stderr, "curl_global_init() failed\n");
  167. return TEST_ERR_MAJOR_BAD;
  168. }
  169. /* prepare share */
  170. printf("SHARE_INIT\n");
  171. share = curl_share_init();
  172. if(!share) {
  173. fprintf(stderr, "curl_share_init() failed\n");
  174. curl_global_cleanup();
  175. return TEST_ERR_MAJOR_BAD;
  176. }
  177. if(CURLSHE_OK == scode) {
  178. printf("CURLSHOPT_LOCKFUNC\n");
  179. scode = curl_share_setopt(share, CURLSHOPT_LOCKFUNC, my_lock);
  180. }
  181. if(CURLSHE_OK == scode) {
  182. printf("CURLSHOPT_UNLOCKFUNC\n");
  183. scode = curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, my_unlock);
  184. }
  185. if(CURLSHE_OK == scode) {
  186. printf("CURLSHOPT_USERDATA\n");
  187. scode = curl_share_setopt(share, CURLSHOPT_USERDATA, &user);
  188. }
  189. if(CURLSHE_OK == scode) {
  190. printf("CURL_LOCK_DATA_COOKIE\n");
  191. scode = curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
  192. }
  193. if(CURLSHE_OK == scode) {
  194. printf("CURL_LOCK_DATA_DNS\n");
  195. scode = curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
  196. }
  197. if(CURLSHE_OK != scode) {
  198. fprintf(stderr, "curl_share_setopt() failed\n");
  199. curl_share_cleanup(share);
  200. curl_global_cleanup();
  201. return TEST_ERR_MAJOR_BAD;
  202. }
  203. /* initial cookie manipulation */
  204. curl = curl_easy_init();
  205. if(!curl) {
  206. fprintf(stderr, "curl_easy_init() failed\n");
  207. curl_share_cleanup(share);
  208. curl_global_cleanup();
  209. return TEST_ERR_MAJOR_BAD;
  210. }
  211. printf("CURLOPT_SHARE\n");
  212. test_setopt(curl, CURLOPT_SHARE, share);
  213. printf("CURLOPT_COOKIELIST injected_and_clobbered\n");
  214. test_setopt(curl, CURLOPT_COOKIELIST,
  215. "Set-Cookie: injected_and_clobbered=yes; "
  216. "domain=host.foo.com; expires=Sat Feb 2 11:56:27 GMT 2030");
  217. printf("CURLOPT_COOKIELIST ALL\n");
  218. test_setopt(curl, CURLOPT_COOKIELIST, "ALL");
  219. printf("CURLOPT_COOKIELIST session\n");
  220. test_setopt(curl, CURLOPT_COOKIELIST, "Set-Cookie: session=elephants");
  221. printf("CURLOPT_COOKIELIST injected\n");
  222. test_setopt(curl, CURLOPT_COOKIELIST,
  223. "Set-Cookie: injected=yes; domain=host.foo.com; "
  224. "expires=Sat Feb 2 11:56:27 GMT 2030");
  225. printf("CURLOPT_COOKIELIST SESS\n");
  226. test_setopt(curl, CURLOPT_COOKIELIST, "SESS");
  227. printf("CLEANUP\n");
  228. curl_easy_cleanup(curl);
  229. res = 0;
  230. /* start treads */
  231. for(i = 1; i <= THREADS; i++) {
  232. /* set thread data */
  233. tdata.url = suburl(URL, i); /* must be curl_free()d */
  234. tdata.share = share;
  235. /* simulate thread, direct call of "thread" function */
  236. printf("*** run %d\n",i);
  237. fire(&tdata);
  238. curl_free(tdata.url);
  239. }
  240. /* fetch a another one and save cookies */
  241. printf("*** run %d\n", i);
  242. curl = curl_easy_init();
  243. if(!curl) {
  244. fprintf(stderr, "curl_easy_init() failed\n");
  245. curl_share_cleanup(share);
  246. curl_global_cleanup();
  247. return TEST_ERR_MAJOR_BAD;
  248. }
  249. url = suburl(URL, i);
  250. headers = sethost(NULL);
  251. test_setopt(curl, CURLOPT_HTTPHEADER, headers);
  252. test_setopt(curl, CURLOPT_URL, url);
  253. printf("CURLOPT_SHARE\n");
  254. test_setopt(curl, CURLOPT_SHARE, share);
  255. printf("CURLOPT_COOKIEJAR\n");
  256. test_setopt(curl, CURLOPT_COOKIEJAR, JAR);
  257. printf("CURLOPT_COOKIELIST FLUSH\n");
  258. test_setopt(curl, CURLOPT_COOKIELIST, "FLUSH");
  259. printf("PERFORM\n");
  260. curl_easy_perform(curl);
  261. printf("CLEANUP\n");
  262. curl_easy_cleanup(curl);
  263. curl_free(url);
  264. curl_slist_free_all(headers);
  265. /* load cookies */
  266. curl = curl_easy_init();
  267. if(!curl) {
  268. fprintf(stderr, "curl_easy_init() failed\n");
  269. curl_share_cleanup(share);
  270. curl_global_cleanup();
  271. return TEST_ERR_MAJOR_BAD;
  272. }
  273. url = suburl(URL, i);
  274. headers = sethost(NULL);
  275. test_setopt(curl, CURLOPT_HTTPHEADER, headers);
  276. test_setopt(curl, CURLOPT_URL, url);
  277. printf("CURLOPT_SHARE\n");
  278. test_setopt(curl, CURLOPT_SHARE, share);
  279. printf("CURLOPT_COOKIELIST ALL\n");
  280. test_setopt(curl, CURLOPT_COOKIELIST, "ALL");
  281. printf("CURLOPT_COOKIEJAR\n");
  282. test_setopt(curl, CURLOPT_COOKIEFILE, JAR);
  283. printf("CURLOPT_COOKIELIST RELOAD\n");
  284. test_setopt(curl, CURLOPT_COOKIELIST, "RELOAD");
  285. code = curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
  286. if(code != CURLE_OK) {
  287. fprintf(stderr, "curl_easy_getinfo() failed\n");
  288. res = TEST_ERR_MAJOR_BAD;
  289. goto test_cleanup;
  290. }
  291. printf("loaded cookies:\n");
  292. if(!cookies) {
  293. fprintf(stderr, " reloading cookies from '%s' failed\n", JAR);
  294. res = TEST_ERR_MAJOR_BAD;
  295. goto test_cleanup;
  296. }
  297. printf("-----------------\n");
  298. next_cookie = cookies;
  299. while(next_cookie) {
  300. printf(" %s\n", next_cookie->data);
  301. next_cookie = next_cookie->next;
  302. }
  303. printf("-----------------\n");
  304. curl_slist_free_all(cookies);
  305. /* try to free share, expect to fail because share is in use*/
  306. printf("try SHARE_CLEANUP...\n");
  307. scode = curl_share_cleanup(share);
  308. if(scode == CURLSHE_OK) {
  309. fprintf(stderr, "curl_share_cleanup succeed but error expected\n");
  310. share = NULL;
  311. }
  312. else {
  313. printf("SHARE_CLEANUP failed, correct\n");
  314. }
  315. test_cleanup:
  316. /* clean up last handle */
  317. printf("CLEANUP\n");
  318. curl_easy_cleanup(curl);
  319. curl_slist_free_all(headers);
  320. curl_free(url);
  321. /* free share */
  322. printf("SHARE_CLEANUP\n");
  323. scode = curl_share_cleanup(share);
  324. if(scode != CURLSHE_OK)
  325. fprintf(stderr, "curl_share_cleanup failed, code errno %d\n",
  326. (int)scode);
  327. printf("GLOBAL_CLEANUP\n");
  328. curl_global_cleanup();
  329. return res;
  330. }