Home | History | Annotate | Download | only in libtest
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2012, 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 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 
     24 /* lib591 is used for test cases 591, 592, 593 and 594 */
     25 
     26 #ifdef HAVE_LIMITS_H
     27 #include <limits.h>
     28 #endif
     29 
     30 #include <fcntl.h>
     31 
     32 #include "testutil.h"
     33 #include "warnless.h"
     34 #include "memdebug.h"
     35 
     36 #define TEST_HANG_TIMEOUT 60 * 1000
     37 
     38 int test(char *URL)
     39 {
     40   CURL *easy = NULL;
     41   CURLM *multi = NULL;
     42   int res = 0;
     43   int running;
     44   int msgs_left;
     45   CURLMsg *msg;
     46   FILE *upload = NULL;
     47   int error;
     48 
     49   start_test_timing();
     50 
     51   upload = fopen(libtest_arg3, "rb");
     52   if(!upload) {
     53     error = ERRNO;
     54     fprintf(stderr, "fopen() failed with error: %d (%s)\n",
     55             error, strerror(error));
     56     fprintf(stderr, "Error opening file: (%s)\n", libtest_arg3);
     57     return TEST_ERR_FOPEN;
     58   }
     59 
     60   res_global_init(CURL_GLOBAL_ALL);
     61   if(res) {
     62     fclose(upload);
     63     return res;
     64   }
     65 
     66   easy_init(easy);
     67 
     68   /* go verbose */
     69   easy_setopt(easy, CURLOPT_VERBOSE, 1L);
     70 
     71   /* specify target */
     72   easy_setopt(easy, CURLOPT_URL, URL);
     73 
     74   /* enable uploading */
     75   easy_setopt(easy, CURLOPT_UPLOAD, 1L);
     76 
     77   /* data pointer for the file read function */
     78   easy_setopt(easy, CURLOPT_READDATA, upload);
     79 
     80   /* use active mode FTP */
     81   easy_setopt(easy, CURLOPT_FTPPORT, "-");
     82 
     83   /* server connection timeout */
     84   easy_setopt(easy, CURLOPT_ACCEPTTIMEOUT_MS,
     85               strtol(libtest_arg2, NULL, 10)*1000);
     86 
     87   multi_init(multi);
     88 
     89   multi_add_handle(multi, easy);
     90 
     91   for(;;) {
     92     struct timeval interval;
     93     fd_set fdread;
     94     fd_set fdwrite;
     95     fd_set fdexcep;
     96     long timeout = -99;
     97     int maxfd = -99;
     98 
     99     multi_perform(multi, &running);
    100 
    101     abort_on_test_timeout();
    102 
    103     if(!running)
    104       break; /* done */
    105 
    106     FD_ZERO(&fdread);
    107     FD_ZERO(&fdwrite);
    108     FD_ZERO(&fdexcep);
    109 
    110     multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
    111 
    112     /* At this point, maxfd is guaranteed to be greater or equal than -1. */
    113 
    114     multi_timeout(multi, &timeout);
    115 
    116     /* At this point, timeout is guaranteed to be greater or equal than -1. */
    117 
    118     if(timeout != -1L) {
    119       int itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
    120       interval.tv_sec = itimeout/1000;
    121       interval.tv_usec = (itimeout%1000)*1000;
    122     }
    123     else {
    124       interval.tv_sec = 0;
    125       interval.tv_usec = 100000L; /* 100 ms */
    126     }
    127 
    128     select_test(maxfd+1, &fdread, &fdwrite, &fdexcep, &interval);
    129 
    130     abort_on_test_timeout();
    131   }
    132 
    133   msg = curl_multi_info_read(multi, &msgs_left);
    134   if(msg)
    135     res = msg->data.result;
    136 
    137 test_cleanup:
    138 
    139   /* undocumented cleanup sequence - type UA */
    140 
    141   curl_multi_cleanup(multi);
    142   curl_easy_cleanup(easy);
    143   curl_global_cleanup();
    144 
    145   /* close the local file */
    146   fclose(upload);
    147 
    148   return res;
    149 }
    150