提交 8914b875 编写于 作者: M Michael Chapman 提交者: Michal Privoznik

tests: Test virIdentityGetSystem

Test it once with SELinux enabled and once with it disabled.
Signed-off-by: NMichael Chapman <mike@very.puzzling.org>
上级 1af9800b
...@@ -779,6 +779,10 @@ virstoragetest_LDADD = $(LDADDS) ...@@ -779,6 +779,10 @@ virstoragetest_LDADD = $(LDADDS)
viridentitytest_SOURCES = \ viridentitytest_SOURCES = \
viridentitytest.c testutils.h testutils.c viridentitytest.c testutils.h testutils.c
viridentitytest_LDADD = $(LDADDS) viridentitytest_LDADD = $(LDADDS)
if WITH_SELINUX
viridentitytest_DEPENDENCIES = libsecurityselinuxhelper.la \
../src/libvirt.la
endif WITH_SELINUX
virkeycodetest_SOURCES = \ virkeycodetest_SOURCES = \
virkeycodetest.c testutils.h testutils.c virkeycodetest.c testutils.h testutils.c
......
...@@ -22,6 +22,10 @@ ...@@ -22,6 +22,10 @@
#include <stdlib.h> #include <stdlib.h>
#if WITH_SELINUX
# include <selinux/selinux.h>
#endif
#include "testutils.h" #include "testutils.h"
#include "viridentity.h" #include "viridentity.h"
...@@ -148,7 +152,7 @@ static int testIdentityEqual(const void *data ATTRIBUTE_UNUSED) ...@@ -148,7 +152,7 @@ static int testIdentityEqual(const void *data ATTRIBUTE_UNUSED)
goto cleanup; goto cleanup;
if (virIdentityIsEqual(identa, identb)) { if (virIdentityIsEqual(identa, identb)) {
VIR_DEBUG("Mis-atched identities should not be equal"); VIR_DEBUG("Mis-matched identities should not be equal");
goto cleanup; goto cleanup;
} }
...@@ -159,17 +163,86 @@ cleanup: ...@@ -159,17 +163,86 @@ cleanup:
return ret; return ret;
} }
static int testIdentityGetSystem(const void *data)
{
const char *context = data;
int ret = -1;
virIdentityPtr ident = NULL;
const char *val;
#if !WITH_SELINUX
if (context) {
VIR_DEBUG("libvirt not compiled with SELinux, skipping this test");
ret = EXIT_AM_SKIP;
goto cleanup;
}
#endif
if (!(ident = virIdentityGetSystem())) {
VIR_DEBUG("Unable to get system identity");
goto cleanup;
}
if (virIdentityGetAttr(ident,
VIR_IDENTITY_ATTR_SELINUX_CONTEXT,
&val) < 0)
goto cleanup;
if (STRNEQ_NULLABLE(val, context)) {
VIR_DEBUG("Unexpected SELinux context attribute");
goto cleanup;
}
ret = 0;
cleanup:
virObjectUnref(ident);
return ret;
}
static int testSetFakeSELinuxContext(const void *data ATTRIBUTE_UNUSED)
{
#if WITH_SELINUX
return setcon_raw((security_context_t)data);
#else
VIR_DEBUG("libvirt not compiled with SELinux, skipping this test");
return EXIT_AM_SKIP;
#endif
}
static int testDisableFakeSELinux(const void *data ATTRIBUTE_UNUSED)
{
#if WITH_SELINUX
return security_disable();
#else
VIR_DEBUG("libvirt not compiled with SELinux, skipping this test");
return EXIT_AM_SKIP;
#endif
}
static int static int
mymain(void) mymain(void)
{ {
const char *context = "unconfined_u:unconfined_r:unconfined_t:s0";
int ret = 0; int ret = 0;
if (virtTestRun("Identity attributes ", testIdentityAttrs, NULL) < 0) if (virtTestRun("Identity attributes ", testIdentityAttrs, NULL) < 0)
ret = -1; ret = -1;
if (virtTestRun("Identity equality ", testIdentityEqual, NULL) < 0) if (virtTestRun("Identity equality ", testIdentityEqual, NULL) < 0)
ret = -1; ret = -1;
if (virtTestRun("Setting fake SELinux context ", testSetFakeSELinuxContext, context) < 0)
ret = -1;
if (virtTestRun("System identity (fake SELinux enabled) ", testIdentityGetSystem, context) < 0)
ret = -1;
if (virtTestRun("Disabling fake SELinux ", testDisableFakeSELinux, NULL) < 0)
ret = -1;
if (virtTestRun("System identity (fake SELinux disabled) ", testIdentityGetSystem, NULL) < 0)
ret = -1;
return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE; return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
} }
#if WITH_SELINUX
VIRT_TEST_MAIN_PRELOAD(mymain, abs_builddir "/.libs/libsecurityselinuxhelper.so")
#else
VIRT_TEST_MAIN(mymain) VIRT_TEST_MAIN(mymain)
#endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册