Merge lp:~mardy/signon-keyring-extension/mock-keyring into lp:signon-keyring-extension

Proposed by Alberto Mardegan
Status: Merged
Approved by: David King
Approved revision: 14
Merged at revision: 14
Proposed branch: lp:~mardy/signon-keyring-extension/mock-keyring
Merge into: lp:signon-keyring-extension
Diff against target: 417 lines (+345/-18)
4 files modified
tests/keyring-test.cpp (+3/-17)
tests/mocked-gnome-keyring.c (+304/-0)
tests/mocked-gnome-keyring.h (+36/-0)
tests/tests.pro (+2/-1)
To merge this branch: bzr merge lp:~mardy/signon-keyring-extension/mock-keyring
Reviewer Review Type Date Requested Status
David King (community) Approve
jenkins (community) continuous-integration Approve
Review via email: mp+119524@code.launchpad.net

Description of the change

Don't use the real GNOME keyring when running tests

Mock the keyring APIs which we are using and act on a fake in-memory keyring.

To post a comment you must log in.
Revision history for this message
jenkins (martin-mrazik+qa) wrote :
review: Approve (continuous-integration)
Revision history for this message
David King (amigadave) wrote :

Built and tested fine, looks good!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'tests/keyring-test.cpp'
2--- tests/keyring-test.cpp 2011-12-01 13:40:52 +0000
3+++ tests/keyring-test.cpp 2012-08-14 12:29:50 +0000
4@@ -22,37 +22,25 @@
5 */
6
7 #include "keyring-test.h"
8+#include "mocked-gnome-keyring.h"
9 #include "secrets-storage.h"
10
11 #include <QDebug>
12 #include <QTest>
13
14-#include <gnome-keyring.h>
15-
16-/* Let's hope I won't overwrite my credentials when running these tests :-) */
17 #define TEST_CREDENTIALS_ID 0x7fffffff
18
19-#define TEST_KEYRING "signontest"
20-
21 void KeyringTest::initTestCase()
22 {
23 qDebug() << Q_FUNC_INFO;
24
25+ mocked_gnome_keyring_init();
26+
27 plugin = new KeyringPlugin;
28 QVERIFY(plugin != 0);
29
30 storage = plugin->secretsStorage(this);
31 QVERIFY(storage != 0);
32-
33- /* create the keyring for running the tests */
34- GnomeKeyringResult ret;
35- ret = gnome_keyring_create_sync(TEST_KEYRING, "testt3st");
36- if (ret == GNOME_KEYRING_RESULT_OK) {
37- ret = gnome_keyring_unlock_sync(TEST_KEYRING, "testt3st");
38- QCOMPARE(ret, GNOME_KEYRING_RESULT_OK);
39- } else {
40- QCOMPARE(ret, GNOME_KEYRING_RESULT_KEYRING_ALREADY_EXISTS);
41- }
42 }
43
44 void KeyringTest::init()
45@@ -186,8 +174,6 @@
46 {
47 delete storage;
48 delete plugin;
49-
50- gnome_keyring_delete_sync(TEST_KEYRING);
51 }
52
53 QTEST_MAIN(KeyringTest)
54
55=== added file 'tests/mocked-gnome-keyring.c'
56--- tests/mocked-gnome-keyring.c 1970-01-01 00:00:00 +0000
57+++ tests/mocked-gnome-keyring.c 2012-08-14 12:29:50 +0000
58@@ -0,0 +1,304 @@
59+/*
60+ * This file is part of the GNOME-keyring signond extension
61+ *
62+ * Copyright (C) 2012 Canonical Ltd.
63+ *
64+ * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
65+ *
66+ * This library is free software; you can redistribute it and/or
67+ * modify it under the terms of the GNU Lesser General Public License
68+ * version 2.1 as published by the Free Software Foundation.
69+ *
70+ * This library is distributed in the hope that it will be useful, but
71+ * WITHOUT ANY WARRANTY; without even the implied warranty of
72+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
73+ * Lesser General Public License for more details.
74+ *
75+ * You should have received a copy of the GNU Lesser General Public
76+ * License along with this library; if not, write to the Free Software
77+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
78+ * 02110-1301 USA
79+ */
80+
81+#include "mocked-gnome-keyring.h"
82+#include <gnome-keyring.h>
83+
84+#define DEFAULT_KEYRING "mock-keyring"
85+
86+static GHashTable *keyrings = NULL;
87+static guint32 last_item_id = 123;
88+
89+typedef struct {
90+ guint32 id;
91+ GnomeKeyringItemType type;
92+ char *display_name;
93+ GnomeKeyringAttributeList *attributes;
94+ char *secret;
95+} MockedItem;
96+
97+typedef struct {
98+ GList *items;
99+} MockedKeyring;
100+
101+static MockedItem *
102+mocked_item_new(GnomeKeyringItemType type,
103+ const char *display_name,
104+ GnomeKeyringAttributeList *attributes,
105+ const char *secret)
106+{
107+ MockedItem *item = g_slice_new0(MockedItem);
108+ item->id = last_item_id++;
109+ item->type = type;
110+ item->display_name = g_strdup(display_name);
111+ item->attributes = gnome_keyring_attribute_list_copy(attributes);
112+ item->secret = g_strdup(secret);
113+ return item;
114+}
115+
116+static gboolean
117+attributes_are_equal (GnomeKeyringAttribute *a1,
118+ GnomeKeyringAttribute *a2)
119+{
120+ if (g_strcmp0(a1->name, a2->name) != 0) return FALSE;
121+ if (a1->type != a2->type) return FALSE;
122+ if (a1->type == GNOME_KEYRING_ATTRIBUTE_TYPE_STRING) {
123+ return g_strcmp0(a1->value.string, a2->value.string) == 0;
124+ } else {
125+ return a1->value.integer == a2->value.integer;
126+ }
127+}
128+
129+static gboolean
130+mocked_item_has_attributes(MockedItem *item,
131+ GnomeKeyringAttributeList *attributes)
132+{
133+ guint i, j;
134+
135+ for (i = 0; i < attributes->len; i++) {
136+ gboolean found = FALSE;
137+ GnomeKeyringAttribute *required =
138+ &g_array_index(attributes, GnomeKeyringAttribute, i);
139+
140+ for (j = 0; j < item->attributes->len; j++) {
141+ GnomeKeyringAttribute *attribute =
142+ &g_array_index(item->attributes, GnomeKeyringAttribute, j);
143+ if (attributes_are_equal(attribute, required)) {
144+ found = TRUE;
145+ break;
146+ }
147+ }
148+
149+ if (!found) return FALSE;
150+ }
151+
152+ return TRUE;
153+}
154+
155+static void
156+mocked_item_free(MockedItem *item)
157+{
158+ g_free(item->display_name);
159+ gnome_keyring_attribute_list_free(item->attributes);
160+ g_free(item->secret);
161+ g_slice_free(MockedItem, item);
162+}
163+
164+static MockedItem *
165+mocked_keyring_get_item(MockedKeyring *keyring, guint32 item_id)
166+{
167+ GList *list;
168+
169+ for (list = keyring->items; list != NULL; list = g_list_next(list)) {
170+ MockedItem *item = list->data;
171+ if (item->id == item_id) return item;
172+ }
173+
174+ return NULL;
175+}
176+
177+static MockedKeyring *
178+mocked_keyring_new()
179+{
180+ return g_slice_new0(MockedKeyring);
181+}
182+
183+void
184+mocked_gnome_keyring_init()
185+{
186+ keyrings = g_hash_table_new(g_str_hash, g_str_equal);
187+ g_hash_table_insert(keyrings, DEFAULT_KEYRING, mocked_keyring_new());
188+ /* create the keyring for use with the unit tests */
189+ g_hash_table_insert(keyrings, TEST_KEYRING, mocked_keyring_new());
190+}
191+
192+GnomeKeyringResult
193+gnome_keyring_get_default_keyring_sync(char **keyring)
194+{
195+ *keyring = g_strdup(DEFAULT_KEYRING);
196+ return GNOME_KEYRING_RESULT_OK;
197+}
198+
199+GnomeKeyringResult
200+gnome_keyring_item_create_sync(const char *keyring,
201+ GnomeKeyringItemType type,
202+ const char *display_name,
203+ GnomeKeyringAttributeList *attributes,
204+ const char *secret,
205+ gboolean update_if_exists,
206+ guint32 *item_id)
207+{
208+ GList *list, *found = NULL;
209+ GnomeKeyringFound *existing_result = NULL;
210+ MockedKeyring *mocked_keyring;
211+
212+ g_return_val_if_fail (keyring != NULL,
213+ GNOME_KEYRING_RESULT_BAD_ARGUMENTS);
214+ g_return_val_if_fail (type < GNOME_KEYRING_ITEM_LAST_TYPE,
215+ GNOME_KEYRING_RESULT_BAD_ARGUMENTS);
216+
217+ mocked_keyring = g_hash_table_lookup (keyrings, keyring);
218+ if (G_UNLIKELY (mocked_keyring == NULL))
219+ return GNOME_KEYRING_RESULT_NO_SUCH_KEYRING;
220+
221+ gnome_keyring_find_items_sync(type, attributes, &found);
222+ for (list = found; list != NULL; list = g_list_next(list)) {
223+ GnomeKeyringFound *result = list->data;
224+ if (g_strcmp0(result->keyring, keyring) == 0) {
225+ existing_result = result;
226+ break;
227+ }
228+ }
229+ gnome_keyring_found_list_free(found);
230+
231+ if (existing_result != NULL) {
232+ if (!update_if_exists) return GNOME_KEYRING_RESULT_ALREADY_EXISTS;
233+
234+ /* Update the existing item */
235+ MockedItem *item = mocked_keyring_get_item(mocked_keyring,
236+ existing_result->item_id);
237+ g_assert(item != NULL);
238+
239+ g_free(item->display_name);
240+ item->display_name = g_strdup(display_name);
241+
242+ g_free(item->secret);
243+ item->secret = g_strdup(secret);
244+
245+ if (item_id != NULL)
246+ *item_id = item->id;
247+ } else {
248+ MockedItem *item =
249+ mocked_item_new(type, display_name, attributes, secret);
250+ if (item_id != NULL)
251+ *item_id = item->id;
252+
253+ mocked_keyring->items = g_list_prepend(mocked_keyring->items,
254+ item);
255+ }
256+ return GNOME_KEYRING_RESULT_OK;
257+}
258+
259+GnomeKeyringResult
260+gnome_keyring_find_items_sync(GnomeKeyringItemType type,
261+ GnomeKeyringAttributeList *attributes,
262+ GList **found)
263+{
264+ GHashTableIter iter;
265+ const gchar *keyring_name;
266+ MockedKeyring *keyring;
267+ GList *results = NULL;
268+
269+ g_return_val_if_fail (type < GNOME_KEYRING_ITEM_LAST_TYPE,
270+ GNOME_KEYRING_RESULT_BAD_ARGUMENTS);
271+
272+ g_hash_table_iter_init(&iter, keyrings);
273+ while (g_hash_table_iter_next(&iter,
274+ (gpointer)&keyring_name,
275+ (gpointer)&keyring)) {
276+ GList *list;
277+
278+ for (list = keyring->items; list != NULL; list = g_list_next(list)) {
279+ GnomeKeyringFound *found;
280+ MockedItem *item = list->data;
281+ if (item->type != type) continue;
282+
283+ if (!mocked_item_has_attributes(item, attributes)) continue;
284+
285+ found = g_slice_new0(GnomeKeyringFound);
286+ found->keyring = g_strdup(keyring_name);
287+ found->item_id = item->id;
288+ found->attributes =
289+ gnome_keyring_attribute_list_copy(item->attributes);
290+ found->secret = g_strdup(item->secret);
291+ results = g_list_prepend(results, found);
292+ }
293+ }
294+
295+ *found = results;
296+ return results != NULL ?
297+ GNOME_KEYRING_RESULT_OK : GNOME_KEYRING_RESULT_NO_MATCH;
298+}
299+
300+void
301+gnome_keyring_found_free(GnomeKeyringFound *found)
302+{
303+ g_free(found->keyring);
304+ gnome_keyring_attribute_list_free(found->attributes);
305+ g_free(found->secret);
306+ g_slice_free(GnomeKeyringFound, found);
307+}
308+
309+GnomeKeyringResult
310+gnome_keyring_item_get_info_sync(const char *keyring,
311+ guint32 id,
312+ GnomeKeyringItemInfo **info)
313+{
314+ GnomeKeyringItemInfo *result;
315+ MockedKeyring *mocked_keyring;
316+ MockedItem *item;
317+
318+ g_return_val_if_fail (keyring != NULL,
319+ GNOME_KEYRING_RESULT_BAD_ARGUMENTS);
320+
321+ mocked_keyring = g_hash_table_lookup (keyrings, keyring);
322+ if (G_UNLIKELY (mocked_keyring == NULL))
323+ return GNOME_KEYRING_RESULT_NO_SUCH_KEYRING;
324+
325+ item = mocked_keyring_get_item(mocked_keyring, id);
326+ if (item == NULL) return GNOME_KEYRING_RESULT_NO_MATCH;
327+
328+ result = gnome_keyring_item_info_new();
329+ gnome_keyring_item_info_set_type(result, item->type);
330+ gnome_keyring_item_info_set_display_name(result, item->display_name);
331+ gnome_keyring_item_info_set_secret(result, item->secret);
332+ *info = result;
333+ return GNOME_KEYRING_RESULT_OK;
334+}
335+
336+GnomeKeyringResult
337+gnome_keyring_item_delete_sync(const char *keyring,
338+ guint32 item_id)
339+{
340+ MockedKeyring *mocked_keyring;
341+ MockedItem *item;
342+ GList *list;
343+
344+ g_return_val_if_fail (keyring != NULL,
345+ GNOME_KEYRING_RESULT_BAD_ARGUMENTS);
346+
347+ mocked_keyring = g_hash_table_lookup (keyrings, keyring);
348+ if (G_UNLIKELY (mocked_keyring == NULL))
349+ return GNOME_KEYRING_RESULT_NO_SUCH_KEYRING;
350+
351+ for (list = mocked_keyring->items;
352+ list != NULL;
353+ list = g_list_next(list)) {
354+ item = list->data;
355+ if (item->id == item_id) break;
356+ }
357+
358+ if (list == NULL) return GNOME_KEYRING_RESULT_NO_MATCH;
359+ mocked_item_free(item);
360+ mocked_keyring->items = g_list_delete_link(mocked_keyring->items, list);
361+ return GNOME_KEYRING_RESULT_OK;
362+}
363
364=== added file 'tests/mocked-gnome-keyring.h'
365--- tests/mocked-gnome-keyring.h 1970-01-01 00:00:00 +0000
366+++ tests/mocked-gnome-keyring.h 2012-08-14 12:29:50 +0000
367@@ -0,0 +1,36 @@
368+/*
369+ * This file is part of the GNOME-keyring signond extension
370+ *
371+ * Copyright (C) 2012 Canonical Ltd.
372+ *
373+ * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
374+ *
375+ * This library is free software; you can redistribute it and/or
376+ * modify it under the terms of the GNU Lesser General Public License
377+ * version 2.1 as published by the Free Software Foundation.
378+ *
379+ * This library is distributed in the hope that it will be useful, but
380+ * WITHOUT ANY WARRANTY; without even the implied warranty of
381+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
382+ * Lesser General Public License for more details.
383+ *
384+ * You should have received a copy of the GNU Lesser General Public
385+ * License along with this library; if not, write to the Free Software
386+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
387+ * 02110-1301 USA
388+ */
389+
390+#ifndef MOCKED_GNOME_KEYRING_H
391+#define MOCKED_GNOME_KEYRING_H
392+
393+#include <glib.h>
394+
395+G_BEGIN_DECLS
396+
397+#define TEST_KEYRING "signontest"
398+
399+void mocked_gnome_keyring_init();
400+
401+G_END_DECLS
402+
403+#endif // MOCKED_GNOME_KEYRING_H
404
405=== modified file 'tests/tests.pro'
406--- tests/tests.pro 2011-11-21 12:46:50 +0000
407+++ tests/tests.pro 2012-08-14 12:29:50 +0000
408@@ -21,7 +21,8 @@
409 keyring-test.h
410
411 SOURCES = \
412- keyring-test.cpp
413+ keyring-test.cpp \
414+ mocked-gnome-keyring.c \
415
416 INCLUDEPATH += \
417 $${TOP_SRC_DIR}/src

Subscribers

People subscribed via source and target branches