summaryrefslogtreecommitdiff
path: root/pkcs11/gnome2-store/tests/test-import.c
blob: 15158e5c5c364b8c32e993d8520b10fca92efa2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* unit-test-file-store.c: Test file store functionality

   Copyright (C) 2008 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,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Stef Walter <stef@memberwebs.com>
*/

#include "config.h"

#include "egg/egg-testing.h"

#include "gkm/gkm-module.h"
#include "gkm/gkm-test.h"

#include "gnome2-store/gkm-gnome2-store.h"

#define GCR_API_SUBJECT_TO_CHANGE
#define GCK_API_SUBJECT_TO_CHANGE
#include <gck/gck.h>
#include <gcr/gcr-base.h>

#include <p11-kit/p11-kit.h>

#include <glib/gstdio.h>

#include <string.h>

typedef struct {
	CK_FUNCTION_LIST_PTR funcs;
	GList *importers;
	gchar *directory;
} Test;

static void
setup (Test *test,
       gconstpointer unused)
{
	CK_C_INITIALIZE_ARGS args;
	CK_SESSION_HANDLE session;
	GckModule *module;
	GList *modules;
	CK_RV rv;

	test->directory = egg_tests_create_scratch_directory (NULL, NULL);

	memset (&args, 0, sizeof (args));
	args.flags = CKF_OS_LOCKING_OK;
	args.pReserved = g_strdup_printf ("directory='%s'", test->directory);

	test->funcs = gkm_gnome2_store_get_functions ();
	rv = (test->funcs->C_Initialize) (&args);
	gkm_assert_cmprv (rv, ==, CKR_OK);

	/* And now need to log in */
	rv = (test->funcs->C_OpenSession) (GKM_SLOT_ID, CKF_SERIAL_SESSION | CKF_RW_SESSION,
	                                   NULL, NULL, &session);
	gkm_assert_cmprv (rv, ==, CKR_OK);

	/* The directory is empty, so we need to initialize */
	rv = (test->funcs->C_SetPIN) (session, NULL, 0, (CK_BYTE_PTR)"mypin", 5);
	gkm_assert_cmprv (rv, ==, CKR_OK);

	/* Login so the importer doesn't have to */
	rv = (test->funcs->C_Login) (session, CKU_USER, (CK_BYTE_PTR)"mypin", 5);
	gkm_assert_cmprv (rv, ==, CKR_OK);

	module = gck_module_new (test->funcs);
	modules = g_list_prepend (NULL, module);
	gcr_pkcs11_set_modules (modules);
	g_list_free (modules);
	g_object_unref (module);
}

static void
teardown (Test *test,
          gconstpointer unused)
{
	CK_RV rv;

	g_list_free_full (test->importers, g_object_unref);

	gcr_pkcs11_set_modules (NULL);

	rv = (test->funcs->C_Finalize) (NULL);
	gkm_assert_cmprv (rv, ==, CKR_OK);

	/* Cleanup the directory */
	egg_tests_remove_scratch_directory (test->directory);
	g_free (test->directory);
}

static void
on_parser_parsed (GcrParser *parser,
                  gpointer user_data)
{
	Test *test = user_data;
	GcrParsed *parsed;
	GList *importers;

	parsed = gcr_parser_get_parsed (parser);

	if (test->importers == NULL)
		importers = gcr_importer_create_for_parsed (parsed);
	else
		importers = gcr_importer_queue_and_filter_for_parsed (test->importers, parsed);

	g_list_free_full (test->importers, g_object_unref);
	test->importers = importers;
}

static void
test_pkcs12_import (Test *test,
                    gconstpointer unused)
{
	GcrParser *parser;
	GError *error;
	gchar *contents;
	gsize length;
	GList *l;

	g_file_get_contents (SRCDIR "/files/personal.p12", &contents, &length, &error);
	g_assert_no_error (error);

	/* Parse the pkcs12 file */
	parser = gcr_parser_new ();
	gcr_parser_add_password (parser, "booo");
	gcr_parser_format_enable (parser, GCR_FORMAT_DER_PKCS12);
	g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), test);
	gcr_parser_parse_data (parser, (const guchar *)contents, length, &error);
	g_assert_no_error (error);
	g_object_unref (parser);
	g_free (contents);

	/* Should have found importers */
	g_assert (test->importers != NULL);

	for (l = test->importers; l != NULL; l = g_list_next (l)) {
		gcr_importer_import (l->data, NULL, &error);
		g_assert_no_error (error);
	}
}

static void
null_log_handler (const gchar *log_domain,
                  GLogLevelFlags log_level,
                  const gchar *message,
                  gpointer user_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_set_prgname ("test-import");

	/* Suppress these messages in tests */
	g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE,
	                   null_log_handler, NULL);

	g_test_add ("/gnome2-store/import/pkcs12", Test, NULL,
	            setup, test_pkcs12_import, teardown);

	return g_test_run ();
}