xref: /curl/tests/libtest/lib1554.c (revision 71cf0d1f)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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 
27 static const char *ldata_names[] = {
28   "NONE",
29   "SHARE",
30   "COOKIE",
31   "DNS",
32   "SESSION",
33   "CONNECT",
34   "PSL",
35   "HSTS",
36   "NULL",
37 };
38 
test_lock(CURL * handle,curl_lock_data data,curl_lock_access laccess,void * useptr)39 static void test_lock(CURL *handle, curl_lock_data data,
40                       curl_lock_access laccess, void *useptr)
41 {
42   (void)handle;
43   (void)data;
44   (void)laccess;
45   (void)useptr;
46   printf("-> Mutex lock %s\n", ldata_names[data]);
47 }
48 
test_unlock(CURL * handle,curl_lock_data data,void * useptr)49 static void test_unlock(CURL *handle, curl_lock_data data, void *useptr)
50 {
51   (void)handle;
52   (void)data;
53   (void)useptr;
54   printf("<- Mutex unlock %s\n", ldata_names[data]);
55 }
56 
57 /* test function */
test(char * URL)58 CURLcode test(char *URL)
59 {
60   CURLcode res = CURLE_OK;
61   CURLSH *share = NULL;
62   int i;
63 
64   global_init(CURL_GLOBAL_ALL);
65 
66   share = curl_share_init();
67   if(!share) {
68     fprintf(stderr, "curl_share_init() failed\n");
69     goto test_cleanup;
70   }
71 
72   curl_share_setopt(share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
73   curl_share_setopt(share, CURLSHOPT_LOCKFUNC, test_lock);
74   curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, test_unlock);
75 
76   /* Loop the transfer and cleanup the handle properly every lap. This will
77      still reuse connections since the pool is in the shared object! */
78 
79   for(i = 0; i < 3; i++) {
80     CURL *curl = curl_easy_init();
81     if(curl) {
82       curl_easy_setopt(curl, CURLOPT_URL, URL);
83 
84       /* use the share object */
85       curl_easy_setopt(curl, CURLOPT_SHARE, share);
86 
87       /* Perform the request, res will get the return code */
88       res = curl_easy_perform(curl);
89 
90       /* always cleanup */
91       curl_easy_cleanup(curl);
92 
93       /* Check for errors */
94       if(res != CURLE_OK) {
95         fprintf(stderr, "curl_easy_perform() failed: %s\n",
96                 curl_easy_strerror(res));
97         goto test_cleanup;
98       }
99     }
100   }
101 
102 test_cleanup:
103   curl_share_cleanup(share);
104   curl_global_cleanup();
105 
106   return res;
107 }
108