~apparmor-dev/apparmor/master

« back to all changes in this revision

Viewing changes to libraries/libapparmor/src/tst_kernel.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
3
 
 *   Canonical, Ltd. (All rights reserved)
4
 
 *
5
 
 *   This program is free software; you can redistribute it and/or
6
 
 *   modify it under the terms of version 2 of the GNU General Public
7
 
 *   License published by the Free Software Foundation.
8
 
 *
9
 
 *   This program is distributed in the hope that it will be useful,
10
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 *   GNU General Public License for more details.
13
 
 *
14
 
 *   You should have received a copy of the GNU General Public License
15
 
 *   along with this program; if not, contact Novell, Inc. or Canonical
16
 
 *   Ltd.
17
 
 */
18
 
 
19
 
#include <stdio.h>
20
 
#include <string.h>
21
 
 
22
 
#include "kernel.c"
23
 
 
24
 
static int nullcmp_and_strcmp(const void *s1, const void *s2)
25
 
{
26
 
        /* Return 0 if both pointers are NULL & non-zero if only one is NULL */
27
 
        if (!s1 || !s2)
28
 
                return s1 != s2;
29
 
 
30
 
        return strcmp(s1, s2);
31
 
}
32
 
 
33
 
static int do_test_splitcon(char *con, int size, bool strip_nl, char **mode,
34
 
                            const char *expected_label,
35
 
                            const char *expected_mode, const char *error)
36
 
{
37
 
        char *label;
38
 
        int rc = 0;
39
 
 
40
 
        label = splitcon(con, size, strip_nl, mode);
41
 
 
42
 
        if (nullcmp_and_strcmp(label, expected_label)) {
43
 
                fprintf(stderr, "FAIL: %s: label \"%s\" != \"%s\"\n",
44
 
                        error, label, expected_label);
45
 
                rc = 1;
46
 
        }
47
 
 
48
 
        if (mode && nullcmp_and_strcmp(*mode, expected_mode)) {
49
 
                fprintf(stderr, "FAIL: %s: mode \"%s\" != \"%s\"\n",
50
 
                        error, *mode, expected_mode);
51
 
                rc = 1;
52
 
        }
53
 
 
54
 
        return rc;
55
 
}
56
 
 
57
 
static int do_test_aa_splitcon(char *con, char **mode,
58
 
                               const char *expected_label,
59
 
                               const char *expected_mode, const char *error)
60
 
{
61
 
        char *label;
62
 
        int rc = 0;
63
 
 
64
 
        label = aa_splitcon(con, mode);
65
 
 
66
 
        if (nullcmp_and_strcmp(label, expected_label)) {
67
 
                fprintf(stderr, "FAIL: %s: label \"%s\" != \"%s\"\n",
68
 
                        error, label, expected_label);
69
 
                rc = 1;
70
 
        }
71
 
 
72
 
        if (mode && nullcmp_and_strcmp(*mode, expected_mode)) {
73
 
                fprintf(stderr, "FAIL: %s: mode \"%s\" != \"%s\"\n",
74
 
                        error, *mode, expected_mode);
75
 
                rc = 1;
76
 
        }
77
 
 
78
 
        return rc;
79
 
}
80
 
 
81
 
#define TEST_SPLITCON(con, size, strip_nl, expected_label,              \
82
 
                      expected_mode, error)                             \
83
 
        do {                                                            \
84
 
                char c1[] = con;                                        \
85
 
                char c2[] = con;                                        \
86
 
                size_t sz = size < 0 ? strlen(con) : size;              \
87
 
                char *mode;                                             \
88
 
                                                                        \
89
 
                if (do_test_splitcon(c1, sz, strip_nl, &mode,           \
90
 
                                expected_label, expected_mode,          \
91
 
                                "splitcon: " error)) {                  \
92
 
                        rc = 1;                                         \
93
 
                } else if (do_test_splitcon(c2, sz, strip_nl, NULL,     \
94
 
                                expected_label, NULL,                   \
95
 
                                "splitcon: " error " (NULL mode)")) {   \
96
 
                        rc = 1;                                         \
97
 
                }                                                       \
98
 
        } while (0)
99
 
 
100
 
#define TEST_AA_SPLITCON(con, expected_label, expected_mode, error)     \
101
 
        do {                                                            \
102
 
                char c1[] = con;                                        \
103
 
                char c2[] = con;                                        \
104
 
                char c3[] = con "\n";                                   \
105
 
                char *mode;                                             \
106
 
                                                                        \
107
 
                if (do_test_aa_splitcon(c1, &mode, expected_label,      \
108
 
                                expected_mode, "aa_splitcon: " error)) {\
109
 
                        rc = 1;                                         \
110
 
                } else if (do_test_aa_splitcon(c2, NULL, expected_label,\
111
 
                                NULL,                                   \
112
 
                                "aa_splitcon: " error " (NULL mode)")) {\
113
 
                        rc = 1;                                         \
114
 
                } else if (do_test_aa_splitcon(c3, &mode,               \
115
 
                                expected_label, expected_mode,          \
116
 
                                "aa_splitcon: " error " (newline)")) {  \
117
 
                        rc = 1;                                         \
118
 
                }                                                       \
119
 
        } while (0)
