521720e95835b1828e120045f261a54be5db22b2
[external/curl.git] / tests / libtest / lib575.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 /* 3x download!
20  * 1. normal
21  * 2. dup handle
22  * 3. with multi interface
23  */
24
25 int test(char *URL)
26 {
27   CURLMcode m;
28   CURL *handle = NULL, *duphandle;
29   CURLM *mhandle = NULL;
30   int res = 0;
31   int still_running = 0;
32
33   if(curl_global_init(CURL_GLOBAL_ALL)) {
34     fprintf(stderr, "curl_global_init() failed\n");
35     goto test_cleanup;
36   }
37
38   handle = curl_easy_init();
39   if(!handle) {
40     res = CURLE_OUT_OF_MEMORY;
41     goto test_cleanup;
42   }
43
44   test_setopt(handle, CURLOPT_URL, URL);
45   test_setopt(handle, CURLOPT_WILDCARDMATCH, 1L);
46   test_setopt(handle, CURLOPT_VERBOSE, 1L);
47
48   res = curl_easy_perform(handle);
49   if(res)
50     goto test_cleanup;
51
52   res = curl_easy_perform(handle);
53   if(res)
54     goto test_cleanup;
55
56   duphandle = curl_easy_duphandle(handle);
57   if(!duphandle)
58     goto test_cleanup;
59   curl_easy_cleanup(handle);
60   handle = duphandle;
61
62   mhandle = curl_multi_init();
63   if(!mhandle) {
64     fprintf(stderr, "curl_multi_init() failed\n");
65     goto test_cleanup;
66   }
67
68   curl_multi_add_handle(mhandle, handle);
69
70   while(CURLM_CALL_MULTI_PERFORM ==
71         curl_multi_perform(mhandle, &still_running));
72
73   while(still_running) {
74     static struct timeval timeout = /* 100 ms */ { 0, 100000L };
75     int rc;
76     fd_set fdread;
77     fd_set fdwrite;
78     fd_set fdexcep;
79     int max_fdset = -1;
80     FD_ZERO(&fdread);
81     FD_ZERO(&fdwrite);
82     FD_ZERO(&fdexcep);
83
84     m = curl_multi_fdset(mhandle, &fdread, &fdwrite, &fdexcep, &max_fdset);
85     if(m != CURLM_OK) {
86       fprintf(stderr, "curl_multi_fdset() error\n");
87       goto test_cleanup;
88     }
89     /* We call select(max_fdset + 1, ...), specially in case of (maxfd == -1),
90      * we call select(0, ...), which is basically equal to sleep. */
91     rc = select(max_fdset + 1, &fdread, &fdwrite, &fdexcep, &timeout);
92     if(rc == -1) {
93       fprintf(stderr, "select() error\n");
94       goto test_cleanup;
95     }
96     else {
97       while(CURLM_CALL_MULTI_PERFORM ==
98           curl_multi_perform(mhandle, &still_running));
99     }
100   }
101
102 test_cleanup:
103   if(mhandle)
104     curl_multi_cleanup(mhandle);
105   if(handle)
106     curl_easy_cleanup(handle);
107   curl_global_cleanup();
108   return res;
109 }