Blame gdata/tests/oauth2-authorizer.c

Packit 4b6dd7
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
Packit 4b6dd7
/*
Packit 4b6dd7
 * GData Client
Packit 4b6dd7
 * Copyright (C) Philip Withnall 2011, 2014 <philip@tecnocode.co.uk>
Packit 4b6dd7
 *
Packit 4b6dd7
 * GData Client is free software; you can redistribute it and/or
Packit 4b6dd7
 * modify it under the terms of the GNU Lesser General Public
Packit 4b6dd7
 * License as published by the Free Software Foundation; either
Packit 4b6dd7
 * version 2.1 of the License, or (at your option) any later version.
Packit 4b6dd7
 *
Packit 4b6dd7
 * GData Client is distributed in the hope that it will be useful,
Packit 4b6dd7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4b6dd7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 4b6dd7
 * Lesser General Public License for more details.
Packit 4b6dd7
 *
Packit 4b6dd7
 * You should have received a copy of the GNU Lesser General Public
Packit 4b6dd7
 * License along with GData Client.  If not, see <http://www.gnu.org/licenses/>.
Packit 4b6dd7
 */
Packit 4b6dd7
Packit 4b6dd7
#include <glib.h>
Packit 4b6dd7
#include <gdata/gdata.h>
Packit 4b6dd7
Packit 4b6dd7
#include "common.h"
Packit 4b6dd7
Packit 4b6dd7
static GThread *main_thread = NULL;
Packit 4b6dd7
static UhmServer *mock_server = NULL;
Packit 4b6dd7
Packit 4b6dd7
#undef CLIENT_ID  /* from common.h */
Packit 4b6dd7
Packit 4b6dd7
#define CLIENT_ID "352818697630-nqu2cmt5quqd6lr17ouoqmb684u84l1f.apps.googleusercontent.com"
Packit 4b6dd7
#define CLIENT_SECRET "-fA4pHQJxR3zJ-FyAMPQsikg"
Packit 4b6dd7
#define REDIRECT_URI "urn:ietf:wg:oauth:2.0:oob"
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_constructor (void)
Packit 4b6dd7
{
Packit 4b6dd7
	GDataOAuth2Authorizer *authorizer;
Packit 4b6dd7
Packit 4b6dd7
	authorizer = gdata_oauth2_authorizer_new (CLIENT_ID, CLIENT_SECRET,
Packit 4b6dd7
	                                          REDIRECT_URI,
Packit 4b6dd7
	                                          GDATA_TYPE_TASKS_SERVICE);
Packit 4b6dd7
Packit 4b6dd7
	g_assert (authorizer != NULL);
Packit 4b6dd7
	g_assert (GDATA_IS_OAUTH2_AUTHORIZER (authorizer));
Packit 4b6dd7
	g_assert (GDATA_IS_AUTHORIZER (authorizer));
Packit 4b6dd7
Packit 4b6dd7
	g_object_unref (authorizer);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_constructor_for_domains (void)
Packit 4b6dd7
{
Packit 4b6dd7
	GDataOAuth2Authorizer *authorizer;
Packit 4b6dd7
	GDataAuthorizationDomain *domain;
Packit 4b6dd7
	GList *domains;
Packit 4b6dd7
Packit 4b6dd7
	/* Try with standard domains first */
Packit 4b6dd7
	domains = gdata_service_get_authorization_domains (GDATA_TYPE_TASKS_SERVICE);
Packit 4b6dd7
	authorizer = gdata_oauth2_authorizer_new_for_authorization_domains (CLIENT_ID, CLIENT_SECRET,
Packit 4b6dd7
	                                                                    REDIRECT_URI, domains);
Packit 4b6dd7
	g_list_free (domains);
Packit 4b6dd7
Packit 4b6dd7
	g_assert (authorizer != NULL);
Packit 4b6dd7
	g_assert (GDATA_IS_OAUTH2_AUTHORIZER (authorizer));
Packit 4b6dd7
	g_assert (GDATA_IS_AUTHORIZER (authorizer));
Packit 4b6dd7
Packit 4b6dd7
	g_object_unref (authorizer);
Packit 4b6dd7
Packit 4b6dd7
	/* Try again with a custom domain. Note that, as in test_authorization_domain_properties() this should not normally happen in client code. */
Packit 4b6dd7
	domain = GDATA_AUTHORIZATION_DOMAIN (g_object_new (GDATA_TYPE_AUTHORIZATION_DOMAIN,
Packit 4b6dd7
	                                                   "service-name", "test",
Packit 4b6dd7
	                                                   "scope", "test",
Packit 4b6dd7
	                                                   NULL));
Packit 4b6dd7
Packit 4b6dd7
	domains = g_list_prepend (NULL, domain);
Packit 4b6dd7
	authorizer = gdata_oauth2_authorizer_new_for_authorization_domains (CLIENT_ID, CLIENT_SECRET,
Packit 4b6dd7
	                                                                    REDIRECT_URI, domains);
Packit 4b6dd7
	g_list_free (domains);
Packit 4b6dd7
Packit 4b6dd7
	g_assert (authorizer != NULL);
Packit 4b6dd7
	g_assert (GDATA_IS_OAUTH2_AUTHORIZER (authorizer));
Packit 4b6dd7
	g_assert (GDATA_IS_AUTHORIZER (authorizer));
Packit 4b6dd7
Packit 4b6dd7
	g_object_unref (authorizer);
Packit 4b6dd7
	g_object_unref (domain);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
typedef struct {
Packit 4b6dd7
	GDataOAuth2Authorizer *authorizer;
Packit 4b6dd7
Packit 4b6dd7
	guint locale_notification_count;
Packit 4b6dd7
	gulong locale_signal_handler;
Packit 4b6dd7
	guint proxy_resolver_notification_count;
Packit 4b6dd7
	gulong proxy_resolver_signal_handler;
Packit 4b6dd7
	guint timeout_notification_count;
Packit 4b6dd7
	gulong timeout_signal_handler;
Packit 4b6dd7
} OAuth2AuthorizerData;
Packit 4b6dd7
Packit 4b6dd7
/* Used to count that exactly the right number of notify signals are emitted when setting properties */
Packit 4b6dd7
static void
Packit 4b6dd7
notify_cb (GObject *object, GParamSpec *pspec, guint *notification_count)
Packit 4b6dd7
{
Packit 4b6dd7
	/* Check we're running in the main thread */
Packit 4b6dd7
	g_assert (g_thread_self () == main_thread);
Packit 4b6dd7
Packit 4b6dd7
	/* Increment the notification count */
Packit 4b6dd7
	*notification_count = *notification_count + 1;
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
connect_to_oauth2_authorizer (OAuth2AuthorizerData *data)
Packit 4b6dd7
{
Packit 4b6dd7
	/* Connect to notifications from the object to verify they're only emitted the correct number of times */
Packit 4b6dd7
	data->locale_signal_handler = g_signal_connect (data->authorizer, "notify::locale", (GCallback) notify_cb,
Packit 4b6dd7
	                                                &(data->locale_notification_count));
Packit 4b6dd7
	data->proxy_resolver_signal_handler = g_signal_connect (data->authorizer, "notify::proxy-resolver", (GCallback) notify_cb,
Packit 4b6dd7
	                                                        &(data->proxy_resolver_notification_count));
Packit 4b6dd7
	data->timeout_signal_handler = g_signal_connect (data->authorizer, "notify::timeout", (GCallback) notify_cb,
Packit 4b6dd7
	                                                 &(data->timeout_notification_count));
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
set_up_oauth2_authorizer_data (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	data->authorizer = gdata_oauth2_authorizer_new (CLIENT_ID,
Packit 4b6dd7
	                                                CLIENT_SECRET,
Packit 4b6dd7
	                                                REDIRECT_URI,
Packit 4b6dd7
	                                                GDATA_TYPE_TASKS_SERVICE);
Packit 4b6dd7
	connect_to_oauth2_authorizer (data);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
set_up_oauth2_authorizer_data_multiple_domains (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	GList *authorization_domains = NULL;
Packit 4b6dd7
Packit 4b6dd7
	authorization_domains = g_list_prepend (authorization_domains, gdata_tasks_service_get_primary_authorization_domain ());
Packit 4b6dd7
	authorization_domains = g_list_prepend (authorization_domains, gdata_picasaweb_service_get_primary_authorization_domain ());
Packit 4b6dd7
	data->authorizer = gdata_oauth2_authorizer_new_for_authorization_domains (CLIENT_ID, CLIENT_SECRET,
Packit 4b6dd7
	                                                                          REDIRECT_URI, authorization_domains);
Packit 4b6dd7
	g_list_free (authorization_domains);
Packit 4b6dd7
Packit 4b6dd7
	connect_to_oauth2_authorizer (data);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
set_up_oauth2_authorizer_data_locale (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	data->authorizer = gdata_oauth2_authorizer_new (CLIENT_ID,
Packit 4b6dd7
	                                                CLIENT_SECRET,
Packit 4b6dd7
	                                                REDIRECT_URI,
Packit 4b6dd7
	                                                GDATA_TYPE_TASKS_SERVICE);
Packit 4b6dd7
	gdata_oauth2_authorizer_set_locale (data->authorizer, "en_GB");
Packit 4b6dd7
	connect_to_oauth2_authorizer (data);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
set_up_oauth2_authorizer_data_authenticated (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gboolean *skip_test = (gboolean*) user_data;
Packit 4b6dd7
	gchar *authentication_uri, *authorisation_code;
Packit 4b6dd7
Packit 4b6dd7
	gdata_test_mock_server_start_trace (mock_server, "setup-oauth2-authorizer-data-authenticated");
Packit 4b6dd7
Packit 4b6dd7
	/* Chain up */
Packit 4b6dd7
	set_up_oauth2_authorizer_data (data, NULL);
Packit 4b6dd7
Packit 4b6dd7
	/* Get an authentication URI. */
Packit 4b6dd7
	authentication_uri = gdata_oauth2_authorizer_build_authentication_uri (data->authorizer, NULL, FALSE);
Packit 4b6dd7
	g_assert (authentication_uri != NULL);
Packit 4b6dd7
Packit 4b6dd7
	/* Get the authorisation code off the user. */
Packit 4b6dd7
	if (uhm_server_get_enable_online (mock_server)) {
Packit 4b6dd7
		authorisation_code = gdata_test_query_user_for_verifier (authentication_uri);
Packit 4b6dd7
Packit 4b6dd7
		g_free (authentication_uri);
Packit 4b6dd7
Packit 4b6dd7
		if (authorisation_code == NULL) {
Packit 4b6dd7
			*skip_test = TRUE;
Packit 4b6dd7
			goto skip_test;
Packit 4b6dd7
		}
Packit 4b6dd7
	} else {
Packit 4b6dd7
		/* Hard-coded default to match the trace file. */
Packit 4b6dd7
		authorisation_code = g_strdup ("4/GeYb_3HkYh4vyephp-lbvzQs1GAb.YtXAxmx-uJ0eoiIBeO6P2m9iH6kvkQI");
Packit 4b6dd7
		g_free (authentication_uri);
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	/* Authorise the token. */
Packit 4b6dd7
	g_assert (gdata_oauth2_authorizer_request_authorization (data->authorizer, authorisation_code, NULL, NULL) == TRUE);
Packit 4b6dd7
Packit 4b6dd7
skip_test:
Packit 4b6dd7
	g_free (authorisation_code);
Packit 4b6dd7
Packit 4b6dd7
	uhm_server_end_trace (mock_server);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
tear_down_oauth2_authorizer_data (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	/* Clean up signal handlers */
Packit 4b6dd7
	g_signal_handler_disconnect (data->authorizer, data->timeout_signal_handler);
Packit 4b6dd7
	g_signal_handler_disconnect (data->authorizer, data->proxy_resolver_signal_handler);
Packit 4b6dd7
	g_signal_handler_disconnect (data->authorizer, data->locale_signal_handler);
Packit 4b6dd7
Packit 4b6dd7
	g_object_unref (data->authorizer);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test getting and setting the client-id property */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_properties_client_id (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gchar *client_id;
Packit 4b6dd7
Packit 4b6dd7
	/* Verifying the normal state of the property in a newly-constructed instance of GDataOAuth2Authorizer */
Packit 4b6dd7
	g_assert_cmpstr (gdata_oauth2_authorizer_get_client_id (data->authorizer), ==, CLIENT_ID);
Packit 4b6dd7
Packit 4b6dd7
	g_object_get (data->authorizer, "client-id", &client_id, NULL);
Packit 4b6dd7
	g_assert_cmpstr (client_id, ==, CLIENT_ID);
Packit 4b6dd7
	g_free (client_id);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test getting and setting the client-id property */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_properties_client_secret (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gchar *client_secret;
Packit 4b6dd7
Packit 4b6dd7
	/* Verifying the normal state of the property in a newly-constructed instance of GDataOAuth2Authorizer */
Packit 4b6dd7
	g_assert_cmpstr (gdata_oauth2_authorizer_get_client_secret (data->authorizer), ==, CLIENT_SECRET);
Packit 4b6dd7
Packit 4b6dd7
	g_object_get (data->authorizer, "client-secret", &client_secret, NULL);
Packit 4b6dd7
	g_assert_cmpstr (client_secret, ==, CLIENT_SECRET);
Packit 4b6dd7
	g_free (client_secret);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test getting and setting the client-id property */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_properties_redirect_uri (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gchar *redirect_uri;
Packit 4b6dd7
Packit 4b6dd7
	/* Verifying the normal state of the property in a newly-constructed instance of GDataOAuth2Authorizer */
Packit 4b6dd7
	g_assert_cmpstr (gdata_oauth2_authorizer_get_redirect_uri (data->authorizer), ==, REDIRECT_URI);
Packit 4b6dd7
Packit 4b6dd7
	g_object_get (data->authorizer, "redirect-uri", &redirect_uri, NULL);
Packit 4b6dd7
	g_assert_cmpstr (redirect_uri, ==, REDIRECT_URI);
Packit 4b6dd7
	g_free (redirect_uri);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test getting and setting the locale property */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_properties_locale (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gchar *locale;
Packit 4b6dd7
Packit 4b6dd7
	/* Verifying the normal state of the property in a newly-constructed instance of GDataOAuth2Authorizer */
Packit 4b6dd7
	g_assert_cmpstr (gdata_oauth2_authorizer_get_locale (data->authorizer), ==, NULL);
Packit 4b6dd7
Packit 4b6dd7
	g_object_get (data->authorizer, "locale", &locale, NULL);
Packit 4b6dd7
	g_assert_cmpstr (locale, ==, NULL);
Packit 4b6dd7
	g_free (locale);
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpuint (data->locale_notification_count, ==, 0);
Packit 4b6dd7
Packit 4b6dd7
	/* Check setting it works and emits a notification */
Packit 4b6dd7
	gdata_oauth2_authorizer_set_locale (data->authorizer, "en");
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpuint (data->locale_notification_count, ==, 1);
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpstr (gdata_oauth2_authorizer_get_locale (data->authorizer), ==, "en");
Packit 4b6dd7
Packit 4b6dd7
	g_object_get (data->authorizer, "locale", &locale, NULL);
Packit 4b6dd7
	g_assert_cmpstr (locale, ==, "en");
Packit 4b6dd7
	g_free (locale);
Packit 4b6dd7
Packit 4b6dd7
	/* Check setting it to the same value is a no-op */
Packit 4b6dd7
	gdata_oauth2_authorizer_set_locale (data->authorizer, "en");
Packit 4b6dd7
	g_assert_cmpuint (data->locale_notification_count, ==, 1);
Packit 4b6dd7
Packit 4b6dd7
	/* Check setting it back to NULL works */
Packit 4b6dd7
	gdata_oauth2_authorizer_set_locale (data->authorizer, NULL);
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpuint (data->locale_notification_count, ==, 2);
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpstr (gdata_oauth2_authorizer_get_locale (data->authorizer), ==, NULL);
Packit 4b6dd7
Packit 4b6dd7
	g_object_get (data->authorizer, "locale", &locale, NULL);
Packit 4b6dd7
	g_assert_cmpstr (locale, ==, NULL);
Packit 4b6dd7
	g_free (locale);
Packit 4b6dd7
Packit 4b6dd7
	/* Test that setting it using g_object_set() works */
Packit 4b6dd7
	g_object_set (data->authorizer, "locale", "de", NULL);
Packit 4b6dd7
	g_assert_cmpstr (gdata_oauth2_authorizer_get_locale (data->authorizer), ==, "de");
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test getting and setting the timeout property */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_properties_timeout (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	guint timeout;
Packit 4b6dd7
Packit 4b6dd7
	/* Verifying the normal state of the property in a newly-constructed instance of GDataOAuth2Authorizer */
Packit 4b6dd7
	g_assert_cmpuint (gdata_oauth2_authorizer_get_timeout (data->authorizer), ==, 0);
Packit 4b6dd7
Packit 4b6dd7
	g_object_get (data->authorizer, "timeout", &timeout, NULL);
Packit 4b6dd7
	g_assert_cmpuint (timeout, ==, 0);
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpuint (data->timeout_notification_count, ==, 0);
Packit 4b6dd7
Packit 4b6dd7
	/* Check setting it works and emits a notification */
Packit 4b6dd7
	gdata_oauth2_authorizer_set_timeout (data->authorizer, 30);
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpuint (data->timeout_notification_count, ==, 1);
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpuint (gdata_oauth2_authorizer_get_timeout (data->authorizer), ==, 30);
Packit 4b6dd7
Packit 4b6dd7
	g_object_get (data->authorizer, "timeout", &timeout, NULL);
Packit 4b6dd7
	g_assert_cmpuint (timeout, ==, 30);
Packit 4b6dd7
Packit 4b6dd7
	/* Check setting it to the same value is a no-op */
Packit 4b6dd7
	gdata_oauth2_authorizer_set_timeout (data->authorizer, 30);
Packit 4b6dd7
	g_assert_cmpuint (data->timeout_notification_count, ==, 1);
Packit 4b6dd7
Packit 4b6dd7
	/* Check setting it back to 0 works */
Packit 4b6dd7
	gdata_oauth2_authorizer_set_timeout (data->authorizer, 0);
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpuint (data->timeout_notification_count, ==, 2);
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpuint (gdata_oauth2_authorizer_get_timeout (data->authorizer), ==, 0);
Packit 4b6dd7
Packit 4b6dd7
	g_object_get (data->authorizer, "timeout", &timeout, NULL);
Packit 4b6dd7
	g_assert_cmpuint (timeout, ==, 0);
Packit 4b6dd7
Packit 4b6dd7
	/* Test that setting it using g_object_set() works */
Packit 4b6dd7
	g_object_set (data->authorizer, "timeout", 15, NULL);
Packit 4b6dd7
	g_assert_cmpuint (gdata_oauth2_authorizer_get_timeout (data->authorizer), ==, 15);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test getting and setting the proxy-resolver property */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_properties_proxy_resolver (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	GProxyResolver *old_proxy_resolver, *proxy_resolver, *new_proxy_resolver;
Packit 4b6dd7
Packit 4b6dd7
	/* Verifying the normal state of the property in a newly-constructed instance of GDataOAuth2Authorizer.
Packit 4b6dd7
	 * Since the resolver comes from the SoupSession, we don’t know whether it’s initially NULL. */
Packit 4b6dd7
	old_proxy_resolver = gdata_oauth2_authorizer_get_proxy_resolver (data->authorizer);
Packit 4b6dd7
Packit 4b6dd7
	g_object_get (data->authorizer, "proxy-resolver", &proxy_resolver, NULL);
Packit 4b6dd7
	g_assert (proxy_resolver == old_proxy_resolver);
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpuint (data->proxy_resolver_notification_count, ==, 0);
Packit 4b6dd7
Packit 4b6dd7
	/* Check setting it works and emits a notification */
Packit 4b6dd7
	new_proxy_resolver = g_object_ref (g_proxy_resolver_get_default ());
Packit 4b6dd7
	gdata_oauth2_authorizer_set_proxy_resolver (data->authorizer, new_proxy_resolver);
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpuint (data->proxy_resolver_notification_count, ==, 1);
Packit 4b6dd7
Packit 4b6dd7
	g_assert (gdata_oauth2_authorizer_get_proxy_resolver (data->authorizer) != NULL);
Packit 4b6dd7
	g_assert (gdata_oauth2_authorizer_get_proxy_resolver (data->authorizer) == new_proxy_resolver);
Packit 4b6dd7
Packit 4b6dd7
	g_object_get (data->authorizer, "proxy-resolver", &proxy_resolver, NULL);
Packit 4b6dd7
	g_assert (proxy_resolver != NULL);
Packit 4b6dd7
	g_assert (gdata_oauth2_authorizer_get_proxy_resolver (data->authorizer) == new_proxy_resolver);
Packit 4b6dd7
	g_object_unref (proxy_resolver);
Packit 4b6dd7
Packit 4b6dd7
	g_object_unref (new_proxy_resolver);
Packit 4b6dd7
Packit 4b6dd7
	/* Check setting it back to NULL works */
Packit 4b6dd7
	gdata_oauth2_authorizer_set_proxy_resolver (data->authorizer, NULL);
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpuint (data->proxy_resolver_notification_count, ==, 2);
Packit 4b6dd7
Packit 4b6dd7
	g_assert (gdata_oauth2_authorizer_get_proxy_resolver (data->authorizer) == NULL);
Packit 4b6dd7
Packit 4b6dd7
	g_object_get (data->authorizer, "proxy-resolver", &proxy_resolver, NULL);
Packit 4b6dd7
	g_assert (proxy_resolver == NULL);
Packit 4b6dd7
Packit 4b6dd7
	/* Test that setting it using g_object_set() works */
Packit 4b6dd7
	new_proxy_resolver = g_object_ref (g_proxy_resolver_get_default ());
Packit 4b6dd7
	g_object_set (data->authorizer, "proxy-resolver", new_proxy_resolver, NULL);
Packit 4b6dd7
	g_object_unref (new_proxy_resolver);
Packit 4b6dd7
Packit 4b6dd7
	g_assert (gdata_oauth2_authorizer_get_proxy_resolver (data->authorizer) != NULL);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test that gdata_authorizer_refresh_authorization() is a no-op when
Packit 4b6dd7
 * unauthenticated. */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_refresh_authorization_unauthenticated (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gboolean *skip_test = (gboolean*) user_data;
Packit 4b6dd7
	GError *error = NULL;
Packit 4b6dd7
Packit 4b6dd7
	gdata_test_mock_server_start_trace (mock_server, "oauth2-authorizer-refresh-authorization-unauthorized");
Packit 4b6dd7
Packit 4b6dd7
	/* Skip the test if the user's requested */
Packit 4b6dd7
	if (skip_test != NULL && *skip_test == TRUE) {
Packit 4b6dd7
		return;
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	g_assert (gdata_authorizer_refresh_authorization (GDATA_AUTHORIZER (data->authorizer), NULL, &error) == FALSE);
Packit 4b6dd7
	g_assert_no_error (error);
Packit 4b6dd7
	g_clear_error (&error);
Packit 4b6dd7
Packit 4b6dd7
	uhm_server_end_trace (mock_server);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test that gdata_authorizer_refresh_authorization() works when authenticated. */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_refresh_authorization_authenticated (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gboolean *skip_test = (gboolean*) user_data;
Packit 4b6dd7
	GError *error = NULL;
Packit 4b6dd7
Packit 4b6dd7
	gdata_test_mock_server_start_trace (mock_server, "oauth2-authorizer-refresh-authorization-authorized");
Packit 4b6dd7
Packit 4b6dd7
	/* Skip the test if the user's requested */
Packit 4b6dd7
	if (skip_test != NULL && *skip_test == TRUE) {
Packit 4b6dd7
		return;
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	g_assert (gdata_authorizer_refresh_authorization (GDATA_AUTHORIZER (data->authorizer), NULL, &error) == TRUE);
Packit 4b6dd7
	g_assert_no_error (error);
Packit 4b6dd7
	g_clear_error (&error);
Packit 4b6dd7
Packit 4b6dd7
	uhm_server_end_trace (mock_server);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test that processing a request with a NULL domain will not change the request. */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_process_request_null (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	SoupMessage *message;
Packit 4b6dd7
	SoupMessageHeadersIter iter;
Packit 4b6dd7
	guint header_count = 0;
Packit 4b6dd7
	const gchar *name, *value;
Packit 4b6dd7
Packit 4b6dd7
	/* Create a new message with an empty set of request headers */
Packit 4b6dd7
	message = soup_message_new (SOUP_METHOD_GET, "https://example.com/");
Packit 4b6dd7
Packit 4b6dd7
	/* Process the message */
Packit 4b6dd7
	gdata_authorizer_process_request (GDATA_AUTHORIZER (data->authorizer), NULL, message);
Packit 4b6dd7
Packit 4b6dd7
	/* Check that the set of request headers is still empty */
Packit 4b6dd7
	soup_message_headers_iter_init (&iter, message->request_headers);
Packit 4b6dd7
Packit 4b6dd7
	while (soup_message_headers_iter_next (&iter, &name, &value) == TRUE) {
Packit 4b6dd7
		header_count++;
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpuint (header_count, ==, 0);
Packit 4b6dd7
Packit 4b6dd7
	g_object_unref (message);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test that processing a request with an authorizer which hasn't been authenticated yet will not change the request. */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_process_request_unauthenticated (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	SoupMessage *message;
Packit 4b6dd7
	SoupMessageHeadersIter iter;
Packit 4b6dd7
	guint header_count = 0;
Packit 4b6dd7
	const gchar *name, *value;
Packit 4b6dd7
Packit 4b6dd7
	/* Create a new message with an empty set of request headers */
Packit 4b6dd7
	message = soup_message_new (SOUP_METHOD_GET, "https://example.com/");
Packit 4b6dd7
Packit 4b6dd7
	/* Process the message */
Packit 4b6dd7
	gdata_authorizer_process_request (GDATA_AUTHORIZER (data->authorizer), gdata_tasks_service_get_primary_authorization_domain (), message);
Packit 4b6dd7
Packit 4b6dd7
	/* Check that the set of request headers is still empty */
Packit 4b6dd7
	soup_message_headers_iter_init (&iter, message->request_headers);
Packit 4b6dd7
Packit 4b6dd7
	while (soup_message_headers_iter_next (&iter, &name, &value) == TRUE) {
Packit 4b6dd7
		header_count++;
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpuint (header_count, ==, 0);
Packit 4b6dd7
Packit 4b6dd7
	g_object_unref (message);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test that processing a request with an authorizer which has been authenticated will change the request. */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_process_request_authenticated (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gboolean *skip_test = (gboolean*) user_data;
Packit 4b6dd7
	SoupMessage *message;
Packit 4b6dd7
	SoupMessageHeadersIter iter;
Packit 4b6dd7
	guint header_count = 0;
Packit 4b6dd7
	const gchar *name, *value;
Packit 4b6dd7
Packit 4b6dd7
	/* Skip the test if the user's requested */
Packit 4b6dd7
	if (skip_test != NULL && *skip_test == TRUE) {
Packit 4b6dd7
		return;
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	/* Create a new message with an empty set of request headers */
Packit 4b6dd7
	message = soup_message_new (SOUP_METHOD_GET, "https://example.com/");
Packit 4b6dd7
Packit 4b6dd7
	/* Process the message */
Packit 4b6dd7
	gdata_authorizer_process_request (GDATA_AUTHORIZER (data->authorizer), gdata_tasks_service_get_primary_authorization_domain (), message);
Packit 4b6dd7
Packit 4b6dd7
	/* Check that at least one new header has been set */
Packit 4b6dd7
	soup_message_headers_iter_init (&iter, message->request_headers);
Packit 4b6dd7
Packit 4b6dd7
	while (soup_message_headers_iter_next (&iter, &name, &value) == TRUE) {
Packit 4b6dd7
		header_count++;
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	g_assert_cmpuint (header_count, >, 0);
Packit 4b6dd7
Packit 4b6dd7
	g_object_unref (message);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test that building an authentication URI works correctly */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_build_authentication_uri_default (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gchar *authentication_uri;
Packit 4b6dd7
Packit 4b6dd7
	authentication_uri = gdata_oauth2_authorizer_build_authentication_uri (data->authorizer, NULL, FALSE);
Packit 4b6dd7
	g_assert (authentication_uri != NULL && *authentication_uri != '\0');
Packit 4b6dd7
Packit 4b6dd7
	g_test_message ("Building an authentication URI gave “%s”.",
Packit 4b6dd7
	                authentication_uri);
Packit 4b6dd7
Packit 4b6dd7
	g_free (authentication_uri);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test that building an authentication URI with a login hint works
Packit 4b6dd7
 * correctly. */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_build_authentication_uri_hint (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gchar *authentication_uri;
Packit 4b6dd7
Packit 4b6dd7
	authentication_uri = gdata_oauth2_authorizer_build_authentication_uri (data->authorizer, "test.user@gmail.com", FALSE);
Packit 4b6dd7
	g_assert (authentication_uri != NULL && *authentication_uri != '\0');
Packit 4b6dd7
Packit 4b6dd7
	g_test_message ("Building an authentication URI gave “%s”.",
Packit 4b6dd7
	                authentication_uri);
Packit 4b6dd7
Packit 4b6dd7
	g_free (authentication_uri);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test that buildig an authentication URI with a login hint and incremental
Packit 4b6dd7
 * authentication works correctly. */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_build_authentication_uri_incremental (OAuth2AuthorizerData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gchar *authentication_uri;
Packit 4b6dd7
Packit 4b6dd7
	authentication_uri = gdata_oauth2_authorizer_build_authentication_uri (data->authorizer, "test.user@gmail.com", TRUE);
Packit 4b6dd7
	g_assert (authentication_uri != NULL && *authentication_uri != '\0');
Packit 4b6dd7
Packit 4b6dd7
	g_test_message ("Building an authentication URI gave “%s”.",
Packit 4b6dd7
	                authentication_uri);
Packit 4b6dd7
Packit 4b6dd7
	g_free (authentication_uri);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
typedef struct {
Packit 4b6dd7
	OAuth2AuthorizerData parent;
Packit 4b6dd7
	gchar *authorisation_code;
Packit 4b6dd7
} OAuth2AuthorizerInteractiveData;
Packit 4b6dd7
Packit 4b6dd7
/* NOTE: Any consumer of this data has to check for (data->authorisation_code == NULL) and skip the test in that case */
Packit 4b6dd7
static void
Packit 4b6dd7
set_up_oauth2_authorizer_interactive_data (OAuth2AuthorizerInteractiveData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gchar *authentication_uri;
Packit 4b6dd7
Packit 4b6dd7
	/* Chain up */
Packit 4b6dd7
	set_up_oauth2_authorizer_data ((OAuth2AuthorizerData*) data, user_data);
Packit 4b6dd7
Packit 4b6dd7
	/* Get an authentication URI */
Packit 4b6dd7
	authentication_uri = gdata_oauth2_authorizer_build_authentication_uri (data->parent.authorizer, NULL, FALSE);
Packit 4b6dd7
	g_assert (authentication_uri != NULL);
Packit 4b6dd7
Packit 4b6dd7
	/* Wait for the user to retrieve and enter the authorisation code. */
Packit 4b6dd7
	if (uhm_server_get_enable_online (mock_server)) {
Packit 4b6dd7
		data->authorisation_code = gdata_test_query_user_for_verifier (authentication_uri);
Packit 4b6dd7
	} else {
Packit 4b6dd7
		/* Hard-coded default to match the trace file. */
Packit 4b6dd7
		data->authorisation_code = g_strdup ((const gchar *) user_data);
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	g_free (authentication_uri);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
set_up_oauth2_authorizer_interactive_data_bad_credentials (OAuth2AuthorizerInteractiveData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gchar *authentication_uri;
Packit 4b6dd7
Packit 4b6dd7
	/* Chain up */
Packit 4b6dd7
	set_up_oauth2_authorizer_data ((OAuth2AuthorizerData*) data, user_data);
Packit 4b6dd7
Packit 4b6dd7
	/* Get an authentication URI */
Packit 4b6dd7
	authentication_uri = gdata_oauth2_authorizer_build_authentication_uri (data->parent.authorizer, NULL, FALSE);
Packit 4b6dd7
	g_assert (authentication_uri != NULL);
Packit 4b6dd7
Packit 4b6dd7
	/* Give a bogus authorisation code. */
Packit 4b6dd7
	data->authorisation_code = g_strdup ("test");
Packit 4b6dd7
Packit 4b6dd7
	g_free (authentication_uri);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
tear_down_oauth2_authorizer_interactive_data (OAuth2AuthorizerInteractiveData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	g_free (data->authorisation_code);
Packit 4b6dd7
Packit 4b6dd7
	/* Chain up */
Packit 4b6dd7
	tear_down_oauth2_authorizer_data ((OAuth2AuthorizerData*) data, user_data);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test that synchronously authorizing an authorisation code is successful. Note
Packit 4b6dd7
 * that this test has to be interactive, as the user has to visit the
Packit 4b6dd7
 * authentication URI to retrieve an authorisation code. */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_request_authorization_sync (OAuth2AuthorizerInteractiveData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gboolean success;
Packit 4b6dd7
	GError *error = NULL;
Packit 4b6dd7
Packit 4b6dd7
	/* Skip the test if the user's requested */
Packit 4b6dd7
	if (data->authorisation_code == NULL) {
Packit 4b6dd7
		return;
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	gdata_test_mock_server_start_trace (mock_server, "oauth2-authorizer-request-authorization-sync");
Packit 4b6dd7
Packit 4b6dd7
	/* Check we're not authorised beforehand */
Packit 4b6dd7
	g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (data->parent.authorizer),
Packit 4b6dd7
	          gdata_tasks_service_get_primary_authorization_domain ()) == FALSE);
Packit 4b6dd7
Packit 4b6dd7
	/* Test that authorising the token retrieved previously is successful */
Packit 4b6dd7
	success = gdata_oauth2_authorizer_request_authorization (data->parent.authorizer, data->authorisation_code, NULL,
Packit 4b6dd7
	                                                         &error);
Packit 4b6dd7
	g_assert_no_error (error);
Packit 4b6dd7
	g_assert (success == TRUE);
Packit 4b6dd7
	g_clear_error (&error);
Packit 4b6dd7
Packit 4b6dd7
	/* Are we authorised now? */
Packit 4b6dd7
	g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (data->parent.authorizer),
Packit 4b6dd7
	          gdata_tasks_service_get_primary_authorization_domain ()) == TRUE);
Packit 4b6dd7
Packit 4b6dd7
	uhm_server_end_trace (mock_server);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test that synchronously authorizing fails if an invalid authorisation code is
Packit 4b6dd7
 * provided. */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_request_authorization_sync_bad_credentials (OAuth2AuthorizerInteractiveData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gboolean success;
Packit 4b6dd7
	GError *error = NULL;
Packit 4b6dd7
Packit 4b6dd7
	gdata_test_mock_server_start_trace (mock_server, "oauth2-authorizer-request-authorization-sync-bad-credentials");
Packit 4b6dd7
Packit 4b6dd7
	/* Check we're not authorised beforehand */
Packit 4b6dd7
	g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (data->parent.authorizer),
Packit 4b6dd7
	          gdata_tasks_service_get_primary_authorization_domain ()) == FALSE);
Packit 4b6dd7
Packit 4b6dd7
	/* Test that authorising the token retrieved above fails */
Packit 4b6dd7
	success = gdata_oauth2_authorizer_request_authorization (data->parent.authorizer, data->authorisation_code, NULL,
Packit 4b6dd7
	                                                         &error);
Packit 4b6dd7
	g_assert_error (error, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_FORBIDDEN);
Packit 4b6dd7
	g_assert (success == FALSE);
Packit 4b6dd7
	g_clear_error (&error);
Packit 4b6dd7
Packit 4b6dd7
	/* Are we authorised now? */
Packit 4b6dd7
	g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (data->parent.authorizer),
Packit 4b6dd7
	          gdata_tasks_service_get_primary_authorization_domain ()) == FALSE);
Packit 4b6dd7
Packit 4b6dd7
	uhm_server_end_trace (mock_server);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test that cancellation of synchronously authorizing works. Note that this
Packit 4b6dd7
 * test has to be interactive, as the user has to visit the authentication URI
Packit 4b6dd7
 * to retrieve an authorisation code. */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_request_authorization_sync_cancellation (OAuth2AuthorizerInteractiveData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gboolean success;
Packit 4b6dd7
	GCancellable *cancellable;
Packit 4b6dd7
	GError *error = NULL;
Packit 4b6dd7
Packit 4b6dd7
	/* Skip the test if the user's requested */
Packit 4b6dd7
	if (data->authorisation_code == NULL) {
Packit 4b6dd7
		return;
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	gdata_test_mock_server_start_trace (mock_server, "oauth2-authorizer-request-authorization-sync-cancellation");
Packit 4b6dd7
Packit 4b6dd7
	/* Check we're not authorised beforehand */
Packit 4b6dd7
	g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (data->parent.authorizer),
Packit 4b6dd7
	          gdata_tasks_service_get_primary_authorization_domain ()) == FALSE);
Packit 4b6dd7
Packit 4b6dd7
	/* Create the cancellable */
Packit 4b6dd7
	cancellable = g_cancellable_new ();
Packit 4b6dd7
Packit 4b6dd7
	/* Test that authorising the code retrieved above is unsuccessful */
Packit 4b6dd7
	g_cancellable_cancel (cancellable);
Packit 4b6dd7
	success = gdata_oauth2_authorizer_request_authorization (data->parent.authorizer, data->authorisation_code,
Packit 4b6dd7
	                                                         cancellable, &error);
Packit 4b6dd7
	g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
Packit 4b6dd7
	g_assert (success == FALSE);
Packit 4b6dd7
	g_clear_error (&error);
Packit 4b6dd7
Packit 4b6dd7
	/* Are we authorised now? */
Packit 4b6dd7
	g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (data->parent.authorizer),
Packit 4b6dd7
	          gdata_tasks_service_get_primary_authorization_domain ()) == FALSE);
Packit 4b6dd7
Packit 4b6dd7
	g_object_unref (cancellable);
Packit 4b6dd7
Packit 4b6dd7
	uhm_server_end_trace (mock_server);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
typedef struct {
Packit 4b6dd7
	OAuth2AuthorizerInteractiveData parent;
Packit 4b6dd7
	GMainLoop *main_loop;
Packit 4b6dd7
} OAuth2AuthorizerInteractiveAsyncData;
Packit 4b6dd7
Packit 4b6dd7
/* NOTE: Any consumer of this data has to check for
Packit 4b6dd7
 * (data->authorisation_code == NULL) and skip the test in that case */
Packit 4b6dd7
static void
Packit 4b6dd7
set_up_oauth2_authorizer_interactive_async_data (OAuth2AuthorizerInteractiveAsyncData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	/* Chain up */
Packit 4b6dd7
	set_up_oauth2_authorizer_interactive_data ((OAuth2AuthorizerInteractiveData*) data, user_data);
Packit 4b6dd7
Packit 4b6dd7
	/* Set up the main loop */
Packit 4b6dd7
	data->main_loop = g_main_loop_new (NULL, FALSE);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
set_up_oauth2_authorizer_interactive_async_data_bad_credentials (OAuth2AuthorizerInteractiveAsyncData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	/* Chain up */
Packit 4b6dd7
	set_up_oauth2_authorizer_interactive_data_bad_credentials ((OAuth2AuthorizerInteractiveData*) data, user_data);
Packit 4b6dd7
Packit 4b6dd7
	/* Set up the main loop */
Packit 4b6dd7
	data->main_loop = g_main_loop_new (NULL, FALSE);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
tear_down_oauth2_authorizer_interactive_async_data (OAuth2AuthorizerInteractiveAsyncData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	g_main_loop_unref (data->main_loop);
Packit 4b6dd7
Packit 4b6dd7
	/* Chain up */
Packit 4b6dd7
	tear_down_oauth2_authorizer_interactive_data ((OAuth2AuthorizerInteractiveData*) data, user_data);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_request_authorization_async_cb (GDataOAuth2Authorizer *authorizer, GAsyncResult *async_result,
Packit 4b6dd7
                                                       OAuth2AuthorizerInteractiveAsyncData *data)
Packit 4b6dd7
{
Packit 4b6dd7
	gboolean success;
Packit 4b6dd7
	GError *error = NULL;
Packit 4b6dd7
Packit 4b6dd7
	success = gdata_oauth2_authorizer_request_authorization_finish (authorizer, async_result, &error);
Packit 4b6dd7
	g_assert_no_error (error);
Packit 4b6dd7
	g_assert (success == TRUE);
Packit 4b6dd7
	g_clear_error (&error);
Packit 4b6dd7
Packit 4b6dd7
	/* Are we authorised now? */
Packit 4b6dd7
	g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (authorizer),
Packit 4b6dd7
	          gdata_tasks_service_get_primary_authorization_domain ()) == TRUE);
Packit 4b6dd7
Packit 4b6dd7
	g_main_loop_quit (data->main_loop);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test that asynchronously authorizing works. Note that this test has to be
Packit 4b6dd7
 * interactive, as the user has to visit the authentication URI to retrieve an
Packit 4b6dd7
 * authorisation code. */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_request_authorization_async (OAuth2AuthorizerInteractiveAsyncData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	/* Skip the test if the user's requested */
Packit 4b6dd7
	if (data->parent.authorisation_code == NULL) {
Packit 4b6dd7
		return;
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	gdata_test_mock_server_start_trace (mock_server, "oauth2-authorizer-request-authorization-async");
Packit 4b6dd7
Packit 4b6dd7
	/* Check we're not authorised beforehand */
Packit 4b6dd7
	g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (data->parent.parent.authorizer),
Packit 4b6dd7
	          gdata_tasks_service_get_primary_authorization_domain ()) == FALSE);
Packit 4b6dd7
Packit 4b6dd7
	/* Create a main loop and request authorization */
Packit 4b6dd7
	gdata_oauth2_authorizer_request_authorization_async (data->parent.parent.authorizer,
Packit 4b6dd7
	                                                     data->parent.authorisation_code,
Packit 4b6dd7
	                                                     NULL,
Packit 4b6dd7
	                                                     (GAsyncReadyCallback) test_oauth2_authorizer_request_authorization_async_cb, data);
Packit 4b6dd7
Packit 4b6dd7
	g_main_loop_run (data->main_loop);
Packit 4b6dd7
Packit 4b6dd7
	uhm_server_end_trace (mock_server);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_request_authorization_async_bad_credentials_cb (GDataOAuth2Authorizer *authorizer, GAsyncResult *async_result,
Packit 4b6dd7
                                                                       OAuth2AuthorizerInteractiveAsyncData *data)
Packit 4b6dd7
{
Packit 4b6dd7
	gboolean success;
Packit 4b6dd7
	GError *error = NULL;
Packit 4b6dd7
Packit 4b6dd7
	success = gdata_oauth2_authorizer_request_authorization_finish (authorizer, async_result, &error);
Packit 4b6dd7
	g_assert_error (error, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_FORBIDDEN);
Packit 4b6dd7
	g_assert (success == FALSE);
Packit 4b6dd7
	g_clear_error (&error);
Packit 4b6dd7
Packit 4b6dd7
	/* Are we authorised now? */
Packit 4b6dd7
	g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (authorizer),
Packit 4b6dd7
	          gdata_tasks_service_get_primary_authorization_domain ()) == FALSE);
Packit 4b6dd7
Packit 4b6dd7
	g_main_loop_quit (data->main_loop);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test that asynchronously authorizing fails if an invalid authorisation code
Packit 4b6dd7
 * is provided. */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_request_authorization_async_bad_credentials (OAuth2AuthorizerInteractiveAsyncData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	gdata_test_mock_server_start_trace (mock_server, "oauth2-authorizer-request-authorization-async-bad-credentials");
Packit 4b6dd7
Packit 4b6dd7
	/* Check we're not authorised beforehand */
Packit 4b6dd7
	g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (data->parent.parent.authorizer),
Packit 4b6dd7
	          gdata_tasks_service_get_primary_authorization_domain ()) == FALSE);
Packit 4b6dd7
Packit 4b6dd7
	/* Create a main loop and request authorization */
Packit 4b6dd7
	gdata_oauth2_authorizer_request_authorization_async (data->parent.parent.authorizer,
Packit 4b6dd7
	                                                     data->parent.authorisation_code,
Packit 4b6dd7
	                                                     NULL,
Packit 4b6dd7
	                                                     (GAsyncReadyCallback) test_oauth2_authorizer_request_authorization_async_bad_credentials_cb,
Packit 4b6dd7
	                                                     data);
Packit 4b6dd7
Packit 4b6dd7
	g_main_loop_run (data->main_loop);
Packit 4b6dd7
Packit 4b6dd7
	uhm_server_end_trace (mock_server);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_request_authorization_async_cancellation_cb (GDataOAuth2Authorizer *authorizer, GAsyncResult *async_result,
Packit 4b6dd7
                                                                    OAuth2AuthorizerInteractiveAsyncData *data)
Packit 4b6dd7
{
Packit 4b6dd7
	gboolean success;
Packit 4b6dd7
	GError *error = NULL;
Packit 4b6dd7
Packit 4b6dd7
	success = gdata_oauth2_authorizer_request_authorization_finish (authorizer, async_result, &error);
Packit 4b6dd7
	g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
Packit 4b6dd7
	g_assert (success == FALSE);
Packit 4b6dd7
	g_clear_error (&error);
Packit 4b6dd7
Packit 4b6dd7
	/* Are we authorised now? */
Packit 4b6dd7
	g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (authorizer),
Packit 4b6dd7
	          gdata_tasks_service_get_primary_authorization_domain ()) == FALSE);
Packit 4b6dd7
Packit 4b6dd7
	g_main_loop_quit (data->main_loop);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
/* Test that cancelling asynchronously authorizing works. Note that this test
Packit 4b6dd7
 * has to be interactive, as the user has to visit the authentication URI to
Packit 4b6dd7
 * retrieve an authorisation code. */
Packit 4b6dd7
static void
Packit 4b6dd7
test_oauth2_authorizer_request_authorization_async_cancellation (OAuth2AuthorizerInteractiveAsyncData *data, gconstpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	GCancellable *cancellable;
Packit 4b6dd7
Packit 4b6dd7
	/* Skip the test if the user's requested */
Packit 4b6dd7
	if (data->parent.authorisation_code == NULL) {
Packit 4b6dd7
		return;
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	gdata_test_mock_server_start_trace (mock_server, "oauth2-authorizer-request-authorization-async-cancellation");
Packit 4b6dd7
Packit 4b6dd7
	/* Check we're not authorised beforehand */
Packit 4b6dd7
	g_assert (gdata_authorizer_is_authorized_for_domain (GDATA_AUTHORIZER (data->parent.parent.authorizer),
Packit 4b6dd7
	          gdata_tasks_service_get_primary_authorization_domain ()) == FALSE);
Packit 4b6dd7
Packit 4b6dd7
	/* Create the cancellable */
Packit 4b6dd7
	cancellable = g_cancellable_new ();
Packit 4b6dd7
Packit 4b6dd7
	/* Create a main loop and request authorization */
Packit 4b6dd7
	gdata_oauth2_authorizer_request_authorization_async (data->parent.parent.authorizer,
Packit 4b6dd7
	                                                     data->parent.authorisation_code,
Packit 4b6dd7
	                                                     cancellable,
Packit 4b6dd7
	                                                     (GAsyncReadyCallback) test_oauth2_authorizer_request_authorization_async_cancellation_cb,
Packit 4b6dd7
	                                                     data);
Packit 4b6dd7
	g_cancellable_cancel (cancellable);
Packit 4b6dd7
Packit 4b6dd7
	g_main_loop_run (data->main_loop);
Packit 4b6dd7
Packit 4b6dd7
	g_object_unref (cancellable);
Packit 4b6dd7
Packit 4b6dd7
	uhm_server_end_trace (mock_server);
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
static void
Packit 4b6dd7
mock_server_notify_resolver_cb (GObject *object, GParamSpec *pspec,
Packit 4b6dd7
                                gpointer user_data)
Packit 4b6dd7
{
Packit 4b6dd7
	UhmServer *server;
Packit 4b6dd7
	UhmResolver *resolver;
Packit 4b6dd7
Packit 4b6dd7
	server = UHM_SERVER (object);
Packit 4b6dd7
Packit 4b6dd7
	/* Set up the expected domain names here. This should technically be
Packit 4b6dd7
	 * split up between the different unit test suites, but that’s too much
Packit 4b6dd7
	 * effort. */
Packit 4b6dd7
	resolver = uhm_server_get_resolver (server);
Packit 4b6dd7
Packit 4b6dd7
	if (resolver != NULL) {
Packit 4b6dd7
		const gchar *ip_address = uhm_server_get_address (server);
Packit 4b6dd7
Packit 4b6dd7
		uhm_resolver_add_A (resolver, "www.google.com", ip_address);
Packit 4b6dd7
		uhm_resolver_add_A (resolver,
Packit 4b6dd7
		                    "accounts.google.com", ip_address);
Packit 4b6dd7
	}
Packit 4b6dd7
}
Packit 4b6dd7
Packit 4b6dd7
int
Packit 4b6dd7
main (int argc, char *argv[])
Packit 4b6dd7
{
Packit 4b6dd7
	GFile *trace_directory;
Packit 4b6dd7
	gchar *path = NULL;
Packit 4b6dd7
Packit 4b6dd7
	gdata_test_init (argc, argv);
Packit 4b6dd7
Packit 4b6dd7
	mock_server = gdata_test_get_mock_server ();
Packit 4b6dd7
	g_signal_connect (G_OBJECT (mock_server), "notify::resolver",
Packit 4b6dd7
	                  (GCallback) mock_server_notify_resolver_cb, NULL);
Packit 4b6dd7
	path = g_test_build_filename (G_TEST_DIST, "traces/oauth2-authorizer", NULL);
Packit 4b6dd7
	trace_directory = g_file_new_for_path (path);
Packit 4b6dd7
	g_free (path);
Packit 4b6dd7
	uhm_server_set_trace_directory (mock_server, trace_directory);
Packit 4b6dd7
	g_object_unref (trace_directory);
Packit 4b6dd7
Packit 4b6dd7
	main_thread = g_thread_self ();
Packit 4b6dd7
Packit 4b6dd7
	g_test_add_func ("/oauth2-authorizer/constructor", test_oauth2_authorizer_constructor);
Packit 4b6dd7
	g_test_add_func ("/oauth2-authorizer/constructor/for-domains", test_oauth2_authorizer_constructor_for_domains);
Packit 4b6dd7
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/properties/client-id", OAuth2AuthorizerData, NULL, set_up_oauth2_authorizer_data,
Packit 4b6dd7
	            test_oauth2_authorizer_properties_client_id, tear_down_oauth2_authorizer_data);
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/properties/client-secret", OAuth2AuthorizerData, NULL, set_up_oauth2_authorizer_data,
Packit 4b6dd7
	            test_oauth2_authorizer_properties_client_secret, tear_down_oauth2_authorizer_data);
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/properties/redirect-uri", OAuth2AuthorizerData, NULL, set_up_oauth2_authorizer_data,
Packit 4b6dd7
	            test_oauth2_authorizer_properties_redirect_uri, tear_down_oauth2_authorizer_data);
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/properties/locale", OAuth2AuthorizerData, NULL, set_up_oauth2_authorizer_data,
Packit 4b6dd7
	            test_oauth2_authorizer_properties_locale, tear_down_oauth2_authorizer_data);
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/properties/timeout", OAuth2AuthorizerData, NULL, set_up_oauth2_authorizer_data,
Packit 4b6dd7
	            test_oauth2_authorizer_properties_timeout, tear_down_oauth2_authorizer_data);
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/properties/proxy-resolver", OAuth2AuthorizerData, NULL, set_up_oauth2_authorizer_data,
Packit 4b6dd7
	            test_oauth2_authorizer_properties_proxy_resolver, tear_down_oauth2_authorizer_data);
Packit 4b6dd7
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/refresh-authorization/unauthenticated", OAuth2AuthorizerData, NULL,
Packit 4b6dd7
	            set_up_oauth2_authorizer_data, test_oauth2_authorizer_refresh_authorization_unauthenticated, tear_down_oauth2_authorizer_data);
Packit 4b6dd7
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/process-request/null", OAuth2AuthorizerData, NULL,
Packit 4b6dd7
	            set_up_oauth2_authorizer_data, test_oauth2_authorizer_process_request_null, tear_down_oauth2_authorizer_data);
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/process-request/unauthenticated", OAuth2AuthorizerData, NULL,
Packit 4b6dd7
	            set_up_oauth2_authorizer_data, test_oauth2_authorizer_process_request_unauthenticated, tear_down_oauth2_authorizer_data);
Packit 4b6dd7
Packit 4b6dd7
	/* build-authentication-uri tests */
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/build-authentication-uri", OAuth2AuthorizerData, NULL, set_up_oauth2_authorizer_data,
Packit 4b6dd7
	            test_oauth2_authorizer_build_authentication_uri_default, tear_down_oauth2_authorizer_data);
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/build-authentication-uri/multiple-domains", OAuth2AuthorizerData, NULL,
Packit 4b6dd7
	            set_up_oauth2_authorizer_data_multiple_domains, test_oauth2_authorizer_build_authentication_uri_default,
Packit 4b6dd7
	            tear_down_oauth2_authorizer_data);
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/build-authentication-uri/locale", OAuth2AuthorizerData, NULL,
Packit 4b6dd7
	            set_up_oauth2_authorizer_data_locale, test_oauth2_authorizer_build_authentication_uri_default,
Packit 4b6dd7
	            tear_down_oauth2_authorizer_data);
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/build-authentication-uri/hint", OAuth2AuthorizerData, NULL,
Packit 4b6dd7
	            set_up_oauth2_authorizer_data, test_oauth2_authorizer_build_authentication_uri_hint,
Packit 4b6dd7
	            tear_down_oauth2_authorizer_data);
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/build-authentication-uri/incremental", OAuth2AuthorizerData, NULL,
Packit 4b6dd7
	            set_up_oauth2_authorizer_data, test_oauth2_authorizer_build_authentication_uri_incremental,
Packit 4b6dd7
	            tear_down_oauth2_authorizer_data);
Packit 4b6dd7
Packit 4b6dd7
	/* Sync request-authorization tests */
Packit 4b6dd7
	if (gdata_test_interactive () == TRUE) {
Packit 4b6dd7
		g_test_add ("/oauth2-authorizer/request-authorization/sync", OAuth2AuthorizerInteractiveData,
Packit 4b6dd7
		            "4/P-pwMETnCh47w20wexdnflDFhXum.4qZ2A1pkUGsSoiIBeO6P2m8OUKkvkQI",
Packit 4b6dd7
		            set_up_oauth2_authorizer_interactive_data, test_oauth2_authorizer_request_authorization_sync,
Packit 4b6dd7
		            tear_down_oauth2_authorizer_interactive_data);
Packit 4b6dd7
		g_test_add ("/oauth2-authorizer/request-authorization/sync/cancellation", OAuth2AuthorizerInteractiveData,
Packit 4b6dd7
		            "4/P-pwMETnCh47w20wexdnflDFhXum.4qZ2A1pkUGsSoiIBeO6P2m8OUKkvkQI",
Packit 4b6dd7
		            set_up_oauth2_authorizer_interactive_data, test_oauth2_authorizer_request_authorization_sync_cancellation,
Packit 4b6dd7
		            tear_down_oauth2_authorizer_interactive_data);
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/request-authorization/sync/bad-credentials", OAuth2AuthorizerInteractiveData, "",
Packit 4b6dd7
	            set_up_oauth2_authorizer_interactive_data_bad_credentials,
Packit 4b6dd7
	            test_oauth2_authorizer_request_authorization_sync_bad_credentials, tear_down_oauth2_authorizer_interactive_data);
Packit 4b6dd7
Packit 4b6dd7
	/* Async request-authorization tests */
Packit 4b6dd7
	if (gdata_test_interactive () == TRUE) {
Packit 4b6dd7
		g_test_add ("/oauth2-authorizer/request-authorization/async", OAuth2AuthorizerInteractiveAsyncData,
Packit 4b6dd7
		            "4/Gfha9-4IeN09ibTR2Sa2MtQrG9qz.ks8v0zlKR9ceoiIBeO6P2m92f6kvkQI",
Packit 4b6dd7
		            set_up_oauth2_authorizer_interactive_async_data, test_oauth2_authorizer_request_authorization_async,
Packit 4b6dd7
		            tear_down_oauth2_authorizer_interactive_async_data);
Packit 4b6dd7
		g_test_add ("/oauth2-authorizer/request-authorization/async/cancellation", OAuth2AuthorizerInteractiveAsyncData,
Packit 4b6dd7
		            "4/Gfha9-4IeN09ibTR2Sa2MtQrG9qz.ks8v0zlKR9ceoiIBeO6P2m92f6kvkQI",
Packit 4b6dd7
		            set_up_oauth2_authorizer_interactive_async_data, test_oauth2_authorizer_request_authorization_async_cancellation,
Packit 4b6dd7
		            tear_down_oauth2_authorizer_interactive_async_data);
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	g_test_add ("/oauth2-authorizer/request-authorization/async/bad-credentials", OAuth2AuthorizerInteractiveAsyncData, "",
Packit 4b6dd7
	            set_up_oauth2_authorizer_interactive_async_data_bad_credentials,
Packit 4b6dd7
	            test_oauth2_authorizer_request_authorization_async_bad_credentials, tear_down_oauth2_authorizer_interactive_async_data);
Packit 4b6dd7
Packit 4b6dd7
	/* Miscellaneous tests */
Packit 4b6dd7
	if (gdata_test_interactive () == TRUE) {
Packit 4b6dd7
		gboolean skip_test = FALSE;
Packit 4b6dd7
Packit 4b6dd7
		g_test_add ("/oauth2-authorizer/refresh-authorization/authenticated", OAuth2AuthorizerData, &skip_test,
Packit 4b6dd7
		            set_up_oauth2_authorizer_data_authenticated, test_oauth2_authorizer_refresh_authorization_authenticated,
Packit 4b6dd7
		            tear_down_oauth2_authorizer_data);
Packit 4b6dd7
Packit 4b6dd7
		g_test_add ("/oauth2-authorizer/process-request/authenticated", OAuth2AuthorizerData, &skip_test,
Packit 4b6dd7
		            set_up_oauth2_authorizer_data_authenticated, test_oauth2_authorizer_process_request_authenticated,
Packit 4b6dd7
		            tear_down_oauth2_authorizer_data);
Packit 4b6dd7
	}
Packit 4b6dd7
Packit 4b6dd7
	return g_test_run ();
Packit 4b6dd7
}