~ubuntu-branches/ubuntu/natty/packagekit/natty

« back to all changes in this revision

Viewing changes to .pc/glib-2.24.diff/lib/packagekit-glib2/pk-self-test.c

  • Committer: Bazaar Package Importer
  • Author(s): Rohan Garg
  • Date: 2011-01-10 18:40:10 UTC
  • mfrom: (1.3.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110110184010-zzgp9mdbu8101nai
Tags: 0.6.11-1ubuntu1
* Merge from debian, remaining changes
  - Add ubuntu_01_null_selection_crash.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2
 
 *
3
 
 * Copyright (C) 2007-2010 Richard Hughes <richard@hughsie.com>
4
 
 *
5
 
 * Licensed under the GNU Lesser General Public License Version 2.1
6
 
 *
7
 
 * This library is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU Lesser General Public
9
 
 * License as published by the Free Software Foundation; either
10
 
 * version 2.1 of the License, or (at your option) any later version.
11
 
 *
12
 
 * This library is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
 * Lesser General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU Lesser General Public
18
 
 * License along with this library; if not, write to the Free Software
19
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
20
 
 */
21
 
 
22
 
#include "config.h"
23
 
 
24
 
#include <glib-object.h>
25
 
#include <glib/gstdio.h>
26
 
#include <gio/gunixsocketaddress.h>
27
 
 
28
 
#include "pk-catalog.h"
29
 
#include "pk-client.h"
30
 
#include "pk-client-helper.h"
31
 
#include "pk-common.h"
32
 
#include "pk-control.h"
33
 
#include "pk-console-shared.h"
34
 
#include "pk-desktop.h"
35
 
#include "pk-enum.h"
36
 
#include "pk-package.h"
37
 
#include "pk-package-id.h"
38
 
#include "pk-package-ids.h"
39
 
#include "pk-package-sack.h"
40
 
#include "pk-results.h"
41
 
#include "pk-task.h"
42
 
#include "pk-task-text.h"
43
 
#include "pk-task-wrapper.h"
44
 
#include "pk-transaction-list.h"
45
 
#include "pk-version.h"
46
 
#include "pk-control-sync.h"
47
 
#include "pk-client-sync.h"
48
 
#include "pk-progress-bar.h"
49
 
#include "pk-service-pack.h"
50
 
#include "pk-debug.h"
51
 
 
52
 
/** ver:1.0 ***********************************************************/
53
 
static GMainLoop *_test_loop = NULL;
54
 
static guint _test_loop_timeout_id = 0;
55
 
 
56
 
static gboolean
57
 
_g_test_hang_check_cb (gpointer user_data)
58
 
{
59
 
        guint timeout_ms = *((guint*) user_data);
60
 
        g_main_loop_quit (_test_loop);
61
 
        g_warning ("loop not completed in %ims", timeout_ms);
62
 
        g_assert_not_reached ();
63
 
        return FALSE;
64
 
}
65
 
 
66
 
/**
67
 
 * _g_test_loop_run_with_timeout:
68
 
 **/
69
 
static void
70
 
_g_test_loop_run_with_timeout (guint timeout_ms)
71
 
{
72
 
        g_assert (_test_loop_timeout_id == 0);
73
 
        _test_loop = g_main_loop_new (NULL, FALSE);
74
 
        _test_loop_timeout_id = g_timeout_add (timeout_ms, _g_test_hang_check_cb, &timeout_ms);
75
 
        g_main_loop_run (_test_loop);
76
 
}
77
 
 
78
 
#if 0
79
 
static gboolean
80
 
_g_test_hang_wait_cb (gpointer user_data)
81
 
{
82
 
        g_main_loop_quit (_test_loop);
83
 
        _test_loop_timeout_id = 0;
84
 
        return FALSE;
85
 
}
86
 
 
87
 
/**
88
 
 * _g_test_loop_wait:
89
 
 **/
90
 
static void
91
 
_g_test_loop_wait (guint timeout_ms)
92
 
{
93
 
        g_assert (_test_loop_timeout_id == 0);
94
 
        _test_loop = g_main_loop_new (NULL, FALSE);
95
 
        _test_loop_timeout_id = g_timeout_add (timeout_ms, _g_test_hang_wait_cb, &timeout_ms);
96
 
        g_main_loop_run (_test_loop);
97
 
}
98
 
#endif
99
 
 
100
 
/**
101
 
 * _g_test_loop_quit:
102
 
 **/
103
 
static void
104
 
_g_test_loop_quit (void)
105
 
{
106
 
        if (_test_loop_timeout_id > 0) {
107
 
                g_source_remove (_test_loop_timeout_id);
108
 
                _test_loop_timeout_id = 0;
109
 
        }
110
 
        if (_test_loop != NULL) {
111
 
                g_main_loop_quit (_test_loop);
112
 
                g_main_loop_unref (_test_loop);
113
 
                _test_loop = NULL;
114
 
        }
115
 
}
116
 
 
117
 
/**********************************************************************/
118
 
 
119
 
static void
120
 
pk_test_bitfield_func (void)
121
 
{
122
 
        gchar *text;
123
 
        PkBitfield filter;
124
 
        gint value;
125
 
        PkBitfield values;
126
 
 
127
 
        /* check we can convert filter bitfield to text (none) */
128
 
        text = pk_filter_bitfield_to_string (pk_bitfield_value (PK_FILTER_ENUM_NONE));
129
 
        g_assert_cmpstr (text, ==, "none");
130
 
        g_free (text);
131
 
 
132
 
        /* check we can invert a bit 1 -> 0 */
133
 
        values = pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT) | pk_bitfield_value (PK_FILTER_ENUM_NOT_NEWEST);
134
 
        pk_bitfield_invert (values, PK_FILTER_ENUM_NOT_DEVELOPMENT);
135
 
        g_assert_cmpint (values, ==, pk_bitfield_value (PK_FILTER_ENUM_NOT_NEWEST));
136
 
 
137
 
        /* check we can invert a bit 0 -> 1 */
138
 
        values = 0;
139
 
        pk_bitfield_invert (values, PK_FILTER_ENUM_NOT_DEVELOPMENT);
140
 
        g_assert_cmpint (values, ==, pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT));
141
 
 
142
 
        /* check we can convert filter bitfield to text (single) */
143
 
        text = pk_filter_bitfield_to_string (pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT));
144
 
        g_assert_cmpstr (text, ==, "~devel");
145
 
        g_free (text);
146
 
 
147
 
        /* check we can convert filter bitfield to text (plural) */
148
 
        text = pk_filter_bitfield_to_string (pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT) |
149
 
                   pk_bitfield_value (PK_FILTER_ENUM_GUI) |
150
 
                   pk_bitfield_value (PK_FILTER_ENUM_NEWEST));
151
 
        g_assert_cmpstr (text, ==, "~devel;gui;newest");
152
 
        g_free (text);
153
 
 
154
 
        /* check we can convert filter text to bitfield (none) */
155
 
        filter = pk_filter_bitfield_from_string ("none");
156
 
        g_assert_cmpint (filter, ==, pk_bitfield_value (PK_FILTER_ENUM_NONE));
157
 
 
158
 
        /* check we can convert filter text to bitfield (single) */
159
 
        filter = pk_filter_bitfield_from_string ("~devel");
160
 
        g_assert_cmpint (filter, ==, pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT));
161
 
 
162
 
        /* check we can convert filter text to bitfield (plural) */
163
 
        filter = pk_filter_bitfield_from_string ("~devel;gui;newest");
164
 
        g_assert_cmpint (filter, ==, (pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT) |
165
 
                       pk_bitfield_value (PK_FILTER_ENUM_GUI) |
166
 
                       pk_bitfield_value (PK_FILTER_ENUM_NEWEST)));
167
 
 
168
 
        /* check we can add / remove bitfield */
169
 
        filter = pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT) |
170
 
                 pk_bitfield_value (PK_FILTER_ENUM_GUI) |
171
 
                 pk_bitfield_value (PK_FILTER_ENUM_NEWEST);
172
 
        pk_bitfield_add (filter, PK_FILTER_ENUM_NOT_FREE);
173
 
        pk_bitfield_remove (filter, PK_FILTER_ENUM_NOT_DEVELOPMENT);
174
 
        text = pk_filter_bitfield_to_string (filter);
175
 
        g_assert_cmpstr (text, ==, "gui;~free;newest");
176
 
        g_free (text);
177
 
 
178
 
        /* check we can test enum presence */
179
 
        filter = pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT) |
180
 
                 pk_bitfield_value (PK_FILTER_ENUM_GUI) |
181
 
                 pk_bitfield_value (PK_FILTER_ENUM_NEWEST);
182
 
        g_assert (pk_bitfield_contain (filter, PK_FILTER_ENUM_NOT_DEVELOPMENT));
183
 
 
184
 
        /* check we can test enum false-presence */
185
 
        g_assert (!pk_bitfield_contain (filter, PK_FILTER_ENUM_FREE));
186
 
 
187
 
        /* check we can add / remove bitfield to nothing */
188
 
        filter = pk_bitfield_value (PK_FILTER_ENUM_NOT_DEVELOPMENT);
189
 
        pk_bitfield_remove (filter, PK_FILTER_ENUM_NOT_DEVELOPMENT);
190
 
        text = pk_filter_bitfield_to_string (filter);
191
 
        g_assert_cmpstr (text, ==, "none");
192
 
        g_free (text);
193
 
 
194
 
        /* role bitfield from enums (unknown) */
195
 
        values = pk_bitfield_from_enums (PK_ROLE_ENUM_UNKNOWN, -1);
196
 
        g_assert_cmpint (values, ==, pk_bitfield_value (PK_ROLE_ENUM_UNKNOWN));
197
 
 
198
 
        /* role bitfield from enums (random) */
199
 
        values = pk_bitfield_from_enums (PK_ROLE_ENUM_SEARCH_GROUP, PK_ROLE_ENUM_SEARCH_DETAILS, -1);
200
 
        g_assert_cmpint (values, ==, (pk_bitfield_value (PK_ROLE_ENUM_SEARCH_DETAILS) |
201
 
                       pk_bitfield_value (PK_ROLE_ENUM_SEARCH_GROUP)));
202
 
 
203
 
        /* group bitfield from enums (unknown) */
204
 
        values = pk_bitfield_from_enums (PK_GROUP_ENUM_UNKNOWN, -1);
205
 
        g_assert_cmpint (values, ==, pk_bitfield_value (PK_GROUP_ENUM_UNKNOWN));
206
 
 
207
 
        /* group bitfield from enums (random) */
208
 
        values = pk_bitfield_from_enums (PK_GROUP_ENUM_ACCESSIBILITY, -1);
209
 
        g_assert_cmpint (values, ==, (pk_bitfield_value (PK_GROUP_ENUM_ACCESSIBILITY)));
210
 
 
211
 
        /* group bitfield to text (unknown) */
212
 
        values = pk_bitfield_from_enums (PK_GROUP_ENUM_UNKNOWN, -1);
213
 
        text = pk_group_bitfield_to_string (values);
214
 
        g_assert_cmpstr (text, ==, "unknown");
215
 
        g_free (text);
216
 
 
217
 
        /* group bitfield to text (first and last) */
218
 
        values = pk_bitfield_from_enums (PK_GROUP_ENUM_ACCESSIBILITY, PK_GROUP_ENUM_UNKNOWN, -1);
219
 
        text = pk_group_bitfield_to_string (values);
220
 
        g_assert_cmpstr (text, ==, "unknown;accessibility");
221
 
        g_free (text);
222
 
 
223
 
        /* group bitfield to text (random) */
224
 
        values = pk_bitfield_from_enums (PK_GROUP_ENUM_UNKNOWN, PK_GROUP_ENUM_REPOS, -1);
225
 
        text = pk_group_bitfield_to_string (values);
226
 
        g_assert_cmpstr (text, ==, "unknown;repos");
227
 
        g_free (text);
228
 
 
229
 
        /* priority check missing */
230
 
        values = pk_bitfield_value (PK_ROLE_ENUM_SEARCH_DETAILS) |
