This is an automated email from the git hooks/post-receive script. x2go pushed a commit to branch master in repository libx2goclient. commit 4b9bdb04e4a4ccf8e73d101e86c1f4cfc293f72c Author: Mihai Moldovan <ionic@ionic.de> Date: Thu Sep 30 19:07:22 2021 +0200 src/: add x2goclient-network-stub.{c,h}. This is a final class inheriting from X2GoClientNetwork that does nothing. We'll use this explicitly for testing purposes only. --- src/x2goclient-network-stub.c | 318 ++++++++++++++++++++++++++++++++++++++++++ src/x2goclient-network-stub.h | 52 +++++++ 2 files changed, 370 insertions(+) diff --git a/src/x2goclient-network-stub.c b/src/x2goclient-network-stub.c new file mode 100644 index 0000000..b947df4 --- /dev/null +++ b/src/x2goclient-network-stub.c @@ -0,0 +1,318 @@ +/* -*- Mode: C; c-set-style: linux indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 8 -*- */ + +/* x2goclient-network-stub.c - X2Go Client stub network handling + + Copyright (C) 2019-2020 Mike Gabriel + Copyright (C) 2019-2020 Mihai Moldovan + All rights reserved. + + The libx2goclient library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The libx2goclient library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the Mate Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + Boston, MA 02110-1301, USA. + */ + +#include <stdbool.h> + +#include <glib.h> +#include <glib/gi18n.h> +#include <gmodule.h> +#include <gio/gio.h> + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "x2goclient.h" +#include "x2goclient-network-stub.h" + + +/** + * SECTION:x2goclient-network-stub + * @title: X2GoClientNetworkStub + * @short_description: Class faking a network connection + * @see_also: #X2GoClientNetwork + * @stability: Unstable + * @include: libx2goclient/x2goclient-network-stub.h + * + * #X2GoClientNetworkStub is a class faking network connections. + * + * It's only meant for testing purposes. + * + * An example of a more abstract implementation is #X2GoClientNetwork. + */ + +/** + * X2GoClientNetworkStub: + * + * #X2GoClientNetworkStub is an opaque data structure and can only be accessed + * using the following functions. + */ +struct _X2GoClientNetworkStub { + X2GoClientNetwork parent_instance; +}; + +G_DEFINE_TYPE (X2GoClientNetworkStub, x2goclient_network_stub, X2GOCLIENT_TYPE_NETWORK) + + +static void x2goclient_network_stub_dispose (GObject * const object); +static void x2goclient_network_stub_finalize (GObject * const object); +static gboolean x2goclient_network_stub_parent_connect (const gpointer parent, GError ** const gerr); +static gboolean x2goclient_network_stub_parent_disconnect (const gpointer parent, GError ** const gerr); + + +static void x2goclient_network_stub_class_init (X2GoClientNetworkStubClass * const klass) { + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->dispose = &x2goclient_network_stub_dispose; + object_class->dispose = &x2goclient_network_stub_finalize; + + X2GoClientNetworkClass *parent_class = X2GOCLIENT_NETWORK_CLASS (klass); + + parent_class->derived_connect = &x2goclient_network_stub_parent_connect; + parent_class->derived_disconnect = &x2goclient_network_stub_parent_disconnect; + parent_class->parse_sockspec = NULL; +} + +static void x2goclient_network_stub_init (X2GoClientNetworkStub * const self) { + (void) self; +} + +X2GoClientNetworkStub* x2goclient_network_stub_new (void) { + X2GoClientNetworkStub *ret = NULL; + + ret = (X2GoClientNetworkStub*)(g_object_new (X2GOCLIENT_TYPE_NETWORK_STUB, NULL)); + + return (ret); +} + +static void x2goclient_network_stub_dispose (GObject * const object) { + (G_OBJECT_CLASS (x2goclient_network_stub_parent_class))->dispose (object); +} + +static void x2goclient_network_stub_finalize (GObject * const object) { + X2GoClientNetworkStub *self = X2GOCLIENT_NETWORK_STUB (object); + (void) self; + + (G_OBJECT_CLASS (x2goclient_network_stub_parent_class))->finalize (object); +} + +/** + * x2goclient_network_stub_connect: + * @self: (in) (not optional): pointer to the #X2GoClientNetworkStub instance. + * @gerr: (out) (nullable): a return location for a #GError, pass %NULL if not + * interested. + * + * Wraps the stub connect function. + * + * This function can be called on #X2GoClientNetworkStub objects and chains up + * through the inheritance chain, eventually calling the actual connection + * function, x2goclient_network_stub_parent_connect(). + * + * x2goclient_network_stub_parent_connect() does nothing other than printing a + * message and updating the #X2GoClientNetwork:connected property. + * + * This function is idempotent. + * + * Calling this function should be functionally equivalent to calling the + * parent's connect function: + * + * <informalexample> + * <programlisting language='C'> + * // Assume that this variable is pointing to a fully setup + * // #X2GoClientNetworkStub instance. + * X2GoClientNetworkStub *net_stub = magic_stub_object; + * + * // This part should be equivalent to the later one. + * gboolean conn_ret = x2goclient_network_stub_connect (net_stub, NULL); + * if (conn_ret) { + * x2goclient_network_stub_disconnect (net_stub, NULL); + * } + * + * // To this one. + * conn_ret = x2goclient_network_connect ((X2GoClientNetwork*) (net_stub), + * NULL); + * if (conn_ret) { + * x2goclient_network_disconnect ((X2GoClientNetwork*) (net_stub), NULL); + * } + * </programlisting> + * </informalexample> + * + * Returns: a #gboolean value indicating success (%TRUE) or failure (%FALSE). + * + * Since: 0.0.0 + */ +gboolean x2goclient_network_stub_connect (X2GoClientNetworkStub * const self, GError ** const gerr) { + gboolean ret = FALSE; + + g_return_val_if_fail (X2GOCLIENT_IS_NETWORK_STUB (self), ret); + g_return_val_if_fail (((NULL == gerr) || (NULL == *gerr)), ret); + + X2GoClientNetwork *parent = X2GOCLIENT_NETWORK (self); + X2GoClientNetworkClass *parent_class = X2GOCLIENT_NETWORK_GET_CLASS (parent); + + /* Just chain up here. */ + g_assert (parent_class->connect); + + if (parent_class->connect) { + ret = parent_class->connect (parent, gerr); + } + + return (ret); +} + +/* + * x2goclient_network_stub_parent_connect: + * @ptr: (in) (not optional): pointer to the #X2GoClientNetwork parent + * instance. + * @gerr: (out) (nullable): a return location for a #GError, pass %NULL if not + * interested. + * + * Prints a message and does nothing else. + * + * For documentation of its internal behavior, refer to the public function + * x2goclient_network_stub_connect(). + * + * This function is <emphasis role='strong'>not</emphasis> idempotent. + * + * This function should not be called by user code. It's meant to be the + * private connect function of this class, also showcased by the fact that it + * is not idempotent. + * + * User code should call the public functions + * x2goclient_network_stub_connect() on #X2GoClientNetworkStub objects, or + * x2goclient_network_connect() on #X2GoClientNetwork objects. The latter + * function redirects to/automatically calls the more specific connection + * function of the derived class, which, for #X2GoClientNetworkStub objects, + * should be functionally equivalent to calling + * x2goclient_network_stub_connect() directly. + * + * Returns: (transfer full): a #gboolean value indicating success (%TRUE) or + * failure (%FALSE). + * + * Since: 0.0.0 + */ +static gboolean x2goclient_network_stub_parent_connect (const gpointer ptr, GError ** const gerr) { + gboolean ret = FALSE; + + g_return_val_if_fail (X2GOCLIENT_IS_NETWORK_STUB (ptr), ret); + g_return_val_if_fail (((NULL == gerr) || (NULL == *gerr)), ret); + + X2GoClientNetworkStub *self = X2GOCLIENT_NETWORK_STUB (ptr); + (void) self; + + /* Actual implementation here. */ + + /* + * We'll hang down through ret to run or skip steps depending on previous + * failures, so make sure it's initialized to no error. + */ + ret = TRUE; + + g_log (NULL, G_LOG_LEVEL_DEBUG, "Called x2goclient_network_stub_parent_connect"); + + return (ret); +} + +/** + * x2goclient_network_stub_disconnect: + * @self: (in) (not optional): pointer to the #X2GoClientNetworkStub instance. + * @gerr: (out) (nullable): a return location for a #GError, pass %NULL if not + * interested. + * + * Wraps the stub disconnect function. + * + * This function can be called on #X2GoClientNetworkStub objects and chains up + * through the inheritance chain, eventually calling the actual disconnection + * function, x2goclient_network_stub_parent_disconnect(). + * + * x2goclient_network_stub_parent_disconnect() does nothing useful other than + * to print a message. + * + * For more information, see also x2goclient_network_stub_connect(), which + * documents the connection parameters and example usage. + * + * This function is idempotent. + * + * Returns: a #gboolean value indicating success (%TRUE) or failure (%FALSE). + * + * Since: 0.0.0 + */ +gboolean x2goclient_network_stub_disconnect (X2GoClientNetworkStub * const self, GError ** const gerr) { + gboolean ret = FALSE; + + g_return_val_if_fail (X2GOCLIENT_IS_NETWORK_STUB (self), ret); + g_return_val_if_fail (((NULL == gerr) || (NULL == *gerr)), ret); + + X2GoClientNetwork *parent = X2GOCLIENT_NETWORK (self); + X2GoClientNetworkClass *parent_class = X2GOCLIENT_NETWORK_GET_CLASS (parent); + + /* Just chain up here. */ + g_assert (parent_class->disconnect); + + if (parent_class->disconnect) { + ret = parent_class->disconnect (parent, gerr); + } + + return (ret); +} + +/* + * x2goclient_network_stub_parent_disconnect: + * @ptr: (in) (not optional): pointer to the #X2GoClientNetwork parent + * instance. + * @gerr: (out) (nullable): a return location for a #GError, pass %NULL if not + * interested. + * + * Prints a message and does nothing else. + * + * For documentation of its internal behavior, refer to the public + * x2goclient_network_stub_disconnect() function. + * + * This function is <emphasis role='strong'>not</emphasis> idempotent. + * + * This function should not be called by user code. It's meant to be the + * private disconnect function of this class, also showcased by the fact that + * it is not idempotent. + * + * User code should call the public functions + * x2goclient_network_stub_disconnect() on #X2GoClientNetworkStub objects, or + * x2goclient_network_disconnect() on #X2GoClientNetwork objects. The latter + * function redirects to/automatically calls the more specific disconnection + * function of the derived class, which, for #X2GoClientNetworkStub objects, + * should be functionally equivalent to calling + * x2goclient_network_stub_disconnect() directly. + * + * Returns: (transfer full): a #gboolean value indicating success (%TRUE) or + * failure (%FALSE). + * + * Since: 0.0.0 + */ +static gboolean x2goclient_network_stub_parent_disconnect (const gpointer ptr, GError ** const gerr) { + gboolean ret = FALSE; + + g_return_val_if_fail (X2GOCLIENT_IS_NETWORK_STUB (ptr), ret); + g_return_val_if_fail (((NULL == gerr) || (NULL == *gerr)), ret); + + X2GoClientNetworkStub *self = X2GOCLIENT_NETWORK_STUB (ptr); + (void) self; + + /* Actual implementation here. */ + + /* Most things can't really fail, so prepare for success. */ + ret = TRUE; + + g_log (NULL, G_LOG_LEVEL_DEBUG, "Called x2goclient_network_stub_parent_disconnect"); + + return (ret); +} diff --git a/src/x2goclient-network-stub.h b/src/x2goclient-network-stub.h new file mode 100644 index 0000000..37089dd --- /dev/null +++ b/src/x2goclient-network-stub.h @@ -0,0 +1,52 @@ +/* -*- Mode: C; c-set-style: linux indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 8 -*- */ + +/* x2goclient-network-stub.h - X2Go Client stub network handling + + Copyright (C) 2019-2020 Mike Gabriel + Copyright (C) 2019-2020 Mihai Moldovan + All rights reserved. + + The libx2goclient library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The libx2goclient library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the Mate Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + Boston, MA 02110-1301, USA. + */ + +#ifndef x2goclient_network_stub_h +#define x2goclient_network_stub_h + +#include <glib-object.h> +#include <gmodule.h> + +#include "x2goclient-network.h" + +G_BEGIN_DECLS + +#define X2GOCLIENT_TYPE_NETWORK_STUB (x2goclient_network_stub_get_type ()) +G_DECLARE_FINAL_TYPE (X2GoClientNetworkStub, x2goclient_network_stub, X2GOCLIENT, NETWORK_STUB, X2GoClientNetwork) + +X2GoClientNetworkStub* x2goclient_network_stub_new (void); + + +/* + * Error handling helpers. + */ +#define X2GOCLIENT_NETWORK_STUB_ERROR g_quark_from_static_string ("x2goclient-network-stub") + + +gboolean x2goclient_network_stub_connect (X2GoClientNetworkStub * const self, GError ** const gerr); +gboolean x2goclient_network_stub_disconnect (X2GoClientNetworkStub * const self, GError ** const gerr); + +G_END_DECLS + +#endif /* x2goclient_network_stub_h */ -- Alioth's /home/x2go-admin/maintenancescripts/git/hooks/post-receive-email on /srv/git/code.x2go.org/libx2goclient.git