keyfile: fix handling of enum/flags properties after fcfb4b40 (bgo #738585)

When some properties got converted to G_TYPE_ENUM and G_TYPE_FLAGS
the keyfile plugin was not updated to handle these types.

https://bugzilla.gnome.org/show_bug.cgi?id=738585
This commit is contained in:
Dan Williams 2014-10-15 10:39:02 -05:00
parent b69143b508
commit 8176af1152
8 changed files with 232 additions and 2 deletions

View file

@ -1214,6 +1214,27 @@ read_one_setting_value (NMSetting *setting,
nm_log_warn (LOGD_SETTINGS, "Unhandled setting property type (read): '%s/%s' : '%s'",
setting_name, key, G_VALUE_TYPE_NAME (value));
}
} else if (G_VALUE_HOLDS_FLAGS (value)) {
guint64 uint_val;
/* Flags are guint but GKeyFile has no uint reader, just uint64 */
uint_val = nm_keyfile_plugin_kf_get_uint64 (info->keyfile, setting_name, key, &err);
if (!err) {
if (uint_val <= G_MAXUINT)
g_object_set (setting, key, (guint) uint_val, NULL);
else {
nm_log_warn (LOGD_SETTINGS, "Too large FLAGS property (read): '%s/%s' : '%s'",
setting_name, key, G_VALUE_TYPE_NAME (value));
}
}
g_clear_error (&err);
} else if (G_VALUE_HOLDS_ENUM (value)) {
gint int_val;
int_val = nm_keyfile_plugin_kf_get_integer (info->keyfile, setting_name, key, &err);
if (!err)
g_object_set (setting, key, (gint) int_val, NULL);
g_clear_error (&err);
} else {
nm_log_warn (LOGD_SETTINGS, "Unhandled setting property type (read): '%s/%s' : '%s'",
setting_name, key, G_VALUE_TYPE_NAME (value));

View file

@ -28,7 +28,9 @@ KEYFILES = \
Test_minimal_slave_3 \
Test_minimal_slave_4 \
Test_Missing_Vlan_Setting \
Test_Missing_ID_UUID
Test_Missing_ID_UUID \
Test_Enum_Property \
Test_Flags_Property
CERTS = \
test-ca-cert.pem \

View file

@ -0,0 +1,8 @@
[connection]
id=Test Wired Connection IP6
uuid=4e80a56d-c99f-4aad-a6dd-b449bc398c57
type=802-3-ethernet
[ipv6]
method=auto
ip6-privacy=2

View file

@ -0,0 +1,11 @@
[connection]
id=Test Flags Property
uuid=05a5ec81-fa72-4b7c-9f85-4a0dfd36c84f
type=gsm
[gsm]
number=*99#
username=username
password-flags=5
apn=my.apn

View file

@ -3458,6 +3458,182 @@ test_read_minimal_slave ()
g_clear_object (&connection);
}
static void
test_read_enum_property (void)
{
NMConnection *connection;
NMSettingIP6Config *s_ip6;
GError *error = NULL;
gboolean success;
connection = nm_keyfile_plugin_connection_from_file (TEST_KEYFILES_DIR"/Test_Enum_Property", &error);
g_assert_no_error (error);
g_assert (connection);
success = nm_connection_verify (connection, &error);
g_assert_no_error (error);
g_assert (success);
/* IPv6 setting */
s_ip6 = nm_connection_get_setting_ip6_config (connection);
g_assert (s_ip6);
g_assert_cmpint (nm_setting_ip6_config_get_ip6_privacy (s_ip6), ==, NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR);
g_object_unref (connection);
}
static void
test_write_enum_property (void)
{
NMConnection *connection;
NMSettingConnection *s_con;
NMSettingWired *s_wired;
NMSettingIP6Config *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 ();
/* Connection setting */
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 ());
nm_connection_add_setting (connection, NM_SETTING (s_wired));
/* IP6 setting */
s_ip6 = NM_SETTING_IP6_CONFIG (nm_setting_ip6_config_new ());
nm_connection_add_setting (connection, NM_SETTING (s_ip6));
g_object_set (s_ip6,
NM_SETTING_IP6_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_AUTO,
NM_SETTING_IP6_CONFIG_IP6_PRIVACY, NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR,
NULL);
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);
}
static void
test_read_flags_property (void)
{
NMConnection *connection;
NMSettingGsm *s_gsm;
GError *error = NULL;
gboolean success;
connection = nm_keyfile_plugin_connection_from_file (TEST_KEYFILES_DIR"/Test_Flags_Property", &error);
g_assert_no_error (error);
g_assert (connection);
success = nm_connection_verify (connection, &error);
g_assert_no_error (error);
g_assert (success);
/* GSM setting */
s_gsm = nm_connection_get_setting_gsm (connection);
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;
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 ();
/* Connection setting */
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 ();
nm_connection_add_setting (connection, s_gsm);
g_object_set (s_gsm,
NM_SETTING_GSM_NUMBER, "#99*",
NM_SETTING_GSM_APN, "myapn",
NM_SETTING_GSM_USERNAME, "adfasdfasdf",
NM_SETTING_GSM_PASSWORD_FLAGS, NM_SETTING_SECRET_FLAG_NOT_SAVED | NM_SETTING_SECRET_FLAG_NOT_REQUIRED,
NULL);
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);
}
NMTST_DEFINE ();
int main (int argc, char **argv)
@ -3523,6 +3699,11 @@ int main (int argc, char **argv)
g_test_add_func ("/keyfile/test_read_minimal", test_read_minimal);
g_test_add_func ("/keyfile/test_read_minimal_slave", test_read_minimal_slave);
g_test_add_func ("/keyfile/test_read_enum_property ", test_read_enum_property);
g_test_add_func ("/keyfile/test_write_enum_property ", test_write_enum_property);
g_test_add_func ("/keyfile/test_read_flags_property ", test_read_flags_property);
g_test_add_func ("/keyfile/test_write_flags_property ", test_write_flags_property);
return g_test_run ();
}

