2
* Copyright (C) 2015 Canonical, Ltd.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of version 2 of the GNU General Public
6
* License published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, contact Canonical Ltd.
24
#include <sys/apparmor.h>
27
#define OPT_REMOVE "remove"
28
#define OPT_REMOVE_POLICY "remove-policy"
29
#define OPT_REPLACE_ALL "replace-all"
30
#define OPT_FLAG_MAX_CACHES "--max-caches"
32
static void usage(const char *prog)
35
"FAIL - usage: %s %s [%s N] <PATH>\n"
37
" %s %s <PROFILE_NAME>\n"
38
" %s %s [%s N] <PATH>\n",
39
prog, OPT_NEW, OPT_FLAG_MAX_CACHES,
41
prog, OPT_REMOVE_POLICY,
42
prog, OPT_REPLACE_ALL, OPT_FLAG_MAX_CACHES);
45
static int test_new(const char *path, uint16_t max_caches)
47
aa_policy_cache *policy_cache = NULL;
50
if (aa_policy_cache_new(&policy_cache, NULL,
51
AT_FDCWD, path, max_caches)) {
52
perror("FAIL - aa_policy_cache_new");
58
aa_policy_cache_unref(policy_cache);
62
static int test_remove(const char *path)
66
if (aa_policy_cache_remove(AT_FDCWD, path)) {
67
perror("FAIL - aa_policy_cache_remove");
76
static int test_remove_policy(const char *name)
78
aa_kernel_interface *kernel_interface = NULL;
81
if (aa_kernel_interface_new(&kernel_interface, NULL, NULL)) {
82
perror("FAIL - aa_kernel_interface_new");
86
if (aa_kernel_interface_remove_policy(kernel_interface, name)) {
87
perror("FAIL - aa_kernel_interface_remove_policy");
93
aa_kernel_interface_unref(kernel_interface);
97
static int test_replace_all(const char *path, uint16_t max_caches)
99
aa_policy_cache *policy_cache = NULL;
102
if (aa_policy_cache_new(&policy_cache, NULL,
103
AT_FDCWD, path, max_caches)) {
104
perror("FAIL - aa_policy_cache_new");
108
if (aa_policy_cache_replace_all(policy_cache, NULL)) {
109
perror("FAIL - aa_policy_cache_replace_all");
115
aa_policy_cache_unref(policy_cache);
119
int main(int argc, char **argv)
121
uint16_t max_caches = 0;
122
const char *str = NULL;
125
if (!(argc == 3 || argc == 5)) {
130
str = argv[argc - 1];
136
tmp = strtoul(argv[3], NULL, 10);
137
if ((errno == ERANGE && tmp == ULONG_MAX) ||
138
(errno && tmp == 0)) {
139
perror("FAIL - strtoul");
143
if (tmp > UINT16_MAX) {
144
fprintf(stderr, "FAIL - %lu larger than UINT16_MAX\n",
152
if (strcmp(argv[1], OPT_NEW) == 0) {
153
rc = test_new(str, max_caches);
154
} else if (strcmp(argv[1], OPT_REMOVE) == 0 && argc == 3) {
155
rc = test_remove(str);
156
} else if (strcmp(argv[1], OPT_REMOVE_POLICY) == 0 && argc == 3) {
157
rc = test_remove_policy(str);
158
} else if (strcmp(argv[1], OPT_REPLACE_ALL) == 0) {
159
rc = test_replace_all(str, max_caches);