diff options
| author | Michael Biebl <biebl@debian.org> | 2017-11-07 00:14:39 +0100 |
|---|---|---|
| committer | Michael Biebl <biebl@debian.org> | 2017-11-07 00:14:39 +0100 |
| commit | 90e8691111889a7b5f3c812f5a41f15a8a058913 (patch) | |
| tree | f101a879eca27c34a9bfa5f3da52266b22539a36 /src/tests | |
| parent | bdb6eeb0670658255c2a4c3c501c0a27fa8cfe55 (diff) | |
New upstream version 1.9.90 upstream/1.9.90
Diffstat (limited to 'src/tests')
| -rw-r--r-- | src/tests/config/test-config.c | 36 | ||||
| -rw-r--r-- | src/tests/test-general-with-expect.c | 358 | ||||
| -rw-r--r-- | src/tests/test-general.c | 28 | ||||
| -rw-r--r-- | src/tests/test-ip4-config.c | 114 | ||||
| -rw-r--r-- | src/tests/test-ip6-config.c | 151 | ||||
| -rw-r--r-- | src/tests/test-resolvconf-capture.c | 66 | ||||
| -rw-r--r-- | src/tests/test-route-manager.c | 933 | ||||
| -rw-r--r-- | src/tests/test-systemd.c | 2 |
8 files changed, 260 insertions, 1428 deletions
diff --git a/src/tests/config/test-config.c b/src/tests/config/test-config.c index edb5c1aa..80e17d01 100644 --- a/src/tests/config/test-config.c +++ b/src/tests/config/test-config.c @@ -26,6 +26,7 @@ #include "nm-test-device.h" #include "platform/nm-fake-platform.h" #include "nm-bus-manager.h" +#include "nm-connectivity.h" #include "nm-test-utils-core.h" @@ -318,6 +319,40 @@ test_config_global_dns (void) } static void +test_config_connectivity_check (void) +{ + const char *CONFIG_INTERN = BUILDDIR"/test-connectivity-check-intern.conf"; + NMConfig *config; + NMConnectivity *connectivity; + + g_assert (g_file_set_contents (CONFIG_INTERN, "", 0, NULL)); + config = setup_config (NULL, SRCDIR "/NetworkManager.conf", CONFIG_INTERN, NULL, + "/no/such/dir", "", NULL); + connectivity = nm_connectivity_get(); + + g_assert (nm_connectivity_check_enabled (connectivity)); + + /* disable connectivity checking */ + g_test_expect_message ("NetworkManager", G_LOG_LEVEL_INFO, "*config: signal *"); + nm_config_set_connectivity_check_enabled (config, FALSE); + g_test_assert_expected_messages (); + + g_assert (!nm_connectivity_check_enabled (connectivity)); + + /* re-enable connectivity checking */ + g_test_expect_message ("NetworkManager", G_LOG_LEVEL_INFO, "*config: signal *"); + nm_config_set_connectivity_check_enabled (config, TRUE); + g_test_assert_expected_messages (); + + g_assert (nm_connectivity_check_enabled (connectivity)); + + g_object_unref (connectivity); + g_object_unref (config); + + g_assert (remove (CONFIG_INTERN) == 0); +} + +static void test_config_no_auto_default (void) { NMConfig *config; @@ -1018,6 +1053,7 @@ main (int argc, char **argv) g_test_add_func ("/config/set-values", test_config_set_values); g_test_add_func ("/config/global-dns", test_config_global_dns); + g_test_add_func ("/config/connectivity-check", test_config_connectivity_check); g_test_add_func ("/config/signal", test_config_signal); diff --git a/src/tests/test-general-with-expect.c b/src/tests/test-general-with-expect.c index 492e1b1f..c5d26163 100644 --- a/src/tests/test-general-with-expect.c +++ b/src/tests/test-general-with-expect.c @@ -29,7 +29,6 @@ #include <fcntl.h> #include "NetworkManagerUtils.h" -#include "nm-multi-index.h" #include "nm-test-utils-core.h" @@ -499,362 +498,6 @@ test_nm_ethernet_address_is_valid (void) /*****************************************************************************/ -typedef struct { - union { - NMMultiIndexId id_base; - guint bucket; - }; -} NMMultiIndexIdTest; - -typedef struct { - guint64 buckets; - gpointer ptr_value; -} NMMultiIndexTestValue; - -static gboolean -_mi_value_bucket_has (const NMMultiIndexTestValue *value, guint bucket) -{ - g_assert (value); - g_assert (bucket < 64); - - return (value->buckets & (((guint64) 0x01) << bucket)) != 0; -} - -static gboolean -_mi_value_bucket_set (NMMultiIndexTestValue *value, guint bucket) -{ - g_assert (value); - g_assert (bucket < 64); - - if (_mi_value_bucket_has (value, bucket)) - return FALSE; - - value->buckets |= (((guint64) 0x01) << bucket); - return TRUE; -} - -static gboolean -_mi_value_bucket_unset (NMMultiIndexTestValue *value, guint bucket) -{ - g_assert (value); - g_assert (bucket < 64); - - if (!_mi_value_bucket_has (value, bucket)) - return FALSE; - - value->buckets &= ~(((guint64) 0x01) << bucket); - return TRUE; -} - -static guint -_mi_idx_hash (const NMMultiIndexIdTest *id) -{ - g_assert (id && id->bucket < 64); - return id->bucket; -} - -static gboolean -_mi_idx_equal (const NMMultiIndexIdTest *a, const NMMultiIndexIdTest *b) -{ - g_assert (a && a->bucket < 64); - g_assert (b && b->bucket < 64); - - return a->bucket == b->bucket; -} - -static NMMultiIndexIdTest * -_mi_idx_clone (const NMMultiIndexIdTest *id) -{ - NMMultiIndexIdTest *n; - - g_assert (id && id->bucket < 64); - - n = g_new0 (NMMultiIndexIdTest, 1); - n->bucket = id->bucket; - return n; -} - -static void -_mi_idx_destroy (NMMultiIndexIdTest *id) -{ - g_assert (id && id->bucket < 64); - g_free (id); -} - -static NMMultiIndexTestValue * -_mi_create_array (guint num_values) -{ - NMMultiIndexTestValue *array = g_new0 (NMMultiIndexTestValue, num_values); - guint i; - - g_assert (num_values > 0); - - for (i = 0; i < num_values; i++) { - array[i].buckets = 0; - array[i].ptr_value = GUINT_TO_POINTER (i + 1); - } - return array; -} - -typedef struct { - guint num_values; - guint num_buckets; - NMMultiIndexTestValue *array; - int test_idx; -} NMMultiIndexAssertData; - -static gboolean -_mi_assert_index_equals_array_cb (const NMMultiIndexIdTest *id, void *const* values, guint len, NMMultiIndexAssertData *data) -{ - guint i; - gboolean has_test_idx = FALSE; - - g_assert (id && id->bucket < 64); - g_assert (data); - g_assert (values); - g_assert (len > 0); - g_assert (values[len] == NULL); - g_assert (data->test_idx >= -1 || data->test_idx < data->num_buckets); - - g_assert (id->bucket < data->num_buckets); - - for (i = 0; i < data->num_values; i++) - g_assert (!_mi_value_bucket_has (&data->array[i], id->bucket)); - - for (i = 0; i < len; i++) { - guint vi = GPOINTER_TO_UINT (values[i]); - - g_assert (vi >= 1); - g_assert (vi <= data->num_values); - vi--; - if (data->test_idx == vi) - has_test_idx = TRUE; - g_assert (data->array[vi].ptr_value == values[i]); - if (!_mi_value_bucket_set (&data->array[vi], id->bucket)) - g_assert_not_reached (); - } - g_assert ((data->test_idx == -1 && !has_test_idx) || has_test_idx); - return TRUE; -} - -static void -_mi_assert_index_equals_array (guint num_values, guint num_buckets, int test_idx, const NMMultiIndexTestValue *array, const NMMultiIndex *index) -{ - NMMultiIndexAssertData data = { - .num_values = num_values, - .num_buckets = num_buckets, - .test_idx = test_idx, - }; - NMMultiIndexIter iter; - const NMMultiIndexIdTest *id; - void *const* values; - guint len; - NMMultiIndexTestValue *v; - - data.array = _mi_create_array (num_values); - v = test_idx >= 0 ? data.array[test_idx].ptr_value : NULL; - nm_multi_index_foreach (index, v, (NMMultiIndexFuncForeach) _mi_assert_index_equals_array_cb, &data); - if (test_idx >= 0) - g_assert (memcmp (&data.array[test_idx], &array[test_idx], sizeof (NMMultiIndexTestValue)) == 0); - else - g_assert (memcmp (data.array, array, sizeof (NMMultiIndexTestValue) * num_values) == 0); - g_free (data.array); - - - data.array = _mi_create_array (num_values); - v = test_idx >= 0 ? data.array[test_idx].ptr_value : NULL; - nm_multi_index_iter_init (&iter, index, v); - while (nm_multi_index_iter_next (&iter, (gpointer) &id, &values, &len)) - _mi_assert_index_equals_array_cb (id, values, len, &data); - if (test_idx >= 0) - g_assert (memcmp (&data.array[test_idx], &array[test_idx], sizeof (NMMultiIndexTestValue)) == 0); - else - g_assert (memcmp (data.array, array, sizeof (NMMultiIndexTestValue) * num_values) == 0); - g_free (data.array); -} - -typedef enum { - MI_OP_ADD, - MI_OP_REMOVE, - MI_OP_MOVE, -} NMMultiIndexOperation; - -static void -_mi_rebucket (GRand *rand, guint num_values, guint num_buckets, NMMultiIndexOperation op, guint bucket, guint bucket_old, guint array_idx, NMMultiIndexTestValue *array, NMMultiIndex *index) -{ - NMMultiIndexTestValue *v; - NMMultiIndexIdTest id, id_old; - const NMMultiIndexIdTest *id_reverse; - guint64 buckets_old; - guint i; - gboolean had_bucket, had_bucket_old; - - g_assert (array_idx < num_values); - g_assert (bucket < (int) num_buckets); - - v = &array[array_idx]; - - buckets_old = v->buckets; - if (op == MI_OP_MOVE) - had_bucket_old = _mi_value_bucket_has (v, bucket_old); - else - had_bucket_old = FALSE; - had_bucket = _mi_value_bucket_has (v, bucket); - - switch (op) { - - case MI_OP_ADD: - _mi_value_bucket_set (v, bucket); - id.bucket = bucket; - if (nm_multi_index_add (index, &id.id_base, v->ptr_value)) - g_assert (!had_bucket); - else - g_assert (had_bucket); - break; - - case MI_OP_REMOVE: - _mi_value_bucket_unset (v, bucket); - id.bucket = bucket; - if (nm_multi_index_remove (index, &id.id_base, v->ptr_value)) - g_assert (had_bucket); - else - g_assert (!had_bucket); - break; - - case MI_OP_MOVE: - - _mi_value_bucket_unset (v, bucket_old); - _mi_value_bucket_set (v, bucket); - - id.bucket = bucket; - id_old.bucket = bucket_old; - - if (nm_multi_index_move (index, &id_old.id_base, &id.id_base, v->ptr_value)) { - if (bucket == bucket_old) - g_assert (had_bucket_old && had_bucket); - else - g_assert (had_bucket_old && !had_bucket); - } else { - if (bucket == bucket_old) - g_assert (!had_bucket_old && !had_bucket); - else - g_assert (!had_bucket_old || had_bucket); - } - break; - - default: - g_assert_not_reached (); - } - -#if 0 - g_print (">>> rebucket: idx=%3u, op=%3s, bucket=%3i%c -> %3i%c, buckets=%08llx -> %08llx %s\n", array_idx, - op == MI_OP_ADD ? "ADD" : (op == MI_OP_REMOVE ? "REM" : "MOV"), - bucket_old, had_bucket_old ? '*' : ' ', - bucket, had_bucket ? '*' : ' ', - (unsigned long long) buckets_old, (unsigned long long) v->buckets, - buckets_old != v->buckets ? "(changed)" : "(unchanged)"); -#endif - - id_reverse = (const NMMultiIndexIdTest *) nm_multi_index_lookup_first_by_value (index, v->ptr_value); - if (id_reverse) - g_assert (_mi_value_bucket_has (v, id_reverse->bucket)); - else - g_assert (v->buckets == 0); - - for (i = 0; i < 64; i++) { - id.bucket = i; - if (nm_multi_index_contains (index, &id.id_base, v->ptr_value)) - g_assert (_mi_value_bucket_has (v, i)); - else - g_assert (!_mi_value_bucket_has (v, i)); - } - - _mi_assert_index_equals_array (num_values, num_buckets, -1, array, index); - _mi_assert_index_equals_array (num_values, num_buckets, array_idx, array, index); - _mi_assert_index_equals_array (num_values, num_buckets, g_rand_int_range (rand, 0, num_values), array, index); -} - -static void -_mi_test_run (guint num_values, guint num_buckets) -{ - NMMultiIndex *index = nm_multi_index_new ((NMMultiIndexFuncHash) _mi_idx_hash, - (NMMultiIndexFuncEqual) _mi_idx_equal, - (NMMultiIndexFuncClone) _mi_idx_clone, - (NMMultiIndexFuncDestroy) _mi_idx_destroy); - gs_free NMMultiIndexTestValue *array = _mi_create_array (num_values); - GRand *rand = nmtst_get_rand (); - guint i, i_rd, i_idx, i_bucket; - guint num_buckets_all = num_values * num_buckets; - - g_assert (array[0].ptr_value == GUINT_TO_POINTER (1)); - - _mi_assert_index_equals_array (num_values, num_buckets, -1, array, index); - - _mi_rebucket (rand, num_values, num_buckets, MI_OP_ADD, 0, 0, 0, array, index); - _mi_rebucket (rand, num_values, num_buckets, MI_OP_REMOVE, 0, 0, 0, array, index); - - if (num_buckets >= 3) { - _mi_rebucket (rand, num_values, num_buckets, MI_OP_ADD, 0, 0, 0, array, index); - _mi_rebucket (rand, num_values, num_buckets, MI_OP_MOVE, 2, 0, 0, array, index); - _mi_rebucket (rand, num_values, num_buckets, MI_OP_REMOVE, 2, 0, 0, array, index); - } - - g_assert (nm_multi_index_get_num_groups (index) == 0); - - /* randomly change the bucket of entries. */ - for (i = 0; i < 5 * num_values; i++) { - guint array_idx = g_rand_int_range (rand, 0, num_values); - guint bucket = g_rand_int_range (rand, 0, num_buckets); - NMMultiIndexOperation op = g_rand_int_range (rand, 0, MI_OP_MOVE + 1); - guint bucket_old = 0; - - if (op == MI_OP_MOVE) { - if ((g_rand_int (rand) % 2) && array[array_idx].buckets != 0) { - guint64 b; - - /* choose the highest (existing) bucket. */ - bucket_old = 0; - for (b = array[array_idx].buckets; b; b >>= 1) - bucket_old++; - } else { - /* choose a random bucket (even if the item is currently not in that bucket). */ - bucket_old = g_rand_int_range (rand, 0, num_buckets); - } - } - - _mi_rebucket (rand, num_values, num_buckets, op, bucket, bucket_old, array_idx, array, index); - } - - /* remove all elements from all buckets */ - i_rd = g_rand_int (rand); - for (i = 0; i < num_buckets_all; i++) { - i_rd = (i_rd + 101) % num_buckets_all; - i_idx = i_rd / num_buckets; - i_bucket = i_rd % num_buckets; - - if (_mi_value_bucket_has (&array[i_idx], i_bucket)) - _mi_rebucket (rand, num_values, num_buckets, MI_OP_REMOVE, i_bucket, 0, i_idx, array, index); - } - - g_assert (nm_multi_index_get_num_groups (index) == 0); - nm_multi_index_free (index); -} - -static void -test_nm_multi_index (void) -{ - guint i, j; - - for (i = 1; i < 7; i++) { - for (j = 1; j < 6; j++) - _mi_test_run (i, j); - } - _mi_test_run (50, 3); - _mi_test_run (50, 18); -} - -/*****************************************************************************/ - static void test_nm_utils_new_vlan_name (void) { @@ -909,7 +552,6 @@ main (int argc, char **argv) g_test_add_func ("/general/nm_utils_kill_child", test_nm_utils_kill_child); g_test_add_func ("/general/nm_utils_array_remove_at_indexes", test_nm_utils_array_remove_at_indexes); g_test_add_func ("/general/nm_ethernet_address_is_valid", test_nm_ethernet_address_is_valid); - g_test_add_func ("/general/nm_multi_index", test_nm_multi_index); g_test_add_func ("/general/nm_utils_new_vlan_name", test_nm_utils_new_vlan_name); return g_test_run (); diff --git a/src/tests/test-general.c b/src/tests/test-general.c index 37bbc5ca..d36a26ef 100644 --- a/src/tests/test-general.c +++ b/src/tests/test-general.c @@ -272,6 +272,32 @@ test_nm_utils_log_connection_diff (void) /*****************************************************************************/ +static void +do_test_sysctl_ip_conf (int addr_family, + const char *iface, + const char *property) +{ + char path[NM_UTILS_SYSCTL_IP_CONF_PATH_BUFSIZE]; + const char *pp; + + pp = nm_utils_sysctl_ip_conf_path (addr_family, path, iface, property); + g_assert (pp == path); + g_assert (path[0] == '/'); + + g_assert (nm_utils_sysctl_ip_conf_is_path (addr_family, path, iface, property)); + g_assert (nm_utils_sysctl_ip_conf_is_path (addr_family, path, NULL, property)); +} + +static void +test_nm_utils_sysctl_ip_conf_path (void) +{ + do_test_sysctl_ip_conf (AF_INET6, "a", "mtu"); + do_test_sysctl_ip_conf (AF_INET6, "eth0", "mtu"); + do_test_sysctl_ip_conf (AF_INET6, "e23456789012345", "mtu"); +} + +/*****************************************************************************/ + static NMConnection * _match_connection_new (void) { @@ -1716,6 +1742,8 @@ main (int argc, char **argv) g_test_add_func ("/general/nm_utils_ip6_address_same_prefix", test_nm_utils_ip6_address_same_prefix); g_test_add_func ("/general/nm_utils_log_connection_diff", test_nm_utils_log_connection_diff); + g_test_add_func ("/general/nm_utils_sysctl_ip_conf_path", test_nm_utils_sysctl_ip_conf_path); + g_test_add_func ("/general/exp10", test_nm_utils_exp10); g_test_add_func ("/general/connection-match/basic", test_connection_match_basic); diff --git a/src/tests/test-ip4-config.c b/src/tests/test-ip4-config.c index bff715e0..649b443f 100644 --- a/src/tests/test-ip4-config.c +++ b/src/tests/test-ip4-config.c @@ -36,18 +36,27 @@ build_test_config (void) NMPlatformIP4Route route; /* Build up the config to subtract */ - config = nm_ip4_config_new (1); + config = nmtst_ip4_config_new (1); addr = *nmtst_platform_ip4_address ("192.168.1.10", "1.2.3.4", 24); nm_ip4_config_add_address (config, &addr); route = *nmtst_platform_ip4_route ("10.0.0.0", 8, "192.168.1.1"); - nm_ip4_config_add_route (config, &route); + nm_ip4_config_add_route (config, &route, NULL); route = *nmtst_platform_ip4_route ("172.16.0.0", 16, "192.168.1.1"); - nm_ip4_config_add_route (config, &route); + nm_ip4_config_add_route (config, &route, NULL); - nm_ip4_config_set_gateway (config, nmtst_inet4_from_string ("192.168.1.1")); + { + const NMPlatformIP4Route r = { + .rt_source = NM_IP_CONFIG_SOURCE_DHCP, + .gateway = nmtst_inet4_from_string ("192.168.1.1"), + .table_coerced = 0, + .metric = 100, + }; + + nm_ip4_config_add_route (config, &r, NULL); + } nm_ip4_config_add_nameserver (config, nmtst_inet4_from_string ("4.2.2.1")); nm_ip4_config_add_nameserver (config, nmtst_inet4_from_string ("4.2.2.2")); @@ -75,7 +84,7 @@ test_subtract (void) const NMPlatformIP4Route *test_route; const char *expected_addr = "192.168.1.12"; guint32 expected_addr_plen = 24; - const char *expected_route_dest = "8.7.6.5"; + const char *expected_route_dest = "8.0.0.0"; guint32 expected_route_plen = 8; const char *expected_route_next_hop = "192.168.1.1"; guint32 expected_ns1 = nmtst_inet4_from_string ("8.8.8.8"); @@ -84,7 +93,6 @@ test_subtract (void) const char *expected_search = "somewhere.com"; guint32 expected_nis = nmtst_inet4_from_string ("1.2.3.13"); guint32 expected_wins = nmtst_inet4_from_string ("2.3.4.5"); - guint32 expected_mss = 1400; guint32 expected_mtu = 1492; src = build_test_config (); @@ -95,7 +103,7 @@ test_subtract (void) nm_ip4_config_add_address (dst, &addr); route = *nmtst_platform_ip4_route (expected_route_dest, expected_route_plen, expected_route_next_hop); - nm_ip4_config_add_route (dst, &route); + nm_ip4_config_add_route (dst, &route, NULL); nm_ip4_config_add_nameserver (dst, expected_ns1); nm_ip4_config_add_nameserver (dst, expected_ns2); @@ -105,23 +113,23 @@ test_subtract (void) nm_ip4_config_add_nis_server (dst, expected_nis); nm_ip4_config_add_wins (dst, expected_wins); - nm_ip4_config_set_mss (dst, expected_mss); nm_ip4_config_set_mtu (dst, expected_mtu, NM_IP_CONFIG_SOURCE_UNKNOWN); - nm_ip4_config_subtract (dst, src); + nm_ip4_config_subtract (dst, src, 0); /* ensure what's left is what we expect */ g_assert_cmpuint (nm_ip4_config_get_num_addresses (dst), ==, 1); - test_addr = nm_ip4_config_get_address (dst, 0); + test_addr = _nmtst_ip4_config_get_address (dst, 0); g_assert (test_addr != NULL); g_assert_cmpuint (test_addr->address, ==, nmtst_inet4_from_string (expected_addr)); g_assert_cmpuint (test_addr->peer_address, ==, test_addr->address); g_assert_cmpuint (test_addr->plen, ==, expected_addr_plen); - g_assert_cmpuint (nm_ip4_config_get_gateway (dst), ==, 0); + g_assert (!nm_ip4_config_best_default_route_get (dst)); + g_assert_cmpuint (nmtst_ip4_config_get_gateway (dst), ==, 0); g_assert_cmpuint (nm_ip4_config_get_num_routes (dst), ==, 1); - test_route = nm_ip4_config_get_route (dst, 0); + test_route = _nmtst_ip4_config_get_route (dst, 0); g_assert (test_route != NULL); g_assert_cmpuint (test_route->network, ==, nmtst_inet4_from_string (expected_route_dest)); g_assert_cmpuint (test_route->plen, ==, expected_route_plen); @@ -142,7 +150,6 @@ test_subtract (void) g_assert_cmpuint (nm_ip4_config_get_num_wins (dst), ==, 1); g_assert_cmpuint (nm_ip4_config_get_wins (dst, 0), ==, expected_wins); - g_assert_cmpuint (nm_ip4_config_get_mss (dst), ==, expected_mss); g_assert_cmpuint (nm_ip4_config_get_mtu (dst), ==, expected_mtu); g_object_unref (src); @@ -156,8 +163,8 @@ test_compare_with_source (void) NMPlatformIP4Address addr; NMPlatformIP4Route route; - a = nm_ip4_config_new (1); - b = nm_ip4_config_new (2); + a = nmtst_ip4_config_new (1); + b = nmtst_ip4_config_new (2); /* Address */ addr = *nmtst_platform_ip4_address ("1.2.3.4", NULL, 24); @@ -170,10 +177,10 @@ test_compare_with_source (void) /* Route */ route = *nmtst_platform_ip4_route ("10.0.0.0", 8, "192.168.1.1"); route.rt_source = NM_IP_CONFIG_SOURCE_USER; - nm_ip4_config_add_route (a, &route); + nm_ip4_config_add_route (a, &route, NULL); route.rt_source = NM_IP_CONFIG_SOURCE_VPN; - nm_ip4_config_add_route (b, &route); + nm_ip4_config_add_route (b, &route, NULL); /* Assert that the configs are basically the same, eg that the source is ignored */ g_assert (nm_ip4_config_equal (a, b)); @@ -189,34 +196,34 @@ test_add_address_with_source (void) NMPlatformIP4Address addr; const NMPlatformIP4Address *test_addr; - a = nm_ip4_config_new (1); + a = nmtst_ip4_config_new (1); /* Test that a higher priority source is not overwritten */ addr = *nmtst_platform_ip4_address ("1.2.3.4", NULL, 24); addr.addr_source = NM_IP_CONFIG_SOURCE_USER; nm_ip4_config_add_address (a, &addr); - test_addr = nm_ip4_config_get_address (a, 0); + test_addr = _nmtst_ip4_config_get_address (a, 0); g_assert_cmpint (test_addr->addr_source, ==, NM_IP_CONFIG_SOURCE_USER); addr.addr_source = NM_IP_CONFIG_SOURCE_VPN; nm_ip4_config_add_address (a, &addr); - test_addr = nm_ip4_config_get_address (a, 0); + test_addr = _nmtst_ip4_config_get_address (a, 0); g_assert_cmpint (test_addr->addr_source, ==, NM_IP_CONFIG_SOURCE_USER); /* Test that a lower priority address source is overwritten */ - nm_ip4_config_del_address (a, 0); + _nmtst_ip4_config_del_address (a, 0); addr.addr_source = NM_IP_CONFIG_SOURCE_KERNEL; nm_ip4_config_add_address (a, &addr); - test_addr = nm_ip4_config_get_address (a, 0); + test_addr = _nmtst_ip4_config_get_address (a, 0); g_assert_cmpint (test_addr->addr_source, ==, NM_IP_CONFIG_SOURCE_KERNEL); addr.addr_source = NM_IP_CONFIG_SOURCE_USER; nm_ip4_config_add_address (a, &addr); - test_addr = nm_ip4_config_get_address (a, 0); + test_addr = _nmtst_ip4_config_get_address (a, 0); g_assert_cmpint (test_addr->addr_source, ==, NM_IP_CONFIG_SOURCE_USER); g_object_unref (a); @@ -225,50 +232,52 @@ test_add_address_with_source (void) static void test_add_route_with_source (void) { - NMIP4Config *a; + gs_unref_object NMIP4Config *a = NULL; NMPlatformIP4Route route; const NMPlatformIP4Route *test_route; - a = nm_ip4_config_new (1); + a = nmtst_ip4_config_new (1); /* Test that a higher priority source is not overwritten */ - route = *nmtst_platform_ip4_route ("1.2.3.4", 24, "1.2.3.1"); + route = *nmtst_platform_ip4_route ("1.2.3.0", 24, "1.2.3.1"); route.rt_source = NM_IP_CONFIG_SOURCE_USER; - nm_ip4_config_add_route (a, &route); + nm_ip4_config_add_route (a, &route, NULL); - test_route = nm_ip4_config_get_route (a, 0); + g_assert_cmpint (nm_ip4_config_get_num_routes (a), ==, 1); + test_route = _nmtst_ip4_config_get_route (a, 0); g_assert_cmpint (test_route->rt_source, ==, NM_IP_CONFIG_SOURCE_USER); route.rt_source = NM_IP_CONFIG_SOURCE_VPN; - nm_ip4_config_add_route (a, &route); + nm_ip4_config_add_route (a, &route, NULL); - test_route = nm_ip4_config_get_route (a, 0); + g_assert_cmpint (nm_ip4_config_get_num_routes (a), ==, 1); + test_route = _nmtst_ip4_config_get_route (a, 0); g_assert_cmpint (test_route->rt_source, ==, NM_IP_CONFIG_SOURCE_USER); + _nmtst_ip4_config_del_route (a, 0); + g_assert_cmpint (nm_ip4_config_get_num_routes (a), ==, 0); + /* Test that a lower priority address source is overwritten */ - nm_ip4_config_del_route (a, 0); - route.rt_source = NM_IP_CONFIG_SOURCE_KERNEL; - nm_ip4_config_add_route (a, &route); + route.rt_source = NM_IP_CONFIG_SOURCE_RTPROT_KERNEL; + nm_ip4_config_add_route (a, &route, NULL); - test_route = nm_ip4_config_get_route (a, 0); - g_assert_cmpint (test_route->rt_source, ==, NM_IP_CONFIG_SOURCE_KERNEL); + g_assert_cmpint (nm_ip4_config_get_num_routes (a), ==, 1); + test_route = _nmtst_ip4_config_get_route (a, 0); + g_assert_cmpint (test_route->rt_source, ==, NM_IP_CONFIG_SOURCE_RTPROT_KERNEL); - route.rt_source = NM_IP_CONFIG_SOURCE_USER; - nm_ip4_config_add_route (a, &route); - - test_route = nm_ip4_config_get_route (a, 0); - g_assert_cmpint (test_route->rt_source, ==, NM_IP_CONFIG_SOURCE_USER); + route.rt_source = NM_IP_CONFIG_SOURCE_KERNEL; + nm_ip4_config_add_route (a, &route, NULL); - g_object_unref (a); + g_assert_cmpint (nm_ip4_config_get_num_routes (a), ==, 1); + test_route = _nmtst_ip4_config_get_route (a, 0); + g_assert_cmpint (test_route->rt_source, ==, NM_IP_CONFIG_SOURCE_KERNEL); } static void -test_merge_subtract_mss_mtu (void) +test_merge_subtract_mtu (void) { NMIP4Config *cfg1, *cfg2, *cfg3; - guint32 expected_mss2 = 1400; guint32 expected_mtu2 = 1492; - guint32 expected_mss3 = 555; guint32 expected_mtu3 = 666; cfg1 = build_test_config (); @@ -276,24 +285,19 @@ test_merge_subtract_mss_mtu (void) cfg3 = build_test_config (); /* add MSS, MTU to configs to test them */ - nm_ip4_config_set_mss (cfg2, expected_mss2); nm_ip4_config_set_mtu (cfg2, expected_mtu2, NM_IP_CONFIG_SOURCE_UNKNOWN); - nm_ip4_config_set_mss (cfg3, expected_mss3); nm_ip4_config_set_mtu (cfg3, expected_mtu3, NM_IP_CONFIG_SOURCE_UNKNOWN); - nm_ip4_config_merge (cfg1, cfg2, NM_IP_CONFIG_MERGE_DEFAULT); + nm_ip4_config_merge (cfg1, cfg2, NM_IP_CONFIG_MERGE_DEFAULT, 0); /* ensure MSS and MTU are in cfg1 */ - g_assert_cmpuint (nm_ip4_config_get_mss (cfg1), ==, expected_mss2); g_assert_cmpuint (nm_ip4_config_get_mtu (cfg1), ==, expected_mtu2); - nm_ip4_config_merge (cfg1, cfg3, NM_IP_CONFIG_MERGE_DEFAULT); - /* ensure again the MSS and MTU in cfg1 got overriden */ - g_assert_cmpuint (nm_ip4_config_get_mss (cfg1), ==, expected_mss3); + nm_ip4_config_merge (cfg1, cfg3, NM_IP_CONFIG_MERGE_DEFAULT, 0); + /* ensure again the MSS and MTU in cfg1 got overridden */ g_assert_cmpuint (nm_ip4_config_get_mtu (cfg1), ==, expected_mtu3); - nm_ip4_config_subtract (cfg1, cfg3); + nm_ip4_config_subtract (cfg1, cfg3, 0); /* ensure MSS and MTU are zero in cfg1 */ - g_assert_cmpuint (nm_ip4_config_get_mss (cfg1), ==, 0); g_assert_cmpuint (nm_ip4_config_get_mtu (cfg1), ==, 0); g_object_unref (cfg1); @@ -306,7 +310,7 @@ test_strip_search_trailing_dot (void) { NMIP4Config *config; - config = nm_ip4_config_new (1); + config = nmtst_ip4_config_new (1); nm_ip4_config_add_search (config, "."); nm_ip4_config_add_search (config, "foo"); @@ -335,7 +339,7 @@ main (int argc, char **argv) g_test_add_func ("/ip4-config/compare-with-source", test_compare_with_source); g_test_add_func ("/ip4-config/add-address-with-source", test_add_address_with_source); g_test_add_func ("/ip4-config/add-route-with-source", test_add_route_with_source); - g_test_add_func ("/ip4-config/merge-subtract-mss-mtu", test_merge_subtract_mss_mtu); + g_test_add_func ("/ip4-config/merge-subtract-mtu", test_merge_subtract_mtu); g_test_add_func ("/ip4-config/strip-search-trailing-dot", test_strip_search_trailing_dot); return g_test_run (); diff --git a/src/tests/test-ip6-config.c b/src/tests/test-ip6-config.c index 7e83625c..bcbeee3e 100644 --- a/src/tests/test-ip6-config.c +++ b/src/tests/test-ip6-config.c @@ -34,13 +34,13 @@ build_test_config (void) NMIP6Config *config; /* Build up the config to subtract */ - config = nm_ip6_config_new (1); + config = nmtst_ip6_config_new (1); nm_ip6_config_add_address (config, nmtst_platform_ip6_address ("abcd:1234:4321::cdde", "1:2:3:4::5", 64)); - nm_ip6_config_add_route (config, nmtst_platform_ip6_route ("abcd:1234:4321::", 24, "abcd:1234:4321:cdde::2", NULL)); - nm_ip6_config_add_route (config, nmtst_platform_ip6_route ("2001:abba::", 16, "2001:abba::2234", NULL)); + nm_ip6_config_add_route (config, nmtst_platform_ip6_route ("abcd:1200::", 24, "abcd:1234:4321:cdde::2", NULL), NULL); + nm_ip6_config_add_route (config, nmtst_platform_ip6_route ("2001::", 16, "2001:abba::2234", NULL), NULL); - nm_ip6_config_set_gateway (config, nmtst_inet6_from_string ("3001:abba::3234")); + nm_ip6_config_add_route (config, nmtst_platform_ip6_route ("::", 0, "3001:abba::3234", NULL), NULL); nm_ip6_config_add_nameserver (config, nmtst_inet6_from_string ("1:2:3:4::1")); nm_ip6_config_add_nameserver (config, nmtst_inet6_from_string ("1:2:3:4::2")); @@ -60,7 +60,7 @@ test_subtract (void) const NMPlatformIP6Route *test_route; const char *expected_addr = "1122:3344:5566::7788"; guint32 expected_addr_plen = 96; - const char *expected_route_dest = "9991:8882:7773::"; + const char *expected_route_dest = "9991:8800::"; guint32 expected_route_plen = 24; const char *expected_route_next_hop = "1119:2228:3337:4446::5555"; struct in6_addr expected_ns1; @@ -74,7 +74,7 @@ test_subtract (void) /* add a couple more things to the test config */ dst = build_test_config (); nm_ip6_config_add_address (dst, nmtst_platform_ip6_address (expected_addr, NULL, expected_addr_plen)); - nm_ip6_config_add_route (dst, nmtst_platform_ip6_route (expected_route_dest, expected_route_plen, expected_route_next_hop, NULL)); + nm_ip6_config_add_route (dst, nmtst_platform_ip6_route (expected_route_dest, expected_route_plen, expected_route_next_hop, NULL), NULL); expected_ns1 = *nmtst_inet6_from_string ("2222:3333:4444::5555"); nm_ip6_config_add_nameserver (dst, &expected_ns1); @@ -84,21 +84,21 @@ test_subtract (void) nm_ip6_config_add_domain (dst, expected_domain); nm_ip6_config_add_search (dst, expected_search); - nm_ip6_config_subtract (dst, src); + nm_ip6_config_subtract (dst, src, 0); /* ensure what's left is what we expect */ g_assert_cmpuint (nm_ip6_config_get_num_addresses (dst), ==, 1); - test_addr = nm_ip6_config_get_address (dst, 0); + test_addr = _nmtst_ip6_config_get_address (dst, 0); g_assert (test_addr != NULL); tmp = *nmtst_inet6_from_string (expected_addr); g_assert (memcmp (&test_addr->address, &tmp, sizeof (tmp)) == 0); g_assert (memcmp (&test_addr->peer_address, &in6addr_any, sizeof (tmp)) == 0); g_assert_cmpuint (test_addr->plen, ==, expected_addr_plen); - g_assert (nm_ip6_config_get_gateway (dst) == NULL); + g_assert (nm_ip6_config_best_default_route_get (dst) == NULL); g_assert_cmpuint (nm_ip6_config_get_num_routes (dst), ==, 1); - test_route = nm_ip6_config_get_route (dst, 0); + test_route = _nmtst_ip6_config_get_route (dst, 0); g_assert (test_route != NULL); tmp = *nmtst_inet6_from_string (expected_route_dest); @@ -127,8 +127,8 @@ test_compare_with_source (void) NMPlatformIP6Address addr; NMPlatformIP6Route route; - a = nm_ip6_config_new (1); - b = nm_ip6_config_new (2); + a = nmtst_ip6_config_new (1); + b = nmtst_ip6_config_new (2); /* Address */ addr = *nmtst_platform_ip6_address ("1122:3344:5566::7788", NULL, 64); @@ -139,12 +139,12 @@ test_compare_with_source (void) nm_ip6_config_add_address (b, &addr); /* Route */ - route = *nmtst_platform_ip6_route ("abcd:1234:4321::", 24, "abcd:1234:4321:cdde::2", NULL); + route = *nmtst_platform_ip6_route ("abcd:1200::", 24, "abcd:1234:4321:cdde::2", NULL); route.rt_source = NM_IP_CONFIG_SOURCE_USER; - nm_ip6_config_add_route (a, &route); + nm_ip6_config_add_route (a, &route, NULL); route.rt_source = NM_IP_CONFIG_SOURCE_VPN; - nm_ip6_config_add_route (b, &route); + nm_ip6_config_add_route (b, &route, NULL); /* Assert that the configs are basically the same, eg that the source is ignored */ g_assert (nm_ip6_config_equal (a, b)); @@ -160,34 +160,34 @@ test_add_address_with_source (void) NMPlatformIP6Address addr; const NMPlatformIP6Address *test_addr; - a = nm_ip6_config_new (1); + a = nmtst_ip6_config_new (1); /* Test that a higher priority source is not overwritten */ addr = *nmtst_platform_ip6_address ("1122:3344:5566::7788", NULL, 64); addr.addr_source = NM_IP_CONFIG_SOURCE_USER; nm_ip6_config_add_address (a, &addr); - test_addr = nm_ip6_config_get_address (a, 0); + test_addr = _nmtst_ip6_config_get_address (a, 0); g_assert_cmpint (test_addr->addr_source, ==, NM_IP_CONFIG_SOURCE_USER); addr.addr_source = NM_IP_CONFIG_SOURCE_VPN; nm_ip6_config_add_address (a, &addr); - test_addr = nm_ip6_config_get_address (a, 0); + test_addr = _nmtst_ip6_config_get_address (a, 0); g_assert_cmpint (test_addr->addr_source, ==, NM_IP_CONFIG_SOURCE_USER); /* Test that a lower priority address source is overwritten */ - nm_ip6_config_del_address (a, 0); + _nmtst_ip6_config_del_address (a, 0); addr.addr_source = NM_IP_CONFIG_SOURCE_KERNEL; nm_ip6_config_add_address (a, &addr); - test_addr = nm_ip6_config_get_address (a, 0); + test_addr = _nmtst_ip6_config_get_address (a, 0); g_assert_cmpint (test_addr->addr_source, ==, NM_IP_CONFIG_SOURCE_KERNEL); addr.addr_source = NM_IP_CONFIG_SOURCE_USER; nm_ip6_config_add_address (a, &addr); - test_addr = nm_ip6_config_get_address (a, 0); + test_addr = _nmtst_ip6_config_get_address (a, 0); g_assert_cmpint (test_addr->addr_source, ==, NM_IP_CONFIG_SOURCE_USER); g_object_unref (a); @@ -196,41 +196,45 @@ test_add_address_with_source (void) static void test_add_route_with_source (void) { - NMIP6Config *a; + gs_unref_object NMIP6Config *a = NULL; NMPlatformIP6Route route; const NMPlatformIP6Route *test_route; - a = nm_ip6_config_new (1); + a = nmtst_ip6_config_new (1); /* Test that a higher priority source is not overwritten */ - route = *nmtst_platform_ip6_route ("abcd:1234:4321::", 24, "abcd:1234:4321:cdde::2", NULL); + route = *nmtst_platform_ip6_route ("abcd:1200::", 24, "abcd:1234:4321:cdde::2", NULL); route.rt_source = NM_IP_CONFIG_SOURCE_USER; - nm_ip6_config_add_route (a, &route); + nm_ip6_config_add_route (a, &route, NULL); - test_route = nm_ip6_config_get_route (a, 0); + g_assert_cmpint (nm_ip6_config_get_num_routes (a), ==, 1); + test_route = _nmtst_ip6_config_get_route (a, 0); g_assert_cmpint (test_route->rt_source, ==, NM_IP_CONFIG_SOURCE_USER); route.rt_source = NM_IP_CONFIG_SOURCE_VPN; - nm_ip6_config_add_route (a, &route); + nm_ip6_config_add_route (a, &route, NULL); - test_route = nm_ip6_config_get_route (a, 0); + g_assert_cmpint (nm_ip6_config_get_num_routes (a), ==, 1); + test_route = _nmtst_ip6_config_get_route (a, 0); g_assert_cmpint (test_route->rt_source, ==, NM_IP_CONFIG_SOURCE_USER); + _nmtst_ip6_config_del_route (a, 0); + g_assert_cmpint (nm_ip6_config_get_num_routes (a), ==, 0); + /* Test that a lower priority address source is overwritten */ - nm_ip6_config_del_route (a, 0); route.rt_source = NM_IP_CONFIG_SOURCE_KERNEL; - nm_ip6_config_add_route (a, &route); + nm_ip6_config_add_route (a, &route, NULL); - test_route = nm_ip6_config_get_route (a, 0); + g_assert_cmpint (nm_ip6_config_get_num_routes (a), ==, 1); + test_route = _nmtst_ip6_config_get_route (a, 0); g_assert_cmpint (test_route->rt_source, ==, NM_IP_CONFIG_SOURCE_KERNEL); route.rt_source = NM_IP_CONFIG_SOURCE_USER; - nm_ip6_config_add_route (a, &route); + nm_ip6_config_add_route (a, &route, NULL); - test_route = nm_ip6_config_get_route (a, 0); + g_assert_cmpint (nm_ip6_config_get_num_routes (a), ==, 1); + test_route = _nmtst_ip6_config_get_route (a, 0); g_assert_cmpint (test_route->rt_source, ==, NM_IP_CONFIG_SOURCE_USER); - - g_object_unref (a); } static void @@ -256,18 +260,18 @@ test_nm_ip6_config_addresses_sort_check (NMIP6Config *config, NMSettingIP6Config int j = g_rand_int_range (nmtst_get_rand (), i, addr_count); NMTST_SWAP (idx[i], idx[j]); - nm_ip6_config_add_address (copy, nm_ip6_config_get_address (config, idx[i])); + nm_ip6_config_add_address (copy, _nmtst_ip6_config_get_address (config, idx[i])); } /* reorder them again */ - nm_ip6_config_addresses_sort (copy); + _nmtst_ip6_config_addresses_sort (copy); /* check equality using nm_ip6_config_equal() */ if (!nm_ip6_config_equal (copy, config)) { g_message ("%s", "SORTING yields unexpected output:"); for (i = 0; i < addr_count; i++) { - g_message (" >> [%d] = %s", i, nm_platform_ip6_address_to_string (nm_ip6_config_get_address (config, i), NULL, 0)); - g_message (" << [%d] = %s", i, nm_platform_ip6_address_to_string (nm_ip6_config_get_address (copy, i), NULL, 0)); + g_message (" >> [%d] = %s", i, nm_platform_ip6_address_to_string (_nmtst_ip6_config_get_address (config, i), NULL, 0)); + g_message (" << [%d] = %s", i, nm_platform_ip6_address_to_string (_nmtst_ip6_config_get_address (copy, i), NULL, 0)); } g_assert_not_reached (); } @@ -327,7 +331,7 @@ test_strip_search_trailing_dot (void) { NMIP6Config *config; - config = nm_ip6_config_new (1); + config = nmtst_ip6_config_new (1); nm_ip6_config_add_search (config, "."); nm_ip6_config_add_search (config, "foo"); @@ -345,6 +349,71 @@ test_strip_search_trailing_dot (void) /*****************************************************************************/ +static void +test_replace (gconstpointer user_data) +{ + nm_auto_unref_dedup_multi_index NMDedupMultiIndex *multi_idx = nm_dedup_multi_index_new (); + const int TEST_IDX = GPOINTER_TO_INT (user_data); + const int IFINDEX = 1; + gs_unref_object NMIP6Config *src_conf = NULL; + gs_unref_object NMIP6Config *dst_conf = NULL; + NMPlatformIP6Address *addr; + NMPlatformIP6Address addrs[5] = { }; + guint addrs_n = 0; + guint i; + + dst_conf = nm_ip6_config_new (multi_idx, IFINDEX); + src_conf = nm_ip6_config_new (multi_idx, IFINDEX); + + switch (TEST_IDX) { + case 1: + addr = &addrs[addrs_n++]; + addr->ifindex = IFINDEX; + addr->address = *nmtst_inet6_from_string ("fe80::78ec:7a6d:602d:20f2"); + addr->plen = 64; + addr->n_ifa_flags = IFA_F_PERMANENT; + addr->addr_source = NM_IP_CONFIG_SOURCE_KERNEL; + break; + case 2: + addr = &addrs[addrs_n++]; + addr->ifindex = IFINDEX; + addr->address = *nmtst_inet6_from_string ("fe80::78ec:7a6d:602d:20f2"); + addr->plen = 64; + addr->n_ifa_flags = IFA_F_PERMANENT; + addr->addr_source = NM_IP_CONFIG_SOURCE_KERNEL; + + addr = &addrs[addrs_n++]; + addr->ifindex = IFINDEX; + addr->address = *nmtst_inet6_from_string ("1::1"); + addr->plen = 64; + addr->addr_source = NM_IP_CONFIG_SOURCE_USER; + + nm_ip6_config_add_address (dst_conf, addr); + break; + default: + g_assert_not_reached (); + } + + g_assert (addrs_n < G_N_ELEMENTS (addrs)); + + for (i = 0; i < addrs_n; i++) + nm_ip6_config_add_address (src_conf, &addrs[i]); + + nm_ip6_config_replace (dst_conf, src_conf, NULL); + + for (i = 0; i < addrs_n; i++) { + const NMPlatformIP6Address *a = _nmtst_ip6_config_get_address (dst_conf, i); + const NMPlatformIP6Address *b = _nmtst_ip6_config_get_address (src_conf, i); + + g_assert (nm_platform_ip6_address_cmp (&addrs[i], a) == 0); + g_assert (nm_platform_ip6_address_cmp (&addrs[i], b) == 0); + } + g_assert (addrs_n == nm_ip6_config_get_num_addresses (dst_conf)); + g_assert (addrs_n == nm_ip6_config_get_num_addresses (src_conf)); +} + +/*****************************************************************************/ + NMTST_DEFINE(); int @@ -358,6 +427,8 @@ main (int argc, char **argv) g_test_add_func ("/ip6-config/add-route-with-source", test_add_route_with_source); g_test_add_func ("/ip6-config/test_nm_ip6_config_addresses_sort", test_nm_ip6_config_addresses_sort); g_test_add_func ("/ip6-config/strip-search-trailing-dot", test_strip_search_trailing_dot); + g_test_add_data_func ("/ip6-config/replace/1", GINT_TO_POINTER (1), test_replace); + g_test_add_data_func ("/ip6-config/replace/2", GINT_TO_POINTER (2), test_replace); return g_test_run (); } diff --git a/src/tests/test-resolvconf-capture.c b/src/tests/test-resolvconf-capture.c index ccbdafa6..2c34ff74 100644 --- a/src/tests/test-resolvconf-capture.c +++ b/src/tests/test-resolvconf-capture.c @@ -36,42 +36,24 @@ test_capture_empty (void) GArray *ns4 = g_array_new (FALSE, FALSE, sizeof (guint32)); GArray *ns6 = g_array_new (FALSE, FALSE, sizeof (struct in6_addr)); - g_assert (nm_ip4_config_capture_resolv_conf (ns4, NULL, "") == FALSE); + g_assert (!nm_utils_resolve_conf_parse (AF_INET, "", ns4, NULL)); g_assert_cmpint (ns4->len, ==, 0); - g_assert (nm_ip6_config_capture_resolv_conf (ns6, NULL, "") == FALSE); + g_assert (!nm_utils_resolve_conf_parse (AF_INET6, "", ns6, NULL)); g_assert_cmpint (ns6->len, ==, 0); g_array_free (ns4, TRUE); g_array_free (ns6, TRUE); } -static void -assert_dns4_entry (const GArray *a, guint i, const char *s) -{ - guint32 n, m; - - g_assert (inet_aton (s, (void *) &n) != 0); - m = g_array_index (a, guint32, i); - g_assert_cmpint (m, ==, n); -} - -static void -assert_dns6_entry (const GArray *a, guint i, const char *s) -{ - struct in6_addr n = IN6ADDR_ANY_INIT; - struct in6_addr *m; +#define assert_dns4_entry(a, i, s) \ + g_assert_cmpint ((g_array_index ((a), guint32, (i))), ==, nmtst_inet4_from_string (s)); - g_assert (inet_pton (AF_INET6, s, (void *) &n) == 1); - m = &g_array_index (a, struct in6_addr, i); - g_assert (IN6_ARE_ADDR_EQUAL (&n, m)); -} +#define assert_dns6_entry(a, i, s) \ + g_assert (IN6_ARE_ADDR_EQUAL (&g_array_index ((a), struct in6_addr, (i)), nmtst_inet6_from_string (s))) -static void -assert_dns_option (GPtrArray *a, guint i, const char *s) -{ - g_assert_cmpstr (a->pdata[i], ==, s); -} +#define assert_dns_option(a, i, s) \ + g_assert_cmpstr ((a)->pdata[(i)], ==, (s)); static void test_capture_basic4 (void) @@ -84,7 +66,7 @@ test_capture_basic4 (void) "nameserver 4.2.2.1\r\n" "nameserver 4.2.2.2\r\n"; - g_assert (nm_ip4_config_capture_resolv_conf (ns4, NULL, rc)); + g_assert (nm_utils_resolve_conf_parse (AF_INET, rc, ns4, NULL)); g_assert_cmpint (ns4->len, ==, 2); assert_dns4_entry (ns4, 0, "4.2.2.1"); assert_dns4_entry (ns4, 1, "4.2.2.2"); @@ -105,7 +87,7 @@ test_capture_dup4 (void) "nameserver 4.2.2.2\r\n"; /* Check that duplicates are ignored */ - g_assert (nm_ip4_config_capture_resolv_conf (ns4, NULL, rc)); + g_assert (nm_utils_resolve_conf_parse (AF_INET, rc, ns4, NULL)); g_assert_cmpint (ns4->len, ==, 2); assert_dns4_entry (ns4, 0, "4.2.2.1"); assert_dns4_entry (ns4, 1, "4.2.2.2"); @@ -124,7 +106,7 @@ test_capture_basic6 (void) "nameserver 2001:4860:4860::8888\r\n" "nameserver 2001:4860:4860::8844\r\n"; - g_assert (nm_ip6_config_capture_resolv_conf (ns6, NULL, rc)); + g_assert (nm_utils_resolve_conf_parse (AF_INET6, rc, ns6, NULL)); g_assert_cmpint (ns6->len, ==, 2); assert_dns6_entry (ns6, 0, "2001:4860:4860::8888"); assert_dns6_entry (ns6, 1, "2001:4860:4860::8844"); @@ -145,7 +127,7 @@ test_capture_dup6 (void) "nameserver 2001:4860:4860::8844\r\n"; /* Check that duplicates are ignored */ - g_assert (nm_ip6_config_capture_resolv_conf (ns6, NULL, rc)); + g_assert (nm_utils_resolve_conf_parse (AF_INET6, rc, ns6, NULL)); g_assert_cmpint (ns6->len, ==, 2); assert_dns6_entry (ns6, 0, "2001:4860:4860::8888"); assert_dns6_entry (ns6, 1, "2001:4860:4860::8844"); @@ -165,7 +147,7 @@ test_capture_addr4_with_6 (void) "nameserver 4.2.2.2\r\n" "nameserver 2001:4860:4860::8888\r\n"; - g_assert (nm_ip4_config_capture_resolv_conf (ns4, NULL, rc)); + g_assert (nm_utils_resolve_conf_parse (AF_INET, rc, ns4, NULL)); g_assert_cmpint (ns4->len, ==, 2); assert_dns4_entry (ns4, 0, "4.2.2.1"); assert_dns4_entry (ns4, 1, "4.2.2.2"); @@ -185,7 +167,7 @@ test_capture_addr6_with_4 (void) "nameserver 2001:4860:4860::8888\r\n" "nameserver 2001:4860:4860::8844\r\n"; - g_assert (nm_ip6_config_capture_resolv_conf (ns6, NULL, rc)); + g_assert (nm_utils_resolve_conf_parse (AF_INET6, rc, ns6, NULL)); g_assert_cmpint (ns6->len, ==, 2); assert_dns6_entry (ns6, 0, "2001:4860:4860::8888"); assert_dns6_entry (ns6, 1, "2001:4860:4860::8844"); @@ -200,15 +182,17 @@ test_capture_format (void) const char *rc = " nameserver 4.2.2.1\r\n" /* bad */ "nameserver4.2.2.1\r\n" /* bad */ -"nameserver 4.2.2.3\r" /* good */ +"nameserver 4.2.2.3\r" /* good */ "nameserver\t\t4.2.2.4\r\n" /* good */ -"nameserver 4.2.2.5\t\t\r\n"; /* good */ +"nameserver 4.2.2.5\t\t\r\n" /* good */ +"nameserver 4.2.2.6 \r\n"; /* good */ - g_assert (nm_ip4_config_capture_resolv_conf (ns4, NULL, rc)); - g_assert_cmpint (ns4->len, ==, 3); + g_assert (nm_utils_resolve_conf_parse (AF_INET, rc, ns4, NULL)); + g_assert_cmpint (ns4->len, ==, 4); assert_dns4_entry (ns4, 0, "4.2.2.3"); assert_dns4_entry (ns4, 1, "4.2.2.4"); assert_dns4_entry (ns4, 2, "4.2.2.5"); + assert_dns4_entry (ns4, 3, "4.2.2.6"); g_array_free (ns4, TRUE); } @@ -223,7 +207,7 @@ test_capture_dns_options (void) "options debug rotate timeout:5 \r\n" "options edns0\r\n"; - g_assert (nm_ip4_config_capture_resolv_conf (ns4, dns_options, rc)); + g_assert (nm_utils_resolve_conf_parse (AF_INET, rc, ns4, dns_options)); g_assert_cmpint (dns_options->len, ==, 4); assert_dns_option (dns_options, 0, "debug"); assert_dns_option (dns_options, 1, "rotate"); @@ -244,7 +228,7 @@ test_capture_dns_options_dup (void) "options edns0 debug\r\n" "options timeout:5\r\n"; - g_assert (nm_ip4_config_capture_resolv_conf (ns4, dns_options, rc)); + g_assert (nm_utils_resolve_conf_parse (AF_INET, rc, ns4, dns_options)); g_assert_cmpint (dns_options->len, ==, 4); assert_dns_option (dns_options, 0, "debug"); assert_dns_option (dns_options, 1, "rotate"); @@ -263,7 +247,7 @@ test_capture_dns_options_valid4 (void) const char *rc = "options debug: rotate:yes edns0 foobar : inet6\r\n"; - g_assert (nm_ip4_config_capture_resolv_conf (ns4, dns_options, rc)); + g_assert (nm_utils_resolve_conf_parse (AF_INET, rc, ns4, dns_options)); g_assert_cmpint (dns_options->len, ==, 1); assert_dns_option (dns_options, 0, "edns0"); @@ -274,12 +258,12 @@ test_capture_dns_options_valid4 (void) static void test_capture_dns_options_valid6 (void) { - GArray *ns6 = g_array_new (FALSE, FALSE, sizeof (guint32)); + GArray *ns6 = g_array_new (FALSE, FALSE, sizeof (struct in6_addr)); GPtrArray *dns_options = g_ptr_array_new_with_free_func (g_free); const char *rc = "options inet6 debug foobar rotate:\r\n"; - g_assert (nm_ip6_config_capture_resolv_conf (ns6, dns_options, rc)); + g_assert (nm_utils_resolve_conf_parse (AF_INET6, rc, ns6, dns_options)); g_assert_cmpint (dns_options->len, ==, 2); assert_dns_option (dns_options, 0, "inet6"); assert_dns_option (dns_options, 1, "debug"); diff --git a/src/tests/test-route-manager.c b/src/tests/test-route-manager.c deleted file mode 100644 index 6650d26c..00000000 --- a/src/tests/test-route-manager.c +++ /dev/null @@ -1,933 +0,0 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ -/* - * 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, 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. - * - * Copyright (C) 2015 Red Hat, Inc. - * - */ - -#include "nm-default.h" - -#include <arpa/inet.h> -#include <linux/rtnetlink.h> - -#include "platform/nm-platform.h" -#include "platform/nm-platform-utils.h" -#include "nm-route-manager.h" - -#include "platform/tests/test-common.h" - -typedef struct { - int ifindex0, ifindex1; -} test_fixture; - -NMRouteManager *route_manager_get (void); - -NM_DEFINE_SINGLETON_GETTER (NMRouteManager, route_manager_get, NM_TYPE_ROUTE_MANAGER); - -/*****************************************************************************/ - -static void -setup_dev0_ip4 (int ifindex, guint mss_of_first_route, guint32 metric_of_second_route) -{ - GArray *routes = g_array_new (FALSE, FALSE, sizeof (NMPlatformIP4Route)); - NMPlatformIP4Route route = { 0 }; - - route.ifindex = ifindex; - route.mss = 0; - - route.rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER); - inet_pton (AF_INET, "6.6.6.0", &route.network); - route.plen = 24; - route.gateway = INADDR_ANY; - route.metric = 20; - route.mss = mss_of_first_route; - g_array_append_val (routes, route); - - route.rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER); - inet_pton (AF_INET, "7.0.0.0", &route.network); - route.plen = 8; - inet_pton (AF_INET, "6.6.6.1", &route.gateway); - route.metric = metric_of_second_route; - route.mss = 0; - g_array_append_val (routes, route); - - nm_route_manager_ip4_route_sync (route_manager_get (), ifindex, routes, TRUE, TRUE); - g_array_free (routes, TRUE); -} - -static void -setup_dev1_ip4 (int ifindex) -{ - GArray *routes = g_array_new (FALSE, FALSE, sizeof (NMPlatformIP4Route)); - NMPlatformIP4Route route = { 0 }; - - route.ifindex = ifindex; - route.mss = 0; - - /* Add some route outside of route manager. The route manager - * should get rid of it upon sync. */ - nmtstp_ip4_route_add (NM_PLATFORM_GET, - route.ifindex, - NM_IP_CONFIG_SOURCE_USER, - nmtst_inet4_from_string ("9.0.0.0"), - 8, - INADDR_ANY, - 0, - 10, - route.mss); - - route.rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER); - inet_pton (AF_INET, "6.6.6.0", &route.network); - route.plen = 24; - route.gateway = INADDR_ANY; - route.metric = 20; - g_array_append_val (routes, route); - - route.rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER); - inet_pton (AF_INET, "7.0.0.0", &route.network); - route.plen = 8; - route.gateway = INADDR_ANY; - route.metric = 22; - g_array_append_val (routes, route); - - route.rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER); - inet_pton (AF_INET, "8.0.0.0", &route.network); - route.plen = 8; - inet_pton (AF_INET, "6.6.6.2", &route.gateway); - route.metric = 22; - g_array_append_val (routes, route); - - nm_route_manager_ip4_route_sync (route_manager_get (), ifindex, routes, TRUE, TRUE); - g_array_free (routes, TRUE); -} - -static void -update_dev0_ip4 (int ifindex) -{ - GArray *routes = g_array_new (FALSE, FALSE, sizeof (NMPlatformIP4Route)); - NMPlatformIP4Route route = { 0 }; - - route.ifindex = ifindex; - route.mss = 0; - - route.rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER); - inet_pton (AF_INET, "6.6.6.0", &route.network); - route.plen = 24; - route.gateway = INADDR_ANY; - route.metric = 20; - g_array_append_val (routes, route); - - route.rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER); - inet_pton (AF_INET, "7.0.0.0", &route.network); - route.plen = 8; - route.gateway = INADDR_ANY; - route.metric = 21; - g_array_append_val (routes, route); - - nm_route_manager_ip4_route_sync (route_manager_get (), ifindex, routes, TRUE, TRUE); - g_array_free (routes, TRUE); -} - - -static GArray * -ip4_routes (test_fixture *fixture) -{ - GArray *routes = nm_platform_ip4_route_get_all (NM_PLATFORM_GET, - fixture->ifindex0, - NM_PLATFORM_GET_ROUTE_FLAGS_WITH_NON_DEFAULT); - GArray *routes1 = nm_platform_ip4_route_get_all (NM_PLATFORM_GET, - fixture->ifindex1, - NM_PLATFORM_GET_ROUTE_FLAGS_WITH_NON_DEFAULT); - - g_array_append_vals (routes, routes1->data, routes1->len); - g_array_free (routes1, TRUE); - - return routes; -} - -static void -test_ip4 (test_fixture *fixture, gconstpointer user_data) -{ - GArray *routes; - - NMPlatformIP4Route state1[] = { - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = nmtst_inet4_from_string ("6.6.6.0"), - .plen = 24, - .ifindex = fixture->ifindex0, - .gateway = INADDR_ANY, - .metric = 20, - .mss = 1000, - .scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK), - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = nmtst_inet4_from_string ("7.0.0.0"), - .plen = 8, - .ifindex = fixture->ifindex0, - .gateway = nmtst_inet4_from_string ("6.6.6.1"), - .metric = 21021, - .mss = 0, - .scope_inv = nm_platform_route_scope_inv (RT_SCOPE_UNIVERSE), - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = nmtst_inet4_from_string ("7.0.0.0"), - .plen = 8, - .ifindex = fixture->ifindex1, - .gateway = INADDR_ANY, - .metric = 22, - .mss = 0, - .scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK), - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = nmtst_inet4_from_string ("6.6.6.0"), - .plen = 24, - .ifindex = fixture->ifindex1, - .gateway = INADDR_ANY, - .metric = 21, - .mss = 0, - .scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK), - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = nmtst_inet4_from_string ("8.0.0.0"), - .plen = 8, - .ifindex = fixture->ifindex1, - .gateway = nmtst_inet4_from_string ("6.6.6.2"), - .metric = 22, - .mss = 0, - .scope_inv = nm_platform_route_scope_inv (RT_SCOPE_UNIVERSE), - }, - }; - - NMPlatformIP4Route state2[] = { - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = nmtst_inet4_from_string ("6.6.6.0"), - .plen = 24, - .ifindex = fixture->ifindex0, - .gateway = INADDR_ANY, - .metric = 20, - .mss = 0, - .scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK), - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = nmtst_inet4_from_string ("7.0.0.0"), - .plen = 8, - .ifindex = fixture->ifindex0, - .gateway = INADDR_ANY, - .metric = 21, - .mss = 0, - .scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK), - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = nmtst_inet4_from_string ("7.0.0.0"), - .plen = 8, - .ifindex = fixture->ifindex1, - .gateway = INADDR_ANY, - .metric = 22, - .mss = 0, - .scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK), - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = nmtst_inet4_from_string ("6.6.6.0"), - .plen = 24, - .ifindex = fixture->ifindex1, - .gateway = INADDR_ANY, - .metric = 21, - .mss = 0, - .scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK), - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = nmtst_inet4_from_string ("8.0.0.0"), - .plen = 8, - .ifindex = fixture->ifindex1, - .gateway = nmtst_inet4_from_string ("6.6.6.2"), - .metric = 22, - .mss = 0, - .scope_inv = nm_platform_route_scope_inv (RT_SCOPE_UNIVERSE), - }, - }; - - NMPlatformIP4Route state3[] = { - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = nmtst_inet4_from_string ("7.0.0.0"), - .plen = 8, - .ifindex = fixture->ifindex1, - .gateway = INADDR_ANY, - .metric = 22, - .mss = 0, - .scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK), - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = nmtst_inet4_from_string ("6.6.6.0"), - .plen = 24, - .ifindex = fixture->ifindex1, - .gateway = INADDR_ANY, - .metric = 20, - .mss = 0, - .scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK), - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = nmtst_inet4_from_string ("8.0.0.0"), - .plen = 8, - .ifindex = fixture->ifindex1, - .gateway = nmtst_inet4_from_string ("6.6.6.2"), - .metric = 22, - .mss = 0, - .scope_inv = nm_platform_route_scope_inv (RT_SCOPE_UNIVERSE), - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = nmtst_inet4_from_string ("6.6.6.0"), - .plen = 24, - .ifindex = fixture->ifindex1, - .gateway = INADDR_ANY, - /* this is a ghost entry because we synced ifindex0 and restore the route - * with metric 20 (above). But we don't remove the metric 21. */ - .metric = 21, - .mss = 0, - .scope_inv = nm_platform_route_scope_inv (RT_SCOPE_LINK), - }, - }; - - setup_dev0_ip4 (fixture->ifindex0, 1000, 21021); - setup_dev1_ip4 (fixture->ifindex1); - g_test_assert_expected_messages (); - - /* - 6.6.6.0/24 on dev0 won over 6.6.6.0/24 on dev1 - * - 6.6.6.0/24 on dev1 has metric bumped. - * - 7.0.0.0/8 route, metric 21021 added - * - 7.0.0.0/8 route, metric 22 added - * - 8.0.0.0/8 could be added. */ - routes = ip4_routes (fixture); - g_assert_cmpint (routes->len, ==, G_N_ELEMENTS (state1)); - nmtst_platform_ip4_routes_equal ((NMPlatformIP4Route *) routes->data, state1, routes->len, TRUE); - g_array_free (routes, TRUE); - - setup_dev1_ip4 (fixture->ifindex1); - g_test_assert_expected_messages (); - - setup_dev0_ip4 (fixture->ifindex0, 0, 21); - - /* Ensure nothing changed. */ - routes = ip4_routes (fixture); - g_assert_cmpint (routes->len, ==, G_N_ELEMENTS (state1)); - state1[0].mss = 0; - state1[1].metric = 21; - nmtst_platform_ip4_routes_equal ((NMPlatformIP4Route *) routes->data, state1, routes->len, TRUE); - g_array_free (routes, TRUE); - - update_dev0_ip4 (fixture->ifindex0); - - /* minor changes in the routes. Quite similar to state1. */ - routes = ip4_routes (fixture); - g_assert_cmpint (routes->len, ==, G_N_ELEMENTS (state2)); - nmtst_platform_ip4_routes_equal ((NMPlatformIP4Route *) routes->data, state2, routes->len, TRUE); - g_array_free (routes, TRUE); - - nm_route_manager_route_flush (route_manager_get (), fixture->ifindex0); - - /* 6.6.6.0/24 is now on dev1 - * 6.6.6.0/24 is also still on dev1 with bumped metric 21. - * 7.0.0.0/8 gone from dev0, still present on dev1 - * 8.0.0.0/8 is present on dev1 - * No dev0 routes left. */ - routes = ip4_routes (fixture); - g_assert_cmpint (routes->len, ==, G_N_ELEMENTS (state3)); - nmtst_platform_ip4_routes_equal ((NMPlatformIP4Route *) routes->data, state3, routes->len, TRUE); - g_array_free (routes, TRUE); - - nm_route_manager_route_flush (route_manager_get (), fixture->ifindex1); - - /* No routes left. */ - routes = ip4_routes (fixture); - g_assert_cmpint (routes->len, ==, 0); - g_array_free (routes, TRUE); -} - -static void -setup_dev0_ip6 (int ifindex) -{ - GArray *routes = g_array_new (FALSE, FALSE, sizeof (NMPlatformIP6Route)); - NMPlatformIP6Route *route; - - /* Add an address so that a route to the gateway below gets added. */ - nm_platform_ip6_address_add (NM_PLATFORM_GET, - ifindex, - *nmtst_inet6_from_string ("2001:db8:8086::666"), - 64, - in6addr_any, - 3600, - 3600, - 0); - - route = nmtst_platform_ip6_route_full ("2001:db8:8086::", - 48, - NULL, - ifindex, - NM_IP_CONFIG_SOURCE_USER, - 20, - 0); - g_array_append_val (routes, *route); - - route = nmtst_platform_ip6_route_full ("2001:db8:1337::", - 48, - NULL, - ifindex, - NM_IP_CONFIG_SOURCE_USER, - 0, - 0); - g_array_append_val (routes, *route); - - route = nmtst_platform_ip6_route_full ("2001:db8:abad:c0de::", - 64, - "2001:db8:8086::1", - ifindex, - NM_IP_CONFIG_SOURCE_USER, - 21, - 0); - g_array_append_val (routes, *route); - - nm_route_manager_ip6_route_sync (route_manager_get (), ifindex, routes, TRUE, TRUE); - g_array_free (routes, TRUE); -} - -static void -setup_dev1_ip6 (int ifindex) -{ - GArray *routes = g_array_new (FALSE, FALSE, sizeof (NMPlatformIP6Route)); - NMPlatformIP6Route *route; - - /* Add some route outside of route manager. The route manager - * should get rid of it upon sync. */ - nmtstp_ip6_route_add (NM_PLATFORM_GET, - ifindex, - NM_IP_CONFIG_SOURCE_USER, - *nmtst_inet6_from_string ("2001:db8:8088::"), - 48, - in6addr_any, - in6addr_any, - 10, - 0); - - route = nmtst_platform_ip6_route_full ("2001:db8:8086::", - 48, - NULL, - ifindex, - NM_IP_CONFIG_SOURCE_USER, - 20, - 0); - g_array_append_val (routes, *route); - - route = nmtst_platform_ip6_route_full ("2001:db8:1337::", - 48, - NULL, - ifindex, - NM_IP_CONFIG_SOURCE_USER, - 1024, - 0); - g_array_append_val (routes, *route); - - route = nmtst_platform_ip6_route_full ("2001:db8:d34d::", - 64, - "2001:db8:8086::2", - ifindex, - NM_IP_CONFIG_SOURCE_USER, - 20, - 0); - g_array_append_val (routes, *route); - - route = nmtst_platform_ip6_route_full ("2001:db8:abad:c0de::", - 64, - NULL, - ifindex, - NM_IP_CONFIG_SOURCE_USER, - 22, - 0); - g_array_append_val (routes, *route); - - nm_route_manager_ip6_route_sync (route_manager_get (), ifindex, routes, TRUE, TRUE); - g_array_free (routes, TRUE); -} - -static void -update_dev0_ip6 (int ifindex) -{ - GArray *routes = g_array_new (FALSE, FALSE, sizeof (NMPlatformIP6Route)); - NMPlatformIP6Route *route; - - /* Add an address so that a route to the gateway below gets added. */ - nm_platform_ip6_address_add (NM_PLATFORM_GET, - ifindex, - *nmtst_inet6_from_string ("2001:db8:8086::2"), - 64, - in6addr_any, - 3600, - 3600, - 0); - - route = nmtst_platform_ip6_route_full ("2001:db8:8086::", - 48, - NULL, - ifindex, - NM_IP_CONFIG_SOURCE_USER, - 20, - 0); - g_array_append_val (routes, *route); - - route = nmtst_platform_ip6_route_full ("2001:db8:1337::", - 48, - NULL, - ifindex, - NM_IP_CONFIG_SOURCE_USER, - 0, - 0); - g_array_append_val (routes, *route); - - route = nmtst_platform_ip6_route_full ("2001:db8:abad:c0de::", - 64, - NULL, - ifindex, - NM_IP_CONFIG_SOURCE_USER, - 21, - 0); - g_array_append_val (routes, *route); - - nm_route_manager_ip6_route_sync (route_manager_get (), ifindex, routes, TRUE, TRUE); - g_array_free (routes, TRUE); -} - -static GArray * -ip6_routes (test_fixture *fixture) -{ - GArray *routes = nm_platform_ip6_route_get_all (NM_PLATFORM_GET, - fixture->ifindex0, - NM_PLATFORM_GET_ROUTE_FLAGS_WITH_NON_DEFAULT); - GArray *routes1 = nm_platform_ip6_route_get_all (NM_PLATFORM_GET, - fixture->ifindex1, - NM_PLATFORM_GET_ROUTE_FLAGS_WITH_NON_DEFAULT); - - g_array_append_vals (routes, routes1->data, routes1->len); - g_array_free (routes1, TRUE); - - return routes; -} - -static void -test_ip6 (test_fixture *fixture, gconstpointer user_data) -{ - GArray *routes; - - NMPlatformIP6Route state1[] = { - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:8086::"), - .plen = 48, - .ifindex = fixture->ifindex0, - .gateway = in6addr_any, - .metric = 20, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:1337::"), - .plen = 48, - .ifindex = fixture->ifindex0, - .gateway = in6addr_any, - .metric = 1024, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:abad:c0de::"), - .plen = 64, - .ifindex = fixture->ifindex0, - .gateway = *nmtst_inet6_from_string ("2001:db8:8086::1"), - .metric = 21, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:abad:c0de::"), - .plen = 64, - .ifindex = fixture->ifindex1, - .gateway = in6addr_any, - .metric = 22, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:1337::"), - .plen = 48, - .ifindex = fixture->ifindex1, - .gateway = in6addr_any, - .metric = 1025, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:8086::"), - .plen = 48, - .ifindex = fixture->ifindex1, - .gateway = in6addr_any, - .metric = 21, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:d34d::"), - .plen = 64, - .ifindex = fixture->ifindex1, - .gateway = *nmtst_inet6_from_string ("2001:db8:8086::2"), - .metric = 20, - .mss = 0, - }, - }; - - NMPlatformIP6Route state2[] = { - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:8086::"), - .plen = 48, - .ifindex = fixture->ifindex0, - .gateway = in6addr_any, - .metric = 20, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:1337::"), - .plen = 48, - .ifindex = fixture->ifindex0, - .gateway = in6addr_any, - .metric = 1024, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:abad:c0de::"), - .plen = 64, - .ifindex = fixture->ifindex0, - .gateway = in6addr_any, - .metric = 21, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:abad:c0de::"), - .plen = 64, - .ifindex = fixture->ifindex1, - .gateway = in6addr_any, - .metric = 22, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:1337::"), - .plen = 48, - .ifindex = fixture->ifindex1, - .gateway = in6addr_any, - .metric = 1025, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:8086::"), - .plen = 48, - .ifindex = fixture->ifindex1, - .gateway = in6addr_any, - .metric = 21, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:d34d::"), - .plen = 64, - .ifindex = fixture->ifindex1, - .gateway = *nmtst_inet6_from_string ("2001:db8:8086::2"), - .metric = 20, - .mss = 0, - }, - }; - - NMPlatformIP6Route state3[] = { - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:abad:c0de::"), - .plen = 64, - .ifindex = fixture->ifindex1, - .gateway = in6addr_any, - .metric = 22, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:8086::"), - .plen = 48, - .ifindex = fixture->ifindex1, - .gateway = in6addr_any, - .metric = 20, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:1337::"), - .plen = 48, - .ifindex = fixture->ifindex1, - .gateway = in6addr_any, - .metric = 1024, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:1337::"), - .plen = 48, - .ifindex = fixture->ifindex1, - .gateway = in6addr_any, - .metric = 1025, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:8086::"), - .plen = 48, - .ifindex = fixture->ifindex1, - .gateway = in6addr_any, - .metric = 21, - .mss = 0, - }, - { - .rt_source = nmp_utils_ip_config_source_round_trip_rtprot (NM_IP_CONFIG_SOURCE_USER), - .network = *nmtst_inet6_from_string ("2001:db8:d34d::"), - .plen = 64, - .ifindex = fixture->ifindex1, - .gateway = *nmtst_inet6_from_string ("2001:db8:8086::2"), - .metric = 20, - .mss = 0, - }, - }; - - setup_dev0_ip6 (fixture->ifindex0); - setup_dev1_ip6 (fixture->ifindex1); - g_test_assert_expected_messages (); - - /* 2001:db8:8086::/48 on dev0 won over 2001:db8:8086::/48 on dev1 - * 2001:db8:d34d::/64 on dev1 could not be added - * 2001:db8:1337::/48 on dev0 won over 2001:db8:1337::/48 on dev1 and has metric 1024 - * 2001:db8:abad:c0de::/64 routes did not clash */ - routes = ip6_routes (fixture); - g_assert_cmpint (routes->len, ==, G_N_ELEMENTS (state1)); - nmtst_platform_ip6_routes_equal ((NMPlatformIP6Route *) routes->data, state1, routes->len, TRUE); - g_array_free (routes, TRUE); - - - setup_dev1_ip6 (fixture->ifindex1); - g_test_assert_expected_messages (); - setup_dev0_ip6 (fixture->ifindex0); - - /* Ensure nothing changed. */ - routes = ip6_routes (fixture); - g_assert_cmpint (routes->len, ==, G_N_ELEMENTS (state1)); - nmtst_platform_ip6_routes_equal ((NMPlatformIP6Route *) routes->data, state1, routes->len, TRUE); - g_array_free (routes, TRUE); - - update_dev0_ip6 (fixture->ifindex0); - - /* 2001:db8:abad:c0de::/64 on dev0 was updated for gateway removal*/ - routes = ip6_routes (fixture); - g_assert_cmpint (routes->len, ==, G_N_ELEMENTS (state2)); - nmtst_platform_ip6_routes_equal ((NMPlatformIP6Route *) routes->data, state2, routes->len, TRUE); - g_array_free (routes, TRUE); - - nm_route_manager_route_flush (route_manager_get (), fixture->ifindex0); - - /* 2001:db8:abad:c0de::/64 on dev1 is still there, went away from dev0 - * 2001:db8:8086::/48 is now on dev1 - * 2001:db8:1337::/48 is now on dev1, metric of 1024 still applies - * 2001:db8:d34d::/64 is present now that 2001:db8:8086::/48 is on dev1 - * No dev0 routes left. */ - routes = ip6_routes (fixture); - g_assert_cmpint (routes->len, ==, G_N_ELEMENTS (state3)); - nmtst_platform_ip6_routes_equal ((NMPlatformIP6Route *) routes->data, state3, routes->len, TRUE); - g_array_free (routes, TRUE); - - nm_route_manager_route_flush (route_manager_get (), fixture->ifindex1); - - /* No routes left. */ - routes = ip6_routes (fixture); - g_assert_cmpint (routes->len, ==, 0); - g_array_free (routes, TRUE); -} - -/*****************************************************************************/ - -static void -_assert_route_check (const NMPlatformVTableRoute *vtable, gboolean has, const NMPlatformIPXRoute *route) -{ - const NMPlatformIPXRoute *r; - NMPlatformIPXRoute c; - - g_assert (route); - - if (vtable->is_ip4) - r = (const NMPlatformIPXRoute *) nm_platform_ip4_route_get (NM_PLATFORM_GET, route->rx.ifindex, route->r4.network, route->rx.plen, route->rx.metric); - else - r = (const NMPlatformIPXRoute *) nm_platform_ip6_route_get (NM_PLATFORM_GET, route->rx.ifindex, route->r6.network, route->rx.plen, route->rx.metric); - - if (!has) { - g_assert (!r); - } else { - char buf[sizeof (_nm_utils_to_string_buffer)]; - - if (r) { - if (vtable->is_ip4) - c.r4 = route->r4; - else - c.r6 = route->r6; - c.rx.rt_source = nmp_utils_ip_config_source_round_trip_rtprot (c.rx.rt_source); - } - if (!r || vtable->route_cmp (r, &c, TRUE) != 0) { - g_error ("Invalid route. Expect %s, has %s", - vtable->route_to_string (&c, NULL, 0), - vtable->route_to_string (r, buf, sizeof (buf))); - } - } -} - -static void -test_ip4_full_sync (test_fixture *fixture, gconstpointer user_data) -{ - const NMPlatformVTableRoute *vtable = &nm_platform_vtable_route_v4; - gs_unref_array GArray *routes = g_array_new (FALSE, FALSE, sizeof (NMPlatformIP4Route)); - NMPlatformIP4Route r01, r02, r03; - - nm_log_dbg (LOGD_CORE, "TEST start test_ip4_full_sync(): start"); - - r01 = *nmtst_platform_ip4_route_full ("12.3.4.0", 24, NULL, - fixture->ifindex0, NM_IP_CONFIG_SOURCE_USER, - 100, 0, RT_SCOPE_LINK, NULL); - r02 = *nmtst_platform_ip4_route_full ("13.4.5.6", 32, "12.3.4.1", - fixture->ifindex0, NM_IP_CONFIG_SOURCE_USER, - 100, 0, RT_SCOPE_UNIVERSE, NULL); - r03 = *nmtst_platform_ip4_route_full ("14.5.6.7", 32, "12.3.4.1", - fixture->ifindex0, NM_IP_CONFIG_SOURCE_USER, - 110, 0, RT_SCOPE_UNIVERSE, NULL); - g_array_set_size (routes, 2); - g_array_index (routes, NMPlatformIP4Route, 0) = r01; - g_array_index (routes, NMPlatformIP4Route, 1) = r02; - nm_route_manager_ip4_route_sync (route_manager_get (), fixture->ifindex0, routes, TRUE, TRUE); - - _assert_route_check (vtable, TRUE, (const NMPlatformIPXRoute *) &r01); - _assert_route_check (vtable, TRUE, (const NMPlatformIPXRoute *) &r02); - _assert_route_check (vtable, FALSE, (const NMPlatformIPXRoute *) &r03); - - vtable->route_add (NM_PLATFORM_GET, 0, (const NMPlatformIPXRoute *) &r03, -1); - - _assert_route_check (vtable, TRUE, (const NMPlatformIPXRoute *) &r01); - _assert_route_check (vtable, TRUE, (const NMPlatformIPXRoute *) &r02); - _assert_route_check (vtable, TRUE, (const NMPlatformIPXRoute *) &r03); - - nm_route_manager_ip4_route_sync (route_manager_get (), fixture->ifindex0, routes, TRUE, FALSE); - - _assert_route_check (vtable, TRUE, (const NMPlatformIPXRoute *) &r01); - _assert_route_check (vtable, TRUE, (const NMPlatformIPXRoute *) &r02); - _assert_route_check (vtable, TRUE, (const NMPlatformIPXRoute *) &r03); - - g_array_set_size (routes, 1); - - nm_route_manager_ip4_route_sync (route_manager_get (), fixture->ifindex0, routes, TRUE, FALSE); - - _assert_route_check (vtable, TRUE, (const NMPlatformIPXRoute *) &r01); - _assert_route_check (vtable, FALSE, (const NMPlatformIPXRoute *) &r02); - _assert_route_check (vtable, TRUE, (const NMPlatformIPXRoute *) &r03); - - nm_route_manager_ip4_route_sync (route_manager_get (), fixture->ifindex0, routes, TRUE, TRUE); - - _assert_route_check (vtable, TRUE, (const NMPlatformIPXRoute *) &r01); - _assert_route_check (vtable, FALSE, (const NMPlatformIPXRoute *) &r02); - _assert_route_check (vtable, FALSE, (const NMPlatformIPXRoute *) &r03); - - nm_log_dbg (LOGD_CORE, "TEST test_ip4_full_sync(): done"); -} - -/*****************************************************************************/ - -static void -fixture_setup (test_fixture *fixture, gconstpointer user_data) -{ - SignalData *link_added; - - link_added = add_signal_ifname (NM_PLATFORM_SIGNAL_LINK_CHANGED, - NM_PLATFORM_SIGNAL_ADDED, - link_callback, - "nm-test-device0"); - nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, "nm-test-device0")); - g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, "nm-test-device0")); - g_assert (nm_platform_link_dummy_add (NM_PLATFORM_GET, "nm-test-device0", NULL) == NM_PLATFORM_ERROR_SUCCESS); - accept_signal (link_added); - free_signal (link_added); - fixture->ifindex0 = nm_platform_link_get_ifindex (NM_PLATFORM_GET, "nm-test-device0"); - g_assert (nm_platform_link_set_up (NM_PLATFORM_GET, fixture->ifindex0, NULL)); - - link_added = add_signal_ifname (NM_PLATFORM_SIGNAL_LINK_CHANGED, - NM_PLATFORM_SIGNAL_ADDED, - link_callback, - "nm-test-device1"); - nm_platform_link_delete (NM_PLATFORM_GET, nm_platform_link_get_ifindex (NM_PLATFORM_GET, "nm-test-device1")); - g_assert (!nm_platform_link_get_by_ifname (NM_PLATFORM_GET, "nm-test-device1")); - g_assert (nm_platform_link_dummy_add (NM_PLATFORM_GET, "nm-test-device1", NULL) == NM_PLATFORM_ERROR_SUCCESS); - accept_signal (link_added); - free_signal (link_added); - fixture->ifindex1 = nm_platform_link_get_ifindex (NM_PLATFORM_GET, "nm-test-device1"); - g_assert (nm_platform_link_set_up (NM_PLATFORM_GET, fixture->ifindex1, NULL)); -} - -static void -fixture_teardown (test_fixture *fixture, gconstpointer user_data) -{ - nm_platform_link_delete (NM_PLATFORM_GET, fixture->ifindex0); - nm_platform_link_delete (NM_PLATFORM_GET, fixture->ifindex1); -} - -/*****************************************************************************/ - -NMTstpSetupFunc const _nmtstp_setup_platform_func = SETUP; - -void -_nmtstp_init_tests (int *argc, char ***argv) -{ - nmtst_init_assert_logging (argc, argv, "WARN", "ALL"); -} - -void -_nmtstp_setup_tests (void) -{ - g_test_add ("/route-manager/ip4", test_fixture, NULL, fixture_setup, test_ip4, fixture_teardown); - g_test_add ("/route-manager/ip6", test_fixture, NULL, fixture_setup, test_ip6, fixture_teardown); - - g_test_add ("/route-manager/ip4-full-sync", test_fixture, NULL, fixture_setup, test_ip4_full_sync, fixture_teardown); -} diff --git a/src/tests/test-systemd.c b/src/tests/test-systemd.c index f6cf9b58..ab5fed22 100644 --- a/src/tests/test-systemd.c +++ b/src/tests/test-systemd.c @@ -78,7 +78,7 @@ test_dhcp_create (void) sd_dhcp_client *client4 = NULL; int r; - r = sd_dhcp_client_new (&client4); + r = sd_dhcp_client_new (&client4, FALSE); g_assert (r == 0); g_assert (client4); |