diff options
author | Emanuele Aina <em@nerd.ocracy.org> | 2007-06-21 20:52:14 +0000 |
---|---|---|
committer | Sjoerd Simons <sjoerd.simons@collabora.co.uk> | 2009-04-13 15:01:05 +0100 |
commit | 323871a91984219797f928e0e75e4eb8e6f849c3 (patch) | |
tree | 03a8f4fecfa44b222291f33ffd975048d8e2d025 /tests/wocky-test-sasl-auth.c | |
parent | aa65b1798f0b1b4bef0662b5f49ee76dc57c6213 (diff) |
Ported the SASL test to Check using its looping tests support
Diffstat (limited to 'tests/wocky-test-sasl-auth.c')
-rw-r--r-- | tests/wocky-test-sasl-auth.c | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/tests/wocky-test-sasl-auth.c b/tests/wocky-test-sasl-auth.c new file mode 100644 index 0000000..dde79ee --- /dev/null +++ b/tests/wocky-test-sasl-auth.c @@ -0,0 +1,198 @@ +#include <stdio.h> + +#include "wocky-test-sasl-auth-server.h" +#include "test-transport.h" +#include <wocky/wocky-xmpp-connection.h> +#include <wocky/wocky-sasl-auth.h> + +#include <check.h> + +typedef struct { + gchar *description; + gchar *mech; + gboolean allow_plain; + GQuark domain; + int code; + ServerProblem problem; +} test_t; + +GMainLoop *mainloop; +WockyTransport *servertransport; +WockyTransport *clienttransport; +WockyXmppConnection *conn; +WockySaslAuth *sasl = NULL; + +const gchar *username = "test"; +const gchar *password = "test123"; +const gchar *servername = "testserver"; + +gboolean authenticated = FALSE; +gboolean run_done = FALSE; + +test_t *current_test = NULL; +GError *error = NULL; + +static void +got_error(GQuark domain, int code, gchar *message) { + g_set_error(&error, domain, code, message); + run_done = TRUE; + g_main_loop_quit(mainloop); +} + +gboolean +send_hook(WockyTransport *transport, const guint8 *data, + gsize length, GError **error, gpointer user_data) { + WockyTransport *target = + (servertransport == transport) ? clienttransport : servertransport; + + test_transport_write(TEST_TRANSPORT(target), data, length); + return TRUE; +} + +gchar * +return_str(WockySaslAuth *auth, gpointer user_data) { + return g_strdup(user_data); +} + +void +auth_success(WockySaslAuth *sasl, gpointer user_data) { + authenticated = TRUE; + /* Reopen the connection */ + wocky_xmpp_connection_open(conn, servername, NULL, "1.0"); +} + +void +auth_failed(WockySaslAuth *sasl, GQuark domain, + int code, gchar *message, gpointer user_data) { + got_error(domain, code, message); +} + +static void +parse_error(WockyXmppConnection *connection, gpointer user_data) { + fail (); +} + +static void +stream_opened(WockyXmppConnection *connection, + gchar *from, gchar *to, gchar *version, gpointer user_data) { + if (authenticated) + wocky_xmpp_connection_close(conn); +} + +static void +stream_closed(WockyXmppConnection *connection, gpointer user_data) { + run_done = TRUE; + g_main_loop_quit(mainloop); +} + +static void +received_stanza(WockyXmppConnection *connection, WockyXmppStanza *stanza, + gpointer user_data) +{ + if (sasl == NULL) { + GError *error = NULL; + sasl = wocky_sasl_auth_new(); + + g_signal_connect(sasl, "username-requested", + G_CALLBACK(return_str), (gpointer)username); + g_signal_connect(sasl, "password-requested", + G_CALLBACK(return_str), (gpointer)password); + g_signal_connect(sasl, "authentication-succeeded", + G_CALLBACK(auth_success), NULL); + g_signal_connect(sasl, "authentication-failed", + G_CALLBACK(auth_failed), NULL); + + if (!wocky_sasl_auth_authenticate(sasl, servername, + connection, stanza, + current_test->allow_plain, &error)) { + got_error(error->domain, error->code, error->message); + } + } +} + +void +run_rest(test_t *test) { + TestSaslAuthServer *server; + + servertransport = WOCKY_TRANSPORT(test_transport_new(send_hook, NULL)); + + server = test_sasl_auth_server_new(servertransport, + test->mech, username, password, + test->problem); + + authenticated = FALSE; + run_done = FALSE; + current_test = test; + + clienttransport = WOCKY_TRANSPORT(test_transport_new(send_hook, NULL)); + conn = wocky_xmpp_connection_new(clienttransport); + + g_signal_connect(conn, "parse-error", + G_CALLBACK(parse_error), NULL); + g_signal_connect(conn, "stream-opened", + G_CALLBACK(stream_opened), NULL); + g_signal_connect(conn, "stream-closed", + G_CALLBACK(stream_closed), NULL); + g_signal_connect(conn, "received-stanza", + G_CALLBACK(received_stanza), NULL); + wocky_xmpp_connection_open(conn, servername, NULL, "1.0"); + + if (!run_done) { + g_main_loop_run(mainloop); + } + + if (sasl != NULL) { + g_object_unref(sasl); + sasl = NULL; + } + g_object_unref(servertransport); + g_object_unref(clienttransport); + g_object_unref(conn); + + fail_if (test->domain == 0 && error != NULL); + fail_if (test->domain != 0 && error == NULL); + + if (error != NULL) { + fail_if (test->domain != error->domain); + fail_if (test->code != error->code); + } + + if (error != NULL) + g_error_free(error); + error = NULL; +} + +#define SUCCESS(desc, mech, allow_plain) \ + { desc, mech, allow_plain, 0, 0, SERVER_PROBLEM_NO_PROBLEM } + +#define NUMBER_OF_TEST 6 + +START_TEST (test_auth) { + test_t tests[NUMBER_OF_TEST] = { + SUCCESS("Normal authentication", NULL, TRUE), + SUCCESS("Disallow PLAIN", "PLAIN", TRUE), + SUCCESS("Plain method authentication", "PLAIN", TRUE), + SUCCESS("Normal DIGEST-MD5 authentication", "DIGEST-MD5", TRUE), + + { "No supported mechanisms", "NONSENSE", TRUE, + WOCKY_SASL_AUTH_ERROR, WOCKY_SASL_AUTH_ERROR_NO_SUPPORTED_MECHANISMS, + SERVER_PROBLEM_NO_PROBLEM }, + { "No sasl support in server", NULL, TRUE, + WOCKY_SASL_AUTH_ERROR, WOCKY_SASL_AUTH_ERROR_SASL_NOT_SUPPORTED, + SERVER_PROBLEM_NO_SASL }, + }; + + g_type_init(); + + mainloop = g_main_loop_new(NULL, FALSE); + + run_rest(&(tests[_i])); +} END_TEST + +TCase * +make_wocky_sasl_auth_tcase (void) +{ + TCase *tc = tcase_create ("SASL Auth"); + tcase_add_loop_test (tc, test_auth, 0, NUMBER_OF_TEST); + return tc; +} |