xref: /aosp_15_r20/external/curl/tests/libtest/lib541.c (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker /***************************************************************************
2*6236dae4SAndroid Build Coastguard Worker  *                                  _   _ ____  _
3*6236dae4SAndroid Build Coastguard Worker  *  Project                     ___| | | |  _ \| |
4*6236dae4SAndroid Build Coastguard Worker  *                             / __| | | | |_) | |
5*6236dae4SAndroid Build Coastguard Worker  *                            | (__| |_| |  _ <| |___
6*6236dae4SAndroid Build Coastguard Worker  *                             \___|\___/|_| \_\_____|
7*6236dae4SAndroid Build Coastguard Worker  *
8*6236dae4SAndroid Build Coastguard Worker  * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
9*6236dae4SAndroid Build Coastguard Worker  *
10*6236dae4SAndroid Build Coastguard Worker  * This software is licensed as described in the file COPYING, which
11*6236dae4SAndroid Build Coastguard Worker  * you should have received as part of this distribution. The terms
12*6236dae4SAndroid Build Coastguard Worker  * are also available at https://curl.se/docs/copyright.html.
13*6236dae4SAndroid Build Coastguard Worker  *
14*6236dae4SAndroid Build Coastguard Worker  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15*6236dae4SAndroid Build Coastguard Worker  * copies of the Software, and permit persons to whom the Software is
16*6236dae4SAndroid Build Coastguard Worker  * furnished to do so, under the terms of the COPYING file.
17*6236dae4SAndroid Build Coastguard Worker  *
18*6236dae4SAndroid Build Coastguard Worker  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19*6236dae4SAndroid Build Coastguard Worker  * KIND, either express or implied.
20*6236dae4SAndroid Build Coastguard Worker  *
21*6236dae4SAndroid Build Coastguard Worker  * SPDX-License-Identifier: curl
22*6236dae4SAndroid Build Coastguard Worker  *
23*6236dae4SAndroid Build Coastguard Worker  ***************************************************************************/
24*6236dae4SAndroid Build Coastguard Worker #include "test.h"
25*6236dae4SAndroid Build Coastguard Worker 
26*6236dae4SAndroid Build Coastguard Worker #ifdef HAVE_FCNTL_H
27*6236dae4SAndroid Build Coastguard Worker #include <fcntl.h>
28*6236dae4SAndroid Build Coastguard Worker #endif
29*6236dae4SAndroid Build Coastguard Worker 
30*6236dae4SAndroid Build Coastguard Worker #include "memdebug.h"
31*6236dae4SAndroid Build Coastguard Worker 
32*6236dae4SAndroid Build Coastguard Worker /*
33*6236dae4SAndroid Build Coastguard Worker  * Two FTP uploads, the second with no content sent.
34*6236dae4SAndroid Build Coastguard Worker  */
35*6236dae4SAndroid Build Coastguard Worker 
test(char * URL)36*6236dae4SAndroid Build Coastguard Worker CURLcode test(char *URL)
37*6236dae4SAndroid Build Coastguard Worker {
38*6236dae4SAndroid Build Coastguard Worker   CURL *curl;
39*6236dae4SAndroid Build Coastguard Worker   CURLcode res = CURLE_OK;
40*6236dae4SAndroid Build Coastguard Worker   FILE *hd_src;
41*6236dae4SAndroid Build Coastguard Worker   int hd;
42*6236dae4SAndroid Build Coastguard Worker   struct_stat file_info;
43*6236dae4SAndroid Build Coastguard Worker 
44*6236dae4SAndroid Build Coastguard Worker   if(!libtest_arg2) {
45*6236dae4SAndroid Build Coastguard Worker     fprintf(stderr, "Usage: <url> <file-to-upload>\n");
46*6236dae4SAndroid Build Coastguard Worker     return TEST_ERR_USAGE;
47*6236dae4SAndroid Build Coastguard Worker   }
48*6236dae4SAndroid Build Coastguard Worker 
49*6236dae4SAndroid Build Coastguard Worker   hd_src = fopen(libtest_arg2, "rb");
50*6236dae4SAndroid Build Coastguard Worker   if(!hd_src) {
51*6236dae4SAndroid Build Coastguard Worker     fprintf(stderr, "fopen failed with error: %d %s\n",
52*6236dae4SAndroid Build Coastguard Worker             errno, strerror(errno));
53*6236dae4SAndroid Build Coastguard Worker     fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
54*6236dae4SAndroid Build Coastguard Worker     return (CURLcode)-2; /* if this happens things are major weird */
55*6236dae4SAndroid Build Coastguard Worker   }
56*6236dae4SAndroid Build Coastguard Worker 
57*6236dae4SAndroid Build Coastguard Worker   /* get the file size of the local file */
58*6236dae4SAndroid Build Coastguard Worker   hd = fstat(fileno(hd_src), &file_info);
59*6236dae4SAndroid Build Coastguard Worker   if(hd == -1) {
60*6236dae4SAndroid Build Coastguard Worker     /* can't open file, bail out */
61*6236dae4SAndroid Build Coastguard Worker     fprintf(stderr, "fstat() failed with error: %d %s\n",
62*6236dae4SAndroid Build Coastguard Worker             errno, strerror(errno));
63*6236dae4SAndroid Build Coastguard Worker     fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
64*6236dae4SAndroid Build Coastguard Worker     fclose(hd_src);
65*6236dae4SAndroid Build Coastguard Worker     return TEST_ERR_MAJOR_BAD;
66*6236dae4SAndroid Build Coastguard Worker   }
67*6236dae4SAndroid Build Coastguard Worker 
68*6236dae4SAndroid Build Coastguard Worker   if(!file_info.st_size) {
69*6236dae4SAndroid Build Coastguard Worker     fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
70*6236dae4SAndroid Build Coastguard Worker     fclose(hd_src);
71*6236dae4SAndroid Build Coastguard Worker     return TEST_ERR_MAJOR_BAD;
72*6236dae4SAndroid Build Coastguard Worker   }
73*6236dae4SAndroid Build Coastguard Worker 
74*6236dae4SAndroid Build Coastguard Worker   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
75*6236dae4SAndroid Build Coastguard Worker     fprintf(stderr, "curl_global_init() failed\n");
76*6236dae4SAndroid Build Coastguard Worker     fclose(hd_src);
77*6236dae4SAndroid Build Coastguard Worker     return TEST_ERR_MAJOR_BAD;
78*6236dae4SAndroid Build Coastguard Worker   }
79*6236dae4SAndroid Build Coastguard Worker 
80*6236dae4SAndroid Build Coastguard Worker   /* get a curl handle */
81*6236dae4SAndroid Build Coastguard Worker   curl = curl_easy_init();
82*6236dae4SAndroid Build Coastguard Worker   if(!curl) {
83*6236dae4SAndroid Build Coastguard Worker     fprintf(stderr, "curl_easy_init() failed\n");
84*6236dae4SAndroid Build Coastguard Worker     curl_global_cleanup();
85*6236dae4SAndroid Build Coastguard Worker     fclose(hd_src);
86*6236dae4SAndroid Build Coastguard Worker     return TEST_ERR_MAJOR_BAD;
87*6236dae4SAndroid Build Coastguard Worker   }
88*6236dae4SAndroid Build Coastguard Worker 
89*6236dae4SAndroid Build Coastguard Worker   /* enable uploading */
90*6236dae4SAndroid Build Coastguard Worker   test_setopt(curl, CURLOPT_UPLOAD, 1L);
91*6236dae4SAndroid Build Coastguard Worker 
92*6236dae4SAndroid Build Coastguard Worker   /* enable verbose */
93*6236dae4SAndroid Build Coastguard Worker   test_setopt(curl, CURLOPT_VERBOSE, 1L);
94*6236dae4SAndroid Build Coastguard Worker 
95*6236dae4SAndroid Build Coastguard Worker   /* specify target */
96*6236dae4SAndroid Build Coastguard Worker   test_setopt(curl, CURLOPT_URL, URL);
97*6236dae4SAndroid Build Coastguard Worker 
98*6236dae4SAndroid Build Coastguard Worker   /* now specify which file to upload */
99*6236dae4SAndroid Build Coastguard Worker   test_setopt(curl, CURLOPT_READDATA, hd_src);
100*6236dae4SAndroid Build Coastguard Worker 
101*6236dae4SAndroid Build Coastguard Worker   /* Now run off and do what you've been told! */
102*6236dae4SAndroid Build Coastguard Worker   res = curl_easy_perform(curl);
103*6236dae4SAndroid Build Coastguard Worker   if(res)
104*6236dae4SAndroid Build Coastguard Worker     goto test_cleanup;
105*6236dae4SAndroid Build Coastguard Worker 
106*6236dae4SAndroid Build Coastguard Worker   /* and now upload the exact same again, but without rewinding so it already
107*6236dae4SAndroid Build Coastguard Worker      is at end of file */
108*6236dae4SAndroid Build Coastguard Worker   res = curl_easy_perform(curl);
109*6236dae4SAndroid Build Coastguard Worker 
110*6236dae4SAndroid Build Coastguard Worker test_cleanup:
111*6236dae4SAndroid Build Coastguard Worker 
112*6236dae4SAndroid Build Coastguard Worker   /* close the local file */
113*6236dae4SAndroid Build Coastguard Worker   fclose(hd_src);
114*6236dae4SAndroid Build Coastguard Worker 
115*6236dae4SAndroid Build Coastguard Worker   curl_easy_cleanup(curl);
116*6236dae4SAndroid Build Coastguard Worker   curl_global_cleanup();
117*6236dae4SAndroid Build Coastguard Worker 
118*6236dae4SAndroid Build Coastguard Worker   return res;
119*6236dae4SAndroid Build Coastguard Worker }
120