Branch data Line data Source code
1 : :
2 : : #include "config.h"
3 : :
4 : : #undef G_DISABLE_ASSERT
5 : :
6 : : #include "egg/egg-testing.h"
7 : : #include "secret-file-collection.h"
8 : : #include "secret-retrievable.h"
9 : : #include "secret-schema.h"
10 : :
11 : : #include <stdlib.h>
12 : :
13 : : typedef struct {
14 : : gchar *directory;
15 : : GMainLoop *loop;
16 : : SecretFileCollection *collection;
17 : : } Test;
18 : :
19 : : static void
20 : 7 : on_new_async (GObject *source_object,
21 : : GAsyncResult *result,
22 : : gpointer user_data)
23 : : {
24 : 7 : Test *test = user_data;
25 : : GObject *object;
26 : 7 : GError *error = NULL;
27 : :
28 : 7 : object = g_async_initable_new_finish (G_ASYNC_INITABLE (source_object),
29 : : result,
30 : : &error);
31 : 7 : test->collection = SECRET_FILE_COLLECTION (object);
32 : 7 : g_main_loop_quit (test->loop);
33 [ - + ]: 7 : g_assert_no_error (error);
34 : 7 : }
35 : :
36 : : static void
37 : 7 : setup (Test *test,
38 : : gconstpointer data)
39 : : {
40 : : GFile *file;
41 : : gchar *path;
42 : : SecretValue *password;
43 : 7 : gchar *fixture = NULL;
44 : :
45 [ + + ]: 7 : if (data != NULL)
46 : 1 : fixture = g_build_filename (SRCDIR, "libsecret", "fixtures", data, NULL);
47 : 7 : test->directory = egg_tests_create_scratch_directory (fixture, NULL);
48 : 7 : g_free (fixture);
49 : :
50 : 7 : test->loop = g_main_loop_new (NULL, TRUE);
51 : :
52 : 7 : path = g_build_filename (test->directory, "default.keyring", NULL);
53 : 7 : file = g_file_new_for_path (path);
54 : 7 : g_free (path);
55 : :
56 : 7 : password = secret_value_new ("password", -1, "text/plain");
57 : :
58 : 7 : g_async_initable_new_async (SECRET_TYPE_FILE_COLLECTION,
59 : : G_PRIORITY_DEFAULT,
60 : : NULL,
61 : : on_new_async,
62 : : test,
63 : : "file", file,
64 : : "password", password,
65 : : NULL);
66 : :
67 : 7 : g_object_unref (file);
68 : 7 : secret_value_unref (password);
69 : :
70 : 7 : g_main_loop_run (test->loop);
71 : 7 : }
72 : :
73 : : static void
74 : 7 : teardown (Test *test,
75 : : gconstpointer unused)
76 : : {
77 : 7 : egg_tests_remove_scratch_directory (test->directory);
78 : 7 : g_free (test->directory);
79 : :
80 [ + - ]: 7 : g_clear_object (&test->collection);
81 : 7 : g_main_loop_unref (test->loop);
82 : 7 : }
83 : :
84 : : static void
85 : 1 : test_init (Test *test,
86 : : gconstpointer unused)
87 : : {
88 : 1 : }
89 : :
90 : : static void
91 : 1 : test_replace (Test *test,
92 : : gconstpointer unused)
93 : : {
94 : : GHashTable *attributes;
95 : : SecretValue *value;
96 : 1 : GError *error = NULL;
97 : : gboolean ret;
98 : :
99 : 1 : attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
100 : 1 : g_hash_table_insert (attributes, g_strdup ("foo"), g_strdup ("a"));
101 : 1 : g_hash_table_insert (attributes, g_strdup ("bar"), g_strdup ("b"));
102 : 1 : g_hash_table_insert (attributes, g_strdup ("baz"), g_strdup ("c"));
103 : :
104 : 1 : value = secret_value_new ("test1", -1, "text/plain");
105 : 1 : ret = secret_file_collection_replace (test->collection,
106 : : attributes, "label", value,
107 : : &error);
108 [ - + ]: 1 : g_assert_no_error (error);
109 [ - + ]: 1 : g_assert_true (ret);
110 : 1 : secret_value_unref (value);
111 : :
112 : 1 : value = secret_value_new ("test2", -1, "text/plain");
113 : 1 : ret = secret_file_collection_replace (test->collection,
114 : : attributes, "label", value,
115 : : &error);
116 [ - + ]: 1 : g_assert_no_error (error);
117 [ - + ]: 1 : g_assert_true (ret);
118 : 1 : secret_value_unref (value);
119 : 1 : g_hash_table_unref (attributes);
120 : 1 : }
121 : :
122 : : static void
123 : 1 : test_clear (Test *test,
124 : : gconstpointer unused)
125 : : {
126 : : GHashTable *attributes;
127 : : SecretValue *value;
128 : 1 : GError *error = NULL;
129 : : gboolean ret;
130 : :
131 : 1 : attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
132 : 1 : g_hash_table_insert (attributes, g_strdup ("foo"), g_strdup ("a"));
133 : 1 : g_hash_table_insert (attributes, g_strdup ("bar"), g_strdup ("b"));
134 : 1 : g_hash_table_insert (attributes, g_strdup ("baz"), g_strdup ("c"));
135 : :
136 : 1 : value = secret_value_new ("test1", -1, "text/plain");
137 : 1 : ret = secret_file_collection_replace (test->collection,
138 : : attributes, "label", value,
139 : : &error);
140 [ - + ]: 1 : g_assert_no_error (error);
141 [ - + ]: 1 : g_assert_true (ret);
142 : 1 : secret_value_unref (value);
143 : :
144 : 1 : ret = secret_file_collection_clear (test->collection,
145 : : attributes,
146 : : &error);
147 [ - + ]: 1 : g_assert_no_error (error);
148 [ - + ]: 1 : g_assert_true (ret);
149 : 1 : g_hash_table_unref (attributes);
150 : 1 : }
151 : :
152 : : static void
153 : 1 : test_search (Test *test,
154 : : gconstpointer unused)
155 : : {
156 : : GHashTable *attributes;
157 : : SecretValue *value;
158 : 1 : GError *error = NULL;
159 : : GList *matches;
160 : : gboolean ret;
161 : :
162 : 1 : attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
163 : 1 : g_hash_table_insert (attributes, g_strdup ("foo"), g_strdup ("a"));
164 : 1 : g_hash_table_insert (attributes, g_strdup ("bar"), g_strdup ("b"));
165 : 1 : g_hash_table_insert (attributes, g_strdup ("baz"), g_strdup ("c"));
166 : :
167 : 1 : value = secret_value_new ("test1", -1, "text/plain");
168 : 1 : ret = secret_file_collection_replace (test->collection,
169 : : attributes, "label", value,
170 : : &error);
171 [ - + ]: 1 : g_assert_no_error (error);
172 [ - + ]: 1 : g_assert_true (ret);
173 : 1 : secret_value_unref (value);
174 : :
175 : 1 : g_hash_table_remove (attributes, "foo");
176 : :
177 : 1 : value = secret_value_new ("test2", -1, "text/plain");
178 : 1 : ret = secret_file_collection_replace (test->collection,
179 : : attributes, "label", value,
180 : : &error);
181 [ - + ]: 1 : g_assert_no_error (error);
182 [ - + ]: 1 : g_assert_true (ret);
183 : 1 : secret_value_unref (value);
184 : :
185 : 1 : matches = secret_file_collection_search (test->collection, attributes);
186 [ - + ]: 1 : g_assert_cmpint (g_list_length (matches), ==, 2);
187 : 1 : g_list_free_full (matches, (GDestroyNotify)g_variant_unref);
188 : :
189 : 1 : g_hash_table_unref (attributes);
190 : 1 : }
191 : :
192 : : static void
193 : 1 : test_decrypt (Test *test,
194 : : gconstpointer unused)
195 : : {
196 : : GHashTable *attributes;
197 : : SecretValue *value;
198 : 1 : GError *error = NULL;
199 : : GList *matches;
200 : : SecretFileItem *item;
201 : : const gchar *secret;
202 : : gsize n_secret;
203 : : gchar *label;
204 : : gboolean ret;
205 : :
206 : 1 : attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
207 : 1 : g_hash_table_insert (attributes, g_strdup ("foo"), g_strdup ("a"));
208 : 1 : g_hash_table_insert (attributes, g_strdup ("bar"), g_strdup ("b"));
209 : 1 : g_hash_table_insert (attributes, g_strdup ("baz"), g_strdup ("c"));
210 : :
211 : 1 : value = secret_value_new ("test1", -1, "text/plain");
212 : 1 : ret = secret_file_collection_replace (test->collection,
213 : : attributes, "label", value,
214 : : &error);
215 [ - + ]: 1 : g_assert_no_error (error);
216 [ - + ]: 1 : g_assert_true (ret);
217 : 1 : secret_value_unref (value);
218 : :
219 : 1 : matches = secret_file_collection_search (test->collection, attributes);
220 [ - + ]: 1 : g_assert_cmpint (g_list_length (matches), ==, 1);
221 : :
222 : 1 : item = _secret_file_item_decrypt ((GVariant *)matches->data,
223 : : test->collection,
224 : : &error);
225 : 1 : g_list_free_full (matches, (GDestroyNotify)g_variant_unref);
226 [ - + ]: 1 : g_assert_no_error (error);
227 [ - + ]: 1 : g_assert_nonnull (item);
228 : :
229 : 1 : g_object_get (item, "label", &label, NULL);
230 [ - + ]: 1 : g_assert_cmpstr (label, ==, "label");
231 : 1 : g_free (label);
232 : :
233 : 1 : value = secret_retrievable_retrieve_secret_sync (SECRET_RETRIEVABLE (item),
234 : : NULL,
235 : : &error);
236 [ - + ]: 1 : g_assert_no_error (error);
237 : :
238 : 1 : secret = secret_value_get (value, &n_secret);
239 [ - + ]: 1 : g_assert_cmpstr (secret, ==, "test1");
240 : :
241 : 1 : secret_value_unref (value);
242 : 1 : g_object_unref (item);
243 : 1 : g_hash_table_unref (attributes);
244 : 1 : }
245 : :
246 : : static void
247 : 1 : on_write (GObject *source_object,
248 : : GAsyncResult *result,
249 : : gpointer user_data)
250 : : {
251 : : SecretFileCollection *collection =
252 : 1 : SECRET_FILE_COLLECTION (source_object);
253 : 1 : Test *test = user_data;
254 : 1 : GError *error = NULL;
255 : : gboolean ret;
256 : :
257 : 1 : ret = secret_file_collection_write_finish (collection,
258 : : result,
259 : : &error);
260 [ - + ]: 1 : g_assert_no_error (error);
261 [ - + ]: 1 : g_assert_true (ret);
262 : :
263 : 1 : g_main_loop_quit (test->loop);
264 : 1 : }
265 : :
266 : : static void
267 : 1 : test_write (Test *test,
268 : : gconstpointer unused)
269 : : {
270 : : GHashTable *attributes;
271 : : SecretValue *value;
272 : 1 : GError *error = NULL;
273 : : gboolean ret;
274 : :
275 : 1 : attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
276 : 1 : g_hash_table_insert (attributes, g_strdup ("foo"), g_strdup ("a"));
277 : 1 : g_hash_table_insert (attributes, g_strdup ("bar"), g_strdup ("b"));
278 : 1 : g_hash_table_insert (attributes, g_strdup ("baz"), g_strdup ("c"));
279 : :
280 : 1 : value = secret_value_new ("test1", -1, "text/plain");
281 : 1 : ret = secret_file_collection_replace (test->collection,
282 : : attributes, "label1", value,
283 : : &error);
284 [ - + ]: 1 : g_assert_no_error (error);
285 [ - + ]: 1 : g_assert_true (ret);
286 : 1 : secret_value_unref (value);
287 : 1 : g_hash_table_unref (attributes);
288 : :
289 : 1 : attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
290 : 1 : g_hash_table_insert (attributes, g_strdup ("apple"), g_strdup ("a"));
291 : 1 : g_hash_table_insert (attributes, g_strdup ("orange"), g_strdup ("b"));
292 : 1 : g_hash_table_insert (attributes, g_strdup ("banana"), g_strdup ("c"));
293 : :
294 : 1 : value = secret_value_new ("test1", -1, "text/plain");
295 : 1 : ret = secret_file_collection_replace (test->collection,
296 : : attributes, "label2", value,
297 : : &error);
298 [ - + ]: 1 : g_assert_no_error (error);
299 [ - + ]: 1 : g_assert_true (ret);
300 : 1 : secret_value_unref (value);
301 : 1 : g_hash_table_unref (attributes);
302 : :
303 : 1 : secret_file_collection_write (test->collection,
304 : : NULL,
305 : : on_write,
306 : : test);
307 : :
308 : 1 : g_main_loop_run (test->loop);
309 : 1 : }
310 : :
311 : : static void
312 : 1 : test_read (Test *test,
313 : : gconstpointer unused)
314 : : {
315 : : GHashTable *attributes;
316 : : SecretValue *value;
317 : 1 : GError *error = NULL;
318 : : GList *matches;
319 : : SecretFileItem *item;
320 : : const gchar *secret;
321 : : gsize n_secret;
322 : : gchar *label;
323 : :
324 : 1 : attributes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
325 : 1 : g_hash_table_insert (attributes, g_strdup ("foo"), g_strdup ("a"));
326 : :
327 : 1 : matches = secret_file_collection_search (test->collection, attributes);
328 [ - + ]: 1 : g_assert_cmpint (g_list_length (matches), ==, 1);
329 : :
330 : 1 : item = _secret_file_item_decrypt ((GVariant *)matches->data,
331 : : test->collection,
332 : : &error);
333 : 1 : g_list_free_full (matches, (GDestroyNotify)g_variant_unref);
334 [ - + ]: 1 : g_assert_no_error (error);
335 [ - + ]: 1 : g_assert_nonnull (item);
336 : :
337 : 1 : g_object_get (item, "label", &label, NULL);
338 [ - + ]: 1 : g_assert_cmpstr (label, ==, "label1");
339 : 1 : g_free (label);
340 : :
341 : 1 : value = secret_retrievable_retrieve_secret_sync (SECRET_RETRIEVABLE (item),
342 : : NULL,
343 : : &error);
344 [ - + ]: 1 : g_assert_no_error (error);
345 : :
346 : 1 : secret = secret_value_get (value, &n_secret);
347 [ - + ]: 1 : g_assert_cmpstr (secret, ==, "test1");
348 : :
349 : 1 : secret_value_unref (value);
350 : 1 : g_object_unref (item);
351 : 1 : g_hash_table_unref (attributes);
352 : 1 : }
353 : :
354 : : int
355 : 1 : main (int argc, char **argv)
356 : : {
357 : 1 : g_test_init (&argc, &argv, NULL);
358 : 1 : g_set_prgname ("test-file-collection");
359 : 1 : g_test_add ("/file-collection/init", Test, NULL, setup, test_init, teardown);
360 : 1 : g_test_add ("/file-collection/replace", Test, NULL, setup, test_replace, teardown);
361 : 1 : g_test_add ("/file-collection/clear", Test, NULL, setup, test_clear, teardown);
362 : 1 : g_test_add ("/file-collection/search", Test, NULL, setup, test_search, teardown);
363 : 1 : g_test_add ("/file-collection/decrypt", Test, NULL, setup, test_decrypt, teardown);
364 : 1 : g_test_add ("/file-collection/write", Test, NULL, setup, test_write, teardown);
365 : 1 : g_test_add ("/file-collection/read", Test, "default.keyring", setup, test_read, teardown);
366 : :
367 : 1 : return egg_tests_run_with_loop ();
368 : : }
|