231
 
                 pk_bitfield_value (PK_ROLE_ENUM_SEARCH_GROUP);
232
 
        value = pk_bitfield_contain_priority (values, PK_ROLE_ENUM_SEARCH_FILE, -1);
233
 
        g_assert_cmpint (value, ==, -1);
234
 
 
235
 
        /* priority check first */
236
 
        value = pk_bitfield_contain_priority (values, PK_ROLE_ENUM_SEARCH_GROUP, -1);
237
 
        g_assert_cmpint (value, ==, PK_ROLE_ENUM_SEARCH_GROUP);
238
 
 
239
 
        /* priority check second, correct */
240
 
        value = pk_bitfield_contain_priority (values, PK_ROLE_ENUM_SEARCH_FILE, PK_ROLE_ENUM_SEARCH_GROUP, -1);
241
 
        g_assert_cmpint (value, ==, PK_ROLE_ENUM_SEARCH_GROUP);
242
 
}
243
 
 
244
 
static void
245
 
pk_test_catalog_lookup_cb (GObject *object, GAsyncResult *res, gpointer user_data)
246
 
{
247
 
        PkCatalog *catalog = PK_CATALOG (object);
248
 
        GError *error = NULL;
249
 
        GPtrArray *array;
250
 
        guint i;
251
 
        PkPackage *package;
252
 
 
253
 
        /* get the results */
254
 
        array = pk_catalog_lookup_finish (catalog, res, &error);
255
 
        g_assert_no_error (error);
256
 
        g_assert (array != NULL);
257
 
        g_assert_cmpint (array->len, ==, 3);
258
 
 
259
 
        /* list for shits and giggles */
260
 
        for (i=0; i<array->len; i++) {
261
 
                package = g_ptr_array_index (array, i);
262
 
                g_debug ("%i\t%s", i, pk_package_get_id (package));
263
 
        }
264
 
        g_ptr_array_unref (array);
265
 
        _g_test_loop_quit ();
266
 
}
267
 
 
268
 
static void
269
 
pk_test_catalog_progress_cb (PkProgress *progress, PkProgressType type, gpointer user_data)
270
 
{
271
 
        PkStatusEnum status;
272
 
        if (type == PK_PROGRESS_TYPE_STATUS) {
273
 
                g_object_get (progress,
274
 
                      "status", &status,
275
 
                      NULL);
276
 
                g_debug ("now %s", pk_status_enum_to_string (status));
277
 
        }
278
 
}
279
 
 
280
 
static void
281
 
pk_test_catalog_func (void)
282
 
{
283
 
        PkCatalog *catalog;
284
 
 
285
 
        catalog = pk_catalog_new ();
286
 
        g_assert (catalog != NULL);
287
 
 
288
 
        /* lookup catalog */
289
 
        pk_catalog_lookup_async (catalog, TESTDATADIR "/test.catalog", NULL,
290
 
                                 (PkProgressCallback) pk_test_catalog_progress_cb, NULL,
291
 
                                 (GAsyncReadyCallback) pk_test_catalog_lookup_cb, NULL);
292
 
        _g_test_loop_run_with_timeout (150000);
293
 
        g_debug ("resolvd, searched, etc. in %f", g_test_timer_elapsed ());
294
 
 
295
 
        g_object_unref (catalog);
296
 
}
297
 
 
298
 
/**
299
 
 * pk_test_client_helper_output_cb:
300
 
 **/
301
 
static gboolean
302
 
pk_test_client_helper_output_cb (GSocket *socket, GIOCondition condition, gpointer user_data)
303
 
{
304
 
        GError *error = NULL;
305
 
        gsize len;
306
 
        gchar buffer[6];
307
 
        gboolean ret = TRUE;
308
 
 
309
 
        /* the helper process exited */
310
 
        if ((condition & G_IO_HUP) > 0) {
311
 
                g_warning ("socket was disconnected");
312
 
                ret = FALSE;
313
 
                goto out;
314
 
        }
315
 
 
316
 
        /* there is data */
317
 
        if ((condition & G_IO_IN) > 0) {
318
 
                len = g_socket_receive (socket, buffer, 6, NULL, &error);
319
 
                g_assert_no_error (error);
320
 
                g_assert_cmpint (len, >, 0);
321
 
 
322
 
                /* good for us */
323
 
                if (g_strcmp0 (buffer, "pong\n") == 0) {
324
 
                        _g_test_loop_quit ();
325
 
                        goto out;
326
 
                }
327
 
                /* bad */
328
 
                g_warning ("child returned unexpected data: %s", buffer);
329
 
        }
330
 
out:
331
 
        return ret;
332
 
}
333
 
 
334
 
static void
335
 
pk_test_client_helper_func (void)
336
 
{
337
 
        gboolean ret;
338
 
        GError *error = NULL;
339
 
        gchar *filename;
340
 
        PkClientHelper *client_helper;
341
 
        const gchar *argv[] = { TESTDATADIR "/pk-client-helper-test.py", NULL };
342
 
        const gchar *envp[] = { "DAVE=1", NULL };
343
 
        GSocket *socket = NULL;
344
 
        GSocketAddress *address = NULL;
345
 
        gsize wrote;
346
 
        GSource *source;
347
 
 
348
 
        client_helper = pk_client_helper_new ();
349
 
        g_assert (client_helper != NULL);
350
 
 
351
 
        /* unref without using */
352
 
        g_object_unref (client_helper);
353
 
 
354
 
        /* new object */
355
 
        client_helper = pk_client_helper_new ();
356
 
        g_assert (client_helper != NULL);
357
 
 
358
 
        /* create a socket filename */
359
 
        filename = g_build_filename (g_get_tmp_dir (), "pk-self-test.socket", NULL);
360
 
 
361
 
        /* ensure previous sockets are deleted */
362
 
        g_unlink (filename);
363
 
 
364
 
        /* start a demo program */
365
 
        ret = pk_client_helper_start (client_helper, filename, (gchar**) argv, (gchar**) envp, &error);
366
 
        g_assert_no_error (error);
367
 
        g_assert (ret);
368
 
        g_assert (g_file_test (filename, G_FILE_TEST_EXISTS));
369
 
 
370
 
        /* create socket */
371
 
        socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
372
 
        g_assert_no_error (error);
373
 
        g_assert (socket != NULL);
374
 
        g_socket_set_blocking (socket, FALSE);
375
 
        g_socket_set_keepalive (socket, TRUE);
376
 
 
377
 
        /* connect to it */
378
 
        address = g_unix_socket_address_new_with_type (filename, -1, G_UNIX_SOCKET_ADDRESS_PATH);
379
 
        ret = g_socket_connect (socket, address, NULL, &error);
380
 
        g_assert_no_error (error);
381
 
        g_assert (ret);
382
 
 
383
 
        /* socket has data */
384
 
        source = g_socket_create_source (socket, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, NULL);
385
 
        g_source_set_callback (source, (GSourceFunc) pk_test_client_helper_output_cb, NULL, NULL);
386
 
        g_source_attach (source, NULL);
387
 
 
388
 
        /* send some data */
389
 
        wrote = g_socket_send (socket, "ping\n", 5, NULL, &error);
390
 
        g_assert_no_error (error);
391
 
        g_assert_cmpint (wrote, ==, 5);
392
 
 
393
 
        /* run for a few seconds */
394
 
        _g_test_loop_run_with_timeout (1000);
395
 
 
396
 
        /* stop the demo program */
397
 
        ret = pk_client_helper_stop (client_helper, &error);
398
 
        g_assert_no_error (error);
399
 
        g_assert (ret);
400
 
 
401
 
        /* delete socket */
402
 
        g_unlink (filename);
403
 
 
404
 
        g_free (filename);
405
 
        g_object_unref (socket);
406
 
        g_object_unref (address);
407
 
        g_object_unref (client_helper);
408
 
}
409
 
 
410
 
static void
411
 
pk_test_client_resolve_cb (GObject *object, GAsyncResult *res, gpointer user_data)
412
 
{
413
 
        PkClient *client = PK_CLIENT (object);
414
 
        GError *error = NULL;
415
 
        PkResults *results = NULL;
416
 
        PkExitEnum exit_enum;
417
 
        GPtrArray *packages;
418
 
        gboolean idle;
419
 
 
420
 
        /* get the results */
421
 
        results = pk_client_generic_finish (client, res, &error);
422
 
        g_assert_no_error (error);
423
 
        g_assert (results != NULL);
424
 
 
425
 
        exit_enum = pk_results_get_exit_code (results);
426
 
        g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_SUCCESS);
427
 
 
428
 
        packages = pk_results_get_package_array (results);
429
 
        g_assert (packages != NULL);
430
 
 
431
 
        /* check idle */
432
 
        g_object_get (client, "idle", &idle, NULL);
433
 
        g_assert (idle);
434
 
        g_assert_cmpint (packages->len, ==, 2);
435
 
 
436
 
        g_ptr_array_unref (packages);
437
 
 
438
 
        g_debug ("results exit enum = %s", pk_exit_enum_to_string (exit_enum));
439
 
 
440
 
        g_object_unref (results);
441
 
        _g_test_loop_quit ();
442
 
}
443
 
 
444
 
static void
445
 
pk_test_client_get_details_cb (GObject *object, GAsyncResult *res, gpointer user_data)
446
 
{
447
 
        PkClient *client = PK_CLIENT (object);
448
 
        GError *error = NULL;
449
 
        PkResults *results = NULL;
450
 
        PkExitEnum exit_enum;
451
 
        GPtrArray *details;
452
 
 
453
 
        /* get the results */
454
 
        results = pk_client_generic_finish (client, res, &error);
455
 
        g_assert_no_error (error);
456
 
        g_assert (results != NULL);
457
 
 
458
 
        exit_enum = pk_results_get_exit_code (results);
459
 
        g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_SUCCESS);
460
 
 
461
 
        details = pk_results_get_details_array (results);
462
 
        g_assert (details != NULL);
463
 
        g_assert_cmpint (details->len, ==, 1);
464
 
 
465
 
        g_ptr_array_unref (details);
466
 
 
467
 
        g_debug ("results exit enum = %s", pk_exit_enum_to_string (exit_enum));
468
 
 
469
 
        g_object_unref (results);
470
 
        _g_test_loop_quit ();
471
 
}
472
 
 
473
 
static void
474
 
pk_test_client_get_updates_cb (GObject *object, GAsyncResult *res, gpointer user_data)
475
 
{
476
 
        PkClient *client = PK_CLIENT (object);
477
 
        GError *error = NULL;
478
 
        PkResults *results = NULL;
479
 
        PkExitEnum exit_enum;
480
 
        PkPackageSack *sack;
481
 
        guint size;
482
 
 
483
 
        /* get the results */
484
 
        results = pk_client_generic_finish (client, res, &error);
485
 
        g_assert_no_error (error);
486
 
        g_assert (results != NULL);
487
 
 
488
 
        exit_enum = pk_results_get_exit_code (results);
489
 
        g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_SUCCESS);
490
 
 
491
 
        sack = pk_results_get_package_sack (results);
492
 
        g_assert (sack != NULL);
493
 
 
494
 
        /* check size */
495
 
        size = pk_package_sack_get_size (sack);
496
 
        g_assert_cmpint (size, ==, 3);
497
 
 
498
 
        g_object_unref (sack);
499
 
 
500
 
        g_debug ("results exit enum = %s", pk_exit_enum_to_string (exit_enum));
501
 
 
502
 
        g_object_unref (results);
503
 
        _g_test_loop_quit ();
504
 
}
505
 
 
506
 
static void
507
 
pk_test_client_search_name_cb (GObject *object, GAsyncResult *res, gpointer user_data)
508
 
{
509
 
        PkClient *client = PK_CLIENT (object);
510
 
        GError *error = NULL;
511
 
        PkResults *results = NULL;
512
 
        PkExitEnum exit_enum;
513
 
        PkError *error_code = NULL;
514
 
 
515
 
        /* get the results */
516
 
        results = pk_client_generic_finish (client, res, &error);
517
 
        g_assert_no_error (error);
518
 
        g_assert (results != NULL);
519
 
 
520
 
        exit_enum = pk_results_get_exit_code (results);
521
 
        g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_CANCELLED);