120
 
 
121
 
static int test_splitcon(void)
122
 
{
123
 
        int rc = 0;
124
 
 
125
 
        /**
126
 
         * NOTE: the TEST_SPLITCON() macro automatically generates
127
 
         * corresponding tests with a NULL mode pointer.
128
 
         */
129
 
 
130
 
        TEST_SPLITCON("", 0, true, NULL, NULL, "empty string test #1");
131
 
        TEST_SPLITCON("", 0, false, NULL, NULL, "empty string test #2");
132
 
 
133
 
        TEST_SPLITCON("unconfined", -1, true, "unconfined", NULL,
134
 
                      "unconfined #1");
135
 
        TEST_SPLITCON("unconfined", -1, false, "unconfined", NULL,
136
 
                      "unconfined #2");
137
 
        TEST_SPLITCON("unconfined\n", -1, true, "unconfined", NULL,
138
 
                      "unconfined #3");
139
 
        TEST_SPLITCON("unconfined\n", -1, false, NULL, NULL,
140
 
                      "unconfined #4");
141
 
 
142
 
        TEST_SPLITCON("label (mode)", -1, true, "label", "mode",
143
 
                      "basic split #1");
144
 
        TEST_SPLITCON("label (mode)", -1, false, "label", "mode",
145
 
                      "basic split #2");
146
 
        TEST_SPLITCON("label (mode)\n", -1, true, "label", "mode",
147
 
                      "basic split #3");
148
 
        TEST_SPLITCON("label (mode)\n", -1, false, NULL, NULL,
149
 
                      "basic split #4");
150
 
 
151
 
        TEST_SPLITCON("/a/b/c (enforce)", -1, true, "/a/b/c", "enforce",
152
 
                      "path enforce split #1");
153
 
        TEST_SPLITCON("/a/b/c (enforce)", -1, false, "/a/b/c", "enforce",
154
 
                      "path enforce split #2");
155
 
        TEST_SPLITCON("/a/b/c (enforce)\n", -1, true, "/a/b/c", "enforce",
156
 
                      "path enforce split #3");
157
 
        TEST_SPLITCON("/a/b/c (enforce)\n", -1, false, NULL, NULL,
158
 
                      "path enforce split #4");
159
 
 
160
 
        return rc;
161
 
}
162
 
 
163
 
 
164
 
static int test_aa_splitcon(void)
165
 
{
166
 
        int rc = 0;
167
 
 
168
 
        /**
169
 
         * NOTE: the TEST_AA_SPLITCON() macro automatically generates
170
 
         * corresponding tests with a NULL mode pointer and contexts with
171
 
         * trailing newline characters.
172
 
         */
173
 
 
174
 
        TEST_AA_SPLITCON("label (mode)", "label", "mode", "basic split");
175
 
 
176
 
        TEST_AA_SPLITCON("/a/b/c (enforce)", "/a/b/c", "enforce",
177
 
                         "path enforce split");
178
 
 
179
 
        TEST_AA_SPLITCON("/a/b/c (complain)", "/a/b/c", "complain",
180
 
                         "path complain split");
181
 
 
182
 
        TEST_AA_SPLITCON("profile_name (enforce)", "profile_name", "enforce",
183
 
                         "name enforce split");
184
 
 
185
 
        TEST_AA_SPLITCON("profile_name (complain)", "profile_name", "complain",
186
 
                         "name complain split");
187
 
 
188
 
        TEST_AA_SPLITCON("unconfined", "unconfined", NULL, "unconfined");
189
 
 
190
 
        TEST_AA_SPLITCON("(odd) (enforce)", "(odd)", "enforce",
191
 
                         "parenthesized label #1");
192
 
 
193
 
        TEST_AA_SPLITCON("(odd) (enforce) (enforce)", "(odd) (enforce)",
194
 
                         "enforce", "parenthesized label #2");
195
 
 
196
 
        TEST_AA_SPLITCON("/usr/bin/😺 (enforce)", "/usr/bin/😺", "enforce",
197
 
                         "non-ASCII path");
198
 
 
199
 
        TEST_AA_SPLITCON("👍 (enforce)", "👍", "enforce",
200
 
                         "non-ASCII profile name");
201
 
 
202
 
        /* Negative tests */
203
 
 
204
 
        TEST_AA_SPLITCON("", NULL, NULL, "empty string test");
205
 
 
206
 
        TEST_AA_SPLITCON("profile\t(enforce)", NULL, NULL,
207
 
                         "invalid tab separator");
208
 
 
209
 
        TEST_AA_SPLITCON("profile(enforce)", NULL, NULL,
210
 
                         "invalid missing separator");
211
 
 
212
 
        return rc;
213
 
}
214
 
 
215
 
int main(void)
216
 
{
217
 
        int retval, rc = 0;
218
 
 
219
 
        retval = test_splitcon();
220
 
        if (retval)
221
 
                rc = retval;
222
 
 
223
 
        retval = test_aa_splitcon();
224
 
        if (retval)
225
 
                rc = retval;
226
 
 
227
 
        return rc;
228
 
}