Branch data Line data Source code
1 : : /* libsecret - GLib wrapper for Secret Service
2 : : *
3 : : * Copyright 2012 Red Hat Inc.
4 : : *
5 : : * This program is free software: you can redistribute it and/or modify
6 : : * it under the terms of the GNU Lesser General Public License as published
7 : : * by the Free Software Foundation; either version 2 of the licence or (at
8 : : * your option) any later version.
9 : : *
10 : : * See the included COPYING file for more information.
11 : : *
12 : : * Author: Stef Walter <stefw@gnome.org>
13 : : */
14 : :
15 : :
16 : : #include "config.h"
17 : :
18 : : #undef G_DISABLE_ASSERT
19 : :
20 : : #include "secret-collection.h"
21 : : #include "secret-item.h"
22 : : #include "secret-service.h"
23 : : #include "secret-paths.h"
24 : : #include "secret-private.h"
25 : :
26 : : #include "mock-service.h"
27 : :
28 : : #include "egg/egg-testing.h"
29 : :
30 : : #include <glib.h>
31 : :
32 : : #include <errno.h>
33 : : #include <stdlib.h>
34 : :
35 : : static const SecretSchema MOCK_SCHEMA = {
36 : : "org.mock.Schema.Item",
37 : : SECRET_SCHEMA_NONE,
38 : : {
39 : : { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
40 : : { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
41 : : { "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
42 : : }
43 : : };
44 : :
45 : : typedef struct {
46 : : SecretService *service;
47 : : } Test;
48 : :
49 : : static void
50 : 20 : setup (Test *test,
51 : : gconstpointer data)
52 : : {
53 : 20 : GError *error = NULL;
54 : 20 : const gchar *mock_script = data;
55 : :
56 : 20 : mock_service_start (mock_script, &error);
57 [ - + ]: 20 : g_assert_no_error (error);
58 : :
59 : 20 : test->service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
60 [ - + ]: 20 : g_assert_no_error (error);
61 : 20 : g_object_add_weak_pointer (G_OBJECT (test->service), (gpointer *)&test->service);
62 : 20 : }
63 : :
64 : : static void
65 : 20 : teardown (Test *test,
66 : : gconstpointer unused)
67 : : {
68 : 20 : g_object_unref (test->service);
69 : 20 : secret_service_disconnect ();
70 [ - + ]: 20 : g_assert_null (test->service);
71 : :
72 : 20 : mock_service_stop ();
73 : 20 : }
74 : :
75 : : static void
76 : 10 : on_async_result (GObject *source,
77 : : GAsyncResult *result,
78 : : gpointer user_data)
79 : : {
80 : 10 : GAsyncResult **ret = user_data;
81 [ - + ]: 10 : g_assert_nonnull (ret);
82 [ - + ]: 10 : g_assert_null (*ret);
83 : 10 : *ret = g_object_ref (result);
84 : 10 : egg_test_wait_stop ();
85 : 10 : }
86 : :
87 : : static void
88 : 4 : on_notify_stop (GObject *obj,
89 : : GParamSpec *spec,
90 : : gpointer user_data)
91 : : {
92 : 4 : guint *sigs = user_data;
93 [ - + ]: 4 : g_assert_nonnull (sigs);
94 [ - + ]: 4 : g_assert_true (*sigs > 0);
95 [ + + ]: 4 : if (--(*sigs) == 0)
96 : 2 : egg_test_wait_stop ();
97 : 4 : }
98 : :
99 : : static void
100 : 1 : test_new_sync (Test *test,
101 : : gconstpointer unused)
102 : : {
103 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
104 : 1 : GError *error = NULL;
105 : : SecretItem *item;
106 : :
107 : 1 : item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
108 [ - + ]: 1 : g_assert_no_error (error);
109 : :
110 [ - + ]: 1 : g_assert_cmpstr (g_dbus_proxy_get_object_path (G_DBUS_PROXY (item)), ==, item_path);
111 : :
112 : 1 : g_object_unref (item);
113 : 1 : }
114 : :
115 : : static void
116 : 1 : test_new_sync_noexist (Test *test,
117 : : gconstpointer unused)
118 : : {
119 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/0000";
120 : 1 : GError *error = NULL;
121 : : SecretItem *item;
122 : :
123 : 1 : item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
124 [ + - + - : 1 : g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
- + ]
125 [ - + ]: 1 : g_assert_null (item);
126 : 1 : g_clear_error (&error);
127 : 1 : }
128 : :
129 : : static void
130 : 1 : test_new_async (Test *test,
131 : : gconstpointer unused)
132 : : {
133 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
134 : 1 : GAsyncResult *result = NULL;
135 : 1 : GError *error = NULL;
136 : : SecretItem *item;
137 : :
138 : 1 : secret_item_new_for_dbus_path (test->service, item_path, SECRET_ITEM_NONE,
139 : : NULL, on_async_result, &result);
140 [ - + ]: 1 : g_assert_null (result);
141 : :
142 [ - + ]: 1 : egg_test_wait ();
143 : :
144 : 1 : item = secret_item_new_for_dbus_path_finish (result, &error);
145 [ - + ]: 1 : g_assert_no_error (error);
146 : 1 : g_object_unref (result);
147 : :
148 [ - + ]: 1 : g_assert_cmpstr (g_dbus_proxy_get_object_path (G_DBUS_PROXY (item)), ==, item_path);
149 : :
150 : 1 : g_object_unref (item);
151 : 1 : }
152 : :
153 : : static void
154 : 1 : test_new_async_noexist (Test *test,
155 : : gconstpointer unused)
156 : : {
157 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/0000";
158 : 1 : GAsyncResult *result = NULL;
159 : 1 : GError *error = NULL;
160 : : SecretItem *item;
161 : :
162 : 1 : secret_item_new_for_dbus_path (test->service, item_path, SECRET_ITEM_NONE,
163 : : NULL, on_async_result, &result);
164 [ - + ]: 1 : g_assert_null (result);
165 : :
166 [ - + ]: 1 : egg_test_wait ();
167 : :
168 : 1 : item = secret_item_new_for_dbus_path_finish (result, &error);
169 [ + - + - : 1 : g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
- + ]
170 [ - + ]: 1 : g_assert_null (item);
171 : 1 : g_clear_error (&error);
172 : 1 : g_object_unref (result);
173 : 1 : }
174 : :
175 : : #define g_assert_cmpstr_free(str1, op, str2) G_STMT_START { char *str = str1; g_assert_cmpstr (str, op, str2); g_free (str); } G_STMT_END
176 : :
177 : : static void
178 : 1 : test_create_sync (Test *test,
179 : : gconstpointer unused)
180 : : {
181 : 1 : const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
182 : : SecretCollection *collection;
183 : 1 : GError *error = NULL;
184 : : SecretItem *item;
185 : : GHashTable *attributes;
186 : : SecretValue *value;
187 : :
188 : 1 : collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
189 : : SECRET_COLLECTION_NONE, NULL, &error);
190 [ - + ]: 1 : g_assert_no_error (error);
191 : :
192 : 1 : attributes = g_hash_table_new (g_str_hash, g_str_equal);
193 : 1 : g_hash_table_insert (attributes, "even", "true");
194 : 1 : g_hash_table_insert (attributes, "string", "ten");
195 : 1 : g_hash_table_insert (attributes, "number", "10");
196 : :
197 : 1 : value = secret_value_new ("Hoohah", -1, "text/plain");
198 : :
199 : 1 : item = secret_item_create_sync (collection, &MOCK_SCHEMA, attributes, "Tunnel",
200 : : value, SECRET_ITEM_CREATE_NONE, NULL, &error);
201 [ - + ]: 1 : g_assert_no_error (error);
202 : 1 : g_object_add_weak_pointer (G_OBJECT (item), (gpointer *)&item);
203 : :
204 : 1 : g_hash_table_unref (attributes);
205 : 1 : g_object_unref (collection);
206 : 1 : secret_value_unref (value);
207 : :
208 [ - + ]: 1 : g_assert_true (g_str_has_prefix (g_dbus_proxy_get_object_path (G_DBUS_PROXY (item)), collection_path));
209 [ - + ]: 1 : g_assert_cmpstr_free (secret_item_get_label (item), ==, "Tunnel");
210 [ - + ]: 1 : g_assert_false (secret_item_get_locked (item));
211 : :
212 : 1 : g_object_unref (item);
213 [ - + ]: 1 : g_assert_null (item);
214 : 1 : }
215 : :
216 : : static void
217 : 1 : test_create_async (Test *test,
218 : : gconstpointer unused)
219 : : {
220 : 1 : const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
221 : : SecretCollection *collection;
222 : 1 : GAsyncResult *result = NULL;
223 : 1 : GError *error = NULL;
224 : : SecretItem *item;
225 : : GHashTable *attributes;
226 : : SecretValue *value;
227 : :
228 : 1 : collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
229 : : SECRET_COLLECTION_NONE, NULL, &error);
230 [ - + ]: 1 : g_assert_no_error (error);
231 : :
232 : 1 : attributes = g_hash_table_new (g_str_hash, g_str_equal);
233 : 1 : g_hash_table_insert (attributes, "even", "true");
234 : 1 : g_hash_table_insert (attributes, "string", "ten");
235 : 1 : g_hash_table_insert (attributes, "number", "10");
236 : :
237 : 1 : value = secret_value_new ("Hoohah", -1, "text/plain");
238 : :
239 : 1 : secret_item_create (collection, &MOCK_SCHEMA, attributes, "Tunnel",
240 : : value, SECRET_ITEM_CREATE_NONE, NULL, on_async_result, &result);
241 [ - + ]: 1 : g_assert_no_error (error);
242 : :
243 : 1 : g_hash_table_unref (attributes);
244 : 1 : g_object_unref (collection);
245 : 1 : secret_value_unref (value);
246 : :
247 [ - + ]: 1 : egg_test_wait ();
248 : :
249 : 1 : item = secret_item_create_finish (result, &error);
250 [ - + ]: 1 : g_assert_no_error (error);
251 : 1 : g_object_unref (result);
252 : 1 : g_object_add_weak_pointer (G_OBJECT (item), (gpointer *)&item);
253 : :
254 [ - + ]: 1 : g_assert_true (g_str_has_prefix (g_dbus_proxy_get_object_path (G_DBUS_PROXY (item)), collection_path));
255 [ - + ]: 1 : g_assert_cmpstr_free (secret_item_get_label (item), ==, "Tunnel");
256 [ - + ]: 1 : g_assert_false (secret_item_get_locked (item));
257 : :
258 : 1 : g_object_unref (item);
259 [ - + ]: 1 : g_assert_null (item);
260 : 1 : }
261 : :
262 : : static void
263 : 1 : test_properties (Test *test,
264 : : gconstpointer unused)
265 : : {
266 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
267 : 1 : GError *error = NULL;
268 : : GHashTable *attributes;
269 : : SecretService *service;
270 : : SecretItem *item;
271 : : guint64 created;
272 : : guint64 modified;
273 : : gboolean locked;
274 : : gchar *label;
275 : :
276 : 1 : item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
277 [ - + ]: 1 : g_assert_no_error (error);
278 : :
279 [ - + ]: 1 : g_assert_false (secret_item_get_locked (item));
280 [ - + ]: 1 : g_assert_cmpuint (secret_item_get_created (item), <=, time (NULL));
281 [ - + ]: 1 : g_assert_cmpuint (secret_item_get_modified (item), <=, time (NULL));
282 : :
283 : 1 : label = secret_item_get_label (item);
284 [ - + ]: 1 : g_assert_cmpstr (label, ==, "Item One");
285 : 1 : g_free (label);
286 : :
287 : 1 : attributes = secret_item_get_attributes (item);
288 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "one");
289 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "1");
290 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "false");
291 [ - + ]: 1 : g_assert_cmpuint (g_hash_table_size (attributes), ==, 4);
292 : 1 : g_hash_table_unref (attributes);
293 : :
294 : 1 : g_object_get (item,
295 : : "locked", &locked,
296 : : "created", &created,
297 : : "modified", &modified,
298 : : "label", &label,
299 : : "attributes", &attributes,
300 : : "service", &service,
301 : : NULL);
302 : :
303 [ - + ]: 1 : g_assert_false (locked);
304 [ - + ]: 1 : g_assert_cmpuint (created, <=, time (NULL));
305 [ - + ]: 1 : g_assert_cmpuint (modified, <=, time (NULL));
306 : :
307 [ - + ]: 1 : g_assert_cmpstr (label, ==, "Item One");
308 : 1 : g_free (label);
309 : :
310 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "one");
311 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "1");
312 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "false");
313 [ - + ]: 1 : g_assert_cmpuint (g_hash_table_size (attributes), ==, 4);
314 : 1 : g_hash_table_unref (attributes);
315 : :
316 [ - + ]: 1 : g_assert_true (service == test->service);
317 : 1 : g_object_unref (service);
318 : :
319 : 1 : g_object_unref (item);
320 : 1 : }
321 : :
322 : : static void
323 : 1 : test_set_label_sync (Test *test,
324 : : gconstpointer unused)
325 : : {
326 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
327 : 1 : GError *error = NULL;
328 : : SecretItem *item;
329 : : gboolean ret;
330 : : gchar *label;
331 : :
332 : 1 : item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
333 [ - + ]: 1 : g_assert_no_error (error);
334 : :
335 : 1 : label = secret_item_get_label (item);
336 [ - + ]: 1 : g_assert_cmpstr (label, ==, "Item One");
337 : 1 : g_free (label);
338 : :
339 : 1 : ret = secret_item_set_label_sync (item, "Another label", NULL, &error);
340 [ - + ]: 1 : g_assert_no_error (error);
341 [ - + ]: 1 : g_assert_true (ret);
342 : :
343 : 1 : label = secret_item_get_label (item);
344 [ - + ]: 1 : g_assert_cmpstr (label, ==, "Another label");
345 : 1 : g_free (label);
346 : :
347 : 1 : g_object_unref (item);
348 : 1 : }
349 : :
350 : : static void
351 : 1 : test_set_label_async (Test *test,
352 : : gconstpointer unused)
353 : : {
354 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
355 : 1 : GAsyncResult *result = NULL;
356 : 1 : GError *error = NULL;
357 : : SecretItem *item;
358 : : gboolean ret;
359 : : gchar *label;
360 : :
361 : 1 : item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
362 [ - + ]: 1 : g_assert_no_error (error);
363 : :
364 : 1 : label = secret_item_get_label (item);
365 [ - + ]: 1 : g_assert_cmpstr (label, ==, "Item One");
366 : 1 : g_free (label);
367 : :
368 : 1 : secret_item_set_label (item, "Another label", NULL, on_async_result, &result);
369 [ - + ]: 1 : g_assert_null (result);
370 : :
371 [ - + ]: 1 : egg_test_wait ();
372 : :
373 : 1 : ret = secret_item_set_label_finish (item, result, &error);
374 [ - + ]: 1 : g_assert_no_error (error);
375 [ - + ]: 1 : g_assert_true (ret);
376 : 1 : g_object_unref (result);
377 : :
378 : 1 : label = secret_item_get_label (item);
379 [ - + ]: 1 : g_assert_cmpstr (label, ==, "Another label");
380 : 1 : g_free (label);
381 : :
382 : 1 : g_object_unref (item);
383 : 1 : }
384 : :
385 : : static void
386 : 1 : test_set_label_prop (Test *test,
387 : : gconstpointer unused)
388 : : {
389 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
390 : 1 : GAsyncResult *result = NULL;
391 : 1 : GError *error = NULL;
392 : : SecretItem *item;
393 : 1 : guint sigs = 2;
394 : : gchar *label;
395 : :
396 : 1 : secret_item_new_for_dbus_path (test->service, item_path, SECRET_ITEM_NONE, NULL, on_async_result, &result);
397 [ - + ]: 1 : g_assert_null (result);
398 [ - + ]: 1 : egg_test_wait ();
399 : 1 : item = secret_item_new_for_dbus_path_finish (result, &error);
400 [ - + ]: 1 : g_assert_no_error (error);
401 : 1 : g_object_unref (result);
402 : :
403 : 1 : label = secret_item_get_label (item);
404 [ - + ]: 1 : g_assert_cmpstr (label, ==, "Item One");
405 : 1 : g_free (label);
406 : :
407 : 1 : g_signal_connect (item, "notify::label", G_CALLBACK (on_notify_stop), &sigs);
408 : 1 : g_object_set (item, "label", "Blah blah", NULL);
409 : :
410 : : /* Wait for the property to actually 'take' */
411 [ - + ]: 1 : egg_test_wait ();
412 : :
413 : 1 : label = secret_item_get_label (item);
414 [ - + ]: 1 : g_assert_cmpstr (label, ==, "Blah blah");
415 : 1 : g_free (label);
416 : :
417 : 1 : g_object_add_weak_pointer (G_OBJECT (item), (gpointer *)&item);
418 : 1 : g_object_unref (item);
419 : :
420 [ + + ]: 3 : while (item != NULL)
421 : 2 : g_main_context_iteration (g_main_context_get_thread_default (), TRUE);
422 : 1 : }
423 : :
424 : : static void
425 : 1 : test_set_attributes_sync (Test *test,
426 : : gconstpointer unused)
427 : : {
428 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
429 : 1 : GError *error = NULL;
430 : : SecretItem *item;
431 : : gboolean ret;
432 : : GHashTable *attributes;
433 : : gchar *schema_name;
434 : :
435 : 1 : item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
436 [ - + ]: 1 : g_assert_no_error (error);
437 : :
438 : 1 : attributes = secret_item_get_attributes (item);
439 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "one");
440 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "1");
441 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "false");
442 [ - + ]: 1 : g_assert_cmpuint (g_hash_table_size (attributes), ==, 4);
443 : 1 : g_hash_table_unref (attributes);
444 : :
445 : : /* Has some other schema */
446 : 1 : schema_name = secret_item_get_schema_name (item);
447 [ - + ]: 1 : g_assert_cmpstr (schema_name, !=, MOCK_SCHEMA.name);
448 : 1 : g_free (schema_name);
449 : :
450 : 1 : attributes = g_hash_table_new (g_str_hash, g_str_equal);
451 : 1 : g_hash_table_insert (attributes, "string", "five");
452 : 1 : g_hash_table_insert (attributes, "number", "5");
453 : 1 : ret = secret_item_set_attributes_sync (item, &MOCK_SCHEMA, attributes, NULL, &error);
454 : 1 : g_hash_table_unref (attributes);
455 [ - + ]: 1 : g_assert_no_error (error);
456 [ - + ]: 1 : g_assert_true (ret);
457 : :
458 : 1 : attributes = secret_item_get_attributes (item);
459 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "five");
460 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "5");
461 [ - + ]: 1 : g_assert_cmpuint (g_hash_table_size (attributes), ==, 3);
462 : 1 : g_hash_table_unref (attributes);
463 : :
464 : : /* Now has our schema */
465 : 1 : schema_name = secret_item_get_schema_name (item);
466 [ - + ]: 1 : g_assert_cmpstr (schema_name, ==, MOCK_SCHEMA.name);
467 : 1 : g_free (schema_name);
468 : :
469 : 1 : g_object_unref (item);
470 : 1 : }
471 : :
472 : : static void
473 : 1 : test_set_attributes_async (Test *test,
474 : : gconstpointer unused)
475 : : {
476 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
477 : : GHashTable *attributes;
478 : 1 : GError *error = NULL;
479 : 1 : GAsyncResult *result = NULL;
480 : : SecretItem *item;
481 : : gchar *schema_name;
482 : : gboolean ret;
483 : :
484 : 1 : item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
485 [ - + ]: 1 : g_assert_no_error (error);
486 : :
487 : 1 : attributes = secret_item_get_attributes (item);
488 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "one");
489 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "1");
490 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "false");
491 [ - + ]: 1 : g_assert_cmpuint (g_hash_table_size (attributes), ==, 4);
492 : 1 : g_hash_table_unref (attributes);
493 : :
494 : : /* Has some other schema */
495 : 1 : schema_name = secret_item_get_schema_name (item);
496 [ - + ]: 1 : g_assert_cmpstr (schema_name, !=, MOCK_SCHEMA.name);
497 : 1 : g_free (schema_name);
498 : :
499 : 1 : attributes = g_hash_table_new (g_str_hash, g_str_equal);
500 : 1 : g_hash_table_insert (attributes, "string", "five");
501 : 1 : g_hash_table_insert (attributes, "number", "5");
502 : 1 : secret_item_set_attributes (item, &MOCK_SCHEMA, attributes, NULL, on_async_result, &result);
503 [ - + ]: 1 : g_assert_null (result);
504 : 1 : g_hash_table_unref (attributes);
505 : :
506 [ - + ]: 1 : egg_test_wait ();
507 : :
508 : 1 : ret = secret_item_set_attributes_finish (item, result, &error);
509 [ - + ]: 1 : g_assert_no_error (error);
510 [ - + ]: 1 : g_assert_true (ret);
511 : 1 : g_object_unref (result);
512 : :
513 : 1 : attributes = secret_item_get_attributes (item);
514 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "five");
515 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "5");
516 [ - + ]: 1 : g_assert_cmpuint (g_hash_table_size (attributes), ==, 3);
517 : 1 : g_hash_table_unref (attributes);
518 : :
519 : : /* Now has our schema */
520 : 1 : schema_name = secret_item_get_schema_name (item);
521 [ - + ]: 1 : g_assert_cmpstr (schema_name, ==, MOCK_SCHEMA.name);
522 : 1 : g_free (schema_name);
523 : :
524 : 1 : g_object_unref (item);
525 : 1 : }
526 : :
527 : : static void
528 : 1 : test_set_attributes_prop (Test *test,
529 : : gconstpointer unused)
530 : : {
531 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
532 : 1 : GAsyncResult *result = NULL;
533 : 1 : GError *error = NULL;
534 : : SecretItem *item;
535 : : GHashTable *attributes;
536 : 1 : guint sigs = 2;
537 : :
538 : 1 : secret_item_new_for_dbus_path (test->service, item_path, SECRET_ITEM_NONE, NULL, on_async_result, &result);
539 [ - + ]: 1 : g_assert_null (result);
540 [ - + ]: 1 : egg_test_wait ();
541 : 1 : item = secret_item_new_for_dbus_path_finish (result, &error);
542 [ - + ]: 1 : g_assert_no_error (error);
543 : 1 : g_object_unref (result);
544 : :
545 : 1 : attributes = secret_item_get_attributes (item);
546 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "one");
547 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "1");
548 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "false");
549 [ - + ]: 1 : g_assert_cmpuint (g_hash_table_size (attributes), ==, 4);
550 : 1 : g_hash_table_unref (attributes);
551 : :
552 : 1 : g_signal_connect (item, "notify::attributes", G_CALLBACK (on_notify_stop), &sigs);
553 : :
554 : 1 : attributes = g_hash_table_new (g_str_hash, g_str_equal);
555 : 1 : g_hash_table_insert (attributes, "string", "five");
556 : 1 : g_hash_table_insert (attributes, "number", "5");
557 : 1 : g_object_set (item, "attributes", attributes, NULL);
558 : 1 : g_hash_table_unref (attributes);
559 : :
560 : : /* Wait for the property to actually 'take' */
561 [ - + ]: 1 : egg_test_wait ();
562 : :
563 : 1 : attributes = secret_item_get_attributes (item);
564 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "five");
565 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "5");
566 [ - + ]: 1 : g_assert_cmpuint (g_hash_table_size (attributes), ==, 2);
567 : 1 : g_hash_table_unref (attributes);
568 : :
569 : 1 : g_object_add_weak_pointer (G_OBJECT (item), (gpointer *)&item);
570 : 1 : g_object_unref (item);
571 : :
572 [ + + ]: 3 : while (item != NULL)
573 : 2 : g_main_context_iteration (g_main_context_get_thread_default (), TRUE);
574 : 1 : }
575 : :
576 : : static void
577 : 1 : test_load_secret_sync (Test *test,
578 : : gconstpointer unused)
579 : : {
580 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
581 : 1 : GError *error = NULL;
582 : : SecretItem *item;
583 : : SecretValue *value;
584 : : gconstpointer data;
585 : : gboolean ret;
586 : : gsize length;
587 : :
588 : 1 : item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
589 [ - + ]: 1 : g_assert_no_error (error);
590 : :
591 : 1 : value = secret_item_get_secret (item);
592 [ - + ]: 1 : g_assert_null (value);
593 : :
594 : 1 : ret = secret_item_load_secret_sync (item, NULL, &error);
595 [ - + ]: 1 : g_assert_no_error (error);
596 [ - + ]: 1 : g_assert_true (ret);
597 : :
598 : 1 : value = secret_item_get_secret (item);
599 [ - + ]: 1 : g_assert_nonnull (value);
600 : :
601 : 1 : data = secret_value_get (value, &length);
602 [ + - + - ]: 1 : egg_assert_cmpmem (data, length, ==, "111", 3);
603 : :
604 : 1 : secret_value_unref (value);
605 : :
606 : 1 : g_object_unref (item);
607 : 1 : }
608 : :
609 : : static void
610 : 1 : test_load_secret_async (Test *test,
611 : : gconstpointer unused)
612 : : {
613 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
614 : 1 : GAsyncResult *result = NULL;
615 : 1 : GError *error = NULL;
616 : : SecretItem *item;
617 : : SecretValue *value;
618 : : gconstpointer data;
619 : : gboolean ret;
620 : : gsize length;
621 : :
622 : 1 : item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
623 [ - + ]: 1 : g_assert_no_error (error);
624 : :
625 : 1 : value = secret_item_get_secret (item);
626 [ - + ]: 1 : g_assert_null (value);
627 : :
628 : 1 : secret_item_load_secret (item, NULL, on_async_result, &result);
629 [ - + ]: 1 : g_assert_null (result);
630 : :
631 [ - + ]: 1 : egg_test_wait ();
632 : :
633 : 1 : ret = secret_item_load_secret_finish (item, result, &error);
634 [ - + ]: 1 : g_assert_no_error (error);
635 [ - + ]: 1 : g_assert_true (ret);
636 : 1 : g_object_unref (result);
637 : :
638 : 1 : value = secret_item_get_secret (item);
639 [ - + ]: 1 : g_assert_nonnull (value);
640 : :
641 : 1 : data = secret_value_get (value, &length);
642 [ + - + - ]: 1 : egg_assert_cmpmem (data, length, ==, "111", 3);
643 : :
644 : 1 : secret_value_unref (value);
645 : :
646 : 1 : g_object_unref (item);
647 : 1 : }
648 : :
649 : : static void
650 : 1 : test_set_secret_sync (Test *test,
651 : : gconstpointer unused)
652 : : {
653 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
654 : 1 : GError *error = NULL;
655 : : SecretItem *item;
656 : : gconstpointer data;
657 : : SecretValue *value;
658 : : SecretValue *check;
659 : : gboolean ret;
660 : : gsize length;
661 : :
662 : 1 : value = secret_value_new ("Sinking", -1, "strange/content-type");
663 : :
664 : 1 : item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
665 [ - + ]: 1 : g_assert_no_error (error);
666 : :
667 : 1 : ret = secret_item_set_secret_sync (item, value, NULL, &error);
668 [ - + ]: 1 : g_assert_no_error (error);
669 [ - + ]: 1 : g_assert_true (ret);
670 : :
671 : 1 : check = secret_item_get_secret (item);
672 [ - + ]: 1 : g_assert_true (check == value);
673 : 1 : secret_value_unref (check);
674 : 1 : secret_value_unref (value);
675 : :
676 : 1 : ret = secret_item_load_secret_sync (item, NULL, &error);
677 [ - + ]: 1 : g_assert_no_error (error);
678 [ - + ]: 1 : g_assert_true (ret);
679 : :
680 : 1 : value = secret_item_get_secret (item);
681 [ - + ]: 1 : g_assert_nonnull (value);
682 : :
683 : 1 : data = secret_value_get (value, &length);
684 [ + - + - ]: 1 : egg_assert_cmpmem (data, length, ==, "Sinking", 7);
685 [ - + ]: 1 : g_assert_cmpstr (secret_value_get_content_type (value), ==, "strange/content-type");
686 : :
687 : 1 : secret_value_unref (value);
688 : 1 : g_object_unref (item);
689 : 1 : }
690 : :
691 : : static void
692 : 1 : test_secrets_sync (Test *test,
693 : : gconstpointer used)
694 : : {
695 : 1 : const gchar *path_item_one = "/org/freedesktop/secrets/collection/english/1";
696 : 1 : const gchar *path_item_two = "/org/freedesktop/secrets/collection/english/2";
697 : 1 : const gchar *path_item_three = "/org/freedesktop/secrets/collection/spanish/10";
698 : :
699 : : SecretValue *value;
700 : 1 : GError *error = NULL;
701 : : const gchar *password;
702 : : SecretItem *item_one, *item_two, *item_three;
703 : 1 : GList *items = NULL;
704 : : gboolean ret;
705 : : gsize length;
706 : :
707 : 1 : item_one = secret_item_new_for_dbus_path_sync (test->service, path_item_one, SECRET_ITEM_NONE, NULL, &error);
708 : 1 : item_two = secret_item_new_for_dbus_path_sync (test->service, path_item_two, SECRET_ITEM_NONE, NULL, &error);
709 : 1 : item_three = secret_item_new_for_dbus_path_sync (test->service, path_item_three, SECRET_ITEM_NONE, NULL, &error);
710 : :
711 : 1 : items = g_list_append (items, item_one);
712 : 1 : items = g_list_append (items, item_two);
713 : 1 : items = g_list_append (items, item_three);
714 : :
715 : 1 : ret = secret_item_load_secrets_sync (items, NULL, &error);
716 [ - + ]: 1 : g_assert_no_error (error);
717 [ - + ]: 1 : g_assert_true (ret);
718 : :
719 : 1 : value = secret_item_get_secret (item_one);
720 [ - + ]: 1 : g_assert_nonnull (value);
721 : 1 : password = secret_value_get (value, &length);
722 [ - + ]: 1 : g_assert_cmpuint (length, ==, 3);
723 [ - + ]: 1 : g_assert_cmpstr (password, ==, "111");
724 : 1 : secret_value_unref (value);
725 : :
726 : 1 : value = secret_item_get_secret (item_two);
727 [ - + ]: 1 : g_assert_nonnull (value);
728 : 1 : password = secret_value_get (value, &length);
729 [ - + ]: 1 : g_assert_cmpuint (length, ==, 3);
730 [ - + ]: 1 : g_assert_cmpstr (password, ==, "222");
731 : 1 : secret_value_unref (value);
732 : :
733 : 1 : value = secret_item_get_secret (item_three);
734 [ - + ]: 1 : g_assert_null (value);
735 : :
736 : 1 : g_list_free_full (items, g_object_unref);
737 : 1 : }
738 : :
739 : : static void
740 : 1 : test_secrets_async (Test *test,
741 : : gconstpointer used)
742 : : {
743 : 1 : const gchar *path_item_one = "/org/freedesktop/secrets/collection/english/1";
744 : 1 : const gchar *path_item_two = "/org/freedesktop/secrets/collection/english/2";
745 : 1 : const gchar *path_item_three = "/org/freedesktop/secrets/collection/spanish/10";
746 : :
747 : : SecretValue *value;
748 : 1 : GError *error = NULL;
749 : : const gchar *password;
750 : 1 : GAsyncResult *result = NULL;
751 : : SecretItem *item_one, *item_two, *item_three;
752 : 1 : GList *items = NULL;
753 : : gsize length;
754 : : gboolean ret;
755 : :
756 : 1 : item_one = secret_item_new_for_dbus_path_sync (test->service, path_item_one, SECRET_ITEM_NONE, NULL, &error);
757 [ - + ]: 1 : g_assert_no_error (error);
758 : :
759 : 1 : item_two = secret_item_new_for_dbus_path_sync (test->service, path_item_two, SECRET_ITEM_NONE, NULL, &error);
760 [ - + ]: 1 : g_assert_no_error (error);
761 : :
762 : 1 : item_three = secret_item_new_for_dbus_path_sync (test->service, path_item_three, SECRET_ITEM_NONE, NULL, &error);
763 [ - + ]: 1 : g_assert_no_error (error);
764 : :
765 : :
766 : 1 : items = g_list_append (items, item_one);
767 : 1 : items = g_list_append (items, item_two);
768 : 1 : items = g_list_append (items, item_three);
769 : :
770 : 1 : secret_item_load_secrets (items, NULL,
771 : : on_async_result, &result);
772 [ - + ]: 1 : g_assert_null (result);
773 : :
774 [ - + ]: 1 : egg_test_wait ();
775 : :
776 : 1 : ret = secret_item_load_secrets_finish (result, &error);
777 [ - + ]: 1 : g_assert_no_error (error);
778 : 1 : g_object_unref (result);
779 [ - + ]: 1 : g_assert_true (ret);
780 : :
781 : 1 : value = secret_item_get_secret (item_one);
782 [ - + ]: 1 : g_assert_nonnull (value);
783 : 1 : password = secret_value_get (value, &length);
784 [ - + ]: 1 : g_assert_cmpuint (length, ==, 3);
785 [ - + ]: 1 : g_assert_cmpstr (password, ==, "111");
786 : 1 : secret_value_unref (value);
787 : :
788 : 1 : value = secret_item_get_secret (item_two);
789 [ - + ]: 1 : g_assert_nonnull (value);
790 : 1 : password = secret_value_get (value, &length);
791 [ - + ]: 1 : g_assert_cmpuint (length, ==, 3);
792 [ - + ]: 1 : g_assert_cmpstr (password, ==, "222");
793 : 1 : secret_value_unref (value);
794 : :
795 : 1 : value = secret_item_get_secret (item_three);
796 [ - + ]: 1 : g_assert_null (value);
797 : :
798 : 1 : g_object_unref (item_one);
799 : 1 : g_object_unref (item_two);
800 : 1 : g_object_unref (item_three);
801 : 1 : g_list_free (items);
802 : 1 : }
803 : :
804 : : static void
805 : 1 : test_delete_sync (Test *test,
806 : : gconstpointer unused)
807 : : {
808 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
809 : 1 : GError *error = NULL;
810 : : SecretItem *item;
811 : : gboolean ret;
812 : :
813 : 1 : item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
814 [ - + ]: 1 : g_assert_no_error (error);
815 : :
816 : 1 : ret = secret_item_delete_sync (item, NULL, &error);
817 [ - + ]: 1 : g_assert_no_error (error);
818 [ - + ]: 1 : g_assert_true (ret);
819 : :
820 : 1 : g_object_unref (item);
821 : :
822 : 1 : item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
823 [ + - + - : 1 : g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
- + ]
824 [ - + ]: 1 : g_assert_null (item);
825 : 1 : g_clear_error (&error);
826 : 1 : }
827 : :
828 : : static void
829 : 1 : test_delete_async (Test *test,
830 : : gconstpointer unused)
831 : : {
832 : 1 : const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
833 : 1 : GAsyncResult *result = NULL;
834 : 1 : GError *error = NULL;
835 : : SecretItem *item;
836 : : gboolean ret;
837 : :
838 : 1 : item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
839 [ - + ]: 1 : g_assert_no_error (error);
840 : :
841 : 1 : secret_item_delete (item, NULL, on_async_result, &result);
842 [ - + ]: 1 : g_assert_null (result);
843 : :
844 [ - + ]: 1 : egg_test_wait ();
845 : :
846 : 1 : ret = secret_item_delete_finish (item, result, &error);
847 [ - + ]: 1 : g_assert_no_error (error);
848 [ - + ]: 1 : g_assert_true (ret);
849 : :
850 : 1 : g_object_unref (result);
851 : 1 : g_object_unref (item);
852 : :
853 : 1 : item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
854 [ + - + - : 1 : g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
- + ]
855 [ - + ]: 1 : g_assert_null (item);
856 : 1 : g_clear_error (&error);
857 : 1 : }
858 : :
859 : : int
860 : 1 : main (int argc, char **argv)
861 : : {
862 : 1 : g_test_init (&argc, &argv, NULL);
863 : 1 : g_set_prgname ("test-item");
864 : :
865 : 1 : g_test_add ("/item/new-sync", Test, "mock-service-normal.py", setup, test_new_sync, teardown);
866 : 1 : g_test_add ("/item/new-async", Test, "mock-service-normal.py", setup, test_new_async, teardown);
867 : 1 : g_test_add ("/item/new-sync-noexist", Test, "mock-service-normal.py", setup, test_new_sync_noexist, teardown);
868 : 1 : g_test_add ("/item/new-async-noexist", Test, "mock-service-normal.py", setup, test_new_async_noexist, teardown);
869 : 1 : g_test_add ("/item/create-sync", Test, "mock-service-normal.py", setup, test_create_sync, teardown);
870 : 1 : g_test_add ("/item/create-async", Test, "mock-service-normal.py", setup, test_create_async, teardown);
871 : 1 : g_test_add ("/item/properties", Test, "mock-service-normal.py", setup, test_properties, teardown);
872 : 1 : g_test_add ("/item/set-label-sync", Test, "mock-service-normal.py", setup, test_set_label_sync, teardown);
873 : 1 : g_test_add ("/item/set-label-async", Test, "mock-service-normal.py", setup, test_set_label_async, teardown);
874 : 1 : g_test_add ("/item/set-label-prop", Test, "mock-service-normal.py", setup, test_set_label_prop, teardown);
875 : 1 : g_test_add ("/item/set-attributes-sync", Test, "mock-service-normal.py", setup, test_set_attributes_sync, teardown);
876 : 1 : g_test_add ("/item/set-attributes-async", Test, "mock-service-normal.py", setup, test_set_attributes_async, teardown);
877 : 1 : g_test_add ("/item/set-attributes-prop", Test, "mock-service-normal.py", setup, test_set_attributes_prop, teardown);
878 : 1 : g_test_add ("/item/load-secret-sync", Test, "mock-service-normal.py", setup, test_load_secret_sync, teardown);
879 : 1 : g_test_add ("/item/load-secret-async", Test, "mock-service-normal.py", setup, test_load_secret_async, teardown);
880 : 1 : g_test_add ("/item/set-secret-sync", Test, "mock-service-normal.py", setup, test_set_secret_sync, teardown);
881 : 1 : g_test_add ("/item/secrets-sync", Test, "mock-service-normal.py", setup, test_secrets_sync, teardown);
882 : 1 : g_test_add ("/item/secrets-async", Test, "mock-service-normal.py", setup, test_secrets_async, teardown);
883 : 1 : g_test_add ("/item/delete-sync", Test, "mock-service-normal.py", setup, test_delete_sync, teardown);
884 : 1 : g_test_add ("/item/delete-async", Test, "mock-service-normal.py", setup, test_delete_async, teardown);
885 : :
886 : 1 : return egg_tests_run_with_loop ();
887 : : }
|