From dfdbd1b385aa48a4c128283ae2b3234885808f5a Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 12 Oct 2018 18:05:48 +0200 Subject: [PATCH] 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. --- .gitignore | 1 + Makefile.am | 33 +++++++++ shared/meson.build | 28 ++++++++ shared/nm-utils/nm-test-utils.h | 6 ++ shared/nm-utils/tests/test-shared-general.c | 76 +++++++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 shared/nm-utils/tests/test-shared-general.c diff --git a/.gitignore b/.gitignore index 494ff707ba..bb3f82b514 100644 --- a/.gitignore +++ b/.gitignore @@ -147,6 +147,7 @@ test-*.trs /examples/C/qt/change-ipv4-addresses /shared/nm-version-macros.h +/shared/nm-utils/tests/test-shared-general /introspection/org.freedesktop.NetworkManager*.[ch] diff --git a/Makefile.am b/Makefile.am index da4c229d3f..f99481ab7f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -204,6 +204,39 @@ DISTCLEANFILES += $(polkit_policy_DATA) ############################################################################### +check_programs += shared/nm-utils/tests/test-shared-general + +shared_nm_utils_tests_test_shared_general_CPPFLAGS = \ + $(dflt_cppflags) \ + -I$(srcdir)/shared \ + -DNETWORKMANAGER_COMPILATION_TEST \ + -DNETWORKMANAGER_COMPILATION='(NM_NETWORKMANAGER_COMPILATION_GLIB|NM_NETWORKMANAGER_COMPILATION_WITH_GLIB_I18N_PROG)' \ + $(CODE_COVERAGE_CFLAGS) \ + $(GLIB_CFLAGS) \ + $(LIBUDEV_CFLAGS) \ + $(SANITIZER_LIB_CFLAGS) \ + $(NULL) + +shared_nm_utils_tests_test_shared_general_SOURCES = \ + shared/nm-utils/c-list-util.c \ + shared/nm-utils/nm-dedup-multi.c \ + shared/nm-utils/nm-enum-utils.c \ + shared/nm-utils/nm-hash-utils.c \ + shared/nm-utils/nm-io-utils.c \ + shared/nm-utils/nm-random-utils.c \ + shared/nm-utils/nm-secret-utils.c \ + shared/nm-utils/nm-shared-utils.c \ + shared/nm-utils/nm-time-utils.c \ + shared/nm-utils/tests/test-shared-general.c \ + $(NULL) + +shared_nm_utils_tests_test_shared_general_LDADD = \ + $(GLIB_LIBS) \ + shared/libcsiphash.la \ + $(NULL) + +############################################################################### + noinst_LTLIBRARIES += \ introspection/libnmdbus.la diff --git a/shared/meson.build b/shared/meson.build index 9afc9a6d56..274bc736f9 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 c813986210..124cdb45bd 100644 --- a/shared/nm-utils/nm-test-utils.h +++ b/shared/nm-utils/nm-test-utils.h @@ -110,7 +110,9 @@ #include #include +#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 0000000000..f186e423b8 --- /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 (); +} +