first.c 4.6 KB

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