From ff608c24cd1ac409092f1a883452225a8be6513a Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Tue, 28 Oct 2014 12:49:56 -0400 Subject: [PATCH] libnm-core: don't serialize empty address-labels If no address in an NMSettingIP4Config has a label, then don't bother serializing an array of empty strings. --- libnm-core/nm-setting-ip4-config.c | 15 ++++++++++++++- libnm-core/tests/test-general.c | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/libnm-core/nm-setting-ip4-config.c b/libnm-core/nm-setting-ip4-config.c index 9eab49d7f5..6d897b56de 100644 --- a/libnm-core/nm-setting-ip4-config.c +++ b/libnm-core/nm-setting-ip4-config.c @@ -304,16 +304,29 @@ ip4_address_labels_get (NMSetting *setting, const char *property) { NMSettingIPConfig *s_ip = NM_SETTING_IP_CONFIG (setting); + gboolean have_labels = FALSE; GPtrArray *labels; GVariant *ret; int num_addrs, i; - labels = g_ptr_array_new (); num_addrs = nm_setting_ip_config_get_num_addresses (s_ip); for (i = 0; i < num_addrs; i++) { NMIPAddress *addr = nm_setting_ip_config_get_address (s_ip, i); GVariant *label = nm_ip_address_get_attribute (addr, "label"); + if (label) { + have_labels = TRUE; + break; + } + } + if (!have_labels) + return NULL; + + labels = g_ptr_array_sized_new (num_addrs); + for (i = 0; i < num_addrs; i++) { + NMIPAddress *addr = nm_setting_ip_config_get_address (s_ip, i); + GVariant *label = nm_ip_address_get_attribute (addr, "label"); + g_ptr_array_add (labels, (char *) (label ? g_variant_get_string (label, NULL) : "")); } diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 1bab4e011c..09e05856c8 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -350,6 +350,25 @@ test_setting_ip4_config_labels (void) label = nm_ip_address_get_attribute (addr, "label"); g_assert (label == NULL); + /* The 'address-labels' property should be omitted from the serialization if + * there are no non-NULL labels. + */ + conn = nmtst_create_minimal_connection ("label test", NULL, NM_SETTING_WIRED_SETTING_NAME, NULL); + nm_connection_add_setting (conn, nm_setting_duplicate (NM_SETTING (s_ip4))); + dict = nm_connection_to_dbus (conn, NM_CONNECTION_SERIALIZE_ALL); + g_object_unref (conn); + + setting_dict = g_variant_lookup_value (dict, NM_SETTING_IP4_CONFIG_SETTING_NAME, NM_VARIANT_TYPE_SETTING); + g_assert (setting_dict != NULL); + + value = g_variant_lookup_value (setting_dict, "address-labels", NULL); + g_assert (value == NULL); + + g_variant_unref (setting_dict); + g_variant_unref (dict); + + /* Now back to constructing the original s_ip4... */ + /* addr 2 */ addr = nm_ip_address_new (AF_INET, "2.2.2.2", 24, &error); g_assert_no_error (error);