first.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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. ***************************************************************************/
  22. #include "test.h"
  23. #ifdef HAVE_LOCALE_H
  24. # include <locale.h> /* for setlocale() */
  25. #endif
  26. #ifdef HAVE_IO_H
  27. # include <io.h> /* for setmode() */
  28. #endif
  29. #ifdef HAVE_FCNTL_H
  30. # include <fcntl.h> /* for setmode() */
  31. #endif
  32. #ifdef USE_NSS
  33. #include <nspr.h>
  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. #ifdef USE_WINSOCK
  63. Sleep(ms);
  64. #else
  65. struct timeval t;
  66. curlx_mstotv(&t, ms);
  67. select_wrapper(0, NULL, NULL, NULL, &t);
  68. #endif
  69. }
  70. char *libtest_arg2 = NULL;
  71. char *libtest_arg3 = NULL;
  72. int test_argc;
  73. char **test_argv;
  74. struct timeval tv_test_start; /* for test timing */
  75. #ifdef UNITTESTS
  76. int unitfail; /* for unittests */
  77. #endif
  78. #ifdef CURLDEBUG
  79. static void memory_tracking_init(void)
  80. {
  81. char *env;
  82. /* if CURL_MEMDEBUG is set, this starts memory tracking message logging */
  83. env = curl_getenv("CURL_MEMDEBUG");
  84. if(env) {
  85. /* use the value as file name */
  86. char fname[CURL_MT_LOGFNAME_BUFSIZE];
  87. if(strlen(env) >= CURL_MT_LOGFNAME_BUFSIZE)
  88. env[CURL_MT_LOGFNAME_BUFSIZE-1] = '\0';
  89. strcpy(fname, env);
  90. curl_free(env);
  91. curl_dbg_memdebug(fname);
  92. /* this weird stuff here is to make curl_free() get called before
  93. curl_dbg_memdebug() as otherwise memory tracking will log a free()
  94. without an alloc! */
  95. }
  96. /* if CURL_MEMLIMIT is set, this enables fail-on-alloc-number-N feature */
  97. env = curl_getenv("CURL_MEMLIMIT");
  98. if(env) {
  99. char *endptr;
  100. long num = strtol(env, &endptr, 10);
  101. if((endptr != env) && (endptr == env + strlen(env)) && (num > 0))
  102. curl_dbg_memlimit(num);
  103. curl_free(env);
  104. }
  105. }
  106. #else
  107. # define memory_tracking_init() Curl_nop_stmt
  108. #endif
  109. /* returns a hexdump in a static memory area */
  110. char *hexdump(const unsigned char *buffer, size_t len)
  111. {
  112. static char dump[200 * 3 + 1];
  113. char *p = dump;
  114. size_t i;
  115. if(len > 200)
  116. return NULL;
  117. for(i = 0; i<len; i++, p += 3)
  118. msnprintf(p, 4, "%02x ", buffer[i]);
  119. return dump;
  120. }
  121. int main(int argc, char **argv)
  122. {
  123. char *URL;
  124. int result;
  125. #ifdef O_BINARY
  126. # ifdef __HIGHC__
  127. _setmode(stdout, O_BINARY);
  128. # else
  129. setmode(fileno(stdout), O_BINARY);
  130. # endif
  131. #endif
  132. memory_tracking_init();
  133. /*
  134. * Setup proper locale from environment. This is needed to enable locale-
  135. * specific behavior by the C library in order to test for undesired side
  136. * effects that could cause in libcurl.
  137. */
  138. #ifdef HAVE_SETLOCALE
  139. setlocale(LC_ALL, "");
  140. #endif
  141. if(argc< 2) {
  142. fprintf(stderr, "Pass URL as argument please\n");
  143. return 1;
  144. }
  145. test_argc = argc;
  146. test_argv = argv;
  147. if(argc>2)
  148. libtest_arg2 = argv[2];
  149. if(argc>3)
  150. libtest_arg3 = argv[3];
  151. URL = argv[1]; /* provide this to the rest */
  152. fprintf(stderr, "URL: %s\n", URL);
  153. result = test(URL);
  154. #ifdef USE_NSS
  155. if(PR_Initialized())
  156. /* prevent valgrind from reporting possibly lost memory (fd cache, ...) */
  157. PR_Cleanup();
  158. #endif
  159. #ifdef WIN32
  160. /* flush buffers of all streams regardless of mode */
  161. _flushall();
  162. #endif
  163. return result;
  164. }