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