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
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
Copyright (C) 2009 Stefan Walter
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,
<http://www.gnu.org/licenses/>.
Author: Stef Walter <stef@memberwebs.com>
*/
#include "config.h"
#include "mock-secret-module.h"
#include "secret-store/gkm-secret-collection.h"
#include "secret-store/gkm-secret-data.h"
#include "secret-store/gkm-secret-fields.h"
#include "secret-store/gkm-secret-item.h"
#include "secret-store/gkm-secret-textual.h"
#include "gkm/gkm-secret.h"
#include "pkcs11/pkcs11i.h"
#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct {
GkmModule *module;
GkmSession *session;
GkmSecretCollection *collection;
GkmSecretData *sdata;
} Test;
static void
setup (Test *test, gconstpointer unused)
{
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,
"identifier", "test",
"label", "brigadooooooooooooon",
NULL);
test->sdata = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
g_assert (GKM_IS_SECRET_COLLECTION (test->collection));
}
static void
teardown (Test *test, gconstpointer unused)
{
if (test->collection)
g_object_unref (test->collection);
if (test->sdata)
g_object_unref (test->sdata);
test_secret_module_leave_and_finalize ();
}
static void
test_read (Test *test, gconstpointer unused)
{
GkmDataResult res;
gchar *data;
gsize n_data;
if (!g_file_get_contents (SRCDIR "/fixtures/plain.keyring", &data, &n_data, NULL))
g_assert_not_reached ();
res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
g_free (data);
test_secret_collection_validate (test->collection, test->sdata);
g_assert (res == GKM_DATA_SUCCESS);
}
static void
test_read_wrong_format (Test *test, gconstpointer unused)
{
GkmDataResult res;
gchar *data;
gsize n_data;
if (!g_file_get_contents (SRCDIR "/fixtures/encrypted.keyring", &data, &n_data, NULL))
g_assert_not_reached ();
res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
g_free (data);
g_assert (res == GKM_DATA_UNRECOGNIZED);
}
static void
test_read_bad_number (Test *test, gconstpointer unused)
{
GkmSecretItem *item;
GkmDataResult res;
const gchar *value;
gchar *data;
gsize n_data;
if (!g_file_get_contents (SRCDIR "/fixtures/plain-bad-number.keyring", &data, &n_data, NULL))
g_assert_not_reached ();
res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
g_free (data);
g_assert (res == GKM_DATA_SUCCESS);
item = gkm_secret_collection_get_item (test->collection, "1");
g_assert (GKM_IS_SECRET_ITEM (item));
value = gkm_secret_fields_get (gkm_secret_item_get_fields (item), "bad-number");
g_assert (value == NULL);
value = gkm_secret_fields_get (gkm_secret_item_get_fields (item), "missing-number");
g_assert (value == NULL);
}
static void
test_write (Test *test, gconstpointer unused)
{
GkmDataResult res;
gpointer data;
gsize n_data;
test_secret_collection_populate (test->collection, test->sdata);
res = gkm_secret_textual_write (test->collection, test->sdata, &data, &n_data);
g_assert (res == GKM_DATA_SUCCESS);
g_assert (data);
g_assert (n_data);
/* Try parsing it again */
res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
g_assert (res == GKM_DATA_SUCCESS);
}
static void
test_remove_unavailable (Test *test, gconstpointer unused)
{
GkmDataResult res;
GList *items;
gchar *data;
gsize n_data;
if (!g_file_get_contents (SRCDIR "/fixtures/plain.keyring", &data, &n_data, NULL))
g_assert_not_reached ();
res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
g_assert (res == GKM_DATA_SUCCESS);
/* Two items from the file */
items = gkm_secret_collection_get_items (test->collection);
g_assert_cmpint (g_list_length (items), ==, 2);
g_list_free (items);
/* Fill in some more data */
test_secret_collection_populate (test->collection, test->sdata);
/* Should have added three more */
items = gkm_secret_collection_get_items (test->collection);
g_assert_cmpint (g_list_length (items), ==, 5);
g_list_free (items);
/* Re-read the keyring */
res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
g_assert (res == GKM_DATA_SUCCESS);
/* And we're back to two */
items = gkm_secret_collection_get_items (test->collection);
g_assert_cmpint (g_list_length (items), ==, 2);
g_list_free (items);
g_free (data);
}
static void
test_read_with_schema (Test *test,
gconstpointer unused)
{
GkmDataResult res;
GkmSecretItem *item;
gchar *data;
gsize n_data;
if (!g_file_get_contents (SRCDIR "/fixtures/plain-with-schema.keyring", &data, &n_data, NULL))
g_assert_not_reached ();
res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
g_assert (res == GKM_DATA_SUCCESS);
item = gkm_secret_collection_get_item (test->collection, "1");
g_assert (item != NULL);
g_assert_cmpstr (gkm_secret_item_get_schema (item), ==, "se.lostca.is.rishi.secret");
g_free (data);
}
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/search/read", Test, NULL, setup, test_read, teardown);
g_test_add ("/secret-store/search/read_wrong_format", Test, NULL, setup, test_read_wrong_format, teardown);
g_test_add ("/secret-store/search/read_bad_number", Test, NULL, setup, test_read_bad_number, teardown);
g_test_add ("/secret-store/search/write", Test, NULL, setup, test_write, teardown);
g_test_add ("/secret-store/search/remove_unavailable", Test, NULL, setup, test_remove_unavailable, teardown);
g_test_add ("/secret-store/search/read-with-schema", Test, NULL, setup, test_read_with_schema, teardown);
return g_test_run ();
}
|