~ubuntu-branches/ubuntu/oneiric/libgdata/oneiric-backports

« back to all changes in this revision

Viewing changes to gdata/tests/authorization.c

  • Committer: Package Import Robot
  • Author(s): Evan Broder
  • Date: 2011-11-15 21:59:48 UTC
  • mfrom: (4.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20111115215948-e17s889ocgu5fv4f
Tags: 0.10.1-1~oneiric1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/*
 
3
 * GData Client
 
4
 * Copyright (C) Philip Withnall 2011 <philip@tecnocode.co.uk>
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
#include <string.h>
 
21
#include <glib.h>
 
22
#include <gdata/gdata.h>
 
23
 
 
24
#include "common.h"
 
25
 
 
26
/* Used as common "testing domains" to simplify test code. */
 
27
static GDataAuthorizationDomain *test_domain1 = NULL;
 
28
static GDataAuthorizationDomain *test_domain2 = NULL;
 
29
 
 
30
static void
 
31
test_authorization_domain_properties (void)
 
32
{
 
33
        GDataAuthorizationDomain *domain;
 
34
        gchar *service_name, *scope;
 
35
 
 
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",
 
40
                                                           "scope", "scope",
 
41
                                                           NULL));
 
42
 
 
43
        g_assert_cmpstr (gdata_authorization_domain_get_service_name (domain), ==, "service-name");
 
44
        g_assert_cmpstr (gdata_authorization_domain_get_scope (domain), ==, "scope");
 
45
 
 
46
        g_object_get (domain,
 
47
                      "service-name", &service_name,
 
48
                      "scope", &scope,
 
49
                      NULL);
 
50
 
 
51
        g_assert_cmpstr (service_name, ==, "service-name");
 
52
        g_assert_cmpstr (scope, ==, "scope");
 
53
 
 
54
        g_free (service_name);
 
55
        g_free (scope);
 
56
}
 
57
 
 
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))
 
65
 
 
66
typedef struct {
 
67
        GObject parent;
 
68
} SimpleAuthorizer;
 
69
 
 
70
typedef struct {
 
71
        GObjectClass parent;
 
72
} SimpleAuthorizerClass;
 
73
 
 
74
static GType simple_authorizer_get_type (void) G_GNUC_CONST;
 
75
static void simple_authorizer_authorizer_init (GDataAuthorizerInterface *iface);
 
76
 
 
77
G_DEFINE_TYPE_WITH_CODE (SimpleAuthorizer, simple_authorizer, G_TYPE_OBJECT,
 
78
                         G_IMPLEMENT_INTERFACE (GDATA_TYPE_AUTHORIZER, simple_authorizer_authorizer_init))
 
79
 
 
80
static void
 
81
simple_authorizer_class_init (SimpleAuthorizerClass *klass)
 
82
{
 
83
        /* Nothing to see here */
 
84
}
 
85
 
 
86
static void
 
87
simple_authorizer_init (SimpleAuthorizer *self)
 
88
{
 
89
        /* Nothing to see here */
 
90
}
 
91
 
 
92
static void
 
93
simple_authorizer_process_request (GDataAuthorizer *self, GDataAuthorizationDomain *domain, SoupMessage *message)
 
94
{
 
95
        SoupURI *test_uri;
 
96
 
 
97
        /* Check that the domain and message are as expected */
 
98
        g_assert (domain == NULL || GDATA_IS_AUTHORIZATION_DOMAIN (domain));
 
99
        if (domain != NULL) {
 
100
                g_assert_cmpstr (gdata_authorization_domain_get_scope (domain), ==, "scope1");
 
101
        }
 
102
 
 
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);
 
108
 
 
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");
 
113
        } else {
 
114
                soup_message_headers_append (message->request_headers, "process_request_null", "1");
 
115
        }
 
116
}
 
117
 
 
118
static gboolean
 
119
simple_authorizer_is_authorized_for_domain (GDataAuthorizer *self, GDataAuthorizationDomain *domain)
 
