From 04c0ff49cc0da33792dfe8985d0df2875c540468 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Tue, 18 Aug 2015 16:42:05 +1000 Subject: [PATCH] test: add basic context init tests Signed-off-by: Peter Hutterer --- .gitignore | 1 + test/Makefile.am | 9 ++- test/test-context.c | 148 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 test/test-context.c diff --git a/.gitignore b/.gitignore index 5352af6e..3d73e48b 100644 --- a/.gitignore +++ b/.gitignore @@ -42,4 +42,5 @@ test-build-linker test-build-pedantic-c99 test-build-std-gnuc90 test-suite.log +test-context ratbag-command diff --git a/test/Makefile.am b/test/Makefile.am index af095aae..85841b72 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -9,7 +9,8 @@ AM_CXXFLAGS = $(GCC_CXXFLAGS) TEST_LIBS = $(CHECK_LIBS) $(LIBEVDEV_LIBS) $(top_builddir)/src/libratbag.la -run_tests = +run_tests = \ + test-context build_tests = \ test-build-cxx \ @@ -17,10 +18,14 @@ build_tests = \ test-build-pedantic-c99 \ test-build-std-gnuc90 -noinst_PROGRAMS = $(build_tests) +noinst_PROGRAMS = $(build_tests) $(run_tests) noinst_SCRIPTS = symbols-leak-test TESTS = $(run_tests) symbols-leak-test +test_context_SOURCES = test-context.c +test_context_LDADD = $(TEST_LIBS) +test_context_LDFLAGS = -no-install + # build-test only test_build_pedantic_c99_SOURCES = build-pedantic.c test_build_pedantic_c99_CFLAGS = -std=c99 -pedantic -Werror diff --git a/test/test-context.c b/test/test-context.c new file mode 100644 index 00000000..53eb986b --- /dev/null +++ b/test/test-context.c @@ -0,0 +1,148 @@ +/* + * Copyright © 2015 Red Hat, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#include + +#include +#include +#include +#include +#include +#include + +#include "libratbag.h" + +static int +open_restricted(const char *path, int flags, void *user_data) +{ + int fd = open(path, flags); + + if (fd < 0) + fprintf(stderr, "Failed to open %s (%s)\n", + path, strerror(errno)); + + return fd < 0 ? -errno : fd; +} + +static void +close_restricted(int fd, void *user_data) +{ + close(fd); +} + +struct libratbag_interface simple_iface = { + .open_restricted = open_restricted, + .close_restricted = close_restricted, +}; + +START_TEST(context_init_NULL) +{ + struct libratbag *lr; + lr = libratbag_create_context(NULL, NULL); + ck_assert(lr == NULL); +} +END_TEST + +START_TEST(context_init_bad_iface) +{ + struct libratbag *lr; + struct libratbag_interface iface = { + .open_restricted = NULL, + .close_restricted = NULL, + }; + + lr = libratbag_create_context(&iface, NULL); + ck_assert(lr == NULL); + + iface.open_restricted = open_restricted; + lr = libratbag_create_context(&iface, NULL); + ck_assert(lr == NULL); + + iface.open_restricted = NULL; + iface.close_restricted = close_restricted; + lr = libratbag_create_context(&iface, NULL); + ck_assert(lr == NULL); +} +END_TEST + +START_TEST(context_init) +{ + struct libratbag *lr; + + lr = libratbag_create_context(&simple_iface, NULL); + ck_assert(lr != NULL); + libratbag_unref(lr); +} +END_TEST + +START_TEST(context_ref) +{ + struct libratbag *lr; + struct libratbag *lr2; + + lr = libratbag_create_context(&simple_iface, NULL); + ck_assert(lr != NULL); + + lr2 = libratbag_ref(lr); + ck_assert_ptr_eq(lr, lr2); + + lr2 = libratbag_unref(lr2); + ck_assert_ptr_eq(lr2, lr); + + lr2 = libratbag_unref(lr2); + ck_assert_ptr_eq(lr2, NULL); +} +END_TEST + +static Suite * +test_context_suite(void) +{ + 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); + tcase_add_test(tc, context_init); + tcase_add_test(tc, context_ref); + suite_add_tcase(s, tc); + + return s; +} + +int main(void) +{ + int nfailed; + Suite *s; + SRunner *sr; + + s = test_context_suite(); + sr = srunner_create(s); + + srunner_run_all(sr, CK_ENV); + nfailed = srunner_ntests_failed(sr); + srunner_free(sr); + + return (nfailed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +}