summaryrefslogtreecommitdiff
path: root/test/polkitbackend/polkitbackendlocalauthoritytest.c
blob: f76ea415d131d519f82b06a183c37e917c89ee96 (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
/*
 * 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/polkitbackendlocalauthority.h>

#define TEST_CONFIG_PATH "./data/config"
#define TEST_AUTH_PATH1 "./data/authstore1"
#define TEST_AUTH_PATH2 "./data/authstore2"

/* Test helper types */

struct auth_context {
  const gchar *identity;
  gboolean subject_is_local;
  gboolean subject_is_active;
  const gchar *action_id;
  PolkitImplicitAuthorization implicit;
  PolkitImplicitAuthorization expect;
};

static PolkitBackendLocalAuthority *create_authority (void);


/* Test implementations */

static void
test_check_authorization_sync (const void *_ctx)
{
  const struct auth_context *ctx = (const struct auth_context *) _ctx;

  PolkitBackendLocalAuthority *authority = create_authority ();

  PolkitSubject *caller = polkit_unix_session_new ("caller-session");
  g_assert (caller);

  PolkitSubject *subject = polkit_unix_session_new ("subject-session");;
  g_assert (subject);

  GError *error = NULL;
  PolkitIdentity *user_for_subject = polkit_identity_from_string (ctx->identity, &error);
  g_assert_no_error (error);
  g_assert (user_for_subject);

  PolkitDetails *details = polkit_details_new ();
  g_assert (details);

  PolkitDetails *out_details = polkit_details_new ();
  g_assert (out_details);

  PolkitImplicitAuthorization auth;

  auth = polkit_backend_interactive_authority_check_authorization_sync (
      POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority),
      caller,
      subject,
      user_for_subject,
      ctx->subject_is_local,
      ctx->subject_is_active,
      ctx->action_id,
      details,
      ctx->implicit,
      out_details);

  g_assert_cmpint (auth, ==, ctx->expect);

  g_object_unref (authority);
  g_object_unref (caller);
  g_object_unref (subject);
  g_object_unref (user_for_subject);
  g_object_unref (details);
  g_object_unref (out_details);
}


/* Factory for mock local authority. */
static PolkitBackendLocalAuthority *
create_authority (void)
{
  return g_object_new (
      POLKIT_BACKEND_TYPE_LOCAL_AUTHORITY,
      "config-path", TEST_CONFIG_PATH,
      "auth-store-paths", TEST_AUTH_PATH1 ";" TEST_AUTH_PATH2,
      NULL);
}


/* Variations of the check_authorization_sync */
struct auth_context check_authorization_test_data [] = {
  {"unix-user:root", TRUE, TRUE, "com.example.awesomeproduct.foo",
      POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN,
      POLKIT_IMPLICIT_AUTHORIZATION_AUTHORIZED},
  {"unix-user:root", TRUE, FALSE, "com.example.awesomeproduct.foo",
      POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN,
      POLKIT_IMPLICIT_AUTHORIZATION_AUTHENTICATION_REQUIRED},
  {"unix-user:root", FALSE, FALSE, "com.example.awesomeproduct.foo",
      POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN,
      POLKIT_IMPLICIT_AUTHORIZATION_NOT_AUTHORIZED},
  {"unix-user:root", TRUE, TRUE, "com.example.restrictedproduct.foo",
      POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN,
      POLKIT_IMPLICIT_AUTHORIZATION_AUTHENTICATION_REQUIRED},
  {"unix-user:root", TRUE, TRUE, "com.example.missingproduct.foo",
      POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN,
      POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN},
  {NULL},
};


/* Automatically create many variations of the check_authorization_sync test */
static void
add_check_authorization_tests (void) {
  unsigned int i;
  for (i = 0; check_authorization_test_data[i].identity; i++) {
    struct auth_context *ctx = &check_authorization_test_data[i];
    gchar *test_name = g_strdup_printf (
        "/PolkitBackendLocalAuthority/check_authorization_sync_%d", i);
    g_test_add_data_func(test_name, ctx, test_check_authorization_sync);
  }
};


int
main (int argc, char *argv[])
{
  g_type_init ();
  g_test_init (&argc, &argv, NULL);
  polkit_test_redirect_logs ();

  // Register extension point only once. Required to create authority.
  GIOExtensionPoint *ep = g_io_extension_point_register (
      POLKIT_BACKEND_AUTHORITY_EXTENSION_POINT_NAME);
  g_io_extension_point_set_required_type (ep,
      POLKIT_BACKEND_TYPE_AUTHORITY);

  add_check_authorization_tests ();
  return g_test_run ();
};