Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[unit] Added plugins_dbus_handlers test
  • Loading branch information
monich committed Jul 29, 2019
1 parent 8add4b4 commit 78d7b6d
Show file tree
Hide file tree
Showing 8 changed files with 1,077 additions and 6 deletions.
1 change: 1 addition & 0 deletions unit/Makefile
Expand Up @@ -15,6 +15,7 @@ all:
@$(MAKE) -C core_target $*
@$(MAKE) -C core_tlv $*
@$(MAKE) -C core_util $*
@$(MAKE) -C plugins_dbus_handlers $*
@$(MAKE) -C plugins_dbus_handlers_config $*
@$(MAKE) -C plugins_dbus_handlers_type_generic $*
@$(MAKE) -C plugins_dbus_handlers_type_mediatype $*
Expand Down
36 changes: 30 additions & 6 deletions unit/common/Makefile
Expand Up @@ -14,12 +14,6 @@ endif
SRC ?= $(EXE).c
COMMON_SRC ?= test_main.c

#
# Required packages
#

PKGS += libglibutil glib-2.0 gobject-2.0

#
# Default target
#
Expand All @@ -34,10 +28,23 @@ SRC_DIR = .
CORE_DIR = ../../core
COMMON_DIR = ../common
BUILD_DIR = build
GEN_DIR = $(BUILD_DIR)
DEBUG_BUILD_DIR = $(BUILD_DIR)/debug
RELEASE_BUILD_DIR = $(BUILD_DIR)/release
COVERAGE_BUILD_DIR = $(BUILD_DIR)/coverage

#
# Required packages
#

PKGS += libglibutil glib-2.0 gobject-2.0

ifneq ($(GEN_SRC),)
# Additional requirements for generated stubs
PKGS += gio-unix-2.0
INCLUDES += -I. -I$(GEN_DIR)
endif

#
# Tools and flags
#
Expand Down Expand Up @@ -81,12 +88,15 @@ COVERAGE_LIBS += $(COVERAGE_CORE_LIB) $(LIBS)

DEBUG_OBJS = \
$(COMMON_SRC:%.c=$(DEBUG_BUILD_DIR)/common_%.o) \
$(GEN_SRC:%.c=$(DEBUG_BUILD_DIR)/gen_%.o) \
$(SRC:%.c=$(DEBUG_BUILD_DIR)/%.o)
RELEASE_OBJS = \
$(COMMON_SRC:%.c=$(RELEASE_BUILD_DIR)/common_%.o) \
$(GEN_SRC:%.c=$(RELEASE_BUILD_DIR)/gen_%.o) \
$(SRC:%.c=$(RELEASE_BUILD_DIR)/%.o)
COVERAGE_OBJS = \
$(COMMON_SRC:%.c=$(COVERAGE_BUILD_DIR)/common_%.o) \
$(GEN_SRC:%.c=$(COVERAGE_BUILD_DIR)/gen_%.o) \
$(SRC:%.c=$(COVERAGE_BUILD_DIR)/%.o)

#
Expand All @@ -108,6 +118,8 @@ $(DEBUG_OBJS): | $(DEBUG_BUILD_DIR)
$(RELEASE_OBJS): | $(RELEASE_BUILD_DIR)
$(COVERAGE_OBJS): | $(COVERAGE_BUILD_DIR)

$(GEN_SRC:%=$(GEN_DIR)/%): | $(GEN_DIR)

DEBUG_DEPS += $(DEBUG_CORE_LIB)
RELEASE_DEPS += $(RELEASE_CORE_LIB)
COVERAGE_DEPS += $(COVERAGE_CORE_LIB)
Expand Down Expand Up @@ -153,6 +165,9 @@ $(RELEASE_BUILD_DIR):
$(COVERAGE_BUILD_DIR):
mkdir -p $@

$(GEN_DIR)/%.c: %.xml
gdbus-codegen --generate-c-code $(@:%.c=%) $<