522
 
 
523
 
        /* check error code */
524
 
        error_code = pk_results_get_error_code (results);
525
 
        g_assert_cmpint (pk_error_get_code (error_code), ==, PK_ERROR_ENUM_TRANSACTION_CANCELLED);
526
 
        g_assert_cmpstr (pk_error_get_details (error_code), ==, "The task was stopped successfully");
527
 
//      g_assert_cmpstr (pk_error_get_details (error_code), ==, "transaction was cancelled");
528
 
 
529
 
        if (error_code != NULL)
530
 
                g_object_unref (error_code);
531
 
        if (results != NULL)
532
 
                g_object_unref (results);
533
 
        _g_test_loop_quit ();
534
 
}
535
 
 
536
 
static void
537
 
pk_test_client_search_name_cancellable_cancelled_cb (GObject *object, GAsyncResult *res, gpointer user_data)
538
 
{
539
 
        PkClient *client = PK_CLIENT (object);
540
 
        GError *error = NULL;
541
 
        PkResults *results = NULL;
542
 
        results = pk_client_generic_finish (client, res, &error);
543
 
        g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
544
 
        g_assert (results == NULL);
545
 
        _g_test_loop_quit ();
546
 
}
547
 
 
548
 
static guint _progress_cb = 0;
549
 
static guint _status_cb = 0;
550
 
static guint _package_cb = 0;
551
 
static guint _allow_cancel_cb = 0;
552
 
gchar *_tid = NULL;
553
 
 
554
 
static void
555
 
pk_test_client_progress_cb (PkProgress *progress, PkProgressType type, gpointer user_data)
556
 
{
557
 
        gchar *tid;
558
 
        if (type == PK_PROGRESS_TYPE_PACKAGE_ID)
559
 
                _package_cb++;
560
 
        if (type == PK_PROGRESS_TYPE_PERCENTAGE)
561
 
                _progress_cb++;
562
 
        if (type == PK_PROGRESS_TYPE_SUBPERCENTAGE)
563
 
                _progress_cb++;
564
 
        if (type == PK_PROGRESS_TYPE_ALLOW_CANCEL)
565
 
                _allow_cancel_cb++;
566
 
        if (type == PK_PROGRESS_TYPE_STATUS)
567
 
                _status_cb++;
568
 
 
569
 
        /* get the running transaction id if we've not set it before */
570
 
        g_object_get (progress, "transaction-id", &tid, NULL);
571
 
        if (tid != NULL && _tid == NULL)
572
 
                _tid = g_strdup (tid);
573
 
        g_free (tid);
574
 
}
575
 
 
576
 
static gboolean
577
 
pk_test_client_cancel_cb (GCancellable *cancellable)
578
 
{
579
 
        g_debug ("cancelling method");
580
 
        g_cancellable_cancel (cancellable);
581
 
        return FALSE;
582
 
}
583
 
 
584
 
static void
585
 
pk_test_client_download_cb (GObject *object, GAsyncResult *res, gpointer user_data)
586
 
{
587
 
        PkClient *client = PK_CLIENT (object);
588
 
        GError *error = NULL;
589
 
        PkResults *results = NULL;
590
 
        PkExitEnum exit_enum;
591
 
        PkFiles *item;
592
 
        GPtrArray *array = NULL;
593
 
        gchar *package_id = NULL;
594
 
        gchar **files = NULL;
595
 
 
596
 
        /* get the results */
597
 
        results = pk_client_generic_finish (client, res, &error);
598
 
        g_assert_no_error (error);
599
 
        g_assert (results != NULL);
600
 
 
601
 
        exit_enum = pk_results_get_exit_code (results);
602
 
        g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_SUCCESS);
603
 
 
604
 
        /* check number */
605
 
        array = pk_results_get_files_array (results);
606
 
        g_assert_cmpint (array->len, ==, 2);
607
 
 
608
 
        /* check a result */
609
 
        item = g_ptr_array_index (array, 0);
610
 
        g_object_get (item,
611
 
                      "package-id", &package_id,
612
 
                      "files", &files,
613
 
                      NULL);
614
 
        g_assert_cmpstr (package_id, ==, "powertop-common;1.8-1.fc8;i386;fedora");
615
 
        g_assert_cmpint (g_strv_length (files), ==, 1);
616
 
        g_assert_cmpstr (files[0], ==, "/tmp/powertop-common-1.8-1.fc8.rpm");
617
 
 
618
 
        g_strfreev (files);
619
 
        g_free (package_id);
620
 
        g_ptr_array_unref (array);
621
 
        if (results != NULL)
622
 
                g_object_unref (results);
623
 
        _g_test_loop_quit ();
624
 
}
625
 
 
626
 
#if 0
627
 
static void
628
 
pk_test_client_recursive_signal_cb (PkControl *control, gpointer user_data)
629
 
{
630
 
        gboolean ret;
631
 
        ret = pk_control_get_properties (control, NULL, NULL);
632
 
        g_assert (ret);
633
 
}
634
 
#endif
635
 
 
636
 
static void
637
 
pk_test_client_notify_idle_cb (PkClient *client, GParamSpec *pspec, gpointer user_data)
638
 
{
639
 
        gboolean idle;
640
 
        g_object_get (client, "idle", &idle, NULL);
641
 
        g_debug ("idle=%i", idle);
642
 
}
643
 
 
644
 
static void
645
 
pk_test_client_update_system_socket_test_cb (GObject *object, GAsyncResult *res, gpointer user_data)
646
 
{
647
 
        PkClient *client = PK_CLIENT (object);
648
 
        GError *error = NULL;
649
 
        PkResults *results = NULL;
650
 
        GPtrArray *messages;
651
 
 
652
 
        /* get the results */
653
 
        results = pk_client_generic_finish (client, res, &error);
654
 
        g_assert_no_error (error);
655
 
        g_assert (results != NULL);
656
 
 
657
 
        /* make sure we handled the ping/pong frontend-socket thing, which is 5 + 1 */
658
 
        messages = pk_results_get_message_array (results);
659
 
        g_assert_cmpint (messages->len, ==, 6);
660
 
        g_ptr_array_unref (messages);
661
 
 
662
 
        g_object_unref (results);
663
 
        _g_test_loop_quit ();
664
 
}
665
 
 
666
 
static void
667
 
pk_test_client_func (void)
668
 
{
669
 
        PkClient *client;
670
 
        gchar **package_ids;
671
 
//      gchar *file;
672
 
        GCancellable *cancellable;
673
 
        gboolean ret;
674
 
        gchar **values;
675
 
        GError *error = NULL;
676
 
        PkProgress *progress;
677
 
        gchar *tid;
678
 
        PkRoleEnum role;
679
 
        PkStatusEnum status;
680
 
//      PkResults *results;
681
 
 
682
 
#if 0
683
 
        /* test user temp */
684
 
        file = pk_client_get_user_temp ("self-test", NULL);
685
 
        g_assert (g_str_has_suffix (file, "/.PackageKit/self-test"));
686
 
        g_assert (g_str_has_prefix (file, "/home/"));
687
 
        g_free (file);
688
 
 
689
 
        /* test native TRUE */
690
 
        ret = pk_client_is_file_native ("/tmp");
691
 
        g_assert (ret);
692
 
 
693
 
        /* test native FALSE */
694
 
        ret = pk_client_is_file_native ("/tmp/.gvfs/moo");
695
 
        g_assert (!ret);
696
 
 
697
 
        /* test resolve NULL */
698
 
        file = pk_client_real_path (NULL);
699
 
        g_assert (file == NULL);
700
 
 
701
 
        /* test resolve /etc/hosts */
702
 
        file = pk_client_real_path ("/etc/hosts");
703
 
        g_assert_cmpstr (file, ==, "/etc/hosts");
704
 
        g_free (file);
705
 
 
706
 
        /* test resolve /etc/../etc/hosts */
707
 
        file = pk_client_real_path ("/etc/../etc/hosts");
708
 
        g_assert_cmpstr (file, ==, "/etc/hosts");
709
 
        g_free (file);
710
 
#endif
711
 
 
712
 
        /* get client */
713
 
        client = pk_client_new ();
714
 
        g_signal_connect (client, "notify::idle",
715
 
                  G_CALLBACK (pk_test_client_notify_idle_cb), NULL);
716
 
        g_assert (client != NULL);
717
 
 
718
 
        /* check idle */
719
 
        g_object_get (client, "idle", &ret, NULL);
720
 
        g_assert (ret);
721
 
 
722
 
        /* resolve package */
723
 
        package_ids = pk_package_ids_from_string ("glib2;2.14.0;i386;fedora&powertop");
724
 
        pk_client_resolve_async (client, pk_bitfield_value (PK_FILTER_ENUM_INSTALLED), package_ids, NULL,
725
 
                 (PkProgressCallback) pk_test_client_progress_cb, NULL,
726
 
                 (GAsyncReadyCallback) pk_test_client_resolve_cb, NULL);
727
 
        g_strfreev (package_ids);
728
 
        _g_test_loop_run_with_timeout (15000);
729
 
        g_debug ("resolved in %f", g_test_timer_elapsed ());
730
 
 
731
 
        /* check idle */
732
 
        g_object_get (client, "idle", &ret, NULL);
733
 
        g_assert (ret);
734
 
 
735
 
        /* get progress of past transaction */
736
 
        progress = pk_client_get_progress (client, _tid, NULL, &error);
737
 
        g_object_get (progress,
738
 
                      "transaction-id", &tid,
739
 
                      "role", &role,
740
 
                      "status", &status,
741
 
                      NULL);
742
 
        g_assert_cmpstr (tid, ==, _tid);
743
 
        g_assert_cmpint (role, ==, PK_ROLE_ENUM_RESOLVE);
744
 
        g_assert_cmpint (status, ==, PK_STATUS_ENUM_FINISHED);
745
 
        g_debug ("got progress in %f", g_test_timer_elapsed ());
746
 
        g_object_unref (progress);
747
 
        g_free (tid);
748
 
        g_free (_tid);
749
 
 
750
 
        /* got updates */
751
 
        g_assert_cmpint (_progress_cb, >, 0);
752
 
        g_assert_cmpint (_status_cb, >, 0);
753
 
 
754
 
        /* reset */
755
 
        _progress_cb = 0;
756
 
        _status_cb = 0;
757
 
//      _package_cb = 0;
758
 
 
759
 
        /* get details about package */
760
 
        package_ids = pk_package_ids_from_id ("powertop;1.8-1.fc8;i386;fedora");
761
 
        pk_client_get_details_async (client, package_ids, NULL,
762
 
                     (PkProgressCallback) pk_test_client_progress_cb, NULL,
763
 
                     (GAsyncReadyCallback) pk_test_client_get_details_cb, NULL);
764
 
        g_strfreev (package_ids);
765
 
        _g_test_loop_run_with_timeout (15000);
766
 
        g_debug ("resolved in %f", g_test_timer_elapsed ());
767
 
 
768
 
        /* got updates */
769
 
        g_assert_cmpint (_progress_cb, >, 0);
770
 
        g_assert_cmpint (_status_cb, >, 0);
771
 
 
772
 
        /* reset */
773
 
        _progress_cb = 0;
774
 
        _status_cb = 0;
775
 
//      _package_cb = 0;
776
 
 
777
 
        /* get updates */
778
 
        pk_client_get_updates_async (client, pk_bitfield_value (PK_FILTER_ENUM_NONE), NULL,
779
 
                     (PkProgressCallback) pk_test_client_progress_cb, NULL,
780
 
                     (GAsyncReadyCallback) pk_test_client_get_updates_cb, NULL);
781
 
        _g_test_loop_run_with_timeout (15000);
782
 
        g_debug ("got updates in %f", g_test_timer_elapsed ());
783
 
 
784
 
        /* it takes more than 50ms to get the progress of the transaction, and if
785
 
         * getting updates from internal cache, then it'll take a shed load less
786
 
         * than this to complete */
787
 
        if (g_test_timer_elapsed () > 100) {
788
 
                /* got status updates */
789
 
                g_assert_cmpint (_status_cb, >, 0);
790
 
        }
791
 
 
792
 
        /* search by name */
793
 
        cancellable = g_cancellable_new ();
794
 
        values = g_strsplit ("power", "&", -1);
795
 
        pk_client_search_names_async (client, pk_bitfield_value (PK_FILTER_ENUM_NONE), values, cancellable,
796
 
                     (PkProgressCallback) pk_test_client_progress_cb, NULL,
797
 
                     (GAsyncReadyCallback) pk_test_client_search_name_cb, NULL);
798
 
        g_timeout_add (1000, (GSourceFunc) pk_test_client_cancel_cb, cancellable);
799
 
        _g_test_loop_run_with_timeout (15000);
800
 
        g_debug ("cancelled in %f", g_test_timer_elapsed ());
801
 
 
802
 
        /* ensure we abort with error if we cancel */
803
 
        pk_client_search_names_async (client, pk_bitfield_value (PK_FILTER_ENUM_NONE), values, cancellable,
804
 
                     (PkProgressCallback) pk_test_client_progress_cb, NULL,
805
 
                     (GAsyncReadyCallback) pk_test_client_search_name_cancellable_cancelled_cb, NULL);
806
 
        _g_test_loop_run_with_timeout (15000);
807
 
 
808
 
        g_strfreev (values);
809
 
 
810
 
        /* okay now */
811
 
        g_cancellable_reset (cancellable);
812
 
 
813
 
        /* do the update-system role to trigger the fake pipe stuff */
814
 
        pk_client_update_system_async (client, TRUE, NULL,
815
 
                                       (PkProgressCallback) pk_test_client_progress_cb, NULL,
816
 
                                       (GAsyncReadyCallback) pk_test_client_update_system_socket_test_cb, NULL);
817
 
        _g_test_loop_run_with_timeout (15000);
818
 
 
819
 
        /* do downloads */
820
 
        package_ids = pk_package_ids_from_id ("powertop;1.8-1.fc8;i386;fedora");
821
 
        pk_client_download_packages_async (client, package_ids, "/tmp", cancellable,
822
 
                   (PkProgressCallback) pk_test_client_progress_cb, NULL,
823
 
                   (GAsyncReadyCallback) pk_test_client_download_cb, NULL);
824
 
        g_strfreev (package_ids);
825
 
        _g_test_loop_run_with_timeout (15000);
826
 
        g_debug ("downloaded and copied in %f", g_test_timer_elapsed ());
827
 
 
828
 
        /* test recursive signal handling */
829
 
#if 0
830
 
        g_signal_connect (client->priv->control, "repo-list-changed", G_CALLBACK (pk_test_client_recursive_signal_cb), NULL);
831
 
        results = pk_client_repo_set_data (client, "dave", "moo", "data", NULL, NULL, NULL, NULL);
832
 
        g_assert (results != NULL);
833
 
        g_object_unref (results);
834
 
#endif
835
 
 
836
 
        g_object_unref (cancellable);
837
 
        g_object_unref (client);
838
 
}
839
 
 
840
 
