[PATCH libevdev 2/3] test: abort on unexpected log messages

Peter Hutterer peter.hutterer at who-t.net
Thu Jan 16 19:31:54 PST 2014


Add two log functions, one that aborts on a received message. We know when we
expect to receive an error, so anytime this happens unexpectedly should
terminate the test.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 test/test-common.c        | 21 +++++++++++++++++++++
 test/test-common.h        | 10 ++++++++++
 test/test-libevdev-init.c | 10 ++++++++++
 test/test-main.c          |  5 +++++
 test/test-uinput.c        |  2 ++
 5 files changed, 48 insertions(+)

diff --git a/test/test-common.c b/test/test-common.c
index b33a2cd..73ae3bc 100644
--- a/test/test-common.c
+++ b/test/test-common.c
@@ -24,9 +24,30 @@
 #include <check.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <stdio.h>
 
 #include "test-common.h"
 
+void test_logfunc_abort_on_error(enum libevdev_log_priority priority,
+				 void *data,
+				 const char *file, int line,
+				 const char *func,
+				 const char *format, va_list args)
+{
+	vprintf(format, args);
+	ck_abort();
+}
+
+void test_logfunc_ignore_error(enum libevdev_log_priority priority,
+			       void *data,
+			       const char *file, int line,
+			       const char *func,
+			       const char *format, va_list args)
+{
+	printf("Expected log message: ");
+	vprintf(format, args);
+}
+
 int test_create_device(struct uinput_device **uidev_return,
 		       struct libevdev **dev_return,
 		       ...)
diff --git a/test/test-common.h b/test/test-common.h
index 16b5800..d7a33d0 100644
--- a/test/test-common.h
+++ b/test/test-common.h
@@ -42,4 +42,14 @@ int test_create_abs_device(struct uinput_device **uidev,
 			   const struct input_absinfo *abs,
 			   ...);
 
+void test_logfunc_abort_on_error(enum libevdev_log_priority priority,
+				 void *data,
+				 const char *file, int line,
+				 const char *func,
+				 const char *format, va_list args);
+void test_logfunc_ignore_error(enum libevdev_log_priority priority,
+			       void *data,
+			       const char *file, int line,
+			       const char *func,
+			       const char *format, va_list args);
 #endif /* _TEST_COMMON_H_ */
diff --git a/test/test-libevdev-init.c b/test/test-libevdev-init.c
index bf0b874..6be3d74 100644
--- a/test/test-libevdev-init.c
+++ b/test/test-libevdev-init.c
@@ -73,7 +73,10 @@ START_TEST(test_init_and_change_fd)
 	dev = libevdev_new();
 	ck_assert(dev != NULL);
 	ck_assert_int_eq(libevdev_set_fd(dev, -1), -EBADF);
+
+	libevdev_set_log_function(test_logfunc_ignore_error, NULL);
 	ck_assert_int_eq(libevdev_change_fd(dev, -1), -1);
+	libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
 
 	rc = uinput_device_new_with_events(&uidev,
 					   TEST_DEVICE_NAME, DEFAULT_IDS,
@@ -87,7 +90,10 @@ START_TEST(test_init_and_change_fd)
 					   -1);
 	ck_assert_msg(rc == 0, "Failed to create uinput device: %s", strerror(-rc));
 	ck_assert_int_eq(libevdev_set_fd(dev, uinput_device_get_fd(uidev)), 0);
+
+	libevdev_set_log_function(test_logfunc_ignore_error, NULL);
 	ck_assert_int_eq(libevdev_set_fd(dev, 0), -EBADF);
+	libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
 
 	ck_assert_int_eq(libevdev_get_fd(dev), uinput_device_get_fd(uidev));
 
@@ -136,6 +142,8 @@ START_TEST(test_log_init)
 	ck_assert_int_eq(log_fn_called, 2);
 
 	libevdev_free(dev);
+
+	libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
 }
 END_TEST
 
@@ -247,10 +255,12 @@ START_TEST(test_device_grab)
 				-1);
 	ck_assert_msg(rc == 0, "Failed to create device: %s", strerror(-rc));
 
+	libevdev_set_log_function(test_logfunc_ignore_error, NULL);
 	rc = libevdev_grab(dev, 0);
 	ck_assert_int_eq(rc, -EINVAL);
 	rc = libevdev_grab(dev, 1);
 	ck_assert_int_eq(rc, -EINVAL);
+	libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
 
 	rc = libevdev_grab(dev, LIBEVDEV_UNGRAB);
 	ck_assert_int_eq(rc, 0);
diff --git a/test/test-main.c b/test/test-main.c
index 91128c3..6d17f7c 100644
--- a/test/test-main.c
+++ b/test/test-main.c
@@ -26,6 +26,9 @@
 #include <unistd.h>
 #include <sys/ptrace.h>
 #include <sys/wait.h>
+#include <libevdev/libevdev.h>
+
+#include "test-common.h"
 
 extern Suite *event_name_suite(void);
 extern Suite *event_code_suite(void);
@@ -71,6 +74,8 @@ int main(int argc, char **argv)
 	if (is_debugger_attached())
 		setenv("CK_FORK", "no", 0);
 
+	libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
+
 	Suite *s = libevdev_has_event_test();
 	SRunner *sr = srunner_create(s);
 	srunner_add_suite(sr, libevdev_events());
diff --git a/test/test-uinput.c b/test/test-uinput.c
index 2e9d3cd..99738e3 100644
--- a/test/test-uinput.c
+++ b/test/test-uinput.c
@@ -101,9 +101,11 @@ START_TEST(test_uinput_create_device_invalid)
 	libevdev_enable_event_code(dev, EV_REL, REL_X, NULL);
 	libevdev_enable_event_code(dev, EV_REL, REL_Y, NULL);
 
+	libevdev_set_log_function(test_logfunc_ignore_error, NULL);
 	rc = libevdev_uinput_create_from_device(dev, -1, &uidev);
 	ck_assert_int_eq(rc, -EBADF);
 	ck_assert(uidev == NULL);
+	libevdev_set_log_function(test_logfunc_abort_on_error, NULL);
 
 	libevdev_free(dev);
 }
-- 
1.8.4.2



More information about the Input-tools mailing list