~ubuntu-branches/ubuntu/vivid/gnome-keyring/vivid-proposed

« back to all changes in this revision

Viewing changes to pkcs11/gnome2-store/test-import.c

  • Committer: Package Import Robot
  • Author(s): Dimitri John Ledkov
  • Date: 2014-12-06 18:47:09 UTC
  • mfrom: (116.2.6 sid)
  • Revision ID: package-import@ubuntu.com-20141206184709-r2sbrchgej06j31z
Tags: 3.14.0-1ubuntu1
* Merge from debian, remaining changes:
  - Build with dh-autoreconf for new libtool.  
  - Provide upstart user session jobs.
  * debian/gnome-keyring.ubiquity, debian/rules:
    - Apply capabilities at the end of the ubiquity process to make sure new
      installs have gnome-keyring-daemon with cap_ipc_lock+ep.
  * debian/control, debian/*.install, debian/rules:
    - change for multiarch support, install pkcs11 in its own binary

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/* unit-test-file-store.c: Test file store functionality
 
3
 
 
4
   Copyright (C) 2008 Stefan Walter
 
5
 
 
6
   The Gnome Keyring Library is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU Library General Public License as
 
8
   published by the Free Software Foundation; either version 2 of the
 
9
   License, or (at your option) any later version.
 
10
 
 
11
   The Gnome Keyring Library is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
   Library General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU Library General Public
 
17
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
 
18
   <http://www.gnu.org/licenses/>.
 
19
 
 
20
   Author: Stef Walter <stef@memberwebs.com>
 
21
*/
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include "egg/egg-testing.h"
 
26
 
 
27
#include "gkm/gkm-module.h"
 
28
#include "gkm/gkm-test.h"
 
29
 
 
30
#include "gnome2-store/gkm-gnome2-store.h"
 
31
 
 
32
#include <gck/gck.h>
 
33
#include <gcr/gcr-base.h>
 
34
 
 
35
#include <p11-kit/p11-kit.h>
 
36
 
 
37
#include <glib/gstdio.h>
 
38
 
 
39
#include <string.h>
 
40
 
 
41
typedef struct {
 
42
        CK_FUNCTION_LIST_PTR funcs;
 
43
        GList *importers;
 
44
        gchar *directory;
 
45
} Test;
 
46
 
 
47
static void
 
48
setup (Test *test,
 
49
       gconstpointer unused)
 
50
{
 
51
        CK_C_INITIALIZE_ARGS args;
 
52
        CK_SESSION_HANDLE session;
 
53
        GckModule *module;
 
54
        GList *modules;
 
55
        CK_RV rv;
 
56
 
 
57
        test->directory = egg_tests_create_scratch_directory (NULL, NULL);
 
58
 
 
59
        memset (&args, 0, sizeof (args));
 
60
        args.flags = CKF_OS_LOCKING_OK;
 
61
        args.pReserved = g_strdup_printf ("directory='%s'", test->directory);
 
62
 
 
63
        test->funcs = gkm_gnome2_store_get_functions ();
 
64
        rv = (test->funcs->C_Initialize) (&args);
 
65
        gkm_assert_cmprv (rv, ==, CKR_OK);
 
66
 
 
67
        /* And now need to log in */
 
68
        rv = (test->funcs->C_OpenSession) (GKM_SLOT_ID, CKF_SERIAL_SESSION | CKF_RW_SESSION,
 
69
                                           NULL, NULL, &session);
 
70
        gkm_assert_cmprv (rv, ==, CKR_OK);
 
71
 
 
72
        /* The directory is empty, so we need to initialize */
 
73
        rv = (test->funcs->C_SetPIN) (session, NULL, 0, (CK_BYTE_PTR)"mypin", 5);
 
74
        gkm_assert_cmprv (rv, ==, CKR_OK);
 
75
 
 
76
        /* Login so the importer doesn't have to */
 
77
        rv = (test->funcs->C_Login) (session, CKU_USER, (CK_BYTE_PTR)"mypin", 5);
 
78
        gkm_assert_cmprv (rv, ==, CKR_OK);
 
79
 
 
80
        module = gck_module_new (test->funcs);
 
81
        modules = g_list_prepend (NULL, module);
 
82
        gcr_pkcs11_set_modules (modules);
 
83
        g_list_free (modules);
 
84
        g_object_unref (module);
 
85
}
 
