Debian Codegraph @

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

raw
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 <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-data.h"
28 #include "secret-store/gkm-secret-fields.h"
29 #include "secret-store/gkm-secret-item.h"
30 #include "secret-store/gkm-secret-textual.h"
31
32 #include "gkm/gkm-secret.h"
33
34 #include "pkcs11/pkcs11i.h"
35
36 #include <glib.h>
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41
42 typedef struct {
43 GkmModule *module;
44 GkmSession *session;
45 GkmSecretCollection *collection;
46 GkmSecretData *sdata;
47 } Test;
48
49 static void
50 setup (Test *test, gconstpointer unused)
51 {
52 test->module = test_secret_module_initialize_and_enter ();
53 test->session = test_secret_module_open_session (TRUE);
54
55 test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION,
56 "module", test->module,
57 "identifier", "test",
58 "label", "brigadooooooooooooon",
59 NULL);
60
61 test->sdata = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
62
63 g_assert (GKM_IS_SECRET_COLLECTION (test->collection));
64
65 }
66
67 static void
68 teardown (Test *test, gconstpointer unused)
69 {
70 if (test->collection)
71 g_object_unref (test->collection);
72 if (test->sdata)
73 g_object_unref (test->sdata);
74 test_secret_module_leave_and_finalize ();
75 }
76
77 static void
78 test_read (Test *test, gconstpointer unused)
79 {
80 GkmDataResult res;
81 gchar *data;
82 gsize n_data;
83
84 if (!g_file_get_contents (SRCDIR "/pkcs11/secret-store/fixtures/plain.keyring", &data, &n_data, NULL))
85 g_assert_not_reached ();
86 res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
87 g_free (data);
88
89 test_secret_collection_validate (test->collection, test->sdata);
90
91 g_assert (res == GKM_DATA_SUCCESS);
92 }
93
94 static void
95 test_read_wrong_format (Test *test, gconstpointer unused)
96 {
97 GkmDataResult res;
98 gchar *data;
99 gsize n_data;
100
101 if (!g_file_get_contents (SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring", &data, &n_data, NULL))
102 g_assert_not_reached ();
103
104 res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
105 g_free (data);
106
107 g_assert (res == GKM_DATA_UNRECOGNIZED);
108 }
109
110 static void
111 test_read_bad_number (Test *test, gconstpointer unused)
112 {
113 GkmSecretItem *item;
114 GkmDataResult res;
115 const gchar *value;
116 gchar *data;
117 gsize n_data;
118
119 if (!g_file_get_contents (SRCDIR "/pkcs11/secret-store/fixtures/plain-bad-number.keyring", &data, &n_data, NULL))
120 g_assert_not_reached ();
121 res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
122 g_free (data);
123
124 g_assert (res == GKM_DATA_SUCCESS);
125
126 item = gkm_secret_collection_get_item (test->collection, "1");
127 g_assert (GKM_IS_SECRET_ITEM (item));
128 value = gkm_secret_fields_get (gkm_secret_item_get_fields (item), "bad-number");
129 g_assert (value == NULL);
130 value = gkm_secret_fields_get (gkm_secret_item_get_fields (item), "missing-number");
131 g_assert (value == NULL);
132 }
133
134 static void
135 test_write (Test *test, gconstpointer unused)
136 {
137 GkmDataResult res;
138 gpointer data;
139 gsize n_data;
140
141 test_secret_collection_populate (test->collection, test->sdata);
142
143 res = gkm_secret_textual_write (test->collection, test->sdata, &data, &n_data);
144 g_assert (res == GKM_DATA_SUCCESS);
145 g_assert (data);
146 g_assert (n_data);
147
148 /* Try parsing it again */
149 res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
150 g_assert (res == GKM_DATA_SUCCESS);
151 g_free (data);
152 }
153
154 static void
155 test_remove_unavailable (Test *test, gconstpointer unused)
156 {
157 GkmDataResult res;
158 GList *items;
159 gchar *data;
160 gsize n_data;
161
162 if (!g_file_get_contents (SRCDIR "/pkcs11/secret-store/fixtures/plain.keyring", &data, &n_data, NULL))
163 g_assert_not_reached ();
164 res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
165 g_assert (res == GKM_DATA_SUCCESS);
166
167 /* Two items from the file */
168 items = gkm_secret_collection_get_items (test->collection);
169 g_assert_cmpint (g_list_length (items), ==, 2);
170 g_list_free (items);
171
172 /* Fill in some more data */
173 test_secret_collection_populate (test->collection, test->sdata);
174
175 /* Should have added three more */
176 items = gkm_secret_collection_get_items (test->collection);
177 g_assert_cmpint (g_list_length (items), ==, 5);
178 g_list_free (items);
179
180 /* Re-read the keyring */
181 res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
182 g_assert (res == GKM_DATA_SUCCESS);
183
184 /* And we're back to two */
185 items = gkm_secret_collection_get_items (test->collection);
186 g_assert_cmpint (g_list_length (items), ==, 2);
187 g_list_free (items);
188
189 g_free (data);
190 }
191
192 static void
193 test_read_with_schema (Test *test,
194 gconstpointer unused)
195 {
196 GkmDataResult res;
197 GkmSecretItem *item;
198 gchar *data;
199 gsize n_data;
200
201 if (!g_file_get_contents (SRCDIR "/pkcs11/secret-store/fixtures/plain-with-schema.keyring", &data, &n_data, NULL))
202 g_assert_not_reached ();
203 res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
204 g_assert (res == GKM_DATA_SUCCESS);
205
206 item = gkm_secret_collection_get_item (test->collection, "1");
207 g_assert (item != NULL);
208
209 g_assert_cmpstr (gkm_secret_item_get_schema (item), ==, "se.lostca.is.rishi.secret");
210
211 g_free (data);
212 }
213
214 int
215 main (int argc, char **argv)
216 {
217 #if !GLIB_CHECK_VERSION(2,35,0)
218 g_type_init ();
219 #endif
220 g_test_init (&argc, &argv, NULL);
221
222 g_test_add ("/secret-store/search/read", Test, NULL, setup, test_read, teardown);
223 g_test_add ("/secret-store/search/read_wrong_format", Test, NULL, setup, test_read_wrong_format, teardown);
224 g_test_add ("/secret-store/search/read_bad_number", Test, NULL, setup, test_read_bad_number, teardown);
225 g_test_add ("/secret-store/search/write", Test, NULL, setup, test_write, teardown);
226 g_test_add ("/secret-store/search/remove_unavailable", Test, NULL, setup, test_remove_unavailable, teardown);
227 g_test_add ("/secret-store/search/read-with-schema", Test, NULL, setup, test_read_with_schema, teardown);
228
229 return g_test_run ();
230 }