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 <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer
2016-03-11 12:28:10 +10:00
parent 7f7507a0dd
commit 198523fe8f
2 changed files with 48 additions and 26 deletions

View File

@@ -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;

View File

@@ -26,12 +26,16 @@
#include <check.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#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);