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