$(DEBUG_BUILD_DIR)/%.o : $(SRC_DIR)/%.c
$(CC) -c $(DEBUG_CFLAGS) -MT"$@" -MF"$(@:%.o=%.d)" $< -o $@

Expand All @@ -171,6 +186,15 @@ $(RELEASE_BUILD_DIR)/common_%.o : $(COMMON_DIR)/%.c
$(COVERAGE_BUILD_DIR)/common_%.o : $(COMMON_DIR)/%.c
$(CC) -c $(COVERAGE_CFLAGS) -MT"$@" -MF"$(@:%.o=%.d)" $< -o $@

$(DEBUG_BUILD_DIR)/gen_%.o : $(GEN_DIR)/%.c
$(CC) -c $(DEBUG_CFLAGS) -MT"$@" -MF"$(@:%.o=%.d)" $< -o $@

$(RELEASE_BUILD_DIR)/gen_%.o : $(GEN_DIR)/%.c
$(CC) -c $(RELEASE_CFLAGS) -MT"$@" -MF"$(@:%.o=%.d)" $< -o $@

$(COVERAGE_BUILD_DIR)/gen_%.o : $(GEN_DIR)/%.c
$(CC) -c $(COVERAGE_CFLAGS) -MT"$@" -MF"$(@:%.o=%.d)" $< -o $@

$(DEBUG_EXE): $(DEBUG_DEPS) $(DEBUG_OBJS)
$(LD) $(DEBUG_LDFLAGS) $(EXTRA_EXE_LDFLAGS) $(DEBUG_OBJS) $(DEBUG_LIBS) -o $@

Expand Down
164 changes: 164 additions & 0 deletions unit/common/test_dbus.c
@@ -0,0 +1,164 @@
/*
* Copyright (C) 2019 Jolla Ltd.
* Copyright (C) 2019 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "test_dbus.h"

#include <glib/gstdio.h>

#include <gutil_log.h>

struct test_dbus {
char* tmpdir;
GDBusConnection* client_connection;
GDBusConnection* server_connection;
GDBusServer* server;
GDBusAuthObserver* observer;
TestDBusStartFunc start;
void* user_data;
};

static
void
test_dbus_client_connection(
GObject* source,
GAsyncResult* res,
gpointer user_data)
{
TestDBus* self = user_data;

GDEBUG("Got client connection");
g_assert(!self->client_connection);
self->client_connection = g_dbus_connection_new_finish(res, NULL);
g_assert(self->client_connection);

if (self->client_connection && self->server_connection && self->start) {
self->start(self->client_connection, self->server_connection,
self->user_data);
}
}

static
gboolean
test_dbus_server_connection(
GDBusServer* server,
GDBusConnection* connection,
gpointer user_data)
{
TestDBus* self = user_data;

GDEBUG("Got server connection");
g_assert(!self->server_connection);
g_object_ref(self->server_connection = connection);

if (self->client_connection && self->server_connection && self->start) {
self->start(self->client_connection, self->server_connection,
self->user_data);
}
return TRUE;
}

static
gboolean
test_dbus_authorize(
GDBusAuthObserver* observer,
GIOStream* stream,
GCredentials* credentials,
gpointer user_data)
{
GDEBUG("Authorizing server connection");
return TRUE;
}

TestDBus*
test_dbus_new(
TestDBusStartFunc start,
void* user_data)
{
TestDBus* self = g_new0(TestDBus, 1);
char* guid = g_dbus_generate_guid();
char* tmpaddr;
const char* client_addr;

self->start = start;
self->user_data = user_data;
self->tmpdir = g_dir_make_tmp("test_dbus_XXXXXX", NULL);
tmpaddr = g_strconcat("unix:tmpdir=", self->tmpdir, NULL);

self->observer = g_dbus_auth_observer_new();
g_assert(g_signal_connect(self->observer, "authorize-authenticated-peer",
G_CALLBACK(test_dbus_authorize), self));

GDEBUG("DBus server address %s", tmpaddr);
self->server = g_dbus_server_new_sync(tmpaddr, G_DBUS_SERVER_FLAGS_NONE,
guid, self->observer, NULL, NULL);
g_assert(self->server);
g_assert(g_signal_connect(self->server, "new-connection",
G_CALLBACK(test_dbus_server_connection), self));
g_dbus_server_start(self->server);

client_addr = g_dbus_server_get_client_address(self->server);
GDEBUG("D-Bus client address %s", client_addr);
g_dbus_connection_new_for_address(client_addr,
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT, NULL, NULL,
test_dbus_client_connection, self);

g_free(tmpaddr);
g_free(guid);
return self;
}

void
test_dbus_free(
TestDBus* self)
{
if (self) {
if (self->client_connection) {
g_object_unref(self->client_connection);
}
if (self->server_connection) {
g_object_unref(self->server_connection);
}
g_object_unref(self->observer);
g_dbus_server_stop(self->server);
g_rmdir(self->tmpdir);
g_free(self->tmpdir);
g_free(self);
}
}

/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/
64 changes: 64 additions & 0 deletions unit/common/test_dbus.h
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2019 Jolla Ltd.
* Copyright (C) 2019 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef TEST_DBUS_H
#define TEST_DBUS_H

#include <gio/gio.h>

typedef struct test_dbus TestDBus;

typedef
void
(*TestDBusStartFunc)(
GDBusConnection* client,
GDBusConnection* server,
void* user_data);

TestDBus*
test_dbus_new(
TestDBusStartFunc start,
void* user_data);

void
test_dbus_free(
TestDBus* dbus);

#endif /* TEST_DBUS_H */

