Branch data Line data Source code
1 : : /* libsecret - GLib wrapper for Secret Service
2 : : *
3 : : * Copyright 2012 Red Hat Inc.
4 : : *
5 : : * This program is free software: you can redistribute it and/or modify
6 : : * it under the terms of the GNU Lesser General Public License as published
7 : : * by the Free Software Foundation; either version 2 of the licence or (at
8 : : * your option) any later version.
9 : : *
10 : : * See the included COPYING file for more information.
11 : : *
12 : : * Author: Stef Walter <stefw@gnome.org>
13 : : */
14 : :
15 : :
16 : : #include "config.h"
17 : :
18 : : #undef G_DISABLE_ASSERT
19 : :
20 : : #include "secret-attributes.h"
21 : : #include "secret-private.h"
22 : :
23 : : #include "egg/egg-testing.h"
24 : :
25 : : #include <glib.h>
26 : :
27 : : #include <errno.h>
28 : : #include <stdlib.h>
29 : :
30 : : static const SecretSchema MOCK_SCHEMA = {
31 : : "org.mock.Schema",
32 : : SECRET_SCHEMA_DONT_MATCH_NAME,
33 : : {
34 : : { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
35 : : { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
36 : : { "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
37 : : { "bad-type", -1 },
38 : : }
39 : : };
40 : :
41 : : static void
42 : 1 : test_build (void)
43 : : {
44 : : GHashTable *attributes;
45 : :
46 : 1 : attributes = secret_attributes_build (&MOCK_SCHEMA,
47 : : "number", 4,
48 : : "string", "four",
49 : : "even", TRUE,
50 : : NULL);
51 : :
52 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "4");
53 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "four");
54 [ - + ]: 1 : g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "true");
55 : :
56 : 1 : g_hash_table_unref (attributes);
57 : 1 : }
58 : :
59 : : static void
60 : 1 : test_build_unknown (void)
61 : : {
62 : : GHashTable *attributes;
63 : :
64 [ - + ]: 1 : if (g_test_subprocess ()) {
65 : 0 : attributes = secret_attributes_build (&MOCK_SCHEMA,
66 : : "invalid", "whee",
67 : : "string", "four",
68 : : "even", TRUE,
69 : : NULL);
70 [ # # ]: 0 : g_assert_null (attributes);
71 : 0 : return;
72 : : }
73 : :
74 : 1 : g_test_trap_subprocess ("/attributes/build-unknown", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
75 : 1 : g_test_trap_assert_failed ();
76 : 1 : g_test_trap_assert_stderr ("*was not found in*");
77 : : }
78 : :
79 : : static void
80 : 1 : test_build_null_string (void)
81 : : {
82 : : GHashTable *attributes;
83 : :
84 [ - + ]: 1 : if (g_test_subprocess ()) {
85 : 0 : attributes = secret_attributes_build (&MOCK_SCHEMA,
86 : : "number", 4,
87 : : "string", NULL,
88 : : "even", TRUE,
89 : : NULL);
90 [ # # ]: 0 : g_assert_null (attributes);
91 : 0 : return;
92 : : }
93 : :
94 : 1 : g_test_trap_subprocess ("/attributes/build-null-string", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
95 : 1 : g_test_trap_assert_failed ();
96 : 1 : g_test_trap_assert_stderr ("*attribute*NULL*");
97 : : }
98 : :
99 : : static void
100 : 1 : test_build_non_utf8_string (void)
101 : : {
102 : : GHashTable *attributes;
103 : :
104 [ - + ]: 1 : if (g_test_subprocess ()) {
105 : 0 : attributes = secret_attributes_build (&MOCK_SCHEMA,
106 : : "number", 4,
107 : : "string", "\xfftest",
108 : : "even", TRUE,
109 : : NULL);
110 [ # # ]: 0 : g_assert_null (attributes);
111 : 0 : return;
112 : : }
113 : :
114 : 1 : g_test_trap_subprocess ("/attributes/build-non-utf8-string", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
115 : 1 : g_test_trap_assert_failed ();
116 : 1 : g_test_trap_assert_stderr ("*attribute*UTF-8*");
117 : : }
118 : :
119 : : static void
120 : 1 : test_build_bad_type (void)
121 : : {
122 : : GHashTable *attributes;
123 : :
124 [ - + ]: 1 : if (g_test_subprocess ()) {
125 : 0 : attributes = secret_attributes_build (&MOCK_SCHEMA,
126 : : "bad-type", "test",
127 : : NULL);
128 [ # # ]: 0 : g_assert_null (attributes);
129 : 0 : return;
130 : : }
131 : :
132 : 1 : g_test_trap_subprocess ("/attributes/build-bad-type", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
133 : 1 : g_test_trap_assert_failed ();
134 : 1 : g_test_trap_assert_stderr ("*invalid type*");
135 : : }
136 : :
137 : : static void
138 : 1 : test_validate_schema (void)
139 : : {
140 : : GHashTable *attributes;
141 : : gboolean ret;
142 : :
143 : 1 : attributes = g_hash_table_new (g_str_hash, g_str_equal);
144 : 1 : g_hash_table_replace (attributes, "number", "1");
145 : 1 : g_hash_table_replace (attributes, "string", "test");
146 : 1 : g_hash_table_replace (attributes, "xdg:schema", "org.mock.Schema");
147 : :
148 : 1 : ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
149 [ - + ]: 1 : g_assert_true (ret);
150 : :
151 : 1 : g_hash_table_unref (attributes);
152 : 1 : }
153 : :
154 : : static void
155 : 1 : test_validate_schema_empty_ok (void)
156 : : {
157 : : GHashTable *attributes;
158 : : gboolean ret;
159 : :
160 : 1 : attributes = g_hash_table_new (g_str_hash, g_str_equal);
161 : :
162 : 1 : ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, FALSE);
163 [ - + ]: 1 : g_assert_true (ret);
164 : :
165 : 1 : g_hash_table_unref (attributes);
166 : 1 : }
167 : :
168 : : static void
169 : 1 : test_validate_schema_bad_empty_not_ok (void)
170 : : {
171 : : GHashTable *attributes;
172 : : gboolean ret;
173 : :
174 [ - + ]: 1 : if (g_test_subprocess ()) {
175 : 0 : attributes = g_hash_table_new (g_str_hash, g_str_equal);
176 : :
177 : 0 : ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
178 [ # # ]: 0 : g_assert_false (ret);
179 : :
180 : 0 : g_hash_table_unref (attributes);
181 : 0 : return;
182 : : }
183 : :
184 : 1 : g_test_trap_subprocess ("/attributes/validate-schema-bad-empty-not-ok", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
185 : 1 : g_test_trap_assert_failed ();
186 : : }
187 : :
188 : : static void
189 : 1 : test_validate_schema_bad_mismatched_schema (void)
190 : : {
191 : : GHashTable *attributes;
192 : : gboolean ret;
193 : :
194 [ - + ]: 1 : if (g_test_subprocess ()) {
195 : 0 : attributes = g_hash_table_new (g_str_hash, g_str_equal);
196 : 0 : g_hash_table_replace (attributes, "number", "1");
197 : 0 : g_hash_table_replace (attributes, "string", "test");
198 : 0 : g_hash_table_replace (attributes, "xdg:schema", "mismatched.Schema");
199 : :
200 : 0 : ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
201 [ # # ]: 0 : g_assert_false (ret);
202 : :
203 : 0 : g_hash_table_unref (attributes);
204 : 0 : return;
205 : : }
206 : :
207 : 1 : g_test_trap_subprocess ("/attributes/validate-schema-bad-mismatched-schema", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
208 : 1 : g_test_trap_assert_failed ();
209 : : }
210 : :
211 : : static void
212 : 1 : test_validate_schema_bad_wrong_type (void)
213 : : {
214 : : GHashTable *attributes;
215 : : gboolean ret;
216 : 1 : char non_utf8_string[] = {(char) 128, '\0'};
217 : :
218 [ - + ]: 1 : if (g_test_subprocess ()) {
219 : 0 : attributes = g_hash_table_new (g_str_hash, g_str_equal);
220 : 0 : g_hash_table_replace (attributes, "number", "string_in_wrong_place");
221 : 0 : g_hash_table_replace (attributes, "string", non_utf8_string);
222 : 0 : g_hash_table_replace (attributes, "even", "neither_true_nor_false");
223 : 0 : g_hash_table_replace (attributes, "xdg:schema", "org.mock.Schema");
224 : :
225 : 0 : ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
226 [ # # ]: 0 : g_assert_false (ret);
227 : :
228 : 0 : g_hash_table_unref (attributes);
229 : 0 : return;
230 : : }
231 : :
232 : 1 : g_test_trap_subprocess ("/attributes/validate-schema-bad-wrong-type", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
233 : 1 : g_test_trap_assert_failed ();
234 : : }
235 : :
236 : : static void
237 : 1 : test_validate_schema_bad_fake_key (void)
238 : : {
239 : : GHashTable *attributes;
240 : : gboolean ret;
241 : :
242 [ - + ]: 1 : if (g_test_subprocess ()) {
243 : 0 : attributes = g_hash_table_new (g_str_hash, g_str_equal);
244 : 0 : g_hash_table_replace (attributes, "number", "1");
245 : 0 : g_hash_table_replace (attributes, "string", "test");
246 : 0 : g_hash_table_replace (attributes, "xdg:schema", "org.mock.Schema");
247 : 0 : g_hash_table_replace (attributes, "made_up_key", "not_valid");
248 : :
249 : 0 : ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
250 [ # # ]: 0 : g_assert_false (ret);
251 : :
252 : 0 : g_hash_table_unref (attributes);
253 : 0 : return;
254 : : }
255 : :
256 : 1 : g_test_trap_subprocess ("/attributes/validate-schema-bad-fake-key", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
257 : 1 : g_test_trap_assert_failed ();
258 : : }
259 : :
260 : : static void
261 : 1 : test_validate_libgnomekeyring (void)
262 : : {
263 : : GHashTable *attributes;
264 : : gboolean ret;
265 : :
266 : 1 : attributes = g_hash_table_new (g_str_hash, g_str_equal);
267 : 1 : g_hash_table_replace (attributes, "number", "1");
268 : 1 : g_hash_table_replace (attributes, "string", "test");
269 : 1 : g_hash_table_replace (attributes, "gkr:compat", "blah-dee-blah");
270 : :
271 : 1 : ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
272 [ - + ]: 1 : g_assert_true (ret);
273 : :
274 : 1 : g_hash_table_unref (attributes);
275 : 1 : }
276 : :
277 : : int
278 : 1 : main (int argc, char **argv)
279 : : {
280 : 1 : g_test_init (&argc, &argv, NULL);
281 : 1 : g_set_prgname ("test-attributes");
282 : :
283 : 1 : g_test_add_func ("/attributes/build", test_build);
284 : 1 : g_test_add_func ("/attributes/build-unknown", test_build_unknown);
285 : 1 : g_test_add_func ("/attributes/build-null-string", test_build_null_string);
286 : 1 : g_test_add_func ("/attributes/build-non-utf8-string", test_build_non_utf8_string);
287 : 1 : g_test_add_func ("/attributes/build-bad-type", test_build_bad_type);
288 : :
289 : 1 : g_test_add_func ("/attributes/validate-schema", test_validate_schema);
290 : 1 : g_test_add_func ("/attributes/validate-schema-empty-ok", test_validate_schema_empty_ok);
291 : 1 : g_test_add_func ("/attributes/validate-schema-bad-empty-not-ok", test_validate_schema_bad_empty_not_ok);
292 : 1 : g_test_add_func ("/attributes/validate-schema-bad-mismatched-schema", test_validate_schema_bad_mismatched_schema);
293 : 1 : g_test_add_func ("/attributes/validate-schema-bad-wrong-type", test_validate_schema_bad_wrong_type);
294 : 1 : g_test_add_func ("/attributes/validate-schema-bad-fake-key", test_validate_schema_bad_fake_key);
295 : 1 : g_test_add_func ("/attributes/validate-libgnomekeyring", test_validate_libgnomekeyring);
296 : :
297 : 1 : return g_test_run ();
298 : : }
|