summaryrefslogtreecommitdiff
path: root/pkcs11/secret-store/test-secret-textual.c
diff options
context:
space:
mode:
Diffstat (limited to 'pkcs11/secret-store/test-secret-textual.c')
-rw-r--r--pkcs11/secret-store/test-secret-textual.c229
1 files changed, 229 insertions, 0 deletions
diff --git a/pkcs11/secret-store/test-secret-textual.c b/pkcs11/secret-store/test-secret-textual.c
new file mode 100644
index 00000000..0752712c
--- /dev/null
+++ b/pkcs11/secret-store/test-secret-textual.c
@@ -0,0 +1,229 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/*
+ Copyright (C) 2009 Stefan Walter
+
+ The Gnome Keyring Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The Gnome Keyring Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the Gnome Library; see the file COPYING.LIB. If not,
+ <http://www.gnu.org/licenses/>.
+
+ Author: Stef Walter <stef@memberwebs.com>
+*/
+
+#include "config.h"
+
+#include "mock-secret-module.h"
+
+#include "secret-store/gkm-secret-collection.h"
+#include "secret-store/gkm-secret-data.h"
+#include "secret-store/gkm-secret-fields.h"
+#include "secret-store/gkm-secret-item.h"
+#include "secret-store/gkm-secret-textual.h"
+
+#include "gkm/gkm-secret.h"
+
+#include "pkcs11/pkcs11i.h"
+
+#include <glib.h>
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+typedef struct {
+ GkmModule *module;
+ GkmSession *session;
+ GkmSecretCollection *collection;
+ GkmSecretData *sdata;
+} Test;
+
+static void
+setup (Test *test, gconstpointer unused)
+{
+ test->module = test_secret_module_initialize_and_enter ();
+ test->session = test_secret_module_open_session (TRUE);
+
+ test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION,
+ "module", test->module,
+ "identifier", "test",
+ "label", "brigadooooooooooooon",
+ NULL);
+
+ test->sdata = g_object_new (GKM_TYPE_SECRET_DATA, NULL);
+
+ g_assert (GKM_IS_SECRET_COLLECTION (test->collection));
+
+}
+
+static void
+teardown (Test *test, gconstpointer unused)
+{
+ if (test->collection)
+ g_object_unref (test->collection);
+ if (test->sdata)
+ g_object_unref (test->sdata);
+ test_secret_module_leave_and_finalize ();
+}
+
+static void
+test_read (Test *test, gconstpointer unused)
+{
+ GkmDataResult res;
+ gchar *data;
+ gsize n_data;
+
+ if (!g_file_get_contents (SRCDIR "/fixtures/plain.keyring", &data, &n_data, NULL))
+ g_assert_not_reached ();
+ res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
+ g_free (data);
+
+ test_secret_collection_validate (test->collection, test->sdata);
+
+ g_assert (res == GKM_DATA_SUCCESS);
+}
+
+static void
+test_read_wrong_format (Test *test, gconstpointer unused)
+{
+ GkmDataResult res;
+ gchar *data;
+ gsize n_data;
+
+ if (!g_file_get_contents (SRCDIR "/fixtures/encrypted.keyring", &data, &n_data, NULL))
+ g_assert_not_reached ();
+
+ res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
+ g_free (data);
+
+ g_assert (res == GKM_DATA_UNRECOGNIZED);
+}
+
+static void
+test_read_bad_number (Test *test, gconstpointer unused)
+{
+ GkmSecretItem *item;
+ GkmDataResult res;
+ const gchar *value;
+ gchar *data;
+ gsize n_data;
+
+ if (!g_file_get_contents (SRCDIR "/fixtures/plain-bad-number.keyring", &data, &n_data, NULL))
+ g_assert_not_reached ();
+ res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
+ g_free (data);
+
+ g_assert (res == GKM_DATA_SUCCESS);
+
+ item = gkm_secret_collection_get_item (test->collection, "1");
+ g_assert (GKM_IS_SECRET_ITEM (item));
+ value = gkm_secret_fields_get (gkm_secret_item_get_fields (item), "bad-number");
+ g_assert (value == NULL);
+ value = gkm_secret_fields_get (gkm_secret_item_get_fields (item), "missing-number");
+ g_assert (value == NULL);
+}
+
+static void
+test_write (Test *test, gconstpointer unused)
+{
+ GkmDataResult res;
+ gpointer data;
+ gsize n_data;
+
+ test_secret_collection_populate (test->collection, test->sdata);
+
+ res = gkm_secret_textual_write (test->collection, test->sdata, &data, &n_data);
+ g_assert (res == GKM_DATA_SUCCESS);
+ g_assert (data);
+ g_assert (n_data);
+
+ /* Try parsing it again */
+ res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
+ g_assert (res == GKM_DATA_SUCCESS);
+}
+
+static void
+test_remove_unavailable (Test *test, gconstpointer unused)
+{
+ GkmDataResult res;
+ GList *items;
+ gchar *data;
+ gsize n_data;
+
+ if (!g_file_get_contents (SRCDIR "/fixtures/plain.keyring", &data, &n_data, NULL))
+ g_assert_not_reached ();
+ res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
+ g_assert (res == GKM_DATA_SUCCESS);
+
+ /* Two items from the file */
+ items = gkm_secret_collection_get_items (test->collection);
+ g_assert_cmpint (g_list_length (items), ==, 2);
+ g_list_free (items);
+
+ /* Fill in some more data */
+ test_secret_collection_populate (test->collection, test->sdata);
+
+ /* Should have added three more */
+ items = gkm_secret_collection_get_items (test->collection);
+ g_assert_cmpint (g_list_length (items), ==, 5);
+ g_list_free (items);
+
+ /* Re-read the keyring */
+ res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
+ g_assert (res == GKM_DATA_SUCCESS);
+
+ /* And we're back to two */
+ items = gkm_secret_collection_get_items (test->collection);
+ g_assert_cmpint (g_list_length (items), ==, 2);
+ g_list_free (items);
+
+ g_free (data);
+}
+
+static void
+test_read_with_schema (Test *test,
+ gconstpointer unused)
+{
+ GkmDataResult res;
+ GkmSecretItem *item;
+ gchar *data;
+ gsize n_data;
+
+ if (!g_file_get_contents (SRCDIR "/fixtures/plain-with-schema.keyring", &data, &n_data, NULL))
+ g_assert_not_reached ();
+ res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
+ g_assert (res == GKM_DATA_SUCCESS);
+
+ item = gkm_secret_collection_get_item (test->collection, "1");
+ g_assert (item != NULL);
+
+ g_assert_cmpstr (gkm_secret_item_get_schema (item), ==, "se.lostca.is.rishi.secret");
+
+ g_free (data);
+}
+
+int
+main (int argc, char **argv)
+{
+#if !GLIB_CHECK_VERSION(2,35,0)
+ g_type_init ();
+#endif
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add ("/secret-store/search/read", Test, NULL, setup, test_read, teardown);
+ g_test_add ("/secret-store/search/read_wrong_format", Test, NULL, setup, test_read_wrong_format, teardown);
+ g_test_add ("/secret-store/search/read_bad_number", Test, NULL, setup, test_read_bad_number, teardown);
+ g_test_add ("/secret-store/search/write", Test, NULL, setup, test_write, teardown);
+ g_test_add ("/secret-store/search/remove_unavailable", Test, NULL, setup, test_remove_unavailable, teardown);
+ g_test_add ("/secret-store/search/read-with-schema", Test, NULL, setup, test_read_with_schema, teardown);
+
+ return g_test_run ();
+}