120
{
 
121
        gboolean is_test_domain1, is_test_domain2;
 
122
 
 
123
        /* Check that the domain is as expected */
 
124
        g_assert (domain != NULL);
 
125
        g_assert (GDATA_IS_AUTHORIZATION_DOMAIN (domain));
 
126
 
 
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;
 
129
 
 
130
        g_assert (is_test_domain1 == TRUE || is_test_domain2 == TRUE);
 
131
 
 
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));
 
134
 
 
135
        /* Only authorise test_domain1 */
 
136
        return is_test_domain1;
 
137
}
 
138
 
 
139
static void
 
140
simple_authorizer_authorizer_init (GDataAuthorizerInterface *iface)
 
141
{
 
142
        iface->process_request = simple_authorizer_process_request;
 
143
        iface->is_authorized_for_domain = simple_authorizer_is_authorized_for_domain;
 
144
}
 
145
 
 
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))
 
153
 
 
154
typedef struct {
 
155
        GObject parent;
 
156
} NormalAuthorizer;
 
157
 
 
158
typedef struct {
 
159
        GObjectClass parent;
 
160
} NormalAuthorizerClass;
 
161
 
 
162
static GType normal_authorizer_get_type (void) G_GNUC_CONST;
 
163
static void normal_authorizer_authorizer_init (GDataAuthorizerInterface *iface);
 
164
 
 
165
G_DEFINE_TYPE_WITH_CODE (NormalAuthorizer, normal_authorizer, G_TYPE_OBJECT,
 
166
                         G_IMPLEMENT_INTERFACE (GDATA_TYPE_AUTHORIZER, normal_authorizer_authorizer_init))
 
167
 
 
168
static void
 
169
normal_authorizer_class_init (NormalAuthorizerClass *klass)
 
170
{
 
171
        /* Nothing to see here */
 
172
}
 
173
 
 
174
static void
 
175
normal_authorizer_init (NormalAuthorizer *self)
 
176
{
 
177
        /* Nothing to see here */
 
178
}
 
179
 
 
180
static gboolean
 
181
normal_authorizer_refresh_authorization (GDataAuthorizer *self, GCancellable *cancellable, GError **error)
 
182
{
 
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);
 
187
 
 
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));
 
190
 
 
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");
 
194
                return FALSE;
 
195
        }
 
196
 
 
197
        return TRUE;
 
198
}
 
199
 
 
200
static void
 
201
normal_authorizer_authorizer_init (GDataAuthorizerInterface *iface)
 
202
{
 
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;
 
206
 
 
207
        /* Unlike SimpleAuthorizer, also implement refresh_authorization() (but not the async versions). */
 
208
        iface->refresh_authorization = normal_authorizer_refresh_authorization;
 
209
}
 
210
 
 
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))
 
218
 
 
219
typedef struct {
 
220
        GObject parent;
 
221
} ComplexAuthorizer;
 
222
 
 
223
typedef struct {
 
224
        GObjectClass parent;
 
225
} ComplexAuthorizerClass;
 
226
 
 
227
static GType complex_authorizer_get_type (void) G_GNUC_CONST;
 
228
static void complex_authorizer_authorizer_init (GDataAuthorizerInterface *iface);
 
229
 
 
230
G_DEFINE_TYPE_WITH_CODE (ComplexAuthorizer, complex_authorizer, G_TYPE_OBJECT,
 
231
                         G_IMPLEMENT_INTERFACE (GDATA_TYPE_AUTHORIZER, complex_authorizer_authorizer_init))
 
232
 
 
233
static void
 
234
complex_authorizer_class_init (ComplexAuthorizerClass *klass)
 
235
{
 
236
        /* Nothing to see here */
 
237
}
 
238
 
 
239
static void
 
240
complex_authorizer_init (ComplexAuthorizer *self)
 
241
{
 
242
        /* Nothing to see here */
 
243
}
 
244
 
 
245
static void
 