static void
841
 
pk_test_common_func (void)
842
 
{
843
 
        gchar *present;
844
 
        GDate *date;
845
 
 
846
 
        /************************************************************
847
 
         **************            iso8601           ****************
848
 
         ************************************************************/
849
 
        /* get present iso8601 */
850
 
        present = pk_iso8601_present ();
851
 
        g_assert (present != NULL);
852
 
        g_free (present);
853
 
 
854
 
        /************************************************************
855
 
         **************        Date handling         ****************
856
 
         ************************************************************/
857
 
        /* zero length date */
858
 
        date = pk_iso8601_to_date ("");
859
 
        g_assert (date == NULL);
860
 
 
861
 
        /* no day specified */
862
 
        date = pk_iso8601_to_date ("2004-01");
863
 
        g_assert (date == NULL);
864
 
 
865
 
        /* date _and_ time specified */
866
 
        date = pk_iso8601_to_date ("2009-05-08 13:11:12");
867
 
        g_assert_cmpint (date->day, ==, 8);
868
 
        g_assert_cmpint (date->month, ==, 5);
869
 
        g_assert_cmpint (date->year, ==, 2009);
870
 
        g_date_free (date);
871
 
 
872
 
        /* correct date format */
873
 
        date = pk_iso8601_to_date ("2004-02-01");
874
 
        g_assert_cmpint (date->day, ==, 1);
875
 
        g_assert_cmpint (date->month, ==, 2);
876
 
        g_assert_cmpint (date->year, ==, 2004);
877
 
        g_date_free (date);
878
 
}
879
 
 
880
 
static void
881
 
pk_test_console_func (void)
882
 
{
883
 
        gboolean ret;
884
 
 
885
 
        /* get prompt 1 */
886
 
        ret = pk_console_get_prompt ("press enter", TRUE);
887
 
        g_assert (ret);
888
 
 
889
 
        /* get prompt 2 */
890
 
        ret = pk_console_get_prompt ("press enter", TRUE);
891
 
        g_assert (ret);
892
 
 
893
 
        /* get prompt 3 */
894
 
        ret = pk_console_get_prompt ("press Y", TRUE);
895
 
        g_assert (ret);
896
 
 
897
 
        /* get prompt 3 */
898
 
        ret = pk_console_get_prompt ("press N", TRUE);
899
 
        g_assert (!ret);
900
 
}
901
 
 
902
 
static guint _refcount = 0;
903
 
 
904
 
static void
905
 
pk_test_control_get_tid_cb (GObject *object, GAsyncResult *res, gpointer user_data)
906
 
{
907
 
        PkControl *control = PK_CONTROL (object);
908
 
        GError *error = NULL;
909
 
        gchar *tid;
910
 
 
911
 
        /* get the result */
912
 
        tid = pk_control_get_tid_finish (control, res, &error);
913
 
        g_assert_no_error (error);
914
 
        g_assert (tid != NULL);
915
 
 
916
 
        g_debug ("tid = %s", tid);
917
 
        g_free (tid);
918
 
        if (--_refcount == 0)
919
 
                _g_test_loop_quit ();
920
 
}
921
 
 
922
 
static void
923
 
pk_test_control_get_properties_cb (GObject *object, GAsyncResult *res, gpointer user_data)
924
 
{
925
 
        PkControl *control = PK_CONTROL (object);
926
 
        GError *error = NULL;
927
 
        gboolean ret;
928
 
        PkBitfield roles;
929
 
        PkBitfield filters;
930
 
        PkBitfield groups;
931
 
        gchar *text;
932
 
 
933
 
        /* get the result */
934
 
        ret = pk_control_get_properties_finish (control, res, &error);
935
 
        g_assert_no_error (error);
936
 
        g_assert (ret);
937
 
 
938
 
        /* get values */
939
 
        g_object_get (control,
940
 
                      "mime-types", &text,
941
 
                      "roles", &roles,
942
 
                      "filters", &filters,
943
 
                      "groups", &groups,
944
 
                      NULL);
945
 
 
946
 
        /* check mime_types */
947
 
        g_assert_cmpstr (text, ==, "application/x-rpm;application/x-deb");
948
 
        g_free (text);
949
 
 
950
 
        /* check roles */
951
 
        text = pk_role_bitfield_to_string (roles);
952
 
        g_assert_cmpstr (text, ==, "cancel;get-depends;get-details;get-files;get-packages;get-repo-list;"
953
 
                     "get-requires;get-update-detail;get-updates;install-files;install-packages;install-signature;"
954
 
                     "refresh-cache;remove-packages;repo-enable;repo-set-data;resolve;rollback;"
955
 
                     "search-details;search-file;search-group;search-name;update-packages;update-system;"
956
 
                     "what-provides;download-packages;get-distro-upgrades;simulate-install-packages;"
957
 
                     "simulate-remove-packages;simulate-update-packages");
958
 
        g_free (text);
959
 
 
960
 
        /* check filters */
961
 
        text = pk_filter_bitfield_to_string (filters);
962
 
        g_assert_cmpstr (text, ==, "installed;devel;gui");
963
 
        g_free (text);
964
 
 
965
 
        /* check groups */
966
 
        text = pk_group_bitfield_to_string (groups);
967
 
        g_assert_cmpstr (text, ==, "accessibility;games;system");
968
 
        g_debug ("groups = %s", text);
969
 
 
970
 
        g_free (text);
971
 
 
972
 
        if (--_refcount == 0)
973
 
                _g_test_loop_quit ();
974
 
}
975
 
 
976
 
static void
977
 
pk_test_control_get_time_since_action_cb (GObject *object, GAsyncResult *res, gpointer user_data)
978
 
{
979
 
        PkControl *control = PK_CONTROL (object);
980
 
        GError *error = NULL;
981
 
        guint seconds;
982
 
 
983
 
        /* get the result */
984
 
        seconds = pk_control_get_time_since_action_finish (control, res, &error);
985
 
        g_assert_no_error (error);
986
 
        g_assert_cmpint (seconds, !=, 0);
987
 
 
988
 
        _g_test_loop_quit ();
989
 
}
990
 
 
991
 
static void
992
 
pk_test_control_can_authorize_cb (GObject *object, GAsyncResult *res, gpointer user_data)
993
 
{
994
 
        PkControl *control = PK_CONTROL (object);
995
 
        GError *error = NULL;
996
 
        PkAuthorizeEnum auth;
997
 
 
998
 
        /* get the result */
999
 
        auth = pk_control_can_authorize_finish (control, res, &error);
1000
 
        g_assert_no_error (error);
1001
 
        g_assert_cmpint (auth, !=, PK_AUTHORIZE_ENUM_UNKNOWN);
1002
 
 
1003
 
        _g_test_loop_quit ();
1004
 
}
1005
 
 
1006
 
static void
1007
 
pk_test_control_func (void)
1008
 
{
1009
 
        PkControl *control;
1010
 
        guint version;
1011
 
        GError *error = NULL;
1012
 
        gboolean ret;
1013
 
        gchar *text;
1014
 
        PkBitfield roles;
1015
 
        guint i;
1016
 
        const guint LOOP_SIZE = 5;
1017
 
 
1018
 
        /* get control */
1019
 
        control = pk_control_new ();
1020
 
        g_assert (control != NULL);
1021
 
 
1022
 
        /* get TID async */
1023
 
        _refcount = 1;
1024
 
        pk_control_get_tid_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_tid_cb, NULL);
1025
 
        _g_test_loop_run_with_timeout (5000);
1026
 
        g_debug ("got tid in %f", g_test_timer_elapsed ());
1027
 
 
1028
 
        /* get multiple TIDs async */
1029
 
        _refcount = LOOP_SIZE;
1030
 
        for (i=0; i<_refcount; i++) {
1031
 
                g_debug ("getting #%i", i+1);
1032
 
                pk_control_get_tid_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_tid_cb, NULL);
1033
 
        }
1034
 
        _g_test_loop_run_with_timeout (5000);
1035
 
        g_debug ("got %i tids in %f", LOOP_SIZE, g_test_timer_elapsed ());
1036
 
 
1037
 
        /* get properties async */
1038
 
        _refcount = 1;
1039
 
        pk_control_get_properties_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_properties_cb, NULL);
1040
 
        _g_test_loop_run_with_timeout (5000);
1041
 
        g_debug ("got properties types in %f", g_test_timer_elapsed ());
1042
 
 
1043
 
        /* get properties async (again, to test caching) */
1044
 
        _refcount = 1;
1045
 
        pk_control_get_properties_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_properties_cb, NULL);
1046
 
        _g_test_loop_run_with_timeout (5000);
1047
 
        g_debug ("got properties in %f", g_test_timer_elapsed ());
1048
 
 
1049
 
        /* do multiple requests async */
1050
 
        _refcount = LOOP_SIZE * 4;
1051
 
        for (i=0; i<_refcount; i++) {
1052
 
                g_debug ("getting #%i", i+1);
1053
 
                pk_control_get_tid_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_tid_cb, NULL);
