From 198523fe8f67ee16ff71adca3b9272270cb46b31 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Fri, 11 Mar 2016 12:28:10 +1000 Subject: [PATCH] Simplify validity checks with assert Let the caller check that what they're passing in is correct, we don't have to respond with sensible error codes here. Signed-off-by: Peter Hutterer --- src/libratbag.c | 18 ++++----------- test/test-context.c | 56 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/libratbag.c b/src/libratbag.c index c8929f70..2f475b92 100644 --- a/src/libratbag.c +++ b/src/libratbag.c @@ -362,15 +362,8 @@ ratbag_device_new_from_udev_device(struct ratbag *ratbag, char *name = NULL; struct input_id id; - if (!ratbag) { - fprintf(stderr, "ratbag is NULL\n"); - return NULL; - } - - if (!udev_device) { - log_bug_client(ratbag, "udev device is NULL.\n"); - return NULL; - } + assert(ratbag != NULL); + assert(udev_device != NULL); if (get_product_id(udev_device, &id) != 0) goto out_err; @@ -464,10 +457,9 @@ ratbag_create_context(const struct ratbag_interface *interface, { struct ratbag *ratbag; - if (interface == NULL || - interface->open_restricted == NULL || - interface->close_restricted == NULL) - return NULL; + assert(interface != NULL); + assert(interface->open_restricted != NULL); + assert(interface->close_restricted != NULL); ratbag = zalloc(sizeof(*ratbag)); ratbag->refcount = 1; diff --git a/test/test-context.c b/test/test-context.c index 6f98d3c7..d3392e3d 100644 --- a/test/test-context.c +++ b/test/test-context.c @@ -26,12 +26,16 @@ #include #include #include +#include +#include #include #include #include #include "libratbag.h" +#define _unused_ __attribute__ ((unused)) + static int open_restricted(const char *path, int flags, void *user_data) { @@ -65,23 +69,40 @@ END_TEST START_TEST(context_init_bad_iface) { - struct ratbag *lr; + struct ratbag *lr _unused_; struct ratbag_interface iface = { .open_restricted = NULL, .close_restricted = NULL, }; lr = ratbag_create_context(&iface, NULL); - ck_assert(lr == NULL); + /* abort */ +} +END_TEST - iface.open_restricted = open_restricted; - lr = ratbag_create_context(&iface, NULL); - ck_assert(lr == NULL); +START_TEST(context_init_bad_iface_open) +{ + struct ratbag *lr _unused_; + struct ratbag_interface iface = { + .open_restricted = open_restricted, + .close_restricted = NULL, + }; - iface.open_restricted = NULL; - iface.close_restricted = close_restricted; lr = ratbag_create_context(&iface, NULL); - ck_assert(lr == NULL); + /* abort */ +} +END_TEST + +START_TEST(context_init_bad_iface_close) +{ + struct ratbag *lr _unused_; + struct ratbag_interface iface = { + .open_restricted = NULL, + .close_restricted = close_restricted, + }; + + lr = ratbag_create_context(&iface, NULL); + /* abort */ } END_TEST @@ -115,15 +136,19 @@ START_TEST(context_ref) END_TEST static Suite * -test_context_suite(void) +test_context_suite(bool using_valgrind) { TCase *tc; Suite *s; s = suite_create("context"); tc = tcase_create("init"); - tcase_add_test(tc, context_init_NULL); - tcase_add_test(tc, context_init_bad_iface); + if (!using_valgrind) { + tcase_add_test_raise_signal(tc, context_init_NULL, SIGABRT); + tcase_add_test_raise_signal(tc, context_init_bad_iface, SIGABRT); + tcase_add_test_raise_signal(tc, context_init_bad_iface_open, SIGABRT); + tcase_add_test_raise_signal(tc, context_init_bad_iface_close, SIGABRT); + } tcase_add_test(tc, context_init); tcase_add_test(tc, context_ref); suite_add_tcase(s, tc); @@ -136,9 +161,14 @@ int main(void) int nfailed; Suite *s; SRunner *sr; + bool using_valgrind; - s = test_context_suite(); - sr = srunner_create(s); + /* when running under valgrind we're using nofork mode, so a + * signal raised by a test will fail in valgrind */ + using_valgrind = !!getenv("USING_VALGRIND"); + + s = test_context_suite(using_valgrind); + sr = srunner_create(s); srunner_run_all(sr, CK_ENV); nfailed = srunner_ntests_failed(sr);