| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
| 2 |
/* |
| 3 |
Copyright (C) 2009 Stefan Walter |
| 4 |
|
| 5 |
The Gnome Keyring Library is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU Library General Public License as |
| 7 |
published by the Free Software Foundation; either version 2 of the |
| 8 |
License, or (at your option) any later version. |
| 9 |
|
| 10 |
The Gnome Keyring Library is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Library General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Library General Public |
| 16 |
License along with the Gnome Library; see the file COPYING.LIB. If not, |
| 17 |
<http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
Author: Stef Walter <[email protected]> |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include "mock-secret-module.h" |
| 25 |
|
| 26 |
#include "secret-store/gkm-secret-object.h" |
| 27 |
|
| 28 |
#include "gkm/gkm-transaction.h" |
| 29 |
|
| 30 |
#include "pkcs11/pkcs11i.h" |
| 31 |
|
| 32 |
#include <glib.h> |
| 33 |
|
| 34 |
#include <stdlib.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <string.h> |
| 37 |
|
| 38 |
typedef struct { |
| 39 |
GkmModule *module; |
| 40 |
GkmSession *session; |
| 41 |
GkmSecretObject *object; |
| 42 |
} Test; |
| 43 |
|
| 44 |
static void |
| 45 |
setup (Test *test, gconstpointer unused) |
| 46 |
{ |
| 47 |
test->module = test_secret_module_initialize_and_enter (); |
| 48 |
test->session = test_secret_module_open_session (TRUE); |
| 49 |
|
| 50 |
test->object = g_object_new (GKM_TYPE_SECRET_OBJECT, |
| 51 |
"module", test->module, |
| 52 |
"identifier", "my-identifier", |
| 53 |
NULL); |
| 54 |
|
| 55 |
g_assert (GKM_IS_SECRET_OBJECT (test->object)); |
| 56 |
} |
| 57 |
|
| 58 |
static void |
| 59 |
teardown (Test *test, gconstpointer unused) |
| 60 |
{ |
| 61 |
g_object_unref (test->object); |
| 62 |
test_secret_module_leave_and_finalize (); |
| 63 |
} |
| 64 |
|
| 65 |
static void |
| 66 |
test_is_locked (Test *test, gconstpointer unused) |
| 67 |
{ |
| 68 |
/* Plain GkmSecretObject is never locked */ |
| 69 |
g_assert (!gkm_secret_object_is_locked (test->object, test->session)); |
| 70 |
} |
| 71 |
|
| 72 |
static void |
| 73 |
test_identifier_prop (Test *test, gconstpointer unused) |
| 74 |
{ |
| 75 |
const gchar *identifier; |
| 76 |
identifier = gkm_secret_object_get_identifier (test->object); |
| 77 |
g_assert_cmpstr (identifier, ==, "my-identifier"); |
| 78 |
} |
| 79 |
|
| 80 |
static void |
| 81 |
was_notified (GObject *obj, GParamSpec *pspec, gpointer user_data) |
| 82 |
{ |
| 83 |
gboolean* notified = user_data; |
| 84 |
g_assert (user_data); |
| 85 |
*notified = TRUE; |
| 86 |
} |
| 87 |
|
| 88 |
static void |
| 89 |
test_created_prop (Test *test, gconstpointer unused) |
| 90 |
{ |
| 91 |
glong created; |
| 92 |
|
| 93 |
/* Monitor for changes */ |
| 94 |
gboolean notified = FALSE; |
| 95 |
g_signal_connect (test->object, "notify::created", G_CALLBACK (was_notified), ¬ified); |
| 96 |
|
| 97 |
/* Default value */ |
| 98 |
created = gkm_secret_object_get_created (test->object); |
| 99 |
g_assert (created == 0); |
| 100 |
|
| 101 |
/* Set a new value */ |
| 102 |
gkm_secret_object_set_created (test->object, 1247930171); |
| 103 |
g_assert (notified); |
| 104 |
created = gkm_secret_object_get_created (test->object); |
| 105 |
g_assert (created == 1247930171); |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
test_modified_prop (Test *test, gconstpointer unused) |
| 110 |
{ |
| 111 |
glong modified; |
| 112 |
|
| 113 |
/* Monitor for changes */ |
| 114 |
gboolean notified = FALSE; |
| 115 |
g_signal_connect (test->object, "notify::modified", G_CALLBACK (was_notified), ¬ified); |
| 116 |
|
| 117 |
/* Default value */ |
| 118 |
modified = gkm_secret_object_get_modified (test->object); |
| 119 |
g_assert (modified == 0); |
| 120 |
|
| 121 |
/* Set a new value */ |
| 122 |
gkm_secret_object_set_modified (test->object, 1247930171); |
| 123 |
g_assert (notified); |
| 124 |
modified = gkm_secret_object_get_modified (test->object); |
| 125 |
g_assert (modified == 1247930171); |
| 126 |
} |
| 127 |
|
| 128 |
static void |
| 129 |
test_was_modified (Test *test, gconstpointer unused) |
| 130 |
{ |
| 131 |
GkmTransaction *transaction; |
| 132 |
gint64 now; |
| 133 |
|
| 134 |
now = g_get_real_time () / G_USEC_PER_SEC; |
| 135 |
|
| 136 |
transaction = gkm_transaction_new (); |
| 137 |
gkm_secret_object_begin_modified (test->object, transaction); |
| 138 |
g_assert_cmpuint (gkm_transaction_complete_and_unref (transaction), ==, CKR_OK); |
| 139 |
|
| 140 |
g_assert (now == gkm_secret_object_get_modified (test->object)); |
| 141 |
} |
| 142 |
|
| 143 |
static void |
| 144 |
test_label_prop (Test *test, gconstpointer unused) |
| 145 |
{ |
| 146 |
const gchar *label; |
| 147 |
|
| 148 |
/* Monitor for changes */ |
| 149 |
gboolean notified = FALSE; |
| 150 |
g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), ¬ified); |
| 151 |
|
| 152 |
/* Default value */ |
| 153 |
label = gkm_secret_object_get_label (test->object); |
| 154 |
g_assert_cmpstr (label, ==, ""); |
| 155 |
|
| 156 |
/* Set a new value */ |
| 157 |
gkm_secret_object_set_label (test->object, "hello"); |
| 158 |
g_assert (notified); |
| 159 |
label = gkm_secret_object_get_label (test->object); |
| 160 |
g_assert_cmpstr (label, ==, "hello"); |
| 161 |
} |
| 162 |
|
| 163 |
static void |
| 164 |
test_identifier_get_attr (Test *test, gconstpointer unused) |
| 165 |
{ |
| 166 |
gchar buffer[32]; |
| 167 |
CK_ATTRIBUTE attr = { CKA_ID, buffer, 32 }; |
| 168 |
CK_RV rv; |
| 169 |
|
| 170 |
rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr); |
| 171 |
g_assert (rv == CKR_OK); |
| 172 |
g_assert (attr.ulValueLen == 13); |
| 173 |
g_assert (memcmp (buffer, "my-identifier", 13) == 0); |
| 174 |
} |
| 175 |
|
| 176 |
static void |
| 177 |
test_label_get_attr (Test *test, gconstpointer unused) |
| 178 |
{ |
| 179 |
gchar buffer[32]; |
| 180 |
CK_ATTRIBUTE attr = { CKA_LABEL, buffer, 32 }; |
| 181 |
CK_RV rv; |
| 182 |
|
| 183 |
gkm_secret_object_set_label (test->object, "hello"); |
| 184 |
rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr); |
| 185 |
g_assert (rv == CKR_OK); |
| 186 |
g_assert (attr.ulValueLen == 5); |
| 187 |
g_assert (memcmp (buffer, "hello", 5) == 0); |
| 188 |
} |
| 189 |
|
| 190 |
static void |
| 191 |
test_label_set_attr (Test *test, gconstpointer unused) |
| 192 |
{ |
| 193 |
CK_ATTRIBUTE attr = { CKA_LABEL, "hello", 5 }; |
| 194 |
GkmTransaction *transaction = gkm_transaction_new (); |
| 195 |
|
| 196 |
/* Monitor for changes */ |
| 197 |
gboolean notified = FALSE; |
| 198 |
g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), ¬ified); |
| 199 |
|
| 200 |
gkm_object_set_attribute (GKM_OBJECT (test->object), test->session, transaction, &attr); |
| 201 |
g_assert (!gkm_transaction_get_failed (transaction)); |
| 202 |
g_assert (!notified); /* Not notified yet */ |
| 203 |
|
| 204 |
g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello"); |
| 205 |
|
| 206 |
gkm_transaction_complete (transaction); |
| 207 |
g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello"); |
| 208 |
g_assert (notified); /* Notified after transaction complete */ |
| 209 |
|
| 210 |
g_object_unref (transaction); |
| 211 |
} |
| 212 |
|
| 213 |
static void |
| 214 |
test_label_set_attr_fail (Test *test, gconstpointer unused) |
| 215 |
{ |
| 216 |
CK_ATTRIBUTE attr = { CKA_LABEL, "hello", 5 }; |
| 217 |
GkmTransaction *transaction = gkm_transaction_new (); |
| 218 |
gboolean notified = FALSE; |
| 219 |
|
| 220 |
/* Set an old value */ |
| 221 |
gkm_secret_object_set_label (test->object, "old"); |
| 222 |
|
| 223 |
/* Monitor for changes */ |
| 224 |
g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), ¬ified); |
| 225 |
|
| 226 |
/* Set a new value */ |
| 227 |
gkm_object_set_attribute (GKM_OBJECT (test->object), test->session, transaction, &attr); |
| 228 |
g_assert (!gkm_transaction_get_failed (transaction)); |
| 229 |
g_assert (!notified); /* Not notified yet */ |
| 230 |
|
| 231 |
/* Temporarily has new value */ |
| 232 |
g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello"); |
| 233 |
|
| 234 |
/* Fail and complete transaction */ |
| 235 |
gkm_transaction_fail (transaction, CKR_CANCEL); |
| 236 |
gkm_transaction_complete (transaction); |
| 237 |
|
| 238 |
/* Back to old value */ |
| 239 |
g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "old"); |
| 240 |
g_assert (!notified); /* Should never have notified */ |
| 241 |
|
| 242 |
g_object_unref (transaction); |
| 243 |
} |
| 244 |
|
| 245 |
static void |
| 246 |
test_modified_get_attr (Test *test, gconstpointer unused) |
| 247 |
{ |
| 248 |
gchar buffer[32]; |
| 249 |
CK_ATTRIBUTE attr = { CKA_G_MODIFIED, buffer, 32 }; |
| 250 |
CK_RV rv; |
| 251 |
|
| 252 |
gkm_secret_object_set_modified (test->object, 1247930171); |
| 253 |
rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr); |
| 254 |
g_assert (rv == CKR_OK); |
| 255 |
g_assert (attr.ulValueLen == 16); |
| 256 |
g_assert (memcmp (buffer, "2009071815161100", 16) == 0); |
| 257 |
} |
| 258 |
|
| 259 |
static void |
| 260 |
test_created_get_attr (Test *test, gconstpointer unused) |
| 261 |
{ |
| 262 |
gchar buffer[32]; |
| 263 |
CK_ATTRIBUTE attr = { CKA_G_CREATED, buffer, 32 }; |
| 264 |
CK_RV rv; |
| 265 |
|
| 266 |
gkm_secret_object_set_created (test->object, 1247930171); |
| 267 |
rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr); |
| 268 |
g_assert (rv == CKR_OK); |
| 269 |
g_assert (attr.ulValueLen == 16); |
| 270 |
g_assert (memcmp (buffer, "2009071815161100", 16) == 0); |
| 271 |
} |
| 272 |
|
| 273 |
static void |
| 274 |
test_locked_get_attr (Test *test, gconstpointer unused) |
| 275 |
{ |
| 276 |
gchar buffer[32]; |
| 277 |
CK_ATTRIBUTE attr = { CKA_G_LOCKED, buffer, 32 }; |
| 278 |
CK_RV rv; |
| 279 |
|
| 280 |
rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr); |
| 281 |
g_assert (rv == CKR_OK); |
| 282 |
g_assert (attr.ulValueLen == 1); |
| 283 |
g_assert (memcmp (buffer, "\0", 1) == 0); |
| 284 |
} |
| 285 |
|
| 286 |
int |
| 287 |
main (int argc, char **argv) |
| 288 |
{ |
| 289 |
#if !GLIB_CHECK_VERSION(2,35,0) |
| 290 |
g_type_init (); |
| 291 |
#endif |
| 292 |
g_test_init (&argc, &argv, NULL); |
| 293 |
|
| 294 |
g_test_add ("/secret-store/object/is_locked", Test, NULL, setup, test_is_locked, teardown); |
| 295 |
g_test_add ("/secret-store/object/identifier_prop", Test, NULL, setup, test_identifier_prop, teardown); |
| 296 |
g_test_add ("/secret-store/object/created_prop", Test, NULL, setup, test_created_prop, teardown); |
| 297 |
g_test_add ("/secret-store/object/modified_prop", Test, NULL, setup, test_modified_prop, teardown); |
| 298 |
g_test_add ("/secret-store/object/was_modified", Test, NULL, setup, test_was_modified, teardown); |
| 299 |
g_test_add ("/secret-store/object/label_prop", Test, NULL, setup, test_label_prop, teardown); |
| 300 |
g_test_add ("/secret-store/object/identifier_get_attr", Test, NULL, setup, test_identifier_get_attr, teardown); |
| 301 |
g_test_add ("/secret-store/object/label_get_attr", Test, NULL, setup, test_label_get_attr, teardown); |
| 302 |
g_test_add ("/secret-store/object/label_set_attr", Test, NULL, setup, test_label_set_attr, teardown); |
| 303 |
g_test_add ("/secret-store/object/label_set_attr_fail", Test, NULL, setup, test_label_set_attr_fail, teardown); |
| 304 |
g_test_add ("/secret-store/object/modified_get_attr", Test, NULL, setup, test_modified_get_attr, teardown); |
| 305 |
g_test_add ("/secret-store/object/created_get_attr", Test, NULL, setup, test_created_get_attr, teardown); |
| 306 |
g_test_add ("/secret-store/object/locked_get_attr", Test, NULL, setup, test_locked_get_attr, teardown); |
| 307 |
|
| 308 |
return g_test_run (); |
| 309 |
} |