1054
 
                pk_control_get_properties_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_properties_cb, NULL);
1055
 
                pk_control_get_tid_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_tid_cb, NULL);
1056
 
                pk_control_get_properties_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_properties_cb, NULL);
1057
 
        }
1058
 
        _g_test_loop_run_with_timeout (5000);
1059
 
        g_debug ("got %i 2*properties and 2*tids in %f", LOOP_SIZE, g_test_timer_elapsed ());
1060
 
 
1061
 
        /* get time since async */
1062
 
        pk_control_get_time_since_action_async (control, PK_ROLE_ENUM_GET_UPDATES, NULL, (GAsyncReadyCallback) pk_test_control_get_time_since_action_cb, NULL);
1063
 
        _g_test_loop_run_with_timeout (5000);
1064
 
        g_debug ("got get time since in %f", g_test_timer_elapsed ());
1065
 
 
1066
 
        /* get auth state async */
1067
 
        pk_control_can_authorize_async (control, "org.freedesktop.packagekit.system-update", NULL,
1068
 
                (GAsyncReadyCallback) pk_test_control_can_authorize_cb, NULL);
1069
 
        _g_test_loop_run_with_timeout (5000);
1070
 
        g_debug ("get auth state in %f", g_test_timer_elapsed ());
1071
 
 
1072
 
        /* version major */
1073
 
        g_object_get (control, "version-major", &version, NULL);
1074
 
        g_assert_cmpint (version, ==, PK_MAJOR_VERSION);
1075
 
 
1076
 
        /* version minor */
1077
 
        g_object_get (control, "version-minor", &version, NULL);
1078
 
        g_assert_cmpint (version, ==, PK_MINOR_VERSION);
1079
 
 
1080
 
        /* version micro */
1081
 
        g_object_get (control, "version-micro", &version, NULL);
1082
 
        g_assert_cmpint (version, ==, PK_MICRO_VERSION);
1083
 
 
1084
 
        /* get properties sync */
1085
 
        ret = pk_control_get_properties (control, NULL, &error);
1086
 
        g_assert_no_error (error);
1087
 
        g_assert (ret);
1088
 
 
1089
 
        /* get data */
1090
 
        g_object_get (control,
1091
 
                      "roles", &roles,
1092
 
                      NULL);
1093
 
 
1094
 
        /* check data */
1095
 
        text = pk_role_bitfield_to_string (roles);
1096
 
        g_assert_cmpstr (text, ==, "cancel;get-depends;get-details;get-files;get-packages;get-repo-list;"
1097
 
                     "get-requires;get-update-detail;get-updates;install-files;install-packages;install-signature;"
1098
 
                     "refresh-cache;remove-packages;repo-enable;repo-set-data;resolve;rollback;"
1099
 
                     "search-details;search-file;search-group;search-name;update-packages;update-system;"
1100
 
                     "what-provides;download-packages;get-distro-upgrades;simulate-install-packages;"
1101
 
                     "simulate-remove-packages;simulate-update-packages");
1102
 
        g_free (text);
1103
 
 
1104
 
        g_object_unref (control);
1105
 
}
1106
 
 
1107
 
static void
1108
 
pk_test_desktop_func (void)
1109
 
{
1110
 
        PkDesktop *desktop;
1111
 
        gboolean ret;
1112
 
        gchar *package;
1113
 
        GPtrArray *array;
1114
 
        GError *error = NULL;
1115
 
 
1116
 
        desktop = pk_desktop_new ();
1117
 
        g_assert (desktop != NULL);
1118
 
 
1119
 
        /* get package when not valid */
1120
 
        package = pk_desktop_get_package_for_file (desktop, "/usr/share/applications/gpk-update-viewer.desktop", NULL);
1121
 
        g_assert (package == NULL);
1122
 
 
1123
 
        /* file does not exist */
1124
 
        ret = g_file_test (PK_DESKTOP_DEFAULT_DATABASE, G_FILE_TEST_EXISTS);
1125
 
        if (!ret) {
1126
 
                g_warning ("skipping checks as database does not exist");
1127
 
                goto out;
1128
 
        }
1129
 
 
1130
 
        /* open database */
1131
 
        ret = pk_desktop_open_database (desktop, &error);
1132
 
        g_assert_no_error (error);
1133
 
        g_assert (ret);
1134
 
 
1135
 
        /* get package */
1136
 
        package = pk_desktop_get_package_for_file (desktop, "/usr/share/applications/gpk-update-viewer.desktop", NULL);
1137
 
 
1138
 
        /* dummy, not yum */
1139
 
        if (g_strcmp0 (package, "vips-doc") == 0); {
1140
 
                g_debug ("created db with dummy, skipping remaining tests");
1141
 
                goto out;
1142
 
        }
1143
 
        g_assert_cmpstr (package, ==, "gnome-packagekit");
1144
 
        g_free (package);
1145
 
 
1146
 
        /* get files */
1147
 
        array = pk_desktop_get_files_for_package (desktop, "gnome-packagekit", NULL);
1148
 
        g_assert (array != NULL);
1149
 
        g_assert_cmpint (array->len, >=, 5);
1150
 
        g_ptr_array_unref (array);
1151
 
 
1152
 
        /* get shown files */
1153
 
        array = pk_desktop_get_shown_for_package (desktop, "gnome-packagekit", NULL);
1154
 
        g_assert (array != NULL);
1155
 
        g_assert_cmpint (array->len, >=, 3);
1156
 
        g_ptr_array_unref (array);
1157
 
out:
1158
 
        g_object_unref (desktop);
1159
 
}
1160
 
 
1161
 
static void
1162
 
pk_test_enum_func (void)
1163
 
{
1164
 
        const gchar *string;
1165
 
        PkRoleEnum role_value;
1166
 
        guint i;
1167
 
 
1168
 
        /* find value */
1169
 
        role_value = pk_role_enum_from_string ("search-file");
1170
 
        g_assert_cmpint (role_value, ==, PK_ROLE_ENUM_SEARCH_FILE);
1171
 
 
1172
 
        /* find string */
1173
 
        string = pk_role_enum_to_string (PK_ROLE_ENUM_SEARCH_FILE);
1174
 
        g_assert_cmpstr (string, ==, "search-file");
1175
 
 
1176
 
        /* check we convert all the role bitfield */
1177
 
        for (i=1; i<PK_ROLE_ENUM_LAST; i++) {
1178
 
                string = pk_role_enum_to_string (i);
1179
 
                if (string == NULL) {
1180
 
                        /* so we get the value of i in the assert text */
1181
 
                        g_assert_cmpint (0, ==, i);
1182
 
                        break;
1183
 
                }
1184
 
        }
1185
 
 
1186
 
        /* check we convert all the status bitfield */
1187
 
        for (i=1; i<PK_STATUS_ENUM_LAST; i++) {
1188
 
                string = pk_status_enum_to_string (i);
1189
 
                if (string == NULL) {
1190
 
                        /* so we get the value of i in the assert text */
1191
 
                        g_assert_cmpint (0, ==, i);
1192
 
                        break;
1193
 
                }
1194
 
        }
1195
 
 
1196
 
        /* check we convert all the exit bitfield */
1197
 
        for (i=0; i<PK_EXIT_ENUM_LAST; i++) {
1198
 
                string = pk_exit_enum_to_string (i);
1199
 
                if (string == NULL) {
1200
 
                        /* so we get the value of i in the assert text */
1201
 
                        g_assert_cmpint (0, ==, i);
1202
 
                        break;
1203
 
                }
1204
 
        }
1205
 
 
1206
 
        /* check we convert all the filter bitfield */
1207
 
        for (i=0; i<PK_FILTER_ENUM_LAST; i++) {
1208
 
                string = pk_filter_enum_to_string (i);
1209
 
                if (string == NULL) {
1210
 
                        /* so we get the value of i in the assert text */
1211
 
                        g_assert_cmpint (0, ==, i);
1212
 
                        break;
1213
 
                }
1214
 
        }
1215
 
 
1216
 
        /* check we convert all the restart bitfield */
1217
 
        for (i=0; i<PK_RESTART_ENUM_LAST; i++) {
1218
 
                string = pk_restart_enum_to_string (i);
1219
 
                if (string == NULL) {
1220
 
                        /* so we get the value of i in the assert text */
1221
 
                        g_assert_cmpint (0, ==, i);
1222
 
                        break;
1223
 
                }
1224
 
        }
1225
 
 
1226
 
        /* check we convert all the error_code bitfield */
1227
 
        for (i=0; i<PK_ERROR_ENUM_LAST; i++) {
1228
 
                string = pk_error_enum_to_string (i);
1229
 
                if (string == NULL) {
1230
 
                        /* so we get the value of i in the assert text */
1231
 
                        g_assert_cmpint (0, ==, i);
1232
 
                        break;
1233
 
                }
1234
 
        }
1235
 
 
1236
 
        /* check we convert all the group bitfield */
1237
 
        for (i=1; i<PK_GROUP_ENUM_LAST; i++) {
1238
 
                string = pk_group_enum_to_string (i);
1239
 
                if (string == NULL) {
1240
 
                        /* so we get the value of i in the assert text */
1241
 
                        g_assert_cmpint (0, ==, i);
1242
 
                        break;
1243
 
                }
1244
 
        }
1245
 
 
1246
 
        /* check we convert all the info bitfield */
1247
 
        for (i=1; i<PK_INFO_ENUM_LAST; i++) {
1248
 
                string = pk_info_enum_to_string (i);
1249
 
                if (string == NULL) {
1250
 
                        /* so we get the value of i in the assert text */
1251
 
                        g_assert_cmpint (0, ==, i);
1252
 
                        break;
1253
 
                }
1254
 
        }
1255
 
 
1256
 
        /* check we convert all the sig_type bitfield */
1257
 
        for (i=0; i<PK_SIGTYPE_ENUM_LAST; i++) {
1258
 
                string = pk_sig_type_enum_to_string (i);
1259
 
                if (string == NULL) {
1260
 
                        /* so we get the value of i in the assert text */
1261
 
                        g_assert_cmpint (0, ==, i);
1262
 
                        break;
1263
 
                }
1264
 
        }
1265
 
 
1266
 
        /* check we convert all the upgrade bitfield */
1267
 
        for (i=0; i<PK_DISTRO_UPGRADE_ENUM_LAST; i++) {
1268
 
                string = pk_distro_upgrade_enum_to_string (i);
1269
 
                if (string == NULL) {
1270
 
                        /* so we get the value of i in the assert text */
1271
 
                        g_assert_cmpint (0, ==, i);
1272
 
                        break;
1273
 
                }
1274
 
        }
1275
 
 
1276
 
        /* check we convert all the license bitfield */
1277
 
        for (i=0; i<PK_LICENSE_ENUM_LAST; i++) {
1278
 
                string = pk_license_enum_to_string (i);
1279
 
                if (string == NULL) {
1280
 
                        /* so we get the value of i in the assert text */
1281
 
                        g_assert_cmpint (0, ==, i);
1282
 
                        break;
1283
 
                }
1284
 
        }
1285
 
 
1286
 
        /* check we convert all the media type bitfield */
1287
 
        for (i=0; i<PK_MEDIA_TYPE_ENUM_LAST; i++) {
1288
 
                string = pk_media_type_enum_to_string (i);
1289
 
                if (string == NULL) {
1290
 
                        /* so we get the value of i in the assert text */
1291
 
                        g_assert_cmpint (0, ==, i);
1292
 
                        break;
1293
 
                }
1294
 
        }
1295
 
}
1296
 
 
1297
 
static void
1298
 
pk_test_package_id_func (void)
1299
 
