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 "testutil.h" 25#include "warnless.h" 26#include "memdebug.h" 27 28#define MAIN_LOOP_HANG_TIMEOUT 90 * 1000 29#define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000 30 31int test(char *URL) 32{ 33 CURL* curls; 34 CURLM* multi; 35 int still_running; 36 int i = -1; 37 int res = 0; 38 CURLMsg *msg; 39 CURLMcode ret; 40 struct timeval ml_start; 41 struct timeval mp_start; 42 char ml_timedout = FALSE; 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 ((multi = curl_multi_init()) == NULL) { 51 fprintf(stderr, "curl_multi_init() failed\n"); 52 curl_global_cleanup(); 53 return TEST_ERR_MAJOR_BAD; 54 } 55 56 if ((curls = curl_easy_init()) == NULL) { 57 fprintf(stderr, "curl_easy_init() failed\n"); 58 curl_multi_cleanup(multi); 59 curl_global_cleanup(); 60 return TEST_ERR_MAJOR_BAD; 61 } 62 63 test_setopt(curls, CURLOPT_URL, URL); 64 test_setopt(curls, CURLOPT_HEADER, 1L); 65 66 if ((ret = curl_multi_add_handle(multi, curls)) != CURLM_OK) { 67 fprintf(stderr, "curl_multi_add_handle() failed, " 68 "with code %d\n", ret); 69 curl_easy_cleanup(curls); 70 curl_multi_cleanup(multi); 71 curl_global_cleanup(); 72 return TEST_ERR_MAJOR_BAD; 73 } 74 75 mp_timedout = FALSE; 76 mp_start = tutil_tvnow(); 77 78 do { 79 ret = curl_multi_perform(multi, &still_running); 80 if (tutil_tvdiff(tutil_tvnow(), mp_start) > 81 MULTI_PERFORM_HANG_TIMEOUT) { 82 mp_timedout = TRUE; 83 break; 84 } 85 } while (ret == CURLM_CALL_MULTI_PERFORM); 86 87 ml_timedout = FALSE; 88 ml_start = tutil_tvnow(); 89 90 while ((!ml_timedout) && (!mp_timedout) && (still_running)) { 91 struct timeval timeout; 92 int rc; 93 fd_set fdread; 94 fd_set fdwrite; 95 fd_set fdexcep; 96 int maxfd; 97 98 FD_ZERO(&fdread); 99 FD_ZERO(&fdwrite); 100 FD_ZERO(&fdexcep); 101 timeout.tv_sec = 1; 102 timeout.tv_usec = 0; 103 104 if (tutil_tvdiff(tutil_tvnow(), ml_start) > 105 MAIN_LOOP_HANG_TIMEOUT) { 106 ml_timedout = TRUE; 107 break; 108 } 109 110 curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd); 111 rc = select_test(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); 112 switch(rc) { 113 case -1: 114 break; 115 case 0: 116 default: 117 mp_timedout = FALSE; 118 mp_start = tutil_tvnow(); 119 do { 120 ret = curl_multi_perform(multi, &still_running); 121 if (tutil_tvdiff(tutil_tvnow(), mp_start) > 122 MULTI_PERFORM_HANG_TIMEOUT) { 123 mp_timedout = TRUE; 124 break; 125 } 126 } while (ret == CURLM_CALL_MULTI_PERFORM); 127 break; 128 } 129 } 130 if (ml_timedout || mp_timedout) { 131 if (ml_timedout) fprintf(stderr, "ml_timedout\n"); 132 if (mp_timedout) fprintf(stderr, "mp_timedout\n"); 133 fprintf(stderr, "ABORTING TEST, since it seems " 134 "that it would have run forever.\n"); 135 i = TEST_ERR_RUNS_FOREVER; 136 } 137 else { 138 msg = curl_multi_info_read(multi, &still_running); 139 if(msg) 140 /* this should now contain a result code from the easy handle, 141 get it */ 142 i = msg->data.result; 143 } 144 145test_cleanup: 146 147 curl_multi_cleanup(multi); 148 curl_easy_cleanup(curls); 149 curl_global_cleanup(); 150 151 if(res) 152 i = res; 153 154 return i; /* return the final return code */ 155} 156