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#include "test.h"
23
24#include <sys/types.h>
25
26#include "testutil.h"
27#include "warnless.h"
28#include "memdebug.h"
29
30#define MAIN_LOOP_HANG_TIMEOUT     90 * 1000
31#define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
32
33/*
34 * Source code in here hugely as reported in bug report 651460 by
35 * Christopher R. Palmer.
36 *
37 * Use multi interface to get HTTPS document over proxy, and provide
38 * auth info.
39 */
40
41int test(char *URL)
42{
43  CURL *c;
44  CURLM *m = NULL;
45  int res = 0;
46  int running;
47  char done = FALSE;
48  struct timeval ml_start;
49  struct timeval mp_start;
50  char ml_timedout = FALSE;
51  char mp_timedout = FALSE;
52
53  if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
54    fprintf(stderr, "curl_global_init() failed\n");
55    return TEST_ERR_MAJOR_BAD;
56  }
57
58  if ((c = curl_easy_init()) == NULL) {
59    fprintf(stderr, "curl_easy_init() failed\n");
60    curl_global_cleanup();
61    return TEST_ERR_MAJOR_BAD;
62  }
63
64  test_setopt(c, CURLOPT_PROXY, libtest_arg2); /* set in first.c */
65  test_setopt(c, CURLOPT_URL, URL);
66  test_setopt(c, CURLOPT_USERPWD, "test:ing");
67  test_setopt(c, CURLOPT_PROXYUSERPWD, "test:ing");
68  test_setopt(c, CURLOPT_HTTPPROXYTUNNEL, 1L);
69  test_setopt(c, CURLOPT_HEADER, 1L);
70
71  if ((m = curl_multi_init()) == NULL) {
72    fprintf(stderr, "curl_multi_init() failed\n");
73    curl_easy_cleanup(c);
74    curl_global_cleanup();
75    return TEST_ERR_MAJOR_BAD;
76  }
77
78  if ((res = (int)curl_multi_add_handle(m, c)) != CURLM_OK) {
79    fprintf(stderr, "curl_multi_add_handle() failed, "
80            "with code %d\n", res);
81    curl_multi_cleanup(m);
82    curl_easy_cleanup(c);
83    curl_global_cleanup();
84    return TEST_ERR_MAJOR_BAD;
85  }
86
87  ml_timedout = FALSE;
88  ml_start = tutil_tvnow();
89
90  while(!done) {
91    fd_set rd, wr, exc;
92    int max_fd;
93    struct timeval interval;
94
95    interval.tv_sec = 1;
96    interval.tv_usec = 0;
97
98    if (tutil_tvdiff(tutil_tvnow(), ml_start) >
99        MAIN_LOOP_HANG_TIMEOUT) {
100      ml_timedout = TRUE;
101      break;
102    }
103    mp_timedout = FALSE;
104    mp_start = tutil_tvnow();
105
106    while (res == CURLM_CALL_MULTI_PERFORM) {
107      res = (int)curl_multi_perform(m, &running);
108      if (tutil_tvdiff(tutil_tvnow(), mp_start) >
109          MULTI_PERFORM_HANG_TIMEOUT) {
110        mp_timedout = TRUE;
111        break;
112      }
113      if (running <= 0) {
114        done = TRUE;
115        break;
116      }
117    }
118    if (mp_timedout || done)
119      break;
120
121    if (res != CURLM_OK) {
122      fprintf(stderr, "not okay???\n");
123      break;
124    }
125
126    FD_ZERO(&rd);
127    FD_ZERO(&wr);
128    FD_ZERO(&exc);
129    max_fd = 0;
130
131    if (curl_multi_fdset(m, &rd, &wr, &exc, &max_fd) != CURLM_OK) {
132      fprintf(stderr, "unexpected failured of fdset.\n");
133      res = 89;
134      break;
135    }
136
137    if (select_test(max_fd+1, &rd, &wr, &exc, &interval) == -1) {
138      fprintf(stderr, "bad select??\n");
139      res = 95;
140      break;
141    }
142
143    res = CURLM_CALL_MULTI_PERFORM;
144  }
145
146  if (ml_timedout || mp_timedout) {
147    if (ml_timedout) fprintf(stderr, "ml_timedout\n");
148    if (mp_timedout) fprintf(stderr, "mp_timedout\n");
149    fprintf(stderr, "ABORTING TEST, since it seems "
150            "that it would have run forever.\n");
151    res = TEST_ERR_RUNS_FOREVER;
152  }
153
154test_cleanup:
155
156  if(m) {
157    curl_multi_remove_handle(m, c);
158    curl_multi_cleanup(m);
159  }
160  curl_easy_cleanup(c);
161  curl_global_cleanup();
162
163  return res;
164}
165
166