{
1300
 
        gboolean ret;
1301
 
        gchar *text;
1302
 
        gchar **sections;
1303
 
 
1304
 
        /* check not valid - NULL */
1305
 
        ret = pk_package_id_check (NULL);
1306
 
        g_assert (!ret);
1307
 
 
1308
 
        /* check not valid - no name */
1309
 
        ret = pk_package_id_check (";0.0.1;i386;fedora");
1310
 
        g_assert (!ret);
1311
 
 
1312
 
        /* check not valid - invalid */
1313
 
        ret = pk_package_id_check ("moo;0.0.1;i386");
1314
 
        g_assert (!ret);
1315
 
 
1316
 
        /* check valid */
1317
 
        ret = pk_package_id_check ("moo;0.0.1;i386;fedora");
1318
 
        g_assert (ret);
1319
 
 
1320
 
        /* id build */
1321
 
        text = pk_package_id_build ("moo", "0.0.1", "i386", "fedora");
1322
 
        g_assert_cmpstr (text, ==, "moo;0.0.1;i386;fedora");
1323
 
        g_free (text);
1324
 
 
1325
 
        /* id build partial */
1326
 
        text = pk_package_id_build ("moo", NULL, NULL, NULL);
1327
 
        g_assert_cmpstr (text, ==, "moo;;;");
1328
 
        g_free (text);
1329
 
 
1330
 
        /* test printable */
1331
 
        text = pk_package_id_to_printable ("moo;0.0.1;i386;fedora");
1332
 
        g_assert_cmpstr (text, ==, "moo-0.0.1.i386");
1333
 
        g_free (text);
1334
 
 
1335
 
        /* test printable no arch */
1336
 
        text = pk_package_id_to_printable ("moo;0.0.1;;");
1337
 
        g_assert_cmpstr (text, ==, "moo-0.0.1");
1338
 
        g_free (text);
1339
 
 
1340
 
        /* test printable just name */
1341
 
        text = pk_package_id_to_printable ("moo;;;");
1342
 
        g_assert_cmpstr (text, ==, "moo");
1343
 
        g_free (text);
1344
 
 
1345
 
        /* test on real packageid */
1346
 
        sections = pk_package_id_split ("kde-i18n-csb;4:3.5.8~pre20071001-0ubuntu1;all;");
1347
 
        g_assert (sections != NULL);
1348
 
        g_assert_cmpstr (sections[0], ==, "kde-i18n-csb");
1349
 
        g_assert_cmpstr (sections[1], ==, "4:3.5.8~pre20071001-0ubuntu1");
1350
 
        g_assert_cmpstr (sections[2], ==, "all");
1351
 
        g_assert_cmpstr (sections[3], ==, "");
1352
 
        g_strfreev (sections);
1353
 
 
1354
 
        /* test on short packageid */
1355
 
        sections = pk_package_id_split ("kde-i18n-csb;4:3.5.8~pre20071001-0ubuntu1;;");
1356
 
        g_assert (sections != NULL);
1357
 
        g_assert_cmpstr (sections[0], ==, "kde-i18n-csb");
1358
 
        g_assert_cmpstr (sections[1], ==, "4:3.5.8~pre20071001-0ubuntu1");
1359
 
        g_assert_cmpstr (sections[2], ==, "");
1360
 
        g_assert_cmpstr (sections[3], ==, "");
1361
 
        g_strfreev (sections);
1362
 
 
1363
 
        /* test fail under */
1364
 
        sections = pk_package_id_split ("foo;moo");
1365
 
        g_assert (sections == NULL);
1366
 
 
1367
 
        /* test fail over */
1368
 
        sections = pk_package_id_split ("foo;moo;dave;clive;dan");
1369
 
        g_assert (sections == NULL);
1370
 
 
1371
 
        /* test fail missing first */
1372
 
        sections = pk_package_id_split (";0.1.2;i386;data");
1373
 
        g_assert (sections == NULL);
1374
 
}
1375
 
 
1376
 
static void
1377
 
pk_test_package_ids_func (void)
1378
 
{
1379
 
        gboolean ret;
1380
 
        gchar *package_ids_blank[] = {};
1381
 
        gchar **package_ids;
1382
 
 
1383
 
        /* parse va_list */
1384
 
        package_ids = pk_package_ids_from_string ("foo;0.0.1;i386;fedora&bar;0.1.1;noarch;livna");
1385
 
        g_assert (package_ids != NULL);
1386
 
 
1387
 
        /* verify size */
1388
 
        g_assert_cmpint (g_strv_length (package_ids), ==, 2);
1389
 
 
1390
 
        /* verify blank */
1391
 
        ret = pk_package_ids_check (package_ids_blank);
1392
 
        g_assert (!ret);
1393
 
 
1394
 
        /* verify */
1395
 
        ret = pk_package_ids_check (package_ids);
1396
 
        g_assert (ret);
1397
 
 
1398
 
        g_strfreev (package_ids);
1399
 
}
1400
 
 
1401
 
static void
1402
 
pk_test_package_sack_resolve_cb (GObject *object, GAsyncResult *res, gpointer user_data)
1403
 
{
1404
 
        PkPackageSack *sack = PK_PACKAGE_SACK (object);
1405
 
        GError *error = NULL;
1406
 
        gboolean ret;
1407
 
 
1408
 
        /* get the result */
1409
 
        ret = pk_package_sack_merge_generic_finish (sack, res, &error);
1410
 
        g_assert_no_error (error);
1411
 
        g_assert (ret);
1412
 
 
1413
 
        _g_test_loop_quit ();
1414
 
}
1415
 
 
1416
 
static void
1417
 
pk_test_package_sack_details_cb (GObject *object, GAsyncResult *res, gpointer user_data)
1418
 
{
1419
 
        PkPackageSack *sack = PK_PACKAGE_SACK (object);
1420
 
        GError *error = NULL;
1421
 
        gboolean ret;
1422
 
 
1423
 
        /* get the result */
1424
 
        ret = pk_package_sack_merge_generic_finish (sack, res, &error);
1425
 
        g_assert_no_error (error);
1426
 
        g_assert (ret);
1427
 
 
1428
 
        _g_test_loop_quit ();
1429
 
}
1430
 
 
1431
 
static void
1432
 
pk_test_package_sack_update_detail_cb (GObject *object, GAsyncResult *res, gpointer user_data)
1433
 
{
1434
 
        PkPackageSack *sack = PK_PACKAGE_SACK (object);
1435
 
        GError *error = NULL;
1436
 
        gboolean ret;
1437
 
 
1438
 
        /* get the result */
1439
 
        ret = pk_package_sack_merge_generic_finish (sack, res, &error);
1440
 
        g_assert_no_error (error);
1441
 
        g_assert (ret);
1442
 
 
1443
 
        _g_test_loop_quit ();
1444
 
}
1445
 
 
1446
 
/**
1447
 
 * pk_test_package_sack_filter_cb:
1448
 
 **/
1449
 
static gboolean
1450
 
pk_test_package_sack_filter_cb (PkPackage *package, gpointer user_data)
1451
 
{
1452
 
        if (pk_package_get_info (package) == PK_INFO_ENUM_UNKNOWN)
1453
 
                return FALSE;
1454
 
        return TRUE;
1455
 
}
1456
 
 
1457
 
static void
1458
 
pk_test_package_sack_func (void)
1459
 
{
1460
 
        gboolean ret;
1461
 
        PkPackageSack *sack;
1462
 
        PkPackage *package;
1463
 
        gchar *text;
1464
 
        guint size;
1465
 
        PkInfoEnum info = PK_INFO_ENUM_UNKNOWN;
1466
 
        guint64 bytes;
1467
 
 
1468
 
        sack = pk_package_sack_new ();
1469
 
        g_assert (sack != NULL);
1470
 
 
1471
 
        /* get size of unused package sack */
1472
 
        size = pk_package_sack_get_size (sack);
1473
 
        g_assert (size == 0);
1474
 
 
1475
 
        /* remove package not present */
1476
 
        ret = pk_package_sack_remove_package_by_id (sack, "powertop;1.8-1.fc8;i386;fedora");
1477
 
        g_assert (!ret);
1478
 
 
1479
 
        /* find package not present */
1480
 
        package = pk_package_sack_find_by_id (sack, "powertop;1.8-1.fc8;i386;fedora");
1481
 
        g_assert (package == NULL);
1482
 
 
1483
 
        /* add package */
1484
 
        ret = pk_package_sack_add_package_by_id (sack, "powertop;1.8-1.fc8;i386;fedora", NULL);
1485
 
        g_assert (ret);
1486
 
 
1487
 
        /* get size of package sack */
1488
 
        size = pk_package_sack_get_size (sack);
1489
 
        g_assert (size == 1);
1490
 
 
1491
 
        /* merge resolve results */
1492
 
        pk_package_sack_resolve_async (sack, NULL, NULL, NULL, (GAsyncReadyCallback) pk_test_package_sack_resolve_cb, NULL);
1493
 
        _g_test_loop_run_with_timeout (5000);
1494
 
        g_debug ("resolved in %f", g_test_timer_elapsed ());
1495
 
 
1496
 
        /* find package which is present */
1497
 
        package = pk_package_sack_find_by_id (sack, "powertop;1.8-1.fc8;i386;fedora");
1498
 
        g_assert (package != NULL);
1499
 
 
1500
 
        /* check new summary */
1501
 
        g_object_get (package,
1502
 
                      "info", &info,
1503
 
                      "summary", &text,
1504
 
                      NULL);
1505
 
        g_assert_cmpstr (text, ==, "Power consumption monitor");
1506
 
 
1507
 
        /* check new info */
1508
 
        g_assert_cmpint (info, ==, PK_INFO_ENUM_INSTALLED);
1509
 
 
1510
 
        g_free (text);
1511
 
        g_object_unref (package);
1512
 
 
1513
 
        /* merge details results */
1514
 
        pk_package_sack_get_details_async (sack, NULL, NULL, NULL, (GAsyncReadyCallback) pk_test_package_sack_details_cb, NULL);
1515
 
        _g_test_loop_run_with_timeout (5000);
1516
 
        g_debug ("got details in %f", g_test_timer_elapsed ());
1517
 
 
1518
 
        /* find package which is present */
1519
 
        package = pk_package_sack_find_by_id (sack, "powertop;1.8-1.fc8;i386;fedora");
1520
 
        g_assert (package != NULL);
1521
 
 
1522
 
        /* check new url */
1523
 
        g_object_get (package,
1524
 
                      "url", &text,
1525
 
                      NULL);
1526
 
        g_assert_cmpstr (text, ==, "http://live.gnome.org/powertop");
1527
 
        g_object_unref (package);
1528
 
        g_free (text);
1529
 
 
1530
 
        /* merge update detail results */
1531
 
        pk_package_sack_get_update_detail_async (sack, NULL, NULL, NULL, (GAsyncReadyCallback) pk_test_package_sack_update_detail_cb, NULL);
1532
 
        _g_test_loop_run_with_timeout (5000);
1533
 
        g_debug ("got update detail in %f", g_test_timer_elapsed ());
1534
 
 
1535
 
        /* find package which is present */
1536
 
        package = pk_package_sack_find_by_id (sack, "powertop;1.8-1.fc8;i386;fedora");
1537
 
        g_assert (package != NULL);
1538
 
 
1539
 
        /* check new vendor url */
1540
 
        g_object_get (package,
1541
 
                      "update-vendor-url", &text,
1542
 
                      NULL);
1543
 
        g_assert_cmpstr (text, ==, "http://www.distro-update.org/page?moo;Bugfix release for powertop");
1544
 
 
1545
 
        g_free (text);
1546
 
        g_object_unref (package);
1547
 
 
1548
 
        /* chck size in bytes */
1549
 
        bytes = pk_package_sack_get_total_bytes (sack);
1550
 
        g_assert_cmpint (bytes, ==, 103424);
1551
 
 
1552
 
        /* remove package */
1553
 
        ret = pk_package_sack_remove_package_by_id (sack, "powertop;1.8-1.fc8;i386;fedora");
1554
 
        g_assert (ret);
1555
 
 
1556
 
        /* get size of package sack */
1557
 
        size = pk_package_sack_get_size (sack);
1558
 
        g_assert_cmpint (size, ==, 0);
1559
 
 
1560
 
        /* remove already removed package */
1561
 
        ret = pk_package_sack_remove_package_by_id (sack, "powertop;1.8-1.fc8;i386;fedora");
1562
 
        g_assert (!ret);
1563
 
 
1564
 
        /* remove by filter */
1565
 
        pk_package_sack_add_package_by_id (sack, "powertop;1.8-1.fc8;i386;fedora", NULL);
1566
 
        pk_package_sack_add_package_by_id (sack, "powertop-debuginfo;1.8-1.fc8;i386;fedora", NULL);
1567
 
        ret = pk_package_sack_remove_by_filter (sack, pk_test_package_sack_filter_cb, NULL);
1568
 
        g_assert (ret);
1569
 
 
1570
 
        /* check all removed */
1571
 
        size = pk_package_sack_get_size (sack);
1572
 
        g_assert_cmpint (size, ==, 0);
1573
 
 
1574
 
        g_object_unref (sack);
1575
 
}
1576
 
 
1577
 
