From b642eddc31646e40e1211ca56ea93cc20af5e88d Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Fri, 15 Jun 2018 14:51:19 +1000 Subject: [PATCH] test: sort-of autodetect whether we need root privileges It's not really autodetection, we just declare the test suites that need root privs. But this way we can generically check for it from the main() that we re-use across tests. Signed-off-by: Peter Hutterer --- test/test-common.h | 12 ++++++++++-- test/test-libevdev-events.c | 2 +- test/test-libevdev-has-event.c | 2 +- test/test-libevdev-init.c | 2 +- test/test-main.c | 11 ++++++++--- test/test-uinput.c | 2 +- 6 files changed, 22 insertions(+), 9 deletions(-) diff --git a/test/test-common.h b/test/test-common.h index 2f258f3..dc90cc9 100644 --- a/test/test-common.h +++ b/test/test-common.h @@ -22,6 +22,7 @@ #include #include +#include #include @@ -31,17 +32,24 @@ struct libevdev_test { const char *name; Suite* (*setup)(void); + bool needs_root_privileges; } __attribute__((aligned(16))); -#define TEST_SUITE(name) \ +#define _TEST_SUITE(name, root_privs) \ static Suite* (name##_setup)(void); \ static const struct libevdev_test _test \ __attribute__((used)) \ __attribute__((section ("test_section"))) = { \ - #name, name##_setup \ + #name, name##_setup, root_privs \ }; \ static Suite* (name##_setup)(void) +#define TEST_SUITE(name) \ + _TEST_SUITE(name, false) + +#define TEST_SUITE_ROOT_PRIVILEGES(name) \ + _TEST_SUITE(name, true) + #define TEST_DEVICE_NAME "libevdev test device" #include "test-common-uinput.h" diff --git a/test/test-libevdev-events.c b/test/test-libevdev-events.c index 6055d59..165490d 100644 --- a/test/test-libevdev-events.c +++ b/test/test-libevdev-events.c @@ -2140,7 +2140,7 @@ START_TEST(test_event_mt_value_setters_current_slot) } END_TEST -TEST_SUITE(libevdev_events) +TEST_SUITE_ROOT_PRIVILEGES(libevdev_events) { Suite *s = suite_create("libevdev event tests"); diff --git a/test/test-libevdev-has-event.c b/test/test-libevdev-has-event.c index 07646de..74780a5 100644 --- a/test/test-libevdev-has-event.c +++ b/test/test-libevdev-has-event.c @@ -1142,7 +1142,7 @@ START_TEST(test_led_same) } END_TEST -TEST_SUITE(has_events) +TEST_SUITE_ROOT_PRIVILEGES(has_events) { Suite *s = suite_create("libevdev_has_event tests"); diff --git a/test/test-libevdev-init.c b/test/test-libevdev-init.c index c5d7d8c..67bf729 100644 --- a/test/test-libevdev-init.c +++ b/test/test-libevdev-init.c @@ -683,7 +683,7 @@ START_TEST(test_clock_id_events) } END_TEST -TEST_SUITE(libevdev_init_test) +TEST_SUITE_ROOT_PRIVILEGES(libevdev_init_test) { Suite *s = suite_create("libevdev init tests"); diff --git a/test/test-main.c b/test/test-main.c index b1c82cc..42a8802 100644 --- a/test/test-main.c +++ b/test/test-main.c @@ -68,9 +68,14 @@ int main(void) const struct rlimit corelimit = {0, 0}; int failed; - if (getuid() != 0) { - fprintf(stderr, "This test needs to run as root\n"); - return 77; + for (t = &__start_test_section; t < &__stop_test_section; t++) { + if (t->needs_root_privileges) { + if (getuid() != 0) { + fprintf(stderr, "This test needs to run as root\n"); + return 77; + } + break; + } } if (is_debugger_attached()) diff --git a/test/test-uinput.c b/test/test-uinput.c index e538050..d9bee36 100644 --- a/test/test-uinput.c +++ b/test/test-uinput.c @@ -368,7 +368,7 @@ START_TEST(test_uinput_properties) } END_TEST -TEST_SUITE(uinput_suite) +TEST_SUITE_ROOT_PRIVILEGES(uinput_suite) { Suite *s = suite_create("libevdev uinput device tests"); -- 2.7.4