first.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #ifdef USE_WINSOCK
  62. Sleep(ms);
  63. #else
  64. struct timeval t;
  65. curlx_mstotv(&t, ms);
  66. select_wrapper(0, NULL, NULL, NULL, &t);
  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 *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 behavior 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. fprintf(stderr, "Test ended with result %d\n", result);
  152. #ifdef WIN32
  153. /* flush buffers of all streams regardless of mode */
  154. _flushall();
  155. #endif
  156. /* Regular program status codes are limited to 0..127 and 126 and 127 have
  157. * special meanings by the shell, so limit a normal return code to 125 */
  158. return result <= 125 ? result : 125;
  159. }