Use upstream tag
[platform/upstream/libsecret.git] / libsecret / test-attributes.c
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 #include "secret-attributes.h"
19 #include "secret-private.h"
20
21 #include "egg/egg-testing.h"
22
23 #include <glib.h>
24
25 #include <errno.h>
26 #include <stdlib.h>
27
28 static const SecretSchema MOCK_SCHEMA = {
29         "org.mock.Schema",
30         SECRET_SCHEMA_NONE,
31         {
32                 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
33                 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
34                 { "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
35                 { "bad-type", -1 },
36         }
37 };
38
39 static void
40 test_build (void)
41 {
42         GHashTable *attributes;
43
44         attributes = secret_attributes_build (&MOCK_SCHEMA,
45                                               "number", 4,
46                                               "string", "four",
47                                               "even", TRUE,
48                                               NULL);
49
50         g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "4");
51         g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "four");
52         g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "true");
53
54         g_hash_table_unref (attributes);
55 }
56
57 static void
58 test_build_unknown (void)
59 {
60         GHashTable *attributes;
61
62         if (g_test_subprocess ()) {
63                 attributes = secret_attributes_build (&MOCK_SCHEMA,
64                                                       "invalid", "whee",
65                                                       "string", "four",
66                                                       "even", TRUE,
67                                                       NULL);
68                 g_assert (attributes == NULL);
69                 return;
70         }
71
72         g_test_trap_subprocess ("/attributes/build-unknown", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
73         g_test_trap_assert_failed ();
74         g_test_trap_assert_stderr ("*was not found in*");
75 }
76
77 static void
78 test_build_null_string (void)
79 {
80         GHashTable *attributes;
81
82         if (g_test_subprocess ()) {
83                 attributes = secret_attributes_build (&MOCK_SCHEMA,
84                                                       "number", 4,
85                                                       "string", NULL,
86                                                       "even", TRUE,
87                                                       NULL);
88                 g_assert (attributes == NULL);
89                 return;
90         }
91
92         g_test_trap_subprocess ("/attributes/build-null-string", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
93         g_test_trap_assert_failed ();
94         g_test_trap_assert_stderr ("*attribute*NULL*");
95 }
96
97 static void
98 test_build_non_utf8_string (void)
99 {
100         GHashTable *attributes;
101
102         if (g_test_subprocess ()) {
103                 attributes = secret_attributes_build (&MOCK_SCHEMA,
104                                                       "number", 4,
105                                                       "string", "\xfftest",
106                                                       "even", TRUE,
107                                                       NULL);
108                 g_assert (attributes == NULL);
109                 return;
110         }
111
112         g_test_trap_subprocess ("/attributes/build-non-utf8-string", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
113         g_test_trap_assert_failed ();
114         g_test_trap_assert_stderr ("*attribute*UTF-8*");
115 }
116
117 static void
118 test_build_bad_type (void)
119 {
120         GHashTable *attributes;
121
122         if (g_test_subprocess ()) {
123                 attributes = secret_attributes_build (&MOCK_SCHEMA,
124                                                       "bad-type", "test",
125                                                       NULL);
126                 g_assert (attributes == NULL);
127                 return;
128         }
129
130         g_test_trap_subprocess ("/attributes/build-bad-type", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
131         g_test_trap_assert_failed ();
132         g_test_trap_assert_stderr ("*invalid type*");
133 }
134
135 static void
136 test_validate_schema (void)
137 {
138         GHashTable *attributes;
139         gboolean ret;
140
141         attributes = g_hash_table_new (g_str_hash, g_str_equal);
142         g_hash_table_replace (attributes, "number", "1");
143         g_hash_table_replace (attributes, "string", "test");
144         g_hash_table_replace (attributes, "xdg:schema", "org.mock.Schema");
145
146         ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
147         g_assert (ret == TRUE);
148
149         g_hash_table_unref (attributes);
150 }
151
152 static void
153 test_validate_schema_bad (void)
154 {
155         GHashTable *attributes;
156         gboolean ret;
157
158         if (g_test_subprocess ()) {
159                 attributes = g_hash_table_new (g_str_hash, g_str_equal);
160                 g_hash_table_replace (attributes, "number", "1");
161                 g_hash_table_replace (attributes, "string", "test");
162                 g_hash_table_replace (attributes, "xdg:schema", "mismatched.Schema");
163
164                 ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
165                 g_assert (ret == FALSE);
166
167                 g_hash_table_unref (attributes);
168                 return;
169         }
170
171         g_test_trap_subprocess ("/attributes/validate-schema-bad", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
172 }
173
174 static void
175 test_validate_libgnomekeyring (void)
176 {
177         GHashTable *attributes;
178         gboolean ret;
179
180         attributes = g_hash_table_new (g_str_hash, g_str_equal);
181         g_hash_table_replace (attributes, "number", "1");
182         g_hash_table_replace (attributes, "string", "test");
183         g_hash_table_replace (attributes, "gkr:compat", "blah-dee-blah");
184
185         ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
186         g_assert (ret == TRUE);
187
188         g_hash_table_unref (attributes);
189 }
190
191 int
192 main (int argc, char **argv)
193 {
194         g_test_init (&argc, &argv, NULL);
195         g_set_prgname ("test-attributes");
196 #if !GLIB_CHECK_VERSION(2,35,0)
197         g_type_init ();
198 #endif
199
200         g_test_add_func ("/attributes/build", test_build);
201         g_test_add_func ("/attributes/build-unknown", test_build_unknown);
202         g_test_add_func ("/attributes/build-null-string", test_build_null_string);
203         g_test_add_func ("/attributes/build-non-utf8-string", test_build_non_utf8_string);
204         g_test_add_func ("/attributes/build-bad-type", test_build_bad_type);
205
206         g_test_add_func ("/attributes/validate-schema", test_validate_schema);
207         g_test_add_func ("/attributes/validate-schema-bad", test_validate_schema_bad);
208         g_test_add_func ("/attributes/validate-libgnomekeyring", test_validate_libgnomekeyring);
209
210         return g_test_run ();
211 }