tests: added support for DBus service tests

We can now run 'simulated' modems against a ModemManager running in its own
session bus.
This commit is contained in:
Aleksander Morgado
2013-11-25 18:32:20 +01:00
parent ba58aa42f2
commit bff2a8d2f2
8 changed files with 849 additions and 1 deletions

1
.gitignore vendored
View File

@@ -156,6 +156,7 @@ uml290/uml290mode
plugins/test-suite.log
plugins/test-modem-helpers-huawei*
plugins/test-modem-helpers-altair*
plugins/test-service-*
TAGS
ABOUT-NLS

View File

@@ -37,10 +37,55 @@ udevrules_DATA =
# Unit tests
noinst_PROGRAMS =
# Helper libs
noinst_LTLIBRARIES =
########################################
# Common service test support
noinst_LTLIBRARIES += libmm-test-common.la
libmm_test_common_la_SOURCES = \
tests/test-fixture.h \
tests/test-fixture.c \
tests/test-port-context.h \
tests/test-port-context.c
libmm_test_common_la_CPPFLAGS = \
$(MM_CFLAGS) \
-I$(top_srcdir)/include \
-I$(top_builddir)/include \
-I$(top_srcdir)/libmm-glib \
-I$(top_srcdir)/libmm-glib/generated \
-I$(top_builddir)/libmm-glib/generated \
-I$(top_builddir)/libmm-glib/generated/tests \
-DTEST_SERVICES=\""$(abs_top_builddir)/data/tests"\"
libmm_test_common_la_LIBADD = \
${top_builddir}/libmm-glib/generated/tests/libmm-test-generated.la \
$(top_builddir)/libmm-glib/libmm-glib.la
TEST_COMMON_COMPILER_FLAGS = \
$(MM_CFLAGS) \
-I$(top_srcdir)/plugins/tests \
-I$(top_srcdir)/include \
-I$(top_builddir)/include \
-I$(top_srcdir)/libmm-glib \
-I$(top_srcdir)/libmm-glib/generated \
-I$(top_builddir)/libmm-glib/generated \
-I$(top_builddir)/libmm-glib/generated/tests \
-DCOMMON_GSM_PORT_CONF=\""$(abs_top_srcdir)/plugins/tests/gsm-port.conf"\"
TEST_COMMON_LIBADD_FLAGS = \
$(builddir)/libmm-test-common.la \
$(top_builddir)/libmm-glib/libmm-glib.la
########################################
# Icera-specific support
noinst_LTLIBRARIES = libmm-utils-icera.la
noinst_LTLIBRARIES += libmm-utils-icera.la
libmm_utils_icera_la_SOURCES = \
icera/mm-broadband-modem-icera.h \
icera/mm-broadband-modem-icera.c \
@@ -90,6 +135,12 @@ libmm_plugin_generic_la_SOURCES = \
libmm_plugin_generic_la_CPPFLAGS = $(PLUGIN_COMMON_COMPILER_FLAGS)
libmm_plugin_generic_la_LDFLAGS = $(PLUGIN_COMMON_LINKER_FLAGS)
noinst_PROGRAMS += test-service-generic
test_service_generic_SOURCES = generic/tests/test-service-generic.c
test_service_generic_CPPFLAGS = $(TEST_COMMON_COMPILER_FLAGS)
test_service_generic_LDADD = $(TEST_COMMON_LIBADD_FLAGS)
test_service_generic_LDFLAGS = $(PLUGIN_COMMON_LINKER_FLAGS)
## Motorola
libmm_plugin_motorola_la_SOURCES = \
motorola/mm-plugin-motorola.c \

View File

