Imported Upstream version 7.59.0
[platform/upstream/curl.git] / tests / libtest / lib525.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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.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 #include <fcntl.h>
25
26 #include "testutil.h"
27 #include "warnless.h"
28 #include "memdebug.h"
29
30 #define TEST_HANG_TIMEOUT 60 * 1000
31
32 int test(char *URL)
33 {
34   int res = 0;
35   CURL *curl = NULL;
36   FILE *hd_src = NULL;
37   int hd;
38   struct_stat file_info;
39   CURLM *m = NULL;
40   int running;
41
42   start_test_timing();
43
44   if(!libtest_arg2) {
45 #ifdef LIB529
46     /* test 529 */
47     fprintf(stderr, "Usage: lib529 [url] [uploadfile]\n");
48 #else
49     /* test 525 */
50     fprintf(stderr, "Usage: lib525 [url] [uploadfile]\n");
51 #endif
52     return TEST_ERR_USAGE;
53   }
54
55   hd_src = fopen(libtest_arg2, "rb");
56   if(NULL == hd_src) {
57     fprintf(stderr, "fopen failed with error: %d (%s)\n",
58             errno, strerror(errno));
59     fprintf(stderr, "Error opening file: (%s)\n", libtest_arg2);
60     return TEST_ERR_FOPEN;
61   }
62
63   /* get the file size of the local file */
64   hd = fstat(fileno(hd_src), &file_info);
65   if(hd == -1) {
66     /* can't open file, bail out */
67     fprintf(stderr, "fstat() failed with error: %d (%s)\n",
68             errno, strerror(errno));
69     fprintf(stderr, "ERROR: cannot open file (%s)\n", libtest_arg2);
70     fclose(hd_src);
71     return TEST_ERR_FSTAT;
72   }
73
74   res_global_init(CURL_GLOBAL_ALL);
75   if(res) {
76     fclose(hd_src);
77     return res;
78   }
79
80   easy_init(curl);
81
82   /* enable uploading */
83   easy_setopt(curl, CURLOPT_UPLOAD, 1L);
84
85   /* specify target */
86   easy_setopt(curl, CURLOPT_URL, URL);
87
88   /* go verbose */
89   easy_setopt(curl, CURLOPT_VERBOSE, 1L);
90
91   /* use active FTP */
92   easy_setopt(curl, CURLOPT_FTPPORT, "-");
93
94   /* now specify which file to upload */
95   easy_setopt(curl, CURLOPT_READDATA, hd_src);
96
97   /* NOTE: if you want this code to work on Windows with libcurl as a DLL, you
98      MUST also provide a read callback with CURLOPT_READFUNCTION. Failing to
99      do so will give you a crash since a DLL may not use the variable's memory
100      when passed in to it from an app like this. */
101
102   /* Set the size of the file to upload (optional).  If you give a *_LARGE
103      option you MUST make sure that the type of the passed-in argument is a
104      curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
105      make sure that to pass in a type 'long' argument. */
106   easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
107
108   multi_init(m);
109
110   multi_add_handle(m, curl);
111
112   for(;;) {
113     struct timeval interval;
114     fd_set rd, wr, exc;
115     int maxfd = -99;
116
117     interval.tv_sec = 1;
118     interval.tv_usec = 0;
119
120     multi_perform(m, &running);
121
122     abort_on_test_timeout();
123
124     if(!running)
125       break; /* done */
126
127     FD_ZERO(&rd);
128     FD_ZERO(&wr);
129     FD_ZERO(&exc);
130
131     multi_fdset(m, &rd, &wr, &exc, &maxfd);
132
133     /* At this point, maxfd is guaranteed to be greater or equal than -1. */
134
135     select_test(maxfd + 1, &rd, &wr, &exc, &interval);
136
137     abort_on_test_timeout();
138   }
139
140 test_cleanup:
141
142 #ifdef LIB529
143   /* test 529 */
144   /* proper cleanup sequence - type PA */
145   curl_multi_remove_handle(m, curl);
146   curl_multi_cleanup(m);
147   curl_easy_cleanup(curl);
148   curl_global_cleanup();
149 #else
150   /* test 525 */
151   /* proper cleanup sequence - type PB */
152   curl_multi_remove_handle(m, curl);
153   curl_easy_cleanup(curl);
154   curl_multi_cleanup(m);
155   curl_global_cleanup();
156 #endif
157
158   /* close the local file */
159   fclose(hd_src);
160
161   return res;
162 }