~noskcaj/ubuntu/vivid/gnome-keyring/3.15.90

« back to all changes in this revision

Viewing changes to pkcs11/wrap-layer/tests/test-login-specific.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-14 22:13:02 UTC
  • mfrom: (1.3.1)
  • mto: (80.2.8 experimental) (1.1.77)
  • mto: This revision was merged to the branch mainline in revision 148.
  • Revision ID: package-import@ubuntu.com-20120514221302-0l3gjmqpe6xopond
ImportĀ upstreamĀ versionĀ 3.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
#include "config.h"
23
23
 
24
 
#include "test-suite.h"
 
24
#include "wrap-layer/gkm-wrap-layer.h"
 
25
#include "wrap-layer/gkm-wrap-prompt.h"
 
26
 
 
27
#include "egg/egg-testing.h"
25
28
 
26
29
#include "gkm/gkm-mock.h"
27
30
#include "gkm/gkm-test.h"
28
31
 
29
 
#include "wrap-layer/gkm-wrap-layer.h"
30
 
 
31
 
#include "ui/gku-prompt.h"
32
 
 
33
 
static CK_FUNCTION_LIST prompt_login_functions;
34
 
static CK_FUNCTION_LIST_PTR module = NULL;
35
 
static CK_SESSION_HANDLE session = 0;
36
 
 
37
 
TESTING_SETUP (login_specific)
 
32
#include <gcr/gcr-base.h>
 
33
 
 
34
#include <glib-object.h>
 
35
 
 
36
typedef struct {
 
37
        CK_FUNCTION_LIST prompt_login_functions;
 
38
        CK_FUNCTION_LIST_PTR module;
 
39
        CK_SESSION_HANDLE session;
 
40
} Test;
 
41
 
 
42
static void
 
43
setup (Test *test, gconstpointer unused)
38
44
{
39
45
        CK_FUNCTION_LIST_PTR funcs;
40
46
        CK_OBJECT_HANDLE key;
41
47
        CK_SLOT_ID slot_id;
42
48
        CK_ULONG n_slots = 1;
 
49
        const gchar *prompter;
43
50
        CK_ULONG count;
44
51
        CK_RV rv;
45
52
 
53
60
        /* Always start off with test functions */
54
61
        rv = gkm_mock_C_GetFunctionList (&funcs);
55
62
        gkm_assert_cmprv (rv, ==, CKR_OK);
56
 
        memcpy (&prompt_login_functions, funcs, sizeof (prompt_login_functions));
 
63
        memcpy (&test->prompt_login_functions, funcs, sizeof (test->prompt_login_functions));
57
64
 
58
65
        gkm_wrap_layer_reset_modules ();
59
 
        gkm_wrap_layer_add_module (&prompt_login_functions);
60
 
        module = gkm_wrap_layer_get_functions ();
61
 
 
62
 
        gku_prompt_dummy_prepare_response ();
63
 
 
64
 
        /* Open a session */
65
 
        rv = (module->C_Initialize) (NULL);
66
 
        gkm_assert_cmprv (rv, ==, CKR_OK);
67
 
 
68
 
        rv = (module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots);
69
 
        gkm_assert_cmprv (rv, ==, CKR_OK);
70
 
 
71
 
        rv = (module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &session);
 
66
        gkm_wrap_layer_add_module (&test->prompt_login_functions);
 
67
        test->module = gkm_wrap_layer_get_functions ();
 
68
 
 
69
        prompter = gcr_mock_prompter_start ();
 
70
        gkm_wrap_prompt_set_prompter_name (prompter);
 
71
 
 
72
        /* Open a test->session */
 
73
        rv = (test->module->C_Initialize) (NULL);
 
74
        gkm_assert_cmprv (rv, ==, CKR_OK);
 
75
 
 
76
        rv = (test->module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots);
 
77
        gkm_assert_cmprv (rv, ==, CKR_OK);
 
78
 
 
79
        rv = (test->module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &test->session);
72
80
        gkm_assert_cmprv (rv, ==, CKR_OK);
73
81
 
74
82
        /* Find the always authenticate object */
75
 
        rv = (module->C_FindObjectsInit) (session, attrs, 1);
 
83
        rv = (test->module->C_FindObjectsInit) (test->session, attrs, 1);
76
84
        gkm_assert_cmprv (rv, ==, CKR_OK);
77
85
 
78
 
        rv = (module->C_FindObjects) (session, &key, 1, &count);
 
86
        rv = (test->module->C_FindObjects) (test->session, &key, 1, &count);
79
87
        gkm_assert_cmprv (rv, ==, CKR_OK);
80
88
        gkm_assert_cmpulong (count, ==, 1);
81
89
        gkm_assert_cmpulong (key, !=, 0);
82
90
 
83
 
        rv = (module->C_FindObjectsFinal) (session);
 
91
        rv = (test->module->C_FindObjectsFinal) (test->session);
84
92
        gkm_assert_cmprv (rv, ==, CKR_OK);
85
93
 
86
94
        /* Start a signing operation, that needs to be authenticated */
87
 
        rv = (module->C_SignInit) (session, &mech, key);
88
 
        gkm_assert_cmprv (rv, ==, CKR_OK);
89
 
}
90
 
 
91
 
