lib586.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 #include "test.h"
00023 #include "memdebug.h"
00024 
00025 #define THREADS 2
00026 
00027 /* struct containing data of a thread */
00028 struct Tdata {
00029   CURLSH *share;
00030   char *url;
00031 };
00032 
00033 struct userdata {
00034   char *text;
00035   int counter;
00036 };
00037 
00038 /* lock callback */
00039 static void my_lock(CURL *handle, curl_lock_data data,
00040                     curl_lock_access laccess, void *useptr)
00041 {
00042   const char *what;
00043   struct userdata *user = (struct userdata *)useptr;
00044 
00045   (void)handle;
00046   (void)laccess;
00047 
00048   switch(data) {
00049     case CURL_LOCK_DATA_SHARE:
00050       what = "share";
00051       break;
00052     case CURL_LOCK_DATA_DNS:
00053       what = "dns";
00054       break;
00055     case CURL_LOCK_DATA_COOKIE:
00056       what = "cookie";
00057       break;
00058     case CURL_LOCK_DATA_SSL_SESSION:
00059       what = "ssl_session";
00060       break;
00061     default:
00062       fprintf(stderr, "lock: no such data: %d\n", (int)data);
00063       return;
00064   }
00065   printf("lock:   %-6s [%s]: %d\n", what, user->text, user->counter);
00066   user->counter++;
00067 }
00068 
00069 /* unlock callback */
00070 static void my_unlock(CURL *handle, curl_lock_data data, void *useptr)
00071 {
00072   const char *what;
00073   struct userdata *user = (struct userdata *)useptr;
00074   (void)handle;
00075   switch(data) {
00076     case CURL_LOCK_DATA_SHARE:
00077       what = "share";
00078       break;
00079     case CURL_LOCK_DATA_DNS:
00080       what = "dns";
00081       break;
00082     case CURL_LOCK_DATA_COOKIE:
00083       what = "cookie";
00084       break;
00085     case CURL_LOCK_DATA_SSL_SESSION:
00086       what = "ssl_session";
00087       break;
00088     default:
00089       fprintf(stderr, "unlock: no such data: %d\n", (int)data);
00090       return;
00091   }
00092   printf("unlock: %-6s [%s]: %d\n", what, user->text, user->counter);
00093   user->counter++;
00094 }
00095 
00096 /* the dummy thread function */
00097 static void *fire(void *ptr)
00098 {
00099   CURLcode code;
00100   struct Tdata *tdata = (struct Tdata*)ptr;
00101   CURL *curl;
00102   int i=0;
00103 
00104   curl = curl_easy_init();
00105   if(!curl) {
00106     fprintf(stderr, "curl_easy_init() failed\n");
00107     return NULL;
00108   }
00109 
00110   curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
00111   curl_easy_setopt(curl, CURLOPT_VERBOSE,    1L);
00112   curl_easy_setopt(curl, CURLOPT_URL,        tdata->url);
00113   printf("CURLOPT_SHARE\n");
00114   curl_easy_setopt(curl, CURLOPT_SHARE, tdata->share);
00115 
00116   printf("PERFORM\n");
00117   code = curl_easy_perform(curl);
00118   if(code != CURLE_OK) {
00119     fprintf(stderr, "perform url '%s' repeat %d failed, curlcode %d\n",
00120             tdata->url, i, (int)code);
00121   }
00122 
00123   printf("CLEANUP\n");
00124   curl_easy_cleanup(curl);
00125 
00126   return NULL;
00127 }
00128 
00129 /* test function */
00130 int test(char *URL)
00131 {
00132   int res;
00133   CURLSHcode scode = CURLSHE_OK;
00134   char *url;
00135   struct Tdata tdata;
00136   CURL *curl;
00137   CURLSH *share;
00138   int i;
00139   struct userdata user;
00140 
00141   user.text = (char *)"Pigs in space";
00142   user.counter = 0;
00143 
00144   printf("GLOBAL_INIT\n");
00145   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
00146     fprintf(stderr, "curl_global_init() failed\n");
00147     return TEST_ERR_MAJOR_BAD;
00148   }
00149 
00150   /* prepare share */
00151   printf("SHARE_INIT\n");
00152   share = curl_share_init();
00153   if(!share) {
00154     fprintf(stderr, "curl_share_init() failed\n");
00155     curl_global_cleanup();
00156     return TEST_ERR_MAJOR_BAD;
00157   }
00158 
00159   if(CURLSHE_OK == scode) {
00160     printf("CURLSHOPT_LOCKFUNC\n");
00161     scode = curl_share_setopt(share, CURLSHOPT_LOCKFUNC, my_lock);
00162   }
00163   if(CURLSHE_OK == scode) {
00164     printf("CURLSHOPT_UNLOCKFUNC\n");
00165     scode = curl_share_setopt(share, CURLSHOPT_UNLOCKFUNC, my_unlock);
00166   }
00167   if(CURLSHE_OK == scode) {
00168     printf("CURLSHOPT_USERDATA\n");
00169     scode = curl_share_setopt(share, CURLSHOPT_USERDATA, &user);
00170   }
00171   if(CURLSHE_OK == scode) {
00172     printf("CURL_LOCK_DATA_SSL_SESSION\n");
00173     scode = curl_share_setopt(share, CURLSHOPT_SHARE,
00174                               CURL_LOCK_DATA_SSL_SESSION);
00175   }
00176 
00177   if(CURLSHE_OK != scode) {
00178     fprintf(stderr, "curl_share_setopt() failed\n");
00179     curl_share_cleanup(share);
00180     curl_global_cleanup();
00181     return TEST_ERR_MAJOR_BAD;
00182   }
00183 
00184 
00185   res = 0;
00186 
00187   /* start treads */
00188   for(i=1; i<=THREADS; i++) {
00189 
00190     /* set thread data */
00191     tdata.url   = URL;
00192     tdata.share = share;
00193 
00194     /* simulate thread, direct call of "thread" function */
00195     printf("*** run %d\n",i);
00196     fire(&tdata);
00197   }
00198 
00199 
00200   /* fetch a another one */
00201   printf("*** run %d\n", i);
00202   curl = curl_easy_init();
00203   if(!curl) {
00204     fprintf(stderr, "curl_easy_init() failed\n");
00205     curl_share_cleanup(share);
00206     curl_global_cleanup();
00207     return TEST_ERR_MAJOR_BAD;
00208   }
00209 
00210   url = URL;
00211   test_setopt(curl, CURLOPT_URL, url);
00212   printf("CURLOPT_SHARE\n");
00213   test_setopt(curl, CURLOPT_SHARE, share);
00214 
00215   printf("PERFORM\n");
00216   curl_easy_perform(curl);
00217 
00218   /* try to free share, expect to fail because share is in use*/
00219   printf("try SHARE_CLEANUP...\n");
00220   scode = curl_share_cleanup(share);
00221   if(scode==CURLSHE_OK) {
00222     fprintf(stderr, "curl_share_cleanup succeed but error expected\n");
00223     share = NULL;
00224   }
00225   else {
00226     printf("SHARE_CLEANUP failed, correct\n");
00227   }
00228 
00229 test_cleanup:
00230 
00231   /* clean up last handle */
00232   printf("CLEANUP\n");
00233   curl_easy_cleanup(curl);
00234 
00235   /* free share */
00236   printf("SHARE_CLEANUP\n");
00237   scode = curl_share_cleanup(share);
00238   if(scode!=CURLSHE_OK)
00239     fprintf(stderr, "curl_share_cleanup failed, code errno %d\n",
00240             (int)scode);
00241 
00242   printf("GLOBAL_CLEANUP\n");
00243   curl_global_cleanup();
00244 
00245   return res;
00246 }
00247 


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05