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-connection.c b/libnm-core/nm-connection.c index 9e9ab930ef..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,11 +997,11 @@ _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; - 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 +1017,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 +1062,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); } @@ -1060,28 +1071,39 @@ _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 ((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 || s_proxy)) { 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 + : (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 (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 || !s_proxy)) { 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 + : (!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; @@ -1091,13 +1113,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; } /** 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/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/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) { diff --git a/src/settings/plugins/ifcfg-rh/reader.c b/src/settings/plugins/ifcfg-rh/reader.c index bf15d32d29..c46a454633 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,16 +942,16 @@ 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); 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); 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/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); diff --git a/src/settings/plugins/keyfile/tests/test-keyfile.c b/src/settings/plugins/keyfile/tests/test-keyfile.c index e63fa495db..8c4c798133 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) { @@ -62,7 +64,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); @@ -75,15 +77,101 @@ 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; + 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]); + + reread = nm_keyfile_plugin_connection_from_file (testfile, p_error); + 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); +} + +static void +assert_reread_and_unlink (NMConnection *connection, gboolean normalize_connection, const char *testfile) +{ + assert_reread (connection, normalize_connection, testfile); + 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 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 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 +303,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 +311,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 +328,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,19 +342,13 @@ add_one_ip_route (NMSettingIPConfig *s_ip, static void test_write_wired_connection (void) { - NMConnection *connection; + 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"; - gboolean success; - NMConnection *reread; - char *testfile = NULL; - 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"; @@ -302,7 +383,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, @@ -310,7 +390,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 */ @@ -372,36 +451,18 @@ 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); - - /* 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)); - - g_clear_error (&error); - unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); + write_test_connection_and_reread (connection, FALSE); } 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,25 +494,17 @@ 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; + NMTST_UUID_INIT (uuid); + gs_unref_object NMConnection *connection = 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; - pid_t owner_grp; - uid_t owner_uid; const char *dns = "1::cafe"; const char *address = "abcd::beef"; const char *gw = "dcba::beef"; @@ -463,14 +516,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 */ @@ -505,34 +556,16 @@ 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); - - /* 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)); - - g_clear_error (&error); - unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); + write_test_connection_and_reread (connection, FALSE); } 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 +595,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 +628,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 +657,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,28 +696,20 @@ 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; + 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 }; - gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; guint64 timestamp = 0x12344433L; connection = nm_simple_connection_new (); @@ -700,7 +719,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, @@ -708,7 +726,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 */ @@ -743,33 +760,15 @@ 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); - - /* 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)); - - g_clear_error (&error); - unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); + write_test_connection_and_reread (connection, FALSE); } 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,26 +788,21 @@ 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; + NMTST_UUID_INIT (uuid); + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wireless; NMSettingIPConfig *s_ip4; - char *uuid, *testfile = NULL, *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 }; - gboolean success; - NMConnection *reread; - GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; - GKeyFile *keyfile; + gs_unref_keyfile GKeyFile *keyfile = NULL; connection = nm_simple_connection_new (); @@ -817,13 +811,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 */ @@ -843,43 +835,24 @@ 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 (); - 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)); 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); - 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); + assert_reread_and_unlink (connection, TRUE, testfile); } 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,26 +875,21 @@ 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; + NMTST_UUID_INIT (uuid); + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingIPConfig *s_ip4; - char *uuid, *testfile = NULL; + gs_free char *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 65, 49, 50, 51, 0, 50, 50 }; - gboolean success; - NMConnection *reread; - GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; - GKeyFile *keyfile; + gs_free_error GError *error = NULL; + gs_unref_keyfile GKeyFile *keyfile = NULL; gint *intlist; gsize len = 0, i; @@ -934,13 +902,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 ()); @@ -957,19 +923,10 @@ 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 (); - 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); @@ -980,29 +937,15 @@ test_write_intlist_ssid (void) g_assert_cmpint (intlist[i], ==, tmpssid[i]); g_free (intlist); - 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); - 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); + assert_reread_and_unlink (connection, TRUE, testfile); } 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 +969,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,30 +1000,24 @@ 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; + NMTST_UUID_INIT (uuid); + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingIPConfig *s_ip4; - char *uuid, *testfile = NULL; + gs_free char *testfile = NULL; GBytes *ssid; unsigned char tmpssid[] = { 49, 48, 49 }; - gboolean success; - NMConnection *reread; - GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; - GKeyFile *keyfile; + gs_free_error GError *error = NULL; + gs_unref_keyfile GKeyFile *keyfile = NULL; char *tmp; connection = nm_simple_connection_new (); - g_assert (connection); /* Connection setting */ @@ -1090,13 +1025,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 ()); @@ -1113,19 +1046,10 @@ 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 (); - 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); @@ -1133,39 +1057,22 @@ test_write_intlike_ssid (void) g_assert_cmpstr (tmp, ==, "101"); 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, &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); + assert_reread_and_unlink (connection, TRUE, testfile); } static void test_write_intlike_ssid_2 (void) { - NMConnection *connection; + NMTST_UUID_INIT (uuid); + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWireless *s_wifi; NMSettingIPConfig *s_ip4; - char *uuid, *testfile = NULL; + 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; - pid_t owner_grp; - uid_t owner_uid; - GKeyFile *keyfile; + gs_free_error GError *error = NULL; + gs_unref_keyfile GKeyFile *keyfile = NULL; char *tmp; connection = nm_simple_connection_new (); @@ -1177,13 +1084,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 ()); @@ -1200,19 +1105,10 @@ 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 (); - 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); @@ -1220,32 +1116,18 @@ test_write_intlike_ssid_2 (void) g_assert_cmpstr (tmp, ==, "11\\;12\\;13\\;"); 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, &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); + assert_reread_and_unlink (connection, TRUE, testfile); } 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,26 +1163,18 @@ 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; + 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"; - gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; guint64 timestamp = 0x12344433L; connection = nm_simple_connection_new (); @@ -1310,7 +1184,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, @@ -1318,7 +1191,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 */ @@ -1350,36 +1222,17 @@ 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); - 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); - 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); + write_test_connection_and_reread (connection, TRUE); } 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,24 +1269,16 @@ 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; + NMTST_UUID_INIT (uuid); + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingIPConfig *s_ip4; NMSettingGsm *s_gsm; - char *uuid; - gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; guint64 timestamp = 0x12344433L; connection = nm_simple_connection_new (); @@ -1443,7 +1288,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, @@ -1451,7 +1295,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 */ @@ -1479,36 +1322,16 @@ 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); - - /* 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); + write_test_connection_and_reread (connection, TRUE); } 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 +1384,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 +1439,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 +1490,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 +1542,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" @@ -1734,14 +1551,14 @@ 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; - GError *error = NULL; + gs_free_error GError *error = NULL; connection = nm_simple_connection_new (); g_assert (connection != NULL); @@ -1751,13 +1568,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 (); @@ -1815,26 +1630,19 @@ 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; - GKeyFile *keyfile; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; + gs_unref_keyfile GKeyFile *keyfile = NULL; gboolean relative = FALSE; 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); @@ -1851,14 +1659,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 @@ -1899,28 +1700,22 @@ test_write_wired_8021x_tls_connection_path (void) g_free (tmp2); g_free (tmp); - 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" @@ -1937,14 +1732,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); @@ -1985,7 +1773,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 +1783,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,29 +1820,20 @@ 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; + 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"; - gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; connection = nm_simple_connection_new (); - g_assert (connection); /* Connection setting */ @@ -2066,14 +1841,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 (); @@ -2098,36 +1871,17 @@ 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); - - /* 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)); - - unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); + write_test_connection_and_reread (connection, FALSE); } 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,25 +1915,17 @@ 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; + NMTST_UUID_INIT (uuid); + gs_unref_object NMConnection *connection = 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; - pid_t owner_grp; - uid_t owner_uid; connection = nm_simple_connection_new (); g_assert (connection); @@ -2189,7 +1935,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, @@ -2197,7 +1942,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 (); @@ -2222,38 +1966,19 @@ 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); - - /* 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)); - - unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); + write_test_connection_and_reread (connection, FALSE); } 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,25 +2011,17 @@ 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; + 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"; - gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; connection = nm_simple_connection_new (); g_assert (connection); @@ -2314,7 +2031,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, @@ -2323,7 +2039,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 ()); @@ -2345,36 +2060,17 @@ 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); - - /* 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)); - - unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); + write_test_connection_and_reread (connection, FALSE); } 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 +2088,18 @@ 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; + 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; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; - GKeyFile *kf; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; char *s; gint mtu; @@ -2421,13 +2111,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 (); @@ -2435,25 +2123,12 @@ 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); - /* 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); + 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); @@ -2465,24 +2140,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,28 +2179,22 @@ 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; + 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"; - gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; - GKeyFile *kf; + gs_free char *testfile = NULL; + gs_free_error GError *error = NULL; char *s; connection = nm_simple_connection_new (); @@ -2540,13 +2204,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 (); @@ -2567,26 +2229,12 @@ 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); - 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); - g_assert (reread); - 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 (); - 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); @@ -2604,19 +2252,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 +2274,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 +2298,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 +2317,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 +2354,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 +2406,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,24 +2422,16 @@ 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; + NMTST_UUID_INIT (uuid); + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSettingWired *s_wired; NMSettingIPConfig *s_ip6; - char *uuid; - gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; connection = nm_simple_connection_new (); @@ -2811,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 ()); @@ -2833,34 +2460,15 @@ 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); - 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); - g_assert (reread); - - nmtst_assert_connection_equals (reread, FALSE, connection, FALSE); - - unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); + write_test_connection_and_reread (connection, FALSE); } 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,23 +2483,15 @@ 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; + NMTST_UUID_INIT (uuid); + gs_unref_object NMConnection *connection = NULL; NMSettingConnection *s_con; NMSetting *s_gsm; - char *uuid; - gboolean success; - NMConnection *reread; - char *testfile = NULL; - GError *error = NULL; - pid_t owner_grp; - uid_t owner_uid; connection = nm_simple_connection_new (); @@ -2900,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 (); @@ -2920,26 +2518,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); - 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); - g_assert (reread); - - nmtst_assert_connection_equals (reread, FALSE, connection, FALSE); - - unlink (testfile); - g_free (testfile); - - g_object_unref (reread); - g_object_unref (connection); + write_test_connection_and_reread (connection, FALSE); } /*****************************************************************************/