Debian Codegraph @

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

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-secret-binary.c: Test binary keyring read and write
3
4 Copyright (C) 2009 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 <http://www.gnu.org/licenses/>.
19
20 Author: Stef Walter <[email protected]>
21 */
22
23 #include "config.h"
24
25 #include "mock-secret-module.h"
26
27 #include "secret-store/gkm-secret-binary.h"
28 #include "secret-store/gkm-secret-collection.h"
29 #include "secret-store/gkm-secret-data.h"
30 #include "secret-store/gkm-secret-fields.h"
31 #include "secret-store/gkm-secret-item.h"
32
33 #include "gkm/gkm-secret.h"
34
35 #include "pkcs11/pkcs11i.h"
36
37 #include <glib.h>
38
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42
43 typedef struct {
44 GkmModule *module;
45 GkmSession *session;
46 GkmSecretCollection *collection;
47 GkmSecretData *sdata;
48 } Test;
49
50 static void
51 setup (Test *test, gconstpointer unused)
52 {
53 GkmSecret *master;
54
55 test->module = test_secret_module_initialize_and_enter ();
56 test->session = test_secret_module_open_session (TRUE);
57
58 test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION,
59 "module", test->module,
60 "identifier", "test",
61 "label", "brigadooooooooooooon",
62 NULL);
63
64 test->sdata = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
65 master = gkm_secret_new_from_password ("my-keyring-password");
66 gkm_secret_data_set_master (test->sdata, master);
67 g_object_unref (master);
68
69 g_assert (GKM_IS_SECRET_COLLECTION (test->collection));
70
71 }
72
73 static void
74 teardown (Test *test, gconstpointer unused)
75 {
76 g_object_unref (test->collection);
77 g_object_unref (test->sdata);
78 test_secret_module_leave_and_finalize ();
79 }
80
81 static GkmDataResult
82 check_read_keyring_file (Test *test, const gchar *path)
83 {
84 GkmDataResult res;
85 gchar *data;
86 gsize n_data;
87
88 if (!g_file_get_contents (path, &data, &n_data, NULL))
89 g_assert_not_reached ();
90 res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data);
91 g_free (data);
92
93 return res;
94 }
95
96
97 static void
98 test_read_encrypted (Test *test, gconstpointer unused)
99 {
100 GkmDataResult res;
101
102 res = check_read_keyring_file (test, SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring");
103 g_assert (res == GKM_DATA_SUCCESS);
104
105 test_secret_collection_validate (test->collection, test->sdata);
106 }
107
108 static void
109 test_read_wrong_format (Test *test, gconstpointer unused)
110 {
111 GkmDataResult res;
112
113 res = check_read_keyring_file (test, SRCDIR "/pkcs11/secret-store/fixtures/plain.keyring");
114 g_assert (res == GKM_DATA_UNRECOGNIZED);
115 }
116
117 static void
118 test_read_wrong_master (Test *test, gconstpointer unused)
119 {
120 GkmDataResult res;
121 GkmSecret *master;
122
123 master = gkm_secret_new_from_password ("wrong");
124 gkm_secret_data_set_master (test->sdata, master);
125 g_object_unref (master);
126
127 res = check_read_keyring_file (test, SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring");
128 g_assert (res == GKM_DATA_LOCKED);
129 }
130
131 static void
132 test_read_sdata_but_no_master (Test *test, gconstpointer unused)
133 {
134 GkmDataResult res;
135
136 gkm_secret_data_set_master (test->sdata, NULL);
137
138 res = check_read_keyring_file (test, SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring");
139 g_assert (res == GKM_DATA_LOCKED);
140 }
141
142 static void
143 test_write (Test *test, gconstpointer unused)
144 {
145 GkmDataResult res;
146 gpointer data;
147 gsize n_data;
148
149 test_secret_collection_populate (test->collection, test->sdata);
150
151 res = gkm_secret_binary_write (test->collection, test->sdata, &data, &n_data);
152 g_assert (res == GKM_DATA_SUCCESS);
153 g_assert (data);
154 g_assert (n_data);
155
156 /* Try parsing it again */
157 res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data);
158 g_assert (res == GKM_DATA_SUCCESS);
159 g_free (data);
160 }
161
162 static void
163 test_remove_unavailable (Test *test, gconstpointer unused)
164 {
165 GkmDataResult res;
166 GList *items;
167 gchar *data;
168 gsize n_data;
169
170 if (!g_file_get_contents (SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring", &data, &n_data, NULL))
171 g_assert_not_reached ();
172 res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data);
173 g_assert (res == GKM_DATA_SUCCESS);
174
175 /* Two items from the file */
176 items = gkm_secret_collection_get_items (test->collection);
177 g_assert_cmpint (g_list_length (items), ==, 2);
178 g_list_free (items);
179
180 /* Fill in some more data */
181 test_secret_collection_populate (test->collection, test->sdata);
182
183 /* Should have added three more */
184 items = gkm_secret_collection_get_items (test->collection);
185 g_assert_cmpint (g_list_length (items), ==, 5);
186 g_list_free (items);
187
188 /* Re-read the keyring */
189 res = gkm_secret_binary_read (test->collection, test->sdata, data, n_data);
190 g_assert (res == GKM_DATA_SUCCESS);
191
192 /* And we're back to two */
193 items = gkm_secret_collection_get_items (test->collection);
194 g_assert_cmpint (g_list_length (items), ==, 2);
195 g_list_free (items);
196
197 g_free (data);
198 }
199
200 static void
201 test_read_created_on_solaris_opencsw (Test *test, gconstpointer unused)
202 {
203 GkmDataResult res;
204 GkmSecret *master;
205
206 master = gkm_secret_new_from_password ("test");
207 gkm_secret_data_set_master (test->sdata, master);
208 g_object_unref (master);
209
210 res = check_read_keyring_file (test, SRCDIR "/pkcs11/secret-store/fixtures/created-on-solaris-opencsw.keyring");
211 g_assert_cmpint (res, ==, GKM_DATA_SUCCESS);
212 }
213
214 static void
215 test_read_created_on_rhel (Test *test, gconstpointer unused)
216 {
217 GkmDataResult res;
218 GkmSecret *master;
219
220 master = gkm_secret_new_from_password ("test");
221 gkm_secret_data_set_master (test->sdata, master);
222 g_object_unref (master);
223
224 res = check_read_keyring_file (test, SRCDIR "/pkcs11/secret-store/fixtures/created-on-rhel.keyring");
225 g_assert_cmpint (res, ==, GKM_DATA_SUCCESS);
226 }
227
228 static void
229 test_read_with_schema (Test *test,
230 gconstpointer unused)
231 {
232 GkmDataResult res;
233 GkmSecret *master;
234 GkmSecretItem *item;
235
236 master = gkm_secret_new_from_password ("test");
237 gkm_secret_data_set_master (test->sdata, master);
238 g_object_unref (master);
239 res = check_read_keyring_file (test, SRCDIR "/pkcs11/secret-store/fixtures/encrypted-with-schema.keyring");
240 g_assert_cmpint (res, ==, GKM_DATA_SUCCESS);
241
242 item = gkm_secret_collection_get_item (test->collection, "1");
243 g_assert (item != NULL);
244
245 g_assert_cmpstr (gkm_secret_item_get_schema (item), ==, "se.lostca.is.rishi.secret");
246 }
247
248 int
249 main (int argc, char **argv)
250 {
251 #if !GLIB_CHECK_VERSION(2,35,0)
252 g_type_init ();
253 #endif
254 g_test_init (&argc, &argv, NULL);
255
256 g_test_add ("/secret-store/binary/read_encrypted", Test, NULL, setup, test_read_encrypted, teardown);
257 g_test_add ("/secret-store/binary/read_wrong_format", Test, NULL, setup, test_read_wrong_format, teardown);
258 g_test_add ("/secret-store/binary/read_wrong_master", Test, NULL, setup, test_read_wrong_master, teardown);
259 g_test_add ("/secret-store/binary/read_sdata_but_no_master", Test, NULL, setup, test_read_sdata_but_no_master, teardown);
260 g_test_add ("/secret-store/binary/write", Test, NULL, setup, test_write, teardown);
261 g_test_add ("/secret-store/binary/remove_unavailable", Test, NULL, setup, test_remove_unavailable, teardown);
262 g_test_add ("/secret-store/binary/created_on_rhel", Test, NULL, setup, test_read_created_on_rhel, teardown);
263 g_test_add ("/secret-store/binary/created_on_solaris_opencsw", Test, NULL, setup, test_read_created_on_solaris_opencsw, teardown);
264 g_test_add ("/secret-store/binary/read_with_schema", Test, NULL, setup, test_read_with_schema, teardown);
265
266 return g_test_run ();
267 }