~ubuntu-branches/ubuntu/natty/gnome-keyring/natty

« back to all changes in this revision

Viewing changes to library/tests/unit-test-keyrings-prompt.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-02-16 19:00:06 UTC
  • mfrom: (1.1.58 upstream)
  • Revision ID: james.westby@ubuntu.com-20100216190006-cqpnic4zxlkmmi0o
Tags: 2.29.90git20100218-0ubuntu1
Updated to a git snapshot version

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
 
/* unit-test-keyrings-prompt.c: Test basic prompt functionality
3
 
 
4
 
   Copyright (C) 2007 Stefan Walter
5
 
 
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.
10
 
 
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.
15
 
 
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.
20
 
 
21
 
   Author: Stef Walter <stef@memberwebs.com>
22
 
*/
23
 
 
24
 
#include <stdlib.h>
25
 
#include <stdio.h>
26
 
#include <string.h>
27
 
#include <unistd.h>
28
 
 
29
 
#include "run-prompt-test.h"
30
 
 
31
 
#include "library/gnome-keyring.h"
32
 
 
33
 
static void 
34
 
TELL(const char* what)
35
 
{
36
 
        printf("INTERACTION: %s\n", what);
37
 
}
38
 
 
39
 
 
40
 
gchar* default_keyring = NULL;
41
 
 
42
 
#define KEYRING_NAME "unit-test-keyring"
43
 
#define DISPLAY_NAME "Item Display Name"
44
 
#define SECRET "item-secret"
45
 
 
46
 
DEFINE_TEST(stash_default)
47
 
{
48
 
        GnomeKeyringResult res;
49
 
        res = gnome_keyring_get_default_keyring_sync (&default_keyring);
50
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
51
 
}
52
 
 
53
 
DEFINE_TEST(create_prompt_keyring)
54
 
{
55
 
        GnomeKeyringResult res;
56
 
 
57
 
        TELL("press 'DENY'");
58
 
        res = gnome_keyring_create_sync (KEYRING_NAME, NULL);
59
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_DENIED, ==, res);
60
 
        
61
 
        TELL("type in a new keyring password and click 'OK'");
62
 
        
63
 
        res = gnome_keyring_create_sync (KEYRING_NAME, NULL);
64
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
65
 
 
66
 
        res = gnome_keyring_create_sync (KEYRING_NAME, NULL);
67
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_ALREADY_EXISTS, ==, res);
68
 
        
69
 
        res = gnome_keyring_set_default_keyring_sync (KEYRING_NAME);
70
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
71
 
}
72
 
 
73
 
DEFINE_TEST(change_prompt_keyring)
74
 
{
75
 
        GnomeKeyringResult res;
76
 
 
77
 
        TELL("press 'DENY' here");      
78
 
 
79
 
        res = gnome_keyring_change_password_sync (KEYRING_NAME, NULL, NULL); 
80
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_DENIED, ==, res);
81
 
        
82
 
        TELL("type in original password then new keyring password and click 'OK'");
83
 
 
84
 
        res = gnome_keyring_change_password_sync (KEYRING_NAME, NULL, NULL); 
85
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
86
 
}
87
 
 
88
 
DEFINE_TEST(acls)
89
 
{
90
 
        GnomeKeyringResult res;
91
 
        GnomeKeyringAccessControl *ac, *acl;
92
 
        GnomeKeyringItemInfo *info;
93
 
        GList *acls, *l;
94
 
        guint id;
95
 
        gchar *prog;
96
 
        
97
 
        /* Create teh item */
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);
101
 
        
102
 
        /* Get the ACLs */
103
 
        gnome_keyring_item_get_acl_sync (KEYRING_NAME, id, &acls);
104
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
105
 
 
106
 
        /* Make sure we're in the list, since we created */
107
 
        prog = g_get_prgname ();
108
 
        acl = NULL;
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)) {
112
 
                        acl = ac;
113
 
                        break;
114
 
                }
115
 
        }
116
 
        
117
 
        /* "couldn't find ACL for this process on new item" */
118
 
        g_assert (acl != NULL);
119
 
        
120
 
        /* Now remove all ACLs from the item */
121
 
        l = NULL;
122
 
        gnome_keyring_item_set_acl_sync (KEYRING_NAME, id, l);
123
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
124
 
        
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);
131
 
        sleep(2);
132
 
 
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");
136
 
#endif
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);
141
 
        
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);
147
 
        
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);
152
 
        sleep(2);       
153
 
#endif
154
 
}
155
 
 
156
 
DEFINE_TEST(application_secret)
157
 
{
158
 
        GnomeKeyringResult res;
159
 
        GnomeKeyringItemInfo *info;
160
 
        GList *acls;
161
 
        guint id;
162
 
        
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);
168
 
 
169
 
        /* Remove all ACLs from the item */
170
 
        acls = NULL;
171
 
        gnome_keyring_item_set_acl_sync (KEYRING_NAME, id, acls);
172
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
173
 
        
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);
178
 
        sleep(2);
179
 
 
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);
184
 
        sleep(2);
185
 
}
186
 
 
187
 
DEFINE_TEST(unlock_prompt)
188
 
{
189
 
        GnomeKeyringResult res;
190
 
        
191
 
        res = gnome_keyring_lock_all_sync ();
192
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
193
 
 
194
 
        TELL("press 'DENY' here");
195
 
        res = gnome_keyring_unlock_sync (KEYRING_NAME, NULL);
196
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_DENIED, ==, res);
197
 
 
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);
201
 
}
202
 
 
203
 
DEFINE_TEST(find_locked)
204
 
{
205
 
        GnomeKeyringResult res;
206
 
        GnomeKeyringAttributeList* attrs;
207
 
        guint id;
208
 
        GList *found;
209
 
        
210
 
        GTimeVal tv;
211
 
        guint32 unique;
212
 
        
213
 
        /* Make a unique value */
214
 
        g_get_current_time (&tv);
215
 
        unique = ((guint32)tv.tv_sec) ^ ((guint32)tv.tv_usec);
216
 
        
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);
222
 
 
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);
227
 
        
228
 
        /* Lock the keyring ... */
229
 
        res = gnome_keyring_lock_all_sync ();
230
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
231
 
 
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);
236
 
 
237
 
        /* "Wrong number of items found" */
238
 
        g_assert_cmpint (g_list_length (found), ==, 1);
239
 
}
240
 
 
241
 
DEFINE_TEST(get_info_locked)
242
 
{
243
 
        GnomeKeyringResult res;
244
 
        GnomeKeyringItemInfo *info;
245
 
        guint id;
246
 
        
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);
251
 
        
252
 
        /* Lock the keyring ... */
253
 
        res = gnome_keyring_lock_all_sync ();
254
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
255
 
 
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);
260
 
}
261
 
 
262
 
DEFINE_TEST(cleanup)
263
 
{
264
 
        GnomeKeyringResult res;
265
 
        
266
 
        res = gnome_keyring_delete_sync (KEYRING_NAME);
267
 
        g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
268
 
 
269
 
        if (default_keyring) {
270
 
                res = gnome_keyring_set_default_keyring_sync (default_keyring);
271
 
                g_assert_cmpint (GNOME_KEYRING_RESULT_OK, ==, res);
272
 
        }       
273
 
}