TESTING_TEARDOWN (login_specific)
92
 
{
93
 
        CK_RV rv;
94
 
 
95
 
        g_assert (!gku_prompt_dummy_have_response ());
96
 
 
97
 
        rv = (module->C_CloseSession) (session);
98
 
        gkm_assert_cmprv (rv, ==, CKR_OK);
99
 
        session = 0;
100
 
 
101
 
        rv = (module->C_Finalize) (NULL);
102
 
        gkm_assert_cmprv (rv, ==, CKR_OK);
103
 
        module = NULL;
104
 
}
105
 
 
106
 
TESTING_TEST (login_specific_ok_password)
107
 
{
108
 
        CK_RV rv;
109
 
 
110
 
        gku_prompt_dummy_queue_ok_password ("booo");
111
 
 
112
 
        rv = (module->C_Login) (session, CKU_CONTEXT_SPECIFIC, NULL, 0);
113
 
        gkm_assert_cmprv (rv, ==, CKR_OK);
114
 
}
115
 
 
116
 
TESTING_TEST (login_specific_bad_password_then_cancel)
117
 
{
118
 
        CK_RV rv;
119
 
 
120
 
        gku_prompt_dummy_queue_ok_password ("bad password");
121
 
        gku_prompt_dummy_queue_no ();
122
 
 
123
 
        rv = (module->C_Login) (session, CKU_CONTEXT_SPECIFIC, NULL, 0);
124
 
        gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
125
 
}
126
 
 
127
 
TESTING_TEST (login_specific_cancel_immediately)
128
 
{
129
 
        CK_RV rv;
130
 
 
131
 
        gku_prompt_dummy_queue_no ();
132
 
 
133
 
        rv = (module->C_Login) (session, CKU_CONTEXT_SPECIFIC, NULL, 0);
134
 
        gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
 
95
        rv = (test->module->C_SignInit) (test->session, &mech, key);
 
96
        gkm_assert_cmprv (rv, ==, CKR_OK);
 
97
}
 
98
 
 
99
static void
 
100
teardown (Test *test, gconstpointer unused)
 
101
{
 
102
        CK_RV rv;
 
103
 
 
104
        g_assert (!gcr_mock_prompter_is_expecting ());
 
105
        gcr_mock_prompter_stop ();
 
106
 
 
107
        rv = (test->module->C_CloseSession) (test->session);
 
108
        gkm_assert_cmprv (rv, ==, CKR_OK);
 
109
        test->session = 0;
 
110
 
 
111
        rv = (test->module->C_Finalize) (NULL);
 
112
        gkm_assert_cmprv (rv, ==, CKR_OK);
 
113
        test->module = NULL;
 
114
}
 
115
 
 
116
static void
 
117
test_ok_password (Test *test, gconstpointer unused)
 
118
{
 
119
        CK_RV rv;
 
120
 
 
121
        gcr_mock_prompter_expect_password_ok ("booo", NULL);
 
122
 
 
123
        rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0);
 
124
        gkm_assert_cmprv (rv, ==, CKR_OK);
 
125
}
 
126
 
 
127
static void
 
128
test_bad_password_then_cancel (Test *test, gconstpointer unused)
 
129
{
 
130
        CK_RV rv;
 
131
 
 
132
        gcr_mock_prompter_expect_password_ok ("bad password", NULL);
 
133
        gcr_mock_prompter_expect_password_cancel ();
 
134
 
 
135
        rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0);
 
136
        gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
 
137
}
 
138
 
 
139
static void
 
140
test_cancel_immediately (Test *test, gconstpointer unused)
 
141
{
 
142
        CK_RV rv;
 
143
 
 
144
        gcr_mock_prompter_expect_password_cancel ();
 
145
 
 
146
        rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0);
 
147
        gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
 
148
}
 
149
 
 
150
int
 
151
main (int argc, char **argv)
 
152
{
 
153
        g_type_init ();
 
154
        g_test_init (&argc, &argv, NULL);
 
155
 
 
156
        g_test_add ("/wrap-layer/login-specific/ok_password", Test, NULL, setup, test_ok_password, teardown);
 
157
        g_test_add ("/wrap-layer/login-specific/bad_password_then_cancel", Test, NULL, setup, test_bad_password_then_cancel, teardown);
 
158
        g_test_add ("/wrap-layer/login-specific/cancel_immediately", Test, NULL, setup, test_cancel_immediately, teardown);
 
159
 
 
160
        return egg_tests_run_in_thread_with_loop ();
135
161
}