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"
9 gboolean client_sent_basic, client_sent_digest;
10 gboolean server_requested_basic, server_requested_digest;
15 curl_exited (GPid pid, int status, gpointer data)
17 gboolean *done = data;
20 test_data.succeeded = (status == 0);
24 do_test (int n, SoupURI *base_uri, const char *path,
25 gboolean good_user, gboolean good_password,
26 gboolean offer_basic, gboolean offer_digest,
27 gboolean client_sends_basic, gboolean client_sends_digest,
28 gboolean server_requests_basic, gboolean server_requests_digest,
37 debug_printf (1, "%2d. %s, %soffer Basic, %soffer Digest, %s user, %s password\n",
38 n, path, offer_basic ? "" : "don't ",
39 offer_digest ? "" : "don't ",
40 good_user ? "good" : "bad",
41 good_password ? "good" : "bad");
43 uri = soup_uri_new_with_base (base_uri, path);
44 uri_str = soup_uri_to_string (uri, FALSE);
47 args = g_ptr_array_new ();
48 g_ptr_array_add (args, "curl");
49 g_ptr_array_add (args, "-f");
50 g_ptr_array_add (args, "-s");
51 if (offer_basic || offer_digest) {
52 g_ptr_array_add (args, "-u");
55 g_ptr_array_add (args, "user:password");
57 g_ptr_array_add (args, "user:badpassword");
60 g_ptr_array_add (args, "baduser:password");
62 g_ptr_array_add (args, "baduser:badpassword");
65 if (offer_basic && offer_digest)
66 g_ptr_array_add (args, "--anyauth");
68 g_ptr_array_add (args, "--basic");
70 g_ptr_array_add (args, "--digest");
72 g_ptr_array_add (args, uri_str);
73 g_ptr_array_add (args, NULL);
75 memset (&test_data, 0, sizeof (test_data));
76 if (g_spawn_async (NULL, (char **)args->pdata, NULL,
77 G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL | G_SPAWN_DO_NOT_REAP_CHILD,
78 NULL, NULL, &pid, NULL)) {
80 g_child_watch_add (pid, curl_exited, &done);
83 g_main_context_iteration (NULL, TRUE);
85 test_data.succeeded = FALSE;
86 g_ptr_array_free (args, TRUE);
89 if (server_requests_basic != test_data.server_requested_basic) {
91 if (test_data.server_requested_basic)
92 debug_printf (1, " Server sent WWW-Authenticate: Basic, but shouldn't have!\n");
94 debug_printf (1, " Server didn't send WWW-Authenticate: Basic, but should have!\n");
96 if (server_requests_digest != test_data.server_requested_digest) {
98 if (test_data.server_requested_digest)
99 debug_printf (1, " Server sent WWW-Authenticate: Digest, but shouldn't have!\n");
101 debug_printf (1, " Server didn't send WWW-Authenticate: Digest, but should have!\n");
103 if (client_sends_basic != test_data.client_sent_basic) {
105 if (test_data.client_sent_basic)
106 debug_printf (1, " Client sent Authorization: Basic, but shouldn't have!\n");
108 debug_printf (1, " Client didn't send Authorization: Basic, but should have!\n");
110 if (client_sends_digest != test_data.client_sent_digest) {
112 if (test_data.client_sent_digest)
113 debug_printf (1, " Client sent Authorization: Digest, but shouldn't have!\n");
115 debug_printf (1, " Client didn't send Authorization: Digest, but should have!\n");
117 if (success && !test_data.succeeded) {
119 debug_printf (1, " Should have succeeded, but didn't!\n");
120 } else if (!success && test_data.succeeded) {
122 debug_printf (1, " Should not have succeeded, but did!\n");
127 do_auth_tests (SoupURI *base_uri)
130 gboolean use_basic, use_digest, good_user, good_password;
131 gboolean preemptive_basic, good_auth;
133 for (i = 0; i < 16; i++) {
134 use_basic = (i & 1) == 1;
135 use_digest = (i & 2) == 2;
136 good_user = (i & 4) == 4;
137 good_password = (i & 8) == 8;
139 good_auth = good_user && good_password;
141 /* Curl will preemptively send Basic if it's told to
142 * use Basic but not Digest.
144 preemptive_basic = use_basic && !use_digest;
146 /* 1. No auth required. The server will ignore the
147 * Authorization headers completely, and the request
148 * will always succeed.
150 do_test (n++, base_uri, "/foo",
151 good_user, good_password,
153 use_basic, use_digest,
154 /* expected from client */
155 preemptive_basic, FALSE,
156 /* expected from server */
161 /* 2. Basic auth required. The server will send
162 * "WWW-Authenticate: Basic" if the client fails to
163 * send an Authorization: Basic on the first request,
164 * or if it sends a bad password.
166 do_test (n++, base_uri, "/Basic/foo",
167 good_user, good_password,
169 use_basic, use_digest,
170 /* expected from client */
172 /* expected from server */
173 !preemptive_basic || !good_auth, FALSE,
175 use_basic && good_auth);
177 /* 3. Digest auth required. Simpler than the basic
178 * case because the client can't send Digest auth
181 do_test (n++, base_uri, "/Digest/foo",
182 good_user, good_password,
184 use_basic, use_digest,
185 /* expected from client */
186 preemptive_basic, use_digest,
187 /* expected from server */
190 use_digest && good_auth);
192 /* 4. Any auth required. */
193 do_test (n++, base_uri, "/Any/foo",
194 good_user, good_password,
196 use_basic, use_digest,
197 /* expected from client */
198 preemptive_basic, use_digest,
199 /* expected from server */
200 !preemptive_basic || !good_auth, !preemptive_basic || !good_auth,
202 (use_basic || use_digest) && good_auth);
204 /* 5. No auth required again. (Makes sure that
205 * SOUP_AUTH_DOMAIN_REMOVE_PATH works.)
207 do_test (n++, base_uri, "/Any/Not/foo",
208 good_user, good_password,
210 use_basic, use_digest,
211 /* expected from client */
212 preemptive_basic, FALSE,
213 /* expected from server */
221 basic_auth_callback (SoupAuthDomain *auth_domain, SoupMessage *msg,
222 const char *username, const char *password, gpointer data)
224 return !strcmp (username, "user") && !strcmp (password, "password");
228 digest_auth_callback (SoupAuthDomain *auth_domain, SoupMessage *msg,
229 const char *username, gpointer data)
231 if (strcmp (username, "user") != 0)
234 /* Note: this is exactly how you *shouldn't* do it in the real
235 * world; you should have the pre-encoded password stored in a
236 * database of some sort rather than using the cleartext
237 * password in the callback.
239 return soup_auth_domain_digest_encode_password ("user",
245 server_callback (SoupServer *server, SoupMessage *msg,
246 const char *path, GHashTable *query,
247 SoupClientContext *context, gpointer data)
249 if (msg->method != SOUP_METHOD_GET && msg->method != SOUP_METHOD_HEAD) {
250 soup_message_set_status (msg, SOUP_STATUS_NOT_IMPLEMENTED);
254 soup_message_set_response (msg, "text/plain",
257 soup_message_set_status (msg, SOUP_STATUS_OK);
261 got_headers_callback (SoupMessage *msg, gpointer data)
265 header = soup_message_headers_get_one (msg->request_headers,
268 if (strstr (header, "Basic "))
269 test_data.client_sent_basic = TRUE;
270 if (strstr (header, "Digest "))
271 test_data.client_sent_digest = TRUE;
276 wrote_headers_callback (SoupMessage *msg, gpointer data)
280 header = soup_message_headers_get_list (msg->response_headers,
283 if (strstr (header, "Basic "))
284 test_data.server_requested_basic = TRUE;
285 if (strstr (header, "Digest "))
286 test_data.server_requested_digest = TRUE;
291 request_started_callback (SoupServer *server, SoupMessage *msg,
292 SoupClientContext *client, gpointer data)
294 g_signal_connect (msg, "got_headers",
295 G_CALLBACK (got_headers_callback), NULL);
296 g_signal_connect (msg, "wrote_headers",
297 G_CALLBACK (wrote_headers_callback), NULL);
300 static gboolean run_tests = TRUE;
302 static GOptionEntry no_test_entry[] = {
303 { "no-tests", 'n', G_OPTION_FLAG_REVERSE,
304 G_OPTION_ARG_NONE, &run_tests,
305 "Don't run tests, just run the test server", NULL },
310 main (int argc, char **argv)
315 SoupAuthDomain *auth_domain;
317 test_init (argc, argv, no_test_entry);
319 server = soup_test_server_new (FALSE);
320 g_signal_connect (server, "request_started",
321 G_CALLBACK (request_started_callback), NULL);
322 soup_server_add_handler (server, NULL,
323 server_callback, NULL, NULL);
325 auth_domain = soup_auth_domain_basic_new (
326 SOUP_AUTH_DOMAIN_REALM, "server-auth-test",
327 SOUP_AUTH_DOMAIN_ADD_PATH, "/Basic",
328 SOUP_AUTH_DOMAIN_ADD_PATH, "/Any",
329 SOUP_AUTH_DOMAIN_REMOVE_PATH, "/Any/Not",
330 SOUP_AUTH_DOMAIN_BASIC_AUTH_CALLBACK, basic_auth_callback,
332 soup_server_add_auth_domain (server, auth_domain);
333 g_object_unref (auth_domain);
335 auth_domain = soup_auth_domain_digest_new (
336 SOUP_AUTH_DOMAIN_REALM, "server-auth-test",
337 SOUP_AUTH_DOMAIN_ADD_PATH, "/Digest",
338 SOUP_AUTH_DOMAIN_ADD_PATH, "/Any",
339 SOUP_AUTH_DOMAIN_REMOVE_PATH, "/Any/Not",
340 SOUP_AUTH_DOMAIN_DIGEST_AUTH_CALLBACK, digest_auth_callback,
342 soup_server_add_auth_domain (server, auth_domain);
343 g_object_unref (auth_domain);
345 loop = g_main_loop_new (NULL, TRUE);
348 uri = soup_uri_new ("http://127.0.0.1");
349 soup_uri_set_port (uri, soup_server_get_port (server));
353 g_print ("Listening on port %d\n", soup_server_get_port (server));
354 g_main_loop_run (loop);
357 g_main_loop_unref (loop);
358 soup_test_server_quit_unref (server);