Debian Codegraph @

pkcs11/secret-store/test-secret-schema.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /*
3 Copyright (C) 2012 Red Hat Ltd.
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 <http://www.gnu.org/licenses/>.
18
19 Author: Stef Walter <[email protected]>
20 */
21
22 #include "config.h"
23
24 #include "mock-secret-module.h"
25
26 #include "secret-store/gkm-secret-collection.h"
27 #include "secret-store/gkm-secret-item.h"
28 #include "secret-store/gkm-secret-search.h"
29
30 #include "gkm/gkm-credential.h"
31 #include "gkm/gkm-secret.h"
32 #include "gkm/gkm-serializable.h"
33 #include "gkm/gkm-session.h"
34 #include "gkm/gkm-transaction.h"
35 #include "gkm/gkm-test.h"
36
37 #include "pkcs11/pkcs11i.h"
38
39 #include "egg/egg-testing.h"
40
41 #include <glib.h>
42
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46
47 typedef struct {
48 GkmModule *module;
49 GkmSession *session;
50 GkmSecretCollection *collection;
51 } Test;
52
53 static void
54 setup (Test *test,
55 gconstpointer unused)
56 {
57 GkmDataResult res;
58
59 test->module = test_secret_module_initialize_and_enter ();
60 test->session = test_secret_module_open_session (TRUE);
61
62 test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION,
63 "module", test->module,
64 "manager", gkm_session_get_manager (test->session),
65 "identifier", "test-collection",
66 NULL);
67
68 /*
69 * This file contains entries that don't actually have any xdg:schema
70 * entries. It does contain the old libgnome-keyring style item types,
71 * and these should be used to match the appropriate schemas.
72 */
73
74 gkm_secret_collection_set_filename (test->collection,
75 SRCDIR "/pkcs11/secret-store/fixtures/schema1.keyring");
76
77 /* Load the collection */
78 res = gkm_secret_collection_load (test->collection);
79 g_assert (res == GKM_DATA_SUCCESS);
80 gkm_object_expose (GKM_OBJECT (test->collection), TRUE);
81 }
82
83 static void
84 teardown (Test *test,
85 gconstpointer unused)
86 {
87 g_object_unref (test->collection);
88 test_secret_module_leave_and_finalize ();
89 }
90
91 static gint
92 count_number_of_matched (Test *test,
93 CK_ATTRIBUTE *attrs,
94 CK_ULONG n_attrs)
95 {
96 GkmObject *object = NULL;
97 gpointer vdata;
98 gsize vsize;
99 guint count;
100
101 object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_SEARCH, NULL, attrs, 2);
102 g_assert (object != NULL);
103 g_assert (GKM_IS_SECRET_SEARCH (object));
104
105 /* One object matched */
106 vdata = gkm_object_get_attribute_data (object, test->session, CKA_G_MATCHED, &vsize);
107 g_assert (vdata);
108 g_assert (vsize % sizeof (CK_OBJECT_HANDLE) == 0);
109 count = vsize / sizeof (CK_OBJECT_HANDLE);
110 g_free (vdata);
111
112 g_object_unref (object);
113
114 return count;
115 }
116
117 static void
118 test_match_network_xdg_schema_without_schema_unlocked (Test *test,
119 gconstpointer unused)
120 {
121 GkmCredential *cred;
122 CK_RV rv;
123
124 CK_ATTRIBUTE attrs[] = {
125 { CKA_G_FIELDS, "xdg:schema\0org.gnome.keyring.NetworkPassword\0", 45 },
126 { CKA_G_COLLECTION, "test-collection", 15 },
127 };
128
129 /* Unlock the collection */
130 rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session),
131 GKM_OBJECT (test->collection), (CK_UTF8CHAR_PTR)"booo", 4, &cred);
132 gkm_assert_cmprv (rv, ==, CKR_OK);
133
134 g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 1);
135
136 g_object_unref (cred);
137 }
138
139 static void
140 test_match_note_xdg_schema_without_schema_unlocked (Test *test,
141 gconstpointer unused)
142 {
143 GkmCredential *cred;
144 CK_RV rv;
145
146 CK_ATTRIBUTE attrs[] = {
147 { CKA_G_FIELDS, "xdg:schema\0org.gnome.keyring.Note\0", 34 },
148 { CKA_G_COLLECTION, "test-collection", 15 },
149 };
150
151 /* Unlock the collection */
152 rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session),
153 GKM_OBJECT (test->collection), (CK_UTF8CHAR_PTR)"booo", 4, &cred);
154 gkm_assert_cmprv (rv, ==, CKR_OK);
155
156 g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 1);
157
158 g_object_unref (cred);
159 }
160
161 static void
162 test_match_network_xdg_schema_without_schema_locked (Test *test,
163 gconstpointer unused)
164 {
165 CK_ATTRIBUTE attrs[] = {
166 { CKA_G_FIELDS, "xdg:schema\0org.gnome.keyring.NetworkPassword\0", 45 },
167 { CKA_G_COLLECTION, "test-collection", 15 },
168 };
169
170 g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 1);
171 }
172
173 static void
174 test_match_note_xdg_schema_without_schema_locked (Test *test,
175 gconstpointer unused)
176 {
177 CK_ATTRIBUTE attrs[] = {
178 { CKA_G_FIELDS, "xdg:schema\0org.gnome.keyring.Note\0", 34 },
179 { CKA_G_COLLECTION, "test-collection", 15 },
180 };
181
182 g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 1);
183 }
184
185 static void
186 test_match_unknown_xdg_schema_without_schema_unlocked (Test *test,
187 gconstpointer unused)
188 {
189 GkmCredential *cred;
190 CK_RV rv;
191
192 CK_ATTRIBUTE attrs[] = {
193 { CKA_G_FIELDS, "xdg:schema\0org.gnome.Unknown\0", 29 },
194 { CKA_G_COLLECTION, "test-collection", 15 },
195 };
196
197 /* Unlock the collection */
198 rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session),
199 GKM_OBJECT (test->collection), (CK_UTF8CHAR_PTR)"booo", 4, &cred);
200 gkm_assert_cmprv (rv, ==, CKR_OK);
201
202 g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 0);
203
204 g_object_unref (cred);
205 }
206
207 static void
208 test_match_unknown_xdg_schema_without_schema_locked (Test *test,
209 gconstpointer unused)
210 {
211 CK_ATTRIBUTE attrs[] = {
212 { CKA_G_FIELDS, "xdg:schema\0org.gnome.Unknown\0", 29 },
213 { CKA_G_COLLECTION, "test-collection", 15 },
214 };
215
216 g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 0);
217 }
218
219 int
220 main (int argc, char **argv)
221 {
222 #if !GLIB_CHECK_VERSION(2,35,0)
223 g_type_init ();
224 #endif
225 g_test_init (&argc, &argv, NULL);
226
227 g_test_add ("/secret-store/schema/network-xdg-schema-without-schema-unlocked",
228 Test, NULL, setup, test_match_network_xdg_schema_without_schema_unlocked, teardown);
229 g_test_add ("/secret-store/schema/network-xdg-schema-without-schema-locked",
230 Test, NULL, setup, test_match_network_xdg_schema_without_schema_locked, teardown);
231 g_test_add ("/secret-store/schema/note-xdg-schema-without-schema-unlocked",
232 Test, NULL, setup, test_match_note_xdg_schema_without_schema_unlocked, teardown);
233 g_test_add ("/secret-store/schema/note-xdg-schema-without-schema-locked",
234 Test, NULL, setup, test_match_note_xdg_schema_without_schema_locked, teardown);
235 g_test_add ("/secret-store/schema/unknown-schema-without-schema-unlocked",
236 Test, NULL, setup, test_match_unknown_xdg_schema_without_schema_unlocked, teardown);
237 g_test_add ("/secret-store/schema/unknown-schema-without-schema-locked",
238 Test, NULL, setup, test_match_unknown_xdg_schema_without_schema_locked, teardown);
239
240 return g_test_run ();
241 }