auth-test: reorganize this a bit to be more like other test programs
authorDan Winship <danw@gnome.org>
Wed, 5 Oct 2011 17:57:33 +0000 (13:57 -0400)
committerDan Winship <danw@gnome.org>
Sun, 13 Nov 2011 20:06:17 +0000 (15:06 -0500)
tests/auth-test.c

index 5b45a73..8e0132f 100644 (file)
@@ -304,6 +304,111 @@ bug271540_finished (SoupSession *session, SoupMessage *msg, gpointer data)
 }
 
 static void
+do_pipelined_auth_test (const char *base_uri)
+{
+       SoupSession *session;
+       SoupMessage *msg;
+       gboolean authenticated;
+       char *uri;
+       int i;
+
+       debug_printf (1, "Testing pipelined auth (bug 271540):\n");
+       session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
+
+       authenticated = FALSE;
+       g_signal_connect (session, "authenticate",
+                         G_CALLBACK (bug271540_authenticate), &authenticated);
+
+       uri = g_strconcat (base_uri, "Basic/realm1/", NULL);
+       for (i = 0; i < 10; i++) {
+               msg = soup_message_new (SOUP_METHOD_GET, uri);
+               g_object_set_data (G_OBJECT (msg), "#", GINT_TO_POINTER (i + 1));
+               g_signal_connect (msg, "wrote_headers",
+                                 G_CALLBACK (bug271540_sent), &authenticated);
+
+               soup_session_queue_message (session, msg,
+                                           bug271540_finished, &i);
+       }
+       g_free (uri);
+
+       loop = g_main_loop_new (NULL, TRUE);
+       g_main_loop_run (loop);
+       g_main_loop_unref (loop);
+
+       soup_test_session_abort_unref (session);
+}
+
+/* We test two different things here:
+ *
+ *   1. If we get a 401 response with "WWW-Authenticate: Digest
+ *      stale=true...", we should retry and succeed *without* the
+ *      session asking for a password again.
+ *
+ *   2. If we get a successful response with "Authentication-Info:
+ *      nextnonce=...", we should update the nonce automatically so as
+ *      to avoid getting a stale nonce error on the next request.
+ *
+ * In our Apache config, /Digest/realm1 and /Digest/realm1/expire are
+ * set up to use the same auth info, but only the latter has an
+ * AuthDigestNonceLifetime (of 2 seconds). The way nonces work in
+ * Apache, a nonce received from /Digest/realm1 will still expire in
+ * /Digest/realm1/expire, but it won't issue a nextnonce for a request
+ * in /Digest/realm1. This lets us test both behaviors.
+ *
+ * The expected conversation is:
+ *
+ * First message
+ *   GET /Digest/realm1
+ *
+ *   401 Unauthorized
+ *   WWW-Authenticate: Digest nonce=A
+ *
+ *   [emit 'authenticate']
+ *
+ *   GET /Digest/realm1
+ *   Authorization: Digest nonce=A
+ *
+ *   200 OK
+ *   [No Authentication-Info]
+ *
+ * [sleep 2 seconds: nonce A is no longer valid, but we have no
+ * way of knowing that]
+ *
+ * Second message
+ *   GET /Digest/realm1/expire/
+ *   Authorization: Digest nonce=A
+ *
+ *   401 Unauthorized
+ *   WWW-Authenticate: Digest stale=true nonce=B
+ *
+ *   GET /Digest/realm1/expire/
+ *   Authorization: Digest nonce=B
+ *
+ *   200 OK
+ *   Authentication-Info: nextnonce=C
+ *
+ * [sleep 1 second]
+ *
+ * Third message
+ *   GET /Digest/realm1/expire/
+ *   Authorization: Digest nonce=C
+ *   [nonce=B would work here too]
+ *
+ *   200 OK
+ *   Authentication-Info: nextnonce=D
+ *
+ * [sleep 1 second; nonces B and C are no longer valid, but D is]
+ *
+ * Fourth message
+ *   GET /Digest/realm1/expire/
+ *   Authorization: Digest nonce=D
+ *
+ *   200 OK
+ *   Authentication-Info: nextnonce=D
+ *
+ */
+
+static void
 digest_nonce_authenticate (SoupSession *session, SoupMessage *msg,
                           SoupAuth *auth, gboolean retrying, gpointer data)
 {
@@ -359,6 +464,31 @@ do_digest_nonce_test (SoupSession *session,
        g_object_unref (msg);
 }
 
+static void
+do_digest_expiration_test (const char *base_uri)
+{
+       SoupSession *session;
+       char *uri;
+
+       debug_printf (1, "\nTesting digest nonce expiration:\n");
+
+       session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
+
+       uri = g_strconcat (base_uri, "Digest/realm1/", NULL);
+       do_digest_nonce_test (session, "First", uri, TRUE, TRUE);
+       g_free (uri);
+       sleep (2);
+       uri = g_strconcat (base_uri, "Digest/realm1/expire/", NULL);
+       do_digest_nonce_test (session, "Second", uri, TRUE, FALSE);
+       sleep (1);
+       do_digest_nonce_test (session, "Third", uri, FALSE, FALSE);
+       sleep (1);
+       do_digest_nonce_test (session, "Fourth", uri, FALSE, FALSE);
+       g_free (uri);
+
+       soup_test_session_abort_unref (session);
+}
+
 /* Async auth test. We queue three requests to /Basic/realm1, ensuring
  * that they are sent in order. The first and third ones will be
  * paused from the authentication callback. The second will be allowed
@@ -450,6 +580,7 @@ do_async_auth_test (const char *base_uri)
 
        debug_printf (1, "\nTesting async auth:\n");
 
+       loop = g_main_loop_new (NULL, TRUE);
        session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
        remaining = 0;
 
@@ -615,6 +746,7 @@ do_async_auth_test (const char *base_uri)
        g_object_unref (msg1);
 
        g_free (uri);
+       g_main_loop_unref (loop);
 }
 
 typedef struct {
@@ -954,12 +1086,8 @@ do_batch_tests (const gchar *base_uri_str, gint ntests)
 int
 main (int argc, char **argv)
 {
-       SoupSession *session;
-       SoupMessage *msg;
        const char *base_uri;
-       char *uri;
-       gboolean authenticated;
-       int i, ntests;
+       int ntests;
 
        test_init (argc, argv, NULL);
        apache_init ();
@@ -976,131 +1104,12 @@ main (int argc, char **argv)
        ntests = G_N_ELEMENTS (relogin_tests);
        do_batch_tests (base_uri, ntests);
 
-       /* And now for some regression tests */
-       loop = g_main_loop_new (NULL, TRUE);
-
-       debug_printf (1, "Testing pipelined auth (bug 271540):\n");
-       session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
-
-       authenticated = FALSE;
-       g_signal_connect (session, "authenticate",
-                         G_CALLBACK (bug271540_authenticate), &authenticated);
-
-       uri = g_strconcat (base_uri, "Basic/realm1/", NULL);
-       for (i = 0; i < 10; i++) {
-               msg = soup_message_new (SOUP_METHOD_GET, uri);
-               g_object_set_data (G_OBJECT (msg), "#", GINT_TO_POINTER (i + 1));
-               g_signal_connect (msg, "wrote_headers",
-                                 G_CALLBACK (bug271540_sent), &authenticated);
-
-               soup_session_queue_message (session, msg,
-                                           bug271540_finished, &i);
-       }
-       g_free (uri);
-
-       g_main_loop_run (loop);
-       soup_test_session_abort_unref (session);
-
-       debug_printf (1, "\nTesting digest nonce expiration:\n");
-
-       /* We test two different things here:
-        *
-        *   1. If we get a 401 response with
-        *      "WWW-Authenticate: Digest stale=true...", we should
-        *      retry and succeed *without* the session asking for a
-        *      password again.
-        *
-        *   2. If we get a successful response with
-        *      "Authentication-Info: nextnonce=...", we should update
-        *      the nonce automatically so as to avoid getting a
-        *      stale nonce error on the next request.
-        *
-        * In our Apache config, /Digest/realm1 and
-        * /Digest/realm1/expire are set up to use the same auth info,
-        * but only the latter has an AuthDigestNonceLifetime (of 2
-        * seconds). The way nonces work in Apache, a nonce received
-        * from /Digest/realm1 will still expire in
-        * /Digest/realm1/expire, but it won't issue a nextnonce for a
-        * request in /Digest/realm1. This lets us test both
-        * behaviors.
-        *
-        * The expected conversation is:
-        *
-        * First message
-        *   GET /Digest/realm1
-        *
-        *   401 Unauthorized
-        *   WWW-Authenticate: Digest nonce=A
-        *
-        *   [emit 'authenticate']
-        *
-        *   GET /Digest/realm1
-        *   Authorization: Digest nonce=A
-        *
-        *   200 OK
-        *   [No Authentication-Info]
-        *
-        * [sleep 2 seconds: nonce A is no longer valid, but we have no
-        * way of knowing that]
-        *
-        * Second message
-        *   GET /Digest/realm1/expire/
-        *   Authorization: Digest nonce=A
-        *
-        *   401 Unauthorized
-        *   WWW-Authenticate: Digest stale=true nonce=B
-        *
-        *   GET /Digest/realm1/expire/
-        *   Authorization: Digest nonce=B
-        *
-        *   200 OK
-        *   Authentication-Info: nextnonce=C
-        *
-        * [sleep 1 second]
-        *
-        * Third message
-        *   GET /Digest/realm1/expire/
-        *   Authorization: Digest nonce=C
-        *   [nonce=B would work here too]
-        *
-        *   200 OK
-        *   Authentication-Info: nextnonce=D
-        *
-        * [sleep 1 second; nonces B and C are no longer valid, but D is]
-        *
-        * Fourth message
-        *   GET /Digest/realm1/expire/
-        *   Authorization: Digest nonce=D
-        *
-        *   200 OK
-        *   Authentication-Info: nextnonce=D
-        *
-        */
-
-       session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC, NULL);
-
-       uri = g_strconcat (base_uri, "Digest/realm1/", NULL);
-       do_digest_nonce_test (session, "First", uri, TRUE, TRUE);
-       g_free (uri);
-       sleep (2);
-       uri = g_strconcat (base_uri, "Digest/realm1/expire/", NULL);
-       do_digest_nonce_test (session, "Second", uri, TRUE, FALSE);
-       sleep (1);
-       do_digest_nonce_test (session, "Third", uri, FALSE, FALSE);
-       sleep (1);
-       do_digest_nonce_test (session, "Fourth", uri, FALSE, FALSE);
-       g_free (uri);
-
-       soup_test_session_abort_unref (session);
-
-       /* Async auth */
+       /* Other regression tests */
+       do_pipelined_auth_test (base_uri);
+       do_digest_expiration_test (base_uri);
        do_async_auth_test (base_uri);
-
-       /* Selecting correct auth when multiple auth types are available */
        do_select_auth_test ();
 
-       g_main_loop_unref (loop);
-
        test_cleanup ();
        return errors != 0;
 }