first.c (4482B)
1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 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 https://curl.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 * SPDX-License-Identifier: curl 22 * 23 ***************************************************************************/ 24 #include "first.h" 25 26 #ifdef HAVE_LOCALE_H 27 #include <locale.h> /* for setlocale() */ 28 #endif 29 30 #include "memdebug.h" 31 32 int select_wrapper(int nfds, fd_set *rd, fd_set *wr, fd_set *exc, 33 struct timeval *tv) 34 { 35 if(nfds < 0) { 36 SET_SOCKERRNO(SOCKEINVAL); 37 return -1; 38 } 39 #ifdef USE_WINSOCK 40 /* 41 * Winsock select() requires that at least one of the three fd_set 42 * pointers is not NULL and points to a non-empty fdset. IOW Winsock 43 * select() can not be used to sleep without a single fd_set. 44 */ 45 if(!nfds) { 46 Sleep((DWORD)curlx_tvtoms(tv)); 47 return 0; 48 } 49 #endif 50 return select(nfds, rd, wr, exc, tv); 51 } 52 53 char *libtest_arg2 = NULL; 54 char *libtest_arg3 = NULL; 55 char *libtest_arg4 = NULL; 56 int test_argc; 57 char **test_argv; 58 int testnum; 59 60 struct curltime tv_test_start; /* for test timing */ 61 62 int unitfail; /* for unittests */ 63 64 #ifdef CURLDEBUG 65 static void memory_tracking_init(void) 66 { 67 char *env; 68 /* if CURL_MEMDEBUG is set, this starts memory tracking message logging */ 69 env = getenv("CURL_MEMDEBUG"); 70 if(env) { 71 /* use the value as file name */ 72 curl_dbg_memdebug(env); 73 } 74 /* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */ 75 env = getenv("CURL_MEMLIMIT"); 76 if(env) { 77 char *endptr; 78 long num = strtol(env, &endptr, 10); 79 if((endptr != env) && (endptr == env + strlen(env)) && (num > 0)) 80 curl_dbg_memlimit(num); 81 } 82 } 83 #else 84 # define memory_tracking_init() Curl_nop_stmt 85 #endif 86 87 /* returns a hexdump in a static memory area */ 88 char *hexdump(const unsigned char *buf, size_t len) 89 { 90 static char dump[200 * 3 + 1]; 91 char *p = dump; 92 size_t i; 93 if(len > 200) 94 return NULL; 95 for(i = 0; i < len; i++, p += 3) 96 curl_msnprintf(p, 4, "%02x ", buf[i]); 97 return dump; 98 } 99 100 101 int main(int argc, char **argv) 102 { 103 char *URL; 104 CURLcode result; 105 entry_func_t entry_func; 106 char *entry_name; 107 char *env; 108 size_t tmp; 109 110 CURLX_SET_BINMODE(stdout); 111 112 memory_tracking_init(); 113 #ifdef _WIN32 114 curlx_now_init(); 115 #endif 116 117 /* 118 * Setup proper locale from environment. This is needed to enable locale- 119 * specific behavior by the C library in order to test for undesired side 120 * effects that could cause in libcurl. 121 */ 122 #ifdef HAVE_SETLOCALE 123 setlocale(LC_ALL, ""); 124 #endif 125 126 test_argc = argc - 1; 127 test_argv = argv + 1; 128 129 if(argc < 3) { 130 curl_mfprintf(stderr, "Pass testname and URL as arguments please\n"); 131 return 1; 132 } 133 134 entry_name = argv[1]; 135 entry_func = NULL; 136 for(tmp = 0; s_entries[tmp].ptr; ++tmp) { 137 if(strcmp(entry_name, s_entries[tmp].name) == 0) { 138 entry_func = s_entries[tmp].ptr; 139 break; 140 } 141 } 142 143 if(!entry_func) { 144 curl_mfprintf(stderr, "Test '%s' not found.\n", entry_name); 145 return 1; 146 } 147 148 if(argc > 3) 149 libtest_arg2 = argv[3]; 150 151 if(argc > 4) 152 libtest_arg3 = argv[4]; 153 154 if(argc > 5) 155 libtest_arg4 = argv[5]; 156 157 URL = argv[2]; /* provide this to the rest */ 158 159 env = getenv("CURL_TESTNUM"); 160 if(env) 161 testnum = atoi(env); 162 else 163 testnum = 0; 164 165 curl_mfprintf(stderr, "URL: %s\n", URL); 166 167 result = entry_func(URL); 168 curl_mfprintf(stderr, "Test ended with result %d\n", result); 169 170 #ifdef _WIN32 171 /* flush buffers of all streams regardless of mode */ 172 _flushall(); 173 #endif 174 175 /* Regular program status codes are limited to 0..127 and 126 and 127 have 176 * special meanings by the shell, so limit a normal return code to 125 */ 177 return (int)result <= 125 ? (int)result : 125; 178 }