first.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, 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.haxx.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. 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((1000*tv->tv_sec) + (DWORD)(((double)tv->tv_usec)/1000.0));
  54. return 0;
  55. }
  56. #endif
  57. return select(nfds, rd, wr, exc, tv);
  58. }
  59. void wait_ms(int ms)
  60. {
  61. struct timeval t;
  62. t.tv_sec = ms/1000;
  63. ms -= (int)t.tv_sec * 1000;
  64. t.tv_usec = ms * 1000;
  65. select_wrapper(0, NULL, NULL, NULL, &t);
  66. }
  67. char *libtest_arg2 = NULL;
  68. char *libtest_arg3 = NULL;
  69. int test_argc;
  70. char **test_argv;
  71. struct timeval tv_test_start; /* for test timing */
  72. #ifdef UNITTESTS
  73. int unitfail; /* for unittests */
  74. #endif
  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 *buffer, 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 ", buffer[i]);
  116. return dump;
  117. }
  118. int main(int argc, char **argv)
  119. {
  120. char *URL;
  121. int result;
  122. #ifdef O_BINARY
  123. # ifdef __HIGHC__
  124. _setmode(stdout, O_BINARY);
  125. # else
  126. setmode(fileno(stdout), O_BINARY);
  127. # endif
  128. #endif
  129. memory_tracking_init();
  130. /*
  131. * Setup proper locale from environment. This is needed to enable locale-
  132. * specific behaviour by the C library in order to test for undesired side
  133. * effects that could cause in libcurl.
  134. */
  135. #ifdef HAVE_SETLOCALE
  136. setlocale(LC_ALL, "");
  137. #endif
  138. if(argc< 2) {
  139. fprintf(stderr, "Pass URL as argument please\n");
  140. return 1;
  141. }
  142. test_argc = argc;
  143. test_argv = argv;
  144. if(argc>2)
  145. libtest_arg2 = argv[2];
  146. if(argc>3)
  147. libtest_arg3 = argv[3];
  148. URL = argv[1]; /* provide this to the rest */
  149. fprintf(stderr, "URL: %s\n", URL);
  150. result = test(URL);
  151. #ifdef USE_NSS
  152. if(PR_Initialized())
  153. /* prevent valgrind from reporting possibly lost memory (fd cache, ...) */
  154. PR_Cleanup();
  155. #endif
  156. return result;
  157. }