[X2Go-Commits] [libx2goclient] 03/132: src/test/sshtest.c: rework completely, split into a lot of smaller subfunctions, handle errors more correctly, fix running the full suite of tests even if no location was specified.

git-admin at x2go.org git-admin at x2go.org
Fri Dec 3 15:26:28 CET 2021


This is an automated email from the git hooks/post-receive script.

x2go pushed a commit to branch master
in repository libx2goclient.

commit 624f6baf3d520afa345047bdd08db00c7700a8eb
Author: Mihai Moldovan <ionic at ionic.de>
Date:   Fri Oct 30 06:29:41 2020 +0100

    src/test/sshtest.c: rework completely, split into a lot of smaller subfunctions, handle errors more correctly, fix running the full suite of tests even if no location was specified.
---
 src/test/sshtest.c | 287 +++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 243 insertions(+), 44 deletions(-)

diff --git a/src/test/sshtest.c b/src/test/sshtest.c
index 1a3294f..200ebc9 100644
--- a/src/test/sshtest.c
+++ b/src/test/sshtest.c
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <locale.h>
 #include <unistd.h>
+#include <stdbool.h>
 
 #include <libintl.h>
 
@@ -43,77 +44,243 @@
 
 #define _(String) gettext (String)
 
-int main (const int argc, const char * const * const argv) {
-  int ret = EXIT_FAILURE;
-
-  setlocale (LC_MESSAGES, "");
-  bindtextdomain (PACKAGE, LOCALEDIR);
-  textdomain (PACKAGE);
+static _Bool test_x2goclient_network_ssh_properties_session_path (X2GoClientNetworkSSH *net_ssh) {
+  _Bool ret = TRUE;
 
-  g_printf (_("SSH testing utility for %s version %s\n\n"), _(PACKAGE_NAME), _(PACKAGE_VERSION));
+  gchar *session_path = NULL;
+  g_object_get (G_OBJECT (net_ssh), "session-path", &session_path, NULL);
 
-  GString *ssh_uri = NULL;
-  if (argc != 2) {
-    g_fprintf (stderr, _("Error. Program needs exactly one argument: an SSH location specifier.\n"));
+  g_printf ("session-path: ");
+  if (session_path) {
+    g_printf ("%s", session_path);
   }
   else {
-    ssh_uri = g_string_new (argv[1]);
+    g_printf ("not available");
+
+    ret = FALSE;
   }
+  g_printf ("\n");
 
-  X2GoClientNetworkSSH *net_ssh = x2goclient_network_ssh_new (NULL);
-  gchar *session_path = NULL;
-  g_object_get (G_OBJECT (net_ssh), "session-path", &session_path, NULL);
-  g_printf ("session-path: %s\n", session_path);
   g_free (session_path);
   session_path = NULL;
+
+  return (ret);
+}
+
+static _Bool test_x2goclient_network_ssh_properties_socket (const GString * const ssh_uri, X2GoClientNetworkSSH *net_ssh) {
+  _Bool ret = TRUE;
+
+  /*
+   * Set socket specification.
+   *
+   * This can't return any error status per se, but if the parsing fails, the
+   * corresponding read-only socket property will be invalid.
+   */
   g_object_set (G_OBJECT (net_ssh), "socket-spec", ssh_uri, NULL);
+
   GSocketAddress *sock_addr = NULL;
   g_object_get (G_OBJECT (net_ssh), "socket", &sock_addr, NULL);
+
   g_printf ("sock_addr: %p\n", sock_addr);
+
+  ret = (!(!(sock_addr)));
+
   g_clear_object (&sock_addr);
 
-  /*
-   * Provoke error.
-   *
-   * This should not work/change the initial state.
-   */
-  g_object_set (G_OBJECT (net_ssh), "session-path", "/i/don't/exist", NULL);
-  g_object_get (G_OBJECT (net_ssh), "session-path", &session_path, NULL);
-  g_printf ("new session-path: %s\n", session_path);
-  g_free (session_path);
-  session_path = NULL;
+  return (ret);
+}
+
+static _Bool test_x2goclient_network_ssh_properties_session_path_reset (X2GoClientNetworkSSH *net_ssh) {
+  _Bool ret = TRUE;
+
+  g_printf ("Testing session path reset (supposed to fail) ...\n");
+
+  gchar *session_path_orig = NULL;
+  g_object_get (G_OBJECT (net_ssh), "session-path", &session_path_orig, NULL);
+
+  g_printf ("original session-path: ");
+  if (session_path_orig) {
+    g_printf ("%s", session_path_orig);
+  }
+  else {
+    g_printf ("not available");
+  }
+  g_printf ("\n");
+
+  ret &= (!(!(session_path_orig)));
+
+  if (ret) {
+    /*
+     * Provoke error.
+     *
+     * This should not work/change the initial state.
+     */
+    g_object_set (G_OBJECT (net_ssh), "session-path", "/i/don't/exist", NULL);
+
+    gchar *session_path_new = NULL;
+    g_object_get (G_OBJECT (net_ssh), "session-path", &session_path_new, NULL);
+
+    g_printf ("new session-path: ");
+    if (session_path_new) {
+      g_printf ("%s", session_path_new);
+    }
+    else {
+      g_printf ("not available");
+    }
+    g_printf ("\n");
+
+    ret &= (!(strcmp (session_path_orig, session_path_new)));
+
+    if (!(ret)) {
+      g_printf ("Session paths don't match.\n");
+    }
+
+    g_free (session_path_orig);
+    g_free (session_path_new);
+    session_path_orig = session_path_new = NULL;
+  }
+
+  return (ret);
+}
+
+static _Bool test_x2goclient_network_ssh_properties (const GString * const ssh_uri, X2GoClientNetworkSSH *net_ssh) {
+  _Bool ret = TRUE;
+
+  ret &= test_x2goclient_network_ssh_properties_session_path (net_ssh);
+
+  /* Check for valid ssh location. */
+  ret &= (!(!(ssh_uri)));
+
+  if (!(ret)) {
+    g_printf ("SSH location invalid.\n");
+  }
+
+  if (ret) {
+    ret &= test_x2goclient_network_ssh_properties_socket (ssh_uri, net_ssh);
+  }
+
+  if (ret) {
+    ret &= test_x2goclient_network_ssh_properties_session_path_reset (net_ssh);
+  }
+
+  return (ret);
+}
+
+static _Bool test_x2goclient_network_ssh_get_conn_ret (X2GoClientNetworkSSH *net_ssh) {
+  _Bool ret = TRUE;
 
-  /*
-   * Check connection status fetching and setting.
-   *
-   * The latter should fail.
-   */
   g_printf ("Trying to get connection status...\n");
   gboolean conn_ret = TRUE;
   g_object_get (G_OBJECT (net_ssh), "connected", &conn_ret, NULL);
-  g_printf ("Current connection status: %d\n", (int) (conn_ret));
+  g_printf ("Current connection status: %s\n", x2goclient_bool_to_str (conn_ret));
+
+  ret = (!(conn_ret));
+
+  return (ret);
+}
+
+static _Bool test_x2goclient_network_ssh_set_conn_ret (X2GoClientNetworkSSH *net_ssh) {
+  _Bool ret = TRUE;
+
+  gboolean conn_ret_orig = TRUE;
+  g_object_get (G_OBJECT (net_ssh), "connected", &conn_ret_orig, NULL);
 
   g_printf ("Trying to set connection status (should fail)...\n");
-  conn_ret = TRUE;
-  g_object_set (G_OBJECT (net_ssh), "connected", &conn_ret, NULL);
+  gboolean conn_ret_new = TRUE;
+  g_object_set (G_OBJECT (net_ssh), "connected", &conn_ret_new, NULL);
 
   g_printf ("Trying to get connection status again...\n");
-  g_object_get (G_OBJECT (net_ssh), "connected", &conn_ret, NULL);
-  g_printf ("Current connection status (again): %d\n", (int) (conn_ret));
+  g_object_get (G_OBJECT (net_ssh), "connected", &conn_ret_new, NULL);
+  g_printf ("Current connection status (again): %s\n", x2goclient_bool_to_str (conn_ret_new));
+
+  ret = (conn_ret_orig == conn_ret_new);
+
+  if (!(ret)) {
+    g_printf ("Connection statuses don't match.\n");
+  }
+
+  return (ret);
+}
+
+static _Bool test_x2goclient_network_ssh_connect_after_connect (X2GoClientNetworkSSH *net_ssh, const gboolean conn_ret) {
+  _Bool ret = TRUE;
+
+  /* Connect again, shouldn't cause anything to change. */
+  gboolean conn_ret_new = FALSE;
+  g_printf ("Trying to connect again...\n");
+  conn_ret_new = x2goclient_network_ssh_connect (net_ssh, NULL);
+  g_printf ("Connection status: %s.\n", x2goclient_bool_to_str (conn_ret_new));
+
+  ret &= (conn_ret == conn_ret_new);
+
+  if (!(ret)) {
+    g_printf ("Connection statuses don't match.\n");
+  }
+
+  return (ret);
+}
+
+static _Bool test_x2goclient_network_ssh_connect (X2GoClientNetworkSSH *net_ssh) {
+  _Bool ret = TRUE;
 
   /* Actually connect. */
   g_printf ("Trying to connect...\n");
-  conn_ret = x2goclient_network_ssh_connect (net_ssh, NULL);
-  g_printf ("Connection status: %s.\n", (conn_ret) ? "true" : "false");
-  g_object_get (G_OBJECT (net_ssh), "connected", &conn_ret, NULL);
-  g_printf ("Current connection status (via getter): %d\n", (int) (conn_ret));
+  gboolean conn_ret = x2goclient_network_ssh_connect (net_ssh, NULL);
+  g_printf ("Connection status: %s.\n", x2goclient_bool_to_str (conn_ret));
+
+  ret &= conn_ret;
+
+  if (ret) {
+    /* Check connection status getter sync. */
+    gboolean conn_ret_new = FALSE;
+    g_object_get (G_OBJECT (net_ssh), "connected", &conn_ret_new, NULL);
+    g_printf ("Current connection status (via getter): %s\n", x2goclient_bool_to_str (conn_ret_new));
+
+    ret &= (conn_ret == conn_ret_new);
+  }
+
+  /*
+   * Cannot move this to test_x2goclient_network_ssh because we need conn_ret.
+   * Bummer, but likely not too bad.
+   */
+  if (ret) {
+    ret &= test_x2goclient_network_ssh_connect_after_connect (net_ssh, conn_ret);
+  }
+
+  return (ret);
+}
 
-  if (conn_ret) {
-    /* Connect again, shouldn't cause anything to change. */
-    g_printf ("Trying to connect again...\n");
-    conn_ret = x2goclient_network_ssh_connect (net_ssh, NULL);
-    g_printf ("Connection status: %s.\n", (conn_ret) ? "true" : "false");
+static _Bool test_x2goclient_network_ssh (const GString * const ssh_uri) {
+  _Bool ret = TRUE;
 
+  /* Create object. */
+  X2GoClientNetworkSSH *net_ssh = x2goclient_network_ssh_new (NULL);
+
+  ret = (!(!(net_ssh)));
+
+  if (ret) {
+    ret &= test_x2goclient_network_ssh_properties (ssh_uri, net_ssh);
+  }
+
+  if (ret) {
+    /* Check connection status fetching. */
+    ret &= test_x2goclient_network_ssh_get_conn_ret (net_ssh);
+  }
+
+  if (ret) {
+    /*
+     * Check connection status setting.
+     *
+     * This should fail.
+     */
+    ret &= test_x2goclient_network_ssh_set_conn_ret (net_ssh);
+  }
+
+  if (ret) {
+    ret &= test_x2goclient_network_ssh_connect (net_ssh);
+  }
+
+  if (ret) {
     /* Let ssh connection live for a few seconds, give or take. */
     sleep (30);
   }
@@ -122,3 +289,35 @@ int main (const int argc, const char * const * const argv) {
 
   return (ret);
 }
+
+int main (const int argc, const char * const * const argv) {
+  int ret = EXIT_SUCCESS;
+
+  setlocale (LC_MESSAGES, "");
+  bindtextdomain (PACKAGE, LOCALEDIR);
+  textdomain (PACKAGE);
+
+  g_printf (_("SSH testing utility for %s version %s\n\n"), _(PACKAGE_NAME), _(PACKAGE_VERSION));
+
+  GString *ssh_uri = NULL;
+  if (argc != 2) {
+    g_fprintf (stderr, _("Error. Program needs exactly one argument: an SSH location specifier.\n"));
+
+    ret = EXIT_FAILURE;
+  }
+  else {
+    ssh_uri = g_string_new (argv[1]);
+  }
+
+  if (EXIT_SUCCESS == ret) {
+    /* Initialize ret to failure. */
+    ret = EXIT_FAILURE;
+
+    if (test_x2goclient_network_ssh (ssh_uri)) {
+      /* We'll only not fail if all tests succeded. */
+      ret = EXIT_SUCCESS;
+    }
+  }
+
+  return (ret);
+}

--
Alioth's /home/x2go-admin/maintenancescripts/git/hooks/post-receive-email on /srv/git/code.x2go.org/libx2goclient.git


More information about the x2go-commits mailing list