static void
1578
 
pk_test_progress_func (void)
1579
 
{
1580
 
        PkProgress *progress;
1581
 
 
1582
 
        progress = pk_progress_new ();
1583
 
        g_assert (progress != NULL);
1584
 
 
1585
 
        g_object_unref (progress);
1586
 
}
1587
 
 
1588
 
static void
1589
 
pk_test_progress_bar (void)
1590
 
{
1591
 
        PkProgressBar *progress_bar;
1592
 
 
1593
 
        progress_bar = pk_progress_bar_new ();
1594
 
        g_assert (progress_bar != NULL);
1595
 
 
1596
 
        g_object_unref (progress_bar);
1597
 
}
1598
 
 
1599
 
static void
1600
 
pk_test_results_func (void)
1601
 
{
1602
 
        gboolean ret;
1603
 
        PkResults *results;
1604
 
        PkExitEnum exit_enum;
1605
 
        GPtrArray *packages;
1606
 
        PkPackage *item;
1607
 
        PkInfoEnum info;
1608
 
        gchar *package_id;
1609
 
        gchar *summary;
1610
 
 
1611
 
        /* get results */
1612
 
        results = pk_results_new ();
1613
 
        g_assert (results != NULL);
1614
 
 
1615
 
        /* get exit code of unset results */
1616
 
        exit_enum = pk_results_get_exit_code (results);
1617
 
        g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_UNKNOWN);
1618
 
 
1619
 
        /* get package list of unset results */
1620
 
        packages = pk_results_get_package_array (results);
1621
 
        g_assert_cmpint (packages->len, ==, 0);
1622
 
        g_ptr_array_unref (packages);
1623
 
 
1624
 
        /* set valid exit code */
1625
 
        ret = pk_results_set_exit_code (results, PK_EXIT_ENUM_CANCELLED);
1626
 
        g_assert (ret);
1627
 
 
1628
 
        /* get exit code of set results */
1629
 
        exit_enum = pk_results_get_exit_code (results);
1630
 
        g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_CANCELLED);
1631
 
 
1632
 
        /* add package */
1633
 
        item = pk_package_new ();
1634
 
        g_object_set (item,
1635
 
                      "info", PK_INFO_ENUM_AVAILABLE,
1636
 
                      "package-id", "gnome-power-manager;0.1.2;i386;fedora",
1637
 
                      "summary", "Power manager for GNOME",
1638
 
                      NULL);
1639
 
        ret = pk_results_add_package (results, item);
1640
 
        g_object_unref (item);
1641
 
        g_assert (ret);
1642
 
 
1643
 
        /* get package list of set results */
1644
 
        packages = pk_results_get_package_array (results);
1645
 
        g_assert_cmpint (packages->len, ==, 1);
1646
 
 
1647
 
        /* check data */
1648
 
        item = g_ptr_array_index (packages, 0);
1649
 
        g_object_get (item,
1650
 
                      "info", &info,
1651
 
                      "package-id", &package_id,
1652
 
                      "summary", &summary,
1653
 
                      NULL);
1654
 
        g_assert_cmpint (info, ==, PK_INFO_ENUM_AVAILABLE);
1655
 
        g_assert_cmpstr ("gnome-power-manager;0.1.2;i386;fedora", ==, package_id);
1656
 
        g_assert_cmpstr ("Power manager for GNOME", ==, summary);
1657
 
        g_object_ref (item);
1658
 
        g_ptr_array_unref (packages);
1659
 
        g_free (package_id);
1660
 
        g_free (summary);
1661
 
 
1662
 
        /* check ref */
1663
 
        g_object_get (item,
1664
 
                      "info", &info,
1665
 
                      "package-id", &package_id,
1666
 
                      "summary", &summary,
1667
 
                      NULL);
1668
 
        g_assert_cmpint (info, ==, PK_INFO_ENUM_AVAILABLE);
1669
 
        g_assert_cmpstr ("gnome-power-manager;0.1.2;i386;fedora", ==, package_id);
1670
 
        g_assert_cmpstr ("Power manager for GNOME", ==, summary);
1671
 
        g_object_unref (item);
1672
 
        g_free (package_id);
1673
 
        g_free (summary);
1674
 
 
1675
 
        g_object_unref (results);
1676
 
}
1677
 
 
1678
 
static void
1679
 
pk_test_service_pack_create_cb (GObject *object, GAsyncResult *res, gpointer user_data)
1680
 
{
1681
 
        PkServicePack *pack = PK_SERVICE_PACK (object);
1682
 
        GError *error = NULL;
1683
 
        gboolean ret;
1684
 
 
1685
 
        /* get the results */
1686
 
        ret = pk_service_pack_generic_finish (pack, res, &error);
1687
 
        g_assert_no_error (error);
1688
 
        g_assert (ret);
1689
 
 
1690
 
        _g_test_loop_quit ();
1691
 
}
1692
 
 
1693
 
static void
1694
 
pk_test_service_pack_progress_cb (PkProgress *progress, PkProgressType type, gpointer user_data)
1695
 
{
1696
 
        PkStatusEnum status;
1697
 
        if (type == PK_PROGRESS_TYPE_STATUS) {
1698
 
                g_object_get (progress,
1699
 
                      "status", &status,
1700
 
                      NULL);
1701
 
                g_debug ("now %s", pk_status_enum_to_string (status));
1702
 
        }
1703
 
}
1704
 
 
1705
 
static void
1706
 
pk_test_service_pack_func (void)
1707
 
{
1708
 
        PkServicePack *pack;
1709
 
        gchar **package_ids;
1710
 
 
1711
 
        pack = pk_service_pack_new ();
1712
 
        g_assert (pack != NULL);
1713
 
 
1714
 
        /* install package */
1715
 
        package_ids = pk_package_ids_from_id ("glib2;2.14.0;i386;fedora");
1716
 
        pk_service_pack_create_for_package_ids_async (pack, "dave.servicepack", package_ids, NULL, NULL,
1717
 
                        (PkProgressCallback) pk_test_service_pack_progress_cb, NULL,
1718
 
                        (GAsyncReadyCallback) pk_test_service_pack_create_cb, NULL);
1719
 
        g_strfreev (package_ids);
1720
 
        _g_test_loop_run_with_timeout (150000);
1721
 
        g_debug ("installed in %f", g_test_timer_elapsed ());
1722
 
 
1723
 
        g_object_unref (pack);
1724
 
}
1725
 
 
1726
 
static void
1727
 
pk_test_task_install_packages_cb (GObject *object, GAsyncResult *res, gpointer user_data)
1728
 
{
1729
 
        PkTask *task = PK_TASK (object);
1730
 
        GError *error = NULL;
1731
 
        PkResults *results;
1732
 
 
1733
 
        /* get the results */
1734
 
        results = pk_task_generic_finish (task, res, &error);
1735
 
        g_assert (results == NULL);
1736
 
        g_assert_cmpstr (error->message, ==, "could not do untrusted question as no klass support");
1737
 
 
1738
 
        g_error_free (error);
1739
 
        if (results != NULL)
1740
 
                g_object_unref (results);
1741
 
        _g_test_loop_quit ();
1742
 
}
1743
 
 
1744
 
static void
1745
 
pk_test_task_progress_cb (PkProgress *progress, PkProgressType type, gpointer user_data)
1746
 
{
1747
 
        PkStatusEnum status;
1748
 
        if (type == PK_PROGRESS_TYPE_STATUS) {
1749
 
                g_object_get (progress,
1750
 
                      "status", &status,
1751
 
                      NULL);
1752
 
                g_debug ("now %s", pk_status_enum_to_string (status));
1753
 
        }
1754
 
}
1755
 
 
1756
 
static void
1757
 
pk_test_task_func (void)
1758
 
{
1759
 
        PkTask *task;
1760
 
        gchar **package_ids;
1761
 
 
1762
 
        task = pk_task_new ();
1763
 
        g_assert (task != NULL);
1764
 
 
1765
 
        /* install package */
1766
 
        package_ids = pk_package_ids_from_id ("glib2;2.14.0;i386;fedora");
1767
 
        pk_task_install_packages_async (task, package_ids, NULL,
1768
 
                        (PkProgressCallback) pk_test_task_progress_cb, NULL,
1769
 
                        (GAsyncReadyCallback) pk_test_task_install_packages_cb, NULL);
1770
 
        g_strfreev (package_ids);
1771
 
        _g_test_loop_run_with_timeout (150000);
1772
 
        g_debug ("installed in %f", g_test_timer_elapsed ());
1773
 
 
1774
 
        g_object_unref (task);
1775
 
}
1776
 
 
1777
 
static void
1778
 
pk_test_task_text_install_packages_cb (GObject *object, GAsyncResult *res, gpointer user_data)
1779
 
{
1780
 
        PkTaskText *task = PK_TASK_TEXT (object);
1781
 
        GError *error = NULL;
1782
 
        PkResults *results;
1783
 
        PkExitEnum exit_enum;
1784
 
        GPtrArray *packages;
1785
 
 
1786
 
        /* get the results */
1787
 
        results = pk_task_generic_finish (PK_TASK (task), res, &error);
1788
 
        g_assert_no_error (error);
1789
 
        g_assert (results != NULL);
1790
 
 
1791
 
        exit_enum = pk_results_get_exit_code (results);
1792
 
        g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_SUCCESS);
1793
 
 
1794
 
        packages = pk_results_get_package_array (results);
1795
 
        g_assert (packages != NULL);
1796
 
        g_assert_cmpint (packages->len, ==, 4);
1797
 
 
1798
 
        g_ptr_array_unref (packages);
1799
 
 
1800
 
        g_debug ("results exit enum = %s", pk_exit_enum_to_string (exit_enum));
1801
 
 
1802
 
        if (results != NULL)
1803
 
                g_object_unref (results);
1804
 
        _g_test_loop_quit ();
1805
 
}
1806
 
 
1807
 
static void
1808
 
pk_test_task_text_progress_cb (PkProgress *progress, PkProgressType type, gpointer user_data)
1809
 
{
1810
 
        PkStatusEnum status;
1811
 
        if (type == PK_PROGRESS_TYPE_STATUS) {
1812
 
                g_object_get (progress,
1813
 
                      "status", &status,
1814
 
                      NULL);
1815
 
                g_debug ("now %s", pk_status_enum_to_string (status));
1816
 
        }
1817
 
}
1818
 
 
1819
 
static void
1820
 
pk_test_task_text_func (void)
1821
 
{
1822
 
        PkTaskText *task;
1823
 
        gchar **package_ids;
1824
 
 
1825
 
        task = pk_task_text_new ();
1826
 
        g_assert (task != NULL);
1827
 
 
1828
 
        /* For testing, you will need to manually do:
1829
 
        pkcon repo-set-data dummy use-gpg 1
1830
 
        pkcon repo-set-data dummy use-eula 1
1831
 
        pkcon repo-set-data dummy use-media 1
1832
 
        */
1833
 
 
1834
 
        /* install package */
1835
 
        package_ids = pk_package_ids_from_id ("vips-doc;7.12.4-2.fc8;noarch;linva");
1836
 
        pk_task_install_packages_async (PK_TASK (task), package_ids, NULL,
1837
 
                        (PkProgressCallback) pk_test_task_text_progress_cb, NULL,
1838
 
                        (GAsyncReadyCallback) pk_test_task_text_install_packages_cb, NULL);
1839
 
        g_strfreev (package_ids);
1840
 
        _g_test_loop_run_with_timeout (150000);
1841
 
        g_debug ("installed in %f", g_test_timer_elapsed ());
1842
 
 
1843
 
        g_object_unref (task);
1844
 
}
1845
 
 
1846
 