246
complex_authorizer_refresh_authorization_async (GDataAuthorizer *self, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
 
247
{
 
248
        GSimpleAsyncResult *result;
 
249
        GError *error = NULL;
 
250
 
 
251
        /* Check the inputs */
 
252
        g_assert (GDATA_IS_AUTHORIZER (self));
 
253
        g_assert (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
 
254
 
 
255
        result = g_simple_async_result_new (G_OBJECT (self), callback, user_data, complex_authorizer_refresh_authorization_async);
 
256
 
 
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));
 
260
 
 
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");
 
267
        }
 
268
 
 
269
        g_simple_async_result_complete_in_idle (result);
 
270
 
 
271
        g_object_unref (result);
 
272
        g_clear_error (&error);
 
273
}
 
274
 
 
275
static gboolean
 
276
complex_authorizer_refresh_authorization_finish (GDataAuthorizer *self, GAsyncResult *async_result, GError **error)
 
277
{
 
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);
 
282
 
 
283
        g_assert (g_simple_async_result_is_valid (async_result, G_OBJECT (self), complex_authorizer_refresh_authorization_async) == TRUE);
 
284
 
 
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);
 
287
 
 
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));
 
291
 
 
292
        return (g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (async_result), error) == FALSE) ? TRUE : FALSE;
 
293
}
 
294
 
 
295
static void
 
296
complex_authorizer_authorizer_init (GDataAuthorizerInterface *iface)
 
297
{
 
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;
 
303
 
 
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;
 
307
}
 
308
 
 
309
/* Testing data for generic GDataAuthorizer interface tests */
 
310
typedef struct {
 
311
        GDataAuthorizer *authorizer;
 
312
} AuthorizerData;
 
313
 
 
314
static void
 
315
set_up_simple_authorizer_data (AuthorizerData *data, gconstpointer user_data)
 
316
{
 
317
        data->authorizer = GDATA_AUTHORIZER (g_object_new (TYPE_SIMPLE_AUTHORIZER, NULL));
 
318
}
 
319
 
 
320
static void
 
321
set_up_normal_authorizer_data (AuthorizerData *data, gconstpointer user_data)
 
322
{
 
323
        data->authorizer = GDATA_AUTHORIZER (g_object_new (TYPE_NORMAL_AUTHORIZER, NULL));
 
324
}
 
325
 
 
326
static void
 
327
set_up_complex_authorizer_data (AuthorizerData *data, gconstpointer user_data)
 
328
{
 
329
        data->authorizer = GDATA_AUTHORIZER (g_object_new (TYPE_COMPLEX_AUTHORIZER, NULL));
 
330
}
 
331
 
 
332
static void
 
333
tear_down_authorizer_data (AuthorizerData *data, gconstpointer user_data)
 
334
{
 
335
        g_object_unref (data->authorizer);
 
336
}
 
337
 
 
338
/* Test that calling gdata_authorizer_process_request() happens correctly */
 
339
static void
 
340
test_authorizer_process_request (AuthorizerData *data, gconstpointer user_data)
 
341
{
 
342
        SoupMessage *message;
 
343
 
 
344
        message = soup_message_new (SOUP_METHOD_GET, "http://example.com/");
 
345
 
 
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);
 
349
 
 
350
        g_object_unref (message);
 
351
}
 
352
 
 
353
/* Test that calling gdata_authorizer_process_request() happens correctly for a NULL domain */
 
354
static void
 
355
test_authorizer_process_request_null (AuthorizerData *data, gconstpointer user_data)
 
356
{
 
357
        SoupMessage *message;
 
358
 
 
359
        message = soup_message_new (SOUP_METHOD_GET, "http://example.com/");
 
360
 
 
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");
 
364
 
 
365
        g_object_unref (message);
 
366
}
 
367
 
 
368
/* Test that calling gdata_authorizer_is_authorized_for_domain() happens correctly */
 
369
static void
 
370
test_authorizer_is_authorized_for_domain (AuthorizerData *data, gconstpointer user_data)
 
371
{
 
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));
 
375
 
 
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);
 
378
 
 
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);
 
381
}
 
382
 
 
383
/* Test that calling gdata_authorizer_is_authorized_for_domain() with a NULL authorizer always returns FALSE */
 
