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