1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Copyright (C) 2006 Imendio AB
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Martyn Russell <martyn@imendio.com>
*/
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include "tests.h"
#define DAPI_DBUS_SERVICE "org.freedesktop.dapi"
#define DAPI_DBUS_PATH "/org/freedesktop/dapi"
#define DAPI_DBUS_INTERFACE "org.freedesktop.dapi"
DBusGProxy *
tests_get_dbus_proxy (void)
{
DBusGConnection *connection;
static DBusGProxy *proxy = NULL;
GError *error = NULL;
if (proxy) {
return proxy;
}
connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
if (connection == NULL) {
g_warning ("Failed to open connection to session bus: %s\n",
error->message);
g_clear_error (&error);
return NULL;
}
proxy = dbus_g_proxy_new_for_name (connection,
DAPI_DBUS_SERVICE,
DAPI_DBUS_PATH,
DAPI_DBUS_INTERFACE);
return proxy;
}
static GValue *
tests_get_windowinfo (gboolean clear)
{
static GValue windowinfo = {0, };
if (clear && G_IS_VALUE (&windowinfo)) {
g_value_unset (&windowinfo);
return NULL;
}
if (G_IS_VALUE (&windowinfo)) {
return &windowinfo;
}
g_value_init (&windowinfo, G_TYPE_INT);
return &windowinfo;
}
/*
* Actual tests
*/
START_TEST (test_get_capabilities)
{
DBusGProxy *proxy;
gboolean result;
gint capabilities;
proxy = tests_get_dbus_proxy ();
result = org_freedesktop_dapi_capabilities (proxy, &capabilities, NULL);
fail_if (result == FALSE);
fail_if (capabilities < 1);
}
END_TEST
START_TEST (test_get_button_order)
{
DBusGProxy *proxy;
gboolean result;
gint order;
proxy = tests_get_dbus_proxy ();
result = org_freedesktop_dapi_button_order (proxy, &order, NULL);
fail_if (result == FALSE);
fail_if (order < 1);
fail_if (order > 2);
}
END_TEST
START_TEST (test_run_as_user)
{
DBusGProxy *proxy;
gboolean result;
proxy = tests_get_dbus_proxy ();
/* Expected successes */
result = org_freedesktop_dapi_run_as_user (proxy, "root", "/usr/bin/test",
tests_get_windowinfo (FALSE), NULL);
fail_if (result == FALSE);
/* Expected failures */
/* gksu doesn't return FALSE on failure, so we always get
* TRUE, this test can't be concluded until we can trust the
* return value.
*
* result = org_freedesktop_dapi_run_as_user (proxy, "invalidusername", "/test", NULL);
* fail_if (result == TRUE);
*/
result = org_freedesktop_dapi_run_as_user (proxy, NULL, NULL,
tests_get_windowinfo (FALSE), NULL);
fail_if (result == TRUE);
}
END_TEST
START_TEST (test_screen_saving_suspend)
{
DBusGProxy *proxy;
gboolean result;
proxy = tests_get_dbus_proxy ();
result = org_freedesktop_dapi_suspend_screen_saving (proxy, 1, TRUE, NULL);
fail_if (result == FALSE);
/* Should fail due to the client id already setting suspend on */
result = org_freedesktop_dapi_suspend_screen_saving (proxy, 1, TRUE, NULL);
fail_if (result == TRUE);
result = org_freedesktop_dapi_suspend_screen_saving (proxy, 1, FALSE, NULL);
fail_if (result == FALSE);
/* Should fail due to the client id already setting resume */
result = org_freedesktop_dapi_suspend_screen_saving (proxy, 1, FALSE, NULL);
fail_if (result == TRUE);
}
END_TEST
START_TEST (test_open_url)
{
DBusGProxy *proxy;
gboolean result;
proxy = tests_get_dbus_proxy ();
/* Expected successes */
result = org_freedesktop_dapi_open_url (proxy, "http://www.google.com",
tests_get_windowinfo (FALSE), NULL);
fail_if (result == FALSE);
/* Expected failures */
result = org_freedesktop_dapi_open_url (proxy, "www.google.com",
tests_get_windowinfo (FALSE), NULL);
fail_if (result == TRUE);
}
END_TEST
START_TEST (test_execute_url)
{
DBusGProxy *proxy;
gboolean result;
proxy = tests_get_dbus_proxy ();
/* Expected successes */
result = org_freedesktop_dapi_execute_url (proxy, "http://www.imendio.com",
tests_get_windowinfo (FALSE), NULL);
fail_if (result == FALSE);
result = org_freedesktop_dapi_execute_url (proxy, "file:///etc/fstab",
tests_get_windowinfo (FALSE), NULL);
fail_if (result == FALSE);
/* Expected failures */
result = org_freedesktop_dapi_execute_url (proxy, "etc/fstab",
tests_get_windowinfo (FALSE), NULL);
fail_if (result == TRUE);
result = org_freedesktop_dapi_execute_url (proxy, "aaa:///etc/fstab",
tests_get_windowinfo (FALSE), NULL);
fail_if (result == TRUE);
}
END_TEST
START_TEST (test_mail_to)
{
DBusGProxy *proxy;
const gchar *nullp[] = { 0, };
gboolean result;
proxy = tests_get_dbus_proxy ();
/* Note: DBus sucks because it doesn't NULL check the
* G_TYPE_STRV attachments so we have to pass our nullp
* variable in otherwise it will SEGV, --MJR
*/
/* Expected successes */
result = org_freedesktop_dapi_mail_to (proxy,
"test", "foo",
"martyn@imendio.com",
"micke@imendio.com",
"waldo.bastian@intel.com",
nullp,
tests_get_windowinfo (FALSE),
NULL);
fail_if (result == FALSE);
}
END_TEST
START_TEST (test_files)
{
DBusGProxy *proxy;
gchar *filename;
gchar *p = NULL;
gboolean result;
proxy = tests_get_dbus_proxy ();
/* Expected failures */
result = org_freedesktop_dapi_local_file (proxy,
"http://www.google.com/index.html",
"file:///tmp/index.html",
FALSE,
tests_get_windowinfo (FALSE),
&filename,
NULL);
fail_if (result == TRUE);
/* FIXME: This next line WILL fail, because DBus sucks and
* doesn't set data that we are receiving to NULL when there
* is an error.
*
* fail_if (filename != NULL || strlen (filename) > 0);
*/
result = org_freedesktop_dapi_remove_temporary_local_file (proxy, "file:///tmp/somefile.txt", NULL);
fail_if (result == TRUE);
/* Expected successes (we do this after because we use the other APIs to clean up */
result = org_freedesktop_dapi_local_file (proxy,
"http://www.google.com/index.html",
"file:///tmp/index.html",
TRUE,
tests_get_windowinfo (FALSE),
&filename,
NULL);
/* FIXME: We can't use a filename in the format of
* 'file:///tmp/foo' here, we need to remove the 'file://' part
*/
if (filename && strstr (filename, "file://")) {
p = filename + 7;
}
fail_if (result == FALSE);
fail_if (filename == NULL || strlen (filename) < 1);
fail_if (p && g_file_test (p, G_FILE_TEST_EXISTS) == FALSE);
result = org_freedesktop_dapi_remove_temporary_local_file (proxy, filename, NULL);
fail_if (result == FALSE);
fail_if (p && g_file_test (p, G_FILE_TEST_EXISTS) == TRUE);
/* Final clean up */
g_free (filename);
}
END_TEST
static Suite *
tests_create_synchronous_test_suite (void)
{
Suite *suite;
TCase *t_case;
suite = suite_create ("Synchronous");
t_case = tcase_create ("Capabilities");
suite_add_tcase (suite, t_case);
tcase_add_test (t_case, test_get_capabilities);
t_case = tcase_create ("Button Order");
suite_add_tcase (suite, t_case);
tcase_add_test (t_case, test_get_button_order);
t_case = tcase_create ("Run As User");
suite_add_tcase (suite, t_case);
tcase_add_test (t_case, test_run_as_user);
t_case = tcase_create ("Suspend Screen Saving");
suite_add_tcase (suite, t_case);
tcase_add_test (t_case, test_screen_saving_suspend);
t_case = tcase_create ("Opening URL");
suite_add_tcase (suite, t_case);
tcase_add_test (t_case, test_open_url);
t_case = tcase_create ("Executing URL");
suite_add_tcase (suite, t_case);
tcase_add_test (t_case, test_execute_url);
t_case = tcase_create ("Creating Mail");
suite_add_tcase (suite, t_case);
tcase_add_test (t_case, test_mail_to);
return suite;
}
static Suite *
tests_create_asynchronous_test_suite (void)
{
Suite *suite;
TCase *t_case;
suite = suite_create ("Asynchronous");
t_case = tcase_create ("Local/Upload/Remove");
suite_add_tcase (suite, t_case);
tcase_add_test (t_case, test_files);
return suite;
}
int
main (int argc, char **argv)
{
DBusGProxy *proxy;
Suite *suite;
SRunner *runner;
int failures, successes;
g_type_init ();
proxy = tests_get_dbus_proxy ();
if (!proxy) {
g_warning ("Could not connect to DBus service");
return EXIT_FAILURE;
}
suite = suite_create ("-");
runner = srunner_create (suite);
srunner_add_suite (runner, tests_create_synchronous_test_suite ());
srunner_add_suite (runner, tests_create_asynchronous_test_suite ());
srunner_add_suite (runner, tests_create_addressbook_test_suite ());
srunner_run_all (runner, CK_NORMAL);
failures = srunner_ntests_failed (runner);
successes = srunner_ntests_run (runner) - failures;
srunner_free (runner);
/* Clean up */
tests_get_windowinfo (TRUE);
if (successes < 1) {
g_print ("\nHave you checked that the daemon is running?\n\n");
}
return (failures == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
|