Commit dfdbd1b3 authored by Thomas Haller's avatar Thomas Haller

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.
parent a6add817
......@@ -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]
......
......@@ -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
......
......@@ -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()]
)
......@@ -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]; \
......
/*
* 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 ();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment