~noskcaj/ubuntu/vivid/gnome-keyring/3.15.90

« back to all changes in this revision

Viewing changes to pkcs11/secret-store/tests/test-secret-object.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-14 22:13:02 UTC
  • mfrom: (1.3.1)
  • mto: (80.2.8 experimental) (1.1.77)
  • mto: This revision was merged to the branch mainline in revision 148.
  • Revision ID: package-import@ubuntu.com-20120514221302-0l3gjmqpe6xopond
ImportĀ upstreamĀ versionĀ 3.4.1

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
/*
 
3
   Copyright (C) 2009 Stefan Walter
 
4
 
 
5
   The Gnome Keyring Library is free software; you can redistribute it and/or
 
6
   modify it under the terms of the GNU Library General Public License as
 
7
   published by the Free Software Foundation; either version 2 of the
 
8
   License, or (at your option) any later version.
 
9
 
 
10
   The Gnome Keyring Library is distributed in the hope that it will be useful,
 
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
   Library General Public License for more details.
 
14
 
 
15
   You should have received a copy of the GNU Library General Public
 
16
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
 
17
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
18
   Boston, MA 02111-1307, USA.
 
19
 
 
20
   Author: Stef Walter <stef@memberwebs.com>
 
21
*/
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include "mock-secret-module.h"
 
26
 
 
27
#include "secret-store/gkm-secret-object.h"
 
28
 
 
29
#include "gkm/gkm-transaction.h"
 
30
 
 
31
#include "pkcs11/pkcs11i.h"
 
32
 
 
33
#include <glib.h>
 
34
 
 
35
#include <stdlib.h>
 
36
#include <stdio.h>
 
37
#include <string.h>
 
38
 
 
39
typedef struct {
 
40
        GkmModule *module;
 
41
        GkmSession *session;
 
42
        GkmSecretObject *object;
 
43
} Test;
 
44
 
 
45
static void
 
46
setup (Test *test, gconstpointer unused)
 
47
{
 
48
        test->module = test_secret_module_initialize_and_enter ();
 
49
        test->session = test_secret_module_open_session (TRUE);
 
50
 
 
51
        test->object = g_object_new (GKM_TYPE_SECRET_OBJECT,
 
52
                               "module", test->module,
 
53
                               "identifier", "my-identifier",
 
54
                               NULL);
 
55
 
 
56
        g_assert (GKM_IS_SECRET_OBJECT (test->object));
 
57
}
 
58
 
 
59
static void
 
60
teardown (Test *test, gconstpointer unused)
 
61
{
 
62
        g_object_unref (test->object);
 
63
        test_secret_module_leave_and_finalize ();
 
64
}
 
65
 
 
66
static void
 
67
test_is_locked (Test *test, gconstpointer unused)
 
68
{
 
69
        /* Plain GkmSecretObject is never locked */
 
70
        g_assert (!gkm_secret_object_is_locked (test->object, test->session));
 
71
}
 
72
 
 
73
static void
 
74
test_identifier_prop (Test *test, gconstpointer unused)
 
75
{
 
76
        const gchar *identifier;
 
77
        identifier = gkm_secret_object_get_identifier (test->object);
 
78
        g_assert_cmpstr (identifier, ==, "my-identifier");
 
79
}
 
80
 
 
81
static void
 
82
was_notified (GObject *obj, GParamSpec *pspec, gpointer user_data)
 
83
{
 
84
        gboolean* notified = user_data;
 
85
        g_assert (user_data);
 
86
        *notified = TRUE;
 
87
}
 
88
 
 
89
static void
 
90
test_created_prop (Test *test, gconstpointer unused)
 
91
{
 
92
        glong created;
 
93
 
 
94
        /* Monitor for changes */
 
95
        gboolean notified = FALSE;
 
96
        g_signal_connect (test->object, "notify::created", G_CALLBACK (was_notified), &notified);
 
97
 
 
98
        /* Default value */
 
99
        created = gkm_secret_object_get_created (test->object);
 
100
        g_assert (created == 0);
 
101
 
 
102
        /* Set a new value */
 
103
        gkm_secret_object_set_created (test->object, 1247930171);
 
104
        g_assert (notified);
 
105
        created = gkm_secret_object_get_created (test->object);
 
106
        g_assert (created == 1247930171);
 
107
}
 
108
 
 
109
static void
 
110
test_modified_prop (Test *test, gconstpointer unused)
 
