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 651464 by 35 * Christopher R. Palmer. 36 * 37 * Use multi interface to get document over proxy with bad port number. 38 * This caused the interface to "hang" in libcurl 7.10.2. 39 */ 40int test(char *URL) 41{ 42 CURL *c; 43 int res = 0; 44 CURLM *m = NULL; 45 fd_set rd, wr, exc; 46 CURLMcode ret; 47 char done = FALSE; 48 int running; 49 int max_fd; 50 int rc; 51 struct timeval ml_start; 52 struct timeval mp_start; 53 char ml_timedout = FALSE; 54 char mp_timedout = FALSE; 55 56 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { 57 fprintf(stderr, "curl_global_init() failed\n"); 58 return TEST_ERR_MAJOR_BAD; 59 } 60 61 if ((c = curl_easy_init()) == NULL) { 62 fprintf(stderr, "curl_easy_init() failed\n"); 63 curl_global_cleanup(); 64 return TEST_ERR_MAJOR_BAD; 65 } 66 67 /* The point here is that there must not be anything running on the given 68 proxy port */ 69 if (libtest_arg2) 70 test_setopt(c, CURLOPT_PROXY, libtest_arg2); 71 test_setopt(c, CURLOPT_URL, URL); 72 test_setopt(c, CURLOPT_VERBOSE, 1L); 73 74 if ((m = curl_multi_init()) == NULL) { 75 fprintf(stderr, "curl_multi_init() failed\n"); 76 curl_easy_cleanup(c); 77 curl_global_cleanup(); 78 return TEST_ERR_MAJOR_BAD; 79 } 80 81 if ((ret = curl_multi_add_handle(m, c)) != CURLM_OK) { 82 fprintf(stderr, "curl_multi_add_handle() failed, " 83 "with code %d\n", ret); 84 curl_multi_cleanup(m); 85 curl_easy_cleanup(c); 86 curl_global_cleanup(); 87 return TEST_ERR_MAJOR_BAD; 88 } 89 90 ml_timedout = FALSE; 91 ml_start = tutil_tvnow(); 92 93 while (!done) { 94 struct timeval interval; 95 96 interval.tv_sec = 1; 97 interval.tv_usec = 0; 98 99 if (tutil_tvdiff(tutil_tvnow(), ml_start) > 100 MAIN_LOOP_HANG_TIMEOUT) { 101 ml_timedout = TRUE; 102 break; 103 } 104 mp_timedout = FALSE; 105 mp_start = tutil_tvnow(); 106 107 fprintf(stderr, "curl_multi_perform()\n"); 108 109 ret = CURLM_CALL_MULTI_PERFORM; 110 111 while (ret == CURLM_CALL_MULTI_PERFORM) { 112 ret = curl_multi_perform(m, &running); 113 if (tutil_tvdiff(tutil_tvnow(), mp_start) > 114 MULTI_PERFORM_HANG_TIMEOUT) { 115 mp_timedout = TRUE; 116 break; 117 } 118 } 119 if (mp_timedout) 120 break; 121 122 if(!running) { 123 /* This is where this code is expected to reach */ 124 int numleft; 125 CURLMsg *msg = curl_multi_info_read(m, &numleft); 126 fprintf(stderr, "Expected: not running\n"); 127 if(msg && !numleft) 128 res = 100; /* this is where we should be */ 129 else 130 res = 99; /* not correct */ 131 break; 132 } 133 fprintf(stderr, "running == %d, ret == %d\n", running, ret); 134 135 if (ret != CURLM_OK) { 136 res = 2; 137 break; 138 } 139 140 FD_ZERO(&rd); 141 FD_ZERO(&wr); 142 FD_ZERO(&exc); 143 max_fd = 0; 144 145 fprintf(stderr, "curl_multi_fdset()\n"); 146 if (curl_multi_fdset(m, &rd, &wr, &exc, &max_fd) != CURLM_OK) { 147 fprintf(stderr, "unexpected failured of fdset.\n"); 148 res = 3; 149 break; 150 } 151 rc = select_test(max_fd+1, &rd, &wr, &exc, &interval); 152 fprintf(stderr, "select returned %d\n", rc); 153 } 154 155 if (ml_timedout || mp_timedout) { 156 if (ml_timedout) fprintf(stderr, "ml_timedout\n"); 157 if (mp_timedout) fprintf(stderr, "mp_timedout\n"); 158 fprintf(stderr, "ABORTING TEST, since it seems " 159 "that it would have run forever.\n"); 160 res = TEST_ERR_RUNS_FOREVER; 161 } 162 163test_cleanup: 164 165 if(m) { 166 curl_multi_remove_handle(m, c); 167 curl_multi_cleanup(m); 168 } 169 curl_easy_cleanup(c); 170 curl_global_cleanup(); 171 172 return res; 173} 174 175