49a81bf577423e66c19486271b05e1908cfd3326
[platform/upstream/curl.git] / tests / libtest / lib555.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 /* This test case is supposed to be identical to 547 except that this uses the
24  * multi interface and 547 is easy interface.
25  *
26  * argv1 = URL
27  * argv2 = proxy
28  * argv3 = proxyuser:password
29  */
30
31 #include "test.h"
32 #include "testutil.h"
33 #include "warnless.h"
34 #include "memdebug.h"
35
36 #define TEST_HANG_TIMEOUT 60 * 1000
37
38 #define UPLOADTHIS "this is the blurb we want to upload\n"
39
40 static size_t readcallback(void  *ptr,
41                            size_t size,
42                            size_t nmemb,
43                            void *clientp)
44 {
45   int *counter = (int *)clientp;
46
47   if(*counter) {
48     /* only do this once and then require a clearing of this */
49     fprintf(stderr, "READ ALREADY DONE!\n");
50     return 0;
51   }
52   (*counter)++; /* bump */
53
54   if(size * nmemb > strlen(UPLOADTHIS)) {
55     fprintf(stderr, "READ!\n");
56     strcpy(ptr, UPLOADTHIS);
57     return strlen(UPLOADTHIS);
58   }
59   fprintf(stderr, "READ NOT FINE!\n");
60   return 0;
61 }
62 static curlioerr ioctlcallback(CURL *handle,
63                                int cmd,
64                                void *clientp)
65 {
66   int *counter = (int *)clientp;
67   (void)handle; /* unused */
68   if(cmd == CURLIOCMD_RESTARTREAD) {
69     fprintf(stderr, "REWIND!\n");
70     *counter = 0; /* clear counter to make the read callback restart */
71   }
72   return CURLIOE_OK;
73 }
74
75
76 int test(char *URL)
77 {
78   int res = 0;
79   CURL *curl = NULL;
80   int counter=0;
81   CURLM *m = NULL;
82   int running=1;
83
84   start_test_timing();
85
86   global_init(CURL_GLOBAL_ALL);
87
88   easy_init(curl);
89
90   easy_setopt(curl, CURLOPT_URL, URL);
91   easy_setopt(curl, CURLOPT_VERBOSE, 1L);
92   easy_setopt(curl, CURLOPT_HEADER, 1L);
93
94   /* read the POST data from a callback */
95   easy_setopt(curl, CURLOPT_IOCTLFUNCTION, ioctlcallback);
96   easy_setopt(curl, CURLOPT_IOCTLDATA, &counter);
97   easy_setopt(curl, CURLOPT_READFUNCTION, readcallback);
98   easy_setopt(curl, CURLOPT_READDATA, &counter);
99   /* We CANNOT do the POST fine without setting the size (or choose chunked)! */
100   easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(UPLOADTHIS));
101
102   easy_setopt(curl, CURLOPT_POST, 1L);
103 #ifdef CURL_DOES_CONVERSIONS
104   /* Convert the POST data to ASCII. */
105   easy_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
106 #endif
107   easy_setopt(curl, CURLOPT_PROXY, libtest_arg2);
108   easy_setopt(curl, CURLOPT_PROXYUSERPWD, libtest_arg3);
109   easy_setopt(curl, CURLOPT_PROXYAUTH,
110                    (long) (CURLAUTH_NTLM | CURLAUTH_DIGEST | CURLAUTH_BASIC) );
111
112   multi_init(m);
113
114   multi_add_handle(m, curl);
115
116   while (running) {
117     struct timeval timeout;
118     fd_set fdread, fdwrite, fdexcep;
119     int maxfd = -99;
120
121     timeout.tv_sec = 0;
122     timeout.tv_usec = 100000L; /* 100 ms */
123
124     multi_perform(m, &running);
125
126     abort_on_test_timeout();
127
128 #ifdef TPF
129     sleep(1); /* avoid ctl-10 dump */
130 #endif
131
132     if(!running)
133       break; /* done */
134
135     FD_ZERO(&fdread);
136     FD_ZERO(&fdwrite);
137     FD_ZERO(&fdexcep);
138
139     multi_fdset(m, &fdread, &fdwrite, &fdexcep, &maxfd);
140
141     /* At this point, maxfd is guaranteed to be greater or equal than -1. */
142
143     select_test(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
144
145     abort_on_test_timeout();
146   }
147
148 test_cleanup:
149
150   /* proper cleanup sequence - type PA */
151
152   curl_multi_remove_handle(m, curl);
153   curl_multi_cleanup(m);
154   curl_easy_cleanup(curl);
155   curl_global_cleanup();
156
157   return res;
158 }
159