~apparmor-dev/apparmor/master

« back to all changes in this revision

Viewing changes to tests/regression/apparmor/aa_policy_cache.c

  • Committer: Steve Beattie
  • Date: 2019-02-19 09:38:13 UTC
  • Revision ID: sbeattie@ubuntu.com-20190219093813-ud526ee6hwn8nljz
The AppArmor project has been converted to git and is now hosted on
gitlab.

To get the converted repository, please do
  git clone https://gitlab.com/apparmor/apparmor

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2015 Canonical, Ltd.
3
 
 *
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.
7
 
 *
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.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License
14
 
 * along with this program; if not, contact Canonical Ltd.
15
 
 */
16
 
 
17
 
#include <errno.h>
18
 
#include <fcntl.h>
19
 
#include <limits.h>
20
 
#include <stdio.h>
21
 
#include <stdlib.h>
22
 
#include <string.h>
23
 
 
24
 
#include <sys/apparmor.h>
25
 
 
26
 
#define OPT_NEW                 "new"
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"
31
 
 
32
 
static void usage(const char *prog)
33
 
{
34
 
        fprintf(stderr,
35
 
                "FAIL - usage: %s %s [%s N] <PATH>\n"
36
 
                "              %s %s <PATH>\n"
37
 
                "              %s %s <PROFILE_NAME>\n"
38
 
                "              %s %s [%s N] <PATH>\n",
39
 
                prog, OPT_NEW, OPT_FLAG_MAX_CACHES,
40
 
                prog, OPT_REMOVE,
41
 
                prog, OPT_REMOVE_POLICY,
42
 
                prog, OPT_REPLACE_ALL, OPT_FLAG_MAX_CACHES);
43
 
}
44
 
 
45
 
static int test_new(const char *path, uint16_t max_caches)
46
 
{
47
 
        aa_policy_cache *policy_cache = NULL;
48
 
        int rc = 1;
49
 
 
50
 
        if (aa_policy_cache_new(&policy_cache, NULL,
51
 
                                AT_FDCWD, path, max_caches)) {
52
 
                perror("FAIL - aa_policy_cache_new");
53
 
                goto out;
54
 
        }
55
 
 
56
 
        rc = 0;
57
 
out:
58
 
        aa_policy_cache_unref(policy_cache);
59
 
        return rc;
60
 
}
61
 
 
62
 
static int test_remove(const char *path)
63
 
{
64
 
        int rc = 1;
65
 
 
66
 
        if (aa_policy_cache_remove(AT_FDCWD, path)) {
67
 
                perror("FAIL - aa_policy_cache_remove");
68
 
                goto out;
69
 
        }
70
 
 
71
 
        rc = 0;
72
 
out:
73
 
        return rc;
74
 
}
75
 
 
76
 
static int test_remove_policy(const char *name)
77
 
{
78
 
        aa_kernel_interface *kernel_interface = NULL;
79
 
        int rc = 1;
80
 
 
81
 
        if (aa_kernel_interface_new(&kernel_interface, NULL, NULL)) {
82
 
                perror("FAIL - aa_kernel_interface_new");
83
 
                goto out;
84
 
        }
85
 
 
86
 
        if (aa_kernel_interface_remove_policy(kernel_interface, name)) {
87
 
                perror("FAIL - aa_kernel_interface_remove_policy");
88
 
                goto out;
89
 
        }
90
 
 
91
 
        rc = 0;
92
 
out:
93
 
        aa_kernel_interface_unref(kernel_interface);
94
 
        return rc;
95
 
}
96
 
 
97
 
static int test_replace_all(const char *path, uint16_t max_caches)
98
 
{
99
 
        aa_policy_cache *policy_cache = NULL;
100
 
        int rc = 1;
101
 
 
102
 
        if (aa_policy_cache_new(&policy_cache, NULL,
103
 
                                AT_FDCWD, path, max_caches)) {
104
 
                perror("FAIL - aa_policy_cache_new");
105
 
                goto out;
106
 
        }
107
 
 
108
 
        if (aa_policy_cache_replace_all(policy_cache, NULL)) {
109
 
                perror("FAIL - aa_policy_cache_replace_all");
110
 
                goto out;
111
 
        }
112
 
 
113
 
        rc = 0;
114
 
out:
115
 
        aa_policy_cache_unref(policy_cache);
116
 
        return rc;
117
 
}
118
 
 
119
 
int main(int argc, char **argv)
120
 
{
121
 
        uint16_t max_caches = 0;
122
 
        const char *str = NULL;
123
 
        int rc = 1;
124
 
 
125
 
        if (!(argc == 3 || argc == 5)) {
126
 
                usage(argv[0]);
127
 
                exit(1);
128
 
        }
129
 
 
130
 
        str = argv[argc - 1];
131
 
 
132
 
        if (argc == 5) {
133
 
                unsigned long tmp;
134
 
 
135
 
                errno = 0;
136
 
                tmp = strtoul(argv[3], NULL, 10);
137
 
                if ((errno == ERANGE && tmp == ULONG_MAX) ||
138
 
                    (errno && tmp == 0)) {
139
 
                        perror("FAIL - strtoul");
140
 
                        exit(1);
141
 
                }
142
 
 
143
 
                if (tmp > UINT16_MAX) {
144
 
                        fprintf(stderr, "FAIL - %lu larger than UINT16_MAX\n",
145
 
                                tmp);
146
 
                        exit(1);
147
 
                }
148
 
 
149
 
                max_caches = tmp;
150
 
        }
151
 
 
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);
160
 
        } else {
161
 
                usage(argv[0]);
162
 
        }
163
 
 
164
 
        if (!rc)
165
 
                printf("PASS\n");
166
 
 
167
 
        exit(rc);
168
 
}