384
static void
 
385
test_authorizer_is_authorized_for_domain_null (AuthorizerData *data, gconstpointer user_data)
 
386
{
 
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);
 
389
}
 
390
 
 
391
/* Test that calling refresh_authorization() on an authorizer which implements it returns TRUE without error, and only calls the implementation
 
392
 * once */
 
393
static void
 
394
test_authorizer_refresh_authorization (AuthorizerData *data, gconstpointer user_data)
 
395
{
 
396
        gboolean success;
 
397
        GError *error = NULL;
 
398
 
 
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));
 
401
 
 
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);
 
406
 
 
407
        g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "counter")), ==, 1);
 
408
}
 
409
 
 
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 */
 
412
static void
 
413
test_authorizer_refresh_authorization_error (AuthorizerData *data, gconstpointer user_data)
 
414
{
 
415
        gboolean success;
 
416
        GError *error = NULL;
 
417
 
 
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));
 
420
 
 
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));
 
423
 
 
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);
 
428
 
 
429
        g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "counter")), ==, 1);
 
430
}
 
431
 
 
432
/* Test that calling refresh_authorization() on an authorizer which doesn't implement it returns FALSE without an error */
 
433
static void
 
434
test_authorizer_refresh_authorization_unimplemented (AuthorizerData *data, gconstpointer user_data)
 
435
{
 
436
        gboolean success;
 
437
        GError *error = NULL;
 
438
 
 
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);
 
443
}
 
444
 
 
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) */
 
447
static void
 
448
test_authorizer_refresh_authorization_cancellation_unimplemented (AuthorizerData *data, gconstpointer user_data)
 
449
{
 
450
        GCancellable *cancellable;
 
451
        gboolean success;
 
452
        GError *error = NULL;
 
453
 
 
454
        cancellable = g_cancellable_new ();
 
455
        g_cancellable_cancel (cancellable);
 
456
 
 
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);
 
461
 
 
462
        g_object_unref (cancellable);
 
463
}
 
464
 
 
465
/* Set of standard async callback functions for refresh_authorization_async() which check various combinations of success and error value */
 
466
static void
 
467
test_authorizer_refresh_authorization_async_success_no_error_cb (GDataAuthorizer *authorizer, GAsyncResult *async_result, GMainLoop *main_loop)
 
468
{
 
469
        gboolean success;
 
470
        GError *error = NULL;
 
471
 
 
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);
 
476
 
 
477
        g_main_loop_quit (main_loop);
 
478
}
 
479
 
 
480
static void
 
481
test_authorizer_refresh_authorization_async_failure_no_error_cb (GDataAuthorizer *authorizer, GAsyncResult *async_result, GMainLoop *main_loop)
 
482
{
 
483
        gboolean success;
 
484
        GError *error = NULL;
 
485
 
 
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);
 
490
 
 
491
        g_main_loop_quit (main_loop);
 
492
}
 
493
 
 
494
static void
 
495
test_authorizer_refresh_authorization_async_network_error_cb (GDataAuthorizer *authorizer, GAsyncResult *async_result, GMainLoop *main_loop)
 
496
{
 
497
        gboolean success;
 
498
        GError *error = NULL;
 
499
 
 
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);
 
504
 
 
505
        g_main_loop_quit (main_loop);
 
506
}
 
507
 
 
508
static void
 
509
test_authorizer_refresh_authorization_async_protocol_error_cb (GDataAuthorizer *authorizer, GAsyncResult *async_result, GMainLoop *main_loop)
 
510
{
 
511
        gboolean success;
 
512
        GError *error = NULL;
 
513
 
 
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);
 
518
 
 
519
        g_main_loop_quit (main_loop);
 
520
}
 
521
 
 
522
static void
 
523
test_authorizer_refresh_authorization_async_cancelled_error_cb (GDataAuthorizer *authorizer, GAsyncResult *async_result, GMainLoop *main_loop)
 
524
{
 
525
        gboolean success;
 
526
        GError *error = NULL;
 
527
 
 
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);
 