86
 
 
87
static void
 
88
teardown (Test *test,
 
89
          gconstpointer unused)
 
90
{
 
91
        CK_RV rv;
 
92
 
 
93
        g_list_free_full (test->importers, g_object_unref);
 
94
 
 
95
        gcr_pkcs11_set_modules (NULL);
 
96
 
 
97
        rv = (test->funcs->C_Finalize) (NULL);
 
98
        gkm_assert_cmprv (rv, ==, CKR_OK);
 
99
 
 
100
        /* Cleanup the directory */
 
101
        egg_tests_remove_scratch_directory (test->directory);
 
102
        g_free (test->directory);
 
103
}
 
104
 
 
105
static void
 
106
on_parser_parsed (GcrParser *parser,
 
107
                  gpointer user_data)
 
108
{
 
109
        Test *test = user_data;
 
110
        GcrParsed *parsed;
 
111
        GList *importers;
 
112
 
 
113
        parsed = gcr_parser_get_parsed (parser);
 
114
 
 
115
        if (test->importers == NULL)
 
116
                importers = gcr_importer_create_for_parsed (parsed);
 
117
        else
 
118
                importers = gcr_importer_queue_and_filter_for_parsed (test->importers, parsed);
 
119
 
 
120
        g_list_free_full (test->importers, g_object_unref);
 
121
        test->importers = importers;
 
122
}
 
123
 
 
124
static void
 
125
test_pkcs12_import (Test *test,
 
126
                    gconstpointer unused)
 
127
{
 
128
        GcrParser *parser;
 
129
        GError *error;
 
130
        gchar *contents;
 
131
        gsize length;
 
132
        GList *l;
 
133
 
 
134
        g_file_get_contents (SRCDIR "/pkcs11/gnome2-store/fixtures/personal.p12", &contents, &length, &error);
 
135
        g_assert_no_error (error);
 
136
 
 
137
        /* Parse the pkcs12 file */
 
138
        parser = gcr_parser_new ();
 
139
        gcr_parser_add_password (parser, "booo");
 
140
        gcr_parser_format_enable (parser, GCR_FORMAT_DER_PKCS12);
 
141
        g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), test);
 
142
        gcr_parser_parse_data (parser, (const guchar *)contents, length, &error);
 
143
        g_assert_no_error (error);
 
144
        g_object_unref (parser);
 
145
        g_free (contents);
 
146
 
 
147
        /* Should have found importers */
 
148
        g_assert (test->importers != NULL);
 
149
 
 
150
        for (l = test->importers; l != NULL; l = g_list_next (l)) {
 
151
                gcr_importer_import (l->data, NULL, &error);
 
152
                g_assert_no_error (error);
 
153
        }
 
154
}
 
155
 
 
156
static void
 
157
null_log_handler (const gchar *log_domain,
 
158
                  GLogLevelFlags log_level,
 
159
                  const gchar *message,
 
160
                  gpointer user_data)
 
161
{
 
162
 
 
163
}
 
164
 
 
165
int
 
166
main (int argc, char **argv)
 
167
{
 
168
#if !GLIB_CHECK_VERSION(2,35,0)
 
169
        g_type_init ();
 
170
#endif
 
171
        g_test_init (&argc, &argv, NULL);
 
172
 
 
173
        g_set_prgname ("test-import");
 
174
 
 
175
        /* Suppress these messages in tests */
 
176
        g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE,
 
177
                           null_log_handler, NULL);
 
178
 
 
179
        g_test_add ("/gnome2-store/import/pkcs12", Test, NULL,
 
180
                    setup, test_pkcs12_import, teardown);
 
181
 
 
182
        return g_test_run ();
 
183
}