2300ce1095d70289cfe1b9d18f4c9c7c0cf921f3
[platform/upstream/curl.git] / tests / libtest / lib536.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id$
9  */
10
11 #include "test.h"
12
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16
17 #include "testutil.h"
18 #include "memdebug.h"
19
20 #define MAIN_LOOP_HANG_TIMEOUT     90 * 1000
21 #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
22
23 static CURLMcode perform(CURLM * multi)
24 {
25   int handles, maxfd;
26   CURLMcode code;
27   fd_set fdread, fdwrite, fdexcep;
28   struct timeval mp_start;
29   char mp_timedout = FALSE;
30
31   mp_timedout = FALSE;
32   mp_start = tutil_tvnow();
33
34   for (;;) {
35     code = curl_multi_perform(multi, &handles);
36     if (tutil_tvdiff(tutil_tvnow(), mp_start) >
37         MULTI_PERFORM_HANG_TIMEOUT) {
38       mp_timedout = TRUE;
39       break;
40     }
41     if (handles <= 0)
42       return CURLM_OK;
43
44     switch (code) {
45       case CURLM_OK:
46         break;
47       case CURLM_CALL_MULTI_PERFORM:
48         continue;
49       default:
50         return code;
51     }
52
53     FD_ZERO(&fdread);
54     FD_ZERO(&fdwrite);
55     FD_ZERO(&fdexcep);
56     curl_multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
57     if (maxfd < 0)
58       return (CURLMcode) ~CURLM_OK;
59     if (select(maxfd + 1, &fdread, &fdwrite, &fdexcep, 0) == -1)
60       return (CURLMcode) ~CURLM_OK;
61   }
62
63   /* We only reach this point if (mp_timedout) */
64   if (mp_timedout) fprintf(stderr, "mp_timedout\n");
65   fprintf(stderr, "ABORTING TEST, since it seems "
66           "that it would have run forever.\n");
67   return (CURLMcode) ~CURLM_OK;
68 }
69
70 int test(char *URL)
71 {
72   CURLM *multi;
73   CURL *easy;
74   int res = 0;
75
76   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
77     fprintf(stderr, "curl_global_init() failed\n");
78     return TEST_ERR_MAJOR_BAD;
79   }
80
81   if ((multi = curl_multi_init()) == NULL) {
82     fprintf(stderr, "curl_multi_init() failed\n");
83     curl_global_cleanup();
84     return TEST_ERR_MAJOR_BAD;
85   }
86
87   if ((easy = curl_easy_init()) == NULL) {
88     fprintf(stderr, "curl_easy_init() failed\n");
89     curl_multi_cleanup(multi);
90     curl_global_cleanup();
91     return TEST_ERR_MAJOR_BAD;
92   }
93
94   curl_multi_setopt(multi, CURLMOPT_PIPELINING, 1L);
95
96   test_setopt(easy, CURLOPT_WRITEFUNCTION, fwrite);
97   test_setopt(easy, CURLOPT_FAILONERROR, 1L);
98   test_setopt(easy, CURLOPT_URL, URL);
99
100   if (curl_multi_add_handle(multi, easy) != CURLM_OK) {
101     printf("curl_multi_add_handle() failed\n");
102     res = TEST_ERR_MAJOR_BAD;
103   } else {
104     if (perform(multi) != CURLM_OK)
105       printf("retrieve 1 failed\n");
106
107     curl_multi_remove_handle(multi, easy);
108   }
109   curl_easy_reset(easy);
110
111   test_setopt(easy, CURLOPT_FAILONERROR, 1L);
112   test_setopt(easy, CURLOPT_URL, libtest_arg2);
113
114   if (curl_multi_add_handle(multi, easy) != CURLM_OK) {
115     printf("curl_multi_add_handle() 2 failed\n");
116     res = TEST_ERR_MAJOR_BAD;
117   } else {
118     if (perform(multi) != CURLM_OK)
119       printf("retrieve 2 failed\n");
120
121     curl_multi_remove_handle(multi, easy);
122   }
123
124 test_cleanup:
125
126   curl_easy_cleanup(easy);
127   curl_multi_cleanup(multi);
128   curl_global_cleanup();
129
130   printf("Finished!\n");
131
132   return res;
133 }