Home | History | Annotate | Download | only in libtest
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2016, 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 #ifdef HAVE_LIMITS_H
     25 #include <limits.h>
     26 #endif
     27 
     28 #include "testutil.h"
     29 #include "warnless.h"
     30 #include "memdebug.h"
     31 
     32 #define TEST_HANG_TIMEOUT 5 * 1000
     33 
     34 /*
     35  * Test case for below scenario:
     36  *   - Connect to an FTP server using CONNECT_ONLY option
     37  *   - transfer some files with re-using the connection (omitted in test case)
     38  *   - Disconnect from FTP server with sending QUIT command
     39  *
     40  * The test case originated for verifying CONNECT_ONLY option shall not
     41  * block after protocol connect is done, but it returns the message
     42  * with function curl_multi_info_read().
     43  */
     44 
     45 enum {
     46   CONNECT_ONLY_PHASE = 0,
     47   QUIT_PHASE,
     48   LAST_PHASE
     49 };
     50 
     51 int test(char *URL)
     52 {
     53   CURL *easy = NULL;
     54   CURLM *multi = NULL;
     55   int res = 0;
     56   int running;
     57   int msgs_left;
     58   int phase;
     59   CURLMsg *msg;
     60 
     61   start_test_timing();
     62 
     63   res_global_init(CURL_GLOBAL_ALL);
     64   if(res) {
     65     return res;
     66   }
     67 
     68   easy_init(easy);
     69 
     70   multi_init(multi);
     71 
     72   for(phase = CONNECT_ONLY_PHASE; phase < LAST_PHASE; ++phase) {
     73     /* go verbose */
     74     easy_setopt(easy, CURLOPT_VERBOSE, 1L);
     75 
     76     /* specify target */
     77     easy_setopt(easy, CURLOPT_URL, URL);
     78 
     79     /* enable 'CONNECT_ONLY' option when in connect phase */
     80     if(phase == CONNECT_ONLY_PHASE)
     81       easy_setopt(easy, CURLOPT_CONNECT_ONLY, 1L);
     82 
     83     /* enable 'NOBODY' option to send 'QUIT' command in quit phase */
     84     if(phase == QUIT_PHASE) {
     85       easy_setopt(easy, CURLOPT_CONNECT_ONLY, 0L);
     86       easy_setopt(easy, CURLOPT_NOBODY, 1L);
     87       easy_setopt(easy, CURLOPT_FORBID_REUSE, 1L);
     88     }
     89 
     90     multi_add_handle(multi, easy);
     91 
     92     for(;;) {
     93       struct timeval interval;
     94       fd_set fdread;
     95       fd_set fdwrite;
     96       fd_set fdexcep;
     97       long timeout = -99;
     98       int maxfd = -99;
     99 
    100       multi_perform(multi, &running);
    101 
    102       abort_on_test_timeout();
    103 
    104       if(!running)
    105         break; /* done */
    106 
    107       FD_ZERO(&fdread);
    108       FD_ZERO(&fdwrite);
    109       FD_ZERO(&fdexcep);
    110 
    111       multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
    112 
    113       /* At this point, maxfd is guaranteed to be greater or equal than -1. */
    114 
    115       multi_timeout(multi, &timeout);
    116 
    117       /* At this point, timeout is guaranteed to be greater or equal than
    118          -1. */
    119 
    120       if(timeout != -1L) {
    121         int itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
    122         interval.tv_sec = itimeout/1000;
    123         interval.tv_usec = (itimeout%1000)*1000;
    124       }
    125       else {
    126         interval.tv_sec = TEST_HANG_TIMEOUT/1000+1;
    127         interval.tv_usec = 0;
    128       }
    129 
    130       select_test(maxfd+1, &fdread, &fdwrite, &fdexcep, &interval);
    131 
    132       abort_on_test_timeout();
    133     }
    134 
    135     msg = curl_multi_info_read(multi, &msgs_left);
    136     if(msg)
    137       res = msg->data.result;
    138 
    139     multi_remove_handle(multi, easy);
    140   }
    141 
    142 test_cleanup:
    143 
    144   /* undocumented cleanup sequence - type UA */
    145 
    146   curl_multi_cleanup(multi);
    147   curl_easy_cleanup(easy);
    148   curl_global_cleanup();
    149 
    150   return res;
    151 }
    152