Line data Source code
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 <stefw@gnome.org>
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 6 : setup (Test *test,
55 : gconstpointer unused)
56 : {
57 : GkmDataResult res;
58 :
59 6 : test->module = test_secret_module_initialize_and_enter ();
60 6 : test->session = test_secret_module_open_session (TRUE);
61 :
62 6 : 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 6 : gkm_secret_collection_set_filename (test->collection,
75 : SRCDIR "/pkcs11/secret-store/fixtures/schema1.keyring");
76 :
77 : /* Load the collection */
78 6 : res = gkm_secret_collection_load (test->collection);
79 6 : g_assert (res == GKM_DATA_SUCCESS);
80 6 : gkm_object_expose (GKM_OBJECT (test->collection), TRUE);
81 6 : }
82 :
83 : static void
84 6 : teardown (Test *test,
85 : gconstpointer unused)
86 : {
87 6 : g_object_unref (test->collection);
88 6 : test_secret_module_leave_and_finalize ();
89 6 : }
90 :
91 : static gint
92 6 : count_number_of_matched (Test *test,
93 : CK_ATTRIBUTE *attrs,
94 : CK_ULONG n_attrs)
95 : {
96 6 : GkmObject *object = NULL;
97 : gpointer vdata;
98 : gsize vsize;
99 : guint count;
100 :
101 6 : object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_SEARCH, NULL, attrs, 2);
102 6 : g_assert (object != NULL);
103 6 : g_assert (GKM_IS_SECRET_SEARCH (object));
104 :
105 : /* One object matched */
106 6 : vdata = gkm_object_get_attribute_data (object, test->session, CKA_G_MATCHED, &vsize);
107 6 : g_assert (vdata);
108 6 : g_assert (vsize % sizeof (CK_OBJECT_HANDLE) == 0);
109 6 : count = vsize / sizeof (CK_OBJECT_HANDLE);
110 6 : g_free (vdata);
111 :
112 6 : g_object_unref (object);
113 :
114 6 : return count;
115 : }
116 :
117 : static void
118 1 : test_match_network_xdg_schema_without_schema_unlocked (Test *test,
119 : gconstpointer unused)
120 : {
121 : GkmCredential *cred;
122 : CK_RV rv;
123 :
124 1 : 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 1 : rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session),
131 1 : GKM_OBJECT (test->collection), (CK_UTF8CHAR_PTR)"booo", 4, &cred);
132 1 : gkm_assert_cmprv (rv, ==, CKR_OK);
133 :
134 1 : g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 1);
135 :
136 1 : g_object_unref (cred);
137 1 : }
138 :
139 : static void
140 1 : test_match_note_xdg_schema_without_schema_unlocked (Test *test,
141 : gconstpointer unused)
142 : {
143 : GkmCredential *cred;
144 : CK_RV rv;
145 :
146 1 : 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 1 : rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session),
153 1 : GKM_OBJECT (test->collection), (CK_UTF8CHAR_PTR)"booo", 4, &cred);
154 1 : gkm_assert_cmprv (rv, ==, CKR_OK);
155 :
156 1 : g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 1);
157 :
158 1 : g_object_unref (cred);
159 1 : }
160 :
161 : static void
162 1 : test_match_network_xdg_schema_without_schema_locked (Test *test,
163 : gconstpointer unused)
164 : {
165 1 : 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 1 : g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 1);
171 1 : }
172 :
173 : static void
174 1 : test_match_note_xdg_schema_without_schema_locked (Test *test,
175 : gconstpointer unused)
176 : {
177 1 : 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 1 : g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 1);
183 1 : }
184 :
185 : static void
186 1 : test_match_unknown_xdg_schema_without_schema_unlocked (Test *test,
187 : gconstpointer unused)
188 : {
189 : GkmCredential *cred;
190 : CK_RV rv;
191 :
192 1 : 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 1 : rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session),
199 1 : GKM_OBJECT (test->collection), (CK_UTF8CHAR_PTR)"booo", 4, &cred);
200 1 : gkm_assert_cmprv (rv, ==, CKR_OK);
201 :
202 1 : g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 0);
203 :
204 1 : g_object_unref (cred);
205 1 : }
206 :
207 : static void
208 1 : test_match_unknown_xdg_schema_without_schema_locked (Test *test,
209 : gconstpointer unused)
210 : {
211 1 : CK_ATTRIBUTE attrs[] = {
212 : { CKA_G_FIELDS, "xdg:schema\0org.gnome.Unknown\0", 29 },
213 : { CKA_G_COLLECTION, "test-collection", 15 },
214 : };
215 :
216 1 : g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 0);
217 1 : }
218 :
219 : int
220 1 : main (int argc, char **argv)
221 : {
222 : #if !GLIB_CHECK_VERSION(2,35,0)
223 : g_type_init ();
224 : #endif
225 1 : g_test_init (&argc, &argv, NULL);
226 :
227 1 : 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 1 : 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 1 : 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 1 : 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 1 : 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 1 : 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 1 : return g_test_run ();
241 : }
|