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