platform: merge branch 'th/bgo706293_platform_tests'

https://bugzilla.gnome.org/show_bug.cgi?id=706293

Signed-off-by: Thomas Haller <thaller@redhat.com>
This commit is contained in:
Thomas Haller 2014-06-05 18:13:26 +02:00
commit 37aeee5f71
32 changed files with 958 additions and 491 deletions

View file

@ -756,7 +756,7 @@ AS_IF([test "$with_valgrind" == "yes"],
[AC_PATH_PROGS(with_valgrind, valgrind, no)])
# Add conditionals and substitutions
AM_CONDITIONAL(ENABLE_TESTS, test "$enable_tests" != "no")
AM_CONDITIONAL(RUN_ROOT_TESTS, test "$enable_tests" == "root")
AM_CONDITIONAL(REQUIRE_ROOT_TESTS, test "$enable_tests" == "root")
AS_IF([test "$with_valgrind" != "no"],
AC_SUBST(VALGRIND_RULES, 'TESTS_ENVIRONMENT = "$(top_srcdir)/tools/run-test-valgrind.sh" "$(LIBTOOL)" "$(with_valgrind)" "$(top_srcdir)/valgrind.suppressions"'),
AC_SUBST(VALGRIND_RULES, []))

View file

@ -4,7 +4,7 @@ EXTRA_DIST = \
nm-dbus-glib-types.h \
nm-glib-compat.h \
nm-gvaluearray-compat.h \
nm-test-helpers.h \
nm-test-utils.h \
nm-version.h.in \
nm-settings-flags.h

View file

@ -91,4 +91,23 @@ g_type_ensure (GType type)
#endif
/* g_test_initialized() is only available since glib 2.36. */
#if !GLIB_CHECK_VERSION (2, 36, 0)
#define g_test_initialized() (g_test_config_vars->test_initialized)
#endif
/* g_test_skip() is only available since glib 2.38. Add a compatibility wrapper. */
inline static void
__nmtst_g_test_skip (const gchar *msg)
{
#if GLIB_CHECK_VERSION (2, 38, 0)
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
g_test_skip (msg);
G_GNUC_END_IGNORE_DEPRECATIONS
#else
g_debug ("%s", msg);
#endif
}
#define g_test_skip __nmtst_g_test_skip
#endif /* NM_GLIB_COMPAT_H */

View file

