1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2001-2003, Ximian, Inc.
6 #include "test-utils.h"
11 gboolean client_sent_basic, client_sent_digest;
12 gboolean server_requested_basic, server_requested_digest;
17 curl_exited (GPid pid, int status, gpointer data)
19 gboolean *done = data;
22 test_data.succeeded = (status == 0);
26 do_test (int n, SoupURI *base_uri, const char *path,
27 gboolean good_user, gboolean good_password,
28 gboolean offer_basic, gboolean offer_digest,
29 gboolean client_sends_basic, gboolean client_sends_digest,
30 gboolean server_requests_basic, gboolean server_requests_digest,
39 debug_printf (1, "%2d. %s, %soffer Basic, %soffer Digest, %s user, %s password\n",
40 n, path, offer_basic ? "" : "don't ",
41 offer_digest ? "" : "don't ",
42 good_user ? "good" : "bad",
43 good_password ? "good" : "bad");
45 uri = soup_uri_new_with_base (base_uri, path);
46 uri_str = soup_uri_to_string (uri, FALSE);
49 args = g_ptr_array_new ();
50 g_ptr_array_add (args, "curl");
51 g_ptr_array_add (args, "-f");
52 g_ptr_array_add (args, "-s");
53 if (offer_basic || offer_digest) {
54 g_ptr_array_add (args, "-u");
57 g_ptr_array_add (args, "user:password");
59 g_ptr_array_add (args, "user:badpassword");
62 g_ptr_array_add (args, "baduser:password");
64 g_ptr_array_add (args, "baduser:badpassword");
67 if (offer_basic && offer_digest)
68 g_ptr_array_add (args, "--anyauth");
70 g_ptr_array_add (args, "--basic");
72 g_ptr_array_add (args, "--digest");
74 g_ptr_array_add (args, uri_str);
75 g_ptr_array_add (args, NULL);
77 memset (&test_data, 0, sizeof (test_data));
78 if (g_spawn_async (NULL, (char **)args->pdata, NULL,
79 G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL | G_SPAWN_DO_NOT_REAP_CHILD,
80 NULL, NULL, &pid, NULL)) {
82 g_child_watch_add (pid, curl_exited, &done);
85 g_main_context_iteration (NULL, TRUE);
87 test_data.succeeded = FALSE;
88 g_ptr_array_free (args, TRUE);
91 if (server_requests_basic != test_data.server_requested_basic) {
93 if (test_data.server_requested_basic)
94 debug_printf (1, " Server sent WWW-Authenticate: Basic, but shouldn't have!\n");
96 debug_printf (1, " Server didn't send WWW-Authenticate: Basic, but should have!\n");
98 if (server_requests_digest != test_data.server_requested_digest) {
100 if (test_data.server_requested_digest)
101 debug_printf (1, " Server sent WWW-Authenticate: Digest, but shouldn't have!\n");
103 debug_printf (1, " Server didn't send WWW-Authenticate: Digest, but should have!\n");
105 if (client_sends_basic != test_data.client_sent_basic) {
107 if (test_data.client_sent_basic)
108 debug_printf (1, " Client sent Authorization: Basic, but shouldn't have!\n");
110 debug_printf (1, " Client didn't send Authorization: Basic, but should have!\n");
112 if (client_sends_digest != test_data.client_sent_digest) {
114 if (test_data.client_sent_digest)
115 debug_printf (1, " Client sent Authorization: Digest, but shouldn't have!\n");
117 debug_printf (1, " Client didn't send Authorization: Digest, but should have!\n");
119 if (success && !test_data.succeeded) {
121 debug_printf (1, " Should have succeeded, but didn't!\n");
122 } else if (!success && test_data.succeeded) {
124 debug_printf (1, " Should not have succeeded, but did!\n");
129 do_auth_tests (SoupURI *base_uri)
132 gboolean use_basic, use_digest, good_user, good_password;
133 gboolean preemptive_basic, good_auth;
135 for (i = 0; i < 16; i++) {
136 use_basic = (i & 1) == 1;
137 use_digest = (i & 2) == 2;
138 good_user = (i & 4) == 4;
139 good_password = (i & 8) == 8;
141 good_auth = good_user && good_password;
143 /* Curl will preemptively send Basic if it's told to
144 * use Basic but not Digest.
146 preemptive_basic = use_basic && !use_digest;
148 /* 1. No auth required. The server will ignore the
149 * Authorization headers completely, and the request
150 * will always succeed.
152 do_test (n++, base_uri, "/foo",
153 good_user, good_password,
155 use_basic, use_digest,
156 /* expected from client */
157 preemptive_basic, FALSE,
158 /* expected from server */
163 /* 2. Basic auth required. The server will send
164 * "WWW-Authenticate: Basic" if the client fails to
165 * send an Authorization: Basic on the first request,
166 * or if it sends a bad password.
168 do_test (n++, base_uri, "/Basic/foo",
169 good_user, good_password,
171 use_basic, use_digest,
172 /* expected from client */
174 /* expected from server */
175 !preemptive_basic || !good_auth, FALSE,
177 use_basic && good_auth);
179 /* 3. Digest auth required. Simpler than the basic
180 * case because the client can't send Digest auth
183 do_test (n++, base_uri, "/Digest/foo",
184 good_user, good_password,
186 use_basic, use_digest,
187 /* expected from client */
188 preemptive_basic, use_digest,
189 /* expected from server */
192 use_digest && good_auth);
194 /* 4. Any auth required. */
195 do_test (n++, base_uri, "/Any/foo",
196 good_user, good_password,
198 use_basic, use_digest,
199 /* expected from client */
200 preemptive_basic, use_digest,
201 /* expected from server */
202 !preemptive_basic || !good_auth, !preemptive_basic || !good_auth,
204 (use_basic || use_digest) && good_auth);
206 /* 5. No auth required again. (Makes sure that
207 * SOUP_AUTH_DOMAIN_REMOVE_PATH works.)
209 do_test (n++, base_uri, "/Any/Not/foo",
210 good_user, good_password,
212 use_basic, use_digest,
213 /* expected from client */
214 preemptive_basic, FALSE,
215 /* expected from server */
223 basic_auth_callback (SoupAuthDomain *auth_domain, SoupMessage *msg,
224 const char *username, const char *password, gpointer data)
226 return !strcmp (username, "user") && !strcmp (password, "password");
230 digest_auth_callback (SoupAuthDomain *auth_domain, SoupMessage *msg,
231 const char *username, gpointer data)
233 if (strcmp (username, "user") != 0)
236 /* Note: this is exactly how you *shouldn't* do it in the real
237 * world; you should have the pre-encoded password stored in a
238 * database of some sort rather than using the cleartext
239 * password in the callback.
241 return soup_auth_domain_digest_encode_password ("user",
247 server_callback (SoupServer *server, SoupMessage *msg,
248 const char *path, GHashTable *query,
249 SoupClientContext *context, gpointer data)
251 if (msg->method != SOUP_METHOD_GET && msg->method != SOUP_METHOD_HEAD) {
252 soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
256 soup_message_set_response (msg, "text/plain",
259 soup_message_set_status (msg, SOUP_STATUS_OK);
263 got_headers_callback (SoupMessage *msg, gpointer data)
267 header = soup_message_headers_get_one (msg->request_headers,
270 if (strstr (header, "Basic "))
271 test_data.client_sent_basic = TRUE;
272 if (strstr (header, "Digest "))
273 test_data.client_sent_digest = TRUE;
278 wrote_headers_callback (SoupMessage *msg, gpointer data)
282 header = soup_message_headers_get_list (msg->response_headers,
285 if (strstr (header, "Basic "))
286 test_data.server_requested_basic = TRUE;
287 if (strstr (header, "Digest "))
288 test_data.server_requested_digest = TRUE;
293 request_started_callback (SoupServer *server, SoupMessage *msg,
294 SoupClientContext *client, gpointer data)
296 g_signal_connect (msg, "got_headers",
297 G_CALLBACK (got_headers_callback), NULL);
298 g_signal_connect (msg, "wrote_headers",
299 G_CALLBACK (wrote_headers_callback), NULL);
302 static gboolean run_tests = TRUE;
304 static GOptionEntry no_test_entry[] = {
305 { "no-tests", 'n', G_OPTION_FLAG_REVERSE,
306 G_OPTION_ARG_NONE, &run_tests,
307 "Don't run tests, just run the test server", NULL },
312 main (int argc, char **argv)
317 SoupAuthDomain *auth_domain;
319 test_init (argc, argv, no_test_entry);
321 server = soup_test_server_new (FALSE);
322 g_signal_connect (server, "request_started",
323 G_CALLBACK (request_started_callback), NULL);
324 soup_server_add_handler (server, NULL,
325 server_callback, NULL, NULL);
327 auth_domain = soup_auth_domain_basic_new (
328 SOUP_AUTH_DOMAIN_REALM, "server-auth-test",
329 SOUP_AUTH_DOMAIN_ADD_PATH, "/Basic",
330 SOUP_AUTH_DOMAIN_ADD_PATH, "/Any",
331 SOUP_AUTH_DOMAIN_REMOVE_PATH, "/Any/Not",
332 SOUP_AUTH_DOMAIN_BASIC_AUTH_CALLBACK, basic_auth_callback,
334 soup_server_add_auth_domain (server, auth_domain);
335 g_object_unref (auth_domain);
337 auth_domain = soup_auth_domain_digest_new (
338 SOUP_AUTH_DOMAIN_REALM, "server-auth-test",
339 SOUP_AUTH_DOMAIN_ADD_PATH, "/Digest",
340 SOUP_AUTH_DOMAIN_ADD_PATH, "/Any",
341 SOUP_AUTH_DOMAIN_REMOVE_PATH, "/Any/Not",
342 SOUP_AUTH_DOMAIN_DIGEST_AUTH_CALLBACK, digest_auth_callback,
344 soup_server_add_auth_domain (server, auth_domain);
345 g_object_unref (auth_domain);
347 loop = g_main_loop_new (NULL, TRUE);
350 uri = soup_uri_new ("http://127.0.0.1");
351 soup_uri_set_port (uri, soup_server_get_port (server));
355 g_print ("Listening on port %d\n", soup_server_get_port (server));
356 g_main_loop_run (loop);
359 g_main_loop_unref (loop);
360 soup_test_server_quit_unref (server);
367 #else /* HAVE_CURL */
370 main (int argc, char **argv)
372 return 77; /* SKIP */