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