111
{
 
112
        glong modified;
 
113
 
 
114
        /* Monitor for changes */
 
115
        gboolean notified = FALSE;
 
116
        g_signal_connect (test->object, "notify::modified", G_CALLBACK (was_notified), &notified);
 
117
 
 
118
        /* Default value */
 
119
        modified = gkm_secret_object_get_modified (test->object);
 
120
        g_assert (modified == 0);
 
121
 
 
122
        /* Set a new value */
 
123
        gkm_secret_object_set_modified (test->object, 1247930171);
 
124
        g_assert (notified);
 
125
        modified = gkm_secret_object_get_modified (test->object);
 
126
        g_assert (modified == 1247930171);
 
127
}
 
128
 
 
129
static void
 
130
test_was_modified (Test *test, gconstpointer unused)
 
131
{
 
132
        GTimeVal tv;
 
133
        g_get_current_time (&tv);
 
134
        gkm_secret_object_was_modified (test->object);
 
135
        g_assert (tv.tv_sec == gkm_secret_object_get_modified (test->object));
 
136
}
 
137
 
 
138
static void
 
139
test_label_prop (Test *test, gconstpointer unused)
 
140
{
 
141
        const gchar *label;
 
142
 
 
143
        /* Monitor for changes */
 
144
        gboolean notified = FALSE;
 
145
        g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), &notified);
 
146
 
 
147
        /* Default value */
 
148
        label = gkm_secret_object_get_label (test->object);
 
149
        g_assert_cmpstr (label, ==, "");
 
150
 
 
151
        /* Set a new value */
 
152
        gkm_secret_object_set_label (test->object, "hello");
 
153
        g_assert (notified);
 
154
        label = gkm_secret_object_get_label (test->object);
 
155
        g_assert_cmpstr (label, ==, "hello");
 
156
}
 
157
 
 
158
static void
 
159
test_identifier_get_attr (Test *test, gconstpointer unused)
 
160
{
 
161
        gchar buffer[32];
 
162
        CK_ATTRIBUTE attr = { CKA_ID, buffer, 32 };
 
163
        CK_RV rv;
 
164
 
 
165
        rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
 
166
        g_assert (rv == CKR_OK);
 
167
        g_assert (attr.ulValueLen == 13);
 
168
        g_assert (memcmp (buffer, "my-identifier", 13) == 0);
 
169
}
 
170
 
 
171
static void
 
172
test_label_get_attr (Test *test, gconstpointer unused)
 
173
{
 
174
        gchar buffer[32];
 
175
        CK_ATTRIBUTE attr = { CKA_LABEL, buffer, 32 };
 
176
        CK_RV rv;
 
177
 
 
178
        gkm_secret_object_set_label (test->object, "hello");
 
179
        rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
 
180
        g_assert (rv == CKR_OK);
 
181
        g_assert (attr.ulValueLen == 5);
 
182
        g_assert (memcmp (buffer, "hello", 5) == 0);
 
183
}
 
184
 
 
185
static void
 
186
test_label_set_attr (Test *test, gconstpointer unused)
 
187
{
 
188
        CK_ATTRIBUTE attr = { CKA_LABEL, "hello", 5 };
 
189
        GkmTransaction *transaction = gkm_transaction_new ();
 
190
 
 
191
        /* Monitor for changes */
 
192
        gboolean notified = FALSE;
 
193
        g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), &notified);
 
194
 
 
195
        gkm_object_set_attribute (GKM_OBJECT (test->object), test->session, transaction, &attr);
 
196
        g_assert (!gkm_transaction_get_failed (transaction));
 
197
        g_assert (!notified); /* Not notified yet */
 
198
 
 
199
        g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello");
 
200
 
 
201
        gkm_transaction_complete (transaction);
 
202
        g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello");
 
203
        g_assert (notified); /* Notified after transaction complete */
 
204
 
 
205
        g_object_unref (transaction);
 
206
}
 
207
 
 
208
static void
 
209
test_label_set_attr_fail (Test *test, gconstpointer unused)
 
210
{
 
211
        CK_ATTRIBUTE attr = { CKA_LABEL, "hello", 5 };
 
212
        GkmTransaction *transaction = gkm_transaction_new ();
 
213
        gboolean notified = FALSE;
 
214
 
 
215
        /* Set an old value */
 
216
        gkm_secret_object_set_label (test->object, "old");
 
217
 
 
218
        /* Monitor for changes */
 
219
        g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), &notified);
 
220
 
 
221
        /* Set a new value */
 
