Git init
[profile/ivi/libsoup2.4.git] / tests / timeout-test.c
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10
11 #include "libsoup/soup.h"
12
13 #include "test-utils.h"
14
15 static void
16 do_message_to_session (SoupSession *session, const char *uri,
17                        const char *comment, guint expected_status)
18 {
19         SoupMessage *msg;
20
21         debug_printf (1, "    %s\n", comment);
22         msg = soup_message_new ("GET", uri);
23         soup_session_send_message (session, msg);
24
25         if (msg->status_code != expected_status) {
26                 debug_printf (1, "      FAILED: %d %s (expected %d %s)\n",
27                               msg->status_code, msg->reason_phrase,
28                               expected_status,
29                               soup_status_get_phrase (expected_status));
30                 errors++;
31         }
32
33         if (SOUP_STATUS_IS_SUCCESSFUL (msg->status_code) &&
34             !soup_message_is_keepalive (msg)) {
35                 debug_printf (1, "      ERROR: message is not keepalive!");
36                 errors++;
37         }
38
39         g_object_unref (msg);
40 }
41
42 static void
43 request_started_cb (SoupSession *session, SoupMessage *msg,
44                     SoupSocket *socket, gpointer user_data)
45 {
46         SoupSocket **ret = user_data;
47
48         *ret = socket;
49 }
50
51 static void
52 do_tests_for_session (SoupSession *timeout_session,
53                       SoupSession *idle_session,
54                       SoupSession *plain_session,
55                       char *fast_uri, char *slow_uri)
56 {
57         SoupSocket *ret, *idle_first, *idle_second;
58         SoupSocket *plain_first, *plain_second;
59
60         if (idle_session) {
61                 g_signal_connect (idle_session, "request-started",
62                                   G_CALLBACK (request_started_cb), &ret);
63                 do_message_to_session (idle_session, fast_uri, "fast to idle", SOUP_STATUS_OK);
64                 idle_first = ret;
65         }
66
67         if (plain_session) {
68                 g_signal_connect (plain_session, "request-started",
69                                   G_CALLBACK (request_started_cb), &ret);
70                 do_message_to_session (plain_session, fast_uri, "fast to plain", SOUP_STATUS_OK);
71                 plain_first = ret;
72         }
73
74         do_message_to_session (timeout_session, fast_uri, "fast to timeout", SOUP_STATUS_OK);
75         do_message_to_session (timeout_session, slow_uri, "slow to timeout", SOUP_STATUS_IO_ERROR);
76
77         if (idle_session) {
78                 do_message_to_session (idle_session, fast_uri, "fast to idle", SOUP_STATUS_OK);
79                 idle_second = ret;
80                 g_signal_handlers_disconnect_by_func (idle_session,
81                                                       (gpointer)request_started_cb,
82                                                       &ret);
83
84                 if (idle_first == idle_second) {
85                         debug_printf (1, "      ERROR: idle_session did not close first connection\n");
86                         errors++;
87                 }
88         }
89
90         if (plain_session) {
91                 do_message_to_session (plain_session, fast_uri, "fast to plain", SOUP_STATUS_OK);
92                 plain_second = ret;
93                 g_signal_handlers_disconnect_by_func (plain_session,
94                                                       (gpointer)request_started_cb,
95                                                       &ret);
96
97                 if (plain_first != plain_second) {
98                         debug_printf (1, "      ERROR: plain_session closed connection\n");
99                         errors++;
100                 }
101         }
102 }
103
104 static void
105 do_timeout_tests (char *fast_uri, char *slow_uri)
106 {
107         SoupSession *timeout_session, *idle_session, *plain_session;
108
109         debug_printf (1, "  async\n");
110         timeout_session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
111                                          SOUP_SESSION_TIMEOUT, 1,
112                                          NULL);
113         idle_session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
114                                               SOUP_SESSION_IDLE_TIMEOUT, 1,
115                                               NULL);
116         /* The "plain" session also has an idle timeout, but it's longer
117          * than the test takes, so for our purposes it should behave like
118          * it has no timeout.
119          */
120         plain_session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
121                                                SOUP_SESSION_IDLE_TIMEOUT, 2,
122                                                NULL);
123         do_tests_for_session (timeout_session, idle_session, plain_session,
124                               fast_uri, slow_uri);
125         soup_test_session_abort_unref (timeout_session);
126         soup_test_session_abort_unref (idle_session);
127         soup_test_session_abort_unref (plain_session);
128
129         debug_printf (1, "  sync\n");
130         timeout_session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC,
131                                                  SOUP_SESSION_TIMEOUT, 1,
132                                                  NULL);
133         /* SOUP_SESSION_TIMEOUT doesn't work with sync sessions */
134         plain_session = soup_test_session_new (SOUP_TYPE_SESSION_SYNC,
135                                                NULL);
136         do_tests_for_session (timeout_session, NULL, plain_session, fast_uri, slow_uri);
137         soup_test_session_abort_unref (timeout_session);
138 }
139
140 static gboolean
141 timeout_finish_message (gpointer msg)
142 {
143         SoupServer *server = g_object_get_data (G_OBJECT (msg), "server");
144
145         soup_server_unpause_message (server, msg);
146         return FALSE;
147 }
148
149 static void
150 server_handler (SoupServer        *server,
151                 SoupMessage       *msg, 
152                 const char        *path,
153                 GHashTable        *query,
154                 SoupClientContext *client,
155                 gpointer           user_data)
156 {
157         soup_message_set_status (msg, SOUP_STATUS_OK);
158         soup_message_set_response (msg, "text/plain",
159                                    SOUP_MEMORY_STATIC,
160                                    "ok\r\n", 4);
161
162         if (!strcmp (path, "/slow")) {
163                 soup_server_pause_message (server, msg);
164                 g_object_set_data (G_OBJECT (msg), "server", server);
165                 soup_add_timeout (soup_server_get_async_context (server),
166                                   1100, timeout_finish_message, msg);
167         }
168 }
169
170 int
171 main (int argc, char **argv)
172 {
173         SoupServer *server;
174         char *fast_uri, *slow_uri;
175
176         test_init (argc, argv, NULL);
177
178         debug_printf (1, "http\n");
179         server = soup_test_server_new (TRUE);
180         soup_server_add_handler (server, NULL, server_handler, NULL, NULL);
181         fast_uri = g_strdup_printf ("http://127.0.0.1:%u/",
182                                     soup_server_get_port (server));
183         slow_uri = g_strdup_printf ("http://127.0.0.1:%u/slow",
184                                     soup_server_get_port (server));
185         do_timeout_tests (fast_uri, slow_uri);
186         g_free (fast_uri);
187         g_free (slow_uri);
188         soup_test_server_quit_unref (server);
189
190         if (tls_available) {
191                 debug_printf (1, "\nhttps\n");
192                 server = soup_test_server_new_ssl (TRUE);
193                 soup_server_add_handler (server, NULL, server_handler, NULL, NULL);
194                 fast_uri = g_strdup_printf ("https://127.0.0.1:%u/",
195                                             soup_server_get_port (server));
196                 slow_uri = g_strdup_printf ("https://127.0.0.1:%u/slow",
197                                             soup_server_get_port (server));
198                 do_timeout_tests (fast_uri, slow_uri);
199                 g_free (fast_uri);
200                 g_free (slow_uri);
201                 soup_test_server_quit_unref (server);
202         }
203
204         test_cleanup ();
205         return errors != 0;
206 }