File: polkitbackendlocalauthorizationstoretest.c

package info (click to toggle)
policykit-1 0.105-15~deb8u2
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 8,644 kB
  • ctags: 4,294
  • sloc: ansic: 18,560; sh: 11,430; xml: 3,384; makefile: 767
file content (142 lines) | stat: -rw-r--r-- 4,127 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (C) 2011 Google Inc.
 *
 * This library 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 License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Nikki VonHollen <vonhollen@google.com>
 */

#include "glib.h"

#include <polkittesthelper.h>
#include <polkit/polkit.h>
#include <polkitbackend/polkitbackendlocalauthorizationstore.h>

#define DATA_DIR "etc/polkit-1/localauthority/10-test"
#define DATA_EXT ".pkla"

static void
test_new (void)
{
  PolkitBackendLocalAuthorizationStore *store;
  gchar *data_dir_path;
  GFile *data_dir;

  data_dir_path = polkit_test_get_data_path (DATA_DIR);
  g_assert (data_dir_path);

  data_dir = g_file_new_for_path (data_dir_path);
  g_assert (data_dir);

  g_free (data_dir_path);

  store = polkit_backend_local_authorization_store_new (data_dir, DATA_EXT);
  g_assert (store);
}


static void
test_lookup (void)
{
  gchar *data_dir_path;
  GFile *data_dir;
  PolkitBackendLocalAuthorizationStore *store;
  GError *error = NULL;
  PolkitIdentity *identity;
  gboolean ok;
  PolkitImplicitAuthorization ret_any;
  PolkitImplicitAuthorization ret_inactive;
  PolkitImplicitAuthorization ret_active;
  PolkitDetails *details;

  // Get auth store path
  data_dir_path = polkit_test_get_data_path (DATA_DIR);
  g_assert (data_dir_path);

  data_dir = g_file_new_for_path (data_dir_path);
  g_assert (data_dir);
  
  // Create the auth store
  store = polkit_backend_local_authorization_store_new (data_dir, DATA_EXT);
  g_assert (store);

  // We don't care about details
  details = polkit_details_new ();

  // Create an identity to query with
  identity = polkit_identity_from_string ("unix-group:users", &error);
  g_assert (identity);
  g_assert_no_error (error);

  // Lookup an exisiting record
  ok = polkit_backend_local_authorization_store_lookup (
      store,
      identity,
      "com.example.awesomeproduct.foo",
      details,
      &ret_any,
      &ret_inactive,
      &ret_active,
      NULL);
  g_assert (ok);
  g_assert_cmpstr ("no", ==, polkit_implicit_authorization_to_string (ret_any));
  g_assert_cmpstr ("auth_self", ==, polkit_implicit_authorization_to_string (ret_inactive));
  g_assert_cmpstr ("yes", ==, polkit_implicit_authorization_to_string (ret_active));

  // Create another identity to query with
  identity = polkit_identity_from_string ("unix-user:root", &error);
  g_assert (identity);
  g_assert_no_error (error);

  // Lookup another exisiting record
  ok = polkit_backend_local_authorization_store_lookup (
      store,
      identity,
      "com.example.awesomeproduct.foo",
      details,
      &ret_any,
      &ret_inactive,
      &ret_active,
      NULL);
  g_assert (ok);
  g_assert_cmpstr ("no", ==, polkit_implicit_authorization_to_string (ret_any));
  g_assert_cmpstr ("auth_self", ==, polkit_implicit_authorization_to_string (ret_inactive));
  g_assert_cmpstr ("yes", ==, polkit_implicit_authorization_to_string (ret_active));

  // Lookup a missing record
  ok = polkit_backend_local_authorization_store_lookup (
      store,
      identity,
      "com.example.restrictedproduct.dobar",
      details,
      &ret_any,
      &ret_inactive,
      &ret_active,
      NULL);
  g_assert (!ok);
}


int
main (int argc, char *argv[])
{
  g_type_init ();
  g_test_init (&argc, &argv, NULL);
  polkit_test_redirect_logs ();
  g_test_add_func ("/PolkitBackendLocalAuthorizationStore/new", test_new);
  g_test_add_func ("/PolkitBackendLocalAuthorizationStore/lookup", test_lookup);
  return g_test_run ();
}