@@ -0,0 +1,84 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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 2 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:
*
* Copyright (C) 2013 Aleksander Morgado <aleksander@gnu.org>
*/
#include <unistd.h>
#include <glib.h>
#include <glib-object.h>
#include <libmm-glib.h>
#include "test-port-context.h"
#include "test-fixture.h"
/*****************************************************************************/
static void
test_something (TestFixture *fixture)
{
GError *error = NULL;
MMObject *obj;
MMModem *modem;
TestPortContext *port0;
const gchar *ports [] = {
"abstract:port0",
NULL
};
/* Setup new port context */
port0 = test_port_context_new (ports[0]);
test_port_context_load_commands (port0, COMMON_GSM_PORT_CONF);
test_port_context_start (port0);
/* Ensure no modem is modem exported */
test_fixture_no_modem (fixture);
/* Set the test profile */
test_fixture_set_profile (fixture,
"test-something",
"Generic",
(const gchar *const *)ports);
/* Wait and get the modem object */
obj = test_fixture_get_modem (fixture);
/* Get Modem interface, and enable */
modem = mm_object_get_modem (obj);
g_assert (modem != NULL);
mm_modem_enable_sync (modem, NULL, &error);
g_assert_no_error (error);
/* And disable */
mm_modem_disable_sync (modem, NULL, &error);
g_assert_no_error (error);
g_object_unref (modem);
g_object_unref (obj);
/* Stop port context */
test_port_context_stop (port0);
test_port_context_free (port0);
}
/*****************************************************************************/
int main (int argc,
char *argv[])
{
g_test_init (&argc, &argv, NULL);
TEST_ADD ("/MM/Service/Generic", test_something);
return g_test_run ();
}

View File

@@ -0,0 +1,46 @@
AT \r\nOK\r\n
ATE0 \r\nOK\r\n
ATV1 \r\nOK\r\n
AT+CMEE=1 \r\nOK\r\n
ATX4 \r\nOK\r\n
AT&C1 \r\nOK\r\n
AT+IFC=1,1 \r\nOK\r\n
AT+GCAP \r\n+GCAP: +CGSM +DS +ES\r\n\r\nOK\r\n
ATI \r\nManufacturer: Dummy vendor\r\nModel: Dummy model\r\nRevision: Dummy revision\r\nIMEI: 001100110011002<CR><LF>+GCAP: +CGSM,+DS,+ES\r\n\r\nOK\r\n
AT+WS46=? \r\n+WS46: (12,22)\r\n\r\nOK\r\n
AT+CGMI \r\nDummy vendor\r\n\r\nOK\r\n
AT+CGMM \r\nDummy model\r\n\r\nOK\r\n
AT+CGMR \r\nDummy revision\r\n\r\nOK\r\n
AT+CGSN \r\n123456789012345\r\n\r\nOK\r\n
AT+CGDCONT=? \r\n+CGDCONT: (1-11),"IP",,,(0-2),(0-3)\r\n+CGDCONT: (1-11),"IPV6",,,(0-2),(0-3)\r\n+CGDCONT: (1-11),"IPV4V6",,,(0-2),(0-3)\r\n+CGDCONT: (1-11),"PPP",,,(0-2),(0-3)\r\n\r\nOK\r\n
AT+CIMI \r\n998899889988997\r\n\r\nOK\r\n
AT+CLCK=? \r\n+CLCK: ("SC","AO","OI","OX","AI","IR","AB","AG","AC","PS","FD")\r\n\r\nOK\r\n
AT+CLCK="SC",2 \r\n+CLCK: 1\r\n\r\nOK\r\n
AT+CLCK="FD",2 \r\n+CLCK: 1\r\n\r\nOK\r\n
AT+CLCK="PS",2 \r\n+CLCK: 1\r\n\r\nOK\r\n
AT+CFUN? \r\n+CFUN: 1\r\n\r\nOK\r\n
AT+CSCS=? \r\n+CSCS: ("IRA","UCS2","GSM")\r\n\r\nOK\r\n
AT+CSCS="UCS2" \r\nOK\r\n
AT+CSCS? \r\n+CSCS: "UCS2"\r\n\r\nOK\r\n
AT+CREG=2 \r\nOK\r\n
AT+CGREG=2 \r\nOK\r\n
AT+CREG=0 \r\nOK\r\n
AT+CGREG=0 \r\nOK\r\n
AT+CREG? \r\n+CREG: 2,1,"1234","001122BB"\r\n\r\nOK\r\n
AT+CGREG? \r\n+CGREG: 2,1,"31C5","0083F7CD"\r\n\r\nOK\r\n
AT+COPS=3,2;+COPS? \r\n+COPS: 0,2,"21401",2\r\n\r\nOK\r\n
AT+COPS=3,0;+COPS? \r\n+COPS: 0,0,"vodafone ES"\r\n\r\nOK\r\n
AT+CMGF=? \r\n+CMGF: (0,1)\r\n\r\nOK\r\n
AT+CMGF=0 \r\nOK\r\n
AT+CSQ \r\n+CSQ: 17,99\r\n\r\nOK\r\n
# By default, no PIN required
AT+CPIN? \r\n+CPIN: READY\r\n\r\nOK\r\n
# By default, no messaging support
AT+CNMI=? \r\nERROR\r\n
# By default, no USSD support
AT+CUSD=? \r\nERROR\r\n

