Add secret_item_get_schema_name() function

* And add tests for schemas on items
This commit is contained in:
Stef Walter 2012-07-12 13:02:14 +02:00
parent 3c3e05eb11
commit 494c13a265
3 changed files with 82 additions and 14 deletions

View File

@ -1728,6 +1728,32 @@ secret_item_set_secret_sync (SecretItem *self,
return ret; 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 <literal>xdg:schema</literal> 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: * secret_item_get_attributes:
* @self: an item * @self: an item
@ -1787,15 +1813,19 @@ secret_item_set_attributes (SecretItem *self,
GAsyncReadyCallback callback, GAsyncReadyCallback callback,
gpointer user_data) gpointer user_data)
{ {
const gchar *schema_name = NULL;
g_return_if_fail (SECRET_IS_ITEM (self)); g_return_if_fail (SECRET_IS_ITEM (self));
g_return_if_fail (attributes != NULL); g_return_if_fail (attributes != NULL);
/* Warnings raised already */ if (schema != NULL) {
if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE)) if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE))
return; return; /* Warnings raised already */
schema_name = schema->name;
}
_secret_util_set_property (G_DBUS_PROXY (self), "Attributes", _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, secret_item_set_attributes, cancellable,
callback, user_data); callback, user_data);
} }
@ -1848,15 +1878,19 @@ secret_item_set_attributes_sync (SecretItem *self,
GCancellable *cancellable, GCancellable *cancellable,
GError **error) GError **error)
{ {
const gchar *schema_name = NULL;
g_return_val_if_fail (SECRET_IS_ITEM (self), FALSE); g_return_val_if_fail (SECRET_IS_ITEM (self), FALSE);
g_return_val_if_fail (attributes != NULL, FALSE); g_return_val_if_fail (attributes != NULL, FALSE);
/* Warnings raised already */ if (schema != NULL) {
if (schema != NULL && !_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE)) if (!_secret_attributes_validate (schema, attributes, G_STRFUNC, FALSE))
return FALSE; return FALSE; /* Warnings raised already */
schema_name = schema->name;
}
return _secret_util_set_property_sync (G_DBUS_PROXY (self), "Attributes", 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); cancellable, error);
} }

View File

@ -141,6 +141,8 @@ gboolean secret_item_set_secret_sync (SecretItem *self,
GCancellable *cancellable, GCancellable *cancellable,
GError **error); GError **error);
gchar * secret_item_get_schema_name (SecretItem *self);
GHashTable* secret_item_get_attributes (SecretItem *self); GHashTable* secret_item_get_attributes (SecretItem *self);
void secret_item_set_attributes (SecretItem *self, void secret_item_set_attributes (SecretItem *self,

View File

@ -30,6 +30,16 @@
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
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 { typedef struct {
SecretService *service; SecretService *service;
} Test; } Test;
@ -179,7 +189,7 @@ test_create_sync (Test *test,
value = secret_value_new ("Hoohah", -1, "text/plain"); 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); value, FALSE, NULL, &error);
g_assert_no_error (error); g_assert_no_error (error);
@ -218,7 +228,7 @@ test_create_async (Test *test,
value = secret_value_new ("Hoohah", -1, "text/plain"); 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); value, FALSE, NULL, on_async_result, &result);
g_assert_no_error (error); g_assert_no_error (error);
@ -407,6 +417,7 @@ test_set_attributes_sync (Test *test,
SecretItem *item; SecretItem *item;
gboolean ret; gboolean ret;
GHashTable *attributes; GHashTable *attributes;
gchar *schema_name;
item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error); item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
g_assert_no_error (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_assert_cmpuint (g_hash_table_size (attributes), ==, 4);
g_hash_table_unref (attributes); 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); attributes = g_hash_table_new (g_str_hash, g_str_equal);
g_hash_table_insert (attributes, "string", "five"); g_hash_table_insert (attributes, "string", "five");
g_hash_table_insert (attributes, "number", "5"); 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_hash_table_unref (attributes);
g_assert_no_error (error); g_assert_no_error (error);
g_assert (ret == TRUE); g_assert (ret == TRUE);
@ -429,9 +445,14 @@ test_set_attributes_sync (Test *test,
attributes = secret_item_get_attributes (item); attributes = secret_item_get_attributes (item);
g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "five"); g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "five");
g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "5"); 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); 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); g_object_unref (item);
} }
@ -444,6 +465,7 @@ test_set_attributes_async (Test *test,
GError *error = NULL; GError *error = NULL;
GAsyncResult *result = NULL; GAsyncResult *result = NULL;
SecretItem *item; SecretItem *item;
gchar *schema_name;
gboolean ret; gboolean ret;
item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error); 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_assert_cmpuint (g_hash_table_size (attributes), ==, 4);
g_hash_table_unref (attributes); 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); attributes = g_hash_table_new (g_str_hash, g_str_equal);
g_hash_table_insert (attributes, "string", "five"); g_hash_table_insert (attributes, "string", "five");
g_hash_table_insert (attributes, "number", "5"); 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); g_assert (result == NULL);
egg_test_wait (); egg_test_wait ();
@ -472,9 +499,14 @@ test_set_attributes_async (Test *test,
attributes = secret_item_get_attributes (item); attributes = secret_item_get_attributes (item);
g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "five"); g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "five");
g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "5"); 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); 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); g_object_unref (item);
} }