222
        gkm_object_set_attribute (GKM_OBJECT (test->object), test->session, transaction, &attr);
 
223
        g_assert (!gkm_transaction_get_failed (transaction));
 
224
        g_assert (!notified); /* Not notified yet */
 
225
 
 
226
        /* Temporarily has new value */
 
227
        g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello");
 
228
 
 
229
        /* Fail and complete transaction */
 
230
        gkm_transaction_fail (transaction, CKR_CANCEL);
 
231
        gkm_transaction_complete (transaction);
 
232
 
 
233
        /* Back to old value */
 
234
        g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "old");
 
235
        g_assert (!notified); /* Should never have notified */
 
236
 
 
237
        g_object_unref (transaction);
 
238
}
 
239
 
 
240
static void
 
241
test_modified_get_attr (Test *test, gconstpointer unused)
 
242
{
 
243
        gchar buffer[32];
 
244
        CK_ATTRIBUTE attr = { CKA_G_MODIFIED, buffer, 32 };
 
245
        CK_RV rv;
 
246
 
 
247
        gkm_secret_object_set_modified (test->object, 1247930171);
 
248
        rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
 
249
        g_assert (rv == CKR_OK);
 
250
        g_assert (attr.ulValueLen == 16);
 
251
        g_assert (memcmp (buffer, "2009071815161100", 16) == 0);
 
252
}
 
253
 
 
254
static void
 
255
test_created_get_attr (Test *test, gconstpointer unused)
 
256
{
 
257
        gchar buffer[32];
 
258
        CK_ATTRIBUTE attr = { CKA_G_CREATED, buffer, 32 };
 
259
        CK_RV rv;
 
260
 
 
261
        gkm_secret_object_set_created (test->object, 1247930171);
 
262
        rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
 
263
        g_assert (rv == CKR_OK);
 
264
        g_assert (attr.ulValueLen == 16);
 
265
        g_assert (memcmp (buffer, "2009071815161100", 16) == 0);
 
266
}
 
267
 
 
268
static void
 
269
test_locked_get_attr (Test *test, gconstpointer unused)
 
270
{
 
271
        gchar buffer[32];
 
272
        CK_ATTRIBUTE attr = { CKA_G_LOCKED, buffer, 32 };
 
273
        CK_RV rv;
 
274
 
 
275
        rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
 
276
        g_assert (rv == CKR_OK);
 
277
        g_assert (attr.ulValueLen == 1);
 
278
        g_assert (memcmp (buffer, "\0", 1) == 0);
 
279
}
 
280
 
 
281
int
 
282
main (int argc, char **argv)
 
283
{
 
284
        g_type_init ();
 
285
        g_test_init (&argc, &argv, NULL);
 
286
 
 
287
        g_test_add ("/secret-store/object/is_locked", Test, NULL, setup, test_is_locked, teardown);
 
288
        g_test_add ("/secret-store/object/identifier_prop", Test, NULL, setup, test_identifier_prop, teardown);
 
289
        g_test_add ("/secret-store/object/created_prop", Test, NULL, setup, test_created_prop, teardown);
 
290
        g_test_add ("/secret-store/object/modified_prop", Test, NULL, setup, test_modified_prop, teardown);
 
291
        g_test_add ("/secret-store/object/was_modified", Test, NULL, setup, test_was_modified, teardown);
 
292
        g_test_add ("/secret-store/object/label_prop", Test, NULL, setup, test_label_prop, teardown);
 
293
        g_test_add ("/secret-store/object/identifier_get_attr", Test, NULL, setup, test_identifier_get_attr, teardown);
 
294
        g_test_add ("/secret-store/object/label_get_attr", Test, NULL, setup, test_label_get_attr, teardown);
 
295
        g_test_add ("/secret-store/object/label_set_attr", Test, NULL, setup, test_label_set_attr, teardown);
 
296
        g_test_add ("/secret-store/object/label_set_attr_fail", Test, NULL, setup, test_label_set_attr_fail, teardown);
 
297
        g_test_add ("/secret-store/object/modified_get_attr", Test, NULL, setup, test_modified_get_attr, teardown);
 
298
        g_test_add ("/secret-store/object/created_get_attr", Test, NULL, setup, test_created_get_attr, teardown);
 
299
        g_test_add ("/secret-store/object/locked_get_attr", Test, NULL, setup, test_locked_get_attr, teardown);
 
300
 
 
301
        return g_test_run ();
 
302
}