2
* Copyright (C) 2011 Google Inc.
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General
15
* Public License along with this library; if not, write to the
16
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17
* Boston, MA 02111-1307, USA.
19
* Author: Nikki VonHollen <vonhollen@google.com>
23
#include <polkit/polkit.h>
24
#include <polkit/polkitprivate.h>
26
/* Test helper types */
28
struct ComparisonTestData {
29
const gchar *subject_a;
30
const gchar *subject_b;
35
/* Test definitions */
38
test_string (const void *_subject)
40
const gchar *subject = (const gchar *) _subject;
42
PolkitIdentity *identity;
46
/* Create the subject from a string */
47
identity = polkit_identity_from_string (subject, &error);
49
g_assert_no_error (error);
51
/* Create new string for identity */
52
subject_new = polkit_identity_to_string (identity);
54
/* Make sure they match */
55
g_assert_cmpstr (subject_new, ==, subject);
58
g_object_unref (identity);
63
test_gvariant (const void *_subject)
65
const gchar *subject = (const gchar *) _subject;
67
PolkitIdentity *identity, *new_identity;
71
/* Create the subject from a string */
72
identity = polkit_identity_from_string (subject, &error);
73
g_assert_no_error (error);
76
/* Create a GVariant for the subject */
77
value = polkit_identity_to_gvariant (identity);
80
/* Unserialize the subject */
81
new_identity = polkit_identity_new_for_gvariant (value, &error);
82
g_assert_no_error (error);
83
g_assert (new_identity);
84
g_variant_unref (value);
86
/* Make sure the two identities are equal */
87
g_assert (new_identity);
88
g_assert (polkit_identity_equal (identity, new_identity));
90
g_object_unref (identity);
91
g_object_unref (new_identity);
96
test_comparison (const void *_data)
98
struct ComparisonTestData *data = (struct ComparisonTestData *) _data;
100
PolkitIdentity *identity_a, *identity_b;
101
GError *error = NULL;
102
guint hash_a, hash_b;
104
/* Create identities A and B */
105
identity_a = polkit_identity_from_string (data->subject_a, &error);
106
g_assert_no_error (error);
107
g_assert (identity_a);
109
identity_b = polkit_identity_from_string (data->subject_b, &error);
110
g_assert_no_error (error);
111
g_assert (identity_b);
113
/* Compute their hashes */
114
hash_a = polkit_identity_hash (identity_a);
115
hash_b = polkit_identity_hash (identity_b);
117
/* Comparison to self should always work */
118
g_assert (polkit_identity_equal (identity_a, identity_a));
120
/* Are A and B supposed to match? Test hash and comparators */
123
g_assert_cmpint (hash_a, ==, hash_b);
124
g_assert (polkit_identity_equal (identity_a, identity_b));
128
g_assert_cmpint (hash_a, !=, hash_b);
129
g_assert (!polkit_identity_equal (identity_a, identity_b));
132
g_object_unref (identity_a);
133
g_object_unref (identity_b);
139
struct ComparisonTestData comparison_test_data [] = {
140
{"unix-user:root", "unix-user:root", TRUE},
141
{"unix-user:root", "unix-user:john", FALSE},
142
{"unix-user:john", "unix-user:john", TRUE},
144
{"unix-group:root", "unix-group:root", TRUE},
145
{"unix-group:root", "unix-group:jane", FALSE},
146
{"unix-group:jane", "unix-group:jane", TRUE},
148
{"unix-netgroup:foo", "unix-netgroup:foo", TRUE},
149
{"unix-netgroup:foo", "unix-netgroup:bar", FALSE},
151
{"unix-user:root", "unix-group:root", FALSE},
152
{"unix-user:jane", "unix-netgroup:foo", FALSE},
158
add_comparison_tests (void)
161
for (i = 0; comparison_test_data[i].subject_a != NULL; i++)
163
struct ComparisonTestData *test_data = &comparison_test_data[i];
164
gchar *test_name = g_strdup_printf ("/PolkitIdentity/comparison_%d", i);
165
g_test_add_data_func (test_name, test_data, test_comparison);
171
main (int argc, char *argv[])
174
g_test_init (&argc, &argv, NULL);
176
g_test_add_data_func ("/PolkitIdentity/user_string_0", "unix-user:root", test_string);
177
g_test_add_data_func ("/PolkitIdentity/user_string_1", "unix-user:john", test_string);
178
g_test_add_data_func ("/PolkitIdentity/user_string_2", "unix-user:jane", test_string);
180
g_test_add_data_func ("/PolkitIdentity/group_string_0", "unix-group:root", test_string);
181
g_test_add_data_func ("/PolkitIdentity/group_string_1", "unix-group:john", test_string);
182
g_test_add_data_func ("/PolkitIdentity/group_string_2", "unix-group:jane", test_string);
183
g_test_add_data_func ("/PolkitIdentity/group_string_3", "unix-group:users", test_string);
185
g_test_add_data_func ("/PolkitIdentity/netgroup_string", "unix-netgroup:foo", test_string);
187
g_test_add_data_func ("/PolkitIdentity/user_gvariant", "unix-user:root", test_gvariant);
188
g_test_add_data_func ("/PolkitIdentity/group_gvariant", "unix-group:root", test_gvariant);
189
g_test_add_data_func ("/PolkitIdentity/netgroup_gvariant", "unix-netgroup:foo", test_gvariant);
191
add_comparison_tests ();
193
return g_test_run ();