summaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2018-10-12 18:05:48 +0200
committerThomas Haller <thaller@redhat.com>2018-10-18 12:16:55 +0200
commitdfdbd1b385aa48a4c128283ae2b3234885808f5a (patch)
treee271c698a984244c8c42c78e3d001e66cb793577 /shared
parenta6add8175a150f9bf64ac0b27064dc0d736aea6a (diff)
shared/tests: add test for "shared/nm-utils"
"shared/nm-utils" is a loose collection of utility functions. There is a certain aim that they can be used independently. However, they also rely on each other. Add a test that we can build a minimal shared library with these tools, independent of libnm-core.
Diffstat (limited to 'shared')
-rw-r--r--shared/meson.build28
-rw-r--r--shared/nm-utils/nm-test-utils.h6
-rw-r--r--shared/nm-utils/tests/test-shared-general.c76
3 files changed, 110 insertions, 0 deletions
diff --git a/shared/meson.build b/shared/meson.build
index 9afc9a6d5..274bc736f 100644
--- a/shared/meson.build
+++ b/shared/meson.build
@@ -117,3 +117,31 @@ shared_dep = declare_dependency(
],
dependencies: glib_dep,
)
+
+###############################################################################
+
+test_shared_general = executable(
+ 'nm-utils/tests/test-shared-general',
+ [ 'nm-utils/tests/test-shared-general.c',
+ 'nm-utils/c-list-util.c',
+ 'nm-utils/nm-dedup-multi.c',
+ 'nm-utils/nm-enum-utils.c',
+ 'nm-utils/nm-hash-utils.c',
+ 'nm-utils/nm-io-utils.c',
+ 'nm-utils/nm-random-utils.c',
+ 'nm-utils/nm-secret-utils.c',
+ 'nm-utils/nm-shared-utils.c',
+ 'nm-utils/nm-time-utils.c',
+ ],
+ c_args: [
+ '-DNETWORKMANAGER_COMPILATION_TEST',
+ '-DNETWORKMANAGER_COMPILATION=(NM_NETWORKMANAGER_COMPILATION_GLIB|NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_PROG)',
+ ],
+ dependencies: shared_dep,
+ link_with: shared_c_siphash,
+)
+test(
+ 'shared/nm-utils/test-shared-general',
+ test_script,
+ args: test_args + [test_shared_general.full_path()]
+)
diff --git a/shared/nm-utils/nm-test-utils.h b/shared/nm-utils/nm-test-utils.h
index c81398621..124cdb45b 100644
--- a/shared/nm-utils/nm-test-utils.h
+++ b/shared/nm-utils/nm-test-utils.h
@@ -110,7 +110,9 @@
#include <string.h>
#include <errno.h>
+#ifndef NM_TEST_UTILS_NO_LIBNM
#include "nm-utils.h"
+#endif
/*****************************************************************************/
@@ -1089,6 +1091,8 @@ __define_nmtst_static(02, 1024)
__define_nmtst_static(03, 1024)
#undef __define_nmtst_static
+#if defined (__NM_UTILS_H__) || defined (NM_UTILS_H)
+
#define NMTST_UUID_INIT(uuid) \
gs_free char *_nmtst_hidden_##uuid = nm_utils_uuid_generate (); \
const char *const uuid = _nmtst_hidden_##uuid
@@ -1105,6 +1109,8 @@ nmtst_uuid_generate (void)
return u;
}
+#endif
+
#define NMTST_SWAP(x,y) \
G_STMT_START { \
char __nmtst_swap_temp[sizeof(x) == sizeof(y) ? (signed) sizeof(x) : -1]; \
diff --git a/shared/nm-utils/tests/test-shared-general.c b/shared/nm-utils/tests/test-shared-general.c
new file mode 100644
index 000000000..f186e423b
--- /dev/null
+++ b/shared/nm-utils/tests/test-shared-general.c
@@ -0,0 +1,76 @@
+/*
+ * 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.
+ */
+
+#define NM_TEST_UTILS_NO_LIBNM 1
+
+#include "nm-default.h"
+
+#include "nm-utils/nm-time-utils.h"
+#include "nm-utils/nm-random-utils.h"
+
+#include "nm-utils/nm-test-utils.h"
+
+/*****************************************************************************/
+
+static int _monotonic_timestamp_initialized;
+
+void
+_nm_utils_monotonic_timestamp_initialized (const struct timespec *tp,
+ gint64 offset_sec,
+ gboolean is_boottime)
+{
+ g_assert (!_monotonic_timestamp_initialized);
+ _monotonic_timestamp_initialized = 1;
+}
+
+/*****************************************************************************/
+
+static void
+test_monotonic_timestamp (void)
+{
+ g_assert (nm_utils_get_monotonic_timestamp_s () > 0);
+ g_assert (_monotonic_timestamp_initialized);
+}
+
+/*****************************************************************************/
+
+static void
+test_nmhash (void)
+{
+ int rnd;
+
+ nm_utils_random_bytes (&rnd, sizeof (rnd));
+
+ g_assert (nm_hash_val (555, 4) != 0);
+}
+
+/*****************************************************************************/
+
+NMTST_DEFINE ();
+
+int main (int argc, char **argv)
+{
+ nmtst_init (&argc, &argv, TRUE);
+
+ g_test_add_func ("/general/test_monotonic_timestamp", test_monotonic_timestamp);
+ g_test_add_func ("/general/test_nmhash", test_nmhash);
+
+ return g_test_run ();
+}
+