/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/
1 change: 1 addition & 0 deletions unit/coverage/run
Expand Up @@ -17,6 +17,7 @@ core_tag_t2 \
core_target \
core_tlv \
core_util \
plugins_dbus_handlers \
plugins_dbus_handlers_config \
plugins_dbus_handlers_type_generic \
plugins_dbus_handlers_type_mediatype \
Expand Down
8 changes: 8 additions & 0 deletions unit/plugins_dbus_handlers/Makefile
@@ -0,0 +1,8 @@
# -*- Mode: makefile-gmake -*-

EXE = test_plugins_dbus_handlers

GEN_SRC = test.handler.c
COMMON_SRC = test_main.c test_dbus.c

include ../common/Makefile.plugins
35 changes: 35 additions & 0 deletions unit/plugins_dbus_handlers/test.handler.xml
@@ -0,0 +1,35 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="test.handler">
<method name="Handle">
<arg name="ndef" type="ay" direction="in">
<annotation name="org.gtk.GDBus.C.ForceGVariant" value="true"/>
</arg>
<arg name="status" type="i" direction="out"/>
</method>
<method name="Handle2">
<arg name="ndef" type="ay" direction="in">
<annotation name="org.gtk.GDBus.C.ForceGVariant" value="true"/>
</arg>
<arg name="status" type="i" direction="out"/>
</method>
<method name="NoReturn">
<arg name="ndef" type="ay" direction="in">
<annotation name="org.gtk.GDBus.C.ForceGVariant" value="true"/>
</arg>
</method>
<method name="InvalidReturn">
<arg name="ndef" type="ay" direction="in">
<annotation name="org.gtk.GDBus.C.ForceGVariant" value="true"/>
</arg>
<arg name="status" type="s" direction="out"/>
</method>
<method name="Notify">
<arg name="handled" type="b" direction="in"/>
<arg name="ndef" type="ay" direction="in">
<annotation name="org.gtk.GDBus.C.ForceGVariant" value="true"/>
</arg>
</method>
</interface>
</node>

0 comments on commit 78d7b6d

Please sign in to comment.