[gnome-keyring/dbus-api] [secret] Add compat function for converting item types.



commit fccb81c434f5d7fbd37bcd732033aed45dd7ac3a
Author: Stefan Walter <Stefan Walter>
Date:   Mon Aug 3 15:00:41 2009 +0000

    [secret] Add compat function for converting item types.
    
    The old concept of 'item type' is stored as an additional attribute
    gkr:item-type

 pkcs11/secret-store/gck-secret-binary.c            |   10 +---
 pkcs11/secret-store/gck-secret-compat.c            |   46 +++++++++++++++++
 pkcs11/secret-store/gck-secret-compat.h            |    8 ++-
 pkcs11/secret-store/gck-secret-textual.c           |   25 ++++++---
 .../secret-store/tests/unit-test-secret-compat.c   |   52 ++++++++++++++++++++
 5 files changed, 123 insertions(+), 18 deletions(-)
---
diff --git a/pkcs11/secret-store/gck-secret-binary.c b/pkcs11/secret-store/gck-secret-binary.c
index 7591e29..ac5afd8 100644
--- a/pkcs11/secret-store/gck-secret-binary.c
+++ b/pkcs11/secret-store/gck-secret-binary.c
@@ -543,8 +543,7 @@ generate_hashed_items (GckSecretCollection *collection, EggBuffer *buffer)
 		
 		attributes = gck_secret_item_get_fields (l->data);
 		value = g_hash_table_lookup (attributes, "gkr:item-type");
-		if (!value || !convert_to_integer (value, &type))
-			type = 0;
+		type = gck_secret_compat_parse_item_type (value);
 		egg_buffer_add_uint32 (buffer, type);
 		
 		buffer_add_attributes (buffer, attributes, TRUE);
@@ -721,14 +720,13 @@ setup_item_from_info (GckSecretItem *item, gboolean locked, ItemInfo *info)
 {
 	GckSecretObject *obj = GCK_SECRET_OBJECT (item);
 	GckLogin *secret;
-	gchar *type;
+	const gchar *type;
 	
 	gck_secret_object_set_label (obj, info->display_name);
 	gck_secret_object_set_created (obj, info->ctime);
 	gck_secret_object_set_modified (obj, info->mtime);
 	
-	type = g_strdup_printf ("%u", info->type);
-
+	type = gck_secret_compat_format_item_type (info->type);
 	gck_secret_fields_add (info->attributes, "gkr:item-type", type);
 	gck_secret_item_set_fields (item, info->attributes);
 
@@ -743,8 +741,6 @@ setup_item_from_info (GckSecretItem *item, gboolean locked, ItemInfo *info)
 		g_object_set_data_full (G_OBJECT (item), "compat-acl", info->acl, gck_secret_compat_acl_free);
 		info->acl = NULL;
 	}
-
-	g_free (type);
 }
 
 static void
diff --git a/pkcs11/secret-store/gck-secret-compat.c b/pkcs11/secret-store/gck-secret-compat.c
index 1b67acb..10c4851 100644
--- a/pkcs11/secret-store/gck-secret-compat.c
+++ b/pkcs11/secret-store/gck-secret-compat.c
@@ -23,6 +23,8 @@
 
 #include "gck-secret-compat.h"
 
+#include <string.h>
+
 void
 gck_secret_compat_access_free (gpointer data)
 {
@@ -43,3 +45,47 @@ gck_secret_compat_acl_free (gpointer acl)
 	g_list_free (acl);
 }
 
