summaryrefslogtreecommitdiffstats
path: root/src/tests/sbus_tests.c
diff options
context:
space:
mode:
authorStef Walter <stefw@redhat.com>2014-02-21 16:59:48 +0100
committerJakub Hrozek <jhrozek@redhat.com>2014-03-14 13:42:31 +0100
commit06b7bc8ca2e005ed510210d3b8dee16afbabbcc9 (patch)
tree8131673e499cced272643b379d1a754c064f76b2 /src/tests/sbus_tests.c
parentf5e47e1d65f80ffdb1893feab18583a74d661214 (diff)
downloadsssd-06b7bc8ca2e005ed510210d3b8dee16afbabbcc9.tar.gz
sssd-06b7bc8ca2e005ed510210d3b8dee16afbabbcc9.tar.xz
sssd-06b7bc8ca2e005ed510210d3b8dee16afbabbcc9.zip
sbus: Add the sbus_request_parse_or_finish() method
Some DBus types returned from dbus_message_get_args() require memory to be released when done. We automatically attach these to the talloc struct sbus_request memory context in this function. This accepts varargs similar to dbus_message_get_args(), which are rather awkward. However instead of reworking them completely, future generated marshalling code will replace most uses of these varargs. If parsing the dbus message fails, then it responds to the DBus caller with an appropriate error such as o.f.D.Error.InvalidArgs. In these cases (ie: when it returns FALSE) the sbus_request is finished. Migrated some, but not all, uses of dbus_message_get_args() to the new function. Some instances have uncommon semantics such as terminating the connection upon failure to parse a message. Reviewed-by: Jakub Hrozek <jhrozek@redhat.com> Reviewed-by: Pavel Březina <pbrezina@redhat.com> Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
Diffstat (limited to 'src/tests/sbus_tests.c')
-rw-r--r--src/tests/sbus_tests.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/tests/sbus_tests.c b/src/tests/sbus_tests.c
index 7290fe7db..b8cd4abb0 100644
--- a/src/tests/sbus_tests.c
+++ b/src/tests/sbus_tests.c
@@ -42,11 +42,13 @@
#define PILOT_IFACE "test.Pilot"
#define PILOT_BLINK "Blink"
+#define PILOT_EAT "Eat"
/* our vtable */
struct pilot_vtable {
struct sbus_vtable vtable;
sbus_msg_handler_fn Blink;
+ sbus_msg_handler_fn Eat;
};
const struct sbus_method_meta pilot_methods[] = {
@@ -56,6 +58,12 @@ const struct sbus_method_meta pilot_methods[] = {
NULL, /* out args: manually parsed */
offsetof(struct pilot_vtable, Blink),
},
+ {
+ PILOT_EAT, /* method name */
+ NULL, /* in args: manually parsed */
+ NULL, /* out args: manually parsed */
+ offsetof(struct pilot_vtable, Eat),
+ },
{ NULL, }
};
@@ -104,9 +112,35 @@ static int blink_handler(struct sbus_request *req, void *data)
DBUS_TYPE_INVALID);
}
+static int eat_handler(struct sbus_request *req, void *data)
+{
+ dbus_int32_t integer;
+ dbus_bool_t boolean;
+ const char **array;
+ int count;
+
+ if (!sbus_request_parse_or_finish (req,
+ DBUS_TYPE_INT32, &integer,
+ DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &array, &count,
+ DBUS_TYPE_BOOLEAN, &boolean,
+ DBUS_TYPE_INVALID)) {
+ return EOK; /* handled */
+ }
+
+ ck_assert_int_eq(integer, 5);
+ ck_assert(boolean == TRUE);
+ ck_assert_int_eq(count, 3);
+ ck_assert_str_eq(array[0], "one");
+ ck_assert_str_eq(array[1], "two");
+ ck_assert_str_eq(array[2], "three");
+
+ return sbus_request_return_and_finish(req, DBUS_TYPE_INVALID);
+}
+
struct pilot_vtable pilot_impl = {
{ &pilot_meta, 0 },
.Blink = blink_handler,
+ .Eat = eat_handler,
};
static int pilot_test_server_init(struct sbus_connection *server, void *unused)
@@ -179,11 +213,76 @@ START_TEST(test_raw_handler)
}
END_TEST
+START_TEST(test_request_parse_ok)
+{
+ const char *args[] = { "one", "two", "three" };
+ const char **array;
+ TALLOC_CTX *ctx;
+ DBusConnection *client;
+ DBusError error = DBUS_ERROR_INIT;
+ DBusMessage *reply;
+ dbus_bool_t boolean;
+ dbus_int32_t integer;
+ int count;
+
+ ctx = talloc_new(NULL);
+ client = test_dbus_setup_mock(ctx, NULL, pilot_test_server_init, NULL);
+
+ boolean = TRUE;
+ integer = 5;
+ count = 3;
+ array = args;
+ reply = test_dbus_call_sync(client,
+ "/test/leela",
+ PILOT_IFACE,
+ PILOT_EAT,
+ &error,
+ DBUS_TYPE_INT32, &integer,
+ DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &array, count,
+ DBUS_TYPE_BOOLEAN, &boolean,
+ DBUS_TYPE_INVALID);
+ ck_assert(reply != NULL);
+ ck_assert(!dbus_error_is_set(&error));
+ ck_assert(dbus_message_get_args(reply, NULL,
+ DBUS_TYPE_INVALID));
+ dbus_message_unref (reply);
+
+ talloc_free(ctx);
+}
+END_TEST
+
+START_TEST(test_request_parse_bad_args)
+{
+ TALLOC_CTX *ctx;
+ DBusConnection *client;
+ DBusError error = DBUS_ERROR_INIT;
+ DBusMessage *reply;
+
+ ctx = talloc_new(NULL);
+ client = test_dbus_setup_mock(ctx, NULL, pilot_test_server_init, NULL);
+
+ reply = test_dbus_call_sync(client,
+ "/test/leela",
+ PILOT_IFACE,
+ PILOT_EAT,
+ &error,
+ DBUS_TYPE_INVALID); /* bad agruments */
+ ck_assert(reply == NULL);
+ ck_assert(dbus_error_is_set(&error));
+ ck_assert(dbus_error_has_name(&error, DBUS_ERROR_INVALID_ARGS));
+ dbus_error_free(&error);
+
+ talloc_free(ctx);
+}
+END_TEST
+
TCase *create_sbus_tests(void)
{
TCase *tc = tcase_create("tests");
tcase_add_test(tc, test_raw_handler);
+ tcase_add_test(tc, test_request_parse_ok);
+ tcase_add_test(tc, test_request_parse_bad_args);
return tc;
}