static void
1847
 
pk_test_task_wrapper_install_packages_cb (GObject *object, GAsyncResult *res, gpointer user_data)
1848
 
{
1849
 
        PkTaskWrapper *task = PK_TASK_WRAPPER (object);
1850
 
        GError *error = NULL;
1851
 
        PkResults *results;
1852
 
        PkExitEnum exit_enum;
1853
 
        GPtrArray *packages;
1854
 
 
1855
 
        /* get the results */
1856
 
        results = pk_task_generic_finish (PK_TASK (task), res, &error);
1857
 
        g_assert_no_error (error);
1858
 
        g_assert (results != NULL);
1859
 
 
1860
 
        exit_enum = pk_results_get_exit_code (results);
1861
 
        g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_SUCCESS);
1862
 
 
1863
 
        packages = pk_results_get_package_array (results);
1864
 
        g_assert (packages != NULL);
1865
 
        g_assert_cmpint (packages->len, ==, 4);
1866
 
 
1867
 
        g_ptr_array_unref (packages);
1868
 
 
1869
 
        g_debug ("results exit enum = %s", pk_exit_enum_to_string (exit_enum));
1870
 
 
1871
 
        if (results != NULL)
1872
 
                g_object_unref (results);
1873
 
        _g_test_loop_quit ();
1874
 
}
1875
 
 
1876
 
static void
1877
 
pk_test_task_wrapper_progress_cb (PkProgress *progress, PkProgressType type, gpointer user_data)
1878
 
{
1879
 
        PkStatusEnum status;
1880
 
        if (type == PK_PROGRESS_TYPE_STATUS) {
1881
 
                g_object_get (progress,
1882
 
                      "status", &status,
1883
 
                      NULL);
1884
 
                g_debug ("now %s", pk_status_enum_to_string (status));
1885
 
        }
1886
 
}
1887
 
 
1888
 
static void
1889
 
pk_test_task_wrapper_func (void)
1890
 
{
1891
 
        PkTaskWrapper *task;
1892
 
        gchar **package_ids;
1893
 
 
1894
 
        task = pk_task_wrapper_new ();
1895
 
        g_assert (task != NULL);
1896
 
 
1897
 
        /* install package */
1898
 
        package_ids = pk_package_ids_from_id ("vips-doc;7.12.4-2.fc8;noarch;linva");
1899
 
        pk_task_install_packages_async (PK_TASK (task), package_ids, NULL,
1900
 
                        (PkProgressCallback) pk_test_task_wrapper_progress_cb, NULL,
1901
 
                        (GAsyncReadyCallback) pk_test_task_wrapper_install_packages_cb, NULL);
1902
 
        g_strfreev (package_ids);
1903
 
        _g_test_loop_run_with_timeout (150000);
1904
 
        g_debug ("installed in %f", g_test_timer_elapsed ());
1905
 
 
1906
 
        g_object_unref (task);
1907
 
}
1908
 
 
1909
 
guint _added = 0;
1910
 
guint _removed = 0;
1911
 
//guint _refcount = 0;
1912
 
 
1913
 
static void
1914
 
pk_test_transaction_list_resolve_cb (GObject *object, GAsyncResult *res, gpointer user_data)
1915
 
{
1916
 
        PkClient *client = PK_CLIENT (object);
1917
 
        GError *error = NULL;
1918
 
        PkResults *results = NULL;
1919
 
        PkExitEnum exit_enum;
1920
 
 
1921
 
        /* get the results */
1922
 
        results = pk_client_generic_finish (client, res, &error);
1923
 
        g_assert_no_error (error);
1924
 
        g_assert (results != NULL);
1925
 
 
1926
 
        exit_enum = pk_results_get_exit_code (results);
1927
 
        g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_SUCCESS);
1928
 
 
1929
 
        if (results != NULL)
1930
 
                g_object_unref (results);
1931
 
        if (--_refcount == 0)
1932
 
                _g_test_loop_quit ();
1933
 
}
1934
 
 
1935
 
static void
1936
 
pk_test_transaction_list_added_cb (PkTransactionList *tlist, const gchar *tid, gpointer user_data)
1937
 
{
1938
 
        g_debug ("added %s", tid);
1939
 
        _added++;
1940
 
}
1941
 
 
1942
 
static void
1943
 
pk_test_transaction_list_removed_cb (PkTransactionList *tlist, const gchar *tid, gpointer user_data)
1944
 
{
1945
 
        g_debug ("removed %s", tid);
1946
 
        _removed++;
1947
 
}
1948
 
 
1949
 
static gboolean
1950
 
pk_transaction_list_delay_cb (gpointer user_data)
1951
 
{
1952
 
        _g_test_loop_quit ();
1953
 
        return FALSE;
1954
 
}
1955
 
 
1956
 
static void
1957
 
pk_test_transaction_list_func (void)
1958
 
{
1959
 
        PkTransactionList *tlist;
1960
 
        PkClient *client;
1961
 
        gchar **package_ids;
1962
 
 
1963
 
        /* get transaction_list object */
1964
 
        tlist = pk_transaction_list_new ();
1965
 
        g_assert (tlist != NULL);
1966
 
        g_signal_connect (tlist, "added",
1967
 
                  G_CALLBACK (pk_test_transaction_list_added_cb), NULL);
1968
 
        g_signal_connect (tlist, "removed",
1969
 
                  G_CALLBACK (pk_test_transaction_list_removed_cb), NULL);
1970
 
 
1971
 
        /* get client */
1972
 
        client = pk_client_new ();
1973
 
        g_assert (client != NULL);
1974
 
 
1975
 
        /* resolve package */
1976
 
        package_ids = pk_package_ids_from_string ("glib2;2.14.0;i386;fedora&powertop");
1977
 
        _refcount = 2;
1978
 
        pk_client_resolve_async (client, pk_bitfield_value (PK_FILTER_ENUM_INSTALLED), package_ids, NULL, NULL, NULL,
1979
 
                 (GAsyncReadyCallback) pk_test_transaction_list_resolve_cb, NULL);
1980
 
        pk_client_resolve_async (client, pk_bitfield_value (PK_FILTER_ENUM_NOT_INSTALLED), package_ids, NULL, NULL, NULL,
1981
 
                 (GAsyncReadyCallback) pk_test_transaction_list_resolve_cb, NULL);
1982
 
        g_strfreev (package_ids);
1983
 
        _g_test_loop_run_with_timeout (15000);
1984
 
        g_debug ("resolved in %f", g_test_timer_elapsed ());
1985
 
 
1986
 
        /* wait for remove */
1987
 
        g_timeout_add (100, (GSourceFunc) pk_transaction_list_delay_cb, NULL);
1988
 
        _g_test_loop_run_with_timeout (15000);
1989
 
        g_debug ("resolved in %f", g_test_timer_elapsed ());
1990
 
 
1991
 
        /* correct number of added signals */
1992
 
        g_assert_cmpint (_added, ==, 2);
1993
 
 
1994
 
        /* correct number of removed signals */
1995
 
        g_assert_cmpint (_removed, ==, 2);
1996
 
 
1997
 
        g_object_unref (tlist);
1998
 
        g_object_unref (client);
1999
 
}
2000
 
 
2001
 
static void
2002
 
pk_test_package_func (void)
2003
 
{
2004
 
        gboolean ret;
2005
 
        PkPackage *package;
2006
 
        const gchar *id;
2007
 
        gchar *text;
2008
 
 
2009
 
        /* get package */
2010
 
        package = pk_package_new ();
2011
 
        g_assert (package != NULL);
2012
 
 
2013
 
        /* get id of unset package */
2014
 
        id = pk_package_get_id (package);
2015
 
        g_assert_cmpstr (id, ==, NULL);
2016
 
 
2017
 
        /* get id of unset package */
2018
 
        g_object_get (package, "package-id", &text, NULL);
2019
 
        g_assert_cmpstr (text, ==, NULL);
2020
 
        g_free (text);
2021
 
 
2022
 
        /* set invalid id */
2023
 
        ret = pk_package_set_id (package, "gnome-power-manager", NULL);
2024
 
        g_assert (!ret);
2025
 
 
2026
 
        /* set invalid id (sections) */
2027
 
        ret = pk_package_set_id (package, "gnome-power-manager;0.1.2;i386", NULL);
2028
 
        g_assert (!ret);
2029
 
 
2030
 
        /* set invalid name */
2031
 
        ret = pk_package_set_id (package, ";0.1.2;i386;fedora", NULL);
2032
 
        g_assert (!ret);
2033
 
 
2034
 
        /* set valid name */
2035
 
        ret = pk_package_set_id (package, "gnome-power-manager;0.1.2;i386;fedora", NULL);
2036
 
        g_assert (ret);
2037
 
 
2038
 
        /* get id of set package */
2039
 
        id = pk_package_get_id (package);
2040
 
        g_assert_cmpstr (id, ==, "gnome-power-manager;0.1.2;i386;fedora");
2041
 
 
2042
 
        /* get name of set package */
2043
 
        g_object_get (package, "package-id", &text, NULL);
2044
 
        g_assert_cmpstr (text, ==, "gnome-power-manager;0.1.2;i386;fedora");
2045
 
        g_free (text);
2046
 
 
2047
 
        g_object_unref (package);
2048
 
}
2049
 
 
2050
 
int
2051
 
main (int argc, char **argv)
2052
 
{
2053
 
        g_type_init ();
2054
 
 
2055
 
        g_test_init (&argc, &argv, NULL);
2056
 
 
2057
 
        pk_debug_set_verbose (TRUE);
2058
 
        pk_debug_add_log_domain (G_LOG_DOMAIN);
2059
 
 
2060
 
        /* tests go here */
2061
 
        g_test_add_func ("/packagekit-glib2/common", pk_test_common_func);
2062
 
        g_test_add_func ("/packagekit-glib2/enum", pk_test_enum_func);
2063
 
        g_test_add_func ("/packagekit-glib2/desktop", pk_test_desktop_func);
2064
 
        g_test_add_func ("/packagekit-glib2/bitfield", pk_test_bitfield_func);
2065
 
        g_test_add_func ("/packagekit-glib2/package-id", pk_test_package_id_func);
2066
 
        g_test_add_func ("/packagekit-glib2/package-ids", pk_test_package_ids_func);
2067
 
        g_test_add_func ("/packagekit-glib2/progress", pk_test_progress_func);
2068
 
        g_test_add_func ("/packagekit-glib2/results", pk_test_results_func);
2069
 
        g_test_add_func ("/packagekit-glib2/package", pk_test_package_func);
2070
 
        g_test_add_func ("/packagekit-glib2/control", pk_test_control_func);
2071
 
        g_test_add_func ("/packagekit-glib2/transaction-list", pk_test_transaction_list_func);
2072
 
        g_test_add_func ("/packagekit-glib2/client-helper", pk_test_client_helper_func);
2073
 
        g_test_add_func ("/packagekit-glib2/client", pk_test_client_func);
2074
 
        g_test_add_func ("/packagekit-glib2/catalog", pk_test_catalog_func);
2075
 
        g_test_add_func ("/packagekit-glib2/package-sack", pk_test_package_sack_func);
2076
 
        g_test_add_func ("/packagekit-glib2/task", pk_test_task_func);
2077
 
        g_test_add_func ("/packagekit-glib2/task-wrapper", pk_test_task_wrapper_func);
2078
 
        g_test_add_func ("/packagekit-glib2/task-text", pk_test_task_text_func);
2079
 
        g_test_add_func ("/packagekit-glib2/console", pk_test_console_func);
2080
 
        g_test_add_func ("/packagekit-glib2/progress-bar", pk_test_progress_bar);
2081
 
        g_test_add_func ("/packagekit-glib2/service-pack", pk_test_service_pack_func);
2082
 
 
2083
 
        return g_test_run ();
2084
 
}
2085