Git init
[external/curl.git] / tests / libtest / lib536.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 static CURLMcode perform(CURLM * multi)
23 {
24   int handles;
25   CURLMcode code;
26   fd_set fdread, fdwrite, fdexcep;
27   struct timeval mp_start;
28   char mp_timedout = FALSE;
29
30   mp_timedout = FALSE;
31   mp_start = tutil_tvnow();
32
33   for (;;) {
34     static struct timeval timeout = /* 100 ms */ { 0, 100000L };
35     int maxfd = -1;
36
37     code = curl_multi_perform(multi, &handles);
38     if (tutil_tvdiff(tutil_tvnow(), mp_start) >
39         MULTI_PERFORM_HANG_TIMEOUT) {
40       mp_timedout = TRUE;
41       break;
42     }
43     if (handles <= 0)
44       return CURLM_OK;
45
46     switch (code) {
47       case CURLM_OK:
48         break;
49       case CURLM_CALL_MULTI_PERFORM:
50         continue;
51       default:
52         return code;
53     }
54
55     FD_ZERO(&fdread);
56     FD_ZERO(&fdwrite);
57     FD_ZERO(&fdexcep);
58     curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
59
60     /* In a real-world program you OF COURSE check the return code of the
61        function calls.  On success, the value of maxfd is guaranteed to be
62        greater or equal than -1.  We call select(maxfd + 1, ...), specially in
63        case of (maxfd == -1), we call select(0, ...), which is basically equal
64        to sleep. */
65
66     if (select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout) == -1)
67       return (CURLMcode) ~CURLM_OK;
68   }
69
70   /* We only reach this point if (mp_timedout) */
71   if (mp_timedout) fprintf(stderr, "mp_timedout\n");
72   fprintf(stderr, "ABORTING TEST, since it seems "
73           "that it would have run forever.\n");
74   return (CURLMcode) ~CURLM_OK;
75 }
76
77 int test(char *URL)
78 {
79   CURLM *multi;
80   CURL *easy;
81   int res = 0;
82
83   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
84     fprintf(stderr, "curl_global_init() failed\n");
85     return TEST_ERR_MAJOR_BAD;
86   }
87
88   if ((multi = curl_multi_init()) == NULL) {
89     fprintf(stderr, "curl_multi_init() failed\n");
90     curl_global_cleanup();
91     return TEST_ERR_MAJOR_BAD;
92   }
93
94   if ((easy = curl_easy_init()) == NULL) {
95     fprintf(stderr, "curl_easy_init() failed\n");
96     curl_multi_cleanup(multi);
97     curl_global_cleanup();
98     return TEST_ERR_MAJOR_BAD;
99   }
100
101   curl_multi_setopt(multi, CURLMOPT_PIPELINING, 1L);
102
103   test_setopt(easy, CURLOPT_WRITEFUNCTION, fwrite);
104   test_setopt(easy, CURLOPT_FAILONERROR, 1L);
105   test_setopt(easy, CURLOPT_URL, URL);
106
107   if (curl_multi_add_handle(multi, easy) != CURLM_OK) {
108     printf("curl_multi_add_handle() failed\n");
109     res = TEST_ERR_MAJOR_BAD;
110   } else {
111     if (perform(multi) != CURLM_OK)
112       printf("retrieve 1 failed\n");
113
114     curl_multi_remove_handle(multi, easy);
115   }
116   curl_easy_reset(easy);
117
118   test_setopt(easy, CURLOPT_FAILONERROR, 1L);
119   test_setopt(easy, CURLOPT_URL, libtest_arg2);
120
121   if (curl_multi_add_handle(multi, easy) != CURLM_OK) {
122     printf("curl_multi_add_handle() 2 failed\n");
123     res = TEST_ERR_MAJOR_BAD;
124   } else {
125     if (perform(multi) != CURLM_OK)
126       printf("retrieve 2 failed\n");
127
128     curl_multi_remove_handle(multi, easy);
129   }
130
131 test_cleanup:
132
133   curl_easy_cleanup(easy);
134   curl_multi_cleanup(multi);
135   curl_global_cleanup();
136
137   printf("Finished!\n");
138
139   return res;
140 }