532
 
 
533
        g_main_loop_quit (main_loop);
 
534
}
 
535
 
 
536
/* Test that calling refresh_authorization_async() on an authorizer which implements it returns TRUE without an error */
 
537
static void
 
538
test_authorizer_refresh_authorization_async (AuthorizerData *data, gconstpointer user_data)
 
539
{
 
540
        GMainLoop *main_loop;
 
541
 
 
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));
 
546
 
 
547
        main_loop = g_main_loop_new (NULL, FALSE);
 
548
 
 
549
        gdata_authorizer_refresh_authorization_async (data->authorizer, NULL,
 
550
                                                      (GAsyncReadyCallback) test_authorizer_refresh_authorization_async_success_no_error_cb,
 
551
                                                      main_loop);
 
552
 
 
553
        g_main_loop_run (main_loop);
 
554
 
 
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);
 
558
 
 
559
        g_main_loop_unref (main_loop);
 
560
}
 
561
 
 
562
/* Test that calling refresh_authorization_async() on an authorizer which implements it with an error returns FALSE with the appropriate error */
 
563
static void
 
564
test_authorizer_refresh_authorization_async_error (AuthorizerData *data, gconstpointer user_data)
 
565
{
 
566
        GMainLoop *main_loop;
 
567
 
 
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));
 
572
 
 
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));
 
575
 
 
576
        main_loop = g_main_loop_new (NULL, FALSE);
 
577
 
 
578
        gdata_authorizer_refresh_authorization_async (data->authorizer, NULL,
 
579
                                                      (GAsyncReadyCallback) test_authorizer_refresh_authorization_async_network_error_cb, main_loop);
 
580
 
 
581
        g_main_loop_run (main_loop);
 
582
 
 
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);
 
586
 
 
587
        g_main_loop_unref (main_loop);
 
588
}
 
589
 
 
590
/* Test that calling refresh_authorization_async() on an authorizer which implements it, then cancelling the call returns FALSE with a cancellation
 
591
 * error */
 
592
static void
 
593
test_authorizer_refresh_authorization_async_cancellation (AuthorizerData *data, gconstpointer user_data)
 
594
{
 
595
        GCancellable *cancellable;
 
596
        GMainLoop *main_loop;
 
597
 
 
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));
 
602
 
 
603
        main_loop = g_main_loop_new (NULL, FALSE);
 
604
 
 
605
        cancellable = g_cancellable_new ();
 
606
        g_cancellable_cancel (cancellable);
 
607
 
 
608
        gdata_authorizer_refresh_authorization_async (data->authorizer, cancellable,
 
609
                                                      (GAsyncReadyCallback) test_authorizer_refresh_authorization_async_cancelled_error_cb,
 
610
                                                      main_loop);
 
611
 
 
612
        g_main_loop_run (main_loop);
 
613
 
 
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);
 
617
 
 
618
        g_object_unref (cancellable);
 
619
        g_main_loop_unref (main_loop);
 
620
}
 
621
 
 
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 */
 
624
static void
 
625
test_authorizer_refresh_authorization_async_simulated (AuthorizerData *data, gconstpointer user_data)
 
626
{
 
627
        GMainLoop *main_loop;
 
628
 
 
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));
 
631
 
 
632
        main_loop = g_main_loop_new (NULL, FALSE);
 
633
 
 
634
        gdata_authorizer_refresh_authorization_async (data->authorizer, NULL,
 
635
                                                      (GAsyncReadyCallback) test_authorizer_refresh_authorization_async_success_no_error_cb,
 
636
                                                      main_loop);
 
637
 
 
638
        g_main_loop_run (main_loop);
 
639
 
 
640
        g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "counter")), ==, 1);
 
641
 
 
642
        g_main_loop_unref (main_loop);
 
643
}
 
644
 
 
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 */
 
647
static void
 
648
test_authorizer_refresh_authorization_async_error_simulated (AuthorizerData *data, gconstpointer user_data)
 
649
{
 
650
        GMainLoop *main_loop;
 
651
 
 
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));
 
654
 
 
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));
 