@ -1,49 +0,0 @@
/* NetworkManager -- Network link manager
*
* Dan Williams <dcbw@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* (C) Copyright 2008 Red Hat, Inc.
*/
#ifndef NM_TEST_HELPERS_H
#define NM_TEST_HELPERS_H
#include <stdio.h>
#include <unistd.h>
#include <glib.h>
static void
FAIL(const char *test_name, const char *fmt, ...)
{
va_list args;
char buf[500];
g_snprintf (buf, 500, "FAIL: (%s) %s\n", test_name, fmt);
va_start (args, fmt);
vfprintf (stderr, buf, args);
va_end (args);
_exit (1);
}
#define ASSERT(x, test_name, fmt, ...) \
if (!(x)) { \
FAIL (test_name, fmt, ## __VA_ARGS__); \
}
#endif /* NM_TEST_HELPERS_H */

680
include/nm-test-utils.h Normal file
View file

@ -0,0 +1,680 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
* 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.
*
* (C) Copyright 2014 Red Hat, Inc.
*/
#ifndef __NM_TEST_UTILS_H__
#define __NM_TEST_UTILS_H__
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <glib.h>
#include <glib-object.h>
#include <string.h>
#include <errno.h>
#include "nm-utils.h"
#include "nm-glib-compat.h"
struct __nmtst_internal
{
GRand *rand0;
guint32 rand_seed;
GRand *rand;
gboolean is_debug;
gboolean assert_logging;
gboolean no_expect_message;
char *sudo_cmd;
char **orig_argv;
};
extern struct __nmtst_internal __nmtst_internal;
#define NMTST_DEFINE() \
struct __nmtst_internal __nmtst_internal = { 0 };
inline static gboolean
nmtst_initialized (void)
{
return !!__nmtst_internal.rand0;
}
#define __NMTST_LOG(cmd, fmt, ...) \
G_STMT_START { \
g_assert (nmtst_initialized ()); \
if (!__nmtst_internal.assert_logging || __nmtst_internal.no_expect_message) { \
cmd (fmt, __VA_ARGS__); \
} else { \
printf (fmt "\n", __VA_ARGS__); \
} \
} G_STMT_END
/* split the string inplace at specific delimiters, allowing escaping with '\\'.
* Returns a zero terminated array of pointers into @str.
*
* The caller must g_free() the returned argv array.
**/
inline static char **
nmtst_str_split (char *str, const char *delimiters)
{
const char *d;
GArray *result = g_array_sized_new (TRUE, FALSE, sizeof (char *), 3);
g_assert (str);
g_assert (delimiters && !strchr (delimiters, '\\'));
while (*str) {
gsize i = 0, j = 0;
while (TRUE) {
char c = str[i];
if (c == '\0') {
str[j++] = 0;
break;
} else if (c == '\\') {
str[j++] = str[++i];
if (!str[i])
break;
} else {
for (d = delimiters; *d; d++) {
if (c == *d) {
str[j++] = 0;
i++;
goto BREAK_INNER_LOOPS;
}
}
str[j++] = c;
}
i++;
}
BREAK_INNER_LOOPS:
g_array_append_val (result, str);
str = &str[i];
}
return (char **) g_array_free (result, FALSE);
}
/* free instances allocated by nmtst (especially nmtst_init()) on shutdown
* to release memory. After nmtst_free(), the test is uninitialized again. */
inline static void
nmtst_free (void)
{
if (!nmtst_initialized ())
return;
g_rand_free (__nmtst_internal.rand0);
if (__nmtst_internal.rand)
g_rand_free (__nmtst_internal.rand);
g_free (__nmtst_internal.sudo_cmd);
g_strfreev (__nmtst_internal.orig_argv);
memset (&__nmtst_internal, 0, sizeof (__nmtst_internal));
}
inline static void
__nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_level, const char *log_domains)
{
static gsize atexit_registered = 0;
GError *error = NULL;
const char *nmtst_debug;
gboolean is_debug = FALSE;
char *c_log_level = NULL, *c_log_domains = NULL;
char *sudo_cmd = NULL;
GArray *debug_messages = g_array_new (TRUE, FALSE, sizeof (char *));
int i;
gboolean no_expect_message = FALSE;
g_assert (!nmtst_initialized ());
g_assert (!((!!argc) ^ (!!argv)));
g_assert (!argc || (g_strv_length (*argv) == *argc));
g_assert (!assert_logging || (!log_level && !log_domains));
if (argc)
__nmtst_internal.orig_argv = g_strdupv (*argv);
if (argc && !g_test_initialized ()) {
/* g_test_init() is a variadic function, so we cannot pass it
* (variadic) arguments. If you need to pass additional parameters,
* call nmtst_init() with argc==NULL and call g_test_init() yourself. */
/* g_test_init() sets g_log_set_always_fatal() for G_LOG_LEVEL_WARNING
* and G_LOG_LEVEL_CRITICAL. So, beware that the test will fail if you
* have any WARN or ERR log messages -- unless you g_test_expect_message(). */
g_test_init (argc, argv, NULL);
}
__nmtst_internal.assert_logging = !!assert_logging;
#if !GLIB_CHECK_VERSION (2, 35, 0)
g_type_init ();
#endif
is_debug = g_test_verbose ();
nmtst_debug = g_getenv ("NMTST_DEBUG");
if (nmtst_debug) {
char **d_argv, **i_argv, *nmtst_debug_copy;
/* By setting then NMTST_DEBUG variable, @is_debug is set automatically.
* This can be reverted with no-debug (on command line or environment variable). */
is_debug = TRUE;
nmtst_debug_copy = g_strdup (nmtst_debug);
d_argv = nmtst_str_split (nmtst_debug_copy, ",; \t\r\n");
for (i_argv = d_argv; *i_argv; i_argv++) {
const char *debug = *i_argv;
if (!g_ascii_strcasecmp (debug, "debug"))
is_debug = TRUE;
else if (!g_ascii_strcasecmp (debug, "no-debug")) {
/* when specifying the NMTST_DEBUG variable, we set is_debug to true. Use this flag to disable this
* (e.g. for only setting the log-level, but not is_debug). */
is_debug = FALSE;
} else if (!g_ascii_strncasecmp (debug, "log-level=", strlen ("log-level="))) {
g_free (c_log_level);
log_level = c_log_level = g_strdup (&debug[strlen ("log-level=")]);
} else if (!g_ascii_strncasecmp (debug, "log-domains=", strlen ("log-domains="))) {
g_free (c_log_domains);
log_domains = c_log_domains = g_strdup (&debug[strlen ("log-domains=")]);
} else if (!g_ascii_strncasecmp (debug, "sudo-cmd=", strlen ("sudo-cmd="))) {
g_free (sudo_cmd);
sudo_cmd = g_strdup (&debug[strlen ("sudo-cmd=")]);
} else if (!g_ascii_strcasecmp (debug, "no-expect-message")) {
no_expect_message = TRUE;
} else {
char *msg = g_strdup_printf (">>> nmtst: ignore unrecognized NMTST_DEBUG option \"%s\"", debug);
g_array_append_val (debug_messages, msg);
}
}
g_free (d_argv);
g_free (nmtst_debug_copy);
}
if (argv && *argv) {
char **a = *argv;
for (; *a; a++) {
if (!g_ascii_strcasecmp (*a, "--debug"))
is_debug = TRUE;
else if (!g_ascii_strcasecmp (*a, "--no-debug"))
is_debug = FALSE;
}
}
__nmtst_internal.is_debug = is_debug;
__nmtst_internal.rand0 = g_rand_new_with_seed (0);
__nmtst_internal.sudo_cmd = sudo_cmd;
__nmtst_internal.no_expect_message = no_expect_message;
if (!log_level && log_domains) {
/* if the log level is not specified (but the domain is), we assume
* the caller wants to set it depending on is_debug */
log_level = is_debug ? "DEBUG" : "WARN";
}
if (!__nmtst_internal.assert_logging) {
gboolean success = TRUE;
#ifdef NM_LOGGING_H
success = nm_logging_setup (log_level, log_domains, NULL, NULL);
#endif
g_assert (success);
} else if (__nmtst_internal.no_expect_message) {
/* We have a test that would be assert_logging, but the user specified no_expect_message.
* This transforms g_test_expect_message() into a NOP, but we also have to relax
* g_log_set_always_fatal(), which was set by g_test_init(). */
g_log_set_always_fatal (G_LOG_FATAL_MASK);
} else {
#if GLIB_CHECK_VERSION(2,34,0)
/* We were called not to set logging levels. This means, that the user
* expects to assert against (all) messages. Any uncought message is fatal. */
g_log_set_always_fatal (G_LOG_LEVEL_MASK);
#else
/* g_test_expect_message() is a NOP, so allow any messages */
g_log_set_always_fatal (G_LOG_FATAL_MASK);
#endif
}
if ((!__nmtst_internal.assert_logging || (__nmtst_internal.assert_logging && __nmtst_internal.no_expect_message)) &&
(is_debug || (log_level && !g_ascii_strcasecmp (log_level, "DEBUG"))) &&
!g_getenv ("G_MESSAGES_DEBUG"))
{
/* if we are @is_debug or @log_level=="DEBUG" and
* G_MESSAGES_DEBUG is unset, we set G_MESSAGES_DEBUG=all.
* To disable this default behaviour, set G_MESSAGES_DEBUG='' */
/* Note that g_setenv is not thread safe, but you should anyway call
* nmtst_init() at the very start. */
g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
}
if (!nm_utils_init (&error))
g_error ("failed to initialize libnm-util: %s", error->message);
g_assert (!error);
/* Delay messages until we setup logging. */
for (i = 0; i < debug_messages->len; i++)
__NMTST_LOG (g_message, "%s", g_array_index (debug_messages, const char *, i));
g_strfreev ((char **) g_array_free (debug_messages, FALSE));
g_free (c_log_level);
g_free (c_log_domains);
if (g_once_init_enter (&atexit_registered)) {
atexit (nmtst_free);
g_once_init_leave (&atexit_registered, 1);
}
}
#ifdef NM_LOGGING_H
inline static void
nmtst_init_with_logging (int *argc, char ***argv, const char *log_level, const char *log_domains)
{
__nmtst_init (argc, argv, FALSE, log_level, log_domains);
}
inline static void
nmtst_init_assert_logging (int *argc, char ***argv)
{
__nmtst_init (argc, argv, TRUE, NULL, NULL);
}
#else
inline static void
nmtst_init (int *argc, char ***argv, gboolean assert_logging)
{
__nmtst_init (argc, argv, assert_logging, NULL, NULL);
}
#endif
inline static gboolean
nmtst_is_debug (void)
{
g_assert (nmtst_initialized ());
return __nmtst_internal.is_debug;
}
#if GLIB_CHECK_VERSION(2,34,0)
#undef g_test_expect_message
#define g_test_expect_message(...) \
G_STMT_START { \
g_assert (nmtst_initialized ()); \
if (__nmtst_internal.assert_logging && __nmtst_internal.no_expect_message) { \
g_debug ("nmtst: swallow g_test_expect_message %s", G_STRINGIFY ((__VA_ARGS__))); \
} else { \
G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
g_test_expect_message (__VA_ARGS__); \
G_GNUC_END_IGNORE_DEPRECATIONS \
} \
} G_STMT_END
#endif
inline static GRand *
nmtst_get_rand0 ()
{
g_assert (nmtst_initialized ());
return __nmtst_internal.rand0;
}
inline static GRand *
nmtst_get_rand ()
{
g_assert (nmtst_initialized ());
if (G_UNLIKELY (!__nmtst_internal.rand)) {
guint32 seed;
const char *str;
if ((str = g_getenv ("NMTST_SEED_RAND"))) {
gchar *s;
gint64 i;
i = g_ascii_strtoll (str, &s, 0);
g_assert (s[0] == '\0' && i >= 0 && i < G_MAXINT32);
seed = i;
__nmtst_internal.rand = g_rand_new_with_seed (seed);
} else {
__nmtst_internal.rand = g_rand_new ();
seed = g_rand_int (__nmtst_internal.rand);
g_rand_set_seed (__nmtst_internal.rand, seed);
}
__nmtst_internal.rand_seed = seed;
__NMTST_LOG (g_message, ">> initialize nmtst_get_rand() with seed=%u", seed);
}
return __nmtst_internal.rand;
}
inline static const char *
nmtst_get_sudo_cmd (void)
{
g_assert (nmtst_initialized ());
return __nmtst_internal.sudo_cmd;
}
inline static void
nmtst_reexec_sudo (void)
{
char *str;
char **argv;
int i;
int errsv;
g_assert (nmtst_initialized ());
g_assert (__nmtst_internal.orig_argv);
if (!__nmtst_internal.sudo_cmd)
return;
str = g_strjoinv (" ", __nmtst_internal.orig_argv);
__NMTST_LOG (g_message, ">> exec %s %s", __nmtst_internal.sudo_cmd, str);
argv = g_new0 (char *, 1 + g_strv_length (__nmtst_internal.orig_argv) + 1);
argv[0] = __nmtst_internal.sudo_cmd;
for (i = 0; __nmtst_internal.orig_argv[i]; i++)
argv[i+1] = __nmtst_internal.orig_argv[i];
execvp (__nmtst_internal.sudo_cmd, argv);
errsv = errno;
g_error (">> exec %s failed: %d - %s", __nmtst_internal.sudo_cmd, errsv, strerror (errsv));
}
#define __define_nmtst_static(NUM,SIZE) \
inline static const char * \
nmtst_static_##SIZE##_##NUM (const char *str) \
{ \
gsize l; \
static char buf[SIZE]; \
\
if (!str) \
return NULL; \
l = g_strlcpy (buf, str, sizeof (buf)); \
g_assert (l < sizeof (buf)); \
return buf; \
}
__define_nmtst_static(01, 1024)
__define_nmtst_static(02, 1024)
__define_nmtst_static(03, 1024)
#undef __define_nmtst_static
#define NMTST_SWAP(x,y) \
G_STMT_START { \
char __nmtst_swap_temp[sizeof(x) == sizeof(y) ? (signed) sizeof(x) : -1]; \
memcpy(__nmtst_swap_temp, &y, sizeof(x)); \
memcpy(&y, &x, sizeof(x)); \
memcpy(&x, __nmtst_swap_temp, sizeof(x)); \
} G_STMT_END
inline static guint32
nmtst_inet4_from_string (const char *str)
{
guint32 addr;
int success;
if (!str)
return 0;
success = inet_pton (AF_INET, str, &addr);
g_assert (success == 1);
return addr;
}
inline static const struct in6_addr *
nmtst_inet6_from_string (const char *str)
{
static struct in6_addr addr;
int success;
if (!str)
addr = in6addr_any;
else {
success = inet_pton (AF_INET6, str, &addr);
g_assert (success == 1);
}
return &addr;
}
inline static void
FAIL(const char *test_name, const char *fmt, ...)
{
va_list args;
char buf[500];
g_snprintf (buf, 500, "FAIL: (%s) %s\n", test_name, fmt);
va_start (args, fmt);
vfprintf (stderr, buf, args);
va_end (args);
_exit (1);
}
#define ASSERT(x, test_name, fmt, ...) \
if (!(x)) { \
FAIL (test_name, fmt, ## __VA_ARGS__); \
}
#define nmtst_spawn_sync(working_directory, standard_out, standard_err, assert_exit_status, ...) \
__nmtst_spawn_sync (working_directory, standard_out, standard_err, assert_exit_status, ##__VA_ARGS__, NULL)
inline static gint
__nmtst_spawn_sync (const char *working_directory, char **standard_out, char **standard_err, int assert_exit_status, ...) G_GNUC_NULL_TERMINATED;
inline static gint
__nmtst_spawn_sync (const char *working_directory, char **standard_out, char **standard_err, int assert_exit_status, ...)
{
gint exit_status = 0;
GError *error = NULL;
char *arg;
va_list va_args;
GPtrArray *argv = g_ptr_array_new ();
gboolean success;
va_start (va_args, assert_exit_status);
while ((arg = va_arg (va_args, char *)))
g_ptr_array_add (argv, arg);
va_end (va_args);
g_assert (argv->len >= 1);
g_ptr_array_add (argv, NULL);
success = g_spawn_sync (working_directory,
(char**) argv->pdata,
NULL,
0 /*G_SPAWN_DEFAULT*/,
NULL,
NULL,
standard_out,
standard_err,
&exit_status,
&error);
if (!success)
g_error ("nmtst_spawn_sync(%s): %s", ((char **) argv->pdata)[0], error->message);
g_assert (!error);
g_assert (!standard_out || *standard_out);
g_assert (!standard_err || *standard_err);
if (assert_exit_status != -1) {
/* exit status is a guint8 on success. Set @assert_exit_status to -1
* not to check for the exit status. */
g_assert (WIFEXITED (exit_status));
g_assert_cmpint (WEXITSTATUS (exit_status), ==, assert_exit_status);
}
g_ptr_array_free (argv, TRUE);
return exit_status;
}
/*******************************************************************************/
#ifdef NM_PLATFORM_H
inline static NMPlatformIP6Address *
nmtst_platform_ip6_address (const char *address, const char *peer_address, guint plen)
{
static NMPlatformIP6Address addr;
memset (&addr, 0, sizeof (addr));
addr.address = *nmtst_inet6_from_string (address);
addr.peer_address = *nmtst_inet6_from_string (peer_address);
addr.plen = plen;
return &addr;
}
inline static NMPlatformIP6Address *
nmtst_platform_ip6_address_full (const char *address, const char *peer_address, guint plen,
int ifindex, NMPlatformSource source, guint32 timestamp,
guint32 lifetime, guint32 preferred, guint flags)
{
NMPlatformIP6Address *addr = nmtst_platform_ip6_address (address, peer_address, plen);
addr->ifindex = ifindex;
addr->source = source;
addr->timestamp = timestamp;
addr->lifetime = lifetime;
addr->preferred = preferred;
addr->flags = flags;
return addr;
}
inline static NMPlatformIP6Route *
nmtst_platform_ip6_route (const char *network, guint plen, const char *gateway)
{
static NMPlatformIP6Route route;
memset (&route, 0, sizeof (route));
route.network = *nmtst_inet6_from_string (network);
route.plen = plen;
route.gateway = *nmtst_inet6_from_string (gateway);
return &route;
}
inline static NMPlatformIP6Route *
nmtst_platform_ip6_route_full (const char *network, guint plen, const char *gateway,
int ifindex, NMPlatformSource source,
guint metric, guint mss)
{
NMPlatformIP6Route *route = nmtst_platform_ip6_route (network, plen, gateway);
route->ifindex = ifindex;
route->source = source;
route->metric = metric;
route->mss = mss;
return route;
}
inline static void
nmtst_platform_ip4_routes_equal (const NMPlatformIP4Route *a, const NMPlatformIP4Route *b, gsize len)
{
gsize i;
g_assert (a);
g_assert (b);
for (i = 0; i < len; i++) {
if (nm_platform_ip4_route_cmp (&a[i], &b[i]) != 0) {
g_error ("Error comparing IPv4 route[%lu]: %s vs %s", (long unsigned) i,
nmtst_static_1024_01 (nm_platform_ip4_route_to_string (&a[i])),
nmtst_static_1024_02 (nm_platform_ip4_route_to_string (&b[i])));
g_assert_not_reached ();
}
/* also check with memcmp, though this might fail for valid programs (due to field alignment) */
g_assert_cmpint (memcmp (&a[i], &b[i], sizeof (a[i])), ==, 0);
}
}
inline static void
nmtst_platform_ip6_routes_equal (const NMPlatformIP6Route *a, const NMPlatformIP6Route *b, gsize len)
{
gsize i;
g_assert (a);
g_assert (b);
for (i = 0; i < len; i++) {
if (nm_platform_ip6_route_cmp (&a[i], &b[i]) != 0) {
g_error ("Error comparing IPv6 route[%lu]: %s vs %s", (long unsigned) i,
nmtst_static_1024_01 (nm_platform_ip6_route_to_string (&a[i])),
nmtst_static_1024_02 (nm_platform_ip6_route_to_string (&b[i])));
g_assert_not_reached ();
}
/* also check with memcmp, though this might fail for valid programs (due to field alignment) */
g_assert_cmpint (memcmp (&a[i], &b[i], sizeof (a[i])), ==, 0);
}
}
#endif
#ifdef NM_IP4_CONFIG_H
inline static NMIP4Config *
nmtst_ip4_config_clone (NMIP4Config *config)
{
NMIP4Config *copy = nm_ip4_config_new ();
g_assert (copy);
g_assert (config);
nm_ip4_config_replace (copy, config, NULL);
return copy;
}
#endif
#ifdef NM_IP6_CONFIG_H
inline static NMIP6Config *
nmtst_ip6_config_clone (NMIP6Config *config)
{
NMIP6Config *copy = nm_ip6_config_new ();
g_assert (copy);
g_assert (config);
nm_ip6_config_replace (copy, config, NULL);
return copy;
}
#endif
#endif /* __NM_TEST_UTILS_H__ */

View file

@ -59,8 +59,7 @@ libnm_util_la_private_headers = \
nm-param-spec-specialized.h \
nm-util-private.h \
nm-utils-private.h \
nm-setting-private.h \
nm-test-utils.h
nm-setting-private.h
libnm_util_la_csources = \
crypto.c \

View file

@ -1,261 +0,0 @@
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/*
* 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.
*
* (C) Copyright 2014 Red Hat, Inc.
*/
#ifndef __NM_TEST_UTILS_H__
#define __NM_TEST_UTILS_H__
#include <arpa/inet.h>
#include <glib.h>
struct __nmtst_internal
{
GRand *rand0;
guint32 rand_seed;
GRand *rand;
};
extern struct __nmtst_internal __nmtst_internal;
#define NMTST_DEFINE() \
struct __nmtst_internal __nmtst_internal = { 0 };
inline void nmtst_init (int *argc, char ***argv);
inline void
nmtst_init (int *argc, char ***argv)
{
g_assert (!__nmtst_internal.rand0);
g_assert (!((!!argc) ^ (!!argv)));
if (argc) {
/* g_test_init() is a variadic function, so we cannot pass it
* (variadic) arguments. If you need to pass additional parameters,
* call nmtst_init() with argc==NULL and call g_test_init() yourself. */
g_test_init (argc, argv, NULL);
}
g_type_init ();
__nmtst_internal.rand0 = g_rand_new_with_seed (0);
}
inline GRand *nmtst_get_rand0 (void);
inline GRand *
nmtst_get_rand0 ()
{
g_assert (__nmtst_internal.rand0);
return __nmtst_internal.rand0;
}
inline GRand *nmtst_get_rand (void);
inline GRand *
nmtst_get_rand ()
{
if (G_UNLIKELY (!__nmtst_internal.rand)) {
guint32 seed;
const char *str;
if ((str = g_getenv ("NMTST_SEED_RAND"))) {
gchar *s;
gint64 i;
i = g_ascii_strtoll (str, &s, 0);
g_assert (s[0] == '\0' && i >= 0 && i < G_MAXINT32);
seed = i;
__nmtst_internal.rand = g_rand_new_with_seed (seed);
} else {
__nmtst_internal.rand = g_rand_new ();
seed = g_rand_int (__nmtst_internal.rand);
g_rand_set_seed (__nmtst_internal.rand, seed);
}
__nmtst_internal.rand_seed = seed;
g_message (">> initialize nmtst_get_rand() with seed=%u", seed);
}
return __nmtst_internal.rand;
}
#define NMTST_SWAP(x,y) \
G_STMT_START { \
char __nmtst_swap_temp[sizeof(x) == sizeof(y) ? (signed) sizeof(x) : -1]; \
memcpy(__nmtst_swap_temp, &y, sizeof(x)); \
memcpy(&y, &x, sizeof(x)); \
memcpy(&x, __nmtst_swap_temp, sizeof(x)); \
} G_STMT_END
inline guint32 nmtst_inet4_from_string (const char *str);
inline guint32
nmtst_inet4_from_string (const char *str)
{
guint32 addr;
int success;
if (!str)
return 0;
success = inet_pton (AF_INET, str, &addr);
g_assert (success == 1);
return addr;
}
inline struct in6_addr *nmtst_inet6_from_string (const char *str);
inline struct in6_addr *
nmtst_inet6_from_string (const char *str)
{
static struct in6_addr addr;
int success;
if (!str)
addr = in6addr_any;
else {
success = inet_pton (AF_INET6, str, &addr);
g_assert (success == 1);
}
return &addr;
}
#ifdef NM_PLATFORM_H
inline NMPlatformIP6Address *nmtst_platform_ip6_address (const char *address, const char *peer_address, guint plen);
inline NMPlatformIP6Address *
nmtst_platform_ip6_address (const char *address, const char *peer_address, guint plen)
{
static NMPlatformIP6Address addr;
memset (&addr, 0, sizeof (addr));
addr.address = *nmtst_inet6_from_string (address);
addr.peer_address = *nmtst_inet6_from_string (peer_address);
addr.plen = plen;
return &addr;
}
inline NMPlatformIP6Address *
nmtst_platform_ip6_address_full (const char *address, const char *peer_address, guint plen,
int ifindex, NMPlatformSource source, guint32 timestamp,
guint32 lifetime, guint32 preferred, guint flags);
inline NMPlatformIP6Address *
nmtst_platform_ip6_address_full (const char *address, const char *peer_address, guint plen,
int ifindex, NMPlatformSource source, guint32 timestamp,
guint32 lifetime, guint32 preferred, guint flags)
{
NMPlatformIP6Address *addr = nmtst_platform_ip6_address (address, peer_address, plen);
addr->ifindex = ifindex;
addr->source = source;
addr->timestamp = timestamp;
addr->lifetime = lifetime;
addr->preferred = preferred;
addr->flags = flags;
return addr;
}
inline NMPlatformIP6Route * nmtst_platform_ip6_route (const char *network, guint plen, const char *gateway);
inline NMPlatformIP6Route *
nmtst_platform_ip6_route (const char *network, guint plen, const char *gateway)
{
static NMPlatformIP6Route route;
memset (&route, 0, sizeof (route));
route.network = *nmtst_inet6_from_string (network);
route.plen = plen;
route.gateway = *nmtst_inet6_from_string (gateway);
return &route;
}
inline NMPlatformIP6Route *
nmtst_platform_ip6_route_full (const char *network, guint plen, const char *gateway,
int ifindex, NMPlatformSource source,
guint metric, guint mss);
inline NMPlatformIP6Route *
nmtst_platform_ip6_route_full (const char *network, guint plen, const char *gateway,
int ifindex, NMPlatformSource source,
guint metric, guint mss)
{
NMPlatformIP6Route *route = nmtst_platform_ip6_route (network, plen, gateway);
route->ifindex = ifindex;
route->source = source;
route->metric = metric;
route->mss = mss;
return route;
}
#endif
#ifdef NM_IP4_CONFIG_H
inline NMIP4Config *nmtst_ip4_config_clone (NMIP4Config *config);
inline NMIP4Config *
nmtst_ip4_config_clone (NMIP4Config *config)
{
NMIP4Config *copy = nm_ip4_config_new ();
g_assert (copy);
g_assert (config);
nm_ip4_config_replace (copy, config, NULL);
return copy;
}
#endif
#ifdef NM_IP6_CONFIG_H
inline NMIP6Config *nmtst_ip6_config_clone (NMIP6Config *config);
inline NMIP6Config *
nmtst_ip6_config_clone (NMIP6Config *config)
{
NMIP6Config *copy = nm_ip6_config_new ();
g_assert (copy);
g_assert (config);
nm_ip6_config_replace (copy, config, NULL);
return copy;
}
#endif
#endif /* __NM_TEST_UTILS_H__ */

View file

@ -28,10 +28,11 @@
#include <stdio.h>
#include <string.h>
#include "nm-test-helpers.h"
#include "crypto.h"
#include "nm-utils.h"
#include "nm-test-utils.h"
#if 0
static const char *pem_rsa_key_begin = "-----BEGIN RSA PRIVATE KEY-----";
static const char *pem_rsa_key_end = "-----END RSA PRIVATE KEY-----";

View file

@ -25,7 +25,6 @@
#include <netinet/ether.h>
#include <linux/if_infiniband.h>
#include "nm-test-helpers.h"
#include <nm-utils.h>
#include "nm-setting-private.h"
@ -44,7 +43,8 @@
#include "nm-utils.h"
#include "nm-util-private.h"
#include "nm-dbus-glib-types.h"
#include "nm-glib-compat.h"
#include "nm-test-utils.h"
static void
vpn_check_func (const char *key, const char *value, gpointer user_data)
@ -2484,17 +2484,13 @@ test_setting_old_uuid (void)
g_assert (success == TRUE);
}
NMTST_DEFINE ();
int main (int argc, char **argv)
{
GError *error = NULL;
char *base;
#if !GLIB_CHECK_VERSION (2, 35, 0)
g_type_init ();
#endif
if (!nm_utils_init (&error))
FAIL ("nm-utils-init", "failed to initialize libnm-util: %s", error->message);
nmtst_init (&argc, &argv, TRUE);
/* The tests */
test_setting_vpn_items ();

View file

@ -22,7 +22,6 @@
#include <glib.h>
#include <string.h>
#include "nm-test-helpers.h"
#include <nm-utils.h>
#include "nm-setting-connection.h"
@ -37,6 +36,7 @@
#include "nm-setting-pppoe.h"
#include "nm-setting-vpn.h"
#include "nm-test-utils.h"
#define TEST_NEED_SECRETS_EAP_TLS_CA_CERT TEST_CERT_DIR "/test_ca_cert.pem"
#define TEST_NEED_SECRETS_EAP_TLS_CLIENT_CERT TEST_CERT_DIR "/test_key_and_cert.pem"

View file

@ -22,12 +22,13 @@
#include <glib.h>
#include <string.h>
#include "nm-test-helpers.h"
#include <nm-utils.h>
#include "nm-setting-connection.h"
#include "nm-setting-8021x.h"
#include "nm-test-utils.h"
static void
compare_blob_data (const char *test,
const char *key_path,

View file

@ -22,7 +22,6 @@
#include <glib.h>
#include <string.h>
#include "nm-test-helpers.h"
#include <nm-utils.h>
#include "nm-setting-8021x.h"
@ -39,6 +38,7 @@
#include "nm-setting-wireless.h"
#include "nm-setting-wireless-security.h"
#include "nm-test-utils.h"
static void
test_defaults (GType type, const char *name)

View file

@ -22,6 +22,10 @@
#ifndef NM_LOGGING_H
#define NM_LOGGING_H
#ifdef __NM_TEST_UTILS_H__
#error nm-test-utils.h must be included as last header
#endif
#include <glib.h>
#include <glib-object.h>

View file

@ -36,8 +36,6 @@ typedef struct {
GArray *ip6_addresses;
GArray *ip4_routes;
GArray *ip6_routes;
GSList *link_added_ids;
} NMFakePlatformPrivate;
typedef struct {
@ -163,25 +161,14 @@ link_get_all (NMPlatform *platform)
return links;
}
typedef struct {
NMPlatform *platform;
int ifindex;
guint id;
} LinkAddedInfo;
static gboolean
link_added_emit (gpointer user_data)
_nm_platform_link_get (NMPlatform *platform, int ifindex, NMPlatformLink *link)
{
LinkAddedInfo *info = user_data;
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (info->platform);
NMFakePlatformLink *device;
NMFakePlatformLink *device = link_get (platform, ifindex);
priv->link_added_ids = g_slist_remove (priv->link_added_ids, GUINT_TO_POINTER (info->id));
device = link_get (info->platform, info->ifindex);
g_assert (device);
g_signal_emit_by_name (info->platform, NM_PLATFORM_SIGNAL_LINK_CHANGED, info->ifindex, &device->link, NM_PLATFORM_SIGNAL_ADDED, NM_PLATFORM_REASON_INTERNAL);
return FALSE;
if (device)
*link = device->link;
return !!device;
}
static gboolean
@ -189,23 +176,13 @@ link_add (NMPlatform *platform, const char *name, NMLinkType type, const void *a
{
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (platform);
NMFakePlatformLink device;
LinkAddedInfo *info;
link_init (&device, priv->links->len, type, name);
g_array_append_val (priv->links, device);
if (device.link.ifindex) {
/* Platform requires LINK_ADDED signal emission from an idle handler */
info = g_new0 (LinkAddedInfo, 1);
info->platform = platform;
info->ifindex = device.link.ifindex;
info->id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
link_added_emit,
info,
g_free);
priv->link_added_ids = g_slist_prepend (priv->link_added_ids, GUINT_TO_POINTER (info->id));
}
if (device.link.ifindex)
g_signal_emit_by_name (platform, NM_PLATFORM_SIGNAL_LINK_CHANGED, device.link.ifindex, &device, NM_PLATFORM_SIGNAL_ADDED, NM_PLATFORM_REASON_INTERNAL);
return TRUE;
}
@ -850,6 +827,7 @@ ip4_address_add (NMPlatform *platform, int ifindex,
int i;
memset (&address, 0, sizeof (address));
address.source = NM_PLATFORM_SOURCE_KERNEL;
address.ifindex = ifindex;
address.address = addr;
address.peer_address = peer_addr;
@ -891,6 +869,7 @@ ip6_address_add (NMPlatform *platform, int ifindex,
int i;
memset (&address, 0, sizeof (address));
address.source = NM_PLATFORM_SOURCE_KERNEL;
address.ifindex = ifindex;
address.address = addr;
address.peer_address = peer_addr;
@ -1068,6 +1047,7 @@ ip4_route_add (NMPlatform *platform, int ifindex, in_addr_t network, int plen,
guint i;
memset (&route, 0, sizeof (route));
route.source = NM_PLATFORM_SOURCE_KERNEL;
route.ifindex = ifindex;
route.network = network;
route.plen = plen;
@ -1105,6 +1085,7 @@ ip6_route_add (NMPlatform *platform, int ifindex, struct in6_addr network, int p
guint i;
memset (&route, 0, sizeof (route));
route.source = NM_PLATFORM_SOURCE_KERNEL;
route.ifindex = ifindex;
route.network = network;
route.plen = plen;
@ -1250,11 +1231,6 @@ nm_fake_platform_finalize (GObject *object)
{
NMFakePlatformPrivate *priv = NM_FAKE_PLATFORM_GET_PRIVATE (object);
int i;
GSList *iter;
for (iter = priv->link_added_ids; iter; iter = iter->next)
g_source_remove (GPOINTER_TO_UINT (iter->data));
g_slist_free (priv->link_added_ids);
g_hash_table_unref (priv->options);
for (i = 0; i < priv->links->len; i++) {
@ -1288,6 +1264,7 @@ nm_fake_platform_class_init (NMFakePlatformClass *klass)
platform_class->sysctl_set = sysctl_set;
platform_class->sysctl_get = sysctl_get;
platform_class->link_get = _nm_platform_link_get;
platform_class->link_get_all = link_get_all;
platform_class->link_add = link_add;
platform_class->link_delete = link_delete;

View file

@ -1535,15 +1535,24 @@ delete_object (NMPlatform *platform, struct nl_object *obj, gboolean do_refresh_
debug("delete_object failed with \"%s\" (%d), meaning the object was already removed",
nl_geterror (nle), nle);
break;
case -NLE_FAILURE:
if (object_type == OBJECT_TYPE_IP6_ADDRESS) {
/* On RHEL7 kernel, deleting a non existing address fails with ENXIO (which libnl maps to NLE_FAILURE) */
debug("delete_object for address failed with \"%s\" (%d), meaning the address was already removed",
nl_geterror (nle), nle);
break;
}
goto DEFAULT;
case -NLE_NOADDR:
if (object_type == OBJECT_TYPE_IP4_ADDRESS || object_type == OBJECT_TYPE_IP6_ADDRESS) {
debug("delete_object for address failed with \"%s\" (%d), meaning the address was already removed",
nl_geterror (nle), nle);
break;
}
/* fall-through to error, because we only expect this for addresses. */
goto DEFAULT;
DEFAULT:
default:
error ("Netlink error deleting %s: %s", to_string_object (platform, obj), nl_geterror (nle));
error ("Netlink error deleting %s: %s (%d)", to_string_object (platform, obj), nl_geterror (nle), nle);
return FALSE;
}
@ -1627,7 +1636,7 @@ event_notification (struct nl_msg *msg, gpointer user_data)
return NL_OK;
nl_cache_remove (cached_object);
/* Don't announced removed interfaces that are not recognized by
/* Don't announce removed interfaces that are not recognized by
* udev. They were either not yet discovered or they have been
* already removed and announced.
*/
@ -1868,6 +1877,22 @@ link_get_all (NMPlatform *platform)
return links;
}
static gboolean
_nm_platform_link_get (NMPlatform *platform, int ifindex, NMPlatformLink *link)
{
NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
auto_nl_object struct rtnl_link *rtnllink;
rtnllink = rtnl_link_get (priv->link_cache, ifindex);
if (rtnllink) {
if (link_is_announceable (platform, rtnllink)) {
if (init_link (platform, link, rtnllink))
return TRUE;
}
}
return FALSE;
}
static struct nl_object *
build_rtnl_link (int ifindex, const char *name, NMLinkType type)
{
@ -3569,38 +3594,40 @@ udev_device_added (NMPlatform *platform,
auto_nl_object struct rtnl_link *rtnllink = NULL;
const char *ifname;
int ifindex;
gboolean is_changed;
gboolean was_announceable = FALSE;
ifname = g_udev_device_get_name (udev_device);
if (!ifname) {
debug ("failed to get device's interface");
debug ("udev-add: failed to get device's interface");
return;
}
if (g_udev_device_get_property (udev_device, "IFINDEX"))
ifindex = g_udev_device_get_property_as_int (udev_device, "IFINDEX");
else {
warning ("(%s): failed to get device's ifindex", ifname);
warning ("(%s): udev-add: failed to get device's ifindex", ifname);
return;
}
if (ifindex <= 0) {
warning ("(%s): udev-add: retrieved invalid IFINDEX=%d", ifname, ifindex);
return;
}
if (!g_udev_device_get_sysfs_path (udev_device)) {
debug ("(%s): couldn't determine device path; ignoring...", ifname);
debug ("(%s): udev-add: couldn't determine device path; ignoring...", ifname);
return;
}
is_changed = g_hash_table_lookup_extended (priv->udev_devices, GINT_TO_POINTER (ifindex), NULL, NULL);
rtnllink = rtnl_link_get (priv->link_cache, ifindex);
if (rtnllink)
was_announceable = link_is_announceable (platform, rtnllink);
g_hash_table_insert (priv->udev_devices, GINT_TO_POINTER (ifindex),
g_object_ref (udev_device));
/* Don't announce devices that have not yet been discovered via Netlink. */
rtnllink = rtnl_link_get (priv->link_cache, ifindex);
if (!rtnllink) {
debug ("%s: not found in link cache, ignoring...", ifname);
return;
}
announce_object (platform, (struct nl_object *) rtnllink, is_changed ? NM_PLATFORM_SIGNAL_CHANGED : NM_PLATFORM_SIGNAL_ADDED, NM_PLATFORM_REASON_EXTERNAL);
/* Announce devices only if they also have been discovered via Netlink. */
if (rtnllink && link_is_announceable (platform, rtnllink))
announce_object (platform, (struct nl_object *) rtnllink, was_announceable ? NM_PLATFORM_SIGNAL_CHANGED : NM_PLATFORM_SIGNAL_ADDED, NM_PLATFORM_REASON_EXTERNAL);
}
static void
@ -3608,12 +3635,13 @@ udev_device_removed (NMPlatform *platform,
GUdevDevice *udev_device)
{
NMLinuxPlatformPrivate *priv = NM_LINUX_PLATFORM_GET_PRIVATE (platform);
auto_nl_object struct rtnl_link *rtnllink = NULL;
int ifindex = 0;
gboolean was_announceable = FALSE;
if (g_udev_device_get_property (udev_device, "IFINDEX")) {
if (g_udev_device_get_property (udev_device, "IFINDEX"))
ifindex = g_udev_device_get_property_as_int (udev_device, "IFINDEX");
g_hash_table_remove (priv->udev_devices, GINT_TO_POINTER (ifindex));
} else {
else {
GHashTableIter iter;
gpointer key, value;
@ -3625,19 +3653,24 @@ udev_device_removed (NMPlatform *platform,
while (g_hash_table_iter_next (&iter, &key, &value)) {
if ((GUdevDevice *)value == udev_device) {
ifindex = GPOINTER_TO_INT (key);
g_hash_table_iter_remove (&iter);
break;
}
}
}
/* Announce device removal if it's still in the Netlink cache. */
if (ifindex) {
auto_nl_object struct rtnl_link *device = rtnl_link_get (priv->link_cache, ifindex);
debug ("udev-remove: IFINDEX=%d", ifindex);
if (ifindex <= 0)
return;
if (device)
announce_object (platform, (struct nl_object *) device, NM_PLATFORM_SIGNAL_REMOVED, NM_PLATFORM_REASON_EXTERNAL);
}
rtnllink = rtnl_link_get (priv->link_cache, ifindex);
if (rtnllink)
was_announceable = link_is_announceable (platform, rtnllink);
g_hash_table_remove (priv->udev_devices, GINT_TO_POINTER (ifindex));
/* Announce device removal if it is no longer announceable. */
if (was_announceable && !link_is_announceable (platform, rtnllink))
announce_object (platform, (struct nl_object *) rtnllink, NM_PLATFORM_SIGNAL_REMOVED, NM_PLATFORM_REASON_EXTERNAL);
}
static void
@ -3795,6 +3828,7 @@ nm_linux_platform_class_init (NMLinuxPlatformClass *klass)
platform_class->sysctl_set = sysctl_set;
platform_class->sysctl_get = sysctl_get;
platform_class->link_get = _nm_platform_link_get;
platform_class->link_get_all = link_get_all;
platform_class->link_add = link_add;
platform_class->link_delete = link_delete;

View file

@ -314,7 +314,7 @@ nm_platform_sysctl_get_int_checked (const char *path, guint base, gint64 min, gi
/**
* nm_platform_query_devices:
*
* Emit #NMPlatform:link-added signals for all currently-known links.
* Emit #NMPlatform:link-changed ADDED signals for all currently-known links.
* Should only be called at startup.
*/
void
@ -439,6 +439,27 @@ nm_platform_link_get_all (void)
return result;
}
/**
* nm_platform_link_get:
* @ifindex: ifindex of the link
* @link: (out): output NMPlatformLink structure.
*
* If a link with given @ifindex exists, fill the given NMPlatformLink
* structure.
*
* Returns: %TRUE, if such a link exists, %FALSE otherwise.
* If the link does not exist, the content of @link is undefined.
**/
gboolean
nm_platform_link_get (int ifindex, NMPlatformLink *link)
{
g_return_val_if_fail (ifindex > 0, FALSE);
g_return_val_if_fail (link, FALSE);
g_return_val_if_fail (klass->link_get, FALSE);
return !!klass->link_get (platform, ifindex, link);
}
/**
* nm_platform_link_add:
* @name: Interface name
@ -447,8 +468,8 @@ nm_platform_link_get_all (void)
* @address_len: the length of the @address
*
* Add a software interface. Sets platform->error to NM_PLATFORM_ERROR_EXISTS
* if interface is already already exists. Any link-added signal will be
* emitted from an idle handler and not within this function.
* if interface is already already exists. Any link-changed ADDED signal will be
* emitted directly, before this function finishes.
*/
static gboolean
nm_platform_link_add (const char *name, NMLinkType type, const void *address, size_t address_len)

View file

@ -346,6 +346,7 @@ typedef struct {
gboolean (*sysctl_set) (NMPlatform *, const char *path, const char *value);
char * (*sysctl_get) (NMPlatform *, const char *path);
gboolean (*link_get) (NMPlatform *platform, int ifindex, NMPlatformLink *link);
GArray *(*link_get_all) (NMPlatform *);
gboolean (*link_add) (NMPlatform *, const char *name, NMLinkType type, const void *address, size_t address_len);
gboolean (*link_delete) (NMPlatform *, int ifindex);
@ -477,6 +478,7 @@ char *nm_platform_sysctl_get (const char *path);
gint32 nm_platform_sysctl_get_int32 (const char *path, gint32 fallback);
gint64 nm_platform_sysctl_get_int_checked (const char *path, guint base, gint64 min, gint64 max, gint64 fallback);
gboolean nm_platform_link_get (int ifindex, NMPlatformLink *link);
GArray *nm_platform_link_get_all (void);
gboolean nm_platform_dummy_add (const char *name);
gboolean nm_platform_bridge_add (const char *name, const void *address, size_t address_len);

View file

@ -14,6 +14,10 @@ AM_CPPFLAGS = \
$(GUDEV_CFLAGS) \
$(LIBNL_CFLAGS)
if REQUIRE_ROOT_TESTS
AM_CPPFLAGS += -DREQUIRE_ROOT_TESTS=1
endif
PLATFORM_SOURCES = \
../nm-platform.c \
../nm-fake-platform.c \
@ -106,20 +110,7 @@ test_cleanup_linux_CPPFLAGS = \
-DKERNEL_HACKS=1
test_cleanup_linux_LDADD = $(PLATFORM_LDADD)
# Unfortunately, we cannot run nm-linux-platform-test as an automatic test
# program by default, as it requires root access and modifies kernel
# configuration.
#
# However, we can check whether the fake platform fakes platform behavior
# correctly.
#@VALGRIND_RULES@
TESTS = test-link-fake test-address-fake test-route-fake test-cleanup-fake test-link-linux test-address-linux test-route-linux test-cleanup-linux
@VALGRIND_RULES@
USERTESTS = test-link-fake test-address-fake test-route-fake test-cleanup-fake
ROOTTESTS = test-link-linux test-address-linux test-route-linux test-cleanup-linux
# If explicitly enabled, we can run the root tests
if RUN_ROOT_TESTS
TESTS = $(USERTESTS) $(ROOTTESTS)
else
TESTS = $(USERTESTS)
endif

View file

@ -252,7 +252,7 @@ setup_tests (void)
nm_platform_link_delete (nm_platform_link_get_ifindex (DEVICE_NAME));
g_assert (!nm_platform_link_exists (DEVICE_NAME));
g_assert (nm_platform_dummy_add (DEVICE_NAME));
wait_signal (link_added);
accept_signal (link_added);
free_signal (link_added);
g_test_add_func ("/address/internal/ip4", test_ip4_address);

View file

@ -34,7 +34,7 @@ test_cleanup_internal ()
/* Create and set up device */
g_assert (nm_platform_dummy_add (DEVICE_NAME));
wait_signal (link_added);
accept_signal (link_added);
free_signal (link_added);
g_assert (nm_platform_link_set_up (nm_platform_link_get_ifindex (DEVICE_NAME)));
ifindex = nm_platform_link_get_ifindex (DEVICE_NAME);

View file

@ -1,5 +1,6 @@
#include "test-common.h"
#include "nm-glib-compat.h"
#include "nm-test-utils.h"
SignalData *
add_signal_full (const char *name, NMPlatformSignalChangeType change_type, GCallback callback, int ifindex, const char *ifname)
@ -47,6 +48,9 @@ accept_signal (SignalData *data)
void
wait_signal (SignalData *data)
{
if (data->received)
g_error ("Signal '%s' received before waiting for it.", data->name);
data->loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (data->loop);
g_clear_pointer (&data->loop, g_main_loop_unref);
@ -95,6 +99,11 @@ link_callback (NMPlatform *platform, int ifindex, NMPlatformLink *received, NMPl
debug ("Received signal '%s-%s' ifindex %d ifname '%s'.", data->name, _change_type_to_string (data->change_type), ifindex, received->name);
data->received = TRUE;
if (change_type == NM_PLATFORM_SIGNAL_REMOVED)
g_assert (!nm_platform_link_get_name (ifindex));
else
g_assert (nm_platform_link_get_name (ifindex));
/* Check the data */
g_assert (received->ifindex > 0);
links = nm_platform_link_get_all ();
@ -130,23 +139,27 @@ run_command (const char *format, ...)
g_free (command);
}
NMTST_DEFINE();
int
main (int argc, char **argv)
{
int result;
const char *program = *argv;
openlog (G_LOG_DOMAIN, LOG_CONS | LOG_PERROR, LOG_DAEMON);
nmtst_init_with_logging (&argc, &argv, NULL, "ALL");
#if !GLIB_CHECK_VERSION (2, 35, 0)
g_type_init ();
if (SETUP == nm_linux_platform_setup && getuid() != 0) {
/* Try to exec as sudo, this function does not return, if a sudo-cmd is set. */
nmtst_reexec_sudo ();
#ifdef REQUIRE_ROOT_TESTS
g_message ("Fail test: requires root privileges (%s)", program);
return EXIT_FAILURE;
#else
g_message ("Skipping test: requires root privileges (%s)", program);
return 77;
#endif
g_test_init (&argc, &argv, NULL);
/* Enable debug messages if called with --debug */
for (; *argv; argv++) {
if (!g_strcmp0 (*argv, "--debug")) {
nm_logging_setup ("debug", NULL, NULL, NULL);
}
}
SETUP ();

View file

@ -1,4 +1,5 @@
#include "test-common.h"
#include "nm-test-utils.h"
#define LO_INDEX 1
#define LO_NAME "lo"
@ -112,7 +113,7 @@ software_add (NMLinkType link_type, const char *name)
/* Don't call link_callback for the bridge interface */
parent_added = add_signal_ifname (NM_PLATFORM_SIGNAL_LINK_CHANGED, NM_PLATFORM_SIGNAL_ADDED, link_callback, PARENT_NAME);
if (nm_platform_bridge_add (PARENT_NAME, NULL, 0))
wait_signal (parent_added);
accept_signal (parent_added);
free_signal (parent_added);
{
@ -144,7 +145,7 @@ test_slave (int master, int type, SignalData *master_changed)
g_assert (ifindex > 0);
link_changed = add_signal_ifindex (NM_PLATFORM_SIGNAL_LINK_CHANGED, NM_PLATFORM_SIGNAL_CHANGED, link_callback, ifindex);
link_removed = add_signal_ifindex (NM_PLATFORM_SIGNAL_LINK_CHANGED, NM_PLATFORM_SIGNAL_REMOVED, link_callback, ifindex);
wait_signal (link_added);
accept_signal (link_added);
/* Set the slave up to see whether master's IFF_LOWER_UP is set correctly.
*
@ -182,7 +183,20 @@ test_slave (int master, int type, SignalData *master_changed)
}
g_assert (!nm_platform_link_is_up (ifindex));
g_assert (!nm_platform_link_is_connected (ifindex));
g_assert (!nm_platform_link_is_connected (master));
if (nm_platform_link_is_connected (master)) {
if (nm_platform_link_get_type (master) == NM_LINK_TYPE_TEAM) {
/* Older team versions (e.g. Fedora 17) have a bug that team master stays
* IFF_LOWER_UP even if its slave is down. Double check it with iproute2 and if
* `ip link` also claims master to be up, accept it. */
char *stdout = NULL;
nmtst_spawn_sync (NULL, &stdout, NULL, 0, "/sbin/ip", "link", "show", "dev", nm_platform_link_get_name (master));
g_assert (strstr (stdout, "LOWER_UP"));
g_free (stdout);
} else
g_assert_not_reached ();
}
/* Set slave up and see if master gets up too */
g_assert (nm_platform_link_set_up (ifindex)); no_error ();
@ -246,7 +260,7 @@ test_software (NMLinkType link_type, const char *link_typename)
link_added = add_signal_ifname (NM_PLATFORM_SIGNAL_LINK_CHANGED, NM_PLATFORM_SIGNAL_ADDED, link_callback, DEVICE_NAME);
g_assert (software_add (link_type, DEVICE_NAME));
no_error ();
wait_signal (link_added);
accept_signal (link_added);
g_assert (nm_platform_link_exists (DEVICE_NAME));
ifindex = nm_platform_link_get_ifindex (DEVICE_NAME);
g_assert (ifindex >= 0);
@ -348,6 +362,13 @@ test_bridge (void)
static void
test_bond (void)
{
if (SETUP == nm_linux_platform_setup &&
!g_file_test ("/proc/1/net/bonding", G_FILE_TEST_IS_DIR) &&
system("modprobe --show bonding") != 0) {
g_test_skip ("Skipping test for bonding: bonding module not available");
return;
}
test_software (NM_LINK_TYPE_BOND, "bond");
}
@ -381,7 +402,7 @@ test_internal (void)
/* Add device */
g_assert (nm_platform_dummy_add (DEVICE_NAME));
no_error ();
wait_signal (link_added);
accept_signal (link_added);
/* Try to add again */
g_assert (!nm_platform_dummy_add (DEVICE_NAME));
@ -453,9 +474,11 @@ test_internal (void)
static void
test_external (void)
{
NMPlatformLink link;
SignalData *link_added = add_signal_ifname (NM_PLATFORM_SIGNAL_LINK_CHANGED, NM_PLATFORM_SIGNAL_ADDED, link_callback, DEVICE_NAME);
SignalData *link_changed, *link_removed;
int ifindex;
gboolean success;
run_command ("ip link add %s type %s", DEVICE_NAME, "dummy");
wait_signal (link_added);
@ -468,6 +491,13 @@ test_external (void)
link_changed = add_signal_ifindex (NM_PLATFORM_SIGNAL_LINK_CHANGED, NM_PLATFORM_SIGNAL_CHANGED, link_callback, ifindex);
link_removed = add_signal_ifindex (NM_PLATFORM_SIGNAL_LINK_CHANGED, NM_PLATFORM_SIGNAL_REMOVED, link_callback, ifindex);
success = nm_platform_link_get (ifindex, &link);
g_assert (success);
if (!link.driver) {
/* we still lack the notification via UDEV. Expect another link changed signal. */
wait_signal (link_changed);
}
/* Up/connected/arp */
g_assert (!nm_platform_link_is_up (ifindex));
g_assert (!nm_platform_link_is_connected (ifindex));

View file

@ -1,4 +1,5 @@
#include "test-common.h"
#include "nm-test-utils.h"
#define DEVICE_NAME "nm-test-device"
@ -93,18 +94,21 @@ test_ip4_route ()
/* Test route listing */
routes = nm_platform_ip4_route_get_all (ifindex, TRUE);
memset (rts, 0, sizeof (rts));
rts[0].source = NM_PLATFORM_SOURCE_KERNEL;
rts[0].network = gateway;
rts[0].plen = 32;
rts[0].ifindex = ifindex;
rts[0].gateway = INADDR_ANY;
rts[0].metric = metric;
rts[0].mss = mss;
rts[1].source = NM_PLATFORM_SOURCE_KERNEL;
rts[1].network = network;
rts[1].plen = plen;
rts[1].ifindex = ifindex;
rts[1].gateway = gateway;
rts[1].metric = metric;
rts[1].mss = mss;
rts[2].source = NM_PLATFORM_SOURCE_KERNEL;
rts[2].network = 0;
rts[2].plen = 0;
rts[2].ifindex = ifindex;
@ -113,6 +117,7 @@ test_ip4_route ()
rts[2].mss = mss;
g_assert_cmpint (routes->len, ==, 3);
g_assert (!memcmp (routes->data, rts, sizeof (rts)));
nmtst_platform_ip4_routes_equal ((NMPlatformIP4Route *) routes->data, rts, routes->len);
g_array_unref (routes);
/* Remove route */
@ -176,18 +181,21 @@ test_ip6_route ()
/* Test route listing */
routes = nm_platform_ip6_route_get_all (ifindex, TRUE);
memset (rts, 0, sizeof (rts));
rts[0].source = NM_PLATFORM_SOURCE_KERNEL;
rts[0].network = gateway;
rts[0].plen = 128;
rts[0].ifindex = ifindex;
rts[0].gateway = in6addr_any;
rts[0].metric = metric;
rts[0].mss = mss;
rts[1].source = NM_PLATFORM_SOURCE_KERNEL;
rts[1].network = network;
rts[1].plen = plen;
rts[1].ifindex = ifindex;
rts[1].gateway = gateway;
rts[1].metric = metric;
rts[1].mss = mss;
rts[2].source = NM_PLATFORM_SOURCE_KERNEL;
rts[2].network = in6addr_any;
rts[2].plen = 0;
rts[2].ifindex = ifindex;
@ -196,6 +204,7 @@ test_ip6_route ()
rts[2].mss = mss;
g_assert_cmpint (routes->len, ==, 3);
g_assert (!memcmp (routes->data, rts, sizeof (rts)));
nmtst_platform_ip6_routes_equal ((NMPlatformIP6Route *) routes->data, rts, routes->len);
g_array_unref (routes);
/* Remove route */
@ -220,7 +229,7 @@ setup_tests (void)
nm_platform_link_delete (nm_platform_link_get_ifindex (DEVICE_NAME));
g_assert (!nm_platform_link_exists (DEVICE_NAME));
g_assert (nm_platform_dummy_add (DEVICE_NAME));
wait_signal (link_added);
accept_signal (link_added);
free_signal (link_added);
g_assert (nm_platform_link_set_up (nm_platform_link_get_ifindex (DEVICE_NAME)));

View file

@ -23,11 +23,10 @@
#include <unistd.h>
#include <string.h>
#include "nm-test-helpers.h"
#include "common.h"
#include "utils.h"
#include "nm-test-utils.h"
static void
test_get_ifcfg_name (const char *desc,
@ -114,10 +113,14 @@ test_ignored (const char *desc, const char *path, gboolean expected_ignored)
ASSERT (result == expected_ignored, desc, "unexpected ignore result for path '%s'", path);
}
NMTST_DEFINE ();
int main (int argc, char **argv)
{
char *base;
nmtst_init (&argc, &argv, TRUE);
/* The tests */
test_get_ifcfg_name ("get-ifcfg-name-bad", "/foo/bar/adfasdfadf", FALSE, NULL);
test_get_ifcfg_name ("get-ifcfg-name-good", "/foo/bar/ifcfg-FooBar", FALSE, "FooBar");

View file

@ -48,14 +48,15 @@
#include <nm-setting-dcb.h>
#include <nm-util-private.h>
#include "nm-test-helpers.h"
#include "NetworkManagerUtils.h"
#include "nm-glib-compat.h"
#include "common.h"
#include "reader.h"
#include "writer.h"
#include "utils.h"
#include "nm-logging.h"
#include "nm-test-utils.h"
#if 0
static void
@ -14377,27 +14378,11 @@ test_svUnescape ()
#define TPATH "/settings/plugins/ifcfg-rh/"
NMTST_DEFINE ();
int main (int argc, char **argv)
{
GError *error = NULL;
gboolean success;
g_test_init (&argc, &argv, NULL);
#if GLIB_CHECK_VERSION(2,34,0)
/* consider even unexpected g_message()s to be fatal */
g_log_set_always_fatal (G_LOG_LEVEL_MASK);
#else
/* g_test_expect_message() is dummied out, so allow warnings */
g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
#endif
#if !GLIB_CHECK_VERSION (2, 35, 0)
g_type_init ();
#endif
success = nm_utils_init (&error);
g_assert_no_error (error);
g_assert (success);
nmtst_init_assert_logging (&argc, &argv);
g_test_add_func (TPATH "svUnescape", test_svUnescape);

View file

@ -28,7 +28,6 @@
#include <unistd.h>
#include <nm-utils.h>
#include "nm-test-helpers.h"
#include "nm-linux-platform.h"
#include "nm-logging.h"
@ -38,6 +37,8 @@
#include "connection_parser.h"
#include "nm-config.h"
#include "nm-test-utils.h"
/* Fake NMConfig handling; the values it returns don't matter, so this
* is easier than forcing it to read our own config file, etc.
*/
@ -450,16 +451,16 @@ test_missing_config ()
"get connection should fail with 'Unknown config for eth8'");
}
NMTST_DEFINE ();
int
main (int argc, char **argv)
{
char *f;
#if !GLIB_CHECK_VERSION (2, 35, 0)
g_type_init ();
#endif
nm_linux_platform_setup ();
nmtst_init_assert_logging (&argc, &argv);
nm_logging_setup ("WARN", "DEFAULT", NULL, NULL);
f = g_build_filename (argv[1], "net", NULL);

View file

@ -23,12 +23,12 @@
#include <nm-utils.h>
#include "nm-test-helpers.h"
#include "nm-logging.h"
#include "interface_parser.h"
#include "parser.h"
#include "nm-test-utils.h"
typedef struct {
char *key;
char *data;

View file

@ -40,13 +40,13 @@
#include <nm-setting-gsm.h>
#include <nm-setting-8021x.h>
#include <nm-setting-infiniband.h>
#include "nm-test-helpers.h"
#include "nm-glib-compat.h"
#include <nm-logging.h>
#include "reader.h"
#include "writer.h"
#include "nm-test-utils.h"
#define TEST_WIRED_FILE TEST_KEYFILES_DIR"/Test_Wired_Connection"
#define TEST_WIRELESS_FILE TEST_KEYFILES_DIR"/Test_Wireless_Connection"
@ -3410,19 +3410,13 @@ test_read_missing_vlan_setting (void)
g_object_unref (connection);
}
NMTST_DEFINE ();
int main (int argc, char **argv)
{
GError *error = NULL;
char *base;
#if !GLIB_CHECK_VERSION (2, 35, 0)
g_type_init ();
#endif
g_log_set_always_fatal (G_LOG_LEVEL_MASK);
if (!nm_utils_init (&error))
FAIL ("nm-utils-init", "failed to initialize libnm-util: %s", error->message);
nmtst_init_assert_logging (&argc, &argv);
/* The tests */
test_read_valid_wired_connection ();

View file

@ -39,12 +39,11 @@
#include <nm-setting-ip4-config.h>
#include <nm-setting-8021x.h>
#include "nm-test-helpers.h"
#include "nm-glib-compat.h"
#include "nm-supplicant-config.h"
#include "nm-supplicant-settings-verify.h"
#include "nm-test-utils.h"
static gboolean
validate_opt (const char *detail,
GHashTable *hash,
@ -495,17 +494,13 @@ test_wifi_wpa_psk_types (void)
test_wifi_wpa_psk ("wifi-wep-psk-passphrase", TYPE_STRING, key2, (gconstpointer) key2, strlen (key2));
}
NMTST_DEFINE ();
int main (int argc, char **argv)
{
GError *error = NULL;
char *base;
#if !GLIB_CHECK_VERSION (2, 35, 0)
g_type_init ();
#endif
if (!nm_utils_init (&error))
FAIL ("nm-utils-init", "failed to initialize libnm-util: %s", error->message);
nmtst_init (&argc, &argv, TRUE);
/* The tests */
test_wifi_open ();

View file

@ -25,12 +25,13 @@
#include <arpa/inet.h>
#include <string.h>
#include "nm-test-helpers.h"
#include <nm-utils.h>
#include "nm-dhcp-manager.h"
#include "nm-logging.h"
#include "nm-test-utils.h"
typedef struct {
const char *name;
const char *value;
@ -836,24 +837,15 @@ test_ip4_prefix_classless (gconstpointer test_data)
g_hash_table_destroy (options);
}
NMTST_DEFINE ();
int main (int argc, char **argv)
{
GError *error = NULL;
char *path;
const char *clients[2][2] = { {DHCLIENT_PATH, "dhclient"}, {DHCPCD_PATH, "dhcpcd"} };
guint32 i;
g_test_init (&argc, &argv, NULL);
#if !GLIB_CHECK_VERSION(2,34,0)
g_log_set_always_fatal (G_LOG_LEVEL_CRITICAL);
#endif
#if !GLIB_CHECK_VERSION (2, 35, 0)
g_type_init ();
#endif
if (!nm_utils_init (&error))
FAIL ("nm-utils-init", "failed to initialize libnm-util: %s", error->message);
nmtst_init_assert_logging (&argc, &argv);
nm_logging_setup ("WARN", "DEFAULT", NULL, NULL);
for (i = 0; i < 2; i++) {

View file

@ -24,6 +24,7 @@
#include "nm-ip6-config.h"
#include "nm-logging.h"
#include "nm-test-utils.h"
static NMIP6Config *
@ -324,7 +325,7 @@ NMTST_DEFINE();
int
main (int argc, char **argv)
{
nmtst_init (&argc, &argv);
nmtst_init_with_logging (&argc, &argv, NULL, "ALL");
g_test_add_func ("/ip6-config/subtract", test_subtract);
g_test_add_func ("/ip6-config/compare-with-source", test_compare_with_source);

29
tools/test-sudo-wrapper.sh Executable file
View file

@ -0,0 +1,29 @@
#!/bin/bash
CMD="$1"
shift;
# convert the libtool internal path
resolve_cmd() {
local C="$1"
local C2="$(echo "$C" | sed 's#^\(.*/\)\.libs/lt-\([^/]\+\)$#\1\2#')"
if [[ "$C2" != "$C" && ! -x "$C2" ]]; then
# such a file does not exist... back to $C
C2="$C"
fi
echo "$C2"
}
if [[ $UID == 0 ]]; then
# we are already root. Execute directly.
exec "$(resolve_cmd "$CMD")" "$@"
elif [[ "$NMTST_SUDO_NO_CALL_SELF" != "" ]]; then
# when setting $NMTST_SUDO_NO_CALL_SELF, pass the (resolved) command
# directly to sudo.
exec sudo "$(resolve_cmd "$CMD")" "$@"
else
# by default, call self again with sudo.
exec sudo -E "$0" "$CMD" "$@"
fi