Home | History | Annotate | Download | only in libtest
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel (at) 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 /* This is the 'proxyauth.c' test app posted by Shmulik Regev on the libcurl
     23  * mailing list on 10 Jul 2007, converted to a test case.
     24  *
     25  * argv1 = URL
     26  * argv2 = proxy
     27  * argv3 = proxyuser:password
     28  * argv4 = host name to use for the custom Host: header
     29  */
     30 
     31 #include "test.h"
     32 
     33 #include <limits.h>
     34 
     35 #include "testutil.h"
     36 #include "warnless.h"
     37 #include "memdebug.h"
     38 
     39 #define TEST_HANG_TIMEOUT 60 * 1000
     40 
     41 #define PROXY libtest_arg2
     42 #define PROXYUSERPWD libtest_arg3
     43 #define HOST test_argv[4]
     44 
     45 #define NUM_HANDLES 2
     46 
     47 static CURL *eh[NUM_HANDLES];
     48 
     49 static int init(int num, CURLM *cm, const char *url, const char *userpwd,
     50                 struct curl_slist *headers)
     51 {
     52   int res = 0;
     53 
     54   res_easy_init(eh[num]);
     55   if(res)
     56     goto init_failed;
     57 
     58   res_easy_setopt(eh[num], CURLOPT_URL, url);
     59   if(res)
     60     goto init_failed;
     61 
     62   res_easy_setopt(eh[num], CURLOPT_PROXY, PROXY);
     63   if(res)
     64     goto init_failed;
     65 
     66   res_easy_setopt(eh[num], CURLOPT_PROXYUSERPWD, userpwd);
     67   if(res)
     68     goto init_failed;
     69 
     70   res_easy_setopt(eh[num], CURLOPT_PROXYAUTH, (long)CURLAUTH_ANY);
     71   if(res)
     72     goto init_failed;
     73 
     74   res_easy_setopt(eh[num], CURLOPT_VERBOSE, 1L);
     75   if(res)
     76     goto init_failed;
     77 
     78   res_easy_setopt(eh[num], CURLOPT_HEADER, 1L);
     79   if(res)
     80     goto init_failed;
     81 
     82   res_easy_setopt(eh[num], CURLOPT_HTTPHEADER, headers); /* custom Host: */
     83   if(res)
     84     goto init_failed;
     85 
     86   res_multi_add_handle(cm, eh[num]);
     87   if(res)
     88     goto init_failed;
     89 
     90   return 0; /* success */
     91 
     92 init_failed:
     93 
     94   curl_easy_cleanup(eh[num]);
     95   eh[num] = NULL;
     96 
     97   return res; /* failure */
     98 }
     99 
    100 static int loop(int num, CURLM *cm, const char *url, const char *userpwd,
    101                 struct curl_slist *headers)
    102 {
    103   CURLMsg *msg;
    104   long L;
    105   int Q, U = -1;
    106   fd_set R, W, E;
    107   struct timeval T;
    108   int res = 0;
    109 
    110   res = init(num, cm, url, userpwd, headers);
    111   if(res)
    112     return res;
    113 
    114   while(U) {
    115 
    116     int M = -99;
    117 
    118     res_multi_perform(cm, &U);
    119     if(res)
    120       return res;
    121 
    122     res_test_timedout();
    123     if(res)
    124       return res;
    125 
    126     if(U) {
    127       FD_ZERO(&R);
    128       FD_ZERO(&W);
    129       FD_ZERO(&E);
    130 
    131       res_multi_fdset(cm, &R, &W, &E, &M);
    132       if(res)
    133         return res;
    134 
    135       /* At this point, M is guaranteed to be greater or equal than -1. */
    136 
    137       res_multi_timeout(cm, &L);
    138       if(res)
    139         return res;
    140 
    141       /* At this point, L is guaranteed to be greater or equal than -1. */
    142 
    143       if(L != -1) {
    144         int itimeout = (L > (long)INT_MAX) ? INT_MAX : (int)L;
    145         T.tv_sec = itimeout/1000;
    146         T.tv_usec = (itimeout%1000)*1000;
    147       }
    148       else {
    149         T.tv_sec = 5;
    150         T.tv_usec = 0;
    151       }
    152 
    153       res_select_test(M + 1, &R, &W, &E, &T);
    154       if(res)
    155         return res;
    156     }
    157 
    158     while((msg = curl_multi_info_read(cm, &Q)) != NULL) {
    159       if(msg->msg == CURLMSG_DONE) {
    160         int i;
    161         CURL *e = msg->easy_handle;
    162         fprintf(stderr, "R: %d - %s\n", (int)msg->data.result,
    163                 curl_easy_strerror(msg->data.result));
    164         curl_multi_remove_handle(cm, e);
    165         curl_easy_cleanup(e);
    166         for(i = 0; i < NUM_HANDLES; i++) {
    167           if(eh[i] == e) {
    168             eh[i] = NULL;
    169             break;
    170           }
    171         }
    172       }
    173       else
    174         fprintf(stderr, "E: CURLMsg (%d)\n", (int)msg->msg);
    175     }
    176 
    177     res_test_timedout();
    178     if(res)
    179       return res;
    180   }
    181 
    182   return 0; /* success */
    183 }
    184 
    185 int test(char *URL)
    186 {
    187   CURLM *cm = NULL;
    188   struct curl_slist *headers = NULL;
    189   char buffer[246]; /* naively fixed-size */
    190   int res = 0;
    191   int i;
    192 
    193   for(i = 0; i < NUM_HANDLES; i++)
    194     eh[i] = NULL;
    195 
    196   start_test_timing();
    197 
    198   if(test_argc < 4)
    199     return 99;
    200 
    201   snprintf(buffer, sizeof(buffer), "Host: %s", HOST);
    202 
    203   /* now add a custom Host: header */
    204   headers = curl_slist_append(headers, buffer);
    205   if(!headers) {
    206     fprintf(stderr, "curl_slist_append() failed\n");
    207     return TEST_ERR_MAJOR_BAD;
    208   }
    209 
    210   res_global_init(CURL_GLOBAL_ALL);
    211   if(res) {
    212     curl_slist_free_all(headers);
    213     return res;
    214   }
    215 
    216   res_multi_init(cm);
    217   if(res) {
    218     curl_global_cleanup();
    219     curl_slist_free_all(headers);
    220     return res;
    221   }
    222 
    223   res = loop(0, cm, URL, PROXYUSERPWD, headers);
    224   if(res)
    225     goto test_cleanup;
    226 
    227   fprintf(stderr, "lib540: now we do the request again\n");
    228 
    229   res = loop(1, cm, URL, PROXYUSERPWD, headers);
    230 
    231 test_cleanup:
    232 
    233   /* proper cleanup sequence - type PB */
    234 
    235   for(i = 0; i < NUM_HANDLES; i++) {
    236     curl_multi_remove_handle(cm, eh[i]);
    237     curl_easy_cleanup(eh[i]);
    238   }
    239 
    240   curl_multi_cleanup(cm);
    241   curl_global_cleanup();
    242 
    243   curl_slist_free_all(headers);
    244 
    245   return res;
    246 }
    247