657
 
 
658
        main_loop = g_main_loop_new (NULL, FALSE);
 
659
 
 
660
        gdata_authorizer_refresh_authorization_async (data->authorizer, NULL,
 
661
                                                      (GAsyncReadyCallback) test_authorizer_refresh_authorization_async_protocol_error_cb, main_loop);
 
662
 
 
663
        g_main_loop_run (main_loop);
 
664
 
 
665
        g_assert_cmpuint (GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (data->authorizer), "counter")), ==, 1);
 
666
 
 
667
        g_main_loop_unref (main_loop);
 
668
}
 
669
 
 
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 */
 
672
static void
 
673
test_authorizer_refresh_authorization_async_cancellation_simulated (AuthorizerData *data, gconstpointer user_data)
 
674
{
 
675
        GCancellable *cancellable;
 
676
        GMainLoop *main_loop;
 
677
 
 
678
        main_loop = g_main_loop_new (NULL, FALSE);
 
679
 
 
680
        cancellable = g_cancellable_new ();
 
681
        g_cancellable_cancel (cancellable);
 
682
 
 
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,
 
687
                                                      main_loop);
 
688
 
 
689
        g_main_loop_run (main_loop);
 
690
 
 
691
        g_object_unref (cancellable);
 
692
        g_main_loop_unref (main_loop);
 
693
}
 
694
 
 
695
/* Test that calling refresh_authorization_async() on an authorizer which doesn't implement it returns FALSE without an error */
 
696
static void
 
697
test_authorizer_refresh_authorization_async_unimplemented (AuthorizerData *data, gconstpointer user_data)
 
698
{
 
699
        GMainLoop *main_loop;
 
700
 
 
701
        main_loop = g_main_loop_new (NULL, FALSE);
 
702
 
 
703
        gdata_authorizer_refresh_authorization_async (data->authorizer, NULL,
 
704
                                                      (GAsyncReadyCallback) test_authorizer_refresh_authorization_async_failure_no_error_cb,
 
705
                                                      main_loop);
 
706
 
 
707
        g_main_loop_run (main_loop);
 
708
 
 
709
        g_main_loop_unref (main_loop);
 
710
}
 
711
 
 
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) */
 
714
static void
 
715
test_authorizer_refresh_authorization_async_cancellation_unimplemented (AuthorizerData *data, gconstpointer user_data)
 
716
{
 
717
        GCancellable *cancellable;
 
718
        GMainLoop *main_loop;
 
719
 
 
720
        main_loop = g_main_loop_new (NULL, FALSE);
 
721
 
 
722
        cancellable = g_cancellable_new ();
 
723
        g_cancellable_cancel (cancellable);
 
724
 
 
725
        gdata_authorizer_refresh_authorization_async (data->authorizer, cancellable,
 
726
                                                      (GAsyncReadyCallback) test_authorizer_refresh_authorization_async_failure_no_error_cb,
 
727
                                                      main_loop);
 
728
 
 
729
        g_main_loop_run (main_loop);
 
730
 
 
731
        g_object_unref (cancellable);
 
732
        g_main_loop_unref (main_loop);
 
733
}
 
734
 
 
735
int
 
736
main (int argc, char *argv[])
 
737
{
 
738
        int retval;
 
739
 
 
740
        gdata_test_init (argc, argv);
 
741
 
 
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",
 
745
                                     "scope", "scope1",
 
746
                                     NULL);
 
747
        test_domain2 = g_object_new (GDATA_TYPE_AUTHORIZATION_DOMAIN,
 
748
                                     "service-name", "service-name2",
 
749
                                     "scope", "scope2",
 
750
                                     NULL);
 
751
 
 
752
        /* GDataAuthorizationDomain tests */
 
753
        g_test_add_func ("/authorization-domain/properties", test_authorization_domain_properties);
 
754
 
 
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);
 
788
 
 
789
        retval = g_test_run ();
 
790
 
 
791
        g_object_unref (test_domain2);
 
792
        g_object_unref (test_domain1);
 
793
 
 
794
        return retval;
 
795
}