From 494c13a265619b6588b4011d5bf3251de9ac49e4 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Thu, 12 Jul 2012 13:02:14 +0200 Subject: [PATCH] Add secret_item_get_schema_name() function * And add tests for schemas on items --- library/secret-item.c | 50 ++++++++++++++++++++++++++++++++------- library/secret-item.h | 2 ++ library/tests/test-item.c | 44 +++++++++++++++++++++++++++++----- 3 files changed, 82 insertions(+), 14 deletions(-) diff --git a/library/secret-item.c b/library/secret-item.c index a9a871c..3180d26 100644 --- a/library/secret-item.c +++ b/library/secret-item.c @@ -1728,6 +1728,32 @@ secret_item_set_secret_sync (SecretItem *self, return ret; } +/** + * secret_item_get_schema_name: + * @self: an item + * + * Gets the name of the schema that this item was stored with. This is also + * available at the xdg:schema attribute. + * + * Returns: (transfer full): the schema name + */ +gchar * +secret_item_get_schema_name (SecretItem *self) +{ + gchar *schema_name; + GVariant *variant; + + g_return_val_if_fail (SECRET_IS_ITEM (self), NULL); + + variant = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (self), "Attributes"); + g_return_val_if_fail (variant != NULL, NULL); + + g_variant_lookup (variant, "xdg:schema", "s", &schema_name); + g_variant_unref (variant); + + return schema_name; +} + /** * secret_item_get_attributes: * @self: an item @@ -1787,15 +1813,19 @@ secret_item_set_attributes (SecretItem *self, GAsyncReadyCallback callback, gpointer user_data) { + const gchar *schema_name = NULL; + g_return_if_fail (SECRET_IS_ITEM (self)); g_return_if_fail (attributes != NULL); - /* Warnings raised already */ - if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE)) - return; + if (schema != NULL) { + if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE)) + return; /* Warnings raised already */ + schema_name = schema->name; + } _secret_util_set_property (G_DBUS_PROXY (self), "Attributes", - _secret_attributes_to_variant (attributes, NULL), + _secret_attributes_to_variant (attributes, schema_name), secret_item_set_attributes, cancellable, callback, user_data); } @@ -1848,15 +1878,19 @@ secret_item_set_attributes_sync (SecretItem *self, GCancellable *cancellable, GError **error) { + const gchar *schema_name = NULL; + g_return_val_if_fail (SECRET_IS_ITEM (self), FALSE); g_return_val_if_fail (attributes != NULL, FALSE); - /* Warnings raised already */ - if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE)) - return FALSE; + if (schema != NULL) { + if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE)) + return FALSE; /* Warnings raised already */ + schema_name = schema->name; + } return _secret_util_set_property_sync (G_DBUS_PROXY (self), "Attributes", - _secret_attributes_to_variant (attributes, NULL), + _secret_attributes_to_variant (attributes, schema_name), cancellable, error); } diff --git a/library/secret-item.h b/library/secret-item.h index ed2ea76..85863cb 100644 --- a/library/secret-item.h +++ b/library/secret-item.h @@ -141,6 +141,8 @@ gboolean secret_item_set_secret_sync (SecretItem *self, GCancellable *cancellable, GError **error); +gchar * secret_item_get_schema_name (SecretItem *self); + GHashTable* secret_item_get_attributes (SecretItem *self); void secret_item_set_attributes (SecretItem *self, diff --git a/library/tests/test-item.c b/library/tests/test-item.c index 6c844a2..c2ab9b2 100644 --- a/library/tests/test-item.c +++ b/library/tests/test-item.c @@ -30,6 +30,16 @@ #include #include +static const SecretSchema MOCK_SCHEMA = { + "org.mock.Schema.Item", + SECRET_SCHEMA_NONE, + { + { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER }, + { "string", SECRET_SCHEMA_ATTRIBUTE_STRING }, + { "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN }, + } +}; + typedef struct { SecretService *service; } Test; @@ -179,7 +189,7 @@ test_create_sync (Test *test, value = secret_value_new ("Hoohah", -1, "text/plain"); - item = secret_item_create_sync (collection, NULL, attributes, "Tunnel", + item = secret_item_create_sync (collection, &MOCK_SCHEMA, attributes, "Tunnel", value, FALSE, NULL, &error); g_assert_no_error (error); @@ -218,7 +228,7 @@ test_create_async (Test *test, value = secret_value_new ("Hoohah", -1, "text/plain"); - secret_item_create (collection, NULL, attributes, "Tunnel", + secret_item_create (collection, &MOCK_SCHEMA, attributes, "Tunnel", value, FALSE, NULL, on_async_result, &result); g_assert_no_error (error); @@ -407,6 +417,7 @@ test_set_attributes_sync (Test *test, SecretItem *item; gboolean ret; GHashTable *attributes; + gchar *schema_name; item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error); g_assert_no_error (error); @@ -418,10 +429,15 @@ test_set_attributes_sync (Test *test, g_assert_cmpuint (g_hash_table_size (attributes), ==, 4); g_hash_table_unref (attributes); + /* Has some other schema */ + schema_name = secret_item_get_schema_name (item); + g_assert_cmpstr (schema_name, !=, MOCK_SCHEMA.name); + g_free (schema_name); + attributes = g_hash_table_new (g_str_hash, g_str_equal); g_hash_table_insert (attributes, "string", "five"); g_hash_table_insert (attributes, "number", "5"); - ret = secret_item_set_attributes_sync (item, NULL, attributes, NULL, &error); + ret = secret_item_set_attributes_sync (item, &MOCK_SCHEMA, attributes, NULL, &error); g_hash_table_unref (attributes); g_assert_no_error (error); g_assert (ret == TRUE); @@ -429,9 +445,14 @@ test_set_attributes_sync (Test *test, attributes = secret_item_get_attributes (item); g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "five"); g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "5"); - g_assert_cmpuint (g_hash_table_size (attributes), ==, 2); + g_assert_cmpuint (g_hash_table_size (attributes), ==, 3); g_hash_table_unref (attributes); + /* Now has our schema */ + schema_name = secret_item_get_schema_name (item); + g_assert_cmpstr (schema_name, ==, MOCK_SCHEMA.name); + g_free (schema_name); + g_object_unref (item); } @@ -444,6 +465,7 @@ test_set_attributes_async (Test *test, GError *error = NULL; GAsyncResult *result = NULL; SecretItem *item; + gchar *schema_name; gboolean ret; item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error); @@ -456,10 +478,15 @@ test_set_attributes_async (Test *test, g_assert_cmpuint (g_hash_table_size (attributes), ==, 4); g_hash_table_unref (attributes); + /* Has some other schema */ + schema_name = secret_item_get_schema_name (item); + g_assert_cmpstr (schema_name, !=, MOCK_SCHEMA.name); + g_free (schema_name); + attributes = g_hash_table_new (g_str_hash, g_str_equal); g_hash_table_insert (attributes, "string", "five"); g_hash_table_insert (attributes, "number", "5"); - secret_item_set_attributes (item, NULL, attributes, NULL, on_async_result, &result); + secret_item_set_attributes (item, &MOCK_SCHEMA, attributes, NULL, on_async_result, &result); g_assert (result == NULL); egg_test_wait (); @@ -472,9 +499,14 @@ test_set_attributes_async (Test *test, attributes = secret_item_get_attributes (item); g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "five"); g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "5"); - g_assert_cmpuint (g_hash_table_size (attributes), ==, 2); + g_assert_cmpuint (g_hash_table_size (attributes), ==, 3); g_hash_table_unref (attributes); + /* Now has our schema */ + schema_name = secret_item_get_schema_name (item); + g_assert_cmpstr (schema_name, ==, MOCK_SCHEMA.name); + g_free (schema_name); + g_object_unref (item); }