1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2012, 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 <fcntl.h>
25
26#include "testutil.h"
27#include "warnless.h"
28#include "memdebug.h"
29
30#define TEST_HANG_TIMEOUT 30 * 1000
31
32/* 500 milliseconds allowed. An extreme number but lets be really conservative
33   to allow old and slow machines to run this test too */
34#define MAX_BLOCKED_TIME_US 500000
35
36/* return the number of microseconds between two time stamps */
37static int elapsed(struct timeval *before,
38                   struct timeval *after)
39{
40  ssize_t result;
41
42  result = (after->tv_sec - before->tv_sec) * 1000000 +
43    after->tv_usec - before->tv_usec;
44  if (result < 0)
45    result = 0;
46
47  return curlx_sztosi(result);
48}
49
50
51int test(char *URL)
52{
53  CURL *handle = NULL;
54  CURLM *mhandle = NULL;
55  int res = 0;
56  int still_running = 0;
57
58  start_test_timing();
59
60  global_init(CURL_GLOBAL_ALL);
61
62  easy_init(handle);
63
64  easy_setopt(handle, CURLOPT_URL, URL);
65  easy_setopt(handle, CURLOPT_VERBOSE, 1L);
66
67  multi_init(mhandle);
68
69  multi_add_handle(mhandle, handle);
70
71  multi_perform(mhandle, &still_running);
72
73  abort_on_test_timeout();
74
75  while(still_running) {
76    struct timeval timeout;
77    fd_set fdread;
78    fd_set fdwrite;
79    fd_set fdexcep;
80    int maxfd = -99;
81    struct timeval before;
82    struct timeval after;
83    int e;
84
85    timeout.tv_sec = 0;
86    timeout.tv_usec = 100000L; /* 100 ms */
87
88    FD_ZERO(&fdread);
89    FD_ZERO(&fdwrite);
90    FD_ZERO(&fdexcep);
91
92    multi_fdset(mhandle, &fdread, &fdwrite, &fdexcep, &maxfd);
93
94    /* At this point, maxfd is guaranteed to be greater or equal than -1. */
95
96    select_test(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
97
98    abort_on_test_timeout();
99
100    fprintf(stderr, "ping\n");
101    before = tutil_tvnow();
102
103    multi_perform(mhandle, &still_running);
104
105    abort_on_test_timeout();
106
107    after = tutil_tvnow();
108    e = elapsed(&before, &after);
109    fprintf(stderr, "pong = %d\n", e);
110
111    if(e > MAX_BLOCKED_TIME_US) {
112      res = 100;
113      break;
114    }
115  }
116
117test_cleanup:
118
119  /* undocumented cleanup sequence - type UA */
120
121  curl_multi_cleanup(mhandle);
122  curl_easy_cleanup(handle);
123  curl_global_cleanup();
124
125  return res;
126}
127