collection: Have gobject-introspection and glib-mkenums recognize flags
[platform/upstream/libsecret.git] / libsecret / tests / 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_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
63                 attributes = secret_attributes_build (&MOCK_SCHEMA,
64                                                       "invalid", "whee",
65                                                       "string", "four",
66                                                       "even", TRUE,
67                                                       NULL);
68                 g_assert (attributes == NULL);
69         }
70
71         g_test_trap_assert_failed ();
72         g_test_trap_assert_stderr ("*was not found in*");
73 }
74
75 static void
76 test_build_null_string (void)
77 {
78         GHashTable *attributes;
79
80         if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
81                 attributes = secret_attributes_build (&MOCK_SCHEMA,
82                                                       "number", 4,
83                                                       "string", NULL,
84                                                       "even", TRUE,
85                                                       NULL);
86                 g_assert (attributes == NULL);
87         }
88
89         g_test_trap_assert_failed ();
90         g_test_trap_assert_stderr ("*attribute*NULL*");
91 }
92
93 static void
94 test_build_non_utf8_string (void)
95 {
96         GHashTable *attributes;
97
98         if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
99                 attributes = secret_attributes_build (&MOCK_SCHEMA,
100                                                       "number", 4,
101                                                       "string", "\xfftest",
102                                                       "even", TRUE,
103                                                       NULL);
104                 g_assert (attributes == NULL);
105         }
106
107         g_test_trap_assert_failed ();
108         g_test_trap_assert_stderr ("*attribute*UTF-8*");
109 }
110
111 static void
112 test_build_bad_type (void)
113 {
114         GHashTable *attributes;
115
116         if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
117                 attributes = secret_attributes_build (&MOCK_SCHEMA,
118                                                       "bad-type", "test",
119                                                       NULL);
120                 g_assert (attributes == NULL);
121         }
122
123         g_test_trap_assert_failed ();
124         g_test_trap_assert_stderr ("*invalid type*");
125 }
126
127 static void
128 test_validate_schema (void)
129 {
130         GHashTable *attributes;
131         gboolean ret;
132
133         attributes = g_hash_table_new (g_str_hash, g_str_equal);
134         g_hash_table_replace (attributes, "number", "1");
135         g_hash_table_replace (attributes, "string", "test");
136         g_hash_table_replace (attributes, "xdg:schema", "org.mock.Schema");
137
138         ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
139         g_assert (ret == TRUE);
140
141         g_hash_table_unref (attributes);
142 }
143
144 static void
145 test_validate_schema_bad (void)
146 {
147         GHashTable *attributes;
148         gboolean ret;
149
150         attributes = g_hash_table_new (g_str_hash, g_str_equal);
151         g_hash_table_replace (attributes, "number", "1");
152         g_hash_table_replace (attributes, "string", "test");
153         g_hash_table_replace (attributes, "xdg:schema", "mismatched.Schema");
154
155         if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
156                 ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
157                 g_assert (ret == FALSE);
158         }
159
160         g_hash_table_unref (attributes);
161 }
162
163 static void
164 test_validate_libgnomekeyring (void)
165 {
166         GHashTable *attributes;
167         gboolean ret;
168
169         attributes = g_hash_table_new (g_str_hash, g_str_equal);
170         g_hash_table_replace (attributes, "number", "1");
171         g_hash_table_replace (attributes, "string", "test");
172         g_hash_table_replace (attributes, "gkr:compat", "blah-dee-blah");
173
174         ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
175         g_assert (ret == TRUE);
176
177         g_hash_table_unref (attributes);
178 }
179
180 int
181 main (int argc, char **argv)
182 {
183         g_test_init (&argc, &argv, NULL);
184         g_set_prgname ("test-attributes");
185 #if !GLIB_CHECK_VERSION(2,35,0)
186         g_type_init ();
187 #endif
188
189         g_test_add_func ("/attributes/build", test_build);
190         g_test_add_func ("/attributes/build-unknown", test_build_unknown);
191         g_test_add_func ("/attributes/build-null-string", test_build_null_string);
192         g_test_add_func ("/attributes/build-non-utf8-string", test_build_non_utf8_string);
193         g_test_add_func ("/attributes/build-bad-type", test_build_bad_type);
194
195         g_test_add_func ("/attributes/validate-schema", test_validate_schema);
196         g_test_add_func ("/attributes/validate-schema-bad", test_validate_schema_bad);
197         g_test_add_func ("/attributes/validate-libgnomekeyring", test_validate_libgnomekeyring);
198
199         return g_test_run ();
200 }