1 /* libsecret - GLib wrapper for Secret Service
3 * Copyright 2012 Red Hat Inc.
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.
10 * See the included COPYING file for more information.
12 * Author: Stef Walter <stefw@gnome.org>
18 #include "secret-attributes.h"
19 #include "secret-private.h"
21 #include "egg/egg-testing.h"
28 static const SecretSchema MOCK_SCHEMA = {
32 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
33 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
34 { "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
42 GHashTable *attributes;
44 attributes = secret_attributes_build (&MOCK_SCHEMA,
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");
54 g_hash_table_unref (attributes);
58 test_build_unknown (void)
60 GHashTable *attributes;
62 if (g_test_subprocess ()) {
63 attributes = secret_attributes_build (&MOCK_SCHEMA,
68 g_assert (attributes == NULL);
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*");
78 test_build_null_string (void)
80 GHashTable *attributes;
82 if (g_test_subprocess ()) {
83 attributes = secret_attributes_build (&MOCK_SCHEMA,
88 g_assert (attributes == NULL);
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*");
98 test_build_non_utf8_string (void)
100 GHashTable *attributes;
102 if (g_test_subprocess ()) {
103 attributes = secret_attributes_build (&MOCK_SCHEMA,
105 "string", "\xfftest",
108 g_assert (attributes == NULL);
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*");
118 test_build_bad_type (void)
120 GHashTable *attributes;
122 if (g_test_subprocess ()) {
123 attributes = secret_attributes_build (&MOCK_SCHEMA,
126 g_assert (attributes == NULL);
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*");
136 test_validate_schema (void)
138 GHashTable *attributes;
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");
146 ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
147 g_assert (ret == TRUE);
149 g_hash_table_unref (attributes);
153 test_validate_schema_bad (void)
155 GHashTable *attributes;
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");
164 ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
165 g_assert (ret == FALSE);
167 g_hash_table_unref (attributes);
171 g_test_trap_subprocess ("/attributes/validate-schema-bad", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
175 test_validate_libgnomekeyring (void)
177 GHashTable *attributes;
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");
185 ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
186 g_assert (ret == TRUE);
188 g_hash_table_unref (attributes);
192 main (int argc, char **argv)
194 g_test_init (&argc, &argv, NULL);
195 g_set_prgname ("test-attributes");
196 #if !GLIB_CHECK_VERSION(2,35,0)
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);
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);
210 return g_test_run ();