View File

@@ -0,0 +1,186 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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 2 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:
*
* Copyright (C) 2013 Aleksander Morgado <aleksander@gnu.org>
*/
#include "test-fixture.h"
void
test_fixture_setup (TestFixture *fixture)
{
GError *error = NULL;
GVariant *result;
/* Create the global dbus-daemon for this test suite */
fixture->dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
/* Add the private directory with our in-tree service files,
* TEST_SERVICES is defined by the build system to point
* to the right directory. */
g_test_dbus_add_service_dir (fixture->dbus, TEST_SERVICES);
/* Start the private DBus daemon */
g_test_dbus_up (fixture->dbus);
/* Create DBus connection */
fixture->connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
if (fixture->connection == NULL)
g_error ("Error getting connection to test bus: %s", error->message);
/* Ping to autostart MM; wait up to 3s */
result = g_dbus_connection_call_sync (fixture->connection,
"org.freedesktop.ModemManager1",
"/org/freedesktop/ModemManager1",
"org.freedesktop.DBus.Peer",
"Ping",
NULL, /* inputs */
NULL, /* outputs */
G_DBUS_CALL_FLAGS_NONE,
3000, /* timeout, ms */
NULL, /* cancellable */
&error);
if (!result)
g_error ("Error starting ModemManager in test bus: %s", error->message);
g_variant_unref (result);
/* Create the proxy that we're going to test */
fixture->test = mm_gdbus_test_proxy_new_sync (fixture->connection,
G_DBUS_PROXY_FLAGS_NONE,
"org.freedesktop.ModemManager1",
"/org/freedesktop/ModemManager1",
NULL, /* cancellable */
&error);
if (fixture->test == NULL)
g_error ("Error getting ModemManager test proxy: %s", error->message);
}
void
test_fixture_teardown (TestFixture *fixture)
{
g_object_unref (fixture->connection);
/* Tear down the proxy */
if (fixture->test)
g_object_unref (fixture->test);
/* Stop the private D-Bus daemon; stopping the bus will stop MM as well */
g_test_dbus_down (fixture->dbus);
g_object_unref (fixture->dbus);
}
void
test_fixture_set_profile (TestFixture *fixture,
const gchar *profile_name,
const gchar *plugin,
const gchar *const *ports)
{
GError *error = NULL;
/* Set the test profile */
g_assert (fixture->test != NULL);
if (!mm_gdbus_test_call_set_profile_sync (fixture->test,
profile_name,
plugin,
ports,
NULL, /* cancellable */
&error))
g_error ("Error setting test profile: %s", error->message);
}
MMObject *
test_fixture_get_modem (TestFixture *fixture)
{
GError *error = NULL;
MMManager *manager;
MMObject *found = NULL;
guint wait_time = 0;
/* Create manager */
g_assert (fixture->connection != NULL);
manager = mm_manager_new_sync (fixture->connection,
G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
NULL, /* cancellable */
&error);
if (!manager)
g_error ("Couldn't create manager: %s", error->message);
/* Find new modem object */
while (!found) {
GList *modems;
guint n_modems;
modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (manager));
n_modems = g_list_length (modems);
g_assert_cmpuint (n_modems, <=, 1);
if (n_modems == 0) {
/* Wait a bit before re-checking. We can do this kind of wait
* because properties in the manager are updated in another
* thread */
g_assert_cmpuint (wait_time, <=, 20);
wait_time++;
sleep (1);
} else
found = MM_OBJECT (g_object_ref (modems->data));
g_list_free_full (modems, (GDestroyNotify) g_object_unref);
}
g_message ("Found modem at '%s'", mm_object_get_path (found));
g_object_unref (manager);
return found;
}
void
test_fixture_no_modem (TestFixture *fixture)
{
GError *error = NULL;
MMManager *manager;
guint wait_time = 0;
gboolean no_modems = FALSE;
/* Create manager */
g_assert (fixture->connection != NULL);
manager = mm_manager_new_sync (fixture->connection,
G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
NULL, /* cancellable */
&error);
if (!manager)
g_error ("Couldn't create manager: %s", error->message);
/* Find new modem object */
while (!no_modems) {
GList *modems;
guint n_modems;
modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (manager));
n_modems = g_list_length (modems);
g_assert_cmpuint (n_modems, <=, 1);
if (n_modems == 1) {
/* Wait a bit before re-checking. We can do this kind of wait
* because properties in the manager are updated in another
* thread */
g_assert_cmpuint (wait_time, <=, 20);
wait_time++;
sleep (1);
} else
no_modems = TRUE;
g_list_free_full (modems, (GDestroyNotify) g_object_unref);
}
g_object_unref (manager);
}

