2 * Copyright (C) 2013, 2014 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; If not, see
16 * <http://www.gnu.org/licenses/>.
23 # include <selinux/selinux.h>
26 #include "testutils.h"
28 #include "viridentity.h"
33 #include "virlockspace.h"
35 #define VIR_FROM_THIS VIR_FROM_NONE
37 VIR_LOG_INIT("tests.identitytest");
39 static int testIdentityAttrs(const void *data ATTRIBUTE_UNUSED
)
45 if (!(ident
= virIdentityNew()))
48 if (virIdentitySetAttr(ident
,
49 VIR_IDENTITY_ATTR_UNIX_USER_NAME
,
53 if (virIdentityGetAttr(ident
,
54 VIR_IDENTITY_ATTR_UNIX_USER_NAME
,
58 if (STRNEQ_NULLABLE(val
, "fred")) {
59 VIR_DEBUG("Expected 'fred' got '%s'", NULLSTR(val
));
63 if (virIdentityGetAttr(ident
,
64 VIR_IDENTITY_ATTR_UNIX_GROUP_NAME
,
69 VIR_DEBUG("Unexpected groupname attribute");
73 if (virIdentitySetAttr(ident
,
74 VIR_IDENTITY_ATTR_UNIX_USER_NAME
,
76 VIR_DEBUG("Unexpectedly overwrote attribute");
80 if (virIdentityGetAttr(ident
,
81 VIR_IDENTITY_ATTR_UNIX_USER_NAME
,
85 if (STRNEQ_NULLABLE(val
, "fred")) {
86 VIR_DEBUG("Expected 'fred' got '%s'", NULLSTR(val
));
92 virObjectUnref(ident
);
97 static int testIdentityEqual(const void *data ATTRIBUTE_UNUSED
)
100 virIdentityPtr identa
= NULL
;
101 virIdentityPtr identb
= NULL
;
103 if (!(identa
= virIdentityNew()))
105 if (!(identb
= virIdentityNew()))
108 if (!virIdentityIsEqual(identa
, identb
)) {
109 VIR_DEBUG("Empty identities were not equal");
113 if (virIdentitySetAttr(identa
,
114 VIR_IDENTITY_ATTR_UNIX_USER_NAME
,
118 if (virIdentityIsEqual(identa
, identb
)) {
119 VIR_DEBUG("Mis-matched identities should not be equal");
123 if (virIdentitySetAttr(identb
,
124 VIR_IDENTITY_ATTR_UNIX_USER_NAME
,
128 if (!virIdentityIsEqual(identa
, identb
)) {
129 VIR_DEBUG("Matched identities were not equal");
133 if (virIdentitySetAttr(identa
,
134 VIR_IDENTITY_ATTR_UNIX_GROUP_NAME
,
137 if (virIdentitySetAttr(identb
,
138 VIR_IDENTITY_ATTR_UNIX_GROUP_NAME
,
142 if (!virIdentityIsEqual(identa
, identb
)) {
143 VIR_DEBUG("Matched identities were not equal");
147 if (virIdentitySetAttr(identb
,
148 VIR_IDENTITY_ATTR_SASL_USER_NAME
,
149 "fred@FLINTSTONE.COM") < 0)
152 if (virIdentityIsEqual(identa
, identb
)) {
153 VIR_DEBUG("Mis-matched identities should not be equal");
159 virObjectUnref(identa
);
160 virObjectUnref(identb
);
164 static int testIdentityGetSystem(const void *data
)
166 const char *context
= data
;
168 virIdentityPtr ident
= NULL
;
173 VIR_DEBUG("libvirt not compiled with SELinux, skipping this test");
179 if (!(ident
= virIdentityGetSystem())) {
180 VIR_DEBUG("Unable to get system identity");
184 if (virIdentityGetAttr(ident
,
185 VIR_IDENTITY_ATTR_SELINUX_CONTEXT
,
189 if (STRNEQ_NULLABLE(val
, context
)) {
190 VIR_DEBUG("Unexpected SELinux context attribute");
196 virObjectUnref(ident
);
200 static int testSetFakeSELinuxContext(const void *data ATTRIBUTE_UNUSED
)
203 return setcon_raw((security_context_t
)data
);
205 VIR_DEBUG("libvirt not compiled with SELinux, skipping this test");
210 static int testDisableFakeSELinux(const void *data ATTRIBUTE_UNUSED
)
213 return security_disable();
215 VIR_DEBUG("libvirt not compiled with SELinux, skipping this test");
223 const char *context
= "unconfined_u:unconfined_r:unconfined_t:s0";
226 if (virTestRun("Identity attributes ", testIdentityAttrs
, NULL
) < 0)
228 if (virTestRun("Identity equality ", testIdentityEqual
, NULL
) < 0)
230 if (virTestRun("Setting fake SELinux context ", testSetFakeSELinuxContext
, context
) < 0)
232 if (virTestRun("System identity (fake SELinux enabled) ", testIdentityGetSystem
, context
) < 0)
234 if (virTestRun("Disabling fake SELinux ", testDisableFakeSELinux
, NULL
) < 0)
236 if (virTestRun("System identity (fake SELinux disabled) ", testIdentityGetSystem
, NULL
) < 0)
239 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
243 VIR_TEST_MAIN_PRELOAD(mymain
, abs_builddir
"/.libs/libsecurityselinuxhelper.so")
245 VIR_TEST_MAIN(mymain
)