gplugin/gplugin

Parents 0f07cacab39d
Children 90d067db1688
Add gplugin_plugin_info_get_id_normalized. Fixes GPLUGIN-128

Testing Done:
Ran the new unit tests

Reviewed at https://reviews.imfreedom.org/r/131/
--- a/gplugin/gplugin-plugin-info.c Tue Sep 22 00:30:00 2020 -0500
+++ b/gplugin/gplugin-plugin-info.c Tue Sep 22 00:42:26 2020 -0500
@@ -794,6 +794,38 @@
}
/**
+ * gplugin_info_get_id_normalized:
+ * @info: The #GPluginPluginInfo instance.
+ *
+ * Gets the normalized version of the id from @info. That is, a version where
+ * only alpha-numeric and -'s are in the id.
+ *
+ * Returns: (transfer full): The normalized id of @info.
+ */
+gchar *
+gplugin_info_get_id_normalized(GPluginPluginInfo *info)
+{
+ GPluginPluginInfoPrivate *priv = NULL;
+ gchar *copy = NULL;
+
+ g_return_val_if_fail(GPLUGIN_IS_PLUGIN_INFO(info), NULL);
+
+ priv = gplugin_plugin_info_get_instance_private(info);
+
+ /* this only happens if an id wasn't passed, but that should be caught by
+ * loader and then the manager, but that doesn't stop someone from just
+ * creating a GPluginPluginInfo and doing dumb shit with it.
+ */
+ if(priv->id == NULL) {
+ return NULL;
+ }
+
+ copy = g_strdup(priv->id);
+
+ return g_strcanon(copy, G_CSET_A_2_Z G_CSET_a_2_z G_CSET_DIGITS "-", '-');
+}
+
+/**
* gplugin_plugin_info_get_abi_version:
* @info: The #GPluginPluginInfo instance.
*
--- a/gplugin/gplugin-plugin-info.h Tue Sep 22 00:30:00 2020 -0500
+++ b/gplugin/gplugin-plugin-info.h Tue Sep 22 00:42:26 2020 -0500
@@ -55,6 +55,7 @@
/* clang-format on */
const gchar *gplugin_plugin_info_get_id(GPluginPluginInfo *info);
+gchar *gplugin_info_get_id_normalized(GPluginPluginInfo *info);
guint32 gplugin_plugin_info_get_abi_version(GPluginPluginInfo *info);
gboolean gplugin_plugin_info_get_internal(GPluginPluginInfo *info);
gboolean gplugin_plugin_info_get_load_on_query(GPluginPluginInfo *info);
--- a/gplugin/tests/meson.build Tue Sep 22 00:30:00 2020 -0500
+++ b/gplugin/tests/meson.build Tue Sep 22 00:42:26 2020 -0500
@@ -34,6 +34,10 @@
dependencies : [gplugin_dep, GLIB, GOBJECT])
test('Find Plugins', e)
+e = executable('test-id-normalize', 'test-id-normalize.c',
+ dependencies : [gplugin_dep, GLIB, GOBJECT])
+test('ID Normalization', e)
+
e = executable('test-loader-registration', 'test-loader-registration.c',
dependencies : [gplugin_dep, GLIB, GOBJECT])
test('Loader Registration', e)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/gplugin/tests/test-id-normalize.c Tue Sep 22 00:42:26 2020 -0500
@@ -0,0 +1,111 @@
+/*
+ * Copyright (C) 2011-2020 Gary Kramlich <grim@reaperworld.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <glib.h>
+
+#include <gplugin.h>
+
+/******************************************************************************
+ * Tests
+ *****************************************************************************/
+static void
+test_id_normalize(const gchar *input, const gchar *expected)
+{
+ GPluginPluginInfo *info = NULL;
+ gchar *normalized = NULL;
+
+ info = gplugin_plugin_info_new(input, 0, NULL);
+ normalized = gplugin_info_get_id_normalized(info);
+ g_object_unref(G_OBJECT(info));
+
+ g_assert_cmpstr(normalized, ==, expected);
+
+ g_free(normalized);
+}
+
+static void
+test_id_normalize_null(void)
+{
+ test_id_normalize(NULL, NULL);
+}
+
+static void
+test_id_normalize_alpha_upper(void)
+{
+ test_id_normalize(G_CSET_A_2_Z, G_CSET_A_2_Z);
+}
+
+static void
+test_id_normalize_alpha_lower(void)
+{
+ test_id_normalize(G_CSET_a_2_z, G_CSET_a_2_z);
+}
+
+static void
+test_id_normalize_digits(void)
+{
+ test_id_normalize(G_CSET_DIGITS, G_CSET_DIGITS);
+}
+
+static void
+test_id_normalize_whitespace(void)
+{
+ test_id_normalize(" ", "-");
+ test_id_normalize("\r", "-");
+ test_id_normalize("\n", "-");
+ test_id_normalize("\r\n", "--");
+ test_id_normalize("\t", "-");
+
+ /* this is a zero width space */
+ test_id_normalize("\xe2\x80\x8b", "---");
+}
+
+static void
+test_id_normalize_symbols(void)
+{
+ test_id_normalize("a~!@#$%^&*()_+{}|:\"<>?z", "a---------------------z");
+ test_id_normalize("a`-=[]\\;',./z", "a-----------z");
+}
+
+static void
+test_id_normalize_emoji(void)
+{
+ /* 🔌 is 4 bytes which is why we need 4 -'s */
+ test_id_normalize("🔌", "----");
+ test_id_normalize("a🔌z", "a----z");
+}
+
+/******************************************************************************
+ * Main
+ *****************************************************************************/
+gint
+main(gint argc, gchar **argv)
+{
+ g_test_init(&argc, &argv, NULL);
+
+ gplugin_init(GPLUGIN_CORE_FLAGS_NONE);
+
+ g_test_add_func("/id-normalize/null", test_id_normalize_null);
+ g_test_add_func("/id-normalize/alpha/upper", test_id_normalize_alpha_upper);
+ g_test_add_func("/id-normalize/alpha/lower", test_id_normalize_alpha_lower);
+ g_test_add_func("/id-normalize/digits", test_id_normalize_digits);
+ g_test_add_func("/id-normalize/whitespace", test_id_normalize_whitespace);
+ g_test_add_func("/id-normalize/symbols", test_id_normalize_symbols);
+ g_test_add_func("/id-normalize/emoji", test_id_normalize_emoji);
+
+ return g_test_run();
+}