View File

@@ -0,0 +1,54 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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 2 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:
*
* Copyright (C) 2013 Aleksander Morgado <aleksander@gnu.org>
*/
#ifndef TEST_FIXTURE_H
#define TEST_FIXTURE_H
#include <gio/gio.h>
#include <libmm-glib.h>
#include "mm-gdbus-test.h"
/*****************************************************************************/
/* Test fixture setup */
/* The fixture contains a GTestDBus object and
* a proxy to the service we're going to be testing.
*/
typedef struct {
GTestDBus *dbus;
MmGdbusTest *test;
GDBusConnection *connection;
} TestFixture;
void test_fixture_setup (TestFixture *fixture);
void test_fixture_teardown (TestFixture *fixture);
typedef void (*TCFunc) (TestFixture *, gconstpointer);
#define TEST_ADD(path,method) \
g_test_add (path, \
TestFixture, \
NULL, \
(TCFunc)test_fixture_setup, \
(TCFunc)method, \
(TCFunc)test_fixture_teardown)
void test_fixture_set_profile (TestFixture *fixture,
const gchar *profile_name,
const gchar *plugin,
const gchar *const *ports);
MMObject *test_fixture_get_modem (TestFixture *fixture);
void test_fixture_no_modem (TestFixture *fixture);
#endif /* TEST_FIXTURE_H */

View File

