2 * QEMU list authorization object tests
4 * Copyright (c) 2018 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu/main-loop.h"
23 #include "qemu/module.h"
24 #include "authz/listfile.h"
28 static gchar
*qemu_authz_listfile_test_save(const gchar
*name
,
31 gchar
*path
= g_strdup_printf("%s/default-deny.cfg", workdir
);
34 if (!g_file_set_contents(path
, cfg
, -1, &gerr
)) {
35 g_printerr("Unable to save config %s: %s\n",
46 static void test_authz_default_deny(void)
48 gchar
*file
= qemu_authz_listfile_test_save(
50 "{ \"policy\": \"deny\" }");
51 Error
*local_err
= NULL
;
53 QAuthZListFile
*auth
= qauthz_list_file_new("auth0",
58 g_assert(local_err
== NULL
);
59 g_assert(!qauthz_is_allowed(QAUTHZ(auth
), "fred", &error_abort
));
61 object_unparent(OBJECT(auth
));
64 static void test_authz_default_allow(void)
66 gchar
*file
= qemu_authz_listfile_test_save(
68 "{ \"policy\": \"allow\" }");
69 Error
*local_err
= NULL
;
71 QAuthZListFile
*auth
= qauthz_list_file_new("auth0",
76 g_assert(local_err
== NULL
);
77 g_assert(qauthz_is_allowed(QAUTHZ(auth
), "fred", &error_abort
));
79 object_unparent(OBJECT(auth
));
82 static void test_authz_explicit_deny(void)
84 gchar
*file
= qemu_authz_listfile_test_save(
87 " { \"match\": \"fred\","
88 " \"policy\": \"deny\","
89 " \"format\": \"exact\" } ],"
90 " \"policy\": \"allow\" }");
91 Error
*local_err
= NULL
;
93 QAuthZListFile
*auth
= qauthz_list_file_new("auth0",
98 g_assert(local_err
== NULL
);
100 g_assert(!qauthz_is_allowed(QAUTHZ(auth
), "fred", &error_abort
));
102 object_unparent(OBJECT(auth
));
105 static void test_authz_explicit_allow(void)
107 gchar
*file
= qemu_authz_listfile_test_save(
108 "explicit-allow.cfg",
110 " { \"match\": \"fred\","
111 " \"policy\": \"allow\","
112 " \"format\": \"exact\" } ],"
113 " \"policy\": \"deny\" }");
114 Error
*local_err
= NULL
;
116 QAuthZListFile
*auth
= qauthz_list_file_new("auth0",
121 g_assert(local_err
== NULL
);
123 g_assert(qauthz_is_allowed(QAUTHZ(auth
), "fred", &error_abort
));
125 object_unparent(OBJECT(auth
));
129 static void test_authz_complex(void)
131 gchar
*file
= qemu_authz_listfile_test_save(
134 " { \"match\": \"fred\","
135 " \"policy\": \"allow\","
136 " \"format\": \"exact\" },"
137 " { \"match\": \"bob\","
138 " \"policy\": \"allow\","
139 " \"format\": \"exact\" },"
140 " { \"match\": \"dan\","
141 " \"policy\": \"deny\","
142 " \"format\": \"exact\" },"
143 " { \"match\": \"dan*\","
144 " \"policy\": \"allow\","
145 " \"format\": \"glob\" } ],"
146 " \"policy\": \"deny\" }");
148 Error
*local_err
= NULL
;
150 QAuthZListFile
*auth
= qauthz_list_file_new("auth0",
155 g_assert(local_err
== NULL
);
157 g_assert(qauthz_is_allowed(QAUTHZ(auth
), "fred", &error_abort
));
158 g_assert(qauthz_is_allowed(QAUTHZ(auth
), "bob", &error_abort
));
159 g_assert(!qauthz_is_allowed(QAUTHZ(auth
), "dan", &error_abort
));
160 g_assert(qauthz_is_allowed(QAUTHZ(auth
), "danb", &error_abort
));
162 object_unparent(OBJECT(auth
));
166 int main(int argc
, char **argv
)
171 g_test_init(&argc
, &argv
, NULL
);
173 module_call_init(MODULE_INIT_QOM
);
175 workdir
= g_dir_make_tmp("qemu-test-authz-listfile-XXXXXX",
178 g_printerr("Unable to create temporary dir: %s\n",
184 g_test_add_func("/auth/list/default/deny", test_authz_default_deny
);
185 g_test_add_func("/auth/list/default/allow", test_authz_default_allow
);
186 g_test_add_func("/auth/list/explicit/deny", test_authz_explicit_deny
);
187 g_test_add_func("/auth/list/explicit/allow", test_authz_explicit_allow
);
188 g_test_add_func("/auth/list/complex", test_authz_complex
);