Add secret_item_get_schema_name() function
authorStef Walter <stefw@gnome.org>
Thu, 12 Jul 2012 11:02:14 +0000 (13:02 +0200)
committerStef Walter <stefw@gnome.org>
Thu, 12 Jul 2012 11:02:33 +0000 (13:02 +0200)
 * And add tests for schemas on items

library/secret-item.c
library/secret-item.h
library/tests/test-item.c

index a9a871cff4efbf20e172ea060221ce549b26f2d2..3180d260d543461ce099e9f5b8f80aa69a767f1f 100644 (file)
@@ -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 <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:
  * @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);
 }
 
index ed2ea762a62390b49d2c5626ac33760f27a50a14..85863cb5dd3d62b328a1a9986061aaea988e4ddc 100644 (file)
@@ -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,
index 6c844a2d47fb35cd888d3f2377994c461f747784..c2ab9b250ed847ff9e7f9ba2d24d84cdd1477bc2 100644 (file)
 #include <errno.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 {
        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);
 }