1e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET/***************************************************************************
2e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *                                  _   _ ____  _
3e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *  Project                     ___| | | |  _ \| |
4e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *                             / __| | | | |_) | |
5e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *                            | (__| |_| |  _ <| |___
6e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *                             \___|\___/|_| \_\_____|
7e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *
8d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
9e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *
10e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * This software is licensed as described in the file COPYING, which
11e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * you should have received as part of this distribution. The terms
12d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo * are also available at https://curl.haxx.se/docs/copyright.html.
13e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *
14e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * copies of the Software, and permit persons to whom the Software is
16e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * furnished to do so, under the terms of the COPYING file.
17e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *
18e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET * KIND, either express or implied.
20e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET *
21e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET ***************************************************************************/
22e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#include "test.h"
23e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
24e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#include <fcntl.h>
25e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
26e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#include "testutil.h"
27e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#include "warnless.h"
28e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#include "memdebug.h"
29e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
30e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#define TEST_HANG_TIMEOUT 30 * 1000
31e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
32e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET/* 500 milliseconds allowed. An extreme number but lets be really conservative
33e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET   to allow old and slow machines to run this test too */
34e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET#define MAX_BLOCKED_TIME_US 500000
35e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
36e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET/* return the number of microseconds between two time stamps */
37e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETstatic int elapsed(struct timeval *before,
38e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET                   struct timeval *after)
39e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET{
40e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  ssize_t result;
41e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
42e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  result = (after->tv_sec - before->tv_sec) * 1000000 +
43e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    after->tv_usec - before->tv_usec;
44d15eaac64ca1a7244824d660ea84e8815a23d058Alex Deymo  if(result < 0)
45e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    result = 0;
46e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
47e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  return curlx_sztosi(result);
48e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET}
49e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
50e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
51e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETint test(char *URL)
52e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET{
53e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  CURL *handle = NULL;
54e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  CURLM *mhandle = NULL;
55e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  int res = 0;
56e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  int still_running = 0;
57e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
58e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  start_test_timing();
59e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
60e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  global_init(CURL_GLOBAL_ALL);
61e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
62e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  easy_init(handle);
63e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
64e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  easy_setopt(handle, CURLOPT_URL, URL);
65e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  easy_setopt(handle, CURLOPT_VERBOSE, 1L);
66e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
67e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  multi_init(mhandle);
68e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
69e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  multi_add_handle(mhandle, handle);
70e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
71e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  multi_perform(mhandle, &still_running);
72e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
73e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  abort_on_test_timeout();
74e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
75e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  while(still_running) {
76e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    struct timeval timeout;
77e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    fd_set fdread;
78e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    fd_set fdwrite;
79e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    fd_set fdexcep;
80e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    int maxfd = -99;
81e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    struct timeval before;
82e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    struct timeval after;
83e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    int e;
84e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
85e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    timeout.tv_sec = 0;
86e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    timeout.tv_usec = 100000L; /* 100 ms */
87e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
88e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    FD_ZERO(&fdread);
89e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    FD_ZERO(&fdwrite);
90e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    FD_ZERO(&fdexcep);
91e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
92e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    multi_fdset(mhandle, &fdread, &fdwrite, &fdexcep, &maxfd);
93e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
94e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    /* At this point, maxfd is guaranteed to be greater or equal than -1. */
95e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
96e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    select_test(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
97e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
98e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    abort_on_test_timeout();
99e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
100e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    fprintf(stderr, "ping\n");
101e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    before = tutil_tvnow();
102e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
103e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    multi_perform(mhandle, &still_running);
104e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
105e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    abort_on_test_timeout();
106e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
107e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    after = tutil_tvnow();
108e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    e = elapsed(&before, &after);
109e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    fprintf(stderr, "pong = %d\n", e);
110e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
111e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    if(e > MAX_BLOCKED_TIME_US) {
112e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      res = 100;
113e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET      break;
114e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET    }
115e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  }
116e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
117e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNETtest_cleanup:
118e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
119e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  /* undocumented cleanup sequence - type UA */
120e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
121e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_multi_cleanup(mhandle);
122e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_easy_cleanup(handle);
123e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  curl_global_cleanup();
124e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET
125e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET  return res;
126e6cd738ed3716c02557fb3a47515244e949ade39Bertrand SIMONNET}
127