@@ -0,0 +1,391 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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 2 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:
*
* Copyright (C) 2013 Aleksander Morgado <aleksander@gnu.org>
*/
#include <gio/gio.h>
#include <gio/gunixsocketaddress.h>
#include <string.h>
#include "test-port-context.h"
#define BUFFER_SIZE 1024
struct _TestPortContext {
gchar *name;
GThread *thread;
gboolean ready;
GCond ready_cond;
GMutex ready_mutex;
GMainLoop *loop;
GSocketService *socket_service;
GList *clients;
GHashTable *commands;
};
/*****************************************************************************/
void
test_port_context_set_command (TestPortContext *self,
const gchar *command,
const gchar *response)
{
if (G_UNLIKELY (!self->commands))
self->commands = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_replace (self->commands, g_strdup (command), g_strcompress (response));
}
void
test_port_context_load_commands (TestPortContext *self,
const gchar *file)
{
GError *error = NULL;
gchar *contents;
gchar *current;
if (!g_file_get_contents (file, &contents, NULL, &error))
g_error ("Couldn't load commands file '%s': %s",
g_filename_display_name (file),
error->message);
current = contents;
while (current) {
gchar *next;
next = strchr (current, '\n');
if (next) {
*next = '\0';
next++;
}
g_strstrip (current);
if (current[0] != '\0' && current[0] != '#') {
gchar *response;
response = current;
while (*response != ' ')
response++;
g_assert (*response == ' ');
*response = '\0';
response++;
while (*response == ' ')
response++;
g_assert (*response != '\0');
test_port_context_set_command (self, current, response);
}
current = next;
}
g_free (contents);
}
static const gchar *
process_next_command (TestPortContext *ctx,
GByteArray *buffer)
{
gsize i = 0;
gchar *command;
const gchar *response;
static const gchar *error_response = "\r\nERROR\r\n";
/* Find command end */
while (buffer->data[i] != '\r' && buffer->data[i] != '\n' && i < buffer->len)
i++;
if (i == buffer->len)
/* no command */
return NULL;
while ((buffer->data[i] == '\r' || buffer->data[i] == '\n') && i < buffer->len)
buffer->data[i++] = '\0';
/* Setup command and lookup response */
command = g_strndup ((gchar *)buffer->data, i);
response = g_hash_table_lookup (ctx->commands, command);
g_free (command);
/* Remove command from buffer */
g_byte_array_remove_range (buffer, 0, i);
return response ? response : error_response;
}
/*****************************************************************************/
typedef struct {
TestPortContext *ctx;
GSocketConnection *connection;
GSource *connection_readable_source;
GByteArray *buffer;
} Client;
static void
client_free (Client *client)
{
g_source_destroy (client->connection_readable_source);
g_source_unref (client->connection_readable_source);
g_output_stream_close (g_io_stream_get_output_stream (G_IO_STREAM (client->connection)), NULL, NULL);
if (client->buffer)
g_byte_array_unref (client->buffer);
g_object_unref (client->connection);
g_slice_free (Client, client);
}
static void
connection_close (Client *client)
{
client->ctx->clients = g_list_remove (client->ctx->clients, client);
client_free (client);
}
static void
client_parse_request (Client *client)
{
const gchar *response;
do {
response = process_next_command (client->ctx, client->buffer);
if (response) {
GError *error = NULL;
if (!g_output_stream_write_all (g_io_stream_get_output_stream (G_IO_STREAM (client->connection)),
response,
strlen (response),
NULL, /* bytes_written */
NULL, /* cancellable */
&error)) {
g_warning ("Cannot send response to client: %s", error->message);
g_error_free (error);
}
}
} while (response);
}
static gboolean
connection_readable_cb (GSocket *socket,
GIOCondition condition,
Client *client)
{
guint8 buffer[BUFFER_SIZE];
GError *error = NULL;
gssize r;
if (condition & G_IO_HUP || condition & G_IO_ERR) {
g_debug ("client connection closed");
connection_close (client);
return FALSE;
}
if (!(condition & G_IO_IN || condition & G_IO_PRI))
return TRUE;
r = g_input_stream_read (g_io_stream_get_input_stream (G_IO_STREAM (client->connection)),
buffer,
BUFFER_SIZE,
NULL,
&error);
if (r < 0) {
g_warning ("Error reading from istream: %s", error ? error->message : "unknown");
if (error)
g_error_free (error);
/* Close the device */
connection_close (client);
return FALSE;
}
if (r == 0)
return TRUE;
/* else, r > 0 */
if (!G_UNLIKELY (client->buffer))
client->buffer = g_byte_array_sized_new (r);
g_byte_array_append (client->buffer, buffer, r);
/* Try to parse input messages */
client_parse_request (client);
return TRUE;
}
static Client *
client_new (TestPortContext *self,
GSocketConnection *connection)
{
Client *client;
client = g_slice_new0 (Client);
client->ctx = self;
client->connection = g_object_ref (connection);
client->connection_readable_source = g_socket_create_source (g_socket_connection_get_socket (client->connection),
G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
NULL);
g_source_set_callback (client->connection_readable_source,
(GSourceFunc)connection_readable_cb,
client,
NULL);
g_source_attach (client->connection_readable_source, NULL);
return client;
}
/* /\*****************************************************************************\/ */
static void
incoming_cb (GSocketService *service,
GSocketConnection *connection,
GObject *unused,
TestPortContext *self)
{
Client *client;
client = client_new (self, connection);
self->clients = g_list_append (self->clients, client);
}
static void
create_socket_service (TestPortContext *self)
{
GError *error = NULL;
GSocketService *service;
GSocketAddress *address;
GSocket *socket;
g_assert (self->socket_service == NULL);
/* Create socket */
socket = g_socket_new (G_SOCKET_FAMILY_UNIX,
G_SOCKET_TYPE_STREAM,
G_SOCKET_PROTOCOL_DEFAULT,
&error);
if (!socket)
g_error ("Cannot create socket: %s", error->message);
/* Bind to address */
address = (g_unix_socket_address_new_with_type (
self->name,
-1,
G_UNIX_SOCKET_ADDRESS_ABSTRACT));
if (!g_socket_bind (socket, address, TRUE, &error))
g_error ("Cannot bind socket: %s", error->message);
/* Listen */
if (!g_socket_listen (socket, &error))
g_error ("Cannot listen in socket: %s", error->message);
/* Create socket service */
service = g_socket_service_new ();
g_signal_connect (service, "incoming", G_CALLBACK (incoming_cb), self);
if (!g_socket_listener_add_socket (G_SOCKET_LISTENER (service),
socket,
NULL, /* don't pass an object, will take a reference */
&error))
g_error ("Cannot add listener to socket: %s", error->message);
/* Start it */
g_socket_service_start (service);
/* And store it */
self->socket_service = service;
/* Signal that the thread is ready */
g_mutex_lock (&self->ready_mutex);
self->ready = TRUE;
g_cond_signal (&self->ready_cond);
g_mutex_unlock (&self->ready_mutex);
if (socket)
g_object_unref (socket);
if (address)
g_object_unref (address);
}
/*****************************************************************************/
void
test_port_context_stop (TestPortContext *self)
{
g_assert (self->thread != NULL);
g_assert (self->loop != NULL);
g_main_loop_quit (self->loop);
g_thread_join (self->thread);
g_thread_unref (self->thread);
self->thread = NULL;
}
static gpointer
port_context_thread_func (TestPortContext *self)
{
create_socket_service (self);
g_assert (self->loop == NULL);
self->loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
g_main_loop_run (self->loop);
g_main_loop_unref (self->loop);
self->loop = NULL;
return NULL;
}
void
test_port_context_start (TestPortContext *self)
{
g_assert (self->thread == NULL);
self->thread = g_thread_new (self->name,
(GThreadFunc)port_context_thread_func,
self);
/* Now wait until the thread has finished its initialization and is
* ready to serve connections */
g_mutex_lock (&self->ready_mutex);
while (!self->ready)
g_cond_wait (&self->ready_cond, &self->ready_mutex);
g_mutex_unlock (&self->ready_mutex);
}
/*****************************************************************************/
void
test_port_context_free (TestPortContext *self)
{
g_assert (self->thread == NULL);
g_assert (self->loop == NULL);
g_cond_clear (&self->ready_cond);
g_mutex_clear (&self->ready_mutex);
if (self->commands)
g_hash_table_unref (self->commands);
g_list_free_full (self->clients, (GDestroyNotify)client_free);
if (self->socket_service) {
if (g_socket_service_is_active (self->socket_service))
g_socket_service_stop (self->socket_service);
g_object_unref (self->socket_service);
}
g_free (self->name);
g_slice_free (TestPortContext, self);
}
TestPortContext *
test_port_context_new (const gchar *name)
{
TestPortContext *self;
self = g_slice_new0 (TestPortContext);
self->name = g_strdup (name);
g_cond_init (&self->ready_cond);
g_mutex_init (&self->ready_mutex);
return self;
}

View File

@@ -0,0 +1,35 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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 2 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:
*
* Copyright (C) 2013 Aleksander Morgado <aleksander@gnu.org>
*/
#ifndef TEST_PORT_CONTEXT_H
#define TEST_PORT_CONTEXT_H
#include <glib.h>
#include <glib-object.h>
typedef struct _TestPortContext TestPortContext;
TestPortContext *test_port_context_new (const gchar *name);
void test_port_context_start (TestPortContext *self);
void test_port_context_stop (TestPortContext *self);
void test_port_context_free (TestPortContext *self);
void test_port_context_set_command (TestPortContext *self,
const gchar *command,
const gchar *response);
void test_port_context_load_commands (TestPortContext *self,
const gchar *commands_file);
#endif /* TEST_PORT_CONTEXT_H */