aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/tests
diff options
context:
space:
mode:
authorAleksander Morgado <aleksandermj@chromium.org>2022-12-08 13:37:55 +0000
committerAleksander Morgado <aleksander@aleksander.es>2023-01-03 13:56:25 +0000
commite14b904cbd6816cb0227d519d308ae71ddaf6e07 (patch)
tree4997ab68cc606fdf4d72a571e821cec0c8df42ef /src/plugins/tests
parent072d7ac9065f444e83b390a1e2af5471ac0d48f6 (diff)
build: move plugins directory to src/plugins
We are going to allow including the plugin sources built within the ModemManager daemon binary; moving the sources within the daemon sources directory makes it easier.
Diffstat (limited to 'src/plugins/tests')
-rw-r--r--src/plugins/tests/gsm-port.conf46
-rw-r--r--src/plugins/tests/test-fixture.c163
-rw-r--r--src/plugins/tests/test-fixture.h54
-rw-r--r--src/plugins/tests/test-helpers.c52
-rw-r--r--src/plugins/tests/test-helpers.h26
-rw-r--r--src/plugins/tests/test-keyfiles.c79
-rw-r--r--src/plugins/tests/test-port-context.c422
-rw-r--r--src/plugins/tests/test-port-context.h35
-rw-r--r--src/plugins/tests/test-udev-rules.c257
9 files changed, 1134 insertions, 0 deletions
diff --git a/src/plugins/tests/gsm-port.conf b/src/plugins/tests/gsm-port.conf
new file mode 100644
index 00000000..ae157834
--- /dev/null
+++ b/src/plugins/tests/gsm-port.conf
@@ -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: Some vendor\r\nModel: Some model\r\nRevision: Some 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\nSome vendor\r\n\r\nOK\r\n
+AT+CGMM \r\nSome model\r\n\r\nOK\r\n
+AT+CGMR \r\nSome 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
diff --git a/src/plugins/tests/test-fixture.c b/src/plugins/tests/test-fixture.c
new file mode 100644
index 00000000..29eb8d55
--- /dev/null
+++ b/src/plugins/tests/test-fixture.c
@@ -0,0 +1,163 @@
+/* -*- 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 30s */
+ 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,
+ 30000, /* 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);
+}
+
+static MMObject *
+common_get_modem (TestFixture *fixture,
+ gboolean modem_expected)
+{
+ MMObject *found = NULL;
+ guint wait_time = 0;
+
+ /* Find new modem object */
+ while (TRUE) {
+ GError *error = NULL;
+ MMManager *manager;
+ GList *modems;
+ guint n_modems;
+ gboolean ready = FALSE;
+
+ /* Create manager on each loop, so that we don't require on an external
+ * global main context processing to receive the DBus property updates.
+ */
+ 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);
+
+ 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 ((guint)modem_expected == n_modems) {
+ if (modems) {
+ found = MM_OBJECT (g_object_ref (modems->data));
+ g_message ("Found modem at '%s'", mm_object_get_path (found));
+ }
+ ready = TRUE;
+ }
+
+ g_list_free_full (modems, g_object_unref);
+ g_object_unref (manager);
+
+ if (ready)
+ break;
+
+ /* Blocking wait */
+ g_assert_cmpuint (wait_time, <=, 20);
+ wait_time++;
+ sleep (1);
+ }
+
+ return found;
+}
+
+MMObject *
+test_fixture_get_modem (TestFixture *fixture)
+{
+ return common_get_modem (fixture, TRUE);
+}
+
+void
+test_fixture_no_modem (TestFixture *fixture)
+{
+ common_get_modem (fixture, FALSE);
+}
diff --git a/src/plugins/tests/test-fixture.h b/src/plugins/tests/test-fixture.h
new file mode 100644
index 00000000..b6c24379
--- /dev/null
+++ b/src/plugins/tests/test-fixture.h
@@ -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 */
diff --git a/src/plugins/tests/test-helpers.c b/src/plugins/tests/test-helpers.c
new file mode 100644
index 00000000..8148d908
--- /dev/null
+++ b/src/plugins/tests/test-helpers.c
@@ -0,0 +1,52 @@
+/* -*- 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) 2019 Aleksander Morgado <aleksander@aleksander.es>
+ */
+
+#include <ModemManager.h>
+#define _LIBMM_INSIDE_MM
+#include <libmm-glib.h>
+
+#include "mm-log.h"
+#include "mm-modem-helpers.h"
+
+#include "test-helpers.h"
+
+void
+mm_test_helpers_compare_bands (GArray *bands,
+ const MMModemBand *expected_bands,
+ guint n_expected_bands)
+{
+ gchar *bands_str;
+ GArray *expected_bands_array;
+ gchar *expected_bands_str;
+
+ if (!expected_bands || !n_expected_bands) {
+ g_assert (!bands);
+ return;
+ }
+
+ g_assert (bands);
+ mm_common_bands_garray_sort (bands);
+ bands_str = mm_common_build_bands_string ((MMModemBand *)(gpointer)(bands->data), bands->len);
+
+ expected_bands_array = g_array_sized_new (FALSE, FALSE, sizeof (MMModemBand), n_expected_bands);
+ g_array_append_vals (expected_bands_array, expected_bands, n_expected_bands);
+ mm_common_bands_garray_sort (expected_bands_array);
+ expected_bands_str = mm_common_build_bands_string ((MMModemBand *)(gpointer)(expected_bands_array->data), expected_bands_array->len);
+ g_array_unref (expected_bands_array);
+
+ g_assert_cmpstr (bands_str, ==, expected_bands_str);
+ g_free (bands_str);
+ g_free (expected_bands_str);
+}
diff --git a/src/plugins/tests/test-helpers.h b/src/plugins/tests/test-helpers.h
new file mode 100644
index 00000000..8362754c
--- /dev/null
+++ b/src/plugins/tests/test-helpers.h
@@ -0,0 +1,26 @@
+/* -*- 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) 2019 Aleksander Morgado <aleksander@aleksander.es>
+ */
+
+#ifndef TEST_HELPERS_H
+#define TEST_HELPERS_H
+
+#include <glib.h>
+#include <libmm-glib.h>
+
+void mm_test_helpers_compare_bands (GArray *bands,
+ const MMModemBand *expected_bands,
+ guint n_expected_bands);
+
+#endif /* TEST_HELPERS_H */
diff --git a/src/plugins/tests/test-keyfiles.c b/src/plugins/tests/test-keyfiles.c
new file mode 100644
index 00000000..f92df9c3
--- /dev/null
+++ b/src/plugins/tests/test-keyfiles.c
@@ -0,0 +1,79 @@
+/* -*- 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) 2018 Aleksander Morgado <aleksander@aleksander.es>
+ */
+#include <config.h>
+
+#include <glib.h>
+#include <glib-object.h>
+#include <string.h>
+#include <stdio.h>
+#include <locale.h>
+
+#define _LIBMM_INSIDE_MM
+#include <libmm-glib.h>
+
+#include "mm-log-test.h"
+
+/************************************************************/
+
+static void
+common_test (const gchar *keyfile_path)
+{
+ GKeyFile *keyfile;
+ GError *error = NULL;
+ gboolean ret;
+
+ if (!keyfile_path)
+ return;
+
+ keyfile = g_key_file_new ();
+ ret = g_key_file_load_from_file (keyfile, keyfile_path, G_KEY_FILE_NONE, &error);
+ g_assert_no_error (error);
+ g_assert (ret);
+ g_key_file_unref (keyfile);
+}
+
+/* Placeholder test to avoid compiler warning about common_test() being unused
+ * when none of the plugins enabled in build have custom key files. */
+static void
+test_placeholder (void)
+{
+ common_test (NULL);
+}
+
+/************************************************************/
+
+#if defined ENABLE_PLUGIN_FOXCONN
+static void
+test_foxconn_t77w968 (void)
+{
+ common_test (TESTKEYFILE_FOXCONN_T77W968);
+}
+#endif
+
+/************************************************************/
+
+int main (int argc, char **argv)
+{
+ setlocale (LC_ALL, "");
+
+ g_test_init (&argc, &argv, NULL);
+ g_test_add_func ("/MM/test-keyfiles/placeholder", test_placeholder);
+
+#if defined ENABLE_PLUGIN_FOXCONN
+ g_test_add_func ("/MM/test-keyfiles/foxconn/t77w968", test_foxconn_t77w968);
+#endif
+
+ return g_test_run ();
+}
diff --git a/src/plugins/tests/test-port-context.c b/src/plugins/tests/test-port-context.c
new file mode 100644
index 00000000..e96cff7b
--- /dev/null
+++ b/src/plugins/tests/test-port-context.c
@@ -0,0 +1,422 @@
+/* -*- 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;
+ GMainContext *context;
+ GSocket *socket;
+ 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 (i < buffer->len && buffer->data[i] != '\r' && buffer->data[i] != '\n')
+ i++;
+ if (i == buffer->len)
+ /* no command */
+ return NULL;
+
+ while (i < buffer->len && (buffer->data[i] == '\r' || buffer->data[i] == '\n'))
+ 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, self->context);
+
+ 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_str_has_prefix (self->name, "abstract:") ?
+ G_UNIX_SOCKET_ADDRESS_ABSTRACT :
+ G_UNIX_SOCKET_ADDRESS_PATH)));
+ if (!g_socket_bind (socket, address, TRUE, &error))
+ g_error ("Cannot bind socket: %s", error->message);
+ g_object_unref (address);
+
+ /* 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 both the service and the socket.
+ * Since GLib 2.42 the socket may not be explicitly closed when the
+ * listener is diposed, so we'll do it ourselves. */
+ self->socket_service = service;
+ self->socket = socket;
+
+ /* 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);
+}
+
+/*****************************************************************************/
+
+static gboolean
+cancel_loop_cb (TestPortContext *self)
+{
+ g_main_loop_quit (self->loop);
+ return FALSE;
+}
+
+void
+test_port_context_stop (TestPortContext *self)
+{
+ g_assert (self->thread != NULL);
+ g_assert (self->loop != NULL);
+ g_assert (self->context != NULL);
+
+ /* Cancel main loop of the port context thread, by scheduling an idle task
+ * in the thread-owned main context */
+ g_main_context_invoke (self->context, (GSourceFunc) cancel_loop_cb, self);
+
+ g_thread_join (self->thread);
+ self->thread = NULL;
+}
+
+static gpointer
+port_context_thread_func (TestPortContext *self)
+{
+ g_assert (self->loop == NULL);
+ g_assert (self->context == NULL);
+
+ /* Define main context and loop for the thread */
+ self->context = g_main_context_new ();
+ self->loop = g_main_loop_new (self->context, FALSE);
+ g_main_context_push_thread_default (self->context);
+
+ /* Once the thread default context is setup, launch service */
+ create_socket_service (self);
+
+ g_main_loop_run (self->loop);
+
+ g_main_loop_unref (self->loop);
+ self->loop = NULL;
+ g_main_context_unref (self->context);
+ self->context = 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) {
+ GError *error = NULL;
+
+ if (!g_socket_close (self->socket, &error)) {
+ g_debug ("Couldn't close socket: %s", error->message);
+ g_error_free (error);
+ }
+ g_object_unref (self->socket);
+ }
+ 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;
+}
diff --git a/src/plugins/tests/test-port-context.h b/src/plugins/tests/test-port-context.h
new file mode 100644
index 00000000..9aaf1077
--- /dev/null
+++ b/src/plugins/tests/test-port-context.h
@@ -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 */
diff --git a/src/plugins/tests/test-udev-rules.c b/src/plugins/tests/test-udev-rules.c
new file mode 100644
index 00000000..fe11c783
--- /dev/null
+++ b/src/plugins/tests/test-udev-rules.c
@@ -0,0 +1,257 @@
+/* -*- 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) 2016 Aleksander Morgado <aleksander@aleksander.es>
+ */
+
+#include <config.h>
+
+#include <glib.h>
+#include <glib-object.h>
+#include <string.h>
+#include <stdio.h>
+#include <locale.h>
+
+#define _LIBMM_INSIDE_MM
+#include <libmm-glib.h>
+
+#include "mm-kernel-device-generic-rules.h"
+#include "mm-log-test.h"
+
+/************************************************************/
+
+static void
+common_test (const gchar *plugindir)
+{
+ GArray *rules;
+ GError *error = NULL;
+
+ if (!plugindir)
+ return;
+
+ rules = mm_kernel_device_generic_rules_load (plugindir, &error);
+ g_assert_no_error (error);
+ g_assert (rules);
+ g_assert (rules->len > 0);
+
+ g_array_unref (rules);
+}
+
+/* Placeholder test to avoid compiler warning about common_test() being unused
+ * when none of the plugins enabled in build have custom udev rules. */
+static void
+test_placeholder (void)
+{
+ common_test (NULL);
+}
+
+/************************************************************/
+
+#if defined ENABLE_PLUGIN_HUAWEI
+static void
+test_huawei (void)
+{
+ common_test (TESTUDEVRULESDIR_HUAWEI);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_MBM
+static void
+test_mbm (void)
+{
+ common_test (TESTUDEVRULESDIR_MBM);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_NOKIA_ICERA
+static void
+test_nokia_icera (void)
+{
+ common_test (TESTUDEVRULESDIR_NOKIA_ICERA);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_ZTE
+static void
+test_zte (void)
+{
+ common_test (TESTUDEVRULESDIR_ZTE);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_LONGCHEER
+static void
+test_longcheer (void)
+{
+ common_test (TESTUDEVRULESDIR_LONGCHEER);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_SIMTECH
+static void
+test_simtech (void)
+{
+ common_test (TESTUDEVRULESDIR_SIMTECH);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_X22X
+static void
+test_x22x (void)
+{
+ common_test (TESTUDEVRULESDIR_X22X);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_CINTERION
+static void
+test_cinterion (void)
+{
+ common_test (TESTUDEVRULESDIR_CINTERION);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_DELL
+static void
+test_dell (void)
+{
+ common_test (TESTUDEVRULESDIR_DELL);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_TELIT
+static void
+test_telit (void)
+{
+ common_test (TESTUDEVRULESDIR_TELIT);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_MTK
+static void
+test_mtk (void)
+{
+ common_test (TESTUDEVRULESDIR_MTK);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_HAIER
+static void
+test_haier (void)
+{
+ common_test (TESTUDEVRULESDIR_HAIER);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_FIBOCOM
+static void
+test_fibocom (void)
+{
+ common_test (TESTUDEVRULESDIR_FIBOCOM);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_QUECTEL
+static void
+test_quectel (void)
+{
+ common_test (TESTUDEVRULESDIR_QUECTEL);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_GOSUNCN
+static void
+test_gosuncn (void)
+{
+ common_test (TESTUDEVRULESDIR_GOSUNCN);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_QCOM_SOC && defined WITH_QMI
+static void
+test_qcom_soc (void)
+{
+ common_test (TESTUDEVRULESDIR_QCOM_SOC);
+}
+#endif
+
+#if defined ENABLE_PLUGIN_LINKTOP
+static void
+test_linktop (void)
+{
+ common_test (TESTUDEVRULESDIR_LINKTOP);
+}
+#endif
+
+/************************************************************/
+
+int main (int argc, char **argv)
+{
+ setlocale (LC_ALL, "");
+
+ g_test_init (&argc, &argv, NULL);
+ g_test_add_func ("/MM/test-udev-rules/placeholder", test_placeholder);
+
+#if defined ENABLE_PLUGIN_HUAWEI
+ g_test_add_func ("/MM/test-udev-rules/huawei", test_huawei);
+#endif
+#if defined ENABLE_PLUGIN_MBM
+ g_test_add_func ("/MM/test-udev-rules/mbm", test_mbm);
+#endif
+#if defined ENABLE_PLUGIN_NOKIA_ICERA
+ g_test_add_func ("/MM/test-udev-rules/nokia-icera", test_nokia_icera);
+#endif
+#if defined ENABLE_PLUGIN_ZTE
+ g_test_add_func ("/MM/test-udev-rules/zte", test_zte);
+#endif
+#if defined ENABLE_PLUGIN_LONGCHEER
+ g_test_add_func ("/MM/test-udev-rules/longcheer", test_longcheer);
+#endif
+#if defined ENABLE_PLUGIN_SIMTECH
+ g_test_add_func ("/MM/test-udev-rules/simtech", test_simtech);
+#endif
+#if defined ENABLE_PLUGIN_X22X
+ g_test_add_func ("/MM/test-udev-rules/x22x", test_x22x);
+#endif
+#if defined ENABLE_PLUGIN_CINTERION
+ g_test_add_func ("/MM/test-udev-rules/cinterion", test_cinterion);
+#endif
+#if defined ENABLE_PLUGIN_DELL
+ g_test_add_func ("/MM/test-udev-rules/dell", test_dell);
+#endif
+#if defined ENABLE_PLUGIN_TELIT
+ g_test_add_func ("/MM/test-udev-rules/telit", test_telit);
+#endif
+#if defined ENABLE_PLUGIN_MTK
+ g_test_add_func ("/MM/test-udev-rules/mtk", test_mtk);
+#endif
+#if defined ENABLE_PLUGIN_HAIER
+ g_test_add_func ("/MM/test-udev-rules/haier", test_haier);
+#endif
+#if defined ENABLE_PLUGIN_FIBOCOM
+ g_test_add_func ("/MM/test-udev-rules/fibocom", test_fibocom);
+#endif
+#if defined ENABLE_PLUGIN_QUECTEL
+ g_test_add_func ("/MM/test-udev-rules/quectel", test_quectel);
+#endif
+#if defined ENABLE_PLUGIN_GOSUNCN
+ g_test_add_func ("/MM/test-udev-rules/gosuncn", test_gosuncn);
+#endif
+#if defined ENABLE_PLUGIN_QCOM_SOC && defined WITH_QMI
+ g_test_add_func ("/MM/test-udev-rules/qcom-soc", test_qcom_soc);
+#endif
+#if defined ENABLE_PLUGIN_LINKTOP
+ g_test_add_func ("/MM/test-udev-rules/linktop", test_linktop);
+#endif
+
+ return g_test_run ();
+}