summary refs log tree commit diff
path: root/shared/nm-utils/tests
diff options
context:
space:
mode:
Diffstat (limited to 'shared/nm-utils/tests')
-rw-r--r--shared/nm-utils/tests/meson.build21
-rw-r--r--shared/nm-utils/tests/test-shared-general.c78
2 files changed, 83 insertions, 16 deletions
diff --git a/shared/nm-utils/tests/meson.build b/shared/nm-utils/tests/meson.build
new file mode 100644
index 00000000..1ee5efff
--- /dev/null
+++ b/shared/nm-utils/tests/meson.build
@@ -0,0 +1,21 @@
+test_unit = 'test-shared-general'
+
+c_flags = [
+  '-DNETWORKMANAGER_COMPILATION_TEST',
+  '-DNETWORKMANAGER_COMPILATION=(NM_NETWORKMANAGER_COMPILATION_GLIB|NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_PROG)',
+]
+
+exe = executable(
+  test_unit,
+  test_unit + '.c',
+  c_args: c_flags,
+  dependencies: libnm_utils_base_dep,
+  link_with: libnm_systemd_logging_stub,
+)
+
+test(
+  'shared/nm-utils/' + test_unit,
+  test_script,
+  args: test_args + [exe.full_path()],
+  timeout: default_test_timeout,
+)
diff --git a/shared/nm-utils/tests/test-shared-general.c b/shared/nm-utils/tests/test-shared-general.c
index 24affba7..34da19b3 100644
--- a/shared/nm-utils/tests/test-shared-general.c
+++ b/shared/nm-utils/tests/test-shared-general.c
@@ -1,20 +1,6 @@
+// SPDX-License-Identifier: LGPL-2.1+
 /*
- * 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, write to the
- * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301 USA.
- *
- * Copyright 2018 Red Hat, Inc.
+ * Copyright (C) 2018 Red Hat, Inc.
  */
 
 #define NM_TEST_UTILS_NO_LIBNM 1
@@ -24,12 +10,38 @@
 #include "nm-std-aux/unaligned.h"
 #include "nm-glib-aux/nm-random-utils.h"
 #include "nm-glib-aux/nm-time-utils.h"
+#include "nm-glib-aux/nm-ref-string.h"
 
 #include "nm-utils/nm-test-utils.h"
 
 /*****************************************************************************/
 
 static void
+test_gpid (void)
+{
+	const int *int_ptr;
+	GPid pid = 42;
+
+	/* We redefine G_PID_FORMAT, because it's only available since glib 2.53.5.
+	 *
+	 * Also, this is the format for GPid, which for glib is always a typedef
+	 * for "int". Add a check for that here.
+	 *
+	 * G_PID_FORMAT is not about pid_t, which might be a smaller int, and which we would
+	 * check with SIZEOF_PID_T. */
+	G_STATIC_ASSERT (sizeof (GPid) == sizeof (int));
+
+	g_assert_cmpstr (""G_PID_FORMAT, ==, "i");
+
+	/* check that it's really "int". We will get a compiler warning, if that's not
+	 * the case. */
+	int_ptr = &pid;
+	g_assert_cmpint (*int_ptr, ==, 42);
+}
+
+/*****************************************************************************/
+
+static void
 test_monotonic_timestamp (void)
 {
 	g_assert (nm_utils_get_monotonic_timestamp_s () > 0);
@@ -510,12 +522,45 @@ test_nm_utils_bin2hexstr (void)
 
 /*****************************************************************************/
 
+static void
+test_nm_ref_string (void)
+{
+	nm_auto_ref_string NMRefString *s1 = NULL;
+	NMRefString *s2;
+
+	s1 = nm_ref_string_new ("hallo");
+	g_assert (s1);
+	g_assert_cmpstr (s1->str, ==, "hallo");
+	g_assert_cmpint (s1->len, ==, strlen ("hallo"));
+
+	s2 = nm_ref_string_new ("hallo");
+	g_assert (s2 == s1);
+	nm_ref_string_unref (s2);
+
+	s2 = nm_ref_string_new (NULL);
+	g_assert (!s2);
+	nm_ref_string_unref (s2);
+
+#define STR_WITH_NUL "hallo\0test\0"
+	s2 = nm_ref_string_new_len (STR_WITH_NUL, NM_STRLEN (STR_WITH_NUL));
+	g_assert (s2);
+	g_assert_cmpstr (s2->str, ==, "hallo");
+	g_assert_cmpint (s2->len, ==, NM_STRLEN (STR_WITH_NUL));
+	g_assert_cmpint (s2->len, >, strlen (s2->str));
+	g_assert_cmpmem (s2->str, s2->len, STR_WITH_NUL, NM_STRLEN (STR_WITH_NUL));
+	g_assert (s2->str[s2->len] == '\0');
+	nm_ref_string_unref (s2);
+}
+
+/*****************************************************************************/
+
 NMTST_DEFINE ();
 
 int main (int argc, char **argv)
 {
 	nmtst_init (&argc, &argv, TRUE);
 
+	g_test_add_func ("/general/test_gpid", test_gpid);
 	g_test_add_func ("/general/test_monotonic_timestamp", test_monotonic_timestamp);
 	g_test_add_func ("/general/test_nmhash", test_nmhash);
 	g_test_add_func ("/general/test_nm_make_strv", test_make_strv);
@@ -526,6 +571,7 @@ int main (int argc, char **argv)
 	g_test_add_func ("/general/test_strv_cmp", test_strv_cmp);
 	g_test_add_func ("/general/test_strstrip_avoid_copy", test_strstrip_avoid_copy);
 	g_test_add_func ("/general/test_nm_utils_bin2hexstr", test_nm_utils_bin2hexstr);
+	g_test_add_func ("/general/test_nm_ref_string", test_nm_ref_string);
 
 	return g_test_run ();
 }