1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the
15 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301 USA.
18 * Copyright 2014 Red Hat, Inc.
21 #ifndef __NM_TEST_UTILS_H__
22 #define __NM_TEST_UTILS_H__
24 /*******************************************************************************
27 * Our tests (make check) include this header-only file nm-test-utils.h.
30 * In tests, nm-logging redirects to glib logging. By default, glib suppresses all debug
31 * messages unless you set G_MESSAGES_DEBUG. To enable debug logging, you can explicitly set
32 * G_MESSAGES_DEBUG. Otherwise, nm-test will set G_MESSAGES_DEBUG=all in debug mode (see below).
33 * For nm-logging, you can configure the log-level and domains via NMTST_DEBUG environment
37 * Some tests assert against logged messages (g_test_expect_message()).
38 * By specifying no-expect-message in NMTST_DEBUG, you can disable assert logging
39 * and g_test_assert_expected_messages() will not fail.
41 * NMTST_SEED_RAND environment variable:
42 * Tests that use random numbers from nmtst_get_rand() get seeded randomly at each start.
43 * You can specify the seed by setting NMTST_SEED_RAND. Also, tests will print the seed
44 * to stdout, so that you know the choosen seed.
47 * NMTST_DEBUG environment variable:
49 * "debug", "no-debug": when at test is run in debug mode, it might behave differently,
50 * depending on the test. See nmtst_is_debug().
52 * - a test might leave the logging level unspecified. In this case, running in
53 * debug mode, will turn on DEBUG logging, otherwise WARN logging only.
54 * - if G_MESSAGES_DEBUG is unset, nm-test will set G_MESSAGES_DEBUG=all
55 * for tests that don't do assert-logging.
56 * Debug mode is determined as follows (highest priority first):
57 * - command line option --debug/--no-debug
58 * - NMTST_DEBUG=debug/no-debug
59 * - setting NMTST_DEBUG implies debugging turned on
62 * "no-expect-message": for tests that would assert against log messages, disable
65 * "log-level=LEVEL", "log-domains=DOMAIN": reset the log level and domain for tests.
66 * It only has an effect for nm-logging messages.
67 * This has no effect if the test asserts against logging (unless no-expect-message),
68 * otherwise, changing the logging would break tests.
69 * If you set the level to DEBUG or TRACE, it also sets G_MESSAGES_DEBUG=all (unless
70 * in assert-logging mode and unless G_MESSAGES_DEBUG is already defined).
72 * "TRACE", this is shorthand for "log-level=TRACE".
74 * "D", this is shorthand for "log-level=TRACE,no-expect-message".
76 * "sudo-cmd=PATH": when running root tests as normal user, the test will execute
77 * itself by invoking sudo at PATH.
79 * NMTST_DEBUG="sudo-cmd=$PWD/tools/test-sudo-wrapper.sh" make -C src/platform/tests/ check
81 * "slow|quick|thorough": enable/disable long-running tests. This sets nmtst_test_quick().
82 * Whether long-running tests are enabled is determined as follows (highest priority first):
83 * - specifying the value in NMTST_DEBUG has highest priority
84 * - respect g_test_quick(), if the command line contains '-mslow', '-mquick', '-mthorough'.
85 * - use compile time default
87 * "p=PATH"|"s=PATH": passes the path to g_test_init() as "-p" and "-s", respectively.
88 * Unfortunately, these options conflict with "--tap" which our makefile passes to the
89 * tests, thus it's only useful outside of `make check`.
91 *******************************************************************************/
93 #include "nm-default.h"
95 #include <arpa/inet.h>
102 #include "nm-utils.h"
104 #ifdef __NETWORKMANAGER_LOGGING_H__
105 /* We are running tests under src/. Let's include some files by default.
106 * They are useful, and affect how nm-test-utils.h itself behaves. */
107 #include "NetworkManagerUtils.h"
108 #include "nm-keyfile-internal.h"
111 /*******************************************************************************/
113 /* general purpose functions that have no dependency on other nmtst functions */
115 #define nmtst_assert_error(error, expect_error_domain, expect_error_code, expect_error_pattern) \
117 GError *_error = (error); \
118 GQuark _expect_error_domain = (expect_error_domain); \
119 const char *_expect_error_pattern = (expect_error_pattern); \
121 if (_expect_error_domain) \
122 g_assert_error (_error, _expect_error_domain, (expect_error_code)); \
125 g_assert (_error->message); \
126 if ( _expect_error_pattern \
127 && !g_pattern_match_simple (_expect_error_pattern, _error->message)) { \
128 g_error ("%s:%d: error message does not have expected pattern '%s'. Instead it is '%s' (%s, %d)", \
129 __FILE__, __LINE__, \
130 _expect_error_pattern, _error->message, g_quark_to_string (_error->domain), _error->code); \
134 #define NMTST_WAIT(max_wait_ms, wait) \
136 gboolean _not_expired = TRUE; \
137 gint64 _nmtst_end, _nmtst_max_wait_us = (max_wait_ms) * 1000L; \
139 _nmtst_end = g_get_monotonic_time () + _nmtst_max_wait_us; \
142 if (g_get_monotonic_time () > _nmtst_end) { \
143 _not_expired = FALSE; \
150 #define NMTST_WAIT_ASSERT(max_wait_ms, wait) \
152 if (!(NMTST_WAIT (max_wait_ms, wait))) \
153 g_assert_not_reached (); \
157 _nmtst_assert_success (gboolean success, GError *error, const char *file, int line)
159 if (!success || error)
160 g_error ("(%s:%d) FAILURE success=%d, error=%s", file, line, success, error ? error->message : "(no error)");
162 #define nmtst_assert_success(success, error) _nmtst_assert_success ((success), (error), __FILE__, __LINE__)
164 #define nmtst_assert_no_success(success, error) \
167 g_assert (!(success)); \
170 /*******************************************************************************/
172 struct __nmtst_internal
178 gboolean assert_logging;
179 gboolean no_expect_message;
181 gboolean test_tap_log;
186 extern struct __nmtst_internal __nmtst_internal;
188 #define NMTST_DEFINE() \
189 struct __nmtst_internal __nmtst_internal = { 0 }; \
191 __attribute__ ((destructor)) static void \
194 __nmtst_internal.assert_logging = FALSE; \
195 g_test_assert_expected_messages (); \
200 inline static gboolean
201 nmtst_initialized (void)
203 return !!__nmtst_internal.rand0;
206 #define __NMTST_LOG(cmd, ...) \
208 g_assert (nmtst_initialized ()); \
209 if (!__nmtst_internal.assert_logging || __nmtst_internal.no_expect_message) { \
212 printf (_NM_UTILS_MACRO_FIRST (__VA_ARGS__) "\n" _NM_UTILS_MACRO_REST (__VA_ARGS__)); \
216 /* split the string inplace at specific delimiters, allowing escaping with '\\'.
217 * Returns a zero terminated array of pointers into @str.
219 * The caller must g_free() the returned argv array.
221 inline static char **
222 nmtst_str_split (char *str, const char *delimiters)
225 GArray *result = g_array_sized_new (TRUE, FALSE, sizeof (char *), 3);
228 g_assert (delimiters && !strchr (delimiters, '\\'));
239 } else if (c == '\\') {
244 for (d = delimiters; *d; d++) {
248 goto BREAK_INNER_LOOPS;
257 g_array_append_val (result, str);
261 return (char **) g_array_free (result, FALSE);
265 /* free instances allocated by nmtst (especially nmtst_init()) on shutdown
266 * to release memory. After nmtst_free(), the test is uninitialized again. */
270 if (!nmtst_initialized ())
273 g_rand_free (__nmtst_internal.rand0);
274 if (__nmtst_internal.rand)
275 g_rand_free (__nmtst_internal.rand);
276 g_free (__nmtst_internal.sudo_cmd);
277 g_strfreev (__nmtst_internal.orig_argv);
279 memset (&__nmtst_internal, 0, sizeof (__nmtst_internal));
283 __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_level, const char *log_domains, gboolean *out_set_logging)
285 const char *nmtst_debug;
286 gboolean is_debug = FALSE;
287 char *c_log_level = NULL, *c_log_domains = NULL;
288 char *sudo_cmd = NULL;
289 GArray *debug_messages = g_array_new (TRUE, FALSE, sizeof (char *));
291 gboolean no_expect_message = FALSE;
292 gboolean _out_set_logging;
293 gboolean test_quick = FALSE;
294 gboolean test_quick_set = FALSE;
295 gboolean test_quick_argv = FALSE;
296 gs_unref_ptrarray GPtrArray *p_tests = NULL;
297 gs_unref_ptrarray GPtrArray *s_tests = NULL;
299 if (!out_set_logging)
300 out_set_logging = &_out_set_logging;
301 *out_set_logging = FALSE;
303 g_assert (!nmtst_initialized ());
305 g_assert (!((!!argc) ^ (!!argv)));
306 g_assert (!argc || (g_strv_length (*argv) == *argc));
307 g_assert (!assert_logging || (!log_level && !log_domains));
309 #ifdef __NETWORKMANAGER_UTILS_H__
310 if (!nm_utils_get_testing_initialized ())
311 _nm_utils_set_testing (_NM_UTILS_TEST_GENERAL);
315 __nmtst_internal.orig_argv = g_strdupv (*argv);
317 __nmtst_internal.assert_logging = !!assert_logging;
321 is_debug = g_test_verbose ();
323 nmtst_debug = g_getenv ("NMTST_DEBUG");
325 char **d_argv, **i_argv, *nmtst_debug_copy;
327 /* By setting then NMTST_DEBUG variable, @is_debug is set automatically.
328 * This can be reverted with no-debug (on command line or environment variable). */
331 nmtst_debug_copy = g_strdup (nmtst_debug);
332 d_argv = nmtst_str_split (nmtst_debug_copy, ",; \t\r\n");
334 for (i_argv = d_argv; *i_argv; i_argv++) {
335 const char *debug = *i_argv;
337 if (!g_ascii_strcasecmp (debug, "debug"))
339 else if (!g_ascii_strcasecmp (debug, "no-debug")) {
340 /* when specifying the NMTST_DEBUG variable, we set is_debug to true. Use this flag to disable this
341 * (e.g. for only setting the log-level, but not is_debug). */
343 } else if (!g_ascii_strncasecmp (debug, "log-level=", strlen ("log-level="))) {
344 g_free (c_log_level);
345 log_level = c_log_level = g_strdup (&debug[strlen ("log-level=")]);
346 } else if (!g_ascii_strcasecmp (debug, "D")) {
347 /* shorthand for "log-level=TRACE,no-expect-message" */
348 g_free (c_log_level);
349 log_level = c_log_level = g_strdup ("TRACE");
350 no_expect_message = TRUE;
351 } else if (!g_ascii_strcasecmp (debug, "TRACE")) {
352 g_free (c_log_level);
353 log_level = c_log_level = g_strdup ("TRACE");
354 } else if (!g_ascii_strncasecmp (debug, "log-domains=", strlen ("log-domains="))) {
355 g_free (c_log_domains);
356 log_domains = c_log_domains = g_strdup (&debug[strlen ("log-domains=")]);
357 } else if (!g_ascii_strncasecmp (debug, "sudo-cmd=", strlen ("sudo-cmd="))) {
359 sudo_cmd = g_strdup (&debug[strlen ("sudo-cmd=")]);
360 } else if (!g_ascii_strcasecmp (debug, "no-expect-message")) {
361 no_expect_message = TRUE;
362 } else if (!g_ascii_strncasecmp (debug, "p=", strlen ("p="))) {
364 p_tests = g_ptr_array_new_with_free_func (g_free);
365 g_ptr_array_add (p_tests, g_strdup (&debug[strlen ("p=")]));
366 } else if (!g_ascii_strncasecmp (debug, "s=", strlen ("s="))) {
368 s_tests = g_ptr_array_new_with_free_func (g_free);
369 g_ptr_array_add (s_tests, g_strdup (&debug[strlen ("s=")]));
370 } else if (!g_ascii_strcasecmp (debug, "slow") || !g_ascii_strcasecmp (debug, "thorough")) {
372 test_quick_set = TRUE;
373 } else if (!g_ascii_strcasecmp (debug, "quick")) {
375 test_quick_set = TRUE;
377 char *msg = g_strdup_printf (">>> nmtst: ignore unrecognized NMTST_DEBUG option \"%s\"", debug);
379 g_array_append_val (debug_messages, msg);
384 g_free (nmtst_debug_copy);
387 if (__nmtst_internal.orig_argv) {
388 char **a = __nmtst_internal.orig_argv;
391 if (!g_ascii_strcasecmp (*a, "--debug"))
393 else if (!g_ascii_strcasecmp (*a, "--no-debug"))
395 else if ( !strcmp (*a, "-m=slow")
396 || !strcmp (*a, "-m=thorough")
397 || !strcmp (*a, "-m=quick")
398 || (!strcmp (*a, "-m") && *(a+1)
399 && ( !strcmp (*(a+1), "quick")
400 || !strcmp (*(a+1), "slow")
401 || !strcmp (*(a+1), "thorough"))))
402 test_quick_argv = TRUE;
403 else if (strcmp (*a, "--tap") == 0)
404 __nmtst_internal.test_tap_log = TRUE;
408 if (!argc || g_test_initialized ()) {
409 if (p_tests || s_tests) {
410 char *msg = g_strdup_printf (">>> nmtst: ignore -p and -s options for test which calls g_test_init() itself");
412 g_array_append_val (debug_messages, msg);
415 /* g_test_init() is a variadic function, so we cannot pass it
416 * (variadic) arguments. If you need to pass additional parameters,
417 * call nmtst_init() with argc==NULL and call g_test_init() yourself. */
419 /* g_test_init() sets g_log_set_always_fatal() for G_LOG_LEVEL_WARNING
420 * and G_LOG_LEVEL_CRITICAL. So, beware that the test will fail if you
421 * have any WARN or ERR log messages -- unless you g_test_expect_message(). */
422 GPtrArray *arg_array = g_ptr_array_new ();
423 gs_free char **arg_array_c = NULL;
425 static char **s_tests_x, **p_tests_x;
428 for (i = 0; i < *argc; i++)
429 g_ptr_array_add (arg_array, (*argv)[i]);
431 g_ptr_array_add (arg_array, "./test");
433 if (test_quick_set && !test_quick_argv)
434 g_ptr_array_add (arg_array, "-m=quick");
436 if (!__nmtst_internal.test_tap_log) {
437 for (i = 0; p_tests && i < p_tests->len; i++) {
438 g_ptr_array_add (arg_array, "-p");
439 g_ptr_array_add (arg_array, p_tests->pdata[i]);
441 for (i = 0; s_tests && i < s_tests->len; i++) {
442 g_ptr_array_add (arg_array, "-s");
443 g_ptr_array_add (arg_array, s_tests->pdata[i]);
445 } else if (p_tests || s_tests) {
446 char *msg = g_strdup_printf (">>> nmtst: ignore -p and -s options for tap-tests");
448 g_array_append_val (debug_messages, msg);
451 g_ptr_array_add (arg_array, NULL);
453 arg_array_n = arg_array->len - 1;
454 arg_array_c = (char **) g_ptr_array_free (arg_array, FALSE);
456 g_test_init (&arg_array_n, &arg_array_c, NULL);
459 /* collaps argc/argv by removing the arguments detected
460 * by g_test_init(). */
461 for (i = 1, j = 1; i < *argc; i++) {
462 if ((*argv)[i] == arg_array_c[j])
467 for (i = 1, j = 1; i < *argc; i++) {
469 (*argv)[j++] = (*argv)[i];
477 /* we must "leak" the test paths because they are not cloned by g_test_init(). */
478 if (!__nmtst_internal.test_tap_log) {
480 p_tests_x = (char **) g_ptr_array_free (p_tests, FALSE);
484 s_tests_x = (char **) g_ptr_array_free (s_tests, FALSE);
491 __nmtst_internal.test_quick = test_quick;
492 else if (test_quick_argv)
493 __nmtst_internal.test_quick = g_test_quick ();
495 #ifdef NMTST_TEST_QUICK
496 __nmtst_internal.test_quick = NMTST_TEST_QUICK;
498 __nmtst_internal.test_quick = FALSE;
502 __nmtst_internal.is_debug = is_debug;
503 __nmtst_internal.rand0 = g_rand_new_with_seed (0);
504 __nmtst_internal.sudo_cmd = sudo_cmd;
505 __nmtst_internal.no_expect_message = no_expect_message;
507 if (!log_level && log_domains) {
508 /* if the log level is not specified (but the domain is), we assume
509 * the caller wants to set it depending on is_debug */
510 log_level = is_debug ? "DEBUG" : "WARN";
513 if (!__nmtst_internal.assert_logging) {
514 gboolean success = TRUE;
515 #ifdef __NETWORKMANAGER_LOGGING_H__
516 success = nm_logging_setup (log_level, log_domains, NULL, NULL);
517 *out_set_logging = TRUE;
520 #if GLIB_CHECK_VERSION(2,34,0)
521 if (__nmtst_internal.no_expect_message)
522 g_log_set_always_fatal (G_LOG_FATAL_MASK);
524 /* g_test_expect_message() is a NOP, so allow any messages */
525 g_log_set_always_fatal (G_LOG_FATAL_MASK);
527 } else if (__nmtst_internal.no_expect_message) {
528 /* We have a test that would be assert_logging, but the user specified no_expect_message.
529 * This transforms g_test_expect_message() into a NOP, but we also have to relax
530 * g_log_set_always_fatal(), which was set by g_test_init(). */
531 g_log_set_always_fatal (G_LOG_FATAL_MASK);
532 #ifdef __NETWORKMANAGER_LOGGING_H__
533 if (c_log_domains || c_log_level) {
534 /* Normally, tests with assert_logging do not overwrite the logging level/domains because
535 * the logging statements are part of the assertions. But if the test is run with
536 * no-expect-message *and* the logging is set explicitly via environment variables,
537 * we still reset the logging. */
540 success = nm_logging_setup (log_level, log_domains, NULL, NULL);
541 *out_set_logging = TRUE;
546 #if GLIB_CHECK_VERSION(2,34,0)
547 /* We were called not to set logging levels. This means, that the user
548 * expects to assert against (all) messages. Any uncought message is fatal. */
549 g_log_set_always_fatal (G_LOG_LEVEL_MASK);
551 /* g_test_expect_message() is a NOP, so allow any messages */
552 g_log_set_always_fatal (G_LOG_FATAL_MASK);
556 if ((!__nmtst_internal.assert_logging || (__nmtst_internal.assert_logging && __nmtst_internal.no_expect_message)) &&
557 (is_debug || (c_log_level && (!g_ascii_strcasecmp (c_log_level, "DEBUG") || !g_ascii_strcasecmp (c_log_level, "TRACE")))) &&
558 !g_getenv ("G_MESSAGES_DEBUG"))
560 /* if we are @is_debug or @log_level=="DEBUG" and
561 * G_MESSAGES_DEBUG is unset, we set G_MESSAGES_DEBUG=all.
562 * To disable this default behaviour, set G_MESSAGES_DEBUG='' */
564 /* Note that g_setenv is not thread safe, but you should anyway call
565 * nmtst_init() at the very start. */
566 g_setenv ("G_MESSAGES_DEBUG", "all", TRUE);
569 /* Delay messages until we setup logging. */
570 for (i = 0; i < debug_messages->len; i++)
571 __NMTST_LOG (g_message, "%s", g_array_index (debug_messages, const char *, i));
573 g_strfreev ((char **) g_array_free (debug_messages, FALSE));
574 g_free (c_log_level);
575 g_free (c_log_domains);
577 #ifdef __NETWORKMANAGER_UTILS_H__
578 /* ensure that monotonic timestamp is called (because it initially logs a line) */
579 nm_utils_get_monotonic_timestamp_s ();
584 gs_free_error GError *error = NULL;
586 if (!nm_utils_init (&error))
587 g_assert_not_reached ();
588 g_assert_no_error (error);
593 #ifdef __NETWORKMANAGER_LOGGING_H__
595 nmtst_init_with_logging (int *argc, char ***argv, const char *log_level, const char *log_domains)
597 __nmtst_init (argc, argv, FALSE, log_level, log_domains, NULL);
600 nmtst_init_assert_logging (int *argc, char ***argv, const char *log_level, const char *log_domains)
602 gboolean set_logging;
604 __nmtst_init (argc, argv, TRUE, NULL, NULL, &set_logging);
609 success = nm_logging_setup (log_level, log_domains, NULL, NULL);
615 nmtst_init (int *argc, char ***argv, gboolean assert_logging)
617 __nmtst_init (argc, argv, assert_logging, NULL, NULL, NULL);
621 inline static gboolean
622 nmtst_is_debug (void)
624 g_assert (nmtst_initialized ());
625 return __nmtst_internal.is_debug;
628 inline static gboolean
629 nmtst_test_quick (void)
631 g_assert (nmtst_initialized ());
632 return __nmtst_internal.test_quick;
635 #if GLIB_CHECK_VERSION(2,34,0)
636 #undef g_test_expect_message
637 #define g_test_expect_message(...) \
639 g_assert (nmtst_initialized ()); \
640 if (__nmtst_internal.assert_logging && __nmtst_internal.no_expect_message) { \
641 g_debug ("nmtst: assert-logging: g_test_expect_message %s", G_STRINGIFY ((__VA_ARGS__))); \
643 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
644 g_test_expect_message (__VA_ARGS__); \
645 G_GNUC_END_IGNORE_DEPRECATIONS \
648 #undef g_test_assert_expected_messages_internal
649 #define g_test_assert_expected_messages_internal(domain, file, line, func) \
651 const char *_domain = (domain); \
652 const char *_file = (file); \
653 const char *_func = (func); \
654 int _line = (line); \
656 if (__nmtst_internal.assert_logging && __nmtst_internal.no_expect_message) \
657 g_debug ("nmtst: assert-logging: g_test_assert_expected_messages(%s, %s:%d, %s)", _domain?:"", _file?:"", _line, _func?:""); \
659 G_GNUC_BEGIN_IGNORE_DEPRECATIONS \
660 g_test_assert_expected_messages_internal (_domain, _file, _line, _func); \
661 G_GNUC_END_IGNORE_DEPRECATIONS \
665 /*****************************************************************************/
667 typedef struct _NmtstTestData NmtstTestData;
669 typedef void (*NmtstTestDataRelease) (const NmtstTestData *test_data);
671 struct _NmtstTestData {
672 const char *testpath;
673 NmtstTestDataRelease fcn_release;
679 _nmtst_test_data_unpack (const NmtstTestData *test_data, gsize n_args, ...)
685 g_assert (test_data);
686 g_assert_cmpint (n_args, ==, test_data->n_args);
688 va_start (ap, n_args);
689 for (i = 0; i < n_args; i++) {
690 p = va_arg (ap, gpointer *);
693 *p = test_data->args[i];
697 #define nmtst_test_data_unpack(test_data, ...) _nmtst_test_data_unpack(test_data, NM_NARG (__VA_ARGS__), ##__VA_ARGS__)
700 _nmtst_test_data_free (gpointer data)
702 NmtstTestData *test_data = data;
704 g_assert (test_data);
706 if (test_data->fcn_release)
707 test_data->fcn_release (test_data);
709 g_free ((gpointer) test_data->testpath);
714 _nmtst_add_test_func_full (const char *testpath, GTestDataFunc test_func, NmtstTestDataRelease fcn_release, gsize n_args, ...)
720 data = g_malloc (G_STRUCT_OFFSET (NmtstTestData, args) + sizeof (gpointer) * (n_args + 1));
722 data->testpath = g_strdup (testpath);
723 data->fcn_release = fcn_release;
724 data->n_args = n_args;
725 va_start (ap, n_args);
726 for (i = 0; i < n_args; i++)
727 data->args[i] = va_arg (ap, gpointer);
728 data->args[i] = NULL;
731 g_test_add_data_func_full (testpath,
734 _nmtst_test_data_free);
736 #define nmtst_add_test_func_full(testpath, test_func, fcn_release, ...) _nmtst_add_test_func_full(testpath, test_func, fcn_release, NM_NARG (__VA_ARGS__), ##__VA_ARGS__)
737 #define nmtst_add_test_func(testpath, test_func, ...) nmtst_add_test_func_full(testpath, test_func, NULL, ##__VA_ARGS__)
739 /*****************************************************************************/
741 inline static GRand *
742 nmtst_get_rand0 (void)
744 g_assert (nmtst_initialized ());
745 return __nmtst_internal.rand0;
748 inline static GRand *
749 nmtst_get_rand (void)
751 g_assert (nmtst_initialized ());
753 if (G_UNLIKELY (!__nmtst_internal.rand)) {
757 if ((str = g_getenv ("NMTST_SEED_RAND"))) {
761 i = g_ascii_strtoll (str, &s, 0);
762 g_assert (s[0] == '\0' && i >= 0 && i < G_MAXUINT32);
765 __nmtst_internal.rand = g_rand_new_with_seed (seed);
767 __nmtst_internal.rand = g_rand_new ();
769 seed = g_rand_int (__nmtst_internal.rand);
770 g_rand_set_seed (__nmtst_internal.rand, seed);
772 __nmtst_internal.rand_seed = seed;
774 g_print ("\nnmtst: initialize nmtst_get_rand() with NMTST_SEED_RAND=%u\n", seed);
776 return __nmtst_internal.rand;
779 inline static guint32
780 nmtst_get_rand_int (void)
782 return g_rand_int (nmtst_get_rand ());
786 nmtst_rand_perm (GRand *rand, void *dst, const void *src, gsize elmt_size, gsize n_elmt)
793 g_assert (elmt_size > 0);
794 g_assert (n_elmt < G_MAXINT32);
799 if (src && dst != src)
800 memcpy (dst, src, elmt_size * n_elmt);
803 rand = nmtst_get_rand ();
805 bu = g_slice_alloc (elmt_size);
808 for (i = n_elmt; i > 1; i--) {
809 j = g_rand_int_range (rand, 0, i);
812 pj = &p_[j * elmt_size];
815 memcpy (bu, p_, elmt_size);
816 memcpy (p_, pj, elmt_size);
817 memcpy (pj, bu, elmt_size);
822 g_slice_free1 (elmt_size, bu);
826 /*****************************************************************************/
828 inline static gboolean
829 _nmtst_main_loop_run_timeout (gpointer user_data)
831 GMainLoop **p_loop = user_data;
836 g_main_loop_quit (*p_loop);
839 return G_SOURCE_REMOVE;
842 inline static gboolean
843 nmtst_main_loop_run (GMainLoop *loop, int timeout_ms)
845 GSource *source = NULL;
847 GMainLoop *loopx = loop;
849 if (timeout_ms > 0) {
850 source = g_timeout_source_new (timeout_ms);
851 g_source_set_callback (source, _nmtst_main_loop_run_timeout, &loopx, NULL);
852 id = g_source_attach (source, g_main_loop_get_context (loop));
854 g_source_unref (source);
857 g_main_loop_run (loop);
859 /* if the timeout was reached, return FALSE. */
860 return loopx != NULL;
864 _nmtst_main_loop_quit_on_notify (GObject *object, GParamSpec *pspec, gpointer user_data)
866 GMainLoop *loop = user_data;
868 g_assert (G_IS_OBJECT (object));
871 g_main_loop_quit (loop);
873 #define nmtst_main_loop_quit_on_notify ((GCallback) _nmtst_main_loop_quit_on_notify)
875 /*****************************************************************************/
877 inline static const char *
878 nmtst_get_sudo_cmd (void)
880 g_assert (nmtst_initialized ());
881 return __nmtst_internal.sudo_cmd;
885 nmtst_reexec_sudo (void)
892 g_assert (nmtst_initialized ());
893 g_assert (__nmtst_internal.orig_argv);
895 if (!__nmtst_internal.sudo_cmd)
898 str = g_strjoinv (" ", __nmtst_internal.orig_argv);
899 __NMTST_LOG (g_message, ">> exec %s %s", __nmtst_internal.sudo_cmd, str);
901 argv = g_new0 (char *, 1 + g_strv_length (__nmtst_internal.orig_argv) + 1);
902 argv[0] = __nmtst_internal.sudo_cmd;
903 for (i = 0; __nmtst_internal.orig_argv[i]; i++)
904 argv[i+1] = __nmtst_internal.orig_argv[i];
906 execvp (__nmtst_internal.sudo_cmd, argv);
909 g_error (">> exec %s failed: %d - %s", __nmtst_internal.sudo_cmd, errsv, strerror (errsv));
912 /*****************************************************************************/
915 nmtst_find_all_indexes (gpointer *elements,
919 gboolean (*equal_fcn) (gpointer element, gpointer needle, gpointer user_data),
926 for (i = 0; i < n_needles; i++) {
929 for (j = 0; j < n_elements; j++) {
932 for (k = 0; k < i; k++) {
937 if (equal_fcn (elements[j], needles[i], user_data)) {
953 /*****************************************************************************/
955 #define __define_nmtst_static(NUM,SIZE) \
956 inline static const char * \
957 nmtst_static_##SIZE##_##NUM (const char *str) \
960 static char buf[SIZE]; \
964 l = g_strlcpy (buf, str, sizeof (buf)); \
965 g_assert (l < sizeof (buf)); \
968 __define_nmtst_static(01, 1024)
969 __define_nmtst_static(02, 1024)
970 __define_nmtst_static(03, 1024)
971 #undef __define_nmtst_static
973 inline static const char *
974 nmtst_uuid_generate (void)
977 gs_free char *m = NULL;
979 m = nm_utils_uuid_generate ();
980 g_assert (m && strlen (m) == sizeof (u) - 1);
981 memcpy (u, m, sizeof (u));
985 #define NMTST_SWAP(x,y) \
987 char __nmtst_swap_temp[sizeof(x) == sizeof(y) ? (signed) sizeof(x) : -1]; \
988 memcpy(__nmtst_swap_temp, &y, sizeof(x)); \
989 memcpy(&y, &x, sizeof(x)); \
990 memcpy(&x, __nmtst_swap_temp, sizeof(x)); \
993 #define nmtst_assert_str_has_substr(str, substr) \
995 const char *__str = (str); \
996 const char *__substr = (substr); \
999 g_assert (__substr); \
1000 if (strstr (__str, __substr) == NULL) \
1001 g_error ("%s:%d: Expects \"%s\" but got \"%s\"", __FILE__, __LINE__, __substr, __str); \
1004 inline static guint32
1005 nmtst_inet4_from_string (const char *str)
1013 success = inet_pton (AF_INET, str, &addr);
1015 g_assert (success == 1);
1020 inline static const struct in6_addr *
1021 nmtst_inet6_from_string (const char *str)
1023 static struct in6_addr addr;
1029 success = inet_pton (AF_INET6, str, &addr);
1030 g_assert (success == 1);
1037 _nmtst_assert_ip4_address (const char *file, int line, in_addr_t addr, const char *str_expected)
1039 if (nmtst_inet4_from_string (str_expected) != addr) {
1042 g_error ("%s:%d: Unexpected IPv4 address: expected %s, got %s",
1043 file, line, str_expected ? str_expected : "0.0.0.0",
1044 inet_ntop (AF_INET, &addr, buf, sizeof (buf)));
1047 #define nmtst_assert_ip4_address(addr, str_expected) _nmtst_assert_ip4_address (__FILE__, __LINE__, addr, str_expected)
1050 _nmtst_assert_ip6_address (const char *file, int line, const struct in6_addr *addr, const char *str_expected)
1052 struct in6_addr any = in6addr_any;
1057 if (memcmp (nmtst_inet6_from_string (str_expected), addr, sizeof (*addr)) != 0) {
1060 g_error ("%s:%d: Unexpected IPv6 address: expected %s, got %s",
1061 file, line, str_expected ? str_expected : "::",
1062 inet_ntop (AF_INET6, addr, buf, sizeof (buf)));
1065 #define nmtst_assert_ip6_address(addr, str_expected) _nmtst_assert_ip6_address (__FILE__, __LINE__, addr, str_expected)
1067 #define nmtst_spawn_sync(working_directory, standard_out, standard_err, assert_exit_status, ...) \
1068 __nmtst_spawn_sync (working_directory, standard_out, standard_err, assert_exit_status, ##__VA_ARGS__, NULL)
1070 __nmtst_spawn_sync (const char *working_directory, char **standard_out, char **standard_err, int assert_exit_status, ...) G_GNUC_NULL_TERMINATED;
1072 __nmtst_spawn_sync (const char *working_directory, char **standard_out, char **standard_err, int assert_exit_status, ...)
1074 gint exit_status = 0;
1075 GError *error = NULL;
1078 GPtrArray *argv = g_ptr_array_new ();
1081 va_start (va_args, assert_exit_status);
1082 while ((arg = va_arg (va_args, char *)))
1083 g_ptr_array_add (argv, arg);
1086 g_assert (argv->len >= 1);
1087 g_ptr_array_add (argv, NULL);
1089 success = g_spawn_sync (working_directory,
1090 (char**) argv->pdata,
1092 0 /*G_SPAWN_DEFAULT*/,
1100 g_error ("nmtst_spawn_sync(%s): %s", ((char **) argv->pdata)[0], error->message);
1103 g_assert (!standard_out || *standard_out);
1104 g_assert (!standard_err || *standard_err);
1106 if (assert_exit_status != -1) {
1107 /* exit status is a guint8 on success. Set @assert_exit_status to -1
1108 * not to check for the exit status. */
1109 g_assert (WIFEXITED (exit_status));
1110 g_assert_cmpint (WEXITSTATUS (exit_status), ==, assert_exit_status);
1113 g_ptr_array_free (argv, TRUE);
1117 /*******************************************************************************/
1119 inline static char *
1120 nmtst_file_resolve_relative_path (const char *rel, const char *cwd)
1122 gs_free char *cwd_free = NULL;
1124 g_assert (rel && *rel);
1126 if (g_path_is_absolute (rel))
1127 return g_strdup (rel);
1130 cwd = cwd_free = g_get_current_dir ();
1131 return g_build_filename (cwd, rel, NULL);
1135 _nmtst_assert_resolve_relative_path_equals (const char *f1, const char *f2, const char *file, int line)
1137 gs_free char *p1 = NULL, *p2 = NULL;
1139 p1 = nmtst_file_resolve_relative_path (f1, NULL);
1140 p2 = nmtst_file_resolve_relative_path (f2, NULL);
1141 g_assert (p1 && *p1);
1143 /* Fixme: later we might need to coalesce repeated '/', "./", and "../".
1144 * For now, it's good enough. */
1145 if (g_strcmp0 (p1, p2) != 0)
1146 g_error ("%s:%d : filenames don't match \"%s\" vs. \"%s\" // \"%s\" - \"%s\"", file, line, f1, f2, p1, p2);
1148 #define nmtst_assert_resolve_relative_path_equals(f1, f2) _nmtst_assert_resolve_relative_path_equals (f1, f2, __FILE__, __LINE__);
1150 /*******************************************************************************/
1152 #ifdef __NETWORKMANAGER_PLATFORM_H__
1154 inline static NMPlatformIP6Address *
1155 nmtst_platform_ip6_address (const char *address, const char *peer_address, guint plen)
1157 static NMPlatformIP6Address addr;
1159 memset (&addr, 0, sizeof (addr));
1160 addr.address = *nmtst_inet6_from_string (address);
1161 addr.peer_address = *nmtst_inet6_from_string (peer_address);
1167 inline static NMPlatformIP6Address *
1168 nmtst_platform_ip6_address_full (const char *address, const char *peer_address, guint plen,
1169 int ifindex, NMIPConfigSource source, guint32 timestamp,
1170 guint32 lifetime, guint32 preferred, guint32 flags)
1172 NMPlatformIP6Address *addr = nmtst_platform_ip6_address (address, peer_address, plen);
1174 addr->ifindex = ifindex;
1175 addr->source = source;
1176 addr->timestamp = timestamp;
1177 addr->lifetime = lifetime;
1178 addr->preferred = preferred;
1179 addr->n_ifa_flags = flags;
1184 inline static NMPlatformIP4Route *
1185 nmtst_platform_ip4_route (const char *network, guint plen, const char *gateway)
1187 static NMPlatformIP4Route route;
1189 memset (&route, 0, sizeof (route));
1190 route.network = nmtst_inet4_from_string (network);
1192 route.gateway = nmtst_inet4_from_string (gateway);
1197 inline static NMPlatformIP4Route *
1198 nmtst_platform_ip4_route_full (const char *network, guint plen, const char *gateway,
1199 int ifindex, NMIPConfigSource source,
1200 guint metric, guint mss,
1202 const char *pref_src)
1204 NMPlatformIP4Route *route = nmtst_platform_ip4_route (network, plen, gateway);
1206 route->ifindex = ifindex;
1207 route->source = source;
1208 route->metric = metric;
1210 route->scope_inv = nm_platform_route_scope_inv (scope);
1211 route->pref_src = nmtst_inet4_from_string (pref_src);
1216 inline static NMPlatformIP6Route *
1217 nmtst_platform_ip6_route (const char *network, guint plen, const char *gateway)
1219 static NMPlatformIP6Route route;
1221 memset (&route, 0, sizeof (route));
1222 route.network = *nmtst_inet6_from_string (network);
1224 route.gateway = *nmtst_inet6_from_string (gateway);
1229 inline static NMPlatformIP6Route *
1230 nmtst_platform_ip6_route_full (const char *network, guint plen, const char *gateway,
1231 int ifindex, NMIPConfigSource source,
1232 guint metric, guint mss)
1234 NMPlatformIP6Route *route = nmtst_platform_ip6_route (network, plen, gateway);
1236 route->ifindex = ifindex;
1237 route->source = source;
1238 route->metric = metric;
1245 _nmtst_platform_ip4_routes_equal_sort (gconstpointer a, gconstpointer b, gpointer user_data)
1247 return nm_platform_ip4_route_cmp ((const NMPlatformIP4Route *) a, (const NMPlatformIP4Route *) b);
1251 nmtst_platform_ip4_routes_equal (const NMPlatformIP4Route *a, const NMPlatformIP4Route *b, gsize len, gboolean ignore_order)
1254 gs_free const NMPlatformIP4Route *c_a = NULL, *c_b = NULL;
1260 a = c_a = g_memdup (a, sizeof (NMPlatformIP4Route) * len);
1261 b = c_b = g_memdup (b, sizeof (NMPlatformIP4Route) * len);
1262 g_qsort_with_data (c_a, len, sizeof (NMPlatformIP4Route), _nmtst_platform_ip4_routes_equal_sort, NULL);
1263 g_qsort_with_data (c_b, len, sizeof (NMPlatformIP4Route), _nmtst_platform_ip4_routes_equal_sort, NULL);
1266 for (i = 0; i < len; i++) {
1267 if (nm_platform_ip4_route_cmp (&a[i], &b[i]) != 0) {
1268 char buf[sizeof (_nm_utils_to_string_buffer)];
1270 g_error ("Error comparing IPv4 route[%lu]: %s vs %s", (long unsigned) i,
1271 nm_platform_ip4_route_to_string (&a[i], NULL, 0),
1272 nm_platform_ip4_route_to_string (&b[i], buf, sizeof (buf)));
1273 g_assert_not_reached ();
1279 _nmtst_platform_ip6_routes_equal_sort (gconstpointer a, gconstpointer b, gpointer user_data)
1281 return nm_platform_ip6_route_cmp ((const NMPlatformIP6Route *) a, (const NMPlatformIP6Route *) b);
1285 nmtst_platform_ip6_routes_equal (const NMPlatformIP6Route *a, const NMPlatformIP6Route *b, gsize len, gboolean ignore_order)
1288 gs_free const NMPlatformIP6Route *c_a = NULL, *c_b = NULL;
1294 a = c_a = g_memdup (a, sizeof (NMPlatformIP6Route) * len);
1295 b = c_b = g_memdup (b, sizeof (NMPlatformIP6Route) * len);
1296 g_qsort_with_data (c_a, len, sizeof (NMPlatformIP6Route), _nmtst_platform_ip6_routes_equal_sort, NULL);
1297 g_qsort_with_data (c_b, len, sizeof (NMPlatformIP6Route), _nmtst_platform_ip6_routes_equal_sort, NULL);
1300 for (i = 0; i < len; i++) {
1301 if (nm_platform_ip6_route_cmp (&a[i], &b[i]) != 0) {
1302 char buf[sizeof (_nm_utils_to_string_buffer)];
1304 g_error ("Error comparing IPv6 route[%lu]: %s vs %s", (long unsigned) i,
1305 nm_platform_ip6_route_to_string (&a[i], NULL, 0),
1306 nm_platform_ip6_route_to_string (&b[i], buf, sizeof (buf)));
1307 g_assert_not_reached ();
1315 #ifdef __NETWORKMANAGER_IP4_CONFIG_H__
1317 inline static NMIP4Config *
1318 nmtst_ip4_config_clone (NMIP4Config *config)
1320 NMIP4Config *copy = nm_ip4_config_new (-1);
1324 nm_ip4_config_replace (copy, config, NULL);
1331 #ifdef __NETWORKMANAGER_IP6_CONFIG_H__
1333 inline static NMIP6Config *
1334 nmtst_ip6_config_clone (NMIP6Config *config)
1336 NMIP6Config *copy = nm_ip6_config_new (-1);
1340 nm_ip6_config_replace (copy, config, NULL);
1346 #ifdef NM_SETTING_IP_CONFIG_H
1348 nmtst_setting_ip_config_add_address (NMSettingIPConfig *s_ip,
1349 const char *address,
1357 if (nm_utils_ipaddr_valid (AF_INET, address))
1359 else if (nm_utils_ipaddr_valid (AF_INET6, address))
1362 g_assert_not_reached ();
1364 addr = nm_ip_address_new (family, address, prefix, NULL);
1366 g_assert (nm_setting_ip_config_add_address (s_ip, addr));
1367 nm_ip_address_unref (addr);
1371 nmtst_setting_ip_config_add_route (NMSettingIPConfig *s_ip,
1374 const char *next_hop,
1382 if (nm_utils_ipaddr_valid (AF_INET, dest))
1384 else if (nm_utils_ipaddr_valid (AF_INET6, dest))
1387 g_assert_not_reached ();
1389 route = nm_ip_route_new (family, dest, prefix, next_hop, metric, NULL);
1391 g_assert (nm_setting_ip_config_add_route (s_ip, route));
1392 nm_ip_route_unref (route);
1394 #endif /* NM_SETTING_IP_CONFIG_H */
1396 #if (defined(__NM_SIMPLE_CONNECTION_H__) && defined(__NM_SETTING_CONNECTION_H__)) || (defined(NM_CONNECTION_H))
1398 inline static NMConnection *
1399 nmtst_clone_connection (NMConnection *connection)
1401 g_assert (NM_IS_CONNECTION (connection));
1403 #if defined(__NM_SIMPLE_CONNECTION_H__)
1404 return nm_simple_connection_new_clone (connection);
1406 return nm_connection_duplicate (connection);
1410 inline static NMConnection *
1411 nmtst_create_minimal_connection (const char *id, const char *uuid, const char *type, NMSettingConnection **out_s_con)
1414 NMSetting *s_base = NULL;
1415 NMSettingConnection *s_con;
1416 gs_free char *uuid_free = NULL;
1421 g_assert (nm_utils_is_uuid (uuid));
1423 uuid = uuid_free = nm_utils_uuid_generate ();
1428 #if defined(__NM_SIMPLE_CONNECTION_H__)
1429 type_g = nm_setting_lookup_type (type);
1431 type_g = nm_connection_lookup_setting_type (type);
1434 g_assert (type_g != G_TYPE_INVALID);
1436 s_base = g_object_new (type_g, NULL);
1437 g_assert (NM_IS_SETTING (s_base));
1440 #if defined(__NM_SIMPLE_CONNECTION_H__)
1441 con = nm_simple_connection_new ();
1443 con = nm_connection_new ();
1446 s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ());
1448 g_object_set (s_con,
1449 NM_SETTING_CONNECTION_ID, id,
1450 NM_SETTING_CONNECTION_UUID, uuid,
1451 NM_SETTING_CONNECTION_TYPE, type,
1453 nm_connection_add_setting (con, NM_SETTING (s_con));
1456 nm_connection_add_setting (con, s_base);
1463 inline static gboolean
1464 _nmtst_connection_normalize_v (NMConnection *connection, va_list args)
1466 GError *error = NULL;
1468 gboolean was_modified = FALSE;
1469 GHashTable *parameters = NULL;
1472 g_assert (NM_IS_CONNECTION (connection));
1474 while ((p_name = va_arg (args, const char *))) {
1476 parameters = g_hash_table_new (g_str_hash, g_str_equal);
1477 g_hash_table_insert (parameters, (gpointer *) p_name, va_arg (args, gpointer));
1480 success = nm_connection_normalize (connection,
1484 g_assert_no_error (error);
1488 g_hash_table_destroy (parameters);
1490 return was_modified;
1493 inline static gboolean
1494 _nmtst_connection_normalize (NMConnection *connection, ...)
1496 gboolean was_modified;
1499 va_start (args, connection);
1500 was_modified = _nmtst_connection_normalize_v (connection, args);
1503 return was_modified;
1505 #define nmtst_connection_normalize(connection, ...) \
1506 _nmtst_connection_normalize(connection, ##__VA_ARGS__, NULL)
1508 inline static NMConnection *
1509 _nmtst_connection_duplicate_and_normalize (NMConnection *connection, ...)
1511 gboolean was_modified;
1514 connection = nmtst_clone_connection (connection);
1516 va_start (args, connection);
1517 was_modified = _nmtst_connection_normalize_v (connection, args);
1522 #define nmtst_connection_duplicate_and_normalize(connection, ...) \
1523 _nmtst_connection_duplicate_and_normalize(connection, ##__VA_ARGS__, NULL)
1526 nmtst_assert_connection_equals (NMConnection *a, gboolean normalize_a, NMConnection *b, gboolean normalize_b)
1529 gs_unref_object NMConnection *a2 = NULL;
1530 gs_unref_object NMConnection *b2 = NULL;
1531 GHashTable *out_settings = NULL;
1533 g_assert (NM_IS_CONNECTION (a));
1534 g_assert (NM_IS_CONNECTION (b));
1537 a = a2 = nmtst_connection_duplicate_and_normalize (a);
1539 b = b2 = nmtst_connection_duplicate_and_normalize (b);
1541 compare = nm_connection_diff (a, b, NM_SETTING_COMPARE_FLAG_EXACT, &out_settings);
1542 if (!compare || out_settings) {
1543 const char *name, *pname;
1544 GHashTable *setting;
1545 GHashTableIter iter, iter2;
1547 __NMTST_LOG (g_message, ">>> ASSERTION nmtst_assert_connection_equals() fails");
1549 g_hash_table_iter_init (&iter, out_settings);
1550 while (g_hash_table_iter_next (&iter, (gpointer *) &name, (gpointer *) &setting)) {
1551 __NMTST_LOG (g_message, ">>> differences in setting '%s':", name);
1553 g_hash_table_iter_init (&iter2, setting);
1554 while (g_hash_table_iter_next (&iter2, (gpointer *) &pname, NULL))
1555 __NMTST_LOG (g_message, ">>> differences in setting '%s.%s'", name, pname);
1559 #ifdef __NM_KEYFILE_INTERNAL_H__
1561 gs_unref_keyfile GKeyFile *kf_a = NULL, *kf_b = NULL;
1562 gs_free char *str_a = NULL, *str_b = NULL;
1564 kf_a = nm_keyfile_write (a, NULL, NULL, NULL);
1565 kf_b = nm_keyfile_write (b, NULL, NULL, NULL);
1568 str_a = g_key_file_to_data (kf_a, NULL, NULL);
1570 str_b = g_key_file_to_data (kf_b, NULL, NULL);
1572 __NMTST_LOG (g_message, ">>> Connection A as kf (*WARNING: keyfile representation might not show the difference*):\n%s", str_a);
1573 __NMTST_LOG (g_message, ">>> Connection B as kf (*WARNING: keyfile representation might not show the difference*):\n%s", str_b);
1578 g_assert (!out_settings);
1580 compare = nm_connection_compare (a, b, NM_SETTING_COMPARE_FLAG_EXACT);
1585 nmtst_assert_connection_verifies (NMConnection *con)
1587 /* assert that the connection does verify, it might be normaliziable or not */
1588 GError *error = NULL;
1591 g_assert (NM_IS_CONNECTION (con));
1593 success = nm_connection_verify (con, &error);
1594 g_assert_no_error (error);
1599 nmtst_assert_connection_verifies_without_normalization (NMConnection *con)
1601 /* assert that the connection verifies and does not need any normalization */
1602 GError *error = NULL;
1604 gboolean was_modified = FALSE;
1605 gs_unref_object NMConnection *clone = NULL;
1607 clone = nmtst_clone_connection (con);
1609 nmtst_assert_connection_verifies (con);
1611 success = nm_connection_normalize (clone, NULL, &was_modified, &error);
1612 g_assert_no_error (error);
1614 nmtst_assert_connection_equals (con, FALSE, clone, FALSE);
1615 g_assert (!was_modified);
1619 nmtst_assert_connection_verifies_and_normalizable (NMConnection *con)
1621 /* assert that the connection does verify, but normalization still modifies it */
1622 GError *error = NULL;
1624 gboolean was_modified = FALSE;
1625 gs_unref_object NMConnection *clone = NULL;
1627 clone = nmtst_clone_connection (con);
1629 nmtst_assert_connection_verifies (con);
1631 success = nm_connection_normalize (clone, NULL, &was_modified, &error);
1632 g_assert_no_error (error);
1634 g_assert (was_modified);
1637 nmtst_assert_connection_verifies_without_normalization (clone);
1641 nmtst_assert_connection_verifies_after_normalization (NMConnection *con,
1642 GQuark expect_error_domain,
1643 gint expect_error_code)
1645 /* assert that the connection does not verify, but normalization does fix it */
1646 GError *error = NULL;
1648 gboolean was_modified = FALSE;
1649 gs_unref_object NMConnection *clone = NULL;
1651 clone = nmtst_clone_connection (con);
1653 success = nm_connection_verify (con, &error);
1654 nmtst_assert_error (error, expect_error_domain, expect_error_code, NULL);
1655 g_assert (!success);
1656 g_clear_error (&error);
1658 success = nm_connection_normalize (clone, NULL, &was_modified, &error);
1659 g_assert_no_error (error);
1661 g_assert (was_modified);
1664 nmtst_assert_connection_verifies_without_normalization (clone);
1668 nmtst_assert_connection_unnormalizable (NMConnection *con,
1669 GQuark expect_error_domain,
1670 gint expect_error_code)
1672 /* assert that the connection does not verify, and it cannot be fixed by normalization */
1674 GError *error = NULL;
1676 gboolean was_modified = FALSE;
1677 gs_unref_object NMConnection *clone = NULL;
1679 clone = nmtst_clone_connection (con);
1681 success = nm_connection_verify (con, &error);
1682 nmtst_assert_error (error, expect_error_domain, expect_error_code, NULL);
1683 g_assert (!success);
1684 g_clear_error (&error);
1686 success = nm_connection_normalize (clone, NULL, &was_modified, &error);
1687 nmtst_assert_error (error, expect_error_domain, expect_error_code, NULL);
1688 g_assert (!success);
1689 g_assert (!was_modified);
1690 nmtst_assert_connection_equals (con, FALSE, clone, FALSE);
1691 g_clear_error (&error);
1695 nmtst_assert_setting_verifies (NMSetting *setting)
1697 /* assert that the setting verifies without an error */
1699 GError *error = NULL;
1702 g_assert (NM_IS_SETTING (setting));
1704 success = nm_setting_verify (setting, NULL, &error);
1705 g_assert_no_error (error);
1710 nmtst_assert_setting_verify_fails (NMSetting *setting,
1711 GQuark expect_error_domain,
1712 gint expect_error_code)
1714 /* assert that the setting verification fails */
1716 GError *error = NULL;
1719 g_assert (NM_IS_SETTING (setting));
1721 success = nm_setting_verify (setting, NULL, &error);
1722 nmtst_assert_error (error, expect_error_domain, expect_error_code, NULL);
1723 g_assert (!success);
1724 g_clear_error (&error);
1729 #ifdef __NM_UTILS_H__
1731 nmtst_assert_hwaddr_equals (gconstpointer hwaddr1, gssize hwaddr1_len, const char *expected, const char *file, int line)
1733 guint8 buf2[NM_UTILS_HWADDR_LEN_MAX];
1734 gsize hwaddr2_len = 1;
1738 g_assert (hwaddr1_len > 0 && hwaddr1_len <= NM_UTILS_HWADDR_LEN_MAX);
1740 g_assert (expected);
1741 for (p = expected; *p; p++) {
1742 if (*p == ':' || *p == '-')
1745 g_assert (hwaddr2_len <= NM_UTILS_HWADDR_LEN_MAX);
1746 g_assert (nm_utils_hwaddr_aton (expected, buf2, hwaddr2_len));
1748 /* Manually check the entire hardware address instead of using
1749 * nm_utils_hwaddr_matches() because that function doesn't compare
1750 * entire InfiniBand addresses for various (legitimate) reasons.
1752 success = (hwaddr1_len == hwaddr2_len);
1754 success = !memcmp (hwaddr1, buf2, hwaddr1_len);
1756 g_error ("assert: %s:%d: hwaddr '%s' (%zd) expected, but got %s (%zd)",
1757 file, line, expected, hwaddr2_len, nm_utils_hwaddr_ntoa (hwaddr1, hwaddr1_len), hwaddr1_len);
1760 #define nmtst_assert_hwaddr_equals(hwaddr1, hwaddr1_len, expected) \
1761 nmtst_assert_hwaddr_equals (hwaddr1, hwaddr1_len, expected, __FILE__, __LINE__)
1764 #if defined(__NM_SIMPLE_CONNECTION_H__) && defined(__NM_SETTING_CONNECTION_H__) && defined(__NM_KEYFILE_INTERNAL_H__)
1766 inline static NMConnection *
1767 nmtst_create_connection_from_keyfile (const char *keyfile_str, const char *keyfile_name, const char *base_dir)
1770 GError *error = NULL;
1774 g_assert (keyfile_str);
1776 keyfile = g_key_file_new ();
1777 success = g_key_file_load_from_data (keyfile, keyfile_str, strlen (keyfile_str), G_KEY_FILE_NONE, &error);
1778 g_assert_no_error (error);
1781 con = nm_keyfile_read (keyfile, keyfile_name, base_dir, NULL, NULL, &error);
1782 g_assert_no_error (error);
1783 g_assert (NM_IS_CONNECTION (con));
1785 g_key_file_unref (keyfile);
1787 nmtst_connection_normalize (con);
1794 #ifdef __NM_CONNECTION_H__
1796 inline static GVariant *
1797 _nmtst_variant_new_vardict (int dummy, ...)
1799 GVariantBuilder builder;
1804 g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
1806 va_start (ap, dummy);
1807 while ((name = va_arg (ap, const char *))) {
1808 variant = va_arg (ap, GVariant *);
1809 g_variant_builder_add (&builder, "{sv}", name, variant);
1813 return g_variant_builder_end (&builder);
1815 #define nmtst_variant_new_vardict(...) _nmtst_variant_new_vardict (0, __VA_ARGS__, NULL)
1817 #define nmtst_assert_variant_is_of_type(variant, type) \
1819 GVariant *_variantx = (variant); \
1821 g_assert (_variantx); \
1822 g_assert (g_variant_is_of_type (_variantx, (type))); \
1825 #define nmtst_assert_variant_uint32(variant, val) \
1827 GVariant *_variant = (variant); \
1829 nmtst_assert_variant_is_of_type (_variant, G_VARIANT_TYPE_UINT32); \
1830 g_assert_cmpint (g_variant_get_uint32 (_variant), ==, (val)); \
1833 #define nmtst_assert_variant_string(variant, str) \
1836 GVariant *_variant = (variant); \
1837 const char *_str = (str); \
1839 nmtst_assert_variant_is_of_type (_variant, G_VARIANT_TYPE_STRING); \
1841 g_assert_cmpstr (g_variant_get_string (_variant, &_l), ==, _str); \
1842 g_assert_cmpint (_l, ==, strlen (_str)); \
1846 NMTST_VARIANT_EDITOR_CONNECTION,
1847 NMTST_VARIANT_EDITOR_SETTING,
1848 NMTST_VARIANT_EDITOR_PROPERTY
1849 } NmtstVariantEditorPhase;
1851 #define NMTST_VARIANT_EDITOR(__connection_variant, __code) \
1853 GVariantIter __connection_iter, *__setting_iter; \
1854 GVariantBuilder __connection_builder, __setting_builder; \
1855 const char *__cur_setting_name, *__cur_property_name; \
1856 GVariant *__property_val; \
1857 NmtstVariantEditorPhase __phase; \
1859 g_variant_builder_init (&__connection_builder, NM_VARIANT_TYPE_CONNECTION); \
1860 g_variant_iter_init (&__connection_iter, __connection_variant); \
1862 __phase = NMTST_VARIANT_EDITOR_CONNECTION; \
1863 __cur_setting_name = NULL; \
1864 __cur_property_name = NULL; \
1866 while (g_variant_iter_next (&__connection_iter, "{&sa{sv}}", &__cur_setting_name, &__setting_iter)) { \
1867 g_variant_builder_init (&__setting_builder, NM_VARIANT_TYPE_SETTING); \
1868 __phase = NMTST_VARIANT_EDITOR_SETTING; \
1869 __cur_property_name = NULL; \
1872 while ( __cur_setting_name \
1873 && g_variant_iter_next (__setting_iter, "{&sv}", &__cur_property_name, &__property_val)) { \
1874 __phase = NMTST_VARIANT_EDITOR_PROPERTY; \
1877 if (__cur_property_name) { \
1878 g_variant_builder_add (&__setting_builder, "{sv}", \
1879 __cur_property_name, \
1882 g_variant_unref (__property_val); \
1885 if (__cur_setting_name) \
1886 g_variant_builder_add (&__connection_builder, "{sa{sv}}", __cur_setting_name, &__setting_builder); \
1888 g_variant_builder_clear (&__setting_builder); \
1889 g_variant_iter_free (__setting_iter); \
1892 g_variant_unref (__connection_variant); \
1894 __connection_variant = g_variant_builder_end (&__connection_builder); \
1897 #define NMTST_VARIANT_ADD_SETTING(__setting_name, __setting_variant) \
1899 if (__phase == NMTST_VARIANT_EDITOR_CONNECTION) \
1900 g_variant_builder_add (&__connection_builder, "{s@a{sv}}", __setting_name, __setting_variant); \
1903 #define NMTST_VARIANT_DROP_SETTING(__setting_name) \
1905 if (__phase == NMTST_VARIANT_EDITOR_SETTING && __cur_setting_name) { \
1906 if (!strcmp (__cur_setting_name, __setting_name)) \
1907 __cur_setting_name = NULL; \
1911 #define NMTST_VARIANT_ADD_PROPERTY(__setting_name, __property_name, __format_string, __value) \
1913 if (__phase == NMTST_VARIANT_EDITOR_SETTING) { \
1914 if (!strcmp (__cur_setting_name, __setting_name)) { \
1915 g_variant_builder_add (&__setting_builder, "{sv}", __property_name, \
1916 g_variant_new (__format_string, __value)); \
1921 #define NMTST_VARIANT_DROP_PROPERTY(__setting_name, __property_name) \
1923 if (__phase == NMTST_VARIANT_EDITOR_PROPERTY && __cur_property_name) { \
1924 if ( !strcmp (__cur_setting_name, __setting_name) \
1925 && !strcmp (__cur_property_name, __property_name)) \
1926 __cur_property_name = NULL; \
1930 #define NMTST_VARIANT_CHANGE_PROPERTY(__setting_name, __property_name, __format_string, __value) \
1932 NMTST_VARIANT_DROP_PROPERTY (__setting_name, __property_name); \
1933 NMTST_VARIANT_ADD_PROPERTY (__setting_name, __property_name, __format_string, __value); \
1936 #endif /* __NM_CONNECTION_H__ */
1938 #endif /* __NM_TEST_UTILS_H__ */