1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel (at) 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 "testtrace.h" 25 #include "memdebug.h" 26 27 #ifdef LIB585 28 29 static int counter; 30 31 static curl_socket_t tst_opensocket(void *clientp, 32 curlsocktype purpose, 33 struct curl_sockaddr *addr) 34 { 35 (void)clientp; 36 (void)purpose; 37 printf("[OPEN] counter: %d\n", ++counter); 38 return socket(addr->family, addr->socktype, addr->protocol); 39 } 40 41 static int tst_closesocket(void *clientp, curl_socket_t sock) 42 { 43 (void)clientp; 44 printf("[CLOSE] counter: %d\n", counter--); 45 return sclose(sock); 46 } 47 48 static void setupcallbacks(CURL *curl) 49 { 50 curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, tst_opensocket); 51 curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, tst_closesocket); 52 counter = 0; 53 } 54 55 #else 56 #define setupcallbacks(x) Curl_nop_stmt 57 #endif 58 59 60 int test(char *URL) 61 { 62 CURLcode res; 63 CURL *curl; 64 char *ipstr=NULL; 65 66 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { 67 fprintf(stderr, "curl_global_init() failed\n"); 68 return TEST_ERR_MAJOR_BAD; 69 } 70 71 if ((curl = curl_easy_init()) == NULL) { 72 fprintf(stderr, "curl_easy_init() failed\n"); 73 curl_global_cleanup(); 74 return TEST_ERR_MAJOR_BAD; 75 } 76 77 test_setopt(curl, CURLOPT_URL, URL); 78 test_setopt(curl, CURLOPT_HEADER, 1L); 79 80 libtest_debug_config.nohex = 1; 81 libtest_debug_config.tracetime = 1; 82 test_setopt(curl, CURLOPT_DEBUGDATA, &libtest_debug_config); 83 test_setopt(curl, CURLOPT_DEBUGFUNCTION, libtest_debug_cb); 84 test_setopt(curl, CURLOPT_VERBOSE, 1L); 85 86 if(libtest_arg3 && !strcmp(libtest_arg3, "activeftp")) 87 test_setopt(curl, CURLOPT_FTPPORT, "-"); 88 89 setupcallbacks(curl); 90 91 res = curl_easy_perform(curl); 92 93 if(!res) { 94 res = curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ipstr); 95 if (libtest_arg2) { 96 FILE *moo = fopen(libtest_arg2, "wb"); 97 if(moo) { 98 double time_namelookup; 99 double time_connect; 100 double time_pretransfer; 101 double time_starttransfer; 102 double time_total; 103 fprintf(moo, "IP: %s\n", ipstr); 104 curl_easy_getinfo(curl, CURLINFO_NAMELOOKUP_TIME, &time_namelookup); 105 curl_easy_getinfo(curl, CURLINFO_CONNECT_TIME, &time_connect); 106 curl_easy_getinfo(curl, CURLINFO_PRETRANSFER_TIME, &time_pretransfer); 107 curl_easy_getinfo(curl, CURLINFO_STARTTRANSFER_TIME, 108 &time_starttransfer); 109 curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &time_total); 110 111 /* since the timing will always vary we only compare relative differences 112 between these 5 times */ 113 if(time_namelookup > time_connect) { 114 fprintf(moo, "namelookup vs connect: %f %f\n", 115 time_namelookup, time_connect); 116 } 117 if(time_connect > time_pretransfer) { 118 fprintf(moo, "connect vs pretransfer: %f %f\n", 119 time_connect, time_pretransfer); 120 } 121 if(time_pretransfer > time_starttransfer) { 122 fprintf(moo, "pretransfer vs starttransfer: %f %f\n", 123 time_pretransfer, time_starttransfer); 124 } 125 if(time_starttransfer > time_total) { 126 fprintf(moo, "starttransfer vs total: %f %f\n", 127 time_starttransfer, time_total); 128 } 129 130 fclose(moo); 131 } 132 } 133 } 134 135 test_cleanup: 136 137 curl_easy_cleanup(curl); 138 curl_global_cleanup(); 139 140 return (int)res; 141 } 142 143