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_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
63 attributes = secret_attributes_build (&MOCK_SCHEMA,
68 g_assert (attributes == NULL);
71 g_test_trap_assert_failed ();
72 g_test_trap_assert_stderr ("*was not found in*");
76 test_build_null_string (void)
78 GHashTable *attributes;
80 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
81 attributes = secret_attributes_build (&MOCK_SCHEMA,
86 g_assert (attributes == NULL);
89 g_test_trap_assert_failed ();
90 g_test_trap_assert_stderr ("*attribute*NULL*");
94 test_build_non_utf8_string (void)
96 GHashTable *attributes;
98 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
99 attributes = secret_attributes_build (&MOCK_SCHEMA,
101 "string", "\xfftest",
104 g_assert (attributes == NULL);
107 g_test_trap_assert_failed ();
108 g_test_trap_assert_stderr ("*attribute*UTF-8*");
112 test_build_bad_type (void)
114 GHashTable *attributes;
116 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
117 attributes = secret_attributes_build (&MOCK_SCHEMA,
120 g_assert (attributes == NULL);
123 g_test_trap_assert_failed ();
124 g_test_trap_assert_stderr ("*invalid type*");
128 test_validate_schema (void)
130 GHashTable *attributes;
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");
138 ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
139 g_assert (ret == TRUE);
141 g_hash_table_unref (attributes);
145 test_validate_schema_bad (void)
147 GHashTable *attributes;
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");
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);
160 g_hash_table_unref (attributes);
164 test_validate_libgnomekeyring (void)
166 GHashTable *attributes;
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");
174 ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
175 g_assert (ret == TRUE);
177 g_hash_table_unref (attributes);
181 main (int argc, char **argv)
183 g_test_init (&argc, &argv, NULL);
184 g_set_prgname ("test-attributes");
185 #if !GLIB_CHECK_VERSION(2,35,0)
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);
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);
199 return g_test_run ();