| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
| 2 |
/* test-credential.c: Test credentials |
| 3 |
|
| 4 |
Copyright (C) 2009 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 <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include "mock-module.h" |
| 26 |
#include "mock-locked-object.h" |
| 27 |
|
| 28 |
#include "egg/egg-testing.h" |
| 29 |
|
| 30 |
#include "gkm/gkm-attributes.h" |
| 31 |
#include "gkm/gkm-credential.h" |
| 32 |
#include "gkm/gkm-object.h" |
| 33 |
#include "gkm/gkm-secret.h" |
| 34 |
#include "gkm/gkm-session.h" |
| 35 |
#include "gkm/gkm-module.h" |
| 36 |
#include "gkm/gkm-test.h" |
| 37 |
|
| 38 |
#include "pkcs11i.h" |
| 39 |
|
| 40 |
typedef struct { |
| 41 |
GkmModule *module; |
| 42 |
GkmSession *session; |
| 43 |
GkmObject *object; |
| 44 |
} Test; |
| 45 |
|
| 46 |
static void |
| 47 |
setup (Test *test, gconstpointer unused) |
| 48 |
{ |
| 49 |
CK_RV rv; |
| 50 |
test->module = mock_module_initialize_and_enter (); |
| 51 |
test->session = mock_module_open_session (TRUE); |
| 52 |
|
| 53 |
rv = gkm_module_C_Login (test->module, gkm_session_get_handle (test->session), CKU_USER, NULL, 0); |
| 54 |
g_assert (rv == CKR_OK); |
| 55 |
|
| 56 |
test->object = mock_locked_object_new (test->module, gkm_module_get_manager (test->module)); |
| 57 |
gkm_object_expose (test->object, TRUE); |
| 58 |
} |
| 59 |
|
| 60 |
static void |
| 61 |
teardown (Test *test, gconstpointer unused) |
| 62 |
{ |
| 63 |
g_object_unref (test->object); |
| 64 |
mock_module_leave_and_finalize (); |
| 65 |
} |
| 66 |
|
| 67 |
static void |
| 68 |
test_create (Test *test, gconstpointer unused) |
| 69 |
{ |
| 70 |
CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; |
| 71 |
CK_OBJECT_HANDLE locked = gkm_object_get_handle (test->object); |
| 72 |
|
| 73 |
CK_ATTRIBUTE attrs[] = { |
| 74 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 75 |
{ CKA_G_OBJECT, &locked, sizeof (locked) }, |
| 76 |
{ CKA_VALUE, "mock", 4 }, |
| 77 |
}; |
| 78 |
|
| 79 |
CK_OBJECT_HANDLE handle; |
| 80 |
CK_RV rv; |
| 81 |
|
| 82 |
rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); |
| 83 |
g_assert (rv == CKR_OK); |
| 84 |
g_assert (handle != 0); |
| 85 |
|
| 86 |
rv = gkm_session_C_DestroyObject (test->session, handle); |
| 87 |
g_assert (rv == CKR_OK); |
| 88 |
} |
| 89 |
|
| 90 |
static void |
| 91 |
test_create_missing_pin (Test *test, gconstpointer unused) |
| 92 |
{ |
| 93 |
CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; |
| 94 |
CK_OBJECT_HANDLE locked = gkm_object_get_handle (test->object); |
| 95 |
|
| 96 |
CK_ATTRIBUTE attrs[] = { |
| 97 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 98 |
{ CKA_G_OBJECT, &locked, sizeof (locked) }, |
| 99 |
}; |
| 100 |
|
| 101 |
CK_OBJECT_HANDLE handle; |
| 102 |
CK_RV rv; |
| 103 |
|
| 104 |
rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); |
| 105 |
g_assert (rv == CKR_USER_NOT_LOGGED_IN); |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
test_create_no_object (Test *test, gconstpointer unused) |
| 110 |
{ |
| 111 |
CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; |
| 112 |
CK_BBOOL token = CK_FALSE; |
| 113 |
CK_OBJECT_HANDLE objhand = (CK_ULONG)-1; |
| 114 |
CK_ATTRIBUTE attr; |
| 115 |
|
| 116 |
CK_ATTRIBUTE attrs[] = { |
| 117 |
{ CKA_TOKEN, &token, sizeof (token) }, |
| 118 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 119 |
}; |
| 120 |
|
| 121 |
CK_OBJECT_HANDLE handle; |
| 122 |
CK_RV rv; |
| 123 |
|
| 124 |
rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); |
| 125 |
g_assert (rv == CKR_OK); |
| 126 |
g_assert (handle != 0); |
| 127 |
|
| 128 |
attr.type = CKA_G_OBJECT; |
| 129 |
attr.pValue = &objhand; |
| 130 |
attr.ulValueLen = sizeof (objhand); |
| 131 |
rv = gkm_session_C_GetAttributeValue (test->session, handle, &attr, 1); |
| 132 |
g_assert (rv == CKR_OK); |
| 133 |
g_assert (objhand == 0); |
| 134 |
} |
| 135 |
|
| 136 |
static void |
| 137 |
test_create_invalid_object (Test *test, gconstpointer unused) |
| 138 |
{ |
| 139 |
CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; |
| 140 |
CK_OBJECT_HANDLE locked = 0; |
| 141 |
CK_BBOOL token = CK_FALSE; |
| 142 |
|
| 143 |
CK_ATTRIBUTE attrs[] = { |
| 144 |
{ CKA_TOKEN, &token, sizeof (token) }, |
| 145 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 146 |
{ CKA_G_OBJECT, &locked, sizeof (locked) }, |
| 147 |
}; |
| 148 |
|
| 149 |
CK_OBJECT_HANDLE handle; |
| 150 |
CK_RV rv; |
| 151 |
|
| 152 |
rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); |
| 153 |
g_assert (rv == CKR_OBJECT_HANDLE_INVALID); |
| 154 |
} |
| 155 |
|
| 156 |
static void |
| 157 |
test_get_attributes (Test *test, gconstpointer unused) |
| 158 |
{ |
| 159 |
CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; |
| 160 |
CK_OBJECT_HANDLE locked = gkm_object_get_handle (test->object); |
| 161 |
|
| 162 |
CK_ATTRIBUTE attrs[] = { |
| 163 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 164 |
{ CKA_G_OBJECT, &locked, sizeof (locked) }, |
| 165 |
{ CKA_VALUE, "mock", 4 }, |
| 166 |
}; |
| 167 |
|
| 168 |
CK_OBJECT_HANDLE handle; |
| 169 |
CK_ATTRIBUTE check; |
| 170 |
CK_ULONG value; |
| 171 |
CK_RV rv; |
| 172 |
|
| 173 |
rv = gkm_session_C_CreateObject (test->session, attrs, G_N_ELEMENTS (attrs), &handle); |
| 174 |
g_assert (rv == CKR_OK); |
| 175 |
g_assert (handle != 0); |
| 176 |
|
| 177 |
check.type = CKA_G_OBJECT; |
| 178 |
check.pValue = &value; |
| 179 |
check.ulValueLen = sizeof (value); |
| 180 |
|
| 181 |
rv = gkm_session_C_GetAttributeValue (test->session, handle, &check, 1); |
| 182 |
g_assert (rv == CKR_OK); |
| 183 |
g_assert (check.ulValueLen == sizeof (value)); |
| 184 |
g_assert (value == locked); |
| 185 |
} |
| 186 |
|
| 187 |
static void |
| 188 |
test_object_property (Test *test, gconstpointer unused) |
| 189 |
{ |
| 190 |
GkmCredential *auth; |
| 191 |
GkmObject *check; |
| 192 |
CK_RV rv; |
| 193 |
|
| 194 |
rv = gkm_credential_create (test->module, NULL, test->object, (guchar*)"mock", 4, &auth); |
| 195 |
g_assert (rv == CKR_OK); |
| 196 |
g_assert (auth); |
| 197 |
|
| 198 |
g_object_get (auth, "object", &check, NULL); |
| 199 |
g_assert (check == test->object); |
| 200 |
g_object_unref (check); |
| 201 |
|
| 202 |
check = gkm_credential_get_object (auth); |
| 203 |
g_assert (check == test->object); |
| 204 |
|
| 205 |
g_object_unref (auth); |
| 206 |
} |
| 207 |
|
| 208 |
static void |
| 209 |
test_login_property (Test *test, gconstpointer unused) |
| 210 |
{ |
| 211 |
GkmCredential *cred; |
| 212 |
GkmSecret *check, *secret; |
| 213 |
const gchar *password; |
| 214 |
gsize n_password; |
| 215 |
CK_RV rv; |
| 216 |
|
| 217 |
rv = gkm_credential_create (test->module, NULL, test->object, (guchar*)"mock", 4, &cred); |
| 218 |
g_assert (rv == CKR_OK); |
| 219 |
g_assert (cred); |
| 220 |
|
| 221 |
g_object_get (cred, "secret", &check, NULL); |
| 222 |
g_assert (check); |
| 223 |
password = gkm_secret_get_password (check, &n_password); |
| 224 |
g_assert (n_password == 4); |
| 225 |
g_assert (memcmp (password, "mock", 4) == 0); |
| 226 |
g_object_unref (check); |
| 227 |
|
| 228 |
check = gkm_credential_get_secret (cred); |
| 229 |
g_assert (n_password == 4); |
| 230 |
g_assert (memcmp (password, "mock", 4) == 0); |
| 231 |
|
| 232 |
secret = gkm_secret_new ((guchar*)"xxx", -1); |
| 233 |
gkm_credential_set_secret (cred, secret); |
| 234 |
check = gkm_credential_get_secret (cred); |
| 235 |
g_assert (check == secret); |
| 236 |
g_object_unref (secret); |
| 237 |
|
| 238 |
g_object_unref (cred); |
| 239 |
} |
| 240 |
|
| 241 |
static GType |
| 242 |
boxed_string (void) |
| 243 |
{ |
| 244 |
static GType type = 0; |
| 245 |
if (!type) |
| 246 |
type = g_boxed_type_register_static ("TestBoxedString", |
| 247 |
(GBoxedCopyFunc)g_strdup, |
| 248 |
(GBoxedFreeFunc)g_free); |
| 249 |
return type; |
| 250 |
} |
| 251 |
|
| 252 |
static void |
| 253 |
test_data (Test *test, gconstpointer unused) |
| 254 |
{ |
| 255 |
GkmCredential *cred; |
| 256 |
GType type = boxed_string (); |
| 257 |
gchar *check; |
| 258 |
CK_RV rv; |
| 259 |
|
| 260 |
rv = gkm_credential_create (test->module, NULL, test->object, (guchar*)"mock", 4, &cred); |
| 261 |
g_assert (rv == CKR_OK); |
| 262 |
g_assert (cred); |
| 263 |
|
| 264 |
g_assert (gkm_credential_peek_data (cred, type) == NULL); |
| 265 |
|
| 266 |
gkm_credential_set_data (cred, type, "one"); |
| 267 |
|
| 268 |
check = gkm_credential_pop_data (cred, type); |
| 269 |
g_assert_cmpstr ("one", ==, check); |
| 270 |
g_free (check); |
| 271 |
|
| 272 |
g_assert_cmpstr ("one", ==, gkm_credential_peek_data (cred, type)); |
| 273 |
|
| 274 |
gkm_credential_set_data (cred, type, "ONE"); |
| 275 |
g_assert_cmpstr ("ONE", ==, gkm_credential_peek_data (cred, type)); |
| 276 |
|
| 277 |
gkm_credential_set_data (cred, 0, NULL); |
| 278 |
g_assert (gkm_credential_peek_data (cred, 0) == NULL); |
| 279 |
|
| 280 |
g_object_unref (cred); |
| 281 |
} |
| 282 |
|
| 283 |
static void |
| 284 |
test_connect_object (Test *test, gconstpointer unused) |
| 285 |
{ |
| 286 |
GkmCredential *cred; |
| 287 |
CK_RV rv; |
| 288 |
|
| 289 |
rv = gkm_credential_create (test->module, NULL, NULL, (guchar*)"mock", 4, &cred); |
| 290 |
g_assert (rv == CKR_OK); |
| 291 |
g_assert (cred); |
| 292 |
|
| 293 |
gkm_credential_connect (cred, test->object); |
| 294 |
g_assert (gkm_credential_get_object (cred) == test->object); |
| 295 |
|
| 296 |
g_object_unref (cred); |
| 297 |
} |
| 298 |
|
| 299 |
static void |
| 300 |
test_value_is_accessible_to_daemon (Test *test, gconstpointer unused) |
| 301 |
{ |
| 302 |
GkmCredential *cred; |
| 303 |
gchar buffer[20]; |
| 304 |
CK_ATTRIBUTE attr; |
| 305 |
CK_RV rv; |
| 306 |
|
| 307 |
rv = gkm_credential_create (test->module, NULL, NULL, (guchar*)"mock", 4, &cred); |
| 308 |
g_assert (rv == CKR_OK); |
| 309 |
g_assert (cred); |
| 310 |
|
| 311 |
attr.type = CKA_VALUE; |
| 312 |
attr.pValue = buffer; |
| 313 |
attr.ulValueLen = sizeof (buffer); |
| 314 |
|
| 315 |
rv = gkm_object_get_attribute (GKM_OBJECT (cred), test->session, &attr); |
| 316 |
gkm_assert_cmprv (rv, ==, CKR_OK); |
| 317 |
egg_assert_cmpmem ("mock", 4, ==, attr.pValue, attr.ulValueLen); |
| 318 |
|
| 319 |
g_object_unref (cred); |
| 320 |
} |
| 321 |
|
| 322 |
static void |
| 323 |
test_value_is_inaccessible_to_applications (Test *test, gconstpointer unused) |
| 324 |
{ |
| 325 |
GkmCredential *cred; |
| 326 |
CK_G_APPLICATION app; |
| 327 |
gchar buffer[20]; |
| 328 |
CK_ATTRIBUTE attr; |
| 329 |
CK_SESSION_HANDLE handle; |
| 330 |
GkmSession *session; |
| 331 |
CK_RV rv; |
| 332 |
|
| 333 |
memset (&app, 0, sizeof (app)); |
| 334 |
rv = gkm_module_C_OpenSession (test->module, 1, CKF_SERIAL_SESSION | CKF_G_APPLICATION_SESSION, &app, NULL, &handle); |
| 335 |
gkm_assert_cmprv (rv, ==, CKR_OK); |
| 336 |
|
| 337 |
session = gkm_module_lookup_session (test->module, handle); |
| 338 |
g_assert (session); |
| 339 |
|
| 340 |
rv = gkm_credential_create (test->module, NULL, NULL, (guchar*)"mock", 4, &cred); |
| 341 |
g_assert (rv == CKR_OK); |
| 342 |
g_assert (cred); |
| 343 |
|
| 344 |
attr.type = CKA_VALUE; |
| 345 |
attr.pValue = buffer; |
| 346 |
attr.ulValueLen = sizeof (buffer); |
| 347 |
|
| 348 |
rv = gkm_object_get_attribute (GKM_OBJECT (cred), session, &attr); |
| 349 |
gkm_assert_cmprv (rv, ==, CKR_ATTRIBUTE_SENSITIVE); |
| 350 |
|
| 351 |
g_object_unref (cred); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
int |
| 356 |
main (int argc, char **argv) |
| 357 |
{ |
| 358 |
#if !GLIB_CHECK_VERSION(2,35,0) |
| 359 |
g_type_init (); |
| 360 |
#endif |
| 361 |
g_test_init (&argc, &argv, NULL); |
| 362 |
|
| 363 |
g_test_add ("/gkm/credential/create", Test, NULL, setup, test_create, teardown); |
| 364 |
g_test_add ("/gkm/credential/create_missing_pin", Test, NULL, setup, test_create_missing_pin, teardown); |
| 365 |
g_test_add ("/gkm/credential/create_no_object", Test, NULL, setup, test_create_no_object, teardown); |
| 366 |
g_test_add ("/gkm/credential/create_invalid_object", Test, NULL, setup, test_create_invalid_object, teardown); |
| 367 |
g_test_add ("/gkm/credential/get_attributes", Test, NULL, setup, test_get_attributes, teardown); |
| 368 |
g_test_add ("/gkm/credential/object_property", Test, NULL, setup, test_object_property, teardown); |
| 369 |
g_test_add ("/gkm/credential/login_property", Test, NULL, setup, test_login_property, teardown); |
| 370 |
g_test_add ("/gkm/credential/data", Test, NULL, setup, test_data, teardown); |
| 371 |
g_test_add ("/gkm/credential/connect_object", Test, NULL, setup, test_connect_object, teardown); |
| 372 |
g_test_add ("/gkm/credential/value_is_accessible_to_daemon", Test, NULL, setup, test_value_is_accessible_to_daemon, teardown); |
| 373 |
g_test_add ("/gkm/credential/value_is_inaccessible_to_applications", Test, NULL, setup, test_value_is_inaccessible_to_applications, teardown); |
| 374 |
|
| 375 |
return g_test_run (); |
| 376 |
} |