summaryrefslogtreecommitdiff
path: root/pkcs11/secret-store/tests/test-secret-schema.c
blob: 68c2709dfc6aeaaf5c2bc857f2a2795decf23ba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
   Copyright (C) 2012 Red Hat Ltd.

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Stef Walter <stefw@gnome.org>
*/

#include "config.h"

#include "mock-secret-module.h"

#include "secret-store/gkm-secret-collection.h"
#include "secret-store/gkm-secret-item.h"
#include "secret-store/gkm-secret-search.h"

#include "gkm/gkm-credential.h"
#include "gkm/gkm-secret.h"
#include "gkm/gkm-serializable.h"
#include "gkm/gkm-session.h"
#include "gkm/gkm-transaction.h"
#include "gkm/gkm-test.h"

#include "pkcs11/pkcs11i.h"

#include "egg/egg-testing.h"

#include <glib.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct {
	GkmModule *module;
	GkmSession *session;
	GkmSecretCollection *collection;
} Test;

static void
setup (Test *test,
       gconstpointer unused)
{
	GkmDataResult res;

	test->module = test_secret_module_initialize_and_enter ();
	test->session = test_secret_module_open_session (TRUE);

	test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION,
	                                 "module", test->module,
	                                 "manager", gkm_session_get_manager (test->session),
	                                 "identifier", "test-collection",
	                                 NULL);

	/*
	 * This file contains entries that don't actually have any xdg:schema
	 * entries. It does contain the old libgnome-keyring style item types,
	 * and these should be used to match the appropriate schemas.
	 */

	gkm_secret_collection_set_filename (test->collection,
	                                    SRCDIR "/files/schema1.keyring");

	/* Load the collection */
	res = gkm_secret_collection_load (test->collection);
	g_assert (res == GKM_DATA_SUCCESS);
	gkm_object_expose (GKM_OBJECT (test->collection), TRUE);
}

static void
teardown (Test *test,
          gconstpointer unused)
{
	g_object_unref (test->collection);
	test_secret_module_leave_and_finalize ();
}

static gint
count_number_of_matched (Test *test,
                         CK_ATTRIBUTE *attrs,
                         CK_ULONG n_attrs)
{
	GkmObject *object = NULL;
	gpointer vdata;
	gsize vsize;
	guint count;

	object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_SEARCH, NULL, attrs, 2);
	g_assert (object != NULL);
	g_assert (GKM_IS_SECRET_SEARCH (object));

	/* One object matched */
	vdata = gkm_object_get_attribute_data (object, test->session, CKA_G_MATCHED, &vsize);
	g_assert (vdata);
	g_assert (vsize % sizeof (CK_OBJECT_HANDLE) == 0);
	count = vsize / sizeof (CK_OBJECT_HANDLE);
	g_free (vdata);

	g_object_unref (object);

	return count;
}

static void
test_match_network_xdg_schema_without_schema_unlocked (Test *test,
                                                       gconstpointer unused)
{
	GkmCredential *cred;
	CK_RV rv;

	CK_ATTRIBUTE attrs[] = {
	        { CKA_G_FIELDS, "xdg:schema\0org.gnome.keyring.NetworkPassword\0", 45 },
	        { CKA_G_COLLECTION, "test-collection", 15 },
	};

	/* Unlock the collection */
	rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session),
	                            GKM_OBJECT (test->collection), (CK_UTF8CHAR_PTR)"booo", 4, &cred);
	gkm_assert_cmprv (rv, ==, CKR_OK);

	g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 1);

	g_object_unref (cred);
}

static void
test_match_note_xdg_schema_without_schema_unlocked (Test *test,
                                                    gconstpointer unused)
{
	GkmCredential *cred;
	CK_RV rv;

	CK_ATTRIBUTE attrs[] = {
	        { CKA_G_FIELDS, "xdg:schema\0org.gnome.keyring.Note\0", 34 },
	        { CKA_G_COLLECTION, "test-collection", 15 },
	};

	/* Unlock the collection */
	rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session),
	                            GKM_OBJECT (test->collection), (CK_UTF8CHAR_PTR)"booo", 4, &cred);
	gkm_assert_cmprv (rv, ==, CKR_OK);

	g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 1);

	g_object_unref (cred);
}

static void
test_match_network_xdg_schema_without_schema_locked (Test *test,
                                                     gconstpointer unused)
{
	CK_ATTRIBUTE attrs[] = {
	        { CKA_G_FIELDS, "xdg:schema\0org.gnome.keyring.NetworkPassword\0", 45 },
	        { CKA_G_COLLECTION, "test-collection", 15 },
	};

	g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 1);
}

static void
test_match_note_xdg_schema_without_schema_locked (Test *test,
                                                  gconstpointer unused)
{
	CK_ATTRIBUTE attrs[] = {
	        { CKA_G_FIELDS, "xdg:schema\0org.gnome.keyring.Note\0", 34 },
	        { CKA_G_COLLECTION, "test-collection", 15 },
	};

	g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 1);
}

static void
test_match_unknown_xdg_schema_without_schema_unlocked (Test *test,
                                                       gconstpointer unused)
{
	GkmCredential *cred;
	CK_RV rv;

	CK_ATTRIBUTE attrs[] = {
	        { CKA_G_FIELDS, "xdg:schema\0org.gnome.Unknown\0", 29 },
	        { CKA_G_COLLECTION, "test-collection", 15 },
	};

	/* Unlock the collection */
	rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session),
	                            GKM_OBJECT (test->collection), (CK_UTF8CHAR_PTR)"booo", 4, &cred);
	gkm_assert_cmprv (rv, ==, CKR_OK);

	g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 0);

	g_object_unref (cred);
}

static void
test_match_unknown_xdg_schema_without_schema_locked (Test *test,
                                                     gconstpointer unused)
{
	CK_ATTRIBUTE attrs[] = {
	        { CKA_G_FIELDS, "xdg:schema\0org.gnome.Unknown\0", 29 },
	        { CKA_G_COLLECTION, "test-collection", 15 },
	};

	g_assert_cmpint (count_number_of_matched (test, attrs, 2), ==, 0);
}

int
main (int argc, char **argv)
{
#if !GLIB_CHECK_VERSION(2,35,0)
	g_type_init ();
#endif
	g_test_init (&argc, &argv, NULL);

	g_test_add ("/secret-store/schema/network-xdg-schema-without-schema-unlocked",
	            Test, NULL, setup, test_match_network_xdg_schema_without_schema_unlocked, teardown);
	g_test_add ("/secret-store/schema/network-xdg-schema-without-schema-locked",
	            Test, NULL, setup, test_match_network_xdg_schema_without_schema_locked, teardown);
	g_test_add ("/secret-store/schema/note-xdg-schema-without-schema-unlocked",
	            Test, NULL, setup, test_match_note_xdg_schema_without_schema_unlocked, teardown);
	g_test_add ("/secret-store/schema/unknown-schema-without-schema-locked",
	            Test, NULL, setup, test_match_note_xdg_schema_without_schema_locked, teardown);
	g_test_add ("/secret-store/schema/unknown-schema-without-schema-unlocked",
	            Test, NULL, setup, test_match_unknown_xdg_schema_without_schema_unlocked, teardown);
	g_test_add ("/secret-store/schema/unknown-schema-without-schema-locked",
	            Test, NULL, setup, test_match_unknown_xdg_schema_without_schema_locked, teardown);

	return g_test_run ();
}