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
23#include "test.h"
24
25#include "testutil.h"
26#include "memdebug.h"
27
28#define MAIN_LOOP_HANG_TIMEOUT     90 * 1000
29#define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
30
31/*
32 * Get a single URL without select().
33 */
34
35int test(char *URL)
36{
37  CURL *c;
38  CURLM *m = NULL;
39  int res = 0;
40  int running=1;
41  double connect_time = 0.0;
42  struct timeval mp_start;
43  char mp_timedout = FALSE;
44
45  if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
46    fprintf(stderr, "curl_global_init() failed\n");
47    return TEST_ERR_MAJOR_BAD;
48  }
49
50  if ((c = curl_easy_init()) == NULL) {
51    fprintf(stderr, "curl_easy_init() failed\n");
52    curl_global_cleanup();
53    return TEST_ERR_MAJOR_BAD;
54  }
55
56  test_setopt(c, CURLOPT_HEADER, 1L);
57  test_setopt(c, CURLOPT_URL, URL);
58
59  if ((m = curl_multi_init()) == NULL) {
60    fprintf(stderr, "curl_multi_init() failed\n");
61    curl_easy_cleanup(c);
62    curl_global_cleanup();
63    return TEST_ERR_MAJOR_BAD;
64  }
65
66  if ((res = (int)curl_multi_add_handle(m, c)) != CURLM_OK) {
67    fprintf(stderr, "curl_multi_add_handle() failed, "
68            "with code %d\n", res);
69    curl_multi_cleanup(m);
70    curl_easy_cleanup(c);
71    curl_global_cleanup();
72    return TEST_ERR_MAJOR_BAD;
73  }
74
75  mp_timedout = FALSE;
76  mp_start = tutil_tvnow();
77
78  while (running) {
79    res = (int)curl_multi_perform(m, &running);
80    if (tutil_tvdiff(tutil_tvnow(), mp_start) >
81        MULTI_PERFORM_HANG_TIMEOUT) {
82      mp_timedout = TRUE;
83      break;
84    }
85    if (running <= 0) {
86      fprintf(stderr, "nothing left running.\n");
87      break;
88    }
89  }
90
91  if (mp_timedout) {
92    if (mp_timedout) fprintf(stderr, "mp_timedout\n");
93    fprintf(stderr, "ABORTING TEST, since it seems "
94            "that it would have run forever.\n");
95    res = TEST_ERR_RUNS_FOREVER;
96  }
97
98  curl_easy_getinfo(c, CURLINFO_CONNECT_TIME, &connect_time);
99  if (connect_time <= 0.0) {
100    fprintf(stderr, "connect time is <=0.0\n");
101    res = TEST_ERR_MAJOR_BAD;
102  }
103
104test_cleanup:
105
106  if(m) {
107    curl_multi_remove_handle(m, c);
108    curl_multi_cleanup(m);
109  }
110  curl_easy_cleanup(c);
111  curl_global_cleanup();
112
113  return res;
114}
115
116