first.c 5.2 KB

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