~ubuntu-branches/ubuntu/trusty/policykit-1/trusty

« back to all changes in this revision

Viewing changes to test/polkit/polkitidentitytest.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2012-01-06 12:28:54 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20120106122854-ib9s0ej8akqiy0lb
Tags: 0.104-1
* New upstream release.
  - Add support for netgroups. (LP: #724052)
* debian/rules: Disable systemd support, continue to work with ConsokeKit.
* 05_revert-admin-identities-unix-group-wheel.patch: Refresh to apply
  cleanly.
* debian/libpolkit-gobject-1-0.symbols: Add new symbols from this new
  release.
* debian/rules: Do not let test failures fail the build. The new test suite
  also runs a test against the system D-BUS/ConsoleKit, which can't work on
  buildds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011 Google Inc.
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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.
 
18
 *
 
19
 * Author: Nikki VonHollen <vonhollen@google.com>
 
20
 */
 
21
 
 
22
#include "glib.h"
 
23
#include <polkit/polkit.h>
 
24
#include <polkit/polkitprivate.h>
 
25
 
 
26
/* Test helper types */
 
27
 
 
28
struct ComparisonTestData {
 
29
  const gchar *subject_a;
 
30
  const gchar *subject_b;
 
31
  gboolean equal;
 
32
};
 
33
 
 
34
 
 
35
/* Test definitions */
 
36
 
 
37
static void
 
38
test_string (const void *_subject)
 
39
{
 
40
  const gchar *subject = (const gchar *) _subject;
 
41
 
 
42
  PolkitIdentity *identity;
 
43
  GError *error = NULL;
 
44
  gchar *subject_new;
 
45
 
 
46
  /* Create the subject from a string */
 
47
  identity = polkit_identity_from_string (subject, &error);
 
48
  g_assert (identity);
 
49
  g_assert_no_error (error);
 
50
 
 
51
  /* Create new string for identity */
 
52
  subject_new = polkit_identity_to_string (identity);
 
53
 
 
54
  /* Make sure they match */
 
55
  g_assert_cmpstr (subject_new, ==, subject);
 
56
 
 
57
  g_free (subject_new);
 
58
  g_object_unref (identity);
 
59
}
 
60
 
 
61
 
 
62
static void
 
63
test_gvariant (const void *_subject)
 
64
{
 
65
  const gchar *subject = (const gchar *) _subject;
 
66
 
 
67
  PolkitIdentity *identity, *new_identity;
 
68
  GError *error = NULL;
 
69
  GVariant *value;
 
70
 
 
71
  /* Create the subject from a string */
 
72
  identity = polkit_identity_from_string (subject, &error);
 
73
  g_assert_no_error (error);
 
74
  g_assert (identity);
 
75
 
 
76
  /* Create a GVariant for the subject */
 
77
  value = polkit_identity_to_gvariant (identity);
 
78
  g_assert (value);
 
79
 
 
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);
 
85
 
 
86
  /* Make sure the two identities are equal */
 
87
  g_assert (new_identity);
 
88
  g_assert (polkit_identity_equal (identity, new_identity));
 
89
 
 
90
  g_object_unref (identity);
 
91
  g_object_unref (new_identity);
 
92
}
 
93
 
 
94
 
 
95
static void
 
96
test_comparison (const void *_data)
 
97
{
 
98
  struct ComparisonTestData *data = (struct ComparisonTestData *) _data;
 
99
 
 
100
  PolkitIdentity *identity_a, *identity_b;
 
101
  GError *error = NULL;
 
102
  guint hash_a, hash_b;
 
103
 
 
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);
 
108
 
 
109
  identity_b = polkit_identity_from_string (data->subject_b, &error);
 
110
  g_assert_no_error (error);
 
111
  g_assert (identity_b);
 
112
 
 
113
  /* Compute their hashes */
 
114
  hash_a = polkit_identity_hash (identity_a);
 
115
  hash_b = polkit_identity_hash (identity_b);
 
116
 
 
117
  /* Comparison to self should always work */
 
118
  g_assert (polkit_identity_equal (identity_a, identity_a));
 
119
 
 
120
  /* Are A and B supposed to match? Test hash and comparators */
 
121
  if (data->equal)
 
122
  {
 
123
    g_assert_cmpint (hash_a, ==, hash_b);
 
124
    g_assert (polkit_identity_equal (identity_a, identity_b));
 
125
  }
 
126
  else
 
127
  {
 
128
    g_assert_cmpint (hash_a, !=, hash_b);
 
129
    g_assert (!polkit_identity_equal (identity_a, identity_b));
 
130
  }
 
131
 
 
132
  g_object_unref (identity_a);
 
133
  g_object_unref (identity_b);
 
134
}
 
135
 
 
136
 
 
137
/* Test helpers */
 
138
 
 
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},
 
143
 
 
144
  {"unix-group:root", "unix-group:root", TRUE},
 
145
  {"unix-group:root", "unix-group:jane", FALSE},
 
146
  {"unix-group:jane", "unix-group:jane", TRUE},
 
147
 
 
148
  {"unix-netgroup:foo", "unix-netgroup:foo", TRUE},
 
149
  {"unix-netgroup:foo", "unix-netgroup:bar", FALSE},
 
150
 
 
151
  {"unix-user:root", "unix-group:root", FALSE},
 
152
  {"unix-user:jane", "unix-netgroup:foo", FALSE},
 
153
 
 
154
  {NULL},
 
155
};
 
156
 
 
157
static void
 
158
add_comparison_tests (void)
 
159
{
 
160
  unsigned int i;
 
161
  for (i = 0; comparison_test_data[i].subject_a != NULL; i++)
 
162
  {
 
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);
 
166
  }
 
167
}
 
168
 
 
169
 
 
170
int
 
171
main (int argc, char *argv[])
 
172
{
 
173
  g_type_init ();
 
174
  g_test_init (&argc, &argv, NULL);
 
175
 
 
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);
 
179
 
 
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);
 
184
 
 
185
  g_test_add_data_func ("/PolkitIdentity/netgroup_string", "unix-netgroup:foo", test_string);
 
186
 
 
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);
 
190
 
 
191
  add_comparison_tests ();
 
192
 
 
193
  return g_test_run ();
 
194
}