+guint
+gck_secret_compat_parse_item_type (const gchar *value)
+{
+	if (value == NULL)
+		return 0; /* The default */
+	if (strcmp (value, "generic-secret") == 0)
+		return 0; /* GNOME_KEYRING_ITEM_GENERIC_SECRET */
+	if (strcmp (value, "network-password") == 0)
+		return 1; /* GNOME_KEYRING_ITEM_NETWORK_PASSWORD */
+	if (strcmp (value, "note") == 0)
+		return 2; /* GNOME_KEYRING_ITEM_NOTE */
+	if (strcmp (value, "chained-keyring-password") == 0)
+		return 3; /* GNOME_KEYRING_ITEM_CHAINED_KEYRING_PASSWORD */
+	if (strcmp (value, "encryption-key-password") == 0)
+		return 4; /* GNOME_KEYRING_ITEM_ENCRYPTION_KEY_PASSWORD */
+	if (strcmp (value, "pk-storage") == 0)
+		return 0x100; /* GNOME_KEYRING_ITEM_PK_STORAGE */
+
+	/* The default: GNOME_KEYRING_ITEM_GENERIC_SECRET */
+	return 0;
+}
+
+const gchar*
+gck_secret_compat_format_item_type (guint value)
+{
+	/* Only GNOME_KEYRING_ITEM_TYPE_MASK */
+	switch (value & 0x0000ffff)
+	{
+	case 0: /* GNOME_KEYRING_ITEM_GENERIC_SECRET */
+		return "generic-secret";
+	case 1: /* GNOME_KEYRING_ITEM_NETWORK_PASSWORD */
+		return "network-password";
+	case 2: /* GNOME_KEYRING_ITEM_NOTE */
+		return "note";
+	case 3: /* GNOME_KEYRING_ITEM_CHAINED_KEYRING_PASSWORD */
+		return "chained-keyring-password";
+	case 4: /* GNOME_KEYRING_ITEM_ENCRYPTION_KEY_PASSWORD */
+		return "encryption-key-password";
+	case 0x100: /* GNOME_KEYRING_ITEM_PK_STORAGE */
+		return "pk-storage";
+	default:
+		return NULL;
+	};
+}
diff --git a/pkcs11/secret-store/gck-secret-compat.h b/pkcs11/secret-store/gck-secret-compat.h
index bc6b724..f5a287d 100644
--- a/pkcs11/secret-store/gck-secret-compat.h
+++ b/pkcs11/secret-store/gck-secret-compat.h
@@ -36,8 +36,12 @@ typedef struct _GckSecretAccess {
 	GckSecretAccessType types_allowed;
 } GckSecretAccess;
 
-void     gck_secret_compat_access_free (gpointer ac);
+void           gck_secret_compat_access_free         (gpointer ac);
 
-void     gck_secret_compat_acl_free    (gpointer acl);
+void           gck_secret_compat_acl_free            (gpointer acl);
+
+guint          gck_secret_compat_parse_item_type     (const gchar *value);
+
+const gchar*   gck_secret_compat_format_item_type    (guint value);
 
 #endif /* __GCK_SECRET_COMPAT_H__ */
diff --git a/pkcs11/secret-store/gck-secret-textual.c b/pkcs11/secret-store/gck-secret-textual.c
index 999fb27..fc3bfb8 100644
--- a/pkcs11/secret-store/gck-secret-textual.c
+++ b/pkcs11/secret-store/gck-secret-textual.c
@@ -317,12 +317,12 @@ generate_item (GKeyFile *file, GckSecretItem *item)
 	
 	/* 
 	 * COMPATIBILITY: We no longer have the concept of an item type.
-	 * The compat-item-type field serves that purpose.
+	 * The gkr:item-type field serves that purpose.
 	 */
 	
-	value = g_hash_table_lookup (attributes, "compat-item-type");
-	if (value != NULL)
-		g_key_file_set_string (file, groupname, "item-type", value);
+	value = g_hash_table_lookup (attributes, "gkr:item-type");
+	g_key_file_set_integer (file, groupname, "item-type",
+	                        gck_secret_compat_parse_item_type (value));
 
 	value = gck_secret_object_get_label (obj);
 	if (value != NULL)
@@ -349,9 +349,11 @@ parse_item (GKeyFile *file, GckSecretItem *item, const gchar **groups)
 	GckSecretObject *obj;
 	GHashTable *attributes;
 	const gchar *groupname;
