Git init
[external/curl.git] / tests / libtest / lib525.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include "test.h"
11
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15
16 #include "testutil.h"
17 #include "memdebug.h"
18
19 #define MAIN_LOOP_HANG_TIMEOUT     90 * 1000
20 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
21
22 int test(char *URL)
23 {
24   int res = 0;
25   CURL *curl;
26   FILE *hd_src ;
27   int hd ;
28   int error;
29   struct_stat file_info;
30   int running;
31   char done=FALSE;
32   CURLM *m = NULL;
33   struct timeval ml_start;
34   struct timeval mp_start;
35   char ml_timedout = FALSE;
36   char mp_timedout = FALSE;
37
38   if (!libtest_arg2) {
39     fprintf(stderr, "Usage: lib525 [url] [uploadfile]\n");
40     return -1;
41   }
42
43   hd_src = fopen(libtest_arg2, "rb");
44   if(NULL == hd_src) {
45     error = ERRNO;
46     fprintf(stderr, "fopen() failed with error: %d %s\n",
47             error, strerror(error));
48     fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
49     return TEST_ERR_MAJOR_BAD;
50   }
51
52   /* get the file size of the local file */
53   hd = fstat(fileno(hd_src), &file_info);
54   if(hd == -1) {
55     /* can't open file, bail out */
56     error = ERRNO;
57     fprintf(stderr, "fstat() failed with error: %d %s\n",
58             error, strerror(error));
59     fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
60     fclose(hd_src);
61     return -1;
62   }
63
64   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
65     fprintf(stderr, "curl_global_init() failed\n");
66     fclose(hd_src);
67     return TEST_ERR_MAJOR_BAD;
68   }
69
70   if ((curl = curl_easy_init()) == NULL) {
71     fprintf(stderr, "curl_easy_init() failed\n");
72     fclose(hd_src);
73     curl_global_cleanup();
74     return TEST_ERR_MAJOR_BAD;
75   }
76
77   /* enable uploading */
78   test_setopt(curl, CURLOPT_UPLOAD, 1L);
79
80   /* specify target */
81   test_setopt(curl,CURLOPT_URL, URL);
82
83   /* go verbose */
84   test_setopt(curl, CURLOPT_VERBOSE, 1L);
85
86   /* use active FTP */
87   test_setopt(curl, CURLOPT_FTPPORT, "-");
88
89   /* now specify which file to upload */
90   test_setopt(curl, CURLOPT_READDATA, hd_src);
91
92   /* NOTE: if you want this code to work on Windows with libcurl as a DLL, you
93      MUST also provide a read callback with CURLOPT_READFUNCTION. Failing to
94      do so will give you a crash since a DLL may not use the variable's memory
95      when passed in to it from an app like this. */
96
97   /* Set the size of the file to upload (optional).  If you give a *_LARGE
98      option you MUST make sure that the type of the passed-in argument is a
99      curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
100      make sure that to pass in a type 'long' argument. */
101   test_setopt(curl, CURLOPT_INFILESIZE_LARGE,
102                    (curl_off_t)file_info.st_size);
103
104   if ((m = curl_multi_init()) == NULL) {
105     fprintf(stderr, "curl_multi_init() failed\n");
106     curl_easy_cleanup(curl);
107     curl_global_cleanup();
108     fclose(hd_src);
109     return TEST_ERR_MAJOR_BAD;
110   }
111
112   if ((res = (int)curl_multi_add_handle(m, curl)) != CURLM_OK) {
113     fprintf(stderr, "curl_multi_add_handle() failed, "
114             "with code %d\n", res);
115     curl_multi_cleanup(m);
116     curl_easy_cleanup(curl);
117     curl_global_cleanup();
118     fclose(hd_src);
119     return TEST_ERR_MAJOR_BAD;
120   }
121
122   ml_timedout = FALSE;
123   ml_start = tutil_tvnow();
124
125   while (!done) {
126     fd_set rd, wr, exc;
127     int max_fd;
128     struct timeval interval;
129
130     interval.tv_sec = 1;
131     interval.tv_usec = 0;
132
133     if (tutil_tvdiff(tutil_tvnow(), ml_start) >
134         MAIN_LOOP_HANG_TIMEOUT) {
135       ml_timedout = TRUE;
136       break;
137     }
138     mp_timedout = FALSE;
139     mp_start = tutil_tvnow();
140
141     while (res == CURLM_CALL_MULTI_PERFORM) {
142       res = (int)curl_multi_perform(m, &running);
143       if (tutil_tvdiff(tutil_tvnow(), mp_start) >
144           MULTI_PERFORM_HANG_TIMEOUT) {
145         mp_timedout = TRUE;
146         break;
147       }
148       if (running <= 0) {
149         done = TRUE;
150         break;
151       }
152     }
153     if (mp_timedout || done)
154       break;
155
156     if (res != CURLM_OK) {
157       fprintf(stderr, "not okay???\n");
158       break;
159     }
160
161     FD_ZERO(&rd);
162     FD_ZERO(&wr);
163     FD_ZERO(&exc);
164     max_fd = 0;
165
166     if (curl_multi_fdset(m, &rd, &wr, &exc, &max_fd) != CURLM_OK) {
167       fprintf(stderr, "unexpected failured of fdset.\n");
168       res = 189;
169       break;
170     }
171
172     if (select_test(max_fd+1, &rd, &wr, &exc, &interval) == -1) {
173       fprintf(stderr, "bad select??\n");
174       res = 195;
175       break;
176     }
177
178     res = CURLM_CALL_MULTI_PERFORM;
179   }
180
181   if (ml_timedout || mp_timedout) {
182     if (ml_timedout) fprintf(stderr, "ml_timedout\n");
183     if (mp_timedout) fprintf(stderr, "mp_timedout\n");
184     fprintf(stderr, "ABORTING TEST, since it seems "
185             "that it would have run forever.\n");
186     res = TEST_ERR_RUNS_FOREVER;
187   }
188
189 test_cleanup:
190
191 #ifdef LIB529
192   /* test 529 */
193   if(m) {
194     curl_multi_remove_handle(m, curl);
195     curl_multi_cleanup(m);
196   }
197   curl_easy_cleanup(curl);
198 #else
199   /* test 525 */
200   if(m)
201     curl_multi_remove_handle(m, curl);
202   curl_easy_cleanup(curl);
203   if(m)
204     curl_multi_cleanup(m);
205 #endif
206
207   fclose(hd_src); /* close the local file */
208
209   curl_global_cleanup();
210   return res;
211 }