[calls/wip/ui-manage-accounts: 15/26] test: Add credentials test




commit 299e030783371a0fdef0146fe1911a34936f673d
Author: Evangelos Ribeiro Tzaras <evangelos tzaras puri sm>
Date:   Thu Jun 24 17:45:48 2021 +0200

    test: Add credentials test
    
    We're testing the newly added API (which is used for the account management).

 tests/meson.build        |  14 +++
 tests/test-credentials.c | 235 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 249 insertions(+)
---
diff --git a/tests/meson.build b/tests/meson.build
index f54f4c78..25c493ba 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -127,4 +127,18 @@ t = executable('util', test_sources,
                )
 test('util', t, env: test_env)
 
+test_sources = [ 'test-credentials.c' ]
+t = executable('credentials', test_sources,
+               c_args : test_cflags,
+               link_args: test_link_args,
+               pie: true,
+               link_with : [calls_vala, libcalls],
+               dependencies: calls_deps,
+               include_directories : [
+                   calls_includes,
+                 ]
+               )
+test('credentials', t, env: test_env)
+
+
 endif
diff --git a/tests/test-credentials.c b/tests/test-credentials.c
new file mode 100644
index 00000000..89b6d166
--- /dev/null
+++ b/tests/test-credentials.c
@@ -0,0 +1,235 @@
+/*
+ * Copyright (C) 2021 Purism SPC
+ *
+ * SPDX-License-Identifier: GPL-3.0+
+ *
+ * Author: Evangelos Ribeiro Tzaras <evangelos tzaras puri sm>
+ */
+
+#include "calls-credentials.h"
+
+#include <gtk/gtk.h>
+
+
+static const char *credential_data =
+  "# Credentials for Alice at home\n"
+  "[Alice-home]\n"
+  "User=alice\n"
+  "Password=White Rabbit\n"
+  "Host=example.org\n"
+  "Port=0\n"
+  "DisplayName=Alice@Home\n"
+  "# Credentials for Alice at work\n"
+  "[Alice-work]\n"
+  "User=alice\n"
+  "Password=White Rabbit\n"
+  "Host=example.org\n"
+  "Port=0\n"
+  "Protocol=UDP\n"
+  "DisplayName=Alice@Work\n"
+  "# Credentials for Alice with a wrong password\n"
+  "[Alice-error-password]\n"
+  "User=alice\n"
+  "Password=Brown Rabbit\n"
+  "Host=example.org\n"
+  "Port=0\n"
+  "DisplayName=Alice@Wonderland\n"
+  "[Alice-error-protocol]\n"
+  "User=alice\n"
+  "Password=White Rabbit\n"
+  "Host=example.org\n"
+  "Port=0\n"
+  "Protocol=TCP\n"
+  "DisplayName=Alice@Wonderland\n"
+  "# Credentials for Bob\n"
+  "[Bob]\n"
+  "User=bob\n"
+  "Host=bobs-server.org\n"
+  "Password=1234\n"
+  "Protocol=TLS\n"
+  "DisplayName=Bobby Brown\n"
+  "# Debugging Credentials for Charley\n"
+  "[Charley]\n"
+  "User=charley\n"
+  "Debug=1\n"
+  "# Invalid credentials for Dorothy (missing host)\n"
+  "[Dorothy]\n"
+  "User=dorothy\n";
+
+
+static gboolean
+are_credentials_the_same (CallsCredentials *cred_a,
+                          CallsCredentials *cred_b)
+{
+  g_autofree char *a_host = NULL;
+  g_autofree char *b_host = NULL;
+  g_autofree char *a_user = NULL;
+  g_autofree char *b_user = NULL;
+  g_autofree char *a_password = NULL;
+  g_autofree char *b_password = NULL;
+  g_autofree char *a_protocol = NULL;
+  g_autofree char *b_protocol = NULL;
+  gint a_port, b_port;
+  gboolean a_debug, b_debug;
+
+  g_object_get (cred_a,
+                "host", &a_host,
+                "user", &a_user,
+                "password", &a_password,
+                "protocol", &a_protocol,
+                "port", &a_port,
+                "debug", &a_debug,
+                NULL);
+
+  g_object_get (cred_b,
+                "host", &b_host,
+                "user", &b_user,
+                "password", &b_password,
+                "protocol", &b_protocol,
+                "port", &b_port,
+                "debug", &b_debug,
+                NULL);
+
+  if (g_strcmp0 (a_host, b_host) != 0)
+    return FALSE;
+
+  if (g_strcmp0 (a_user, b_user) != 0)
+    return FALSE;
+
+  if (g_strcmp0 (a_password, b_password) != 0)
+    return FALSE;
+
+  if (g_strcmp0 (a_protocol, b_protocol) != 0)
+    return FALSE;
+
+  if (a_port != b_port)
+    return FALSE;
+
+  if (a_debug != b_debug)
+    return FALSE;
+
+  return TRUE;
+}
+
+static void
+test_credentials_load (void)
+{
+  g_autoptr (CallsCredentials) alice_home = NULL;
+  g_autoptr (CallsCredentials) alice_work = NULL;
+  g_autoptr (CallsCredentials) alice_err_pw = NULL;
+  g_autoptr (CallsCredentials) alice_err_prot = NULL;
+  g_autoptr (CallsCredentials) bob = NULL;
+  g_autoptr (CallsCredentials) charley = NULL;
+  g_autoptr (CallsCredentials) dorothy = NULL;
+  g_autoptr (GKeyFile) keyfile = g_key_file_new ();
+
+  g_assert_true (g_key_file_load_from_data (keyfile,
+                                            credential_data,
+                                            strlen (credential_data),
+                                            G_KEY_FILE_NONE,
+                                            NULL));
+
+  alice_home = calls_credentials_new_from_keyfile (keyfile, "Alice-home");
+  g_assert_nonnull (alice_home);
+
+  alice_work = calls_credentials_new_from_keyfile (keyfile, "Alice-work");
+  g_assert_nonnull (alice_work);
+
+  alice_err_pw = calls_credentials_new_from_keyfile (keyfile, "Alice-error-password");
+  g_assert_nonnull (alice_err_pw);
+
+  alice_err_prot = calls_credentials_new_from_keyfile (keyfile, "Alice-error-protocol");
+  g_assert_nonnull (alice_err_prot);
+
+  bob = calls_credentials_new_from_keyfile (keyfile, "Bob");
+  g_assert_nonnull (bob);
+
+  charley = calls_credentials_new_from_keyfile (keyfile, "Charley");
+  g_assert_nonnull (charley);
+
+  dorothy = calls_credentials_new_from_keyfile (keyfile, "Dorothy");
+  g_assert_null (dorothy);
+
+  g_assert_true (calls_credentials_is_ready (alice_home));
+  g_assert_true (calls_credentials_is_ready (alice_work));
+  g_assert_true (calls_credentials_is_ready (alice_err_pw));
+  g_assert_true (calls_credentials_is_ready (alice_err_prot));
+  g_assert_true (calls_credentials_is_ready (bob));
+  g_assert_true (calls_credentials_is_ready (charley));
+
+  g_assert_true (are_credentials_the_same (alice_home, alice_work));
+  g_assert_false (are_credentials_the_same (alice_home, alice_err_pw));
+  g_assert_false (are_credentials_the_same (alice_work, alice_err_prot));
+  g_assert_false (are_credentials_the_same (alice_home, bob));
+  g_assert_false (are_credentials_the_same (alice_home, charley));
+  g_assert_false (are_credentials_the_same (bob, charley));
+}
+
+static void
+test_credentials_save_and_load (void)
+{
+  g_autoptr (CallsCredentials) creds_to_save =
+    calls_credentials_new (CALLS_CREDENTIALS_TYPE_NULL, "Alice");
+  g_autoptr (CallsCredentials) creds_to_load = NULL;
+  g_autoptr (GKeyFile) keyfile = g_key_file_new ();
+
+  g_object_set (creds_to_save,
+                "user", "alice",
+                "host", "example.org",
+                "password", "password123",
+                "protocol", "UDP",
+                NULL);
+
+  calls_credentials_save_to_key_file (creds_to_save, keyfile);
+  creds_to_load = calls_credentials_new_from_keyfile (keyfile, "Alice");
+
+  g_assert_nonnull (creds_to_load);
+  g_assert_true (are_credentials_the_same (creds_to_save, creds_to_load));
+  g_assert_true (g_strcmp0 (calls_credentials_get_uuid (creds_to_save),
+                            calls_credentials_get_uuid (creds_to_load)) == 0);
+}
+
+static void
+test_credentials_update_credentials (void)
+{
+  g_autoptr (CallsCredentials) creds = calls_credentials_new (CALLS_CREDENTIALS_TYPE_NULL,
+                                                              "Alice");
+  g_autoptr (CallsCredentials) other_creds = NULL;
+  g_autoptr (GKeyFile) keyfile = g_key_file_new ();
+
+  g_object_set (creds,
+                "user", "alice",
+                "host", "example.org",
+                "password", "password123",
+                "protocol", "UDP",
+                NULL);
+
+  g_assert_true (g_key_file_load_from_data (keyfile,
+                                            credential_data,
+                                            strlen (credential_data),
+                                            G_KEY_FILE_NONE,
+                                            NULL));
+
+  other_creds = calls_credentials_new_from_keyfile (keyfile, "Bob");
+
+  g_assert_false (are_credentials_the_same (creds, other_creds));
+
+  calls_credentials_update_from_credentials (creds, other_creds);
+  g_assert_true (are_credentials_the_same (creds, other_creds));
+}
+
+gint
+main (gint   argc,
+      gchar *argv[])
+{
+  gtk_test_init (&argc, &argv, NULL);
+
+  g_test_add_func ("/Calls/Credentials/load",
+                   test_credentials_load);
+  g_test_add_func ("/Calls/Credentials/save_and_load",
+                   test_credentials_save_and_load);
+  g_test_add_func ("/Calls/Credentials/update",
+                   test_credentials_update_credentials);
+
+  return g_test_run();
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]