keyfile: ensure all-default VLAN setting is read correctly

Settings with all-default values are not written to reduce
complexity of the keyfile (and be more human-readable friendly)
and that includes VLAN settings with a VLAN ID of zero.  So
when reading this file back, if there is no 'base type' setting
(eg, the setting specified by the connection::type property)
then just add that setting.  nm_connection_verify() will catch
cases where an empty 'base type' setting is invalid.
This commit is contained in:
Dan Williams 2013-04-11 11:32:07 -05:00
parent f1d31e36da
commit 9f8b7ff51d
4 changed files with 49 additions and 5 deletions

View file

@ -1195,15 +1195,23 @@ nm_keyfile_plugin_connection_from_file (const char *filename, GError **error)
/* Make sure that we have the base device type setting even if
* the keyfile didn't include it, which can happen when the base
* device type setting is all default values (like ethernet).
* device type setting is all default values (like ethernet where
* the MAC address isn't given, or VLAN when the VLAN ID is zero).
*/
s_con = nm_connection_get_setting_connection (connection);
if (s_con) {
ctype = nm_setting_connection_get_connection_type (s_con);
setting = nm_connection_get_setting_by_name (connection, ctype);
if (ctype) {
if (!setting && !strcmp (ctype, NM_SETTING_WIRED_SETTING_NAME))
nm_connection_add_setting (connection, nm_setting_wired_new ());
if (ctype && !setting) {
NMSetting *base_setting;
GType base_setting_type;
base_setting_type = nm_connection_lookup_setting_type (ctype);
if (base_setting_type != G_TYPE_INVALID) {
base_setting = (NMSetting *) g_object_new (base_setting_type, NULL);
g_assert (base_setting);
nm_connection_add_setting (connection, base_setting);
}
}
}

View file

@ -18,7 +18,8 @@ KEYFILES = \
Test_Bridge_Main \
Test_Bridge_Component \
Test_New_Wired_Group_Name \
Test_New_Wireless_Group_Names
Test_New_Wireless_Group_Names \
Test_Missing_Vlan_Setting
CERTS = \
test-ca-cert.pem \

View file

@ -0,0 +1,11 @@
# Settings with all default values are not written, including
# VLAN settings with a VLAN ID of 0, which is the default value.
[connection]
id=Test Missing Vlan Setting
uuid=4e80a56d-c99f-4aad-a6dd-b449bc398c57
type=vlan
autoconnect=true
[802-3-ethernet]
mac-address=00:11:22:33:44:55

View file

@ -3247,6 +3247,28 @@ test_write_new_wireless_group_names (void)
g_object_unref (connection);
}
static void
test_read_missing_vlan_setting (void)
{
NMConnection *connection;
NMSettingVlan *s_vlan;
GError *error = NULL;
gboolean success;
connection = nm_keyfile_plugin_connection_from_file (TEST_KEYFILES_DIR"/Test_Missing_Vlan_Setting", &error);
g_assert_no_error (error);
g_assert (connection);
success = nm_connection_verify (connection, &error);
g_assert_no_error (error);
g_assert (success);
/* Ensure the VLAN setting exists */
s_vlan = nm_connection_get_setting_vlan (connection);
g_assert (s_vlan);
g_assert_cmpint (nm_setting_vlan_get_id (s_vlan), ==, 0);
g_object_unref (connection);
}
int main (int argc, char **argv)
{
@ -3309,6 +3331,8 @@ int main (int argc, char **argv)
test_read_new_wireless_group_names ();
test_write_new_wireless_group_names ();
test_read_missing_vlan_setting ();
base = g_path_get_basename (argv[0]);
fprintf (stdout, "%s: SUCCESS\n", base);
g_free (base);