1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
4
* Copyright (C) Philip Withnall 2011 <philip@tecnocode.co.uk>
6
* GData Client is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
11
* GData Client is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with GData Client. If not, see <http://www.gnu.org/licenses/>.
22
#include <gdata/gdata.h>
26
/* Used as common "testing domains" to simplify test code. */
27
static GDataAuthorizationDomain *test_domain1 = NULL;
28
static GDataAuthorizationDomain *test_domain2 = NULL;
31
test_authorization_domain_properties (void)
33
GDataAuthorizationDomain *domain;
34
gchar *service_name, *scope;
36
/* NOTE: It's not expected that client code will normally get hold of GDataAuthorizationDomain instances this way.
37
* This is just for testing purposes. */
38
domain = GDATA_AUTHORIZATION_DOMAIN (g_object_new (GDATA_TYPE_AUTHORIZATION_DOMAIN,
39
"service-name", "service-name",
43
g_assert_cmpstr (gdata_authorization_domain_get_service_name (domain), ==, "service-name");
44
g_assert_cmpstr (gdata_authorization_domain_get_scope (domain), ==, "scope");
47
"service-name", &service_name,
51
g_assert_cmpstr (service_name, ==, "service-name");
52
g_assert_cmpstr (scope, ==, "scope");
54
g_free (service_name);
58
/* Simple implementation of GDataAuthorizer for test purposes */
59
#define TYPE_SIMPLE_AUTHORIZER (simple_authorizer_get_type ())
60
#define SIMPLE_AUTHORIZER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), SIMPLE_TYPE_AUTHORIZER, SimpleAuthorizer))
61
#define SIMPLE_AUTHORIZER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), SIMPLE_TYPE_AUTHORIZER, SimpleAuthorizerClass))
62
#define IS_SIMPLE_AUTHORIZER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), SIMPLE_TYPE_AUTHORIZER))
63
#define IS_SIMPLE_AUTHORIZER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), SIMPLE_TYPE_AUTHORIZER))
64
#define SIMPLE_AUTHORIZER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), SIMPLE_TYPE_AUTHORIZER, SimpleAuthorizerClass))
72
} SimpleAuthorizerClass;
74
static GType simple_authorizer_get_type (void) G_GNUC_CONST;
75
static void simple_authorizer_authorizer_init (GDataAuthorizerInterface *iface);
77
G_DEFINE_TYPE_WITH_CODE (SimpleAuthorizer, simple_authorizer, G_TYPE_OBJECT,
78
G_IMPLEMENT_INTERFACE (GDATA_TYPE_AUTHORIZER, simple_authorizer_authorizer_init))
81
simple_authorizer_class_init (SimpleAuthorizerClass *klass)
83
/* Nothing to see here */
87
simple_authorizer_init (SimpleAuthorizer *self)
89
/* Nothing to see here */
93
simple_authorizer_process_request (GDataAuthorizer *self, GDataAuthorizationDomain *domain, SoupMessage *message)
97
/* Check that the domain and message are as expected */
98
g_assert (domain == NULL || GDATA_IS_AUTHORIZATION_DOMAIN (domain));
100
g_assert_cmpstr (gdata_authorization_domain_get_scope (domain), ==, "scope1");
103
g_assert (message != NULL);
104
g_assert (SOUP_IS_MESSAGE (message));
105
test_uri = soup_uri_new ("http://example.com/");
106
g_assert (soup_uri_equal (soup_message_get_uri (message), test_uri) == TRUE);
107
soup_uri_free (test_uri);
109
/* Check that this is the first time we've touched the message, and if so, flag the message as touched */
110
if (domain != NULL) {
111
g_assert (soup_message_headers_get_one (message->request_headers, "process_request") == NULL);
112
soup_message_headers_append (message->request_headers, "process_request", "1");
114
soup_message_headers_append (message->request_headers, "process_request_null", "1");
119
simple_authorizer_is_authorized_for_domain (GDataAuthorizer *self, GDataAuthorizationDomain *domain)
121
gboolean is_test_domain1, is_test_domain2;
123
/* Check that the domain is as expected */
124
g_assert (domain != NULL);
125
g_assert (GDATA_IS_AUTHORIZATION_DOMAIN (domain));
127
is_test_domain1 = (strcmp (gdata_authorization_domain_get_scope (domain), "scope1") == 0) ? TRUE : FALSE;
128
is_test_domain2 = (strcmp (gdata_authorization_domain_get_scope (domain), "scope2") == 0) ? TRUE : FALSE;
130
g_assert (is_test_domain1 == TRUE || is_test_domain2 == TRUE);
132
/* Increment the counter on the domain so we know if this function's been called more than once on each domain */
133
g_object_set_data (G_OBJECT (domain), "counter", GUINT_TO_POINTER (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (domain), "counter")) + 1));
135
/* Only authorise test_domain1 */
136
return is_test_domain1;
140
simple_authorizer_authorizer_init (GDataAuthorizerInterface *iface)
142
iface->process_request = simple_authorizer_process_request;
143
iface->is_authorized_for_domain = simple_authorizer_is_authorized_for_domain;
146
/* Normal implementation of GDataAuthorizer for test purposes */
147
#define TYPE_NORMAL_AUTHORIZER (normal_authorizer_get_type ())
148
#define NORMAL_AUTHORIZER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), NORMAL_TYPE_AUTHORIZER, NormalAuthorizer))
149
#define NORMAL_AUTHORIZER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), NORMAL_TYPE_AUTHORIZER, NormalAuthorizerClass))
150
#define IS_NORMAL_AUTHORIZER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), NORMAL_TYPE_AUTHORIZER))
151
#define IS_NORMAL_AUTHORIZER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), NORMAL_TYPE_AUTHORIZER))
152
#define NORMAL_AUTHORIZER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), NORMAL_TYPE_AUTHORIZER, NormalAuthorizerClass))
160
} NormalAuthorizerClass;
162
static GType normal_authorizer_get_type (void) G_GNUC_CONST;
163
static void normal_authorizer_authorizer_init (GDataAuthorizerInterface *iface);
165
G_DEFINE_TYPE_WITH_CODE (NormalAuthorizer, normal_authorizer, G_TYPE_OBJECT,
166
G_IMPLEMENT_INTERFACE (GDATA_TYPE_AUTHORIZER, normal_authorizer_authorizer_init))
169
normal_authorizer_class_init (NormalAuthorizerClass *klass)
171
/* Nothing to see here */
175
normal_authorizer_init (NormalAuthorizer *self)
177
/* Nothing to see here */
181
normal_authorizer_refresh_authorization (GDataAuthorizer *self, GCancellable *cancellable, GError **error)
183
/* Check the inputs */
184
g_assert (GDATA_IS_AUTHORIZER (self));
185
g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
186
g_assert (error == NULL || *error == NULL);
188
/* Increment the counter on the authorizer so we know if this function's been called more than once */
189
g_object_set_data (G_OBJECT (self), "counter", GUINT_TO_POINTER (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (self), "counter")) + 1));
191
/* If we're instructed to set an error, do so (with an arbitrary error code) */
192
if (g_object_get_data (G_OBJECT (self), "error") != NULL) {
193
g_set_error_literal (error, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_PROTOCOL_ERROR, "Error message");
201
normal_authorizer_authorizer_init (GDataAuthorizerInterface *iface)
203
/* Use the same implementation as SimpleAuthorizer for process_request() and is_authorized_for_domain(). */
204
iface->process_request = simple_authorizer_process_request;
205
iface->is_authorized_for_domain = simple_authorizer_is_authorized_for_domain;
207
/* Unlike SimpleAuthorizer, also implement refresh_authorization() (but not the async versions). */
208
iface->refresh_authorization = normal_authorizer_refresh_authorization;
211
/* Complex implementation of GDataAuthorizer for test purposes */
212
#define TYPE_COMPLEX_AUTHORIZER (complex_authorizer_get_type ())
213
#define COMPLEX_AUTHORIZER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), COMPLEX_TYPE_AUTHORIZER, ComplexAuthorizer))
214
#define COMPLEX_AUTHORIZER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), COMPLEX_TYPE_AUTHORIZER, ComplexAuthorizerClass))
215
#define IS_COMPLEX_AUTHORIZER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), COMPLEX_TYPE_AUTHORIZER))
216
#define IS_COMPLEX_AUTHORIZER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), COMPLEX_TYPE_AUTHORIZER))
217
#define COMPLEX_AUTHORIZER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), COMPLEX_TYPE_AUTHORIZER, ComplexAuthorizerClass))
225
} ComplexAuthorizerClass;
227
static GType complex_authorizer_get_type (void) G_GNUC_CONST;
228
static void complex_authorizer_authorizer_init (GDataAuthorizerInterface *iface);
230
G_DEFINE_TYPE_WITH_CODE (ComplexAuthorizer, complex_authorizer, G_TYPE_OBJECT,
231
G_IMPLEMENT_INTERFACE (GDATA_TYPE_AUTHORIZER, complex_authorizer_authorizer_init))
234
complex_authorizer_class_init (ComplexAuthorizerClass *klass)
236
/* Nothing to see here */
240
complex_authorizer_init (ComplexAuthorizer *self)
242
/* Nothing to see here */
246
complex_authorizer_refresh_authorization_async (GDataAuthorizer *self, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
248
GSimpleAsyncResult *result;
249
GError *error = NULL;
251
/* Check the inputs */
252
g_assert (GDATA_IS_AUTHORIZER (self));
253
g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
255
result = g_simple_async_result_new (G_OBJECT (self), callback, user_data, complex_authorizer_refresh_authorization_async);
257
/* Increment the async counter on the authorizer so we know if this function's been called more than once */
258
g_object_set_data (G_OBJECT (self), "async-counter",
259
GUINT_TO_POINTER (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (self), "async-counter")) + 1));
261
if (g_cancellable_set_error_if_cancelled (cancellable, &error) == TRUE) {
262
/* Handle cancellation */
263
g_simple_async_result_set_from_error (result, error);
264
} else if (g_object_get_data (G_OBJECT (self), "error") != NULL) {
265
/* If we're instructed to set an error, do so (with an arbitrary error code) */
266
g_simple_async_result_set_error (result, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_NETWORK_ERROR, "%s", "Error message");
269
g_simple_async_result_complete_in_idle (result);
271
g_object_unref (result);
272
g_clear_error (&error);
276
complex_authorizer_refresh_authorization_finish (GDataAuthorizer *self, GAsyncResult *async_result, GError **error)
278
/* Check the inputs */
279
g_assert (GDATA_IS_AUTHORIZER (self));
280
g_assert (G_IS_ASYNC_RESULT (async_result));
281
g_assert (error == NULL || *error == NULL);
283
g_assert (g_simple_async_result_is_valid (async_result, G_OBJECT (self), complex_authorizer_refresh_authorization_async) == TRUE);
285
/* Assert that the async function's already been called (once) */
286
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (self), "async-counter")), ==, 1);
288
/* Increment the finish counter on the authorizer so we know if this function's been called more than once */
289
g_object_set_data (G_OBJECT (self), "finish-counter",
290
GUINT_TO_POINTER (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (self), "finish-counter")) + 1));
292
return (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (async_result), error) == FALSE) ? TRUE : FALSE;
296
complex_authorizer_authorizer_init (GDataAuthorizerInterface *iface)
298
/* Use the same implementation as SimpleAuthorizer/NormalAuthorizer for process_request(), is_authorized_for_domain() and
299
* refresh_authorization(). */
300
iface->process_request = simple_authorizer_process_request;
301
iface->is_authorized_for_domain = simple_authorizer_is_authorized_for_domain;
302
iface->refresh_authorization = normal_authorizer_refresh_authorization;
304
/* Unlike NormalAuthorizer, also implement the async versions of refresh_authorization(). */
305
iface->refresh_authorization_async = complex_authorizer_refresh_authorization_async;
306
iface->refresh_authorization_finish = complex_authorizer_refresh_authorization_finish;
309
/* Testing data for generic GDataAuthorizer interface tests */
311
GDataAuthorizer *authorizer;
315
set_up_simple_authorizer_data (AuthorizerData *data, gconstpointer user_data)
317
data->authorizer = GDATA_AUTHORIZER (g_object_new (TYPE_SIMPLE_AUTHORIZER, NULL));
321
set_up_normal_authorizer_data (AuthorizerData *data, gconstpointer user_data)
323
data->authorizer = GDATA_AUTHORIZER (g_object_new (TYPE_NORMAL_AUTHORIZER, NULL));
327
set_up_complex_authorizer_data (AuthorizerData *data, gconstpointer user_data)
329
data->authorizer = GDATA_AUTHORIZER (g_object_new (TYPE_COMPLEX_AUTHORIZER, NULL));
333
tear_down_authorizer_data (AuthorizerData *data, gconstpointer user_data)
335
g_object_unref (data->authorizer);
338
/* Test that calling gdata_authorizer_process_request() happens correctly */
340
test_authorizer_process_request (AuthorizerData *data, gconstpointer user_data)
342
SoupMessage *message;
344
message = soup_message_new (SOUP_METHOD_GET, "http://example.com/");
346
gdata_authorizer_process_request (data->authorizer, test_domain1, message);
347
g_assert_cmpstr (soup_message_headers_get_one (message->request_headers, "process_request"), ==, "1");
348
g_assert (soup_message_headers_get_one (message->request_headers, "process_request_null") == NULL);
350
g_object_unref (message);
353
/* Test that calling gdata_authorizer_process_request() happens correctly for a NULL domain */
355
test_authorizer_process_request_null (AuthorizerData *data, gconstpointer user_data)
357
SoupMessage *message;
359
message = soup_message_new (SOUP_METHOD_GET, "http://example.com/");
361
gdata_authorizer_process_request (data->authorizer, NULL, message);
362
g_assert (soup_message_headers_get_one (message->request_headers, "process_request") == NULL);
363
g_assert_cmpstr (soup_message_headers_get_one (message->request_headers, "process_request_null"), ==, "1");
365
g_object_unref (message);
368
/* Test that calling gdata_authorizer_is_authorized_for_domain() happens correctly */
370
test_authorizer_is_authorized_for_domain (AuthorizerData *data, gconstpointer user_data)
372
/* Set some counters on the test domains to check that the interface implementation is only called once per domain */
373
g_object_set_data (G_OBJECT (test_domain1), "counter", GUINT_TO_POINTER (0));
374
g_object_set_data (G_OBJECT (test_domain2), "counter", GUINT_TO_POINTER (0));
376
g_assert (gdata_authorizer_is_authorized_for_domain (data->authorizer, test_domain1) == TRUE);
377
g_assert (gdata_authorizer_is_authorized_for_domain (data->authorizer, test_domain2) == FALSE);
379
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (test_domain1), "counter")), ==, 1);
380
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (test_domain2), "counter")), ==, 1);
383
/* Test that calling gdata_authorizer_is_authorized_for_domain() with a NULL authorizer always returns FALSE */
385
test_authorizer_is_authorized_for_domain_null (AuthorizerData *data, gconstpointer user_data)
387
g_assert (gdata_authorizer_is_authorized_for_domain (NULL, test_domain1) == FALSE);
388
g_assert (gdata_authorizer_is_authorized_for_domain (NULL, test_domain2) == FALSE);
391
/* Test that calling refresh_authorization() on an authorizer which implements it returns TRUE without error, and only calls the implementation
394
test_authorizer_refresh_authorization (AuthorizerData *data, gconstpointer user_data)
397
GError *error = NULL;
399
/* Set a counter on the authoriser to check that the interface implementation is only called once */
400
g_object_set_data (G_OBJECT (data->authorizer), "counter", GUINT_TO_POINTER (0));
402
success = gdata_authorizer_refresh_authorization (data->authorizer, NULL, &error);
403
g_assert_no_error (error);
404
g_assert (success == TRUE);
405
g_clear_error (&error);
407
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "counter")), ==, 1);
410
/* Test that calling refresh_authorization() on an authorizer which implements it with errors returns FALSE with an error, and only calls the
411
* implementation once */
413
test_authorizer_refresh_authorization_error (AuthorizerData *data, gconstpointer user_data)
416
GError *error = NULL;
418
/* Set a counter on the authoriser to check that the interface implementation is only called once */
419
g_object_set_data (G_OBJECT (data->authorizer), "counter", GUINT_TO_POINTER (0));
421
/* Set a flag on the authoriser to make the NormalAuthorizer implementation return an error for refresh_authorization() */
422
g_object_set_data (G_OBJECT (data->authorizer), "error", GUINT_TO_POINTER (TRUE));
424
success = gdata_authorizer_refresh_authorization (data->authorizer, NULL, &error);
425
g_assert_error (error, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_PROTOCOL_ERROR);
426
g_assert (success == FALSE);
427
g_clear_error (&error);
429
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "counter")), ==, 1);
432
/* Test that calling refresh_authorization() on an authorizer which doesn't implement it returns FALSE without an error */
434
test_authorizer_refresh_authorization_unimplemented (AuthorizerData *data, gconstpointer user_data)
437
GError *error = NULL;
439
success = gdata_authorizer_refresh_authorization (data->authorizer, NULL, &error);
440
g_assert_no_error (error);
441
g_assert (success == FALSE);
442
g_clear_error (&error);
445
/* Test that calling refresh_authorization() on an authorizer which doesn't implement it, then cancelling the call returns FALSE without an error
446
* (not even a cancellation error) */
448
test_authorizer_refresh_authorization_cancellation_unimplemented (AuthorizerData *data, gconstpointer user_data)
450
GCancellable *cancellable;
452
GError *error = NULL;
454
cancellable = g_cancellable_new ();
455
g_cancellable_cancel (cancellable);
457
success = gdata_authorizer_refresh_authorization (data->authorizer, cancellable, &error);
458
g_assert_no_error (error);
459
g_assert (success == FALSE);
460
g_clear_error (&error);
462
g_object_unref (cancellable);
465
/* Set of standard async callback functions for refresh_authorization_async() which check various combinations of success and error value */
467
test_authorizer_refresh_authorization_async_success_no_error_cb (GDataAuthorizer *authorizer, GAsyncResult *async_result, GMainLoop *main_loop)
470
GError *error = NULL;
472
success = gdata_authorizer_refresh_authorization_finish (authorizer, async_result, &error);
473
g_assert_no_error (error);
474
g_assert (success == TRUE);
475
g_clear_error (&error);
477
g_main_loop_quit (main_loop);
481
test_authorizer_refresh_authorization_async_failure_no_error_cb (GDataAuthorizer *authorizer, GAsyncResult *async_result, GMainLoop *main_loop)
484
GError *error = NULL;
486
success = gdata_authorizer_refresh_authorization_finish (authorizer, async_result, &error);
487
g_assert_no_error (error);
488
g_assert (success == FALSE);
489
g_clear_error (&error);
491
g_main_loop_quit (main_loop);
495
test_authorizer_refresh_authorization_async_network_error_cb (GDataAuthorizer *authorizer, GAsyncResult *async_result, GMainLoop *main_loop)
498
GError *error = NULL;
500
success = gdata_authorizer_refresh_authorization_finish (authorizer, async_result, &error);
501
g_assert_error (error, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_NETWORK_ERROR);
502
g_assert (success == FALSE);
503
g_clear_error (&error);
505
g_main_loop_quit (main_loop);
509
test_authorizer_refresh_authorization_async_protocol_error_cb (GDataAuthorizer *authorizer, GAsyncResult *async_result, GMainLoop *main_loop)
512
GError *error = NULL;
514
success = gdata_authorizer_refresh_authorization_finish (authorizer, async_result, &error);
515
g_assert_error (error, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_PROTOCOL_ERROR);
516
g_assert (success == FALSE);
517
g_clear_error (&error);
519
g_main_loop_quit (main_loop);
523
test_authorizer_refresh_authorization_async_cancelled_error_cb (GDataAuthorizer *authorizer, GAsyncResult *async_result, GMainLoop *main_loop)
526
GError *error = NULL;
528
success = gdata_authorizer_refresh_authorization_finish (authorizer, async_result, &error);
529
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
530
g_assert (success == FALSE);
531
g_clear_error (&error);
533
g_main_loop_quit (main_loop);
536
/* Test that calling refresh_authorization_async() on an authorizer which implements it returns TRUE without an error */
538
test_authorizer_refresh_authorization_async (AuthorizerData *data, gconstpointer user_data)
540
GMainLoop *main_loop;
542
/* Set counters on the authoriser to check that the interface implementations are only called once */
543
g_object_set_data (G_OBJECT (data->authorizer), "counter", GUINT_TO_POINTER (0));
544
g_object_set_data (G_OBJECT (data->authorizer), "async-counter", GUINT_TO_POINTER (0));
545
g_object_set_data (G_OBJECT (data->authorizer), "finish-counter", GUINT_TO_POINTER (0));
547
main_loop = g_main_loop_new (NULL, FALSE);
549
gdata_authorizer_refresh_authorization_async (data->authorizer, NULL,
550
(GAsyncReadyCallback) test_authorizer_refresh_authorization_async_success_no_error_cb,
553
g_main_loop_run (main_loop);
555
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "counter")), ==, 0);
556
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "async-counter")), ==, 1);
557
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "finish-counter")), ==, 1);
559
g_main_loop_unref (main_loop);
562
/* Test that calling refresh_authorization_async() on an authorizer which implements it with an error returns FALSE with the appropriate error */
564
test_authorizer_refresh_authorization_async_error (AuthorizerData *data, gconstpointer user_data)
566
GMainLoop *main_loop;
568
/* Set counters on the authoriser to check that the interface implementations are only called once */
569
g_object_set_data (G_OBJECT (data->authorizer), "counter", GUINT_TO_POINTER (0));
570
g_object_set_data (G_OBJECT (data->authorizer), "async-counter", GUINT_TO_POINTER (0));
571
g_object_set_data (G_OBJECT (data->authorizer), "finish-counter", GUINT_TO_POINTER (0));
573
/* Set a flag on the authoriser to make the ComplexAuthorizer implementation return an error for refresh_authorization_async() */
574
g_object_set_data (G_OBJECT (data->authorizer), "error", GUINT_TO_POINTER (TRUE));
576
main_loop = g_main_loop_new (NULL, FALSE);
578
gdata_authorizer_refresh_authorization_async (data->authorizer, NULL,
579
(GAsyncReadyCallback) test_authorizer_refresh_authorization_async_network_error_cb, main_loop);
581
g_main_loop_run (main_loop);
583
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "counter")), ==, 0);
584
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "async-counter")), ==, 1);
585
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "finish-counter")), ==, 1);
587
g_main_loop_unref (main_loop);
590
/* Test that calling refresh_authorization_async() on an authorizer which implements it, then cancelling the call returns FALSE with a cancellation
593
test_authorizer_refresh_authorization_async_cancellation (AuthorizerData *data, gconstpointer user_data)
595
GCancellable *cancellable;
596
GMainLoop *main_loop;
598
/* Set counters on the authoriser to check that the interface implementations are only called once */
599
g_object_set_data (G_OBJECT (data->authorizer), "counter", GUINT_TO_POINTER (0));
600
g_object_set_data (G_OBJECT (data->authorizer), "async-counter", GUINT_TO_POINTER (0));
601
g_object_set_data (G_OBJECT (data->authorizer), "finish-counter", GUINT_TO_POINTER (0));
603
main_loop = g_main_loop_new (NULL, FALSE);
605
cancellable = g_cancellable_new ();
606
g_cancellable_cancel (cancellable);
608
gdata_authorizer_refresh_authorization_async (data->authorizer, cancellable,
609
(GAsyncReadyCallback) test_authorizer_refresh_authorization_async_cancelled_error_cb,
612
g_main_loop_run (main_loop);
614
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "counter")), ==, 0);
615
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "async-counter")), ==, 1);
616
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "finish-counter")), ==, 1);
618
g_object_unref (cancellable);
619
g_main_loop_unref (main_loop);
622
/* Test that calling refresh_authorization_async() on an authorizer which doesn't implement it, but does implement refresh_authorization(), returns
623
* TRUE without an error */
625
test_authorizer_refresh_authorization_async_simulated (AuthorizerData *data, gconstpointer user_data)
627
GMainLoop *main_loop;
629
/* Set a counter on the authoriser to check that the interface implementation is only called once */
630
g_object_set_data (G_OBJECT (data->authorizer), "counter", GUINT_TO_POINTER (0));
632
main_loop = g_main_loop_new (NULL, FALSE);
634
gdata_authorizer_refresh_authorization_async (data->authorizer, NULL,
635
(GAsyncReadyCallback) test_authorizer_refresh_authorization_async_success_no_error_cb,
638
g_main_loop_run (main_loop);
640
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "counter")), ==, 1);
642
g_main_loop_unref (main_loop);
645
/* Test that calling refresh_authorization_async() on an authorizer which doesn't implement it, but does implement refresh_authorization() with an
646
* error, returns FALSE with the appropriate error */
648
test_authorizer_refresh_authorization_async_error_simulated (AuthorizerData *data, gconstpointer user_data)
650
GMainLoop *main_loop;
652
/* Set a counter on the authoriser to check that the interface implementation is only called once */
653
g_object_set_data (G_OBJECT (data->authorizer), "counter", GUINT_TO_POINTER (0));
655
/* Set a flag on the authoriser to make the NormalAuthorizer implementation return an error for refresh_authorization() */
656
g_object_set_data (G_OBJECT (data->authorizer), "error", GUINT_TO_POINTER (TRUE));
658
main_loop = g_main_loop_new (NULL, FALSE);
660
gdata_authorizer_refresh_authorization_async (data->authorizer, NULL,
661
(GAsyncReadyCallback) test_authorizer_refresh_authorization_async_protocol_error_cb, main_loop);
663
g_main_loop_run (main_loop);
665
g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "counter")), ==, 1);
667
g_main_loop_unref (main_loop);
670
/* Test that calling refresh_authorization_async() on an authorizer which doesn't implement it, but does implement refresh_authorization(), then
671
* cancelling the call returns FALSE with a cancellation error */
673
test_authorizer_refresh_authorization_async_cancellation_simulated (AuthorizerData *data, gconstpointer user_data)
675
GCancellable *cancellable;
676
GMainLoop *main_loop;
678
main_loop = g_main_loop_new (NULL, FALSE);
680
cancellable = g_cancellable_new ();
681
g_cancellable_cancel (cancellable);
683
/* Note we don't count how many times the implementation of refresh_authorization() is called, since cancellation can legitimately be
684
* handled by the gdata_authorizer_refresh_authorization_async() code before refresh_authorization() is ever called. */
685
gdata_authorizer_refresh_authorization_async (data->authorizer, cancellable,
686
(GAsyncReadyCallback) test_authorizer_refresh_authorization_async_cancelled_error_cb,
689
g_main_loop_run (main_loop);
691
g_object_unref (cancellable);
692
g_main_loop_unref (main_loop);
695
/* Test that calling refresh_authorization_async() on an authorizer which doesn't implement it returns FALSE without an error */
697
test_authorizer_refresh_authorization_async_unimplemented (AuthorizerData *data, gconstpointer user_data)
699
GMainLoop *main_loop;
701
main_loop = g_main_loop_new (NULL, FALSE);
703
gdata_authorizer_refresh_authorization_async (data->authorizer, NULL,
704
(GAsyncReadyCallback) test_authorizer_refresh_authorization_async_failure_no_error_cb,
707
g_main_loop_run (main_loop);
709
g_main_loop_unref (main_loop);
712
/* Test that calling refresh_authorization_async() on an authorizer which doesn't implement it, then cancelling the call returns FALSE without an
713
* error (not even a cancellation error) */
715
test_authorizer_refresh_authorization_async_cancellation_unimplemented (AuthorizerData *data, gconstpointer user_data)
717
GCancellable *cancellable;
718
GMainLoop *main_loop;
720
main_loop = g_main_loop_new (NULL, FALSE);
722
cancellable = g_cancellable_new ();
723
g_cancellable_cancel (cancellable);
725
gdata_authorizer_refresh_authorization_async (data->authorizer, cancellable,
726
(GAsyncReadyCallback) test_authorizer_refresh_authorization_async_failure_no_error_cb,
729
g_main_loop_run (main_loop);
731
g_object_unref (cancellable);
732
g_main_loop_unref (main_loop);
736
main (int argc, char *argv[])
740
gdata_test_init (argc, argv);
742
/* Note: This is not how GDataAuthorizationDomains are meant to be constructed. Client code is not expected to do this. */
743
test_domain1 = g_object_new (GDATA_TYPE_AUTHORIZATION_DOMAIN,
744
"service-name", "service-name1",
747
test_domain2 = g_object_new (GDATA_TYPE_AUTHORIZATION_DOMAIN,
748
"service-name", "service-name2",
752
/* GDataAuthorizationDomain tests */
753
g_test_add_func ("/authorization-domain/properties", test_authorization_domain_properties);
755
/* GDataAuthorizer interface tests */
756
g_test_add ("/authorizer/process-request", AuthorizerData, NULL, set_up_simple_authorizer_data, test_authorizer_process_request,
757
tear_down_authorizer_data);
758
g_test_add ("/authorizer/process-request/null", AuthorizerData, NULL, set_up_simple_authorizer_data, test_authorizer_process_request_null,
759
tear_down_authorizer_data);
760
g_test_add ("/authorizer/is-authorized-for-domain", AuthorizerData, NULL, set_up_simple_authorizer_data,
761
test_authorizer_is_authorized_for_domain, tear_down_authorizer_data);
762
g_test_add ("/authorizer/is-authorized-for-domain/null", AuthorizerData, NULL, set_up_simple_authorizer_data,
763
test_authorizer_is_authorized_for_domain_null, tear_down_authorizer_data);
764
g_test_add ("/authorizer/refresh-authorization", AuthorizerData, NULL, set_up_normal_authorizer_data,
765
test_authorizer_refresh_authorization, tear_down_authorizer_data);
766
g_test_add ("/authorizer/refresh-authorization/error", AuthorizerData, NULL, set_up_normal_authorizer_data,
767
test_authorizer_refresh_authorization_error, tear_down_authorizer_data);
768
g_test_add ("/authorizer/refresh-authorization/unimplemented", AuthorizerData, NULL, set_up_simple_authorizer_data,
769
test_authorizer_refresh_authorization_unimplemented, tear_down_authorizer_data);
770
g_test_add ("/authorizer/refresh-authorization/cancellation/unimplemented", AuthorizerData, NULL, set_up_simple_authorizer_data,
771
test_authorizer_refresh_authorization_cancellation_unimplemented, tear_down_authorizer_data);
772
g_test_add ("/authorizer/refresh-authorization/async", AuthorizerData, NULL, set_up_complex_authorizer_data,
773
test_authorizer_refresh_authorization_async, tear_down_authorizer_data);
774
g_test_add ("/authorizer/refresh-authorization/async/error", AuthorizerData, NULL, set_up_complex_authorizer_data,
775
test_authorizer_refresh_authorization_async_error, tear_down_authorizer_data);
776
g_test_add ("/authorizer/refresh-authorization/async/cancellation", AuthorizerData, NULL, set_up_complex_authorizer_data,
777
test_authorizer_refresh_authorization_async_cancellation, tear_down_authorizer_data);
778
g_test_add ("/authorizer/refresh-authorization/async/simulated", AuthorizerData, NULL, set_up_normal_authorizer_data,
779
test_authorizer_refresh_authorization_async_simulated, tear_down_authorizer_data);
780
g_test_add ("/authorizer/refresh-authorization/async/error/simulated", AuthorizerData, NULL, set_up_normal_authorizer_data,
781
test_authorizer_refresh_authorization_async_error_simulated, tear_down_authorizer_data);
782
g_test_add ("/authorizer/refresh-authorization/async/cancellation/simulated", AuthorizerData, NULL, set_up_normal_authorizer_data,
783
test_authorizer_refresh_authorization_async_cancellation_simulated, tear_down_authorizer_data);
784
g_test_add ("/authorizer/refresh-authorization/async/unimplemented", AuthorizerData, NULL, set_up_simple_authorizer_data,
785
test_authorizer_refresh_authorization_async_unimplemented, tear_down_authorizer_data);
786
g_test_add ("/authorizer/refresh-authorization/async/cancellation/unimplemented", AuthorizerData, NULL, set_up_simple_authorizer_data,
787
test_authorizer_refresh_authorization_async_cancellation_unimplemented, tear_down_authorizer_data);
789
retval = g_test_run ();
791
g_object_unref (test_domain2);
792
g_object_unref (test_domain1);