1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
/* unit-test-keyrings-prompt.c: Test basic prompt functionality
4
Copyright (C) 2007 Stefan Walter
6
The Gnome Keyring Library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public License as
8
published by the Free Software Foundation; either version 2 of the
9
License, or (at your option) any later version.
11
The Gnome Keyring Library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU Library General Public
17
License along with the Gnome Library; see the file COPYING.LIB. If not,
18
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
Boston, MA 02111-1307, USA.
21
Author: Stef Walter <stef@memberwebs.com>
29
#include "run-prompt-test.h"
31
#include "library/gnome-keyring.h"
34
TELL(const char* what)
36
printf("INTERACTION: %s\n", what);
40
gchar* default_keyring = NULL;
42
#define KEYRING_NAME "unit-test-keyring"
43
#define DISPLAY_NAME "Item Display Name"
44
#define SECRET "item-secret"
46
DEFINE_TEST(stash_default)
48
GnomeKeyringResult res;
49
res = gnome_keyring_get_default_keyring_sync (&default_keyring);
50
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
53
DEFINE_TEST(create_prompt_keyring)
55
GnomeKeyringResult res;
58
res = gnome_keyring_create_sync (KEYRING_NAME, NULL);
59
g_assert_cmpint (GNOME_KEYRING_RESULT_DENIED, ==, res);
61
TELL("type in a new keyring password and click 'OK'");
63
res = gnome_keyring_create_sync (KEYRING_NAME, NULL);
64
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
66
res = gnome_keyring_create_sync (KEYRING_NAME, NULL);
67
g_assert_cmpint (GNOME_KEYRING_RESULT_ALREADY_EXISTS, ==, res);
69
res = gnome_keyring_set_default_keyring_sync (KEYRING_NAME);
70
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
73
DEFINE_TEST(change_prompt_keyring)
75
GnomeKeyringResult res;
77
TELL("press 'DENY' here");
79
res = gnome_keyring_change_password_sync (KEYRING_NAME, NULL, NULL);
80
g_assert_cmpint (GNOME_KEYRING_RESULT_DENIED, ==, res);
82
TELL("type in original password then new keyring password and click 'OK'");
84
res = gnome_keyring_change_password_sync (KEYRING_NAME, NULL, NULL);
85
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
90
GnomeKeyringResult res;
91
GnomeKeyringAccessControl *ac, *acl;
92
GnomeKeyringItemInfo *info;
98
res = gnome_keyring_item_create_sync (KEYRING_NAME, GNOME_KEYRING_ITEM_GENERIC_SECRET,
99
"Fry", NULL, "secret", FALSE, &id);
100
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
103
gnome_keyring_item_get_acl_sync (KEYRING_NAME, id, &acls);
104
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
106
/* Make sure we're in the list, since we created */
107
prog = g_get_prgname ();
109
for (l = acls; l; l = g_list_next (l)) {
110
ac = (GnomeKeyringAccessControl*)l->data;
111
if (strstr (gnome_keyring_item_ac_get_path_name (ac), prog)) {
117
/* "couldn't find ACL for this process on new item" */
118
g_assert (acl != NULL);
120
/* Now remove all ACLs from the item */
122
gnome_keyring_item_set_acl_sync (KEYRING_NAME, id, l);
123
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
125
/* Shouldn't be prompted here, not accessing secrets */
126
TELL("No prompt should show up at this point");
127
res = gnome_keyring_item_get_info_full_sync (KEYRING_NAME, id, GNOME_KEYRING_ITEM_INFO_BASICS, &info);
128
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
129
/* "returned a secret when it shouldn't have" */
130
g_assert (gnome_keyring_item_info_get_secret (info) == NULL);
133
/* Now try to read the item, should be prompted */
134
#ifdef ENABLE_ACL_PROMPTS
135
TELL("Press 'Allow Once' to give program access to the data");
137
res = gnome_keyring_item_get_info_sync (KEYRING_NAME, id, &info);
138
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
139
/* "didn't return a secret when it should have" */
140
g_assert (gnome_keyring_item_info_get_secret (info) != NULL);
142
#ifdef ENABLE_ACL_PROMPTS
143
/* Now try to read the item again, give forever access */
144
TELL("Press 'Always Allow' to give program access to the data");
145
res = gnome_keyring_item_get_info_sync (KEYRING_NAME, id, &info);
146
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
148
/* Now try to read the item, should be prompted */
149
TELL("No prompt should show up at this point");
150
res = gnome_keyring_item_get_info_sync (KEYRING_NAME, id, &info);
151
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
156
DEFINE_TEST(application_secret)
158
GnomeKeyringResult res;
159
GnomeKeyringItemInfo *info;
163
/* Create teh item */
164
res = gnome_keyring_item_create_sync (KEYRING_NAME,
165
GNOME_KEYRING_ITEM_GENERIC_SECRET | GNOME_KEYRING_ITEM_APPLICATION_SECRET,
166
"Fry", NULL, "secret", FALSE, &id);
167
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
169
/* Remove all ACLs from the item */
171
gnome_keyring_item_set_acl_sync (KEYRING_NAME, id, acls);
172
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
174
/* Shouldn't be prompted here, not accessing secrets */
175
TELL("No prompt should show up at this point");
176
res = gnome_keyring_item_get_info_full_sync (KEYRING_NAME, id, GNOME_KEYRING_ITEM_INFO_BASICS, &info);
177
g_assert_cmpint (GNOME_KEYRING_RESULT_DENIED, ==, res);
180
/* Now try to read the item, should be prompted */
181
TELL("No prompt should show up at this point");
182
res = gnome_keyring_item_get_info_sync (KEYRING_NAME, id, &info);
183
g_assert_cmpint (GNOME_KEYRING_RESULT_DENIED, ==, res);
187
DEFINE_TEST(unlock_prompt)
189
GnomeKeyringResult res;
191
res = gnome_keyring_lock_all_sync ();
192
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
194
TELL("press 'DENY' here");
195
res = gnome_keyring_unlock_sync (KEYRING_NAME, NULL);
196
g_assert_cmpint (GNOME_KEYRING_RESULT_DENIED, ==, res);
198
TELL("type in keyring password and click 'OK'");
199
res = gnome_keyring_unlock_sync (KEYRING_NAME, NULL);
200
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
203
DEFINE_TEST(find_locked)
205
GnomeKeyringResult res;
206
GnomeKeyringAttributeList* attrs;
213
/* Make a unique value */
214
g_get_current_time (&tv);
215
unique = ((guint32)tv.tv_sec) ^ ((guint32)tv.tv_usec);
217
attrs = gnome_keyring_attribute_list_new ();
218
gnome_keyring_attribute_list_append_string (attrs, "dog", "barks");
219
gnome_keyring_attribute_list_append_string (attrs, "bird", "tweets");
220
gnome_keyring_attribute_list_append_string (attrs, "iguana", "silence");
221
gnome_keyring_attribute_list_append_uint32 (attrs, "num", unique);
223
/* Create teh item */
224
res = gnome_keyring_item_create_sync (NULL, GNOME_KEYRING_ITEM_GENERIC_SECRET,
225
"Yay!", attrs, SECRET, FALSE, &id);
226
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
228
/* Lock the keyring ... */
229
res = gnome_keyring_lock_all_sync ();
230
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
232
/* Now, try to access the item */
233
TELL("type in keyring password and click 'OK'");
234
res = gnome_keyring_find_items_sync (GNOME_KEYRING_ITEM_GENERIC_SECRET, attrs, &found);
235
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
237
/* "Wrong number of items found" */
238
g_assert_cmpint (g_list_length (found), ==, 1);
241
DEFINE_TEST(get_info_locked)
243
GnomeKeyringResult res;
244
GnomeKeyringItemInfo *info;
247
/* Create teh item */
248
res = gnome_keyring_item_create_sync (NULL, GNOME_KEYRING_ITEM_GENERIC_SECRET,
249
"My test locked", NULL, SECRET, FALSE, &id);
250
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
252
/* Lock the keyring ... */
253
res = gnome_keyring_lock_all_sync ();
254
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
256
/* Now, try to access the item */
257
TELL("type in keyring password and click 'OK'");
258
res = gnome_keyring_item_get_info_sync (NULL, id, &info);
259
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
264
GnomeKeyringResult res;
266
res = gnome_keyring_delete_sync (KEYRING_NAME);
267
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
269
if (default_keyring) {
270
res = gnome_keyring_set_default_keyring_sync (default_keyring);
271
g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);