libnm: add a type argument to nm_utils_uuid_generate_from_string()

There are different types (variants) of UUIDs defined.
Especially variants 3 and 5 are name based variants (rfc4122).

The way we create our UUIDs in nm_utils_uuid_generate_from_string()
however does not create them according to RFC and does not set
the flags to indicate the variant.

Modify the signature of nm_utils_uuid_generate_from_string() to accept
a "uuid_type" argument, so that we later can add other algorithms without
breaking API.
This commit is contained in:
Thomas Haller 2014-11-27 18:40:18 +01:00
parent 21eb6b5d0d
commit 1e313e000d
8 changed files with 30 additions and 19 deletions

View file

@ -1955,6 +1955,8 @@ nm_utils_uuid_generate (void)
* @slen: if negative, treat @s as zero terminated C string.
* Otherwise, assume the length as given (and allow @s to be
* non-null terminated or contain '\0').
* @uuid_type: a type identifier which UUID format to generate.
* @type_args: additional arguments, depending on the uuid_type
*
* For a given @s, this function will always return the same UUID.
*
@ -1962,7 +1964,7 @@ nm_utils_uuid_generate (void)
* object's #NMSettingConnection:id: property
**/
char *
nm_utils_uuid_generate_from_string (const char *s, gssize slen)
nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_type, gpointer type_args)
{
uuid_t uuid;
char *buf = NULL;
@ -1970,6 +1972,10 @@ nm_utils_uuid_generate_from_string (const char *s, gssize slen)
g_return_val_if_fail (s && *s, NULL);
g_return_val_if_fail (slen < 0 || slen > 0, FALSE);
/* for now, only support legacy type */
g_return_val_if_fail (uuid_type == NM_UTILS_UUID_TYPE_LEGACY, NULL);
g_return_val_if_fail (!type_args, NULL);
if (slen < 0)
slen = strlen (s);

View file

@ -120,8 +120,10 @@ GVariant *nm_utils_ip_routes_to_variant (GPtrArray *routes);
GPtrArray *nm_utils_ip_routes_from_variant (GVariant *value,
int family);
#define NM_UTILS_UUID_TYPE_LEGACY 0
char *nm_utils_uuid_generate (void);
char *nm_utils_uuid_generate_from_string (const char *s, gssize slen);
char *nm_utils_uuid_generate_from_string (const char *s, gssize slen, int uuid_type, gpointer type_args);
gboolean nm_utils_file_is_certificate (const char *filename);
gboolean nm_utils_file_is_private_key (const char *filename, gboolean *out_encrypted);

View file

@ -3805,21 +3805,24 @@ test_hexstr2bin (void)
/******************************************************************************/
static void
_test_uuid (const char *expected_uuid, const char *str)
_test_uuid (int uuid_type, const char *expected_uuid, const char *str, gssize slen, gpointer type_args)
{
gs_free char *uuid_test = NULL;
g_assert (str);
uuid_test = nm_utils_uuid_generate_from_string (str, -1);
uuid_test = nm_utils_uuid_generate_from_string (str, slen, uuid_type, type_args);
g_assert (uuid_test);
g_assert (nm_utils_is_uuid (uuid_test));
if (strcmp (uuid_test, expected_uuid)) {
g_error ("UUID test failed: text=%s, uuid=%s, expected=%s",
str, uuid_test, expected_uuid);
g_error ("UUID test failed: type=%d; text=%s, len=%lld, uuid=%s, expected=%s", uuid_type,
str, (long long) slen, uuid_test, expected_uuid);
}
if (slen < 0)
_test_uuid (uuid_type, expected_uuid, str, strlen (str), type_args);
}
static void
@ -3827,17 +3830,17 @@ test_nm_utils_uuid_generate_from_string (void)
{
gs_free char *uuid_test = NULL;
_test_uuid ("0cc175b9-c0f1-b6a8-31c3-99e269772661", "a");
_test_uuid ("098f6bcd-4621-d373-cade-4e832627b4f6", "test");
_test_uuid ("59c0547b-7fe2-1c15-2cce-e328e8bf6742", "/etc/NetworkManager/system-connections/em1");
_test_uuid (NM_UTILS_UUID_TYPE_LEGACY, "0cc175b9-c0f1-b6a8-31c3-99e269772661", "a", -1, NULL);
_test_uuid (NM_UTILS_UUID_TYPE_LEGACY, "098f6bcd-4621-d373-cade-4e832627b4f6", "test", -1, NULL);
_test_uuid (NM_UTILS_UUID_TYPE_LEGACY, "59c0547b-7fe2-1c15-2cce-e328e8bf6742", "/etc/NetworkManager/system-connections/em1", -1, NULL);
g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*char *nm_utils_uuid_generate_from_string(const char *): *s && *s*");
uuid_test = nm_utils_uuid_generate_from_string ("", -1);
g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*char *nm_utils_uuid_generate_from_string(const char *, gssize, int, gpointer): *s && *s*");
uuid_test = nm_utils_uuid_generate_from_string ("", 0, NM_UTILS_UUID_TYPE_LEGACY, NULL);
g_assert (uuid_test == NULL);
g_test_assert_expected_messages ();
g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*char *nm_utils_uuid_generate_from_string(const char *): *s && *s*");
uuid_test = nm_utils_uuid_generate_from_string (NULL, -1);
g_test_expect_message ("libnm", G_LOG_LEVEL_CRITICAL, "*char *nm_utils_uuid_generate_from_string(const char *, gssize, int, gpointer): *s && *s*");
uuid_test = nm_utils_uuid_generate_from_string (NULL, 0, NM_UTILS_UUID_TYPE_LEGACY, NULL);
g_assert (uuid_test == NULL);
g_test_assert_expected_messages ();
}

View file

@ -408,7 +408,7 @@ connection_setting_add (const GPtrArray *block,
s_vlanid ? s_vlanid : "0",
s_hwaddr,
s_ip4addr ? s_ip4addr : "DHCP");
uuid = nm_utils_uuid_generate_from_string (uuid_data, -1);
uuid = nm_utils_uuid_generate_from_string (uuid_data, -1, NM_UTILS_UUID_TYPE_LEGACY, NULL);
g_free (uuid_data);
s_con = nm_setting_connection_new ();

View file

@ -148,7 +148,7 @@ make_connection_setting (const char *file,
uuid = svGetValue (ifcfg, "UUID", FALSE);
if (!uuid || !strlen (uuid)) {
g_free (uuid);
uuid = nm_utils_uuid_generate_from_string (ifcfg->fileName, -1);
uuid = nm_utils_uuid_generate_from_string (ifcfg->fileName, -1, NM_UTILS_UUID_TYPE_LEGACY, NULL);
}
g_object_set (s_con,
@ -4588,7 +4588,7 @@ uuid_from_file (const char *filename)
uuid = svGetValue (ifcfg, "UUID", FALSE);
if (!uuid || !strlen (uuid)) {
g_free (uuid);
uuid = nm_utils_uuid_generate_from_string (ifcfg->fileName, -1);
uuid = nm_utils_uuid_generate_from_string (ifcfg->fileName, -1, NM_UTILS_UUID_TYPE_LEGACY, NULL);
}
svCloseFile (ifcfg);

View file

@ -1629,7 +1629,7 @@ ifnet_update_connection_from_config_block (const char *conn_name,
id = connection_id_from_ifnet_name (conn_name);
uuid = g_strdup (ifnet_get_data (conn_name, "uuid"));
if (!uuid)
uuid = nm_utils_uuid_generate_from_string (id, -1);
uuid = nm_utils_uuid_generate_from_string (id, -1, NM_UTILS_UUID_TYPE_LEGACY, NULL);
g_object_set (setting,
NM_SETTING_CONNECTION_TYPE, type,

View file

@ -686,7 +686,7 @@ ifupdown_update_connection_from_if_block (NMConnection *connection,
idstr = g_strconcat ("Ifupdown (", block->name, ")", NULL);
uuid_base = idstr;
uuid = nm_utils_uuid_generate_from_string (uuid_base, -1);
uuid = nm_utils_uuid_generate_from_string (uuid_base, -1, NM_UTILS_UUID_TYPE_LEGACY, NULL);
g_object_set (s_con,
NM_SETTING_CONNECTION_TYPE, type,
NM_SETTING_CONNECTION_INTERFACE_NAME, block->name,

View file

@ -1316,7 +1316,7 @@ nm_keyfile_plugin_connection_from_file (const char *filename, GError **error)
if (!nm_setting_connection_get_uuid (s_con)) {
char *hashed_uuid;
hashed_uuid = nm_utils_uuid_generate_from_string (filename, -1);
hashed_uuid = nm_utils_uuid_generate_from_string (filename, -1, NM_UTILS_UUID_TYPE_LEGACY, NULL);
g_object_set (s_con, NM_SETTING_CONNECTION_UUID, hashed_uuid, NULL);
g_free (hashed_uuid);
}