diff options
Diffstat (limited to 'libnm-core/tests')
| -rw-r--r-- | libnm-core/tests/certs/test-tpm2wrapped-key.pem | 14 | ||||
| -rw-r--r-- | libnm-core/tests/test-compare.c | 1 | ||||
| -rw-r--r-- | libnm-core/tests/test-crypto.c | 2 | ||||
| -rw-r--r-- | libnm-core/tests/test-general-enums.h | 1 | ||||
| -rw-r--r-- | libnm-core/tests/test-general.c | 110 | ||||
| -rw-r--r-- | libnm-core/tests/test-keyfile.c | 28 | ||||
| -rw-r--r-- | libnm-core/tests/test-secrets.c | 3 | ||||
| -rw-r--r-- | libnm-core/tests/test-setting.c | 462 | ||||
| -rw-r--r-- | libnm-core/tests/test-settings-defaults.c | 1 |
9 files changed, 487 insertions, 135 deletions
diff --git a/libnm-core/tests/certs/test-tpm2wrapped-key.pem b/libnm-core/tests/certs/test-tpm2wrapped-key.pem new file mode 100644 index 00000000..f3fd271c --- /dev/null +++ b/libnm-core/tests/certs/test-tpm2wrapped-key.pem @@ -0,0 +1,14 @@ +-----BEGIN TSS2 PRIVATE KEY----- +MIICEwYGZ4EFCgEDoAMBAQECBQCBAAABBIIBGAEWAAEACwACBEAAAAAQABAIAAAA +AAABAOJdEXw1LO6JWskHBSYQc1NfJAe9DOAyLA4XbXI5asQ8aNbmL51DP9mQQpqq +a1CSRZAIuuorMxyRBBAFpF4OZqjNd/Nskp3iMmifr5yqAYZ3M31MqlBFiiyctqKp +VwIChwsIbKelrsXbty1icP2CH+k4w/nPymPjnfYtgpMe8QW8n6U156ujIdJcISds +QFcl3nrDnD1IumX0/LfanQrRDVSI+m6szvTrdsPMtGeaNMnlz0gx74auo7/CgEjX +69xjPvQpNLHO/nV4EHSdfXH3LamcpWO8aEAVne3MFFA9V0bpv7uKoGhRjssD9kSr +PQzNXfjHpkcOLeH4pzxDMFXDIGUEgeAA3gAgrCL3RRcBryFXToo9ZN3/f4EeeEjK +58ejYomsxqvckhgAEMxbT26fo2h27b4KPlnUpoiL2JPLB0Xz6PJAF8n0YdJUO381 +xhzPTIQop81BxljTLV2C9WGns5bWDPW9ItEbv4UalEwFfxsW4Ma5smLWn3A1UwVN +Z1cW/oUx+3nOCF1TZgbMPszToqCPlpIHd9vO709qpSyULIUkZLHS6PUM7ESY5U81 +f4BITxJR+aYaqErni/FDLsDVP0MQ3CuFHeUDHI0uEzzbfurYFjpp9+caaoWuZzpg +VYV5pjPdgg== +-----END TSS2 PRIVATE KEY----- diff --git a/libnm-core/tests/test-compare.c b/libnm-core/tests/test-compare.c index 7f2ba980..ec4ca437 100644 --- a/libnm-core/tests/test-compare.c +++ b/libnm-core/tests/test-compare.c @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/libnm-core/tests/test-crypto.c b/libnm-core/tests/test-crypto.c index 4ac9679b..9eab29d4 100644 --- a/libnm-core/tests/test-crypto.c +++ b/libnm-core/tests/test-crypto.c @@ -1,5 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ - /* * Dan Williams <dcbw@redhat.com> * diff --git a/libnm-core/tests/test-general-enums.h b/libnm-core/tests/test-general-enums.h index 8aa3f99f..72981171 100644 --- a/libnm-core/tests/test-general-enums.h +++ b/libnm-core/tests/test-general-enums.h @@ -1,4 +1,3 @@ -/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ /* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index a6abf391..601e3edc 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -1,4 +1,3 @@ -/* -*- 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 @@ -32,6 +31,7 @@ #include "nm-utils-private.h" #include "nm-core-internal.h" #include "nm-core-tests-enum-types.h" +#include "nm-team-utils.h" #include "nm-setting-8021x.h" #include "nm-setting-adsl.h" @@ -600,7 +600,7 @@ test_nm_utils_strsplit_set (void) words_exp = g_ptr_array_new_with_free_func (g_free); for (test_run = 0; test_run < 100; test_run++) { gboolean f_allow_escaping = nmtst_get_rand_bool (); - guint words_len = nmtst_get_rand_int () % 100; + guint words_len = nmtst_get_rand_uint32 () % 100; gs_free char *str = NULL; guint i; @@ -610,14 +610,14 @@ test_nm_utils_strsplit_set (void) char *word; guint j; - word_len = nmtst_get_rand_int (); + word_len = nmtst_get_rand_uint32 (); if ((word_len % 100) < 30) word_len = 0; else word_len = (word_len >> 10) % 100; word = g_new (char, word_len + 3); for (j = 0; j < word_len; ) { - guint32 p = nmtst_get_rand_int (); + guint32 p = nmtst_get_rand_uint32 (); static const char delimiters_arr[] = { DELIMITERS_C }; static const char regular_chars[] = "abcdefghijklmnopqrstuvwxyz"; @@ -705,7 +705,7 @@ _do_test_c_list_sort (CListSort *elements, guint n_list, gboolean headless) c_list_init (&head); for (i = 0; i < n_list; i++) { el = &elements[i]; - el->val = nmtst_get_rand_int () % (2*n_list); + el->val = nmtst_get_rand_uint32 () % (2*n_list); c_list_link_tail (&head, &el->lst); } @@ -766,7 +766,7 @@ test_c_list_sort (void) elements = g_new0 (CListSort, N_ELEMENTS); for (n_list = 1; n_list < N_ELEMENTS; n_list++) { if (n_list > 150) { - n_list += nmtst_get_rand_int () % n_list; + n_list += nmtst_get_rand_uint32 () % n_list; if (n_list >= N_ELEMENTS) break; } @@ -774,7 +774,7 @@ test_c_list_sort (void) const guint N_REPEAT = n_list > 50 ? 1 : 5; for (repeat = 0; repeat < N_REPEAT; repeat++) - _do_test_c_list_sort (elements, n_list, nmtst_get_rand_int () % 2); + _do_test_c_list_sort (elements, n_list, nmtst_get_rand_uint32 () % 2); } } } @@ -1855,7 +1855,7 @@ test_setting_to_dbus_all (void) s_wsec = make_test_wsec_setting ("setting-to-dbus-all"); - dict = _nm_setting_to_dbus (NM_SETTING (s_wsec), NULL, NM_CONNECTION_SERIALIZE_ALL); + dict = _nm_setting_to_dbus (NM_SETTING (s_wsec), NULL, NM_CONNECTION_SERIALIZE_ALL, NULL); /* Make sure all keys are there */ g_assert (_variant_contains (dict, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT)); @@ -1875,7 +1875,7 @@ test_setting_to_dbus_no_secrets (void) s_wsec = make_test_wsec_setting ("setting-to-dbus-no-secrets"); - dict = _nm_setting_to_dbus (NM_SETTING (s_wsec), NULL, NM_CONNECTION_SERIALIZE_NO_SECRETS); + dict = _nm_setting_to_dbus (NM_SETTING (s_wsec), NULL, NM_CONNECTION_SERIALIZE_NO_SECRETS, NULL); /* Make sure non-secret keys are there */ g_assert (_variant_contains (dict, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT)); @@ -1897,7 +1897,7 @@ test_setting_to_dbus_only_secrets (void) s_wsec = make_test_wsec_setting ("setting-to-dbus-only-secrets"); - dict = _nm_setting_to_dbus (NM_SETTING (s_wsec), NULL, NM_CONNECTION_SERIALIZE_ONLY_SECRETS); + dict = _nm_setting_to_dbus (NM_SETTING (s_wsec), NULL, NM_CONNECTION_SERIALIZE_ONLY_SECRETS, NULL); /* Make sure non-secret keys are not there */ g_assert (!_variant_contains (dict, NM_SETTING_WIRELESS_SECURITY_KEY_MGMT)); @@ -1928,7 +1928,7 @@ test_setting_to_dbus_transform (void) g_assert_cmpstr (nm_setting_wired_get_mac_address (NM_SETTING_WIRED (s_wired)), ==, test_mac_address); - dict = _nm_setting_to_dbus (s_wired, NULL, NM_CONNECTION_SERIALIZE_ALL); + dict = _nm_setting_to_dbus (s_wired, NULL, NM_CONNECTION_SERIALIZE_ALL, NULL); g_assert (dict != NULL); val = g_variant_lookup_value (dict, NM_SETTING_WIRED_MAC_ADDRESS, G_VARIANT_TYPE_BYTESTRING); @@ -1957,7 +1957,7 @@ test_setting_to_dbus_enum (void) NM_SETTING_IP6_CONFIG_IP6_PRIVACY, NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR, NULL); - dict = _nm_setting_to_dbus (s_ip6, NULL, NM_CONNECTION_SERIALIZE_ALL); + dict = _nm_setting_to_dbus (s_ip6, NULL, NM_CONNECTION_SERIALIZE_ALL, NULL); g_assert (dict != NULL); val = g_variant_lookup_value (dict, NM_SETTING_IP6_CONFIG_IP6_PRIVACY, G_VARIANT_TYPE_INT32); @@ -1976,7 +1976,7 @@ test_setting_to_dbus_enum (void) NM_SETTING_SECRET_FLAG_NOT_SAVED), NULL); - dict = _nm_setting_to_dbus (s_wsec, NULL, NM_CONNECTION_SERIALIZE_ALL); + dict = _nm_setting_to_dbus (s_wsec, NULL, NM_CONNECTION_SERIALIZE_ALL, NULL); g_assert (dict != NULL); val = g_variant_lookup_value (dict, NM_SETTING_WIRELESS_SECURITY_WEP_KEY_TYPE, G_VARIANT_TYPE_UINT32); @@ -1999,7 +1999,7 @@ test_setting_to_dbus_enum (void) NM_SETTING_SERIAL_PARITY, NM_SETTING_SERIAL_PARITY_ODD, NULL); - dict = _nm_setting_to_dbus (s_serial, NULL, NM_CONNECTION_SERIALIZE_ALL); + dict = _nm_setting_to_dbus (s_serial, NULL, NM_CONNECTION_SERIALIZE_ALL, NULL); g_assert (dict != NULL); val = g_variant_lookup_value (dict, NM_SETTING_SERIAL_PARITY, G_VARIANT_TYPE_BYTE); @@ -2114,7 +2114,7 @@ test_setting_new_from_dbus (void) GVariant *dict; s_wsec = make_test_wsec_setting ("setting-new-from-dbus"); - dict = _nm_setting_to_dbus (NM_SETTING (s_wsec), NULL, NM_CONNECTION_SERIALIZE_ALL); + dict = _nm_setting_to_dbus (NM_SETTING (s_wsec), NULL, NM_CONNECTION_SERIALIZE_ALL, NULL); g_object_unref (s_wsec); s_wsec = (NMSettingWirelessSecurity *) _nm_setting_new_from_dbus (NM_TYPE_SETTING_WIRELESS_SECURITY, dict, NULL, NM_SETTING_PARSE_FLAGS_NONE, NULL); @@ -3024,6 +3024,7 @@ test_connection_diff_a_only (void) { NM_SETTING_CONNECTION_AUTH_RETRIES, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_CONNECTION_MDNS, NM_SETTING_DIFF_RESULT_IN_A }, { NM_SETTING_CONNECTION_LLMNR, NM_SETTING_DIFF_RESULT_IN_A }, + { NM_SETTING_CONNECTION_WAIT_DEVICE_TIMEOUT, NM_SETTING_DIFF_RESULT_IN_A }, { NULL, NM_SETTING_DIFF_RESULT_UNKNOWN } } }, { NM_SETTING_WIRED_SETTING_NAME, { @@ -3500,7 +3501,7 @@ test_setting_compare_addresses (void) nm_ip_address_unref (a); - if (nmtst_get_rand_int () % 2) + if (nmtst_get_rand_uint32 () % 2) NMTST_SWAP (s1, s2); success = nm_setting_compare (s1, s2, NM_SETTING_COMPARE_FLAG_EXACT); @@ -3532,7 +3533,7 @@ test_setting_compare_routes (void) nm_ip_route_unref (r); - if (nmtst_get_rand_int () % 2) + if (nmtst_get_rand_uint32 () % 2) NMTST_SWAP (s1, s2); success = nm_setting_compare (s1, s2, NM_SETTING_COMPARE_FLAG_EXACT); @@ -6080,7 +6081,7 @@ test_hexstr2bin (void) static void _do_strquote (const char *str, gsize buf_len, const char *expected) { - char canary = (char) nmtst_get_rand_int (); + char canary = (char) nmtst_get_rand_uint32 (); gs_free char *buf_full = g_malloc (buf_len + 2); char *buf = &buf_full[1]; const char *b; @@ -6807,14 +6808,42 @@ _team_config_equal_check (const char *conf1, gboolean port_config, gboolean expected) { - g_assert_cmpint (_nm_utils_team_config_equal (conf1, conf2, port_config), ==, expected); + nm_auto_free_team_setting NMTeamSetting *team_a = NULL; + nm_auto_free_team_setting NMTeamSetting *team_b = NULL; + gboolean is_same; + + if (nmtst_get_rand_bool ()) + NMTST_SWAP (conf1, conf2); + + if (!nm_streq0 (conf1, conf2)) { + _team_config_equal_check (conf1, conf1, port_config, TRUE); + _team_config_equal_check (conf2, conf2, port_config, TRUE); + } + + team_a = nm_team_setting_new (port_config, conf1); + team_b = nm_team_setting_new (port_config, conf2); + + is_same = (nm_team_setting_cmp (team_a, team_b, TRUE) == 0); + g_assert_cmpint (is_same, ==, expected); + + if (nm_streq0 (conf1, conf2)) { + g_assert_cmpint (nm_team_setting_cmp (team_a, team_b, FALSE), ==, 0); + g_assert (expected); + } else + g_assert_cmpint (nm_team_setting_cmp (team_a, team_b, FALSE), !=, 0); } static void test_nm_utils_team_config_equal (void) { -#if WITH_JSON_VALIDATION - _team_config_equal_check ("", "", TRUE, TRUE); + _team_config_equal_check ("", + "", + TRUE, + TRUE); + _team_config_equal_check ("", + " ", + TRUE, + TRUE); _team_config_equal_check ("{}", "{ }", TRUE, @@ -6822,21 +6851,25 @@ test_nm_utils_team_config_equal (void) _team_config_equal_check ("{}", "{", TRUE, - FALSE); + TRUE); + _team_config_equal_check ("{ \"a\": 1 }", + "{ \"a\": 1 }", + TRUE, + TRUE); + _team_config_equal_check ("{ \"a\": 1 }", + "{ \"a\": 1 }", + TRUE, + TRUE); /* team config */ _team_config_equal_check ("{ }", - "{ \"runner\" : { \"name\" : \"roundrobin\"} }", - FALSE, - TRUE); - _team_config_equal_check ("{ }", "{ \"runner\" : { \"name\" : \"random\"} }", FALSE, - FALSE); + !WITH_JSON_VALIDATION); _team_config_equal_check ("{ \"runner\" : { \"name\" : \"roundrobin\"} }", "{ \"runner\" : { \"name\" : \"random\"} }", FALSE, - FALSE); + !WITH_JSON_VALIDATION); _team_config_equal_check ("{ \"runner\" : { \"name\" : \"random\"} }", "{ \"runner\" : { \"name\" : \"random\"} }", FALSE, @@ -6852,29 +6885,29 @@ test_nm_utils_team_config_equal (void) _team_config_equal_check ("{ \"runner\" : { \"name\" : \"lacp\"} }", "{ \"runner\" : { \"name\" : \"lacp\", \"tx_hash\" : [ \"eth\", \"ipv4\", \"ipv6\" ] } }", FALSE, - TRUE); + !WITH_JSON_VALIDATION); _team_config_equal_check ("{ \"runner\" : { \"name\" : \"roundrobin\"} }", "{ \"runner\" : { \"name\" : \"roundrobin\", \"tx_hash\" : [ \"eth\", \"ipv4\", \"ipv6\" ] } }", FALSE, - FALSE); + !WITH_JSON_VALIDATION); _team_config_equal_check ("{ \"runner\" : { \"name\" : \"lacp\"} }", "{ \"runner\" : { \"name\" : \"lacp\", \"tx_hash\" : [ \"eth\" ] } }", FALSE, - FALSE); + !WITH_JSON_VALIDATION); /* team port config */ _team_config_equal_check ("{ }", "{ \"link_watch\" : { \"name\" : \"ethtool\"} }", TRUE, - TRUE); + !WITH_JSON_VALIDATION); _team_config_equal_check ("{ }", "{ \"link_watch\" : { \"name\" : \"arp_ping\"} }", TRUE, - FALSE); + TRUE); _team_config_equal_check ("{ \"link_watch\" : { \"name\" : \"ethtool\"} }", "{ \"link_watch\" : { \"name\" : \"arp_ping\"} }", TRUE, - FALSE); + !WITH_JSON_VALIDATION); _team_config_equal_check ("{ \"link_watch\" : { \"name\" : \"arp_ping\"} }", "{ \"link_watch\" : { \"name\" : \"arp_ping\"} }", TRUE, @@ -6883,13 +6916,6 @@ test_nm_utils_team_config_equal (void) "{ \"link_watch\" : { \"name\" : \"arp_ping\"}, \"ports\" : { \"eth1\" : {} } }", TRUE, TRUE); -#else - /* Without JSON library, strings are compared for equality */ - _team_config_equal_check ("", "", TRUE, TRUE); - _team_config_equal_check ("", " ", TRUE, FALSE); - _team_config_equal_check ("{ \"a\": 1 }", "{ \"a\": 1 }", TRUE, TRUE); - _team_config_equal_check ("{ \"a\": 1 }", "{ \"a\": 1 }", TRUE, FALSE); -#endif } /*****************************************************************************/ @@ -7188,7 +7214,7 @@ test_nm_utils_ptrarray_find_binary_search_with_duplicates (void) /* fill with random numbers... surely there are some duplicates * there... or maybe even there are none... */ for (i = 0; i < i_len; i++) - arr[i] = GINT_TO_POINTER (nmtst_get_rand_int () % (i_len + BIN_SEARCH_W_DUPS_JITTER)); + arr[i] = GINT_TO_POINTER (nmtst_get_rand_uint32 () % (i_len + BIN_SEARCH_W_DUPS_JITTER)); g_qsort_with_data (arr, i_len, sizeof (gpointer), diff --git a/libnm-core/tests/test-keyfile.c b/libnm-core/tests/test-keyfile.c index 157e8f18..195f3797 100644 --- a/libnm-core/tests/test-keyfile.c +++ b/libnm-core/tests/test-keyfile.c @@ -1,4 +1,3 @@ -/* -*- 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 @@ -36,6 +35,7 @@ #define TEST_CERT_DIR NM_BUILD_SRCDIR"/libnm-core/tests/certs" #define TEST_WIRED_TLS_CA_CERT TEST_CERT_DIR"/test-ca-cert.pem" #define TEST_WIRED_TLS_PRIVKEY TEST_CERT_DIR"/test-key-and-cert.pem" +#define TEST_WIRED_TLS_TPM2KEY TEST_CERT_DIR"/test-tpm2wrapped-key.pem" /*****************************************************************************/ @@ -378,15 +378,15 @@ _test_8021x_cert_check_blob_full (NMConnection *con, const void *data, gsize len #define _test_8021x_cert_check_blob(con, data) _test_8021x_cert_check_blob_full(con, data, NM_STRLEN (data)) static void -test_8021x_cert (void) +_test_8021x_cert_from_files (const char *cert, const char *key) { NMSetting8021x *s_8021x; gs_unref_object NMConnection *con = nmtst_create_minimal_connection ("test-cert", NULL, NM_SETTING_WIRED_SETTING_NAME, NULL); GError *error = NULL; gboolean success; NMSetting8021xCKScheme scheme = NM_SETTING_802_1X_CK_SCHEME_PATH; - gs_free char *full_TEST_WIRED_TLS_CA_CERT = nmtst_file_resolve_relative_path (TEST_WIRED_TLS_CA_CERT, NULL); - gs_free char *full_TEST_WIRED_TLS_PRIVKEY = nmtst_file_resolve_relative_path (TEST_WIRED_TLS_PRIVKEY, NULL); + gs_free char *full_TEST_WIRED_TLS_CA_CERT = nmtst_file_resolve_relative_path (cert, NULL); + gs_free char *full_TEST_WIRED_TLS_PRIVKEY = nmtst_file_resolve_relative_path (key, NULL); /* test writing/reading of certificates of NMSetting8021x */ @@ -445,6 +445,18 @@ test_8021x_cert (void) } +static void +test_8021x_cert (void) +{ + _test_8021x_cert_from_files (TEST_WIRED_TLS_CA_CERT, TEST_WIRED_TLS_PRIVKEY); +} + +static void +test_8021x_cert_tpm2key (void) +{ + _test_8021x_cert_from_files (TEST_WIRED_TLS_CA_CERT, TEST_WIRED_TLS_TPM2KEY); +} + /*****************************************************************************/ static void @@ -626,11 +638,15 @@ test_team_conf_read_valid (void) static void test_team_conf_read_invalid (void) { -#if WITH_JSON_VALIDATION GKeyFile *keyfile = NULL; gs_unref_object NMConnection *con = NULL; NMSettingTeam *s_team; + if (!WITH_JSON_VALIDATION) { + g_test_skip ("team test requires JSON validation"); + return; + } + con = nmtst_create_connection_from_keyfile ( "[connection]\n" "type=team\n" @@ -645,7 +661,6 @@ test_team_conf_read_invalid (void) g_assert (nm_setting_team_get_config (s_team) == NULL); CLEAR (&con, &keyfile); -#endif } /*****************************************************************************/ @@ -848,6 +863,7 @@ int main (int argc, char **argv) g_test_add_func ("/core/keyfile/encode_key", test_encode_key); g_test_add_func ("/core/keyfile/test_8021x_cert", test_8021x_cert); + g_test_add_func ("/core/keyfile/test_8021x_cert_tpm2key", test_8021x_cert_tpm2key); g_test_add_func ("/core/keyfile/test_8021x_cert_read", test_8021x_cert_read); g_test_add_func ("/core/keyfile/test_team_conf_read/valid", test_team_conf_read_valid); g_test_add_func ("/core/keyfile/test_team_conf_read/invalid", test_team_conf_read_invalid); diff --git a/libnm-core/tests/test-secrets.c b/libnm-core/tests/test-secrets.c index 5debe5cf..d657a43c 100644 --- a/libnm-core/tests/test-secrets.c +++ b/libnm-core/tests/test-secrets.c @@ -1,4 +1,3 @@ -/* -*- 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 @@ -649,7 +648,7 @@ test_update_secrets_null_setting_name_with_setting_hash (void) secrets = build_wep_secrets (wepkey); - NMTST_EXPECT_LIBNM_CRITICAL (NMTST_G_RETURN_MSG (setting_name != NULL || full_connection)); + NMTST_EXPECT_LIBNM_CRITICAL (NMTST_G_RETURN_MSG (setting_name || full_connection)); success = nm_connection_update_secrets (connection, NULL, secrets, &error); g_test_assert_expected_messages (); g_assert_no_error (error); diff --git a/libnm-core/tests/test-setting.c b/libnm-core/tests/test-setting.c index 03100a03..aa9de5a0 100644 --- a/libnm-core/tests/test-setting.c +++ b/libnm-core/tests/test-setting.c @@ -45,6 +45,15 @@ /*****************************************************************************/ +/* assert that the define is just a plain integer (boolean). */ + +G_STATIC_ASSERT ( (WITH_JSON_VALIDATION) == 1 + || (WITH_JSON_VALIDATION) == 0); + +_nm_unused static const int _with_json_validation = WITH_JSON_VALIDATION; + +/*****************************************************************************/ + /* converts @dict to a connection. In this case, @dict must be good, without warnings, so that * NM_SETTING_PARSE_FLAGS_STRICT and NM_SETTING_PARSE_FLAGS_BEST_EFFORT yield the exact same results. */ static NMConnection * @@ -92,8 +101,8 @@ _connection_new_from_dbus_strict (GVariant *dict, * after accounting for normalization. */ for (i = 0; i < 10; i++) { NMConnection *cons[] = { con_x_0, con_x_s, con_x_e, con_n_0, con_n_s, con_n_e }; - guint idx_a = (nmtst_get_rand_int () % G_N_ELEMENTS (cons)); - guint idx_b = (nmtst_get_rand_int () % G_N_ELEMENTS (cons)); + guint idx_a = (nmtst_get_rand_uint32 () % G_N_ELEMENTS (cons)); + guint idx_b = (nmtst_get_rand_uint32 () % G_N_ELEMENTS (cons)); gboolean normalize_a, normalize_b; if (idx_a <= 2 && idx_b <= 2) { @@ -128,14 +137,14 @@ _create_random_ipaddr (int addr_family, gboolean as_service) g_assert (NM_IN_SET (addr_family, AF_INET, AF_INET6)); if (as_service) - num = (nmtst_get_rand_int () % 1000) + 30000; + num = (nmtst_get_rand_uint32 () % 1000) + 30000; else num = addr_family == AF_INET ? 32 : 128; if (addr_family == AF_INET) - return g_strdup_printf ("192.168.%u.%u%c%d", nmtst_get_rand_int () % 256, nmtst_get_rand_int () % 256, delimiter, num); + return g_strdup_printf ("192.168.%u.%u%c%d", nmtst_get_rand_uint32 () % 256, nmtst_get_rand_uint32 () % 256, delimiter, num); else - return g_strdup_printf ("a:b:c::%02x:%02x%c%d", nmtst_get_rand_int () % 256, nmtst_get_rand_int () % 256, delimiter, num); + return g_strdup_printf ("a:b:c::%02x:%02x%c%d", nmtst_get_rand_uint32 () % 256, nmtst_get_rand_uint32 () % 256, delimiter, num); } /*****************************************************************************/ @@ -966,7 +975,6 @@ test_dcb_bandwidth_sums (void) /*****************************************************************************/ -#if WITH_JSON_VALIDATION static void _test_team_config_sync (const char *team_config, int notify_peer_count, @@ -989,26 +997,31 @@ _test_team_config_sync (const char *team_config, guint i, j; gboolean found; + if (!WITH_JSON_VALIDATION) { + g_test_skip ("team test requires JSON validation"); + return; + } + s_team = (NMSettingTeam *) nm_setting_team_new (); g_assert (s_team); g_object_set (s_team, NM_SETTING_TEAM_CONFIG, team_config, NULL); - g_assert (nm_setting_team_get_notify_peers_count (s_team) == notify_peer_count); - g_assert (nm_setting_team_get_notify_peers_interval (s_team) == notify_peers_interval); - g_assert (nm_setting_team_get_mcast_rejoin_count (s_team) == mcast_rejoin_count); - g_assert (nm_setting_team_get_mcast_rejoin_interval (s_team) == mcast_rejoin_interval); - g_assert (nm_setting_team_get_runner_tx_balancer_interval (s_team) == runner_tx_balancer_interval); - g_assert (nm_setting_team_get_runner_active (s_team) == runner_active); - g_assert (nm_setting_team_get_runner_fast_rate (s_team) == runner_fast_rate); - g_assert (nm_setting_team_get_runner_sys_prio (s_team) == runner_sys_prio); - g_assert (nm_setting_team_get_runner_min_ports (s_team) == runner_min_ports); - g_assert (nm_streq0 (nm_setting_team_get_runner (s_team), runner)); - g_assert (nm_streq0 (nm_setting_team_get_runner_hwaddr_policy (s_team), runner_hwaddr_policy)); - g_assert (nm_streq0 (nm_setting_team_get_runner_tx_balancer (s_team), runner_tx_balancer)); - g_assert (nm_streq0 (nm_setting_team_get_runner_agg_select_policy (s_team), runner_agg_select_policy)); + g_assert_cmpint (nm_setting_team_get_notify_peers_count (s_team), ==, notify_peer_count); + g_assert_cmpint (nm_setting_team_get_notify_peers_interval (s_team), ==, notify_peers_interval); + g_assert_cmpint (nm_setting_team_get_mcast_rejoin_count (s_team), ==, mcast_rejoin_count); + g_assert_cmpint (nm_setting_team_get_mcast_rejoin_interval (s_team), ==, mcast_rejoin_interval); + g_assert_cmpint (nm_setting_team_get_runner_tx_balancer_interval (s_team), ==, runner_tx_balancer_interval); + g_assert_cmpint (nm_setting_team_get_runner_active (s_team), ==, runner_active); + g_assert_cmpint (nm_setting_team_get_runner_fast_rate (s_team), ==, runner_fast_rate); + g_assert_cmpint (nm_setting_team_get_runner_sys_prio (s_team), ==, runner_sys_prio); + g_assert_cmpint (nm_setting_team_get_runner_min_ports (s_team), ==, runner_min_ports); + g_assert_cmpstr (nm_setting_team_get_runner (s_team), ==, runner); + g_assert_cmpstr (nm_setting_team_get_runner_hwaddr_policy (s_team), ==, runner_hwaddr_policy); + g_assert_cmpstr (nm_setting_team_get_runner_tx_balancer (s_team), ==, runner_tx_balancer); + g_assert_cmpstr (nm_setting_team_get_runner_agg_select_policy (s_team), ==, runner_agg_select_policy); if (runner_tx_hash) { - g_assert (runner_tx_hash->len == nm_setting_team_get_num_runner_tx_hash (s_team)); + g_assert_cmpint (runner_tx_hash->len, ==, nm_setting_team_get_num_runner_tx_hash (s_team)); for (i = 0; i < runner_tx_hash->len; i++) { found = FALSE; for (j = 0; j < nm_setting_team_get_num_runner_tx_hash (s_team); j++) { @@ -1023,7 +1036,7 @@ _test_team_config_sync (const char *team_config, } if (link_watchers) { - g_assert (link_watchers->len == nm_setting_team_get_num_link_watchers (s_team)); + g_assert_cmpint (link_watchers->len, ==, nm_setting_team_get_num_link_watchers (s_team)); for (i = 0; i < link_watchers->len; i++) { found = FALSE; for (j = 0; j < nm_setting_team_get_num_link_watchers (s_team); j++) { @@ -1044,11 +1057,11 @@ static void test_runner_roundrobin_sync_from_config (void) { _test_team_config_sync ("", - 0, 0, 0, 0, - NM_SETTING_TEAM_RUNNER_ROUNDROBIN, + -1, -1, -1, -1, + NULL, NULL, NULL, NULL, -1, - FALSE, FALSE, -1, -1, NULL, + TRUE, FALSE, -1, -1, NULL, NULL); } @@ -1056,11 +1069,11 @@ static void test_runner_broadcast_sync_from_config (void) { _test_team_config_sync ("{\"runner\": {\"name\": \"broadcast\"}}", - 0, 0, 0, 0, + -1, -1, -1, -1, NM_SETTING_TEAM_RUNNER_BROADCAST, NULL, NULL, NULL, -1, - FALSE, FALSE, -1, -1, NULL, + TRUE, FALSE, -1, -1, NULL, NULL); } @@ -1068,11 +1081,11 @@ static void test_runner_random_sync_from_config (void) { _test_team_config_sync ("{\"runner\": {\"name\": \"random\"}}", - 0, 0, 0, 0, + -1, -1, -1, -1, NM_SETTING_TEAM_RUNNER_RANDOM, NULL, NULL, NULL, -1, - FALSE, FALSE, -1, -1, NULL, + TRUE, FALSE, -1, -1, NULL, NULL); } @@ -1080,12 +1093,11 @@ static void test_runner_activebackup_sync_from_config (void) { _test_team_config_sync ("{\"runner\": {\"name\": \"activebackup\"}}", - NM_SETTING_TEAM_NOTIFY_PEERS_COUNT_ACTIVEBACKUP_DEFAULT, 0, - NM_SETTING_TEAM_NOTIFY_MCAST_COUNT_ACTIVEBACKUP_DEFAULT, 0, + -1, -1, -1, -1, NM_SETTING_TEAM_RUNNER_ACTIVEBACKUP, - NM_SETTING_TEAM_RUNNER_HWADDR_POLICY_DEFAULT, + NULL, NULL, NULL, -1, - FALSE, FALSE, -1, -1, NULL, + TRUE, FALSE, -1, -1, NULL, NULL); } @@ -1100,29 +1112,29 @@ test_runner_loadbalance_sync_from_config (void) g_ptr_array_add (tx_hash, g_strdup ("ipv6")); _test_team_config_sync ("{\"runner\": {\"name\": \"loadbalance\"}}", - 0, 0, 0, 0, + -1, -1, -1, -1, NM_SETTING_TEAM_RUNNER_LOADBALANCE, NULL, - tx_hash, NULL, NM_SETTING_TEAM_RUNNER_TX_BALANCER_INTERVAL_DEFAULT, - FALSE, FALSE, -1, -1, NULL, + NULL, NULL, -1, + TRUE, FALSE, -1, -1, NULL, NULL); _test_team_config_sync ("{\"runner\": {\"name\": \"loadbalance\", " "\"tx_hash\": [\"eth\", \"ipv4\", \"ipv6\"]}}", - 0, 0, 0, 0, + -1, -1, -1, -1, NM_SETTING_TEAM_RUNNER_LOADBALANCE, NULL, - tx_hash, NULL, NM_SETTING_TEAM_RUNNER_TX_BALANCER_INTERVAL_DEFAULT, - FALSE, FALSE, -1, -1, NULL, + tx_hash, NULL, -1, + TRUE, FALSE, -1, -1, NULL, NULL); _test_team_config_sync ("{\"runner\": {\"name\": \"loadbalance\", \"tx_hash\": [\"eth\", \"ipv4\", \"ipv6\"], " "\"tx_balancer\": {\"name\": \"basic\", \"balancing_interval\": 30}}}", - 0, 0, 0, 0, + -1, -1, -1, -1, NM_SETTING_TEAM_RUNNER_LOADBALANCE, NULL, tx_hash, "basic", 30, - FALSE, FALSE, -1, -1, NULL, + TRUE, FALSE, -1, -1, NULL, NULL); } @@ -1137,21 +1149,21 @@ test_runner_lacp_sync_from_config (void) g_ptr_array_add (tx_hash, g_strdup ("ipv6")); _test_team_config_sync ("{\"runner\": {\"name\": \"lacp\", \"tx_hash\": [\"eth\", \"ipv4\", \"ipv6\"]}}", - 0, 0, 0, 0, + -1, -1, -1, -1, NM_SETTING_TEAM_RUNNER_LACP, NULL, - tx_hash, NULL, NM_SETTING_TEAM_RUNNER_TX_BALANCER_INTERVAL_DEFAULT, - TRUE, FALSE, NM_SETTING_TEAM_RUNNER_SYS_PRIO_DEFAULT, 0, - NM_SETTING_TEAM_RUNNER_AGG_SELECT_POLICY_DEFAULT, + tx_hash, NULL, -1, + TRUE, FALSE, -1, -1, + NULL, NULL); _test_team_config_sync ("{\"runner\": {\"name\": \"lacp\", \"tx_hash\": [\"eth\", \"ipv4\", \"ipv6\"], " "\"active\": false, \"fast_rate\": true, \"sys_prio\": 10, \"min_ports\": 5, " "\"agg_select_policy\": \"port_config\"}}", - 0, 0, 0, 0, + -1, -1, -1, -1, NM_SETTING_TEAM_RUNNER_LACP, NULL, - tx_hash, NULL, NM_SETTING_TEAM_RUNNER_TX_BALANCER_INTERVAL_DEFAULT, + tx_hash, NULL, -1, FALSE, TRUE, 10, 5, "port_config", NULL); } @@ -1164,11 +1176,11 @@ test_watcher_ethtool_sync_from_config (void) link_watchers = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_team_link_watcher_unref); g_ptr_array_add (link_watchers, nm_team_link_watcher_new_ethtool (0, 0, NULL)); _test_team_config_sync ("{\"link_watch\": {\"name\": \"ethtool\"}}", - 0, 0, 0, 0, - "roundrobin", + -1, -1, -1, -1, + NULL, NULL, NULL, NULL, -1, - FALSE, FALSE, -1, -1, NULL, + TRUE, FALSE, -1, -1, NULL, link_watchers); } @@ -1180,11 +1192,11 @@ test_watcher_nsna_ping_sync_from_config (void) link_watchers = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_team_link_watcher_unref); g_ptr_array_add (link_watchers, nm_team_link_watcher_new_nsna_ping (0, 0, 3, "target.host", NULL)); _test_team_config_sync ("{\"link_watch\": {\"name\": \"nsna_ping\", \"target_host\": \"target.host\"}}", - 0, 0, 0, 0, - "roundrobin", + -1, -1, -1, -1, + NULL, NULL, NULL, NULL, -1, - FALSE, FALSE, -1, -1, NULL, + TRUE, FALSE, -1, -1, NULL, link_watchers); } @@ -1198,11 +1210,11 @@ test_watcher_arp_ping_sync_from_config (void) nm_team_link_watcher_new_arp_ping (0, 0, 3, "target.host", "source.host", 0, NULL)); _test_team_config_sync ("{\"link_watch\": {\"name\": \"arp_ping\", \"target_host\": \"target.host\", " "\"source_host\": \"source.host\"}}", - 0, 0, 0, 0, - "roundrobin", + -1, -1, -1, -1, + NULL, NULL, NULL, NULL, -1, - FALSE, FALSE, -1, -1, NULL, + TRUE, FALSE, -1, -1, NULL, link_watchers); } @@ -1227,11 +1239,11 @@ test_multiple_watchers_sync_from_config (void) "\"validate_active\": true, \"validate_inactive\": true, \"send_always\": true}, " "{\"name\": \"nsna_ping\", \"init_wait\": 3, \"interval\": 6, \"missed_max\": 9, " "\"target_host\": \"target.host\"}]}", - 0, 0, 0, 0, - "roundrobin", + -1, -1, -1, -1, + NULL, NULL, NULL, NULL, -1, - FALSE, FALSE, -1, -1, NULL, + TRUE, FALSE, -1, -1, NULL, link_watchers); } @@ -1250,6 +1262,11 @@ _test_team_port_config_sync (const char *team_port_config, guint i, j; gboolean found; + if (!WITH_JSON_VALIDATION) { + g_test_skip ("team test requires JSON validation"); + return; + } + s_team_port = (NMSettingTeamPort *) nm_setting_team_port_new (); g_assert (s_team_port); @@ -1282,52 +1299,52 @@ _test_team_port_config_sync (const char *team_port_config, static void test_team_port_default (void) { - _test_team_port_config_sync ("", -1, 0, FALSE, 255, 0, NULL); + _test_team_port_config_sync ("", -1, 0, FALSE, -1, -1, NULL); } static void test_team_port_queue_id (void) { _test_team_port_config_sync ("{\"queue_id\": 3}", - 3, 0, FALSE, 255, 0, NULL); + 3, 0, FALSE, -1, -1, NULL); _test_team_port_config_sync ("{\"queue_id\": 0}", - 0, 0, FALSE, 255, 0, NULL); + 0, 0, FALSE, -1, -1, NULL); } static void test_team_port_prio (void) { _test_team_port_config_sync ("{\"prio\": 6}", - -1, 6, FALSE, 255, 0, NULL); + -1, 6, FALSE, -1, -1, NULL); _test_team_port_config_sync ("{\"prio\": 0}", - -1, 0, FALSE, 255, 0, NULL); + -1, 0, FALSE, -1, -1, NULL); } static void test_team_port_sticky (void) { _test_team_port_config_sync ("{\"sticky\": true}", - -1, 0, TRUE, 255, 0, NULL); + -1, 0, TRUE, -1, -1, NULL); _test_team_port_config_sync ("{\"sticky\": false}", - -1, 0, FALSE, 255, 0, NULL); + -1, 0, FALSE, -1, -1, NULL); } static void test_team_port_lacp_prio (void) { _test_team_port_config_sync ("{\"lacp_prio\": 9}", - -1, 0, FALSE, 9, 0, NULL); + -1, 0, FALSE, 9, -1, NULL); _test_team_port_config_sync ("{\"lacp_prio\": 0}", - -1, 0, FALSE, 0, 0, NULL); + -1, 0, FALSE, 0, -1, NULL); } static void test_team_port_lacp_key (void) { _test_team_port_config_sync ("{\"lacp_key\": 12}", - -1, 0, FALSE, 255, 12, NULL); + -1, 0, FALSE, -1, 12, NULL); _test_team_port_config_sync ("{\"lacp_key\": 0}", - -1, 0, FALSE, 255, 0, NULL); + -1, 0, FALSE, -1, 0, NULL); } static void @@ -1354,7 +1371,127 @@ test_team_port_full_config (void) "\"send_always\": true}]}", 10, 20, true, 30, 40, NULL); } -#endif + +/*****************************************************************************/ + +static void +_check_team_setting (NMSetting *setting) +{ + gs_unref_object NMSetting *setting2 = NULL; + gs_unref_object NMSetting *setting_clone = NULL; + gboolean is_port = NM_IS_SETTING_TEAM_PORT (setting); + gs_unref_variant GVariant *variant2 = NULL; + gs_unref_variant GVariant *variant3 = NULL; + + g_assert (NM_IS_SETTING_TEAM (setting) || is_port); + + setting2 = g_object_new (G_OBJECT_TYPE (setting), + is_port + ? NM_SETTING_TEAM_PORT_CONFIG + : NM_SETTING_TEAM_CONFIG, + is_port + ? nm_setting_team_port_get_config (NM_SETTING_TEAM_PORT (setting)) + : nm_setting_team_get_config (NM_SETTING_TEAM (setting)), + NULL); + + if (WITH_JSON_VALIDATION) + nmtst_assert_setting_is_equal (setting, setting2, NM_SETTING_COMPARE_FLAG_EXACT); + + g_clear_object (&setting2); + + nmtst_assert_setting_dbus_roundtrip (setting); + + /* OK, now parse the setting only from the D-Bus variant, but removing the JSON config. + * For that, we have to "drop" the JSON and we do that by resetting the property. + * This causes JSON to be regenerated and it's in a normalized form that will compare + * equal. */ + setting_clone = nm_setting_duplicate (setting); + setting = setting_clone; + if (is_port) { + g_object_set (setting, + NM_SETTING_TEAM_PORT_STICKY, + nm_setting_team_port_get_sticky (NM_SETTING_TEAM_PORT (setting)), + NULL); + } else { + g_object_set (setting, + NM_SETTING_TEAM_RUNNER_SYS_PRIO, + nm_setting_team_get_runner_sys_prio (NM_SETTING_TEAM (setting)), + NULL); + } + variant2 = _nm_setting_to_dbus (setting, NULL, NM_CONNECTION_SERIALIZE_ALL, NULL); + variant3 = nm_utils_gvariant_vardict_filter_drop_one (variant2, "config"); + setting2 = nmtst_assert_setting_dbus_new (G_OBJECT_TYPE (setting), variant3); + nmtst_assert_setting_is_equal (setting, setting2, NM_SETTING_COMPARE_FLAG_EXACT); +} + +static void +test_team_setting (void) +{ + gs_unref_variant GVariant *variant = nmtst_variant_from_string (G_VARIANT_TYPE_VARDICT, + "{'config': <'{\"link_watch\": {\"name\": \"ethtool\"}}'>, 'interface-name': <'nm-team'>, 'link-watchers': <[{'name': <'ethtool'>}]>}"); + gs_free_error GError *error = NULL; + gs_unref_object NMSetting *setting = NULL; + nm_auto_unref_team_link_watcher NMTeamLinkWatcher *watcher1 = nm_team_link_watcher_new_nsna_ping (1, 3, 4, "bbb", NULL); + nm_auto_unref_team_link_watcher NMTeamLinkWatcher *watcher2 = nm_team_link_watcher_new_arp_ping2 (1, 3, 4, -1, "ccc", "ddd", 0, NULL); + + g_assert (watcher1); + g_assert (watcher2); + + setting = _nm_setting_new_from_dbus (NM_TYPE_SETTING_TEAM, + variant, + NULL, + NM_SETTING_PARSE_FLAGS_STRICT, + &error); + nmtst_assert_success (setting, error); + _check_team_setting (setting); + + g_assert_cmpstr (nm_setting_team_get_config (NM_SETTING_TEAM (setting)), ==, "{\"link_watch\": {\"name\": \"ethtool\"}}"); + g_assert_cmpint (nm_setting_team_get_num_link_watchers (NM_SETTING_TEAM (setting)), ==, 1); + + g_object_set (setting, + NM_SETTING_TEAM_RUNNER_SYS_PRIO, + (int) 10, + NULL); + + _check_team_setting (setting); + g_assert_cmpint (nm_setting_team_get_num_link_watchers (NM_SETTING_TEAM (setting)), ==, 1); + g_assert_cmpstr (nm_setting_team_get_config (NM_SETTING_TEAM (setting)), ==, "{ \"runner\": { \"sys_prio\": 10 }, \"link_watch\": { \"name\": \"ethtool\" } }"); + + nm_setting_team_remove_link_watcher (NM_SETTING_TEAM (setting), 0); + + _check_team_setting (setting); + g_assert_cmpint (nm_setting_team_get_num_link_watchers (NM_SETTING_TEAM (setting)), ==, 0); + g_assert_cmpstr (nm_setting_team_get_config (NM_SETTING_TEAM (setting)), ==, "{ \"runner\": { \"sys_prio\": 10 } }"); + + nm_setting_team_add_link_watcher (NM_SETTING_TEAM (setting), watcher1); + _check_team_setting (setting); + g_assert_cmpstr (nm_setting_team_get_config (NM_SETTING_TEAM (setting)), ==, "{ \"runner\": { \"sys_prio\": 10 }, \"link_watch\": { \"name\": \"nsna_ping\", \"interval\": 3, \"init_wait\": 1, \"missed_max\": 4, \"target_host\": \"bbb\" } }"); + + nm_setting_team_add_link_watcher (NM_SETTING_TEAM (setting), watcher2); + _check_team_setting (setting); + g_assert_cmpstr (nm_setting_team_get_config (NM_SETTING_TEAM (setting)), ==, "{ \"runner\": { \"sys_prio\": 10 }, \"link_watch\": [ { \"name\": \"nsna_ping\", \"interval\": 3, \"init_wait\": 1, \"missed_max\": 4, \"target_host\": \"bbb\" }, { \"name\": \"arp_ping\", \"interval\": 3, \"init_wait\": 1, \"missed_max\": 4, \"source_host\": \"ddd\", \"target_host\": \"ccc\" } ] }"); + + nm_setting_team_remove_link_watcher (NM_SETTING_TEAM (setting), 0); + nm_setting_team_remove_link_watcher (NM_SETTING_TEAM (setting), 0); + g_object_set (setting, + NM_SETTING_TEAM_RUNNER_TX_BALANCER_INTERVAL, + (int) 5, + NULL); + g_assert_cmpstr (nm_setting_team_get_config (NM_SETTING_TEAM (setting)), ==, "{ \"runner\": { \"tx_balancer\": { \"balancing_interval\": 5 }, \"sys_prio\": 10 } }"); + + g_object_set (setting, + NM_SETTING_TEAM_RUNNER, + NULL, + NULL); + _check_team_setting (setting); + g_assert_cmpstr (nm_setting_team_get_config (NM_SETTING_TEAM (setting)), ==, "{ \"runner\": { \"tx_balancer\": { \"balancing_interval\": 5 }, \"sys_prio\": 10 } }"); + + g_object_set (setting, + NM_SETTING_TEAM_CONFIG, + "{ \"runner\": { \"tx_hash\": [ \"eth\", \"l3\" ] } }", + NULL); + _check_team_setting (setting); +} /*****************************************************************************/ @@ -2108,6 +2245,105 @@ test_tc_config_dbus (void) /*****************************************************************************/ +static void +_rndt_wired_add_s390_options (NMSettingWired *s_wired, + char **out_keyfile_entries) +{ + gsize n_opts; + gsize i, j; + const char *const*option_names; + gs_free const char **opt_keys = NULL; + gs_strfreev char **opt_vals = NULL; + gs_free bool *opt_found = NULL; + GString *keyfile_entries; + nm_auto_free_gstring GString *str_tmp = NULL; + + option_names = nm_setting_wired_get_valid_s390_options (nmtst_get_rand_bool () ? NULL : s_wired); + + n_opts = NM_PTRARRAY_LEN (option_names); + opt_keys = g_new (const char *, (n_opts + 1)); + nmtst_rand_perm (NULL, opt_keys, option_names, sizeof (const char *), n_opts); + n_opts = nmtst_get_rand_uint32 () % (n_opts + 1); + opt_keys[n_opts] = NULL; + + opt_vals = g_new0 (char *, n_opts + 1); + opt_found = g_new0 (bool, n_opts + 1); + for (i = 0; i < n_opts; i++) { + guint p = nmtst_get_rand_uint32 () % 1000; + + if (p < 200) + opt_vals[i] = nm_strdup_int (i); + else { + opt_vals[i] = g_strdup_printf ("%s%s%s%s-%zu", + ((p % 5) % 2) ? "\n" : "", + ((p % 7) % 2) ? "\t" : "", + ((p % 11) % 2) ? "x" : "", + ((p % 13) % 2) ? "=" : "", + i); + } + } + + if (nmtst_get_rand_bool ()) { + gs_unref_hashtable GHashTable *hash = NULL; + + hash = g_hash_table_new (nm_str_hash, g_str_equal); + for (i = 0; i < n_opts; i++) + g_hash_table_insert (hash, (char *) opt_keys[i], opt_vals[i]); + g_object_set (s_wired, + NM_SETTING_WIRED_S390_OPTIONS, + hash, + NULL); + } else { + _nm_setting_wired_clear_s390_options (s_wired); + for (i = 0; i < n_opts; i++) { + if (!nm_setting_wired_add_s390_option (s_wired, opt_keys[i], opt_vals[i])) + g_assert_not_reached (); + } + } + + g_assert_cmpint (nm_setting_wired_get_num_s390_options (s_wired), ==, n_opts); + + keyfile_entries = g_string_new (NULL); + str_tmp = g_string_new (NULL); + if (n_opts > 0) + g_string_append_printf (keyfile_entries, "[ethernet-s390-options]\n"); + for (i = 0; i < n_opts; i++) { + gssize idx; + const char *k, *v; + + nm_setting_wired_get_s390_option (s_wired, i, &k, &v); + g_assert (k); + g_assert (v); + + idx = nm_utils_strv_find_first ((char **) opt_keys, n_opts, k); + g_assert (idx >= 0); + g_assert (!opt_found[idx]); + opt_found[idx] = TRUE; + g_assert_cmpstr (opt_keys[idx], ==, k); + g_assert_cmpstr (opt_vals[idx], ==, v); + + g_string_truncate (str_tmp, 0); + for (j = 0; v[j] != '\0'; j++) { + if (v[j] == '\n') + g_string_append (str_tmp, "\\n"); + else if (v[j] == '\t') + g_string_append (str_tmp, "\\t"); + else + g_string_append_c (str_tmp, v[j]); + } + + g_string_append_printf (keyfile_entries, + "%s=%s\n", + k, + str_tmp->str); + } + for (i = 0; i < n_opts; i++) + g_assert (opt_found[i]); + if (n_opts > 0) + g_string_append_printf (keyfile_entries, "\n"); + *out_keyfile_entries = g_string_free (keyfile_entries, FALSE); +} + static GPtrArray * _rndt_wg_peers_create (void) { @@ -2116,7 +2352,7 @@ _rndt_wg_peers_create (void) wg_peers = g_ptr_array_new_with_free_func ((GDestroyNotify) nm_wireguard_peer_unref); - n = nmtst_get_rand_int () % 10; + n = nmtst_get_rand_uint32 () % 10; for (i = 0; i < n; i++) { NMWireGuardPeer *peer; guint8 public_key_buf[NM_WIREGUARD_PUBLIC_KEY_LEN]; @@ -2147,12 +2383,12 @@ _rndt_wg_peers_create (void) NM_SETTING_SECRET_FLAG_AGENT_OWNED)); nm_wireguard_peer_set_persistent_keepalive (peer, - nmtst_rand_select ((guint32) 0, nmtst_get_rand_int ())); + nmtst_rand_select ((guint32) 0, nmtst_get_rand_uint32 ())); if (!nm_wireguard_peer_set_endpoint (peer, nmtst_rand_select (s_endpoint, NULL), TRUE)) g_assert_not_reached (); - n_aip = nmtst_rand_select (0, nmtst_get_rand_int () % 10); + n_aip = nmtst_rand_select (0, nmtst_get_rand_uint32 () % 10); for (i_aip = 0; i_aip < n_aip; i_aip++) { gs_free char *aip = NULL; @@ -2349,7 +2585,7 @@ test_roundtrip_conversion (gconstpointer test_data) const char *UUID= "63376701-b61e-4318-bf7e-664a1c1eeaab"; const char *INTERFACE_NAME = nm_sprintf_bufa (100, "ifname%d", MODE); guint32 ETH_MTU = nmtst_rand_select ((guint32) 0u, - nmtst_get_rand_int ()); + nmtst_get_rand_uint32 ()); const char *WG_PRIVATE_KEY = nmtst_get_rand_bool () ? "yGXGK+5bVnxSJUejH4vbpXbq+ZtaG4NB8IHRK/aVtE0=" : NULL; @@ -2357,9 +2593,9 @@ test_roundtrip_conversion (gconstpointer test_data) NM_SETTING_SECRET_FLAG_NOT_SAVED, NM_SETTING_SECRET_FLAG_AGENT_OWNED); const guint WG_LISTEN_PORT = nmtst_rand_select (0u, - nmtst_get_rand_int () % 0x10000); + nmtst_get_rand_uint32 () % 0x10000); const guint WG_FWMARK = nmtst_rand_select (0u, - nmtst_get_rand_int ()); + nmtst_get_rand_uint32 ()); gs_unref_ptrarray GPtrArray *kf_data_arr = g_ptr_array_new_with_free_func (g_free); gs_unref_ptrarray GPtrArray *wg_peers = NULL; const NMConnectionSerializationFlags dbus_serialization_flags[] = { @@ -2385,6 +2621,7 @@ test_roundtrip_conversion (gconstpointer test_data) int is_ipv4; guint i; gboolean success; + gs_free char *s390_keyfile_entries = NULL; switch (MODE) { case 0: @@ -2403,6 +2640,8 @@ test_roundtrip_conversion (gconstpointer test_data) ETH_MTU, NULL); + _rndt_wired_add_s390_options (s_eth, &s390_keyfile_entries); + g_ptr_array_add (kf_data_arr, g_strdup_printf ("[connection]\n" "id=%s\n" @@ -2415,6 +2654,7 @@ test_roundtrip_conversion (gconstpointer test_data) "mac-address-blacklist=\n" "%s" /* mtu */ "\n" + "%s" /* [ethernet-s390-options] */ "[ipv4]\n" "dns-search=\n" "method=auto\n" @@ -2429,7 +2669,8 @@ test_roundtrip_conversion (gconstpointer test_data) INTERFACE_NAME, (ETH_MTU != 0) ? nm_sprintf_bufa (100, "mtu=%u\n", ETH_MTU) - : "")); + : "", + s390_keyfile_entries)); g_ptr_array_add (kf_data_arr, g_strdup_printf ("[connection]\n" @@ -2443,6 +2684,7 @@ test_roundtrip_conversion (gconstpointer test_data) "mac-address-blacklist=\n" "%s" /* mtu */ "\n" + "%s" /* [ethernet-s390-options] */ "[ipv4]\n" "dns-search=\n" "method=auto\n" @@ -2457,7 +2699,8 @@ test_roundtrip_conversion (gconstpointer test_data) INTERFACE_NAME, (ETH_MTU != 0) ? nm_sprintf_bufa (100, "mtu=%d\n", (int) ETH_MTU) - : "")); + : "", + s390_keyfile_entries)); break; @@ -2703,6 +2946,9 @@ test_roundtrip_conversion (gconstpointer test_data) g_assert_cmpint (nm_setting_wired_get_mtu (s_eth), ==, ETH_MTU); g_assert_cmpint (nm_setting_wired_get_mtu (s_eth2), ==, ETH_MTU); + + g_assert_cmpint (nm_setting_wired_get_num_s390_options (s_eth2), ==, nm_setting_wired_get_num_s390_options (s_eth)); + break; case 1: @@ -2979,6 +3225,60 @@ test_routing_rule (gconstpointer test_data) /*****************************************************************************/ +static void +test_parse_tc_handle (void) +{ +#define _parse_tc_handle(str, exp) \ + G_STMT_START { \ + gs_free_error GError *_error = NULL; \ + GError **_perror = nmtst_get_rand_bool () ? &_error : NULL; \ + guint32 _v; \ + const guint32 _v_exp = (exp); \ + \ + _v = _nm_utils_parse_tc_handle (""str"", _perror); \ + \ + if (_v != _v_exp) \ + g_error ("%s:%d: \"%s\" gave %08x but %08x expected.", __FILE__, __LINE__, ""str"", _v, _v_exp); \ + \ + if (_v == TC_H_UNSPEC) \ + g_assert (!_perror || *_perror); \ + else \ + g_assert (!_perror || !*_perror); \ + \ + } G_STMT_END + +#define _parse_tc_handle_inval(str) _parse_tc_handle (str, TC_H_UNSPEC) +#define _parse_tc_handle_valid(str, maj, min) _parse_tc_handle (str, TC_H_MAKE (((guint32) (maj)) << 16, ((guint16) (min)))) + + _parse_tc_handle_inval (""); + _parse_tc_handle_inval (" "); + _parse_tc_handle_inval (" \n"); + _parse_tc_handle_valid ("1", 1, 0); + _parse_tc_handle_valid(" 1 ", 1, 0); + _parse_tc_handle_valid ("1:", 1, 0); + _parse_tc_handle_valid ("1: ", 1, 0); + _parse_tc_handle_valid ("1:0", 1, 0); + _parse_tc_handle_valid ("1 :0", 1, 0); + _parse_tc_handle_valid ("1 \t\n\f\r:0", 1, 0); + _parse_tc_handle_inval ("1 \t\n\f\r\v:0"); + _parse_tc_handle_valid (" 1 : 0 ", 1, 0); + _parse_tc_handle_inval (" \t\v\n1: 0"); + _parse_tc_handle_valid ("1:2", 1, 2); + _parse_tc_handle_valid ("01:02", 1, 2); + _parse_tc_handle_inval ("0x01:0x02"); + _parse_tc_handle_valid (" 01: 02", 1, 2); + _parse_tc_handle_valid ("019: 020", 0x19, 0x20); + _parse_tc_handle_valid ("FFFF: 020", 0xFFFF, 0x20); + _parse_tc_handle_valid ("FfFF: ffff", 0xFFFF, 0xFFFF); + _parse_tc_handle_valid ("FFFF", 0xFFFF, 0); + _parse_tc_handle_inval ("0xFFFF"); + _parse_tc_handle_inval ("10000"); + _parse_tc_handle_valid ("\t\n\f\r FFFF", 0xFFFF, 0); + _parse_tc_handle_inval ("\t\n\f\r \vFFFF"); +} + +/*****************************************************************************/ + NMTST_DEFINE (); int @@ -3026,7 +3326,6 @@ main (int argc, char **argv) g_test_add_func ("/libnm/settings/bridge/vlans", test_bridge_vlans); -#if WITH_JSON_VALIDATION g_test_add_func ("/libnm/settings/team/sync_runner_from_config_roundrobin", test_runner_roundrobin_sync_from_config); g_test_add_func ("/libnm/settings/team/sync_runner_from_config_broadcast", @@ -3055,7 +3354,6 @@ main (int argc, char **argv) g_test_add_func ("/libnm/settings/team-port/sync_from_config_lacp_prio", test_team_port_lacp_prio); g_test_add_func ("/libnm/settings/team-port/sync_from_config_lacp_key", test_team_port_lacp_key); g_test_add_func ("/libnm/settings/team-port/sycn_from_config_full", test_team_port_full_config); -#endif g_test_add_data_func ("/libnm/settings/roundtrip-conversion/general/0", GINT_TO_POINTER (0), test_roundtrip_conversion); g_test_add_data_func ("/libnm/settings/roundtrip-conversion/wireguard/1", GINT_TO_POINTER (1), test_roundtrip_conversion); @@ -3064,5 +3362,9 @@ main (int argc, char **argv) g_test_add_data_func ("/libnm/settings/routing-rule/1", GINT_TO_POINTER (0), test_routing_rule); + g_test_add_func ("/libnm/parse-tc-handle", test_parse_tc_handle); + + g_test_add_func ("/libnm/test_team_setting", test_team_setting); + return g_test_run (); } diff --git a/libnm-core/tests/test-settings-defaults.c b/libnm-core/tests/test-settings-defaults.c index 9e640543..c5156fc9 100644 --- a/libnm-core/tests/test-settings-defaults.c +++ b/libnm-core/tests/test-settings-defaults.c @@ -1,4 +1,3 @@ -/* -*- 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 |