summaryrefslogtreecommitdiff
path: root/libsecret/test-attributes.c
blob: f202989ea6d0057395d2fde2b1edbee28bfd5c1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* libsecret - GLib wrapper for Secret Service
 *
 * Copyright 2012 Red Hat Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 of the licence or (at
 * your option) any later version.
 *
 * See the included COPYING file for more information.
 *
 * Author: Stef Walter <stefw@gnome.org>
 */


#include "config.h"

#undef G_DISABLE_ASSERT

#include "secret-attributes.h"
#include "secret-private.h"

#include "egg/egg-testing.h"

#include <glib.h>

#include <errno.h>
#include <stdlib.h>

static const SecretSchema MOCK_SCHEMA = {
	"org.mock.Schema",
	SECRET_SCHEMA_NONE,
	{
		{ "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
		{ "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
		{ "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
		{ "bad-type", -1 },
	}
};

static void
test_build (void)
{
	GHashTable *attributes;

	attributes = secret_attributes_build (&MOCK_SCHEMA,
	                                      "number", 4,
	                                      "string", "four",
	                                      "even", TRUE,
	                                      NULL);

	g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "4");
	g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "four");
	g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "true");

	g_hash_table_unref (attributes);
}

static void
test_build_unknown (void)
{
	GHashTable *attributes;

	if (g_test_subprocess ()) {
		attributes = secret_attributes_build (&MOCK_SCHEMA,
		                                      "invalid", "whee",
		                                      "string", "four",
		                                      "even", TRUE,
		                                      NULL);
		g_assert_null (attributes);
		return;
	}

	g_test_trap_subprocess ("/attributes/build-unknown", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
	g_test_trap_assert_failed ();
	g_test_trap_assert_stderr ("*was not found in*");
}

static void
test_build_null_string (void)
{
	GHashTable *attributes;

	if (g_test_subprocess ()) {
		attributes = secret_attributes_build (&MOCK_SCHEMA,
		                                      "number", 4,
		                                      "string", NULL,
		                                      "even", TRUE,
		                                      NULL);
		g_assert_null (attributes);
		return;
	}

	g_test_trap_subprocess ("/attributes/build-null-string", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
	g_test_trap_assert_failed ();
	g_test_trap_assert_stderr ("*attribute*NULL*");
}

static void
test_build_non_utf8_string (void)
{
	GHashTable *attributes;

	if (g_test_subprocess ()) {
		attributes = secret_attributes_build (&MOCK_SCHEMA,
		                                      "number", 4,
		                                      "string", "\xfftest",
		                                      "even", TRUE,
		                                      NULL);
		g_assert_null (attributes);
		return;
	}

	g_test_trap_subprocess ("/attributes/build-non-utf8-string", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
	g_test_trap_assert_failed ();
	g_test_trap_assert_stderr ("*attribute*UTF-8*");
}

static void
test_build_bad_type (void)
{
	GHashTable *attributes;

	if (g_test_subprocess ()) {
		attributes = secret_attributes_build (&MOCK_SCHEMA,
		                                      "bad-type", "test",
		                                      NULL);
		g_assert_null (attributes);
		return;
	}

	g_test_trap_subprocess ("/attributes/build-bad-type", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
	g_test_trap_assert_failed ();
	g_test_trap_assert_stderr ("*invalid type*");
}

static void
test_validate_schema (void)
{
	GHashTable *attributes;
	gboolean ret;

	attributes = g_hash_table_new (g_str_hash, g_str_equal);
	g_hash_table_replace (attributes, "number", "1");
	g_hash_table_replace (attributes, "string", "test");
	g_hash_table_replace (attributes, "xdg:schema", "org.mock.Schema");

	ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
	g_assert_true (ret);

	g_hash_table_unref (attributes);
}

static void
test_validate_schema_bad (void)
{
	GHashTable *attributes;
	gboolean ret;

	if (g_test_subprocess ()) {
		attributes = g_hash_table_new (g_str_hash, g_str_equal);
		g_hash_table_replace (attributes, "number", "1");
		g_hash_table_replace (attributes, "string", "test");
		g_hash_table_replace (attributes, "xdg:schema", "mismatched.Schema");

		ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
		g_assert_false (ret);

		g_hash_table_unref (attributes);
		return;
	}

	g_test_trap_subprocess ("/attributes/validate-schema-bad", 0, G_TEST_SUBPROCESS_INHERIT_STDOUT);
}

static void
test_validate_libgnomekeyring (void)
{
	GHashTable *attributes;
	gboolean ret;

	attributes = g_hash_table_new (g_str_hash, g_str_equal);
	g_hash_table_replace (attributes, "number", "1");
	g_hash_table_replace (attributes, "string", "test");
	g_hash_table_replace (attributes, "gkr:compat", "blah-dee-blah");

	ret = _secret_attributes_validate (&MOCK_SCHEMA, attributes, G_STRFUNC, TRUE);
	g_assert_true (ret);

	g_hash_table_unref (attributes);
}

int
main (int argc, char **argv)
{
	g_test_init (&argc, &argv, NULL);
	g_set_prgname ("test-attributes");

	g_test_add_func ("/attributes/build", test_build);
	g_test_add_func ("/attributes/build-unknown", test_build_unknown);
	g_test_add_func ("/attributes/build-null-string", test_build_null_string);
	g_test_add_func ("/attributes/build-non-utf8-string", test_build_non_utf8_string);
	g_test_add_func ("/attributes/build-bad-type", test_build_bad_type);

	g_test_add_func ("/attributes/validate-schema", test_validate_schema);
	g_test_add_func ("/attributes/validate-schema-bad", test_validate_schema_bad);
	g_test_add_func ("/attributes/validate-libgnomekeyring", test_validate_libgnomekeyring);

	return g_test_run ();
}