2
0

first.c 5.4 KB

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