2fc85b97d4ee307bd43a1666810002a792c140e5
[external/curl.git] / tests / libtest / lib541.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include "test.h"
11
12 #ifdef HAVE_SYS_SOCKET_H
13 #include <sys/socket.h>
14 #endif
15 #ifdef HAVE_SYS_TYPES_H
16 #include <sys/types.h>
17 #endif
18 #ifdef HAVE_SYS_STAT_H
19 #include <sys/stat.h>
20 #endif
21 #ifdef HAVE_FCNTL_H
22 #include <fcntl.h>
23 #endif
24
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28
29 #include "memdebug.h"
30
31 /*
32  * Two FTP uploads, the second with no content sent.
33  */
34
35 int test(char *URL)
36 {
37   CURL *curl;
38   CURLcode res = CURLE_OK;
39   FILE *hd_src ;
40   int hd ;
41   struct_stat file_info;
42   int error;
43
44   if (!libtest_arg2) {
45     fprintf(stderr, "Usage: <url> <file-to-upload>\n");
46     return -1;
47   }
48
49   hd_src = fopen(libtest_arg2, "rb");
50   if(NULL == hd_src) {
51     error = ERRNO;
52     fprintf(stderr, "fopen() failed with error: %d %s\n",
53             error, strerror(error));
54     fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
55     return -2; /* if this happens things are major weird */
56   }
57
58   /* get the file size of the local file */
59   hd = fstat(fileno(hd_src), &file_info);
60   if(hd == -1) {
61     /* can't open file, bail out */
62     error = ERRNO;
63     fprintf(stderr, "fstat() failed with error: %d %s\n",
64             error, strerror(error));
65     fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
66     fclose(hd_src);
67     return -1;
68   }
69
70   if(! file_info.st_size) {
71     fprintf(stderr, "ERROR: file %s has zero size!\n", libtest_arg2);
72     fclose(hd_src);
73     return -4;
74   }
75
76   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
77     fprintf(stderr, "curl_global_init() failed\n");
78     fclose(hd_src);
79     return TEST_ERR_MAJOR_BAD;
80   }
81
82   /* get a curl handle */
83   if ((curl = curl_easy_init()) == NULL) {
84     fprintf(stderr, "curl_easy_init() failed\n");
85     curl_global_cleanup();
86     fclose(hd_src);
87     return TEST_ERR_MAJOR_BAD;
88   }
89
90   /* enable uploading */
91   test_setopt(curl, CURLOPT_UPLOAD, 1L);
92
93   /* enable verbose */
94   test_setopt(curl, CURLOPT_VERBOSE, 1L);
95
96   /* specify target */
97   test_setopt(curl,CURLOPT_URL, URL);
98
99   /* now specify which file to upload */
100   test_setopt(curl, CURLOPT_INFILE, hd_src);
101
102   /* Now run off and do what you've been told! */
103   res = curl_easy_perform(curl);
104
105   /* and now upload the exact same again, but without rewinding so it already
106      is at end of file */
107   res = curl_easy_perform(curl);
108
109 test_cleanup:
110
111   /* close the local file */
112   fclose(hd_src);
113
114   curl_easy_cleanup(curl);
115   curl_global_cleanup();
116
117   return res;
118 }