>From 7bf14a1c8ac3a0d8cf2bd73170bc45427920e316 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Tue, 18 Feb 2014 13:27:20 +0100 Subject: [PATCH 4/5] sbus_tests: Add some testing of dispatch and handler code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This starts a DBus server with some handlers, and runs some method calls against it. Note that we don't use the codegen in the sbus_tests, as we sorta want to test this non-codegen related functionality on its own before we run the sbus_codegen_tests. Reviewed-by: Jakub Hrozek Reviewed-by: Pavel Březina Reviewed-by: Lukáš Slebodník (cherry picked from commit f5e47e1d65f80ffdb1893feab18583a74d661214) Conflicts: Makefile.am --- Makefile.am | 11 +++ src/tests/common.h | 15 ++++ src/tests/common_dbus.c | 198 +++++++++++++++++++++++++++++++++++++++++ src/tests/sbus_tests.c | 231 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 455 insertions(+) create mode 100644 src/tests/common_dbus.c create mode 100644 src/tests/sbus_tests.c diff --git a/Makefile.am b/Makefile.am index 01d199ace430ea42e7af1003eb0eb8498243b30d..79ab0fda5abb2612ce69388351b1225c154404ad 100644 --- a/Makefile.am +++ b/Makefile.am @@ -134,6 +134,7 @@ if HAVE_CHECK ipa_hbac-tests \ sss_idmap-tests \ responder_socket_access-tests \ + sbus_tests \ sbus_codegen_tests if BUILD_SSH @@ -1308,6 +1309,16 @@ krb5_child_test_LDADD = \ $(SSSD_INTERNAL_LTLIBS) \ libsss_test_common.la +sbus_tests_SOURCES = \ + src/tests/common_dbus.c \ + src/tests/sbus_tests.c +sbus_tests_CFLAGS = \ + $(CHECK_CFLAGS) +sbus_tests_LDADD = \ + $(SSSD_INTERNAL_LTLIBS) \ + $(SSSD_LIBS) \ + $(CHECK_LIBS) + sbus_codegen_tests_SOURCES = \ src/tests/sbus_codegen_tests.c \ src/tests/sbus_codegen_tests_generated.c \ diff --git a/src/tests/common.h b/src/tests/common.h index 931e603c1c59c7a73434b344cb1633faf37bf83c..ba3b3a7490ec5bd0e75c5f2f66c39542ca0c37ee 100644 --- a/src/tests/common.h +++ b/src/tests/common.h @@ -100,4 +100,19 @@ int test_ev_loop(struct sss_test_ctx *tctx); bool ldb_modules_path_is_set(void); +DBusConnection * +test_dbus_setup_mock(TALLOC_CTX *mem_ctx, + struct tevent_context *loop, + sbus_server_conn_init_fn init_fn, + void *init_pvt_data); + +DBusMessage * +test_dbus_call_sync(DBusConnection *conn, + const char *object_path, + const char *interface, + const char *method, + DBusError *error, + int first_arg_type, + ...); + #endif /* !__TESTS_COMMON_H__ */ diff --git a/src/tests/common_dbus.c b/src/tests/common_dbus.c new file mode 100644 index 0000000000000000000000000000000000000000..3117c080dc3106517bee933a458583f35b04fa63 --- /dev/null +++ b/src/tests/common_dbus.c @@ -0,0 +1,198 @@ +/* + SSSD + + Common utilities for dbus based tests. + + Authors: + Stef Walter + + Copyright (C) Red Hat, Inc 2014 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "config.h" + +#include +#include "tests/common.h" + +struct mock_server { + char *temp_dir; + char *dbus_address; + pid_t pid; + DBusConnection *client; + + /* Used for synchronization */ + int sync_fds[2]; + + /* Only used during init */ + sbus_server_conn_init_fn init_fn; + void *init_pvt_data; +}; + +/* + * If you think we're going to do full error propagation during tests ... + * you're going to have a bad time (reading this code) + */ +#define verify_eq(x, y) \ + do { if ((x) != (y)) { fprintf(stderr, "failed: %s == %s\n", #x, #y); abort(); } } while (0) +#define verify_neq(x, y) \ + do { if ((x) == (y)) { fprintf(stderr, "failed: %s != %s\n", #x, #y); abort(); } } while (0) + +static int +mock_server_cleanup(struct mock_server *mock) +{ + int child_status; + const char *file; + struct stat sb; + + dbus_connection_close(mock->client); + dbus_connection_unref(mock->client); + + /* Tell the server thread to quit */ + verify_eq (write(mock->sync_fds[0], "X", 1), 1); + + /* Wait for the server child, it always returns mock */ + verify_eq (waitpid(mock->pid, &child_status, 0), mock->pid); + verify_eq (child_status, 0); + + file = strchr(mock->dbus_address, '/'); + if (stat(file, &sb) == 0) { + verify_eq (unlink(file), 0); + } + verify_eq (rmdir(mock->temp_dir), 0); + + return EOK; +} + +static int +on_accept_connection(struct sbus_connection *conn, + void *data) +{ + struct mock_server *mock = data; + + verify_eq (mock->init_fn(conn, mock->init_pvt_data), EOK); + + /* Synchronization point: test_dbus_setup_mock() should return */ + verify_eq (write(mock->sync_fds[1], "X", 1), 1); + + return EOK; +} + +static void +on_sync_fd_written(struct tevent_context *loop, + struct tevent_fd *fde, + uint16_t flags, + void *data) +{ + bool *stop_server = data; + *stop_server = true; +} + +static void +mock_server_child(void *data) +{ + struct mock_server *mock = data; + struct tevent_context *loop; + struct sbus_connection *server; + bool stop_server = false; + TALLOC_CTX *ctx; + + ctx = talloc_new(NULL); + loop = tevent_context_init(ctx); + + verify_eq (sbus_new_server(ctx, loop, mock->dbus_address, false, + &server, on_accept_connection, mock), EOK); + + tevent_add_fd(loop, ctx, mock->sync_fds[1], TEVENT_FD_READ, + on_sync_fd_written, &stop_server); + + /* Synchronization point: test_dbus_setup_mock() should connect */ + verify_eq (write(mock->sync_fds[1], "X", 1), 1); + + /* Do the loop */ + while(!stop_server) { + verify_eq (tevent_loop_once(loop), 0); + } + + /* TODO: sbus doesn't support cleanup of a server */ + + talloc_free(ctx); +} + +struct DBusConnection * +test_dbus_setup_mock(TALLOC_CTX *mem_ctx, + struct tevent_context *loop, + sbus_server_conn_init_fn init_fn, + void *init_pvt_data) +{ + struct mock_server *mock; + char dummy; + + mock = talloc_zero(mem_ctx, struct mock_server); + talloc_set_destructor(mock, mock_server_cleanup); + mock->init_fn = init_fn; + mock->init_pvt_data = init_pvt_data; + + mock->temp_dir = mkdtemp(talloc_strdup(mock, "/tmp/sssd-dbus-tests.XXXXXX")); + verify_neq (mock->temp_dir, NULL); + mock->dbus_address = talloc_asprintf(mock, "unix:path=%s/sbus", mock->temp_dir); + verify_neq (mock->dbus_address, NULL); + + /* We use an fd pair as a synchronization device, integrates with tevent well */ + verify_eq (socketpair(PF_LOCAL, SOCK_STREAM, 0, mock->sync_fds), 0); + + /* Run the dbus server in a child process */ + mock->pid = fork(); + if (mock->pid == 0) { + mock_server_child(mock); + _exit(0); + } + + verify_neq (mock->pid, -1); + + /* Synchronization point: wait for sync point in mock_server_child */ + verify_eq (read(mock->sync_fds[0], &dummy, 1), 1); + + /* Open a shared D-BUS connection to the address */ + mock->client = dbus_connection_open_private(mock->dbus_address, NULL); + verify_neq (mock->client, NULL); + + /* Synchronization point: wait for sync point in on_accept_connection */ + verify_eq (read(mock->sync_fds[0], &dummy, 1), 1); + + return mock->client; +} + +DBusMessage * +test_dbus_call_sync(DBusConnection *conn, const char *object_path, + const char *interface, const char *method, + DBusError *error, int first_arg_type, ...) +{ + DBusMessage *message; + DBusMessage *reply; + va_list va; + + message = dbus_message_new_method_call(NULL, object_path, interface, method); + verify_neq(message, NULL); + + va_start(va, first_arg_type); + verify_eq(dbus_message_append_args_valist(message, first_arg_type, va), TRUE); + va_end(va); + + reply = dbus_connection_send_with_reply_and_block(conn, message, -1, error); + dbus_message_unref(message); + + return reply; +} diff --git a/src/tests/sbus_tests.c b/src/tests/sbus_tests.c new file mode 100644 index 0000000000000000000000000000000000000000..7290fe7dbf9d6b0811afbc3b27d3500cff63761a --- /dev/null +++ b/src/tests/sbus_tests.c @@ -0,0 +1,231 @@ +/* + SSSD + + sbus_codegen tests. + + Authors: + Stef Walter + + Copyright (C) Red Hat, Inc 2014 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include +#include + +#include "common.h" + +#include "sbus/sssd_dbus.h" +#include "sbus/sssd_dbus_meta.h" +#include "util/util_errors.h" + +/* + * Although one would normally rely on the codegen to generate these + * structures, we want to test this functionality *before* we test + * the codegen in sbus_codegen_tests ... so these are hand rolled. + */ + +#define PILOT_IFACE "test.Pilot" +#define PILOT_BLINK "Blink" + +/* our vtable */ +struct pilot_vtable { + struct sbus_vtable vtable; + sbus_msg_handler_fn Blink; +}; + +const struct sbus_method_meta pilot_methods[] = { + { + PILOT_BLINK, /* method name */ + NULL, /* in args: manually parsed */ + NULL, /* out args: manually parsed */ + offsetof(struct pilot_vtable, Blink), + }, + { NULL, } +}; + +const struct sbus_interface_meta pilot_meta = { + PILOT_IFACE, /* name */ + pilot_methods, + NULL, /* no signals */ + NULL, /* no properties */ +}; + +static int blink_handler(struct sbus_request *req, void *data) +{ + DBusError error = DBUS_ERROR_INIT; + const char *path; + dbus_int32_t duration = 0; + dbus_bool_t crashed; + + ck_assert(req->intf->vtable->meta == &pilot_meta); + ck_assert(data != NULL); + ck_assert(data == req->intf->instance_data); + + path = dbus_message_get_path(req->message); + ck_assert_str_eq(req->intf->path, path); + + if (strcmp(path, "/test/fry") == 0) { + ck_assert_str_eq(data, "Don't crash"); + } else if (strcmp(path, "/test/leela") == 0) { + ck_assert_str_eq(data, "Crash into the billboard"); + } else { + ck_abort(); + } + + if (!dbus_message_get_args (req->message, &error, + DBUS_TYPE_INT32, &duration, + DBUS_TYPE_INVALID)) { + sbus_request_fail_and_finish(req, &error); + dbus_error_free(&error); + return EOK; + } + + /* Pilot crashes when eyes closed too long */ + crashed = (duration > 5); + + return sbus_request_return_and_finish(req, + DBUS_TYPE_BOOLEAN, &crashed, + DBUS_TYPE_INVALID); +} + +struct pilot_vtable pilot_impl = { + { &pilot_meta, 0 }, + .Blink = blink_handler, +}; + +static int pilot_test_server_init(struct sbus_connection *server, void *unused) +{ + int ret; + + ret = sbus_conn_add_interface(server, + sbus_new_interface(server, "/test/leela", + &pilot_impl.vtable, + "Crash into the billboard")); + ck_assert_int_eq(ret, EOK); + + + ret = sbus_conn_add_interface(server, + sbus_new_interface(server, "/test/fry", + &pilot_impl.vtable, + "Don't crash")); + ck_assert_int_eq(ret, EOK); + + return EOK; +} + +START_TEST(test_raw_handler) +{ + TALLOC_CTX *ctx; + DBusConnection *client; + DBusError error = DBUS_ERROR_INIT; + DBusMessage *reply; + dbus_bool_t crashed; + dbus_int32_t duration; + + ctx = talloc_new(NULL); + client = test_dbus_setup_mock(ctx, NULL, pilot_test_server_init, NULL); + + /* Leela crashes with a duration higher than 5 */ + duration = 10; + reply = test_dbus_call_sync(client, + "/test/leela", + PILOT_IFACE, + PILOT_BLINK, + &error, + DBUS_TYPE_INT32, &duration, + DBUS_TYPE_INVALID); + ck_assert(reply != NULL); + ck_assert(!dbus_error_is_set(&error)); + ck_assert(dbus_message_get_args(reply, NULL, + DBUS_TYPE_BOOLEAN, &crashed, + DBUS_TYPE_INVALID)); + dbus_message_unref (reply); + ck_assert(crashed == true); + + /* Fry daesn't crash with a duration lower than 5 */ + duration = 1; + reply = test_dbus_call_sync(client, + "/test/fry", + PILOT_IFACE, + PILOT_BLINK, + &error, + DBUS_TYPE_INT32, &duration, + DBUS_TYPE_INVALID); + ck_assert(reply != NULL); + ck_assert(!dbus_error_is_set(&error)); + ck_assert(dbus_message_get_args(reply, NULL, + DBUS_TYPE_BOOLEAN, &crashed, + DBUS_TYPE_INVALID)); + dbus_message_unref (reply); + ck_assert(crashed == FALSE); + + talloc_free(ctx); +} +END_TEST + +TCase *create_sbus_tests(void) +{ + TCase *tc = tcase_create("tests"); + + tcase_add_test(tc, test_raw_handler); + + return tc; +} + +Suite *create_suite(void) +{ + Suite *s = suite_create("sbus"); + suite_add_tcase(s, create_sbus_tests()); + return s; +} + +int main(int argc, const char *argv[]) +{ + int opt; + poptContext pc; + int failure_count; + Suite *suite; + SRunner *sr; + + struct poptOption long_options[] = { + POPT_AUTOHELP + POPT_TABLEEND + }; + + pc = poptGetContext(argv[0], argc, argv, long_options, 0); + while ((opt = poptGetNextOpt(pc)) != -1) { + switch (opt) { + default: + fprintf(stderr, "\nInvalid option %s: %s\n\n", + poptBadOption(pc, 0), poptStrerror(opt)); + poptPrintUsage(pc, stderr, 0); + return 1; + } + } + poptFreeContext(pc); + + suite = create_suite(); + sr = srunner_create(suite); + srunner_set_fork_status(sr, CK_NOFORK); + /* If CK_VERBOSITY is set, use that, otherwise it defaults to CK_NORMAL */ + srunner_run_all(sr, CK_ENV); + failure_count = srunner_ntests_failed(sr); + srunner_free(sr); + return (failure_count == 0 ? EXIT_SUCCESS : EXIT_FAILURE); +} -- 1.9.0