Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / tests / libtest / lib525.c
blob4131e699374f9db2509c41467a015a3caeadf9ca
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: lib525.c,v 1.1.1.1 2008-09-23 16:32:06 hoffman Exp $
9 */
11 #include "test.h"
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
17 #include "testutil.h"
19 #define MAIN_LOOP_HANG_TIMEOUT 90 * 1000
20 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
22 int test(char *URL)
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;
33 struct timeval ml_start;
34 struct timeval mp_start;
35 char ml_timedout = FALSE;
36 char mp_timedout = FALSE;
38 if (!libtest_arg2) {
39 fprintf(stderr, "Usage: lib525 [url] [uploadfile]\n");
40 return -1;
43 /* get the file size of the local file */
44 hd = open(libtest_arg2, O_RDONLY) ;
45 fstat(hd, &file_info);
46 close(hd) ;
48 /* get a FILE * of the same file, could also be made with
49 fdopen() from the previous descriptor, but hey this is just
50 an example! */
51 hd_src = fopen(libtest_arg2, "rb");
52 if(NULL == hd_src) {
53 error = ERRNO;
54 fprintf(stderr, "fopen() failed with error: %d %s\n",
55 error, strerror(error));
56 fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
57 return TEST_ERR_MAJOR_BAD;
60 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
61 fprintf(stderr, "curl_global_init() failed\n");
62 fclose(hd_src);
63 return TEST_ERR_MAJOR_BAD;
66 if ((curl = curl_easy_init()) == NULL) {
67 fprintf(stderr, "curl_easy_init() failed\n");
68 fclose(hd_src);
69 curl_global_cleanup();
70 return TEST_ERR_MAJOR_BAD;
73 /* enable uploading */
74 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
76 /* specify target */
77 curl_easy_setopt(curl,CURLOPT_URL, URL);
79 /* go verbose */
80 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
82 /* use active FTP */
83 curl_easy_setopt(curl, CURLOPT_FTPPORT, "-");
85 /* now specify which file to upload */
86 curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
88 /* NOTE: if you want this code to work on Windows with libcurl as a DLL, you
89 MUST also provide a read callback with CURLOPT_READFUNCTION. Failing to
90 do so will give you a crash since a DLL may not use the variable's memory
91 when passed in to it from an app like this. */
93 /* Set the size of the file to upload (optional). If you give a *_LARGE
94 option you MUST make sure that the type of the passed-in argument is a
95 curl_off_t. If you use CURLOPT_INFILESIZE (without _LARGE) you must
96 make sure that to pass in a type 'long' argument. */
97 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
98 (curl_off_t)file_info.st_size);
100 if ((m = curl_multi_init()) == NULL) {
101 fprintf(stderr, "curl_multi_init() failed\n");
102 curl_easy_cleanup(curl);
103 curl_global_cleanup();
104 fclose(hd_src);
105 return TEST_ERR_MAJOR_BAD;
108 if ((res = (int)curl_multi_add_handle(m, curl)) != CURLM_OK) {
109 fprintf(stderr, "curl_multi_add_handle() failed, "
110 "with code %d\n", res);
111 curl_multi_cleanup(m);
112 curl_easy_cleanup(curl);
113 curl_global_cleanup();
114 fclose(hd_src);
115 return TEST_ERR_MAJOR_BAD;
118 ml_timedout = FALSE;
119 ml_start = tutil_tvnow();
121 while (!done) {
122 fd_set rd, wr, exc;
123 int max_fd;
124 struct timeval interval;
126 interval.tv_sec = 1;
127 interval.tv_usec = 0;
129 if (tutil_tvdiff(tutil_tvnow(), ml_start) >
130 MAIN_LOOP_HANG_TIMEOUT) {
131 ml_timedout = TRUE;
132 break;
134 mp_timedout = FALSE;
135 mp_start = tutil_tvnow();
137 while (res == CURLM_CALL_MULTI_PERFORM) {
138 res = (int)curl_multi_perform(m, &running);
139 if (tutil_tvdiff(tutil_tvnow(), mp_start) >
140 MULTI_PERFORM_HANG_TIMEOUT) {
141 mp_timedout = TRUE;
142 break;
144 if (running <= 0) {
145 done = TRUE;
146 break;
149 if (mp_timedout || done)
150 break;
152 if (res != CURLM_OK) {
153 fprintf(stderr, "not okay???\n");
154 break;
157 FD_ZERO(&rd);
158 FD_ZERO(&wr);
159 FD_ZERO(&exc);
160 max_fd = 0;
162 if (curl_multi_fdset(m, &rd, &wr, &exc, &max_fd) != CURLM_OK) {
163 fprintf(stderr, "unexpected failured of fdset.\n");
164 res = 189;
165 break;
168 if (select_test(max_fd+1, &rd, &wr, &exc, &interval) == -1) {
169 fprintf(stderr, "bad select??\n");
170 res = 195;
171 break;
174 res = CURLM_CALL_MULTI_PERFORM;
177 if (ml_timedout || mp_timedout) {
178 if (ml_timedout) fprintf(stderr, "ml_timedout\n");
179 if (mp_timedout) fprintf(stderr, "mp_timedout\n");
180 fprintf(stderr, "ABORTING TEST, since it seems "
181 "that it would have run forever.\n");
182 res = TEST_ERR_RUNS_FOREVER;
185 #ifdef LIB529
186 /* test 529 */
187 curl_multi_remove_handle(m, curl);
188 curl_multi_cleanup(m);
189 curl_easy_cleanup(curl);
190 #else
191 /* test 525 */
192 curl_multi_remove_handle(m, curl);
193 curl_easy_cleanup(curl);
194 curl_multi_cleanup(m);
195 #endif
197 fclose(hd_src); /* close the local file */
199 curl_global_cleanup();
200 return res;