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
=== modified file 'tests/keyring-test.cpp'
--- tests/keyring-test.cpp 2011-12-01 13:40:52 +0000
+++ tests/keyring-test.cpp 2012-08-14 12:29:50 +0000
@@ -22,37 +22,25 @@
22 */22 */
2323
24#include "keyring-test.h"24#include "keyring-test.h"
25#include "mocked-gnome-keyring.h"
25#include "secrets-storage.h"26#include "secrets-storage.h"
2627
27#include <QDebug>28#include <QDebug>
28#include <QTest>29#include <QTest>
2930
30#include <gnome-keyring.h>
31
32/* Let's hope I won't overwrite my credentials when running these tests :-) */
33#define TEST_CREDENTIALS_ID 0x7fffffff31#define TEST_CREDENTIALS_ID 0x7fffffff
3432
35#define TEST_KEYRING "signontest"
36
37void KeyringTest::initTestCase()33void KeyringTest::initTestCase()
38{34{
39 qDebug() << Q_FUNC_INFO;35 qDebug() << Q_FUNC_INFO;
4036
37 mocked_gnome_keyring_init();
38
41 plugin = new KeyringPlugin;39 plugin = new KeyringPlugin;
42 QVERIFY(plugin != 0);40 QVERIFY(plugin != 0);
4341
44 storage = plugin->secretsStorage(this);42 storage = plugin->secretsStorage(this);
45 QVERIFY(storage != 0);43 QVERIFY(storage != 0);
46
47 /* create the keyring for running the tests */
48 GnomeKeyringResult ret;
49 ret = gnome_keyring_create_sync(TEST_KEYRING, "testt3st");
50 if (ret == GNOME_KEYRING_RESULT_OK) {
51 ret = gnome_keyring_unlock_sync(TEST_KEYRING, "testt3st");
52 QCOMPARE(ret, GNOME_KEYRING_RESULT_OK);
53 } else {
54 QCOMPARE(ret, GNOME_KEYRING_RESULT_KEYRING_ALREADY_EXISTS);
55 }
56}44}
5745
58void KeyringTest::init()46void KeyringTest::init()
@@ -186,8 +174,6 @@
186{174{
187 delete storage;175 delete storage;
188 delete plugin;176 delete plugin;
189
190 gnome_keyring_delete_sync(TEST_KEYRING);
191}177}
192178
193QTEST_MAIN(KeyringTest)179QTEST_MAIN(KeyringTest)
194180
=== added file 'tests/mocked-gnome-keyring.c'
--- tests/mocked-gnome-keyring.c 1970-01-01 00:00:00 +0000
+++ tests/mocked-gnome-keyring.c 2012-08-14 12:29:50 +0000
@@ -0,0 +1,304 @@
1/*
2 * This file is part of the GNOME-keyring signond extension
3 *
4 * Copyright (C) 2012 Canonical Ltd.
5 *
6 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * version 2.1 as published by the Free Software Foundation.
11 *
12 * This library is distributed in the hope that it will be useful, but
13 * 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 St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
23#include "mocked-gnome-keyring.h"
24#include <gnome-keyring.h>
25
26#define DEFAULT_KEYRING "mock-keyring"
27
28static GHashTable *keyrings = NULL;
29static guint32 last_item_id = 123;
30
31typedef struct {
32 guint32 id;
33 GnomeKeyringItemType type;
34 char *display_name;
35 GnomeKeyringAttributeList *attributes;
36 char *secret;
37} MockedItem;
38
39typedef struct {
40 GList *items;
41} MockedKeyring;
42
43static MockedItem *
44mocked_item_new(GnomeKeyringItemType type,
45 const char *display_name,
46 GnomeKeyringAttributeList *attributes,
47 const char *secret)
48{
49 MockedItem *item = g_slice_new0(MockedItem);
50 item->id = last_item_id++;
51 item->type = type;
52 item->display_name = g_strdup(display_name);
53 item->attributes = gnome_keyring_attribute_list_copy(attributes);
54 item->secret = g_strdup(secret);
55 return item;
56}
57
58static gboolean
59attributes_are_equal (GnomeKeyringAttribute *a1,
60 GnomeKeyringAttribute *a2)
61{
62 if (g_strcmp0(a1->name, a2->name) != 0) return FALSE;
63 if (a1->type != a2->type) return FALSE;
64 if (a1->type == GNOME_KEYRING_ATTRIBUTE_TYPE_STRING) {
65 return g_strcmp0(a1->value.string, a2->value.string) == 0;
66 } else {
67 return a1->value.integer == a2->value.integer;
68 }
69}
70
71static gboolean
72mocked_item_has_attributes(MockedItem *item,
73 GnomeKeyringAttributeList *attributes)
74{
75 guint i, j;
76
77 for (i = 0; i < attributes->len; i++) {
78 gboolean found = FALSE;
79 GnomeKeyringAttribute *required =
80 &g_array_index(attributes, GnomeKeyringAttribute, i);
81
82 for (j = 0; j < item->attributes->len; j++) {
83 GnomeKeyringAttribute *attribute =
84 &g_array_index(item->attributes, GnomeKeyringAttribute, j);
85 if (attributes_are_equal(attribute, required)) {
86 found = TRUE;
87 break;
88 }
89 }
90
91 if (!found) return FALSE;
92 }
93
94 return TRUE;
95}
96
97static void
98mocked_item_free(MockedItem *item)
99{
100 g_free(item->display_name);
101 gnome_keyring_attribute_list_free(item->attributes);
102 g_free(item->secret);
103 g_slice_free(MockedItem, item);
104}
105
106static MockedItem *
107mocked_keyring_get_item(MockedKeyring *keyring, guint32 item_id)
108{
109 GList *list;
110
111 for (list = keyring->items; list != NULL; list = g_list_next(list)) {
112 MockedItem *item = list->data;
113 if (item->id == item_id) return item;
114 }
115
116 return NULL;
117}
118
119static MockedKeyring *
120mocked_keyring_new()
121{
122 return g_slice_new0(MockedKeyring);
123}
124
125void
126mocked_gnome_keyring_init()
127{
128 keyrings = g_hash_table_new(g_str_hash, g_str_equal);
129 g_hash_table_insert(keyrings, DEFAULT_KEYRING, mocked_keyring_new());
130 /* create the keyring for use with the unit tests */
131 g_hash_table_insert(keyrings, TEST_KEYRING, mocked_keyring_new());
132}
133
134GnomeKeyringResult
135gnome_keyring_get_default_keyring_sync(char **keyring)
136{
137 *keyring = g_strdup(DEFAULT_KEYRING);
138 return GNOME_KEYRING_RESULT_OK;
139}
140
141GnomeKeyringResult
142gnome_keyring_item_create_sync(const char *keyring,
143 GnomeKeyringItemType type,
144 const char *display_name,
145 GnomeKeyringAttributeList *attributes,
146 const char *secret,
147 gboolean update_if_exists,
148 guint32 *item_id)
149{
150 GList *list, *found = NULL;
151 GnomeKeyringFound *existing_result = NULL;
152 MockedKeyring *mocked_keyring;
153
154 g_return_val_if_fail (keyring != NULL,
155 GNOME_KEYRING_RESULT_BAD_ARGUMENTS);
156 g_return_val_if_fail (type < GNOME_KEYRING_ITEM_LAST_TYPE,
157 GNOME_KEYRING_RESULT_BAD_ARGUMENTS);
158
159 mocked_keyring = g_hash_table_lookup (keyrings, keyring);
160 if (G_UNLIKELY (mocked_keyring == NULL))
161 return GNOME_KEYRING_RESULT_NO_SUCH_KEYRING;
162
163 gnome_keyring_find_items_sync(type, attributes, &found);
164 for (list = found; list != NULL; list = g_list_next(list)) {
165 GnomeKeyringFound *result = list->data;
166 if (g_strcmp0(result->keyring, keyring) == 0) {
167 existing_result = result;
168 break;
169 }
170 }
171 gnome_keyring_found_list_free(found);
172
173 if (existing_result != NULL) {
174 if (!update_if_exists) return GNOME_KEYRING_RESULT_ALREADY_EXISTS;
175
176 /* Update the existing item */
177 MockedItem *item = mocked_keyring_get_item(mocked_keyring,
178 existing_result->item_id);
179 g_assert(item != NULL);
180
181 g_free(item->display_name);
182 item->display_name = g_strdup(display_name);
183
184 g_free(item->secret);
185 item->secret = g_strdup(secret);
186
187 if (item_id != NULL)
188 *item_id = item->id;
189 } else {
190 MockedItem *item =
191 mocked_item_new(type, display_name, attributes, secret);
192 if (item_id != NULL)
193 *item_id = item->id;
194
195 mocked_keyring->items = g_list_prepend(mocked_keyring->items,
196 item);
197 }
198 return GNOME_KEYRING_RESULT_OK;
199}
200
201GnomeKeyringResult
202gnome_keyring_find_items_sync(GnomeKeyringItemType type,
203 GnomeKeyringAttributeList *attributes,
204 GList **found)
205{
206 GHashTableIter iter;
207 const gchar *keyring_name;
208 MockedKeyring *keyring;
209 GList *results = NULL;
210
211 g_return_val_if_fail (type < GNOME_KEYRING_ITEM_LAST_TYPE,
212 GNOME_KEYRING_RESULT_BAD_ARGUMENTS);
213
214 g_hash_table_iter_init(&iter, keyrings);
215 while (g_hash_table_iter_next(&iter,
216 (gpointer)&keyring_name,
217 (gpointer)&keyring)) {
218 GList *list;
219
220 for (list = keyring->items; list != NULL; list = g_list_next(list)) {
221 GnomeKeyringFound *found;
222 MockedItem *item = list->data;
223 if (item->type != type) continue;
224
225 if (!mocked_item_has_attributes(item, attributes)) continue;
226
227 found = g_slice_new0(GnomeKeyringFound);
228 found->keyring = g_strdup(keyring_name);
229 found->item_id = item->id;
230 found->attributes =
231 gnome_keyring_attribute_list_copy(item->attributes);
232 found->secret = g_strdup(item->secret);
233 results = g_list_prepend(results, found);
234 }
235 }
236
237 *found = results;
238 return results != NULL ?
239 GNOME_KEYRING_RESULT_OK : GNOME_KEYRING_RESULT_NO_MATCH;
240}
241
242void
243gnome_keyring_found_free(GnomeKeyringFound *found)
244{
245 g_free(found->keyring);
246 gnome_keyring_attribute_list_free(found->attributes);
247 g_free(found->secret);
248 g_slice_free(GnomeKeyringFound, found);
249}
250
251GnomeKeyringResult
252gnome_keyring_item_get_info_sync(const char *keyring,
253 guint32 id,
254 GnomeKeyringItemInfo **info)
255{
256 GnomeKeyringItemInfo *result;
257 MockedKeyring *mocked_keyring;
258 MockedItem *item;
259
260 g_return_val_if_fail (keyring != NULL,
261 GNOME_KEYRING_RESULT_BAD_ARGUMENTS);
262
263 mocked_keyring = g_hash_table_lookup (keyrings, keyring);
264 if (G_UNLIKELY (mocked_keyring == NULL))
265 return GNOME_KEYRING_RESULT_NO_SUCH_KEYRING;
266
267 item = mocked_keyring_get_item(mocked_keyring, id);
268 if (item == NULL) return GNOME_KEYRING_RESULT_NO_MATCH;
269
270 result = gnome_keyring_item_info_new();
271 gnome_keyring_item_info_set_type(result, item->type);
272 gnome_keyring_item_info_set_display_name(result, item->display_name);
273 gnome_keyring_item_info_set_secret(result, item->secret);
274 *info = result;
275 return GNOME_KEYRING_RESULT_OK;
276}
277
278GnomeKeyringResult
279gnome_keyring_item_delete_sync(const char *keyring,
280 guint32 item_id)
281{
282 MockedKeyring *mocked_keyring;
283 MockedItem *item;
284 GList *list;
285
286 g_return_val_if_fail (keyring != NULL,
287 GNOME_KEYRING_RESULT_BAD_ARGUMENTS);
288
289 mocked_keyring = g_hash_table_lookup (keyrings, keyring);
290 if (G_UNLIKELY (mocked_keyring == NULL))
291 return GNOME_KEYRING_RESULT_NO_SUCH_KEYRING;
292
293 for (list = mocked_keyring->items;
294 list != NULL;
295 list = g_list_next(list)) {
296 item = list->data;
297 if (item->id == item_id) break;
298 }
299
300 if (list == NULL) return GNOME_KEYRING_RESULT_NO_MATCH;
301 mocked_item_free(item);
302 mocked_keyring->items = g_list_delete_link(mocked_keyring->items, list);
303 return GNOME_KEYRING_RESULT_OK;
304}
0305
=== added file 'tests/mocked-gnome-keyring.h'
--- tests/mocked-gnome-keyring.h 1970-01-01 00:00:00 +0000
+++ tests/mocked-gnome-keyring.h 2012-08-14 12:29:50 +0000
@@ -0,0 +1,36 @@
1/*
2 * This file is part of the GNOME-keyring signond extension
3 *
4 * Copyright (C) 2012 Canonical Ltd.
5 *
6 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * version 2.1 as published by the Free Software Foundation.
11 *
12 * This library is distributed in the hope that it will be useful, but
13 * 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 St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
23#ifndef MOCKED_GNOME_KEYRING_H
24#define MOCKED_GNOME_KEYRING_H
25
26#include <glib.h>
27
28G_BEGIN_DECLS
29
30#define TEST_KEYRING "signontest"
31
32void mocked_gnome_keyring_init();
33
34G_END_DECLS
35
36#endif // MOCKED_GNOME_KEYRING_H
037
=== modified file 'tests/tests.pro'
--- tests/tests.pro 2011-11-21 12:46:50 +0000
+++ tests/tests.pro 2012-08-14 12:29:50 +0000
@@ -21,7 +21,8 @@
21 keyring-test.h21 keyring-test.h
2222
23SOURCES = \23SOURCES = \
24 keyring-test.cpp24 keyring-test.cpp \
25 mocked-gnome-keyring.c \
2526
26INCLUDEPATH += \27INCLUDEPATH += \
27 $${TOP_SRC_DIR}/src28 $${TOP_SRC_DIR}/src

Subscribers

People subscribed via source and target branches