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