View file

@ -229,6 +229,7 @@ nm_keyfile_plugin_kf_set_##stype (GKeyFile *kf, \
DEFINE_KF_WRAPPER(string, gchar*, const gchar*);
DEFINE_KF_WRAPPER(integer, gint, gint);
DEFINE_KF_WRAPPER(uint64, guint64, guint64);
DEFINE_KF_WRAPPER(boolean, gboolean, gboolean);
DEFINE_KF_WRAPPER(value, gchar*, const gchar*);

View file

@ -61,6 +61,7 @@ void nm_keyfile_plugin_kf_set_##stype (GKeyFile *kf, \
set_ctype value);
DEFINE_KF_WRAPPER_PROTO(string, gchar*, const gchar*)
DEFINE_KF_WRAPPER_PROTO(integer, gint, gint)
DEFINE_KF_WRAPPER_PROTO(uint64, guint64, guint64)
DEFINE_KF_WRAPPER_PROTO(boolean, gboolean, gboolean)
DEFINE_KF_WRAPPER_PROTO(value, gchar*, const gchar*)

View file

@ -863,7 +863,12 @@ write_setting_value (NMSetting *setting,
nm_log_warn (LOGD_SETTINGS, "Unhandled setting property type (write) '%s/%s' : '%s'",
setting_name, key, g_type_name (type));
}
} else {
} else if (G_VALUE_HOLDS_FLAGS (value)) {
/* Flags are guint but GKeyFile has no uint reader, just uint64 */
nm_keyfile_plugin_kf_set_uint64 (info->keyfile, setting_name, key, (guint64) g_value_get_flags (value));
} else if (G_VALUE_HOLDS_ENUM (value))
nm_keyfile_plugin_kf_set_integer (info->keyfile, setting_name, key, (gint) g_value_get_enum (value));
else {
nm_log_warn (LOGD_SETTINGS, "Unhandled setting property type (write) '%s/%s' : '%s'",
setting_name, key, g_type_name (type));
}