3
* Canonical, Ltd. (All rights reserved)
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.
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.
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
24
static int nullcmp_and_strcmp(const void *s1, const void *s2)
26
/* Return 0 if both pointers are NULL & non-zero if only one is NULL */
30
return strcmp(s1, s2);
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)
40
label = splitcon(con, size, strip_nl, mode);
42
if (nullcmp_and_strcmp(label, expected_label)) {
43
fprintf(stderr, "FAIL: %s: label \"%s\" != \"%s\"\n",
44
error, label, expected_label);
48
if (mode && nullcmp_and_strcmp(*mode, expected_mode)) {
49
fprintf(stderr, "FAIL: %s: mode \"%s\" != \"%s\"\n",
50
error, *mode, expected_mode);
57
static int do_test_aa_splitcon(char *con, char **mode,
58
const char *expected_label,
59
const char *expected_mode, const char *error)
64
label = aa_splitcon(con, mode);
66
if (nullcmp_and_strcmp(label, expected_label)) {
67
fprintf(stderr, "FAIL: %s: label \"%s\" != \"%s\"\n",
68
error, label, expected_label);
72
if (mode && nullcmp_and_strcmp(*mode, expected_mode)) {
73
fprintf(stderr, "FAIL: %s: mode \"%s\" != \"%s\"\n",
74
error, *mode, expected_mode);
81
#define TEST_SPLITCON(con, size, strip_nl, expected_label, \
82
expected_mode, error) \
86
size_t sz = size < 0 ? strlen(con) : size; \
89
if (do_test_splitcon(c1, sz, strip_nl, &mode, \
90
expected_label, expected_mode, \
91
"splitcon: " error)) { \
93
} else if (do_test_splitcon(c2, sz, strip_nl, NULL, \
94
expected_label, NULL, \
95
"splitcon: " error " (NULL mode)")) { \
100
#define TEST_AA_SPLITCON(con, expected_label, expected_mode, error) \
104
char c3[] = con "\n"; \
107
if (do_test_aa_splitcon(c1, &mode, expected_label, \
108
expected_mode, "aa_splitcon: " error)) {\
110
} else if (do_test_aa_splitcon(c2, NULL, expected_label,\
112
"aa_splitcon: " error " (NULL mode)")) {\
114
} else if (do_test_aa_splitcon(c3, &mode, \
115
expected_label, expected_mode, \
116
"aa_splitcon: " error " (newline)")) { \
121
static int test_splitcon(void)
126
* NOTE: the TEST_SPLITCON() macro automatically generates
127
* corresponding tests with a NULL mode pointer.
130
TEST_SPLITCON("", 0, true, NULL, NULL, "empty string test #1");
131
TEST_SPLITCON("", 0, false, NULL, NULL, "empty string test #2");
133
TEST_SPLITCON("unconfined", -1, true, "unconfined", NULL,
135
TEST_SPLITCON("unconfined", -1, false, "unconfined", NULL,
137
TEST_SPLITCON("unconfined\n", -1, true, "unconfined", NULL,
139
TEST_SPLITCON("unconfined\n", -1, false, NULL, NULL,
142
TEST_SPLITCON("label (mode)", -1, true, "label", "mode",
144
TEST_SPLITCON("label (mode)", -1, false, "label", "mode",
146
TEST_SPLITCON("label (mode)\n", -1, true, "label", "mode",
148
TEST_SPLITCON("label (mode)\n", -1, false, NULL, NULL,
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");
164
static int test_aa_splitcon(void)
169
* NOTE: the TEST_AA_SPLITCON() macro automatically generates
170
* corresponding tests with a NULL mode pointer and contexts with
171
* trailing newline characters.
174
TEST_AA_SPLITCON("label (mode)", "label", "mode", "basic split");
176
TEST_AA_SPLITCON("/a/b/c (enforce)", "/a/b/c", "enforce",
177
"path enforce split");
179
TEST_AA_SPLITCON("/a/b/c (complain)", "/a/b/c", "complain",
180
"path complain split");
182
TEST_AA_SPLITCON("profile_name (enforce)", "profile_name", "enforce",
183
"name enforce split");
185
TEST_AA_SPLITCON("profile_name (complain)", "profile_name", "complain",
186
"name complain split");
188
TEST_AA_SPLITCON("unconfined", "unconfined", NULL, "unconfined");
190
TEST_AA_SPLITCON("(odd) (enforce)", "(odd)", "enforce",
191
"parenthesized label #1");
193
TEST_AA_SPLITCON("(odd) (enforce) (enforce)", "(odd) (enforce)",
194
"enforce", "parenthesized label #2");
196
TEST_AA_SPLITCON("/usr/bin/😺 (enforce)", "/usr/bin/😺", "enforce",
199
TEST_AA_SPLITCON("👍 (enforce)", "👍", "enforce",
200
"non-ASCII profile name");
204
TEST_AA_SPLITCON("", NULL, NULL, "empty string test");
206
TEST_AA_SPLITCON("profile\t(enforce)", NULL, NULL,
207
"invalid tab separator");
209
TEST_AA_SPLITCON("profile(enforce)", NULL, NULL,
210
"invalid missing separator");
219
retval = test_splitcon();
223
retval = test_aa_splitcon();