From 84346d172ae0c4f02e9327feea1d6da26fa3f38d Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 5 Oct 2016 14:38:01 +0200 Subject: [PATCH 01/11] shared: add NMTST_UUID_INIT() macro --- shared/nm-utils/nm-test-utils.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/shared/nm-utils/nm-test-utils.h b/shared/nm-utils/nm-test-utils.h index 64604e38f1..75d01bb632 100644 --- a/shared/nm-utils/nm-test-utils.h +++ b/shared/nm-utils/nm-test-utils.h @@ -979,6 +979,10 @@ __define_nmtst_static(02, 1024) __define_nmtst_static(03, 1024) #undef __define_nmtst_static +#define NMTST_UUID_INIT(uuid) \ + gs_free char *_nmtst_hidden_##uuid = nm_utils_uuid_generate (); \ + const char *const uuid = _nmtst_hidden_##uuid + inline static const char * nmtst_uuid_generate (void) { From 3e13ebd1c9b0147ef03306a134baed3fb080a633 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 5 Oct 2016 12:35:25 +0200 Subject: [PATCH 02/11] keyfile/tests: refactor tests to use cleanup attributes --- .../plugins/keyfile/tests/test-keyfile.c | 439 ++++++------------ 1 file changed, 142 insertions(+), 297 deletions(-) diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index e63fa495db..381ae5a14b 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -62,7 +62,7 @@ check_ip_route (NMSettingIPConfig *config, int idx, const char *destination, int static NMConnection * keyfile_read_connection_from_file (const char *filename) { - GError *error = NULL; + gs_free_error GError *error = NULL; NMConnection *connection; g_assert (filename); @@ -78,12 +78,12 @@ keyfile_read_connection_from_file (const char *filename) static void test_read_valid_wired_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; - GError *error = NULL; + gs_free_error GError *error = NULL; const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; gboolean success; @@ -215,7 +215,6 @@ test_read_valid_wired_connection (void) check_ip_route (s_ip6, 4, "7:8:9:0:1:2:3:4", 125, NULL, 5); check_ip_route (s_ip6, 5, "8:9:0:1:2:3:4:5", 124, NULL, 6); check_ip_route (s_ip6, 6, "8:9:0:1:2:3:4:6", 123, NULL, -1); - g_object_unref (connection); } static void @@ -224,7 +223,7 @@ add_one_ip_address (NMSettingIPConfig *s_ip, guint32 prefix) { NMIPAddress *ip_addr; - GError *error = NULL; + gs_free_error GError *error = NULL; ip_addr = nm_ip_address_new (NM_IS_SETTING_IP4_CONFIG (s_ip) ? AF_INET : AF_INET6, addr, prefix, &error); @@ -241,7 +240,7 @@ add_one_ip_route (NMSettingIPConfig *s_ip, gint64 metric) { NMIPRoute *route; - GError *error = NULL; + gs_free_error GError *error = NULL; g_assert (prefix > 0); route = nm_ip_route_new (NM_IS_SETTING_IP4_CONFIG (s_ip) ? AF_INET : AF_INET6, @@ -255,7 +254,8 @@ add_one_ip_route (NMSettingIPConfig *s_ip, static void test_write_wired_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; @@ -263,9 +263,8 @@ test_write_wired_connection (void) char *uuid; const char *mac = "99:88:77:66:55:44"; gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; const char *dns1 = "4.2.2.1"; @@ -382,26 +381,20 @@ test_write_wired_connection (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, NULL); - g_assert (reread); - g_assert (nm_connection_compare (connection, reread, NM_SETTING_COMPARE_FLAG_EXACT)); + nmtst_assert_connection_equals (connection, FALSE, reread, FALSE); - g_clear_error (&error); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } static void test_read_ip6_wired_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; - GError *error = NULL; + gs_free_error GError *error = NULL; gboolean success; connection = nm_keyfile_plugin_connection_from_file (TEST_KEYFILES_DIR "/Test_Wired_Connection_IP6", NULL); @@ -433,23 +426,21 @@ test_read_ip6_wired_connection (void) g_assert_cmpint (nm_setting_ip_config_get_num_addresses (s_ip6), ==, 1); check_ip_address (s_ip6, 0, "abcd:1234:ffff::cdde", 64); g_assert_cmpstr (nm_setting_ip_config_get_gateway (s_ip6), ==, "abcd:1234:ffff::cdd1"); - - g_object_unref (connection); } static void test_write_ip6_wired_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; char *uuid; gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; const char *dns = "1::cafe"; @@ -515,24 +506,18 @@ test_write_ip6_wired_connection (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, NULL); - g_assert (reread); - g_assert (nm_connection_compare (connection, reread, NM_SETTING_COMPARE_FLAG_EXACT)); + nmtst_assert_connection_equals (connection, FALSE, reread, FALSE); - g_clear_error (&error); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } static void test_read_wired_mac_case (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; - GError *error = NULL; + gs_free_error GError *error = NULL; const char *mac; char expected_mac_address[ETH_ALEN] = { 0x00, 0x11, 0xaa, 0xbb, 0xcc, 0x55 }; gboolean success; @@ -562,16 +547,14 @@ test_read_wired_mac_case (void) mac = nm_setting_wired_get_mac_address (s_wired); g_assert (mac); g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac_address, sizeof (expected_mac_address))); - - g_object_unref (connection); } static void test_read_mac_old_format (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingWired *s_wired; - GError *error = NULL; + gs_free_error GError *error = NULL; gboolean success; const char *mac; char expected_mac[ETH_ALEN] = { 0x00, 0x11, 0xaa, 0xbb, 0xcc, 0x55 }; @@ -597,16 +580,14 @@ test_read_mac_old_format (void) mac = nm_setting_wired_get_cloned_mac_address (s_wired); g_assert (mac); g_assert (nm_utils_hwaddr_matches (mac, -1, expected_cloned_mac, ETH_ALEN)); - - g_object_unref (connection); } static void test_read_mac_ib_old_format (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingInfiniband *s_ib; - GError *error = NULL; + gs_free_error GError *error = NULL; gboolean success; const char *mac; guint8 expected_mac[INFINIBAND_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, @@ -628,18 +609,16 @@ test_read_mac_ib_old_format (void) mac = nm_setting_infiniband_get_mac_address (s_ib); g_assert (mac); g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac, sizeof (expected_mac))); - - g_object_unref (connection); } static void test_read_valid_wireless_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingIPConfig *s_ip4; - GError *error = NULL; + gs_free_error GError *error = NULL; const char *bssid; const guint8 expected_bssid[ETH_ALEN] = { 0x00, 0x1a, 0x33, 0x44, 0x99, 0x82 }; gboolean success; @@ -669,14 +648,13 @@ test_read_valid_wireless_connection (void) s_ip4 = nm_connection_get_setting_ip4_config (connection); g_assert (s_ip4); g_assert_cmpstr (nm_setting_ip_config_get_method (s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO); - - g_object_unref (connection); } static void test_write_wireless_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingIPConfig *s_ip4; @@ -686,9 +664,8 @@ test_write_wireless_connection (void) GBytes *ssid; unsigned char tmpssid[] = { 0x31, 0x33, 0x33, 0x37 }; gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; guint64 timestamp = 0x12344433L; @@ -753,23 +730,17 @@ test_write_wireless_connection (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, NULL); - g_assert (reread); - g_assert (nm_connection_compare (connection, reread, NM_SETTING_COMPARE_FLAG_EXACT)); + nmtst_assert_connection_equals (connection, FALSE, reread, FALSE); - g_clear_error (&error); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } static void test_read_string_ssid (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingWireless *s_wireless; - GError *error = NULL; + gs_free_error GError *error = NULL; GBytes *ssid; const guint8 *ssid_data; gsize ssid_len; @@ -789,23 +760,22 @@ test_read_string_ssid (void) g_assert (ssid); ssid_data = g_bytes_get_data (ssid, &ssid_len); g_assert_cmpmem (ssid_data, ssid_len, expected_ssid, strlen (expected_ssid)); - - g_object_unref (connection); } static void test_write_string_ssid (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingIPConfig *s_ip4; - char *uuid, *testfile = NULL, *tmp; + char *uuid, *tmp; + gs_free char *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 65, 49, 50, 51, 32, 46, 92, 46, 36, 37, 126, 93 }; gboolean success; - NMConnection *reread; - GError *error = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; GKeyFile *keyfile; @@ -862,24 +832,17 @@ test_write_string_ssid (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, NULL); - g_assert (reread); - nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); - g_clear_error (&error); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } static void test_read_intlist_ssid (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingWireless *s_wifi; - GError *error = NULL; + gs_free_error GError *error = NULL; gboolean success; GBytes *ssid; const guint8 *ssid_data; @@ -902,23 +865,22 @@ test_read_intlist_ssid (void) g_assert (ssid != NULL); ssid_data = g_bytes_get_data (ssid, &ssid_len); g_assert_cmpmem (ssid_data, ssid_len, expected_ssid, strlen (expected_ssid)); - - g_object_unref (connection); } static void test_write_intlist_ssid (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingIPConfig *s_ip4; - char *uuid, *testfile = NULL; + char *uuid; + gs_free char *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 65, 49, 50, 51, 0, 50, 50 }; gboolean success; - NMConnection *reread; - GError *error = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; GKeyFile *keyfile; @@ -985,24 +947,17 @@ test_write_intlist_ssid (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, &error); g_assert_no_error (error); - g_assert (reread); - nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); - g_clear_error (&error); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } static void test_read_intlike_ssid (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingWireless *s_wifi; - GError *error = NULL; + gs_free_error GError *error = NULL; gboolean success; GBytes *ssid; const guint8 *ssid_data; @@ -1026,16 +981,14 @@ test_read_intlike_ssid (void) ssid_data = g_bytes_get_data (ssid, &ssid_len); g_assert_cmpint (ssid_len, ==, strlen (expected_ssid)); g_assert_cmpint (memcmp (ssid_data, expected_ssid, strlen (expected_ssid)), ==, 0); - - g_object_unref (connection); } static void test_read_intlike_ssid_2 (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingWireless *s_wifi; - GError *error = NULL; + gs_free_error GError *error = NULL; gboolean success; GBytes *ssid; const guint8 *ssid_data; @@ -1059,23 +1012,22 @@ test_read_intlike_ssid_2 (void) ssid_data = g_bytes_get_data (ssid, &ssid_len); g_assert_cmpint (ssid_len, ==, strlen (expected_ssid)); g_assert_cmpint (memcmp (ssid_data, expected_ssid, strlen (expected_ssid)), ==, 0); - - g_object_unref (connection); } static void test_write_intlike_ssid (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingIPConfig *s_ip4; - char *uuid, *testfile = NULL; + char *uuid; + gs_free char *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 49, 48, 49 }; gboolean success; - NMConnection *reread; - GError *error = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; GKeyFile *keyfile; @@ -1138,31 +1090,25 @@ test_write_intlike_ssid (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, &error); g_assert_no_error (error); - g_assert (reread); - nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); - g_clear_error (&error); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } static void test_write_intlike_ssid_2 (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingIPConfig *s_ip4; - char *uuid, *testfile = NULL; + char *uuid; + gs_free char *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 49, 49, 59, 49, 50, 59, 49, 51, 59}; gboolean success; - NMConnection *reread; - GError *error = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; GKeyFile *keyfile; @@ -1225,27 +1171,20 @@ test_write_intlike_ssid_2 (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, &error); g_assert_no_error (error); - g_assert (reread); - nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); - g_clear_error (&error); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } static void test_read_bt_dun_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingBluetooth *s_bluetooth; NMSettingSerial *s_serial; NMSettingGsm *s_gsm; - GError *error = NULL; + gs_free_error GError *error = NULL; const char *bdaddr; const guint8 expected_bdaddr[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; gboolean success; @@ -1281,14 +1220,13 @@ test_read_bt_dun_connection (void) s_serial = nm_connection_get_setting_serial (connection); g_assert (s_serial); g_assert (nm_setting_serial_get_parity (s_serial) == NM_SETTING_SERIAL_PARITY_ODD); - - g_object_unref (connection); } static void test_write_bt_dun_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingBluetooth *s_bt; NMSettingIPConfig *s_ip4; @@ -1296,9 +1234,8 @@ test_write_bt_dun_connection (void) char *uuid; const char *bdaddr = "aa:b9:a1:74:55:44"; gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; guint64 timestamp = 0x12344433L; @@ -1360,26 +1297,19 @@ test_write_bt_dun_connection (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, NULL); - g_assert (reread); - nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); - g_clear_error (&error); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } static void test_read_gsm_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingSerial *s_serial; NMSettingGsm *s_gsm; - GError *error = NULL; + gs_free_error GError *error = NULL; gboolean success; connection = nm_keyfile_plugin_connection_from_file (TEST_KEYFILES_DIR "/ATT_Data_Connect_Plain", &error); @@ -1416,22 +1346,20 @@ test_read_gsm_connection (void) s_serial = nm_connection_get_setting_serial (connection); g_assert (s_serial); g_assert_cmpint (nm_setting_serial_get_parity (s_serial), ==, NM_SETTING_SERIAL_PARITY_ODD); - - g_object_unref (connection); } static void test_write_gsm_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingIPConfig *s_ip4; NMSettingGsm *s_gsm; char *uuid; gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; guint64 timestamp = 0x12344433L; @@ -1490,25 +1418,18 @@ test_write_gsm_connection (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, &error); g_assert_no_error (error); - g_assert (reread); - nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); - g_clear_error (&error); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } static void test_read_wired_8021x_tls_blob_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingWired *s_wired; NMSetting8021x *s_8021x; - GError *error = NULL; + gs_free_error GError *error = NULL; const char *tmp; gboolean success; GBytes *blob; @@ -1561,17 +1482,15 @@ test_read_wired_8021x_tls_blob_connection (void) tmp = nm_setting_802_1x_get_private_key_path (s_8021x); g_assert_cmpstr (tmp, ==, "/CASA/dcbw/Desktop/certinfra/client.pem"); - - g_object_unref (connection); } static void test_read_wired_8021x_tls_bad_path_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingWired *s_wired; NMSetting8021x *s_8021x; - GError *error = NULL; + gs_free_error GError *error = NULL; const char *tmp; char *tmp2; gboolean success; @@ -1618,16 +1537,15 @@ test_read_wired_8021x_tls_bad_path_connection (void) g_assert_cmpstr (tmp, ==, tmp2); g_free (tmp2); - g_object_unref (connection); } static void test_read_wired_8021x_tls_old_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingWired *s_wired; NMSetting8021x *s_8021x; - GError *error = NULL; + gs_free_error GError *error = NULL; const char *tmp; gboolean success; @@ -1670,17 +1588,15 @@ test_read_wired_8021x_tls_old_connection (void) tmp = nm_setting_802_1x_get_private_key_path (s_8021x); g_assert (g_strcmp0 (tmp, "/CASA/dcbw/Desktop/certinfra/client.pem") == 0); - - g_object_unref (connection); } static void test_read_wired_8021x_tls_new_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingWired *s_wired; NMSetting8021x *s_8021x; - GError *error = NULL; + gs_free_error GError *error = NULL; const char *tmp; char *tmp2; gboolean success; @@ -1724,7 +1640,6 @@ test_read_wired_8021x_tls_new_connection (void) g_assert_cmpstr (tmp, ==, tmp2); g_free (tmp2); - g_object_unref (connection); } #define TEST_WIRED_TLS_CA_CERT TEST_KEYFILES_DIR"/test-ca-cert.pem" @@ -1741,7 +1656,7 @@ create_wired_tls_connection (NMSetting8021xCKScheme scheme) NMSetting8021x *s_8021x; char *uuid; gboolean success; - GError *error = NULL; + gs_free_error GError *error = NULL; connection = nm_simple_connection_new (); g_assert (connection != NULL); @@ -1815,12 +1730,12 @@ get_path (const char *file, gboolean relative) static void test_write_wired_8021x_tls_connection_path (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; char *tmp, *tmp2; gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; GKeyFile *keyfile; gboolean relative = FALSE; @@ -1901,26 +1816,22 @@ test_write_wired_8021x_tls_connection_path (void) g_key_file_free (keyfile); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } static void test_write_wired_8021x_tls_connection_blob (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSetting8021x *s_8021x; gboolean success; - NMConnection *reread; - char *testfile = NULL; + gs_free char *testfile = NULL; char *new_ca_cert; char *new_client_cert; char *new_priv_key; const char *uuid; - GError *error = NULL; + gs_free_error GError *error = NULL; GBytes *password_raw = NULL; #define PASSWORD_RAW "password-raw\0test" @@ -1985,7 +1896,6 @@ test_write_wired_8021x_tls_connection_blob (void) g_assert (!memcmp (g_bytes_get_data (password_raw, NULL), PASSWORD_RAW, NM_STRLEN (PASSWORD_RAW))); unlink (testfile); - g_free (testfile); /* Clean up written certs */ unlink (new_ca_cert); @@ -1996,18 +1906,15 @@ test_write_wired_8021x_tls_connection_blob (void) unlink (new_priv_key); g_free (new_priv_key); - - g_object_unref (reread); - g_object_unref (connection); } static void test_read_infiniband_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingInfiniband *s_ib; - GError *error = NULL; + gs_free_error GError *error = NULL; const char *mac; guint8 expected_mac[INFINIBAND_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, 0x89, @@ -2036,14 +1943,13 @@ test_read_infiniband_connection (void) mac = nm_setting_infiniband_get_mac_address (s_ib); g_assert (mac); g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac, sizeof (expected_mac))); - - g_object_unref (connection); } static void test_write_infiniband_connection (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingInfiniband *s_ib; NMSettingIPConfig *s_ip4; @@ -2051,9 +1957,8 @@ test_write_infiniband_connection (void) char *uuid; const char *mac = "99:88:77:66:55:44:ab:bc:cd:de:ef:f0:0a:1b:2c:3d:4e:5f:6f:ba"; gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; @@ -2109,25 +2014,19 @@ test_write_infiniband_connection (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, &error); g_assert_no_error (error); - g_assert (reread); - - g_assert (nm_connection_compare (connection, reread, NM_SETTING_COMPARE_FLAG_EXACT)); + nmtst_assert_connection_equals (connection, FALSE, reread, FALSE); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } static void test_read_bridge_main (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingIPConfig *s_ip4; NMSettingBridge *s_bridge; - GError *error = NULL; + gs_free_error GError *error = NULL; const char *expected_id = "Test Bridge Main"; const char *expected_uuid = "8f061643-fe41-4d4c-a8d9-097d26e2ad3a"; gboolean success; @@ -2161,23 +2060,21 @@ test_read_bridge_main (void) g_assert_cmpuint (nm_setting_bridge_get_max_age (s_bridge), ==, 39); g_assert_cmpuint (nm_setting_bridge_get_ageing_time (s_bridge), ==, 235352); g_assert_cmpuint (nm_setting_bridge_get_multicast_snooping (s_bridge), ==, FALSE); - - g_object_unref (connection); } static void test_write_bridge_main (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingBridge *s_bridge; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; char *uuid; gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; @@ -2233,27 +2130,21 @@ test_write_bridge_main (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, &error); g_assert_no_error (error); - g_assert (reread); - - g_assert (nm_connection_compare (connection, reread, NM_SETTING_COMPARE_FLAG_EXACT)); + nmtst_assert_connection_equals (connection, FALSE, reread, FALSE); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } static void test_read_bridge_component (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingBridgePort *s_port; NMSettingWired *s_wired; const char *mac; guint8 expected_mac[ETH_ALEN] = { 0x00, 0x22, 0x15, 0x59, 0x62, 0x97 }; - GError *error = NULL; + gs_free_error GError *error = NULL; const char *expected_id = "Test Bridge Component"; const char *expected_uuid = "d7b4f96c-c45e-4298-bef8-f48574f8c1c0"; gboolean success; @@ -2286,23 +2177,21 @@ test_read_bridge_component (void) g_assert (nm_setting_bridge_port_get_hairpin_mode (s_port)); g_assert_cmpuint (nm_setting_bridge_port_get_priority (s_port), ==, 28); g_assert_cmpuint (nm_setting_bridge_port_get_path_cost (s_port), ==, 100); - - g_object_unref (connection); } static void test_write_bridge_component (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingBridgePort *s_port; NMSettingWired *s_wired; char *uuid; const char *mac = "99:88:77:66:55:44"; gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; @@ -2356,25 +2245,19 @@ test_write_bridge_component (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, &error); g_assert_no_error (error); - g_assert (reread); - - g_assert (nm_connection_compare (connection, reread, NM_SETTING_COMPARE_FLAG_EXACT)); + nmtst_assert_connection_equals (connection, FALSE, reread, FALSE); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } static void test_read_new_wired_group_name (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingWired *s_wired; const char *mac; guint8 expected_mac[ETH_ALEN] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; - GError *error = NULL; + gs_free_error GError *error = NULL; gboolean success; connection = nm_keyfile_plugin_connection_from_file (TEST_KEYFILES_DIR"/Test_New_Wired_Group_Name", &error); @@ -2392,24 +2275,22 @@ test_read_new_wired_group_name (void) mac = nm_setting_wired_get_mac_address (s_wired); g_assert (mac); g_assert (nm_utils_hwaddr_matches (mac, -1, expected_mac, sizeof (expected_mac))); - - g_object_unref (connection); } static void test_write_new_wired_group_name (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; + gs_unref_keyfile GKeyFile *kf = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; char *uuid; gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; - GKeyFile *kf; char *s; gint mtu; @@ -2446,7 +2327,6 @@ test_write_new_wired_group_name (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, &error); g_assert_no_error (error); - g_assert (reread); nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); /* Look at the keyfile itself to ensure we wrote out the new group names and type */ @@ -2465,24 +2345,19 @@ test_write_new_wired_group_name (void) g_assert_cmpint (mtu, ==, 1400); unlink (testfile); - g_free (testfile); - - g_key_file_unref (kf); - g_object_unref (reread); - g_object_unref (connection); } static void test_read_new_wireless_group_names (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; GBytes *ssid; const guint8 *ssid_data; gsize ssid_len; const char *expected_ssid = "foobar"; - GError *error = NULL; + gs_free_error GError *error = NULL; gboolean success; connection = nm_keyfile_plugin_connection_from_file (TEST_KEYFILES_DIR"/Test_New_Wireless_Group_Names", &error); @@ -2509,14 +2384,14 @@ test_read_new_wireless_group_names (void) g_assert (s_wsec); g_assert_cmpstr (nm_setting_wireless_security_get_key_mgmt (s_wsec), ==, "wpa-psk"); g_assert_cmpstr (nm_setting_wireless_security_get_psk (s_wsec), ==, "s3cu4e passphrase"); - - g_object_unref (connection); } static void test_write_new_wireless_group_names (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; + gs_unref_keyfile GKeyFile *kf = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; @@ -2525,12 +2400,10 @@ test_write_new_wireless_group_names (void) unsigned char tmpssid[] = { 0x31, 0x33, 0x33, 0x37 }; const char *expected_psk = "asdfasdfasdfa12315"; gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; - GKeyFile *kf; char *s; connection = nm_simple_connection_new (); @@ -2579,7 +2452,6 @@ test_write_new_wireless_group_names (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, &error); g_assert_no_error (error); - g_assert (reread); nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); /* Look at the keyfile itself to ensure we wrote out the new group names and type */ @@ -2604,19 +2476,14 @@ test_write_new_wireless_group_names (void) g_free (s); unlink (testfile); - g_free (testfile); - - g_key_file_unref (kf); - g_object_unref (reread); - g_object_unref (connection); } static void test_read_missing_vlan_setting (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingVlan *s_vlan; - GError *error = NULL; + gs_free_error GError *error = NULL; gboolean success; connection = nm_keyfile_plugin_connection_from_file (TEST_KEYFILES_DIR"/Test_Missing_Vlan_Setting", &error); @@ -2631,16 +2498,14 @@ test_read_missing_vlan_setting (void) g_assert (s_vlan); g_assert_cmpint (nm_setting_vlan_get_id (s_vlan), ==, 0); g_assert_cmpint (nm_setting_vlan_get_flags (s_vlan), ==, NM_VLAN_FLAG_REORDER_HEADERS); - - g_object_unref (connection); } static void test_read_missing_vlan_flags (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingVlan *s_vlan; - GError *error = NULL; + gs_free_error GError *error = NULL; gboolean success; connection = nm_keyfile_plugin_connection_from_file (TEST_KEYFILES_DIR"/Test_Missing_Vlan_Flags", &error); @@ -2657,15 +2522,13 @@ test_read_missing_vlan_flags (void) g_assert_cmpint (nm_setting_vlan_get_id (s_vlan), ==, 444); g_assert_cmpstr (nm_setting_vlan_get_parent (s_vlan), ==, "em1"); g_assert_cmpint (nm_setting_vlan_get_flags (s_vlan), ==, NM_VLAN_FLAG_REORDER_HEADERS); - - g_object_unref (connection); } static void test_read_missing_id_uuid (void) { - NMConnection *connection; - GError *error = NULL; + gs_unref_object NMConnection *connection = NULL; + gs_free_error GError *error = NULL; gboolean success; connection = nm_keyfile_plugin_connection_from_file (TEST_KEYFILES_DIR"/Test_Missing_ID_UUID", &error); @@ -2678,14 +2541,12 @@ test_read_missing_id_uuid (void) /* Ensure the ID and UUID properties are there */ g_assert_cmpstr (nm_connection_get_id (connection), ==, "Test_Missing_ID_UUID"); g_assert (nm_connection_get_uuid (connection)); - - g_object_unref (connection); } static void test_read_minimal (void) { - NMConnection *connection = NULL; + gs_unref_object NMConnection *connection = NULL; gs_unref_object NMConnection *con_archetype = NULL; NMSettingConnection *s_con; @@ -2717,7 +2578,7 @@ test_read_minimal (void) static void test_read_minimal_slave (void) { - NMConnection *connection = NULL; + gs_unref_object NMConnection *connection = NULL; gs_unref_object NMConnection *con_archetype = NULL; NMSettingConnection *s_con; @@ -2769,9 +2630,9 @@ test_read_minimal_slave (void) static void test_read_enum_property (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingIPConfig *s_ip6; - GError *error = NULL; + gs_free_error GError *error = NULL; gboolean success; connection = nm_keyfile_plugin_connection_from_file (TEST_KEYFILES_DIR"/Test_Enum_Property", &error); @@ -2785,22 +2646,20 @@ test_read_enum_property (void) s_ip6 = nm_connection_get_setting_ip6_config (connection); g_assert (s_ip6); g_assert_cmpint (nm_setting_ip6_config_get_ip6_privacy (NM_SETTING_IP6_CONFIG (s_ip6)), ==, NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR); - - g_object_unref (connection); } static void test_write_enum_property (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip6; char *uuid; gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; @@ -2844,23 +2703,17 @@ test_write_enum_property (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, &error); g_assert_no_error (error); - g_assert (reread); - nmtst_assert_connection_equals (reread, FALSE, connection, FALSE); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } static void test_read_flags_property (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; NMSettingGsm *s_gsm; - GError *error = NULL; + gs_free_error GError *error = NULL; gboolean success; connection = nm_keyfile_plugin_connection_from_file (TEST_KEYFILES_DIR"/Test_Flags_Property", &error); @@ -2875,21 +2728,19 @@ test_read_flags_property (void) g_assert (s_gsm); g_assert_cmpint (nm_setting_gsm_get_password_flags (s_gsm), ==, NM_SETTING_SECRET_FLAG_AGENT_OWNED | NM_SETTING_SECRET_FLAG_NOT_REQUIRED); - - g_object_unref (connection); } static void test_write_flags_property (void) { - NMConnection *connection; + gs_unref_object NMConnection *connection = NULL; + gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSetting *s_gsm; char *uuid; gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; @@ -2931,15 +2782,9 @@ test_write_flags_property (void) /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, &error); g_assert_no_error (error); - g_assert (reread); - nmtst_assert_connection_equals (reread, FALSE, connection, FALSE); unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); } /*****************************************************************************/ From f25cd2b844af319fe0c4652f8bd7397c4bca8765 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 5 Oct 2016 12:54:45 +0200 Subject: [PATCH 03/11] keyfile/tests: refactor checking re-reading keyfile in tests --- .../plugins/keyfile/tests/test-keyfile.c | 152 +++++------------- 1 file changed, 44 insertions(+), 108 deletions(-) diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index 381ae5a14b..d54bbdcdcb 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -36,6 +36,8 @@ #include "nm-test-utils-core.h" +/*****************************************************************************/ + static void check_ip_address (NMSettingIPConfig *config, int idx, const char *address, int plen) { @@ -75,6 +77,32 @@ keyfile_read_connection_from_file (const char *filename) return connection; } +static void +assert_reread (NMConnection *connection, gboolean normalize_connection, const char *testfile) +{ + gs_unref_object NMConnection *reread = NULL; + GError *error = NULL; + GError **p_error = (nmtst_get_rand_int () % 2) ? &error : NULL; + + g_assert (NM_IS_CONNECTION (connection)); + g_assert (testfile && testfile[0]); + + reread = nm_keyfile_plugin_connection_from_file (testfile, p_error); + g_assert_no_error (error); + g_assert (NM_IS_CONNECTION (reread)); + + nmtst_assert_connection_equals (connection, normalize_connection, reread, FALSE); +} + +static void +assert_reread_and_unlink (NMConnection *connection, gboolean normalize_connection, const char *testfile) +{ + assert_reread (connection, normalize_connection, testfile); + unlink (testfile); +} + +/*****************************************************************************/ + static void test_read_valid_wired_connection (void) { @@ -255,7 +283,6 @@ static void test_write_wired_connection (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; @@ -379,11 +406,7 @@ test_write_wired_connection (void) g_assert (success); g_assert (testfile); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, NULL); - nmtst_assert_connection_equals (connection, FALSE, reread, FALSE); - - unlink (testfile); + assert_reread_and_unlink (connection, FALSE, testfile); } static void @@ -432,7 +455,6 @@ static void test_write_ip6_wired_connection (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; @@ -504,11 +526,7 @@ test_write_ip6_wired_connection (void) g_assert (success); g_assert (testfile); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, NULL); - nmtst_assert_connection_equals (connection, FALSE, reread, FALSE); - - unlink (testfile); + assert_reread_and_unlink (connection, FALSE, testfile); } static void @@ -654,7 +672,6 @@ static void test_write_wireless_connection (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingIPConfig *s_ip4; @@ -728,11 +745,7 @@ test_write_wireless_connection (void) g_assert (success); g_assert (testfile); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, NULL); - nmtst_assert_connection_equals (connection, FALSE, reread, FALSE); - - unlink (testfile); + assert_reread_and_unlink (connection, FALSE, testfile); } static void @@ -766,7 +779,6 @@ static void test_write_string_ssid (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingIPConfig *s_ip4; @@ -830,11 +842,7 @@ test_write_string_ssid (void) g_free (tmp); g_key_file_free (keyfile); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, NULL); - nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); - - unlink (testfile); + assert_reread_and_unlink (connection, TRUE, testfile); } static void @@ -871,7 +879,6 @@ static void test_write_intlist_ssid (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingIPConfig *s_ip4; @@ -944,12 +951,7 @@ test_write_intlist_ssid (void) g_key_file_free (keyfile); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, &error); - g_assert_no_error (error); - nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); - - unlink (testfile); + assert_reread_and_unlink (connection, TRUE, testfile); } static void @@ -1018,7 +1020,6 @@ static void test_write_intlike_ssid (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingIPConfig *s_ip4; @@ -1087,19 +1088,13 @@ test_write_intlike_ssid (void) g_key_file_free (keyfile); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, &error); - g_assert_no_error (error); - nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); - - unlink (testfile); + assert_reread_and_unlink (connection, TRUE, testfile); } static void test_write_intlike_ssid_2 (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingIPConfig *s_ip4; @@ -1168,12 +1163,7 @@ test_write_intlike_ssid_2 (void) g_key_file_free (keyfile); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, &error); - g_assert_no_error (error); - nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); - - unlink (testfile); + assert_reread_and_unlink (connection, TRUE, testfile); } static void @@ -1226,7 +1216,6 @@ static void test_write_bt_dun_connection (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingBluetooth *s_bt; NMSettingIPConfig *s_ip4; @@ -1293,13 +1282,8 @@ test_write_bt_dun_connection (void) success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); g_assert_no_error (error); g_assert (success); - g_assert (testfile); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, NULL); - nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); - - unlink (testfile); + assert_reread_and_unlink (connection, TRUE, testfile); } static void @@ -1352,7 +1336,6 @@ static void test_write_gsm_connection (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingIPConfig *s_ip4; NMSettingGsm *s_gsm; @@ -1415,12 +1398,7 @@ test_write_gsm_connection (void) g_assert (success); g_assert (testfile != NULL); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, &error); - g_assert_no_error (error); - nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); - - unlink (testfile); + assert_reread_and_unlink (connection, TRUE, testfile); } static void @@ -1949,7 +1927,6 @@ static void test_write_infiniband_connection (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingInfiniband *s_ib; NMSettingIPConfig *s_ip4; @@ -2011,12 +1988,7 @@ test_write_infiniband_connection (void) g_assert (success); g_assert (testfile); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, &error); - g_assert_no_error (error); - nmtst_assert_connection_equals (connection, FALSE, reread, FALSE); - - unlink (testfile); + assert_reread_and_unlink (connection, FALSE, testfile); } static void @@ -2066,7 +2038,6 @@ static void test_write_bridge_main (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingBridge *s_bridge; NMSettingIPConfig *s_ip4; @@ -2127,12 +2098,7 @@ test_write_bridge_main (void) g_assert (success); g_assert (testfile); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, &error); - g_assert_no_error (error); - nmtst_assert_connection_equals (connection, FALSE, reread, FALSE); - - unlink (testfile); + assert_reread_and_unlink (connection, FALSE, testfile); } static void @@ -2183,7 +2149,6 @@ static void test_write_bridge_component (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingBridgePort *s_port; NMSettingWired *s_wired; @@ -2242,12 +2207,7 @@ test_write_bridge_component (void) g_assert (success); g_assert (testfile); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, &error); - g_assert_no_error (error); - nmtst_assert_connection_equals (connection, FALSE, reread, FALSE); - - unlink (testfile); + assert_reread_and_unlink (connection, FALSE, testfile); } static void @@ -2281,7 +2241,6 @@ static void test_write_new_wired_group_name (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; gs_unref_keyfile GKeyFile *kf = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; @@ -2324,10 +2283,7 @@ test_write_new_wired_group_name (void) g_assert (success); g_assert (testfile); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, &error); - g_assert_no_error (error); - nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); + assert_reread (connection, TRUE, testfile); /* Look at the keyfile itself to ensure we wrote out the new group names and type */ kf = g_key_file_new (); @@ -2390,7 +2346,6 @@ static void test_write_new_wireless_group_names (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; gs_unref_keyfile GKeyFile *kf = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; @@ -2447,12 +2402,7 @@ test_write_new_wireless_group_names (void) g_assert_no_error (error); g_assert (success); - g_assert (testfile); - - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, &error); - g_assert_no_error (error); - nmtst_assert_connection_equals (connection, TRUE, reread, FALSE); + assert_reread (connection, TRUE, testfile); /* Look at the keyfile itself to ensure we wrote out the new group names and type */ kf = g_key_file_new (); @@ -2652,7 +2602,6 @@ static void test_write_enum_property (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip6; @@ -2698,14 +2647,8 @@ test_write_enum_property (void) success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); g_assert_no_error (error); g_assert (success); - g_assert (testfile); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, &error); - g_assert_no_error (error); - nmtst_assert_connection_equals (reread, FALSE, connection, FALSE); - - unlink (testfile); + assert_reread_and_unlink (connection, FALSE, testfile); } static void @@ -2734,7 +2677,6 @@ static void test_write_flags_property (void) { gs_unref_object NMConnection *connection = NULL; - gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSetting *s_gsm; char *uuid; @@ -2777,14 +2719,8 @@ test_write_flags_property (void) success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); g_assert_no_error (error); g_assert (success); - g_assert (testfile); - /* Read the connection back in and compare it to the one we just wrote out */ - reread = nm_keyfile_plugin_connection_from_file (testfile, &error); - g_assert_no_error (error); - nmtst_assert_connection_equals (reread, FALSE, connection, FALSE); - - unlink (testfile); + assert_reread_and_unlink (connection, FALSE, testfile); } /*****************************************************************************/ From 9f9e4b0c35d6429cb103cdd017e5c8b5ec9be8a5 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 5 Oct 2016 13:32:47 +0200 Subject: [PATCH 04/11] keyfile/tests: use cleanup attribute for keyfile --- .../plugins/keyfile/tests/test-keyfile.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index d54bbdcdcb..d7db003412 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -790,7 +790,7 @@ test_write_string_ssid (void) gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; - GKeyFile *keyfile; + gs_unref_keyfile GKeyFile *keyfile = NULL; connection = nm_simple_connection_new (); @@ -840,7 +840,6 @@ test_write_string_ssid (void) g_assert (tmp); g_assert_cmpmem (tmp, strlen (tmp), tmpssid, sizeof (tmpssid)); g_free (tmp); - g_key_file_free (keyfile); assert_reread_and_unlink (connection, TRUE, testfile); } @@ -890,7 +889,7 @@ test_write_intlist_ssid (void) gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; - GKeyFile *keyfile; + gs_unref_keyfile GKeyFile *keyfile = NULL; gint *intlist; gsize len = 0, i; @@ -949,8 +948,6 @@ test_write_intlist_ssid (void) g_assert_cmpint (intlist[i], ==, tmpssid[i]); g_free (intlist); - g_key_file_free (keyfile); - assert_reread_and_unlink (connection, TRUE, testfile); } @@ -1031,11 +1028,10 @@ test_write_intlike_ssid (void) gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; - GKeyFile *keyfile; + gs_unref_keyfile GKeyFile *keyfile = NULL; char *tmp; connection = nm_simple_connection_new (); - g_assert (connection); /* Connection setting */ @@ -1086,8 +1082,6 @@ test_write_intlike_ssid (void) g_assert_cmpstr (tmp, ==, "101"); g_free (tmp); - g_key_file_free (keyfile); - assert_reread_and_unlink (connection, TRUE, testfile); } @@ -1106,7 +1100,7 @@ test_write_intlike_ssid_2 (void) gs_free_error GError *error = NULL; pid_t owner_grp; uid_t owner_uid; - GKeyFile *keyfile; + gs_unref_keyfile GKeyFile *keyfile = NULL; char *tmp; connection = nm_simple_connection_new (); @@ -1161,8 +1155,6 @@ test_write_intlike_ssid_2 (void) g_assert_cmpstr (tmp, ==, "11\\;12\\;13\\;"); g_free (tmp); - g_key_file_free (keyfile); - assert_reread_and_unlink (connection, TRUE, testfile); } @@ -1714,7 +1706,7 @@ test_write_wired_8021x_tls_connection_path (void) gboolean success; gs_free char *testfile = NULL; gs_free_error GError *error = NULL; - GKeyFile *keyfile; + gs_unref_keyfile GKeyFile *keyfile = NULL; gboolean relative = FALSE; connection = create_wired_tls_connection (NM_SETTING_802_1X_CK_SCHEME_PATH); @@ -1792,7 +1784,6 @@ test_write_wired_8021x_tls_connection_path (void) g_free (tmp2); g_free (tmp); - g_key_file_free (keyfile); unlink (testfile); } From 00ef37cd982ee5582e088dd4129b7a7ec3309fd6 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 5 Oct 2016 14:13:06 +0200 Subject: [PATCH 05/11] keyfile/tests: refactor writing of test keyfile --- .../plugins/keyfile/tests/test-keyfile.c | 260 ++++-------------- 1 file changed, 50 insertions(+), 210 deletions(-) diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index d7db003412..93352573bf 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -101,6 +101,38 @@ assert_reread_and_unlink (NMConnection *connection, gboolean normalize_connectio unlink (testfile); } +static void +write_test_connection (NMConnection *connection, char **testfile) +{ + uid_t owner_uid; + gid_t owner_grp; + gboolean success; + GError *error = NULL; + GError **p_error = (nmtst_get_rand_int () % 2) ? &error : NULL; + + g_assert (NM_IS_CONNECTION (connection)); + g_assert (testfile && !*testfile); + + owner_uid = geteuid (); + owner_grp = getegid (); + + success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, testfile, p_error); + g_assert_no_error (error); + g_assert (success); + g_assert (*testfile && (*testfile)[0]); +} + +static void +write_test_connection_and_reread (NMConnection *connection, gboolean normalize_connection) +{ + gs_free char *testfile = NULL; + + g_assert (NM_IS_CONNECTION (connection)); + + write_test_connection (connection, &testfile); + assert_reread_and_unlink (connection, normalize_connection, testfile); +} + /*****************************************************************************/ static void @@ -289,11 +321,6 @@ test_write_wired_connection (void) NMSettingIPConfig *s_ip6; char *uuid; const char *mac = "99:88:77:66:55:44"; - gboolean success; - gs_free char *testfile = NULL; - gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; const char *dns1 = "4.2.2.1"; const char *dns2 = "4.2.2.2"; const char *address1 = "192.168.0.5"; @@ -398,15 +425,7 @@ test_write_wired_connection (void) /* DNS searches */ nm_setting_ip_config_add_dns_search (s_ip6, "wallaceandgromit.com"); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - g_assert (testfile); - - assert_reread_and_unlink (connection, FALSE, testfile); + write_test_connection_and_reread (connection, FALSE); } static void @@ -460,11 +479,6 @@ test_write_ip6_wired_connection (void) NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; char *uuid; - gboolean success; - gs_free char *testfile = NULL; - gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; const char *dns = "1::cafe"; const char *address = "abcd::beef"; const char *gw = "dcba::beef"; @@ -518,15 +532,7 @@ test_write_ip6_wired_connection (void) /* DNS searches */ nm_setting_ip_config_add_dns_search (s_ip6, "wallaceandgromit.com"); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - g_assert (testfile); - - assert_reread_and_unlink (connection, FALSE, testfile); + write_test_connection_and_reread (connection, FALSE); } static void @@ -680,11 +686,6 @@ test_write_wireless_connection (void) const char *bssid = "aa:b9:a1:74:55:44"; GBytes *ssid; unsigned char tmpssid[] = { 0x31, 0x33, 0x33, 0x37 }; - gboolean success; - gs_free char *testfile = NULL; - gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; guint64 timestamp = 0x12344433L; connection = nm_simple_connection_new (); @@ -737,15 +738,7 @@ test_write_wireless_connection (void) NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - g_assert (testfile); - - assert_reread_and_unlink (connection, FALSE, testfile); + write_test_connection_and_reread (connection, FALSE); } static void @@ -786,10 +779,6 @@ test_write_string_ssid (void) gs_free char *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 65, 49, 50, 51, 32, 46, 92, 46, 36, 37, 126, 93 }; - gboolean success; - gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; gs_unref_keyfile GKeyFile *keyfile = NULL; connection = nm_simple_connection_new (); @@ -825,13 +814,7 @@ test_write_string_ssid (void) NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - g_assert (testfile); + write_test_connection (connection, &testfile); /* Ensure the SSID was written out as a string */ keyfile = g_key_file_new (); @@ -887,8 +870,6 @@ test_write_intlist_ssid (void) unsigned char tmpssid[] = { 65, 49, 50, 51, 0, 50, 50 }; gboolean success; gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; gs_unref_keyfile GKeyFile *keyfile = NULL; gint *intlist; gsize len = 0, i; @@ -925,13 +906,7 @@ test_write_intlist_ssid (void) nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - g_assert (testfile != NULL); + write_test_connection (connection, &testfile); /* Ensure the SSID was written out as an int list */ keyfile = g_key_file_new (); @@ -1026,8 +1001,6 @@ test_write_intlike_ssid (void) unsigned char tmpssid[] = { 49, 48, 49 }; gboolean success; gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; gs_unref_keyfile GKeyFile *keyfile = NULL; char *tmp; @@ -1062,13 +1035,7 @@ test_write_intlike_ssid (void) nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - g_assert (testfile != NULL); + write_test_connection (connection, &testfile); /* Ensure the SSID was written out as a plain "101" */ keyfile = g_key_file_new (); @@ -1098,8 +1065,6 @@ test_write_intlike_ssid_2 (void) unsigned char tmpssid[] = { 49, 49, 59, 49, 50, 59, 49, 51, 59}; gboolean success; gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; gs_unref_keyfile GKeyFile *keyfile = NULL; char *tmp; @@ -1135,13 +1100,7 @@ test_write_intlike_ssid_2 (void) nm_connection_add_setting (connection, NM_SETTING (s_ip4)); g_object_set (s_ip4, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_AUTO, NULL); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - g_assert (testfile != NULL); + write_test_connection (connection, &testfile); /* Ensure the SSID was written out as a plain "11;12;13;" */ keyfile = g_key_file_new (); @@ -1214,11 +1173,6 @@ test_write_bt_dun_connection (void) NMSettingGsm *s_gsm; char *uuid; const char *bdaddr = "aa:b9:a1:74:55:44"; - gboolean success; - gs_free char *testfile = NULL; - gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; guint64 timestamp = 0x12344433L; connection = nm_simple_connection_new (); @@ -1268,14 +1222,7 @@ test_write_bt_dun_connection (void) NM_SETTING_GSM_NUMBER, "*99#", NULL); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - - assert_reread_and_unlink (connection, TRUE, testfile); + write_test_connection_and_reread (connection, TRUE); } static void @@ -1332,11 +1279,6 @@ test_write_gsm_connection (void) NMSettingIPConfig *s_ip4; NMSettingGsm *s_gsm; char *uuid; - gboolean success; - gs_free char *testfile = NULL; - gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; guint64 timestamp = 0x12344433L; connection = nm_simple_connection_new (); @@ -1382,15 +1324,7 @@ test_write_gsm_connection (void) NM_SETTING_GSM_SIM_OPERATOR_ID, "310260", NULL); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - g_assert (testfile != NULL); - - assert_reread_and_unlink (connection, TRUE, testfile); + write_test_connection_and_reread (connection, TRUE); } static void @@ -1712,14 +1646,7 @@ test_write_wired_8021x_tls_connection_path (void) connection = create_wired_tls_connection (NM_SETTING_802_1X_CK_SCHEME_PATH); g_assert (connection != NULL); - /* Write out the connection */ - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, geteuid (), getegid (), &testfile, &error); - if (!success) { - g_assert (error); - g_warning ("Failed to write keyfile: %s", error->message); - g_assert (success); - } - g_assert (testfile); + write_test_connection (connection, &testfile); /* Read the connection back in and compare it to the one we just wrote out */ reread = nm_keyfile_plugin_connection_from_file (testfile, &error); @@ -1794,7 +1721,6 @@ test_write_wired_8021x_tls_connection_blob (void) gs_unref_object NMConnection *reread = NULL; NMSettingConnection *s_con; NMSetting8021x *s_8021x; - gboolean success; gs_free char *testfile = NULL; char *new_ca_cert; char *new_client_cert; @@ -1817,14 +1743,7 @@ test_write_wired_8021x_tls_connection_blob (void) NULL); g_bytes_unref (password_raw); - /* Write out the connection */ - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, geteuid (), getegid (), &testfile, &error); - if (!success) { - g_assert (error); - g_warning ("Failed to write keyfile: %s", error->message); - g_assert (success); - } - g_assert (testfile); + write_test_connection (connection, &testfile); /* Check that the new certs got written out */ s_con = nm_connection_get_setting_connection (connection); @@ -1924,14 +1843,8 @@ test_write_infiniband_connection (void) NMSettingIPConfig *s_ip6; char *uuid; const char *mac = "99:88:77:66:55:44:ab:bc:cd:de:ef:f0:0a:1b:2c:3d:4e:5f:6f:ba"; - gboolean success; - gs_free char *testfile = NULL; - gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; connection = nm_simple_connection_new (); - g_assert (connection); /* Connection setting */ @@ -1971,15 +1884,7 @@ test_write_infiniband_connection (void) nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - g_assert (testfile); - - assert_reread_and_unlink (connection, FALSE, testfile); + write_test_connection_and_reread (connection, FALSE); } static void @@ -2034,11 +1939,6 @@ test_write_bridge_main (void) NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; char *uuid; - gboolean success; - gs_free char *testfile = NULL; - gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; connection = nm_simple_connection_new (); g_assert (connection); @@ -2081,15 +1981,7 @@ test_write_bridge_main (void) nm_connection_add_setting (connection, NM_SETTING (s_ip6)); g_object_set (s_ip6, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - g_assert (testfile); - - assert_reread_and_unlink (connection, FALSE, testfile); + write_test_connection_and_reread (connection, FALSE); } static void @@ -2145,11 +2037,6 @@ test_write_bridge_component (void) NMSettingWired *s_wired; char *uuid; const char *mac = "99:88:77:66:55:44"; - gboolean success; - gs_free char *testfile = NULL; - gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; connection = nm_simple_connection_new (); g_assert (connection); @@ -2190,15 +2077,7 @@ test_write_bridge_component (void) NM_SETTING_BRIDGE_PORT_PATH_COST, 99, NULL); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - g_assert (testfile); - - assert_reread_and_unlink (connection, FALSE, testfile); + write_test_connection_and_reread (connection, FALSE); } static void @@ -2239,8 +2118,6 @@ test_write_new_wired_group_name (void) gboolean success; gs_free char *testfile = NULL; gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; char *s; gint mtu; @@ -2266,13 +2143,7 @@ test_write_new_wired_group_name (void) g_object_set (s_wired, NM_SETTING_WIRED_MTU, 1400, NULL); nm_connection_add_setting (connection, NM_SETTING (s_wired)); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - g_assert (testfile); + write_test_connection (connection, &testfile); assert_reread (connection, TRUE, testfile); @@ -2348,8 +2219,6 @@ test_write_new_wireless_group_names (void) gboolean success; gs_free char *testfile = NULL; gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; char *s; connection = nm_simple_connection_new (); @@ -2386,12 +2255,7 @@ test_write_new_wireless_group_names (void) NM_SETTING_WIRELESS_SECURITY_PSK, expected_psk, NULL); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); + write_test_connection (connection, &testfile); assert_reread (connection, TRUE, testfile); @@ -2597,11 +2461,6 @@ test_write_enum_property (void) NMSettingWired *s_wired; NMSettingIPConfig *s_ip6; char *uuid; - gboolean success; - gs_free char *testfile = NULL; - gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; connection = nm_simple_connection_new (); @@ -2632,14 +2491,7 @@ test_write_enum_property (void) nmtst_connection_normalize (connection); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - - assert_reread_and_unlink (connection, FALSE, testfile); + write_test_connection_and_reread (connection, FALSE); } static void @@ -2671,11 +2523,6 @@ test_write_flags_property (void) NMSettingConnection *s_con; NMSetting *s_gsm; char *uuid; - gboolean success; - gs_free char *testfile = NULL; - gs_free_error GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; connection = nm_simple_connection_new (); @@ -2704,14 +2551,7 @@ test_write_flags_property (void) nmtst_connection_normalize (connection); - /* Write out the connection */ - owner_uid = geteuid (); - owner_grp = getegid (); - success = nm_keyfile_plugin_write_test_connection (connection, TEST_SCRATCH_DIR, owner_uid, owner_grp, &testfile, &error); - g_assert_no_error (error); - g_assert (success); - - assert_reread_and_unlink (connection, FALSE, testfile); + write_test_connection_and_reread (connection, FALSE); } /*****************************************************************************/ From cadeaf9c205ea627f6033070806157d9e72bec39 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 5 Oct 2016 14:38:33 +0200 Subject: [PATCH 06/11] keyfile/tests: refactor uuid creation in test --- .../plugins/keyfile/tests/test-keyfile.c | 69 +++++-------------- 1 file changed, 18 insertions(+), 51 deletions(-) diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index 93352573bf..0a21351db4 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -314,12 +314,12 @@ add_one_ip_route (NMSettingIPConfig *s_ip, static void test_write_wired_connection (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; - char *uuid; const char *mac = "99:88:77:66:55:44"; const char *dns1 = "4.2.2.1"; const char *dns2 = "4.2.2.2"; @@ -355,7 +355,6 @@ test_write_wired_connection (void) s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ()); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "Work Wired", NM_SETTING_CONNECTION_UUID, uuid, @@ -363,7 +362,6 @@ test_write_wired_connection (void) NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME, NM_SETTING_CONNECTION_TIMESTAMP, timestamp, NULL); - g_free (uuid); /* Wired setting */ @@ -473,12 +471,12 @@ test_read_ip6_wired_connection (void) static void test_write_ip6_wired_connection (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; - char *uuid; const char *dns = "1::cafe"; const char *address = "abcd::beef"; const char *gw = "dcba::beef"; @@ -490,14 +488,12 @@ test_write_ip6_wired_connection (void) s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ()); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "Work Wired IP6", NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_AUTOCONNECT, FALSE, NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME, NULL); - g_free (uuid); /* Wired setting */ @@ -677,12 +673,12 @@ test_read_valid_wireless_connection (void) static void test_write_wireless_connection (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; - char *uuid; const char *bssid = "aa:b9:a1:74:55:44"; GBytes *ssid; unsigned char tmpssid[] = { 0x31, 0x33, 0x33, 0x37 }; @@ -695,7 +691,6 @@ test_write_wireless_connection (void) s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ()); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "Work Wireless", NM_SETTING_CONNECTION_UUID, uuid, @@ -703,7 +698,6 @@ test_write_wireless_connection (void) NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME, NM_SETTING_CONNECTION_TIMESTAMP, timestamp, NULL); - g_free (uuid); /* Wireless setting */ @@ -771,11 +765,12 @@ test_read_string_ssid (void) static void test_write_string_ssid (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingIPConfig *s_ip4; - char *uuid, *tmp; + char *tmp; gs_free char *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 65, 49, 50, 51, 32, 46, 92, 46, 36, 37, 126, 93 }; @@ -788,13 +783,11 @@ test_write_string_ssid (void) s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ()); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "String SSID Test", NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME, NULL); - g_free (uuid); /* Wireless setting */ @@ -860,11 +853,11 @@ test_read_intlist_ssid (void) static void test_write_intlist_ssid (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingIPConfig *s_ip4; - char *uuid; gs_free char *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 65, 49, 50, 51, 0, 50, 50 }; @@ -883,13 +876,11 @@ test_write_intlist_ssid (void) g_assert (s_con); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "Intlist SSID Test", NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME, NULL); - g_free (uuid); /* Wireless setting */ s_wifi = NM_SETTING_WIRELESS (nm_setting_wireless_new ()); @@ -991,11 +982,11 @@ test_read_intlike_ssid_2 (void) static void test_write_intlike_ssid (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingIPConfig *s_ip4; - char *uuid; gs_free char *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 49, 48, 49 }; @@ -1012,13 +1003,11 @@ test_write_intlike_ssid (void) g_assert (s_con); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "Intlike SSID Test", NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME, NULL); - g_free (uuid); /* Wireless setting */ s_wifi = NM_SETTING_WIRELESS (nm_setting_wireless_new ()); @@ -1055,11 +1044,11 @@ test_write_intlike_ssid (void) static void test_write_intlike_ssid_2 (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingIPConfig *s_ip4; - char *uuid; gs_free char *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 49, 49, 59, 49, 50, 59, 49, 51, 59}; @@ -1077,13 +1066,11 @@ test_write_intlike_ssid_2 (void) g_assert (s_con); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "Intlike SSID Test 2", NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME, NULL); - g_free (uuid); /* Wireless setting */ s_wifi = NM_SETTING_WIRELESS (nm_setting_wireless_new ()); @@ -1166,12 +1153,12 @@ test_read_bt_dun_connection (void) static void test_write_bt_dun_connection (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingBluetooth *s_bt; NMSettingIPConfig *s_ip4; NMSettingGsm *s_gsm; - char *uuid; const char *bdaddr = "aa:b9:a1:74:55:44"; guint64 timestamp = 0x12344433L; @@ -1182,7 +1169,6 @@ test_write_bt_dun_connection (void) s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ()); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "T-Mobile Funkadelic", NM_SETTING_CONNECTION_UUID, uuid, @@ -1190,7 +1176,6 @@ test_write_bt_dun_connection (void) NM_SETTING_CONNECTION_TYPE, NM_SETTING_BLUETOOTH_SETTING_NAME, NM_SETTING_CONNECTION_TIMESTAMP, timestamp, NULL); - g_free (uuid); /* Bluetooth setting */ @@ -1274,11 +1259,11 @@ test_read_gsm_connection (void) static void test_write_gsm_connection (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingIPConfig *s_ip4; NMSettingGsm *s_gsm; - char *uuid; guint64 timestamp = 0x12344433L; connection = nm_simple_connection_new (); @@ -1288,7 +1273,6 @@ test_write_gsm_connection (void) s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ()); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "T-Mobile Funkadelic 2", NM_SETTING_CONNECTION_UUID, uuid, @@ -1296,7 +1280,6 @@ test_write_gsm_connection (void) NM_SETTING_CONNECTION_TYPE, NM_SETTING_GSM_SETTING_NAME, NM_SETTING_CONNECTION_TIMESTAMP, timestamp, NULL); - g_free (uuid); /* IP4 setting */ @@ -1553,12 +1536,12 @@ test_read_wired_8021x_tls_new_connection (void) static NMConnection * create_wired_tls_connection (NMSetting8021xCKScheme scheme) { + NMTST_UUID_INIT (uuid); NMConnection *connection; NMSettingConnection *s_con; NMSettingIPConfig *s_ip4; NMSetting *s_wired; NMSetting8021x *s_8021x; - char *uuid; gboolean success; gs_free_error GError *error = NULL; @@ -1570,13 +1553,11 @@ create_wired_tls_connection (NMSetting8021xCKScheme scheme) g_assert (s_con); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "Wired Really Secure TLS", NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME, NULL); - g_free (uuid); /* IP4 setting */ s_ip4 = (NMSettingIPConfig *) nm_setting_ip4_config_new (); @@ -1836,12 +1817,12 @@ test_read_infiniband_connection (void) static void test_write_infiniband_connection (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingInfiniband *s_ib; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; - char *uuid; const char *mac = "99:88:77:66:55:44:ab:bc:cd:de:ef:f0:0a:1b:2c:3d:4e:5f:6f:ba"; connection = nm_simple_connection_new (); @@ -1852,14 +1833,12 @@ test_write_infiniband_connection (void) g_assert (s_con); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "Work InfiniBand", NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_AUTOCONNECT, FALSE, NM_SETTING_CONNECTION_TYPE, NM_SETTING_INFINIBAND_SETTING_NAME, NULL); - g_free (uuid); /* InfiniBand setting */ s_ib = (NMSettingInfiniband *) nm_setting_infiniband_new (); @@ -1933,12 +1912,12 @@ test_read_bridge_main (void) static void test_write_bridge_main (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingBridge *s_bridge; NMSettingIPConfig *s_ip4; NMSettingIPConfig *s_ip6; - char *uuid; connection = nm_simple_connection_new (); g_assert (connection); @@ -1948,7 +1927,6 @@ test_write_bridge_main (void) g_assert (s_con); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "Test Write Bridge Main", NM_SETTING_CONNECTION_UUID, uuid, @@ -1956,7 +1934,6 @@ test_write_bridge_main (void) NM_SETTING_CONNECTION_TYPE, NM_SETTING_BRIDGE_SETTING_NAME, NM_SETTING_CONNECTION_INTERFACE_NAME, "br0", NULL); - g_free (uuid); /* Bridge setting */ s_bridge = (NMSettingBridge *) nm_setting_bridge_new (); @@ -2031,11 +2008,11 @@ test_read_bridge_component (void) static void test_write_bridge_component (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingBridgePort *s_port; NMSettingWired *s_wired; - char *uuid; const char *mac = "99:88:77:66:55:44"; connection = nm_simple_connection_new (); @@ -2046,7 +2023,6 @@ test_write_bridge_component (void) g_assert (s_con); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "Test Write Bridge Component", NM_SETTING_CONNECTION_UUID, uuid, @@ -2055,7 +2031,6 @@ test_write_bridge_component (void) NM_SETTING_CONNECTION_MASTER, "br0", NM_SETTING_CONNECTION_SLAVE_TYPE, NM_SETTING_BRIDGE_SETTING_NAME, NULL); - g_free (uuid); /* Wired setting */ s_wired = NM_SETTING_WIRED (nm_setting_wired_new ()); @@ -2110,11 +2085,11 @@ test_read_new_wired_group_name (void) static void test_write_new_wired_group_name (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; gs_unref_keyfile GKeyFile *kf = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; - char *uuid; gboolean success; gs_free char *testfile = NULL; gs_free_error GError *error = NULL; @@ -2129,13 +2104,11 @@ test_write_new_wired_group_name (void) g_assert (s_con); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "Test Write Wired New Group Name", NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME, NULL); - g_free (uuid); /* Wired setting */ s_wired = (NMSettingWired *) nm_setting_wired_new (); @@ -2207,12 +2180,12 @@ test_read_new_wireless_group_names (void) static void test_write_new_wireless_group_names (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; gs_unref_keyfile GKeyFile *kf = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingWirelessSecurity *s_wsec; - char *uuid; GBytes *ssid; unsigned char tmpssid[] = { 0x31, 0x33, 0x33, 0x37 }; const char *expected_psk = "asdfasdfasdfa12315"; @@ -2228,13 +2201,11 @@ test_write_new_wireless_group_names (void) s_con = (NMSettingConnection *) nm_setting_connection_new (); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "Test Write New Wireless Group Names", NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRELESS_SETTING_NAME, NULL); - g_free (uuid); /* WiFi setting */ s_wifi = (NMSettingWireless *) nm_setting_wireless_new (); @@ -2456,11 +2427,11 @@ test_read_enum_property (void) static void test_write_enum_property (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip6; - char *uuid; connection = nm_simple_connection_new (); @@ -2469,13 +2440,11 @@ test_write_enum_property (void) s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ()); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "Test Write Enum Property", NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_TYPE, NM_SETTING_WIRED_SETTING_NAME, NULL); - g_free (uuid); /* Wired setting */ s_wired = NM_SETTING_WIRED (nm_setting_wired_new ()); @@ -2519,10 +2488,10 @@ test_read_flags_property (void) static void test_write_flags_property (void) { + NMTST_UUID_INIT (uuid); gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSetting *s_gsm; - char *uuid; connection = nm_simple_connection_new (); @@ -2531,13 +2500,11 @@ test_write_flags_property (void) s_con = NM_SETTING_CONNECTION (nm_setting_connection_new ()); nm_connection_add_setting (connection, NM_SETTING (s_con)); - uuid = nm_utils_uuid_generate (); g_object_set (s_con, NM_SETTING_CONNECTION_ID, "Test Write Flags Property", NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_TYPE, NM_SETTING_GSM_SETTING_NAME, NULL); - g_free (uuid); /* GSM setting */ s_gsm = nm_setting_gsm_new (); From a93fee0844dacb38901087c239f9ca9604d82921 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 5 Oct 2016 14:44:44 +0200 Subject: [PATCH 07/11] keyfile/tests: refactor loading plain keyfile via glib API --- .../plugins/keyfile/tests/test-keyfile.c | 59 ++++++++----------- 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index 0a21351db4..d227c7a5be 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -133,6 +133,23 @@ write_test_connection_and_reread (NMConnection *connection, gboolean normalize_c assert_reread_and_unlink (connection, normalize_connection, testfile); } +static GKeyFile * +keyfile_load_from_file (const char *testfile) +{ + GKeyFile *keyfile; + GError *error = NULL; + gboolean success; + + g_assert (testfile && *testfile); + + keyfile = g_key_file_new (); + success = g_key_file_load_from_file (keyfile, testfile, G_KEY_FILE_NONE, &error); + g_assert_no_error (error); + g_assert(success); + + return keyfile; +} + /*****************************************************************************/ static void @@ -810,8 +827,7 @@ test_write_string_ssid (void) write_test_connection (connection, &testfile); /* Ensure the SSID was written out as a string */ - keyfile = g_key_file_new (); - g_assert (g_key_file_load_from_file (keyfile, testfile, 0, NULL)); + keyfile = keyfile_load_from_file (testfile); tmp = g_key_file_get_string (keyfile, "wifi", NM_SETTING_WIRELESS_SSID, NULL); g_assert (tmp); g_assert_cmpmem (tmp, strlen (tmp), tmpssid, sizeof (tmpssid)); @@ -861,7 +877,6 @@ test_write_intlist_ssid (void) gs_free char *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 65, 49, 50, 51, 0, 50, 50 }; - gboolean success; gs_free_error GError *error = NULL; gs_unref_keyfile GKeyFile *keyfile = NULL; gint *intlist; @@ -900,10 +915,7 @@ test_write_intlist_ssid (void) write_test_connection (connection, &testfile); /* Ensure the SSID was written out as an int list */ - keyfile = g_key_file_new (); - success = g_key_file_load_from_file (keyfile, testfile, 0, &error); - g_assert_no_error (error); - g_assert (success); + keyfile = keyfile_load_from_file (testfile); intlist = g_key_file_get_integer_list (keyfile, "wifi", NM_SETTING_WIRELESS_SSID, &len, &error); g_assert_no_error (error); @@ -990,7 +1002,6 @@ test_write_intlike_ssid (void) gs_free char *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 49, 48, 49 }; - gboolean success; gs_free_error GError *error = NULL; gs_unref_keyfile GKeyFile *keyfile = NULL; char *tmp; @@ -1027,10 +1038,7 @@ test_write_intlike_ssid (void) write_test_connection (connection, &testfile); /* Ensure the SSID was written out as a plain "101" */ - keyfile = g_key_file_new (); - success = g_key_file_load_from_file (keyfile, testfile, 0, &error); - g_assert_no_error (error); - g_assert (success); + keyfile = keyfile_load_from_file (testfile); tmp = g_key_file_get_string (keyfile, "wifi", NM_SETTING_WIRELESS_SSID, &error); g_assert_no_error (error); @@ -1052,7 +1060,6 @@ test_write_intlike_ssid_2 (void) gs_free char *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 49, 49, 59, 49, 50, 59, 49, 51, 59}; - gboolean success; gs_free_error GError *error = NULL; gs_unref_keyfile GKeyFile *keyfile = NULL; char *tmp; @@ -1090,10 +1097,7 @@ test_write_intlike_ssid_2 (void) write_test_connection (connection, &testfile); /* Ensure the SSID was written out as a plain "11;12;13;" */ - keyfile = g_key_file_new (); - success = g_key_file_load_from_file (keyfile, testfile, 0, &error); - g_assert_no_error (error); - g_assert (success); + keyfile = keyfile_load_from_file (testfile); tmp = g_key_file_get_string (keyfile, "wifi", NM_SETTING_WIRELESS_SSID, &error); g_assert_no_error (error); @@ -1644,14 +1648,7 @@ test_write_wired_8021x_tls_connection_path (void) } /* Ensure the cert and key values are properly written out */ - keyfile = g_key_file_new (); - g_assert (keyfile); - success = g_key_file_load_from_file (keyfile, testfile, G_KEY_FILE_NONE, &error); - if (!success) { - g_assert (error); - g_warning ("Failed to re-read test file %s: %s", testfile, error->message); - g_assert (success); - } + keyfile = keyfile_load_from_file (testfile); /* Depending on whether this test is being run from 'make check' or * 'make distcheck' we might be using relative paths (check) or @@ -2090,7 +2087,6 @@ test_write_new_wired_group_name (void) gs_unref_keyfile GKeyFile *kf = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; - gboolean success; gs_free char *testfile = NULL; gs_free_error GError *error = NULL; char *s; @@ -2121,10 +2117,7 @@ test_write_new_wired_group_name (void) assert_reread (connection, TRUE, testfile); /* Look at the keyfile itself to ensure we wrote out the new group names and type */ - kf = g_key_file_new (); - success = g_key_file_load_from_file (kf, testfile, G_KEY_FILE_NONE, &error); - g_assert_no_error (error); - g_assert (success); + kf = keyfile_load_from_file (testfile); s = g_key_file_get_string (kf, NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_TYPE, &error); g_assert_no_error (error); @@ -2189,7 +2182,6 @@ test_write_new_wireless_group_names (void) GBytes *ssid; unsigned char tmpssid[] = { 0x31, 0x33, 0x33, 0x37 }; const char *expected_psk = "asdfasdfasdfa12315"; - gboolean success; gs_free char *testfile = NULL; gs_free_error GError *error = NULL; char *s; @@ -2231,10 +2223,7 @@ test_write_new_wireless_group_names (void) assert_reread (connection, TRUE, testfile); /* Look at the keyfile itself to ensure we wrote out the new group names and type */ - kf = g_key_file_new (); - success = g_key_file_load_from_file (kf, testfile, G_KEY_FILE_NONE, &error); - g_assert_no_error (error); - g_assert (success); + kf = keyfile_load_from_file (testfile); s = g_key_file_get_string (kf, NM_SETTING_CONNECTION_SETTING_NAME, NM_SETTING_CONNECTION_TYPE, &error); g_assert_no_error (error); From 29b576bd70bf169e20ca4e6d2b05075957876de9 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 4 Oct 2016 16:32:09 +0200 Subject: [PATCH 08/11] proxy: cleanup handling of proxy-method The numeric value of NM_SETTING_PROXY_METHOD_NONE should be zero, as that is the more natural default. Also, cast all uses of the enum values in g_object_set() to (int). --- clients/cli/devices.c | 2 +- clients/cli/settings.c | 8 ++++---- libnm-core/nm-setting-proxy.c | 19 ++++++++++++++----- libnm-core/nm-setting-proxy.h | 8 ++++---- src/settings/plugins/ifcfg-rh/reader.c | 5 +++-- src/settings/plugins/ifcfg-rh/writer.c | 2 +- 6 files changed, 27 insertions(+), 17 deletions(-) diff --git a/clients/cli/devices.c b/clients/cli/devices.c index 5409452f36..f9d0f4ef4b 100644 --- a/clients/cli/devices.c +++ b/clients/cli/devices.c @@ -3482,7 +3482,7 @@ do_device_wifi_hotspot (NmCli *nmc, int argc, char **argv) s_proxy = (NMSettingProxy *) nm_setting_proxy_new (); nm_connection_add_setting (connection, NM_SETTING (s_proxy)); - g_object_set (s_proxy, NM_SETTING_PROXY_METHOD, NM_SETTING_PROXY_METHOD_NONE, NULL); + g_object_set (s_proxy, NM_SETTING_PROXY_METHOD, (int) NM_SETTING_PROXY_METHOD_NONE, NULL); /* Activate the connection now */ nmc->nowait_flag = (nmc->timeout == 0); diff --git a/clients/cli/settings.c b/clients/cli/settings.c index dd777b2b2b..9491b7f33f 100644 --- a/clients/cli/settings.c +++ b/clients/cli/settings.c @@ -2133,18 +2133,18 @@ static gboolean nmc_property_proxy_set_method (NMSetting *setting, const char *prop, const char *val, GError **error) { - NMSettingProxyMethod method; + int method; gboolean ret; ret = nm_utils_enum_from_str (nm_setting_proxy_method_get_type(), val, - (int *) &method, NULL); + &method, NULL); if (!ret) { gs_free const char **values = NULL; gs_free char *values_str = NULL; values = nm_utils_enum_get_values (nm_setting_proxy_method_get_type (), - NM_SETTING_PROXY_METHOD_AUTO, + NM_SETTING_PROXY_METHOD_NONE, G_MAXINT); values_str = g_strjoinv (",", (char **) values); g_set_error (error, 1, 0, _("invalid method '%s', use one of %s"), @@ -2559,7 +2559,7 @@ nmc_setting_custom_init (NMSetting *setting) NULL); } else if (NM_IS_SETTING_PROXY (setting)) { g_object_set (NM_SETTING_PROXY (setting), - NM_SETTING_PROXY_METHOD, NM_SETTING_PROXY_METHOD_NONE, + NM_SETTING_PROXY_METHOD, (int) NM_SETTING_PROXY_METHOD_NONE, NULL); } else if (NM_IS_SETTING_TUN (setting)) { g_object_set (NM_SETTING_TUN (setting), diff --git a/libnm-core/nm-setting-proxy.c b/libnm-core/nm-setting-proxy.c index 1cfd7cd491..2e4dca1e65 100644 --- a/libnm-core/nm-setting-proxy.c +++ b/libnm-core/nm-setting-proxy.c @@ -153,7 +153,19 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) NMSettingProxyMethod method; method = priv->method; - if (method == NM_SETTING_PROXY_METHOD_NONE) { + + if (!NM_IN_SET (method, + NM_SETTING_PROXY_METHOD_NONE, + NM_SETTING_PROXY_METHOD_AUTO)) { + g_set_error (error, + NM_CONNECTION_ERROR, + NM_CONNECTION_ERROR_INVALID_PROPERTY, + _("invalid proxy method")); + g_prefix_error (error, "%s.%s: ", NM_SETTING_PROXY_SETTING_NAME, NM_SETTING_PROXY_PAC_URL); + return FALSE; + } + + if (method != NM_SETTING_PROXY_METHOD_AUTO) { if (priv->pac_url) { g_set_error (error, NM_CONNECTION_ERROR, @@ -188,9 +200,6 @@ verify (NMSetting *setting, NMConnection *connection, GError **error) static void nm_setting_proxy_init (NMSettingProxy *setting) { - NMSettingProxyPrivate *priv = NM_SETTING_PROXY_GET_PRIVATE (setting); - - priv->method = NM_SETTING_PROXY_METHOD_NONE; } static void @@ -207,7 +216,7 @@ finalize (GObject *object) static void get_property (GObject *object, guint prop_id, - GValue *value, GParamSpec *pspec) + GValue *value, GParamSpec *pspec) { NMSettingProxy *setting = NM_SETTING_PROXY (object); diff --git a/libnm-core/nm-setting-proxy.h b/libnm-core/nm-setting-proxy.h index c51359fb22..2009dfab98 100644 --- a/libnm-core/nm-setting-proxy.h +++ b/libnm-core/nm-setting-proxy.h @@ -32,16 +32,16 @@ G_BEGIN_DECLS /** * NMSettingProxyMethod: - * @NM_SETTING_PROXY_METHOD_AUTO: DHCP obtained Proxy/ Manual override - * @NM_SETTING_PROXY_METHOD_NONE: No Proxy for the Connection + * @NM_SETTING_PROXY_METHOD_NONE: No Proxy for the Connection + * @NM_SETTING_PROXY_METHOD_AUTO: DHCP obtained Proxy/ Manual override * * The Proxy method. * * Since: 1.6 */ typedef enum { - NM_SETTING_PROXY_METHOD_AUTO = 0, - NM_SETTING_PROXY_METHOD_NONE + NM_SETTING_PROXY_METHOD_NONE = 0, + NM_SETTING_PROXY_METHOD_AUTO = 1, } NMSettingProxyMethod; #define NM_TYPE_SETTING_PROXY (nm_setting_proxy_get_type ()) diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c index bf15d32d29..dd7ea3cefa 100644 --- a/src/settings/plugins/ifcfg-rh/reader.c +++ b/src/settings/plugins/ifcfg-rh/reader.c @@ -922,7 +922,7 @@ make_proxy_setting (shvarFile *ifcfg, GError **error) switch (method) { case NM_SETTING_PROXY_METHOD_AUTO: g_object_set (s_proxy, - NM_SETTING_PROXY_METHOD, NM_SETTING_PROXY_METHOD_AUTO, + NM_SETTING_PROXY_METHOD, (int) NM_SETTING_PROXY_METHOD_AUTO, NULL); value = svGetValue (ifcfg, "PAC_URL", FALSE); @@ -942,8 +942,9 @@ make_proxy_setting (shvarFile *ifcfg, GError **error) break; case NM_SETTING_PROXY_METHOD_NONE: g_object_set (s_proxy, - NM_SETTING_PROXY_METHOD, NM_SETTING_PROXY_METHOD_NONE, + NM_SETTING_PROXY_METHOD, (int) NM_SETTING_PROXY_METHOD_NONE, NULL); + break; } value = svGetValue (ifcfg, "BROWSER_ONLY", FALSE); diff --git a/src/settings/plugins/ifcfg-rh/writer.c b/src/settings/plugins/ifcfg-rh/writer.c index 383337c77f..8dc4cbe2af 100644 --- a/src/settings/plugins/ifcfg-rh/writer.c +++ b/src/settings/plugins/ifcfg-rh/writer.c @@ -2030,7 +2030,7 @@ write_proxy_setting (NMConnection *connection, shvarFile *ifcfg, GError **error) break; case NM_SETTING_PROXY_METHOD_NONE: svSetValue (ifcfg, "PROXY_METHOD", "none", FALSE); - /* Write nothing more */ + break; } browser_only = nm_setting_proxy_get_browser_only (s_proxy); From e2c71c82e7e8bc87b3f1c820adacf3595dc18656 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 4 Oct 2016 18:46:09 +0200 Subject: [PATCH 09/11] libnm: cleanup error paths in _nm_connection_verify() --- libnm-core/nm-connection.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/libnm-core/nm-connection.c b/libnm-core/nm-connection.c index 9e9ab930ef..f7ebe6418b 100644 --- a/libnm-core/nm-connection.c +++ b/libnm-core/nm-connection.c @@ -989,8 +989,7 @@ _nm_connection_verify (NMConnection *connection, GError **error) GHashTableIter iter; gpointer value; GSList *all_settings = NULL, *setting_i; - NMSettingVerifyResult success = NM_SETTING_VERIFY_ERROR; - GError *normalizable_error = NULL; + gs_free_error GError *normalizable_error = NULL; NMSettingVerifyResult normalizable_error_type = NM_SETTING_VERIFY_SUCCESS; g_return_val_if_fail (NM_IS_CONNECTION (connection), NM_SETTING_VERIFY_ERROR); @@ -1006,7 +1005,7 @@ _nm_connection_verify (NMConnection *connection, GError **error) NM_CONNECTION_ERROR_MISSING_SETTING, _("setting not found")); g_prefix_error (error, "%s: ", NM_SETTING_CONNECTION_SETTING_NAME); - goto EXIT; + return NM_SETTING_VERIFY_ERROR; } /* Build up the list of settings */ @@ -1051,8 +1050,8 @@ _nm_connection_verify (NMConnection *connection, GError **error) } else if (verify_result != NM_SETTING_VERIFY_SUCCESS) { g_propagate_error (error, verify_error); g_slist_free (all_settings); - g_return_val_if_fail (verify_result == NM_SETTING_VERIFY_ERROR, success); - goto EXIT; + g_return_val_if_fail (verify_result == NM_SETTING_VERIFY_ERROR, NM_SETTING_VERIFY_ERROR); + return NM_SETTING_VERIFY_ERROR; } g_clear_error (&verify_error); } @@ -1062,26 +1061,32 @@ _nm_connection_verify (NMConnection *connection, GError **error) s_ip6 = nm_connection_get_setting_ip6_config (connection); if (nm_setting_connection_get_master (s_con)) { - if ((normalizable_error_type == NM_SETTING_VERIFY_SUCCESS || - (normalizable_error_type == NM_SETTING_VERIFY_NORMALIZABLE)) && (s_ip4 || s_ip6)) { + if ( NM_IN_SET (normalizable_error_type, NM_SETTING_VERIFY_SUCCESS, + NM_SETTING_VERIFY_NORMALIZABLE) + && (s_ip4 || s_ip6)) { g_clear_error (&normalizable_error); g_set_error_literal (&normalizable_error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_INVALID_SETTING, _("setting not allowed in slave connection")); g_prefix_error (&normalizable_error, "%s: ", - s_ip4 ? NM_SETTING_IP4_CONFIG_SETTING_NAME : NM_SETTING_IP6_CONFIG_SETTING_NAME); + s_ip4 + ? NM_SETTING_IP4_CONFIG_SETTING_NAME + : NM_SETTING_IP6_CONFIG_SETTING_NAME); /* having a slave with IP config *was* and is a verify() error. */ normalizable_error_type = NM_SETTING_VERIFY_NORMALIZABLE_ERROR; } } else { - if (normalizable_error_type == NM_SETTING_VERIFY_SUCCESS && (!s_ip4 || !s_ip6)) { + if ( NM_IN_SET (normalizable_error_type, NM_SETTING_VERIFY_SUCCESS) + && (!s_ip4 || !s_ip6)) { g_set_error_literal (&normalizable_error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_MISSING_SETTING, _("setting is required for non-slave connections")); g_prefix_error (&normalizable_error, "%s: ", - !s_ip4 ? NM_SETTING_IP4_CONFIG_SETTING_NAME : NM_SETTING_IP6_CONFIG_SETTING_NAME); + !s_ip4 + ? NM_SETTING_IP4_CONFIG_SETTING_NAME + : NM_SETTING_IP6_CONFIG_SETTING_NAME); /* having a master without IP config was not a verify() error, accept * it for backward compatibility. */ normalizable_error_type = NM_SETTING_VERIFY_NORMALIZABLE; @@ -1091,13 +1096,10 @@ _nm_connection_verify (NMConnection *connection, GError **error) if (normalizable_error_type != NM_SETTING_VERIFY_SUCCESS) { g_propagate_error (error, normalizable_error); normalizable_error = NULL; - success = normalizable_error_type; - } else - success = NM_SETTING_VERIFY_SUCCESS; + return normalizable_error_type; + } -EXIT: - g_clear_error (&normalizable_error); - return success; + return NM_SETTING_VERIFY_SUCCESS; } /** From 8b51e345af65982b03fb0f0e5114e8a7e14a5090 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 4 Oct 2016 16:44:39 +0200 Subject: [PATCH 10/11] libnm/proxy: add proxy setting for non-slave connection during normalization And reject slave settings with proxies. --- libnm-core/nm-connection.c | 29 +++++++++++++++---- libnm-core/tests/test-general.c | 5 ++-- libnm-core/tests/test-keyfile.c | 16 +++++++++- .../plugins/ifcfg-rh/tests/test-ifcfg-rh.c | 8 +++++ .../plugins/keyfile/tests/test-keyfile.c | 11 +++++++ 5 files changed, 60 insertions(+), 9 deletions(-) diff --git a/libnm-core/nm-connection.c b/libnm-core/nm-connection.c index f7ebe6418b..d26f57a654 100644 --- a/libnm-core/nm-connection.c +++ b/libnm-core/nm-connection.c @@ -724,6 +724,7 @@ _normalize_ip_config (NMConnection *self, GHashTable *parameters) const char *default_ip4_method = NM_SETTING_IP4_CONFIG_METHOD_AUTO; const char *default_ip6_method = NULL; NMSettingIPConfig *s_ip4, *s_ip6; + NMSettingProxy *s_proxy; NMSetting *setting; gboolean changed = FALSE; guint num, i; @@ -735,6 +736,7 @@ _normalize_ip_config (NMConnection *self, GHashTable *parameters) s_ip4 = nm_connection_get_setting_ip4_config (self); s_ip6 = nm_connection_get_setting_ip6_config (self); + s_proxy = nm_connection_get_setting_proxy (self); if (nm_setting_connection_get_master (s_con)) { /* Slave connections don't have IP configuration. */ @@ -745,7 +747,10 @@ _normalize_ip_config (NMConnection *self, GHashTable *parameters) if (s_ip6) nm_connection_remove_setting (self, NM_TYPE_SETTING_IP6_CONFIG); - return s_ip4 || s_ip6; + if (s_proxy) + nm_connection_remove_setting (self, NM_TYPE_SETTING_PROXY); + + return s_ip4 || s_ip6 || s_proxy; } else { /* Ensure all non-slave connections have IP4 and IP6 settings objects. If no * IP6 setting was specified, then assume that means IP6 config is allowed @@ -822,7 +827,13 @@ _normalize_ip_config (NMConnection *self, GHashTable *parameters) changed = TRUE; } } - return !s_ip4 || !s_ip6 || changed; + + if (!s_proxy) { + setting = nm_setting_proxy_new (); + nm_connection_add_setting (self, setting); + } + + return !s_ip4 || !s_ip6 || !s_proxy || changed; } } @@ -986,6 +997,7 @@ _nm_connection_verify (NMConnection *connection, GError **error) NMConnectionPrivate *priv; NMSettingConnection *s_con; NMSettingIPConfig *s_ip4, *s_ip6; + NMSettingProxy *s_proxy; GHashTableIter iter; gpointer value; GSList *all_settings = NULL, *setting_i; @@ -1059,11 +1071,12 @@ _nm_connection_verify (NMConnection *connection, GError **error) s_ip4 = nm_connection_get_setting_ip4_config (connection); s_ip6 = nm_connection_get_setting_ip6_config (connection); + s_proxy = nm_connection_get_setting_proxy (connection); if (nm_setting_connection_get_master (s_con)) { if ( NM_IN_SET (normalizable_error_type, NM_SETTING_VERIFY_SUCCESS, NM_SETTING_VERIFY_NORMALIZABLE) - && (s_ip4 || s_ip6)) { + && (s_ip4 || s_ip6 || s_proxy)) { g_clear_error (&normalizable_error); g_set_error_literal (&normalizable_error, NM_CONNECTION_ERROR, @@ -1072,13 +1085,15 @@ _nm_connection_verify (NMConnection *connection, GError **error) g_prefix_error (&normalizable_error, "%s: ", s_ip4 ? NM_SETTING_IP4_CONFIG_SETTING_NAME - : NM_SETTING_IP6_CONFIG_SETTING_NAME); + : (s_ip6 + ? NM_SETTING_IP6_CONFIG_SETTING_NAME + : NM_SETTING_PROXY_SETTING_NAME)); /* having a slave with IP config *was* and is a verify() error. */ normalizable_error_type = NM_SETTING_VERIFY_NORMALIZABLE_ERROR; } } else { if ( NM_IN_SET (normalizable_error_type, NM_SETTING_VERIFY_SUCCESS) - && (!s_ip4 || !s_ip6)) { + && (!s_ip4 || !s_ip6 || !s_proxy)) { g_set_error_literal (&normalizable_error, NM_CONNECTION_ERROR, NM_CONNECTION_ERROR_MISSING_SETTING, @@ -1086,7 +1101,9 @@ _nm_connection_verify (NMConnection *connection, GError **error) g_prefix_error (&normalizable_error, "%s: ", !s_ip4 ? NM_SETTING_IP4_CONFIG_SETTING_NAME - : NM_SETTING_IP6_CONFIG_SETTING_NAME); + : (!s_ip6 + ? NM_SETTING_IP6_CONFIG_SETTING_NAME + : NM_SETTING_PROXY_SETTING_NAME)); /* having a master without IP config was not a verify() error, accept * it for backward compatibility. */ normalizable_error_type = NM_SETTING_VERIFY_NORMALIZABLE; diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 52c6f80911..414c6659b3 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -3900,6 +3900,7 @@ test_connection_normalize_gateway_never_default (void) nm_connection_add_setting (con, (NMSetting *) s_ip4); nm_connection_add_setting (con, (NMSetting *) s_ip6); + nm_connection_add_setting (con, nm_setting_proxy_new ()); nmtst_assert_connection_verifies_without_normalization (con); g_assert_cmpstr ("1.1.1.254", ==, nm_setting_ip_config_get_gateway (s_ip4)); @@ -3942,7 +3943,7 @@ test_connection_normalize_may_fail (void) nm_connection_add_setting (con, (NMSetting *) s_ip4); nm_connection_add_setting (con, (NMSetting *) s_ip6); - nmtst_assert_connection_verifies_without_normalization (con); + nmtst_assert_connection_verifies_and_normalizable (con); /* Now set method=disabled/ignore and check that may-fail becomes TRUE * after normalization @@ -3989,7 +3990,7 @@ test_connection_normalize_shared_addresses (void) nm_connection_add_setting (con, (NMSetting *) s_ip4); nm_connection_add_setting (con, (NMSetting *) s_ip6); - nmtst_assert_connection_verifies_without_normalization (con); + nmtst_assert_connection_verifies_and_normalizable (con); /* Now we add other addresses and check that they are * removed during normalization diff --git a/libnm-core/tests/test-keyfile.c b/libnm-core/tests/test-keyfile.c index 84dcffbb9f..9944eb5ef8 100644 --- a/libnm-core/tests/test-keyfile.c +++ b/libnm-core/tests/test-keyfile.c @@ -28,6 +28,7 @@ #include "nm-setting-wired.h" #include "nm-setting-8021x.h" #include "nm-setting-team.h" +#include "nm-setting-proxy.h" #include "nm-utils/nm-test-utils.h" @@ -115,8 +116,21 @@ _nm_keyfile_read (GKeyFile *keyfile, if (needs_normalization) { nmtst_assert_connection_verifies_after_normalization (con, 0, 0); nmtst_connection_normalize (con); - } else + } else { + { + NMSettingConnection *s_con; + + /* a non-slave connection must have a proxy setting, but + * keyfile reader does not add that (unless a [proxy] section + * is present. */ + s_con = nm_connection_get_setting_connection (con); + if ( s_con + && !nm_setting_connection_get_master (s_con) + && !nm_connection_get_setting_proxy (con)) + nm_connection_add_setting (con, nm_setting_proxy_new ()); + } nmtst_assert_connection_verifies_without_normalization (con); + } return con; } diff --git a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c index 9ed48bebe0..b71f732d15 100644 --- a/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c +++ b/src/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh.c @@ -3645,6 +3645,8 @@ test_write_wired_static (void) g_assert_cmpint (nm_setting_ip_config_get_route_metric (reread_s_ip4), ==, 204); g_assert_cmpint (nm_setting_ip_config_get_route_metric (reread_s_ip6), ==, 206); + nm_connection_add_setting (connection, nm_setting_proxy_new ()); + nmtst_assert_connection_equals (connection, FALSE, reread, FALSE); route6file = utils_get_route6_path (testfile); @@ -7149,6 +7151,8 @@ test_write_bridge_main (void) NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL); + nm_connection_add_setting (connection, nm_setting_proxy_new ()); + nmtst_assert_connection_verifies_without_normalization (connection); _writer_new_connection (connection, @@ -7760,6 +7764,8 @@ test_write_bond_main (void) NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL); + nm_connection_add_setting (connection, nm_setting_proxy_new ()); + nmtst_assert_connection_verifies_without_normalization (connection); _writer_new_connection (connection, @@ -8538,6 +8544,8 @@ test_write_team_master (void) NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO, NULL); + nm_connection_add_setting (connection, nm_setting_proxy_new ()); + nmtst_assert_connection_verifies_without_normalization (connection); _writer_new_connection (connection, diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index d227c7a5be..8c4c798133 100644 --- a/src/settings/plugins/keyfile/tests/test-keyfile.c +++ b/src/settings/plugins/keyfile/tests/test-keyfile.c @@ -81,8 +81,10 @@ static void assert_reread (NMConnection *connection, gboolean normalize_connection, const char *testfile) { gs_unref_object NMConnection *reread = NULL; + gs_unref_object NMConnection *connection_clone = NULL; GError *error = NULL; GError **p_error = (nmtst_get_rand_int () % 2) ? &error : NULL; + NMSettingConnection *s_con; g_assert (NM_IS_CONNECTION (connection)); g_assert (testfile && testfile[0]); @@ -91,6 +93,15 @@ assert_reread (NMConnection *connection, gboolean normalize_connection, const ch g_assert_no_error (error); g_assert (NM_IS_CONNECTION (reread)); + if ( !normalize_connection + && (s_con = nm_connection_get_setting_connection (connection)) + && !nm_setting_connection_get_master (s_con) + && !nm_connection_get_setting_proxy (connection)) { + connection_clone = nmtst_clone_connection (connection); + connection = connection_clone; + nm_connection_add_setting (connection, nm_setting_proxy_new ()); + } + nmtst_assert_connection_equals (connection, normalize_connection, reread, FALSE); } From 8bc8d343ca3d6ab47675e3b345debef810a57bac Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Wed, 5 Oct 2016 14:58:03 +0200 Subject: [PATCH 11/11] ifcfg-rh/proxy: fix memleak in make_proxy_setting() --- src/settings/plugins/ifcfg-rh/reader.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c index dd7ea3cefa..c46a454633 100644 --- a/src/settings/plugins/ifcfg-rh/reader.c +++ b/src/settings/plugins/ifcfg-rh/reader.c @@ -949,10 +949,9 @@ make_proxy_setting (shvarFile *ifcfg, GError **error) value = svGetValue (ifcfg, "BROWSER_ONLY", FALSE); if (value) { - if (!g_ascii_strcasecmp (value, "yes")) { + if (!g_ascii_strcasecmp (value, "yes")) g_object_set (s_proxy, NM_SETTING_PROXY_BROWSER_ONLY, TRUE, NULL); - g_free (value); - } + g_free (value); } return NM_SETTING (s_proxy);