+	GError *err = NULL;
 	GckLogin *secret;
 	gchar *val;
 	guint64 num;
+	gint type;
 	
 	/* First the main item data */
 	
@@ -361,12 +363,17 @@ parse_item (GKeyFile *file, GckSecretItem *item, const gchar **groups)
 	
 	/* 
 	 * COMPATIBILITY: We no longer have the concept of an item type.
-	 * The compat-item-type field serves that purpose.
+	 * The gkr:item-type field serves that purpose.
 	 */
-	
-	val = g_key_file_get_string (file, groupname, "item-type", NULL);
-	if (val != NULL)
-		g_hash_table_replace (attributes, g_strdup ("compat-item-type"), val);
+
+	type = g_key_file_get_integer (file, groupname, "item-type", &err);
+	if (err) {
+		g_clear_error (&err);
+		type = 0;
+	}
+
+	gck_secret_fields_add (attributes, "gkr:item-type",
+	                       gck_secret_compat_format_item_type (type));
 
 	val = g_key_file_get_string (file, groupname, "display-name", NULL);
 	gck_secret_object_set_label (obj, val);
diff --git a/pkcs11/secret-store/tests/unit-test-secret-compat.c b/pkcs11/secret-store/tests/unit-test-secret-compat.c
index 579753c..358888d 100644
--- a/pkcs11/secret-store/tests/unit-test-secret-compat.c
+++ b/pkcs11/secret-store/tests/unit-test-secret-compat.c
@@ -61,3 +61,55 @@ DEFINE_TEST(acl_free)
 	
 	gck_secret_compat_acl_free (acl);
 }
+
+DEFINE_TEST(parse_item_type)
+{
+	guint type;
+
+	type = gck_secret_compat_parse_item_type ("generic-secret");
+	g_assert_cmpuint (type, ==, 0);
+	type = gck_secret_compat_parse_item_type ("network-password");
+	g_assert_cmpuint (type, ==, 1);
+	type = gck_secret_compat_parse_item_type ("note");
+	g_assert_cmpuint (type, ==, 2);
+	type = gck_secret_compat_parse_item_type ("chained-keyring-password");
+	g_assert_cmpuint (type, ==, 3);
+	type = gck_secret_compat_parse_item_type ("encryption-key-password");
+	g_assert_cmpuint (type, ==, 4);
+	type = gck_secret_compat_parse_item_type ("pk-storage");
+	g_assert_cmpuint (type, ==, 0x100);
+
+	/* Invalid returns generic secret */
+	type = gck_secret_compat_parse_item_type ("invalid");
+	g_assert_cmpuint (type, ==, 0);
+
+	/* Null returns generic secret */
+	type = gck_secret_compat_parse_item_type (NULL);
+	g_assert_cmpuint (type, ==, 0);
+}
+
+DEFINE_TEST(format_item_type)
+{
+	const gchar *type;
+
+	type = gck_secret_compat_format_item_type (0);
+	g_assert_cmpstr (type, ==, "generic-secret");
+	type = gck_secret_compat_format_item_type (1);
+	g_assert_cmpstr (type, ==, "network-password");
+	type = gck_secret_compat_format_item_type (2);
+	g_assert_cmpstr (type, ==, "note");
+	type = gck_secret_compat_format_item_type (3);
+	g_assert_cmpstr (type, ==, "chained-keyring-password");
+	type = gck_secret_compat_format_item_type (4);
+	g_assert_cmpstr (type, ==, "encryption-key-password");
+	type = gck_secret_compat_format_item_type (0x100);
+	g_assert_cmpstr (type, ==, "pk-storage");
+
+	/* Higher bits shouldn't make a difference */
+	type = gck_secret_compat_format_item_type (0xF0000001);
+	g_assert_cmpstr (type, ==, "network-password");
+
+	/* Unrecognized should be null */
+	type = gck_secret_compat_format_item_type (32);
+	g_assert (type == NULL);
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]