xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame^] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 1998 - 2022, 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 "test.h" |
| 25 | |
| 26 | #ifdef HAVE_LOCALE_H |
| 27 | # include <locale.h> /* for setlocale() */ |
| 28 | #endif |
| 29 | |
| 30 | #ifdef HAVE_IO_H |
| 31 | # include <io.h> /* for setmode() */ |
| 32 | #endif |
| 33 | |
| 34 | #ifdef HAVE_FCNTL_H |
| 35 | # include <fcntl.h> /* for setmode() */ |
| 36 | #endif |
| 37 | |
| 38 | #ifdef USE_NSS |
| 39 | #include <nspr.h> |
| 40 | #endif |
| 41 | |
| 42 | #ifdef CURLDEBUG |
| 43 | # define MEMDEBUG_NODEFINES |
| 44 | # include "memdebug.h" |
| 45 | #endif |
| 46 | |
| 47 | #include "timediff.h" |
| 48 | |
| 49 | int select_wrapper(int nfds, fd_set *rd, fd_set *wr, fd_set *exc, |
| 50 | struct timeval *tv) |
| 51 | { |
| 52 | if(nfds < 0) { |
| 53 | SET_SOCKERRNO(EINVAL); |
| 54 | return -1; |
| 55 | } |
| 56 | #ifdef USE_WINSOCK |
| 57 | /* |
| 58 | * Winsock select() requires that at least one of the three fd_set |
| 59 | * pointers is not NULL and points to a non-empty fdset. IOW Winsock |
| 60 | * select() can not be used to sleep without a single fd_set. |
| 61 | */ |
| 62 | if(!nfds) { |
| 63 | Sleep((DWORD)curlx_tvtoms(tv)); |
| 64 | return 0; |
| 65 | } |
| 66 | #endif |
| 67 | return select(nfds, rd, wr, exc, tv); |
| 68 | } |
| 69 | |
| 70 | void wait_ms(int ms) |
| 71 | { |
| 72 | #ifdef USE_WINSOCK |
| 73 | Sleep(ms); |
| 74 | #else |
| 75 | struct timeval t; |
| 76 | curlx_mstotv(&t, ms); |
| 77 | select_wrapper(0, NULL, NULL, NULL, &t); |
| 78 | #endif |
| 79 | } |
| 80 | |
| 81 | char *libtest_arg2 = NULL; |
| 82 | char *libtest_arg3 = NULL; |
| 83 | int test_argc; |
| 84 | char **test_argv; |
| 85 | |
| 86 | struct timeval tv_test_start; /* for test timing */ |
| 87 | |
| 88 | #ifdef UNITTESTS |
| 89 | int unitfail; /* for unittests */ |
| 90 | #endif |
| 91 | |
| 92 | #ifdef CURLDEBUG |
| 93 | static void memory_tracking_init(void) |
| 94 | { |
| 95 | char *env; |
| 96 | /* if CURL_MEMDEBUG is set, this starts memory tracking message logging */ |
| 97 | env = curl_getenv("CURL_MEMDEBUG"); |
| 98 | if(env) { |
| 99 | /* use the value as file name */ |
| 100 | char fname[CURL_MT_LOGFNAME_BUFSIZE]; |
| 101 | if(strlen(env) >= CURL_MT_LOGFNAME_BUFSIZE) |
| 102 | env[CURL_MT_LOGFNAME_BUFSIZE-1] = '\0'; |
| 103 | strcpy(fname, env); |
| 104 | curl_free(env); |
| 105 | curl_dbg_memdebug(fname); |
| 106 | /* this weird stuff here is to make curl_free() get called before |
| 107 | curl_dbg_memdebug() as otherwise memory tracking will log a free() |
| 108 | without an alloc! */ |
| 109 | } |
| 110 | /* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */ |
| 111 | env = curl_getenv("CURL_MEMLIMIT"); |
| 112 | if(env) { |
| 113 | char *endptr; |
| 114 | long num = strtol(env, &endptr, 10); |
| 115 | if((endptr != env) && (endptr == env + strlen(env)) && (num > 0)) |
| 116 | curl_dbg_memlimit(num); |
| 117 | curl_free(env); |
| 118 | } |
| 119 | } |
| 120 | #else |
| 121 | # define memory_tracking_init() Curl_nop_stmt |
| 122 | #endif |
| 123 | |
| 124 | /* returns a hexdump in a static memory area */ |
| 125 | char *hexdump(const unsigned char *buffer, size_t len) |
| 126 | { |
| 127 | static char dump[200 * 3 + 1]; |
| 128 | char *p = dump; |
| 129 | size_t i; |
| 130 | if(len > 200) |
| 131 | return NULL; |
| 132 | for(i = 0; i<len; i++, p += 3) |
| 133 | msnprintf(p, 4, "%02x ", buffer[i]); |
| 134 | return dump; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | int main(int argc, char **argv) |
| 139 | { |
| 140 | char *URL; |
| 141 | int result; |
| 142 | |
| 143 | #ifdef O_BINARY |
| 144 | # ifdef __HIGHC__ |
| 145 | _setmode(stdout, O_BINARY); |
| 146 | # else |
| 147 | setmode(fileno(stdout), O_BINARY); |
| 148 | # endif |
| 149 | #endif |
| 150 | |
| 151 | memory_tracking_init(); |
| 152 | |
| 153 | /* |
| 154 | * Setup proper locale from environment. This is needed to enable locale- |
| 155 | * specific behavior by the C library in order to test for undesired side |
| 156 | * effects that could cause in libcurl. |
| 157 | */ |
| 158 | #ifdef HAVE_SETLOCALE |
| 159 | setlocale(LC_ALL, ""); |
| 160 | #endif |
| 161 | |
| 162 | if(argc< 2) { |
| 163 | fprintf(stderr, "Pass URL as argument please\n"); |
| 164 | return 1; |
| 165 | } |
| 166 | |
| 167 | test_argc = argc; |
| 168 | test_argv = argv; |
| 169 | |
| 170 | if(argc>2) |
| 171 | libtest_arg2 = argv[2]; |
| 172 | |
| 173 | if(argc>3) |
| 174 | libtest_arg3 = argv[3]; |
| 175 | |
| 176 | URL = argv[1]; /* provide this to the rest */ |
| 177 | |
| 178 | fprintf(stderr, "URL: %s\n", URL); |
| 179 | |
| 180 | result = test(URL); |
| 181 | |
| 182 | #ifdef USE_NSS |
| 183 | if(PR_Initialized()) |
| 184 | /* prevent valgrind from reporting possibly lost memory (fd cache, ...) */ |
| 185 | PR_Cleanup(); |
| 186 | #endif |
| 187 | |
| 188 | #ifdef WIN32 |
| 189 | /* flush buffers of all streams regardless of mode */ |
| 190 | _flushall(); |
| 191 | #endif |
| 192 | |
| 193 | return result; |
| 194 | } |