| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
| 2 |
/* test-secret-compat.c: Test secret compat files |
| 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 <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include "secret-store/gkm-secret-data.h" |
| 26 |
|
| 27 |
#include "gkm/gkm-secret.h" |
| 28 |
#include "gkm/gkm-transaction.h" |
| 29 |
|
| 30 |
#include "egg/egg-secure-memory.h" |
| 31 |
|
| 32 |
#include <glib.h> |
| 33 |
|
| 34 |
#include <stdlib.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <string.h> |
| 37 |
|
| 38 |
EGG_SECURE_DEFINE_GLIB_GLOBALS (); |
| 39 |
|
| 40 |
static void |
| 41 |
test_new (void) |
| 42 |
{ |
| 43 |
GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); |
| 44 |
g_assert (GKM_IS_SECRET_DATA (data)); |
| 45 |
g_object_unref (data); |
| 46 |
} |
| 47 |
|
| 48 |
static void |
| 49 |
test_get_set (void) |
| 50 |
{ |
| 51 |
GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); |
| 52 |
GkmSecret *secret = gkm_secret_new_from_password ("barn"); |
| 53 |
GkmSecret *check; |
| 54 |
|
| 55 |
gkm_secret_data_set_secret (data, "my-identifier", secret); |
| 56 |
g_object_unref (secret); |
| 57 |
|
| 58 |
check = gkm_secret_data_get_secret (data, "my-identifier"); |
| 59 |
g_assert (GKM_IS_SECRET (check)); |
| 60 |
g_assert (secret == check); |
| 61 |
g_assert (gkm_secret_equals (check, (guchar*)"barn", -1)); |
| 62 |
|
| 63 |
g_object_unref (data); |
| 64 |
} |
| 65 |
|
| 66 |
static void |
| 67 |
test_get_raw (void) |
| 68 |
{ |
| 69 |
GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); |
| 70 |
GkmSecret *secret = gkm_secret_new_from_password ("barn"); |
| 71 |
const guchar *raw; |
| 72 |
gsize n_raw; |
| 73 |
|
| 74 |
gkm_secret_data_set_secret (data, "my-identifier", secret); |
| 75 |
g_object_unref (secret); |
| 76 |
|
| 77 |
raw = gkm_secret_data_get_raw (data, "my-identifier", &n_raw); |
| 78 |
g_assert (raw); |
| 79 |
g_assert_cmpuint (n_raw, ==, 4); |
| 80 |
g_assert (memcmp (raw, "barn", 4) == 0); |
| 81 |
|
| 82 |
raw = gkm_secret_data_get_raw (data, "not-identifier", &n_raw); |
| 83 |
g_assert (raw == NULL); |
| 84 |
|
| 85 |
g_object_unref (data); |
| 86 |
} |
| 87 |
|
| 88 |
static void |
| 89 |
test_remove (void) |
| 90 |
{ |
| 91 |
GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); |
| 92 |
GkmSecret *secret = gkm_secret_new_from_password ("barn"); |
| 93 |
|
| 94 |
gkm_secret_data_set_secret (data, "my-identifier", secret); |
| 95 |
g_object_unref (secret); |
| 96 |
|
| 97 |
secret = gkm_secret_data_get_secret (data, "my-identifier"); |
| 98 |
g_assert (GKM_IS_SECRET (secret)); |
| 99 |
|
| 100 |
gkm_secret_data_remove_secret (data, "my-identifier"); |
| 101 |
secret = gkm_secret_data_get_secret (data, "my-identifier"); |
| 102 |
g_assert (!secret); |
| 103 |
|
| 104 |
g_object_unref (data); |
| 105 |
} |
| 106 |
|
| 107 |
static void |
| 108 |
test_set_transacted (void) |
| 109 |
{ |
| 110 |
GkmTransaction *transaction = gkm_transaction_new (); |
| 111 |
GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); |
| 112 |
GkmSecret *secret = gkm_secret_new_from_password ("barn"); |
| 113 |
|
| 114 |
/* Transaction, but not complete */ |
| 115 |
gkm_secret_data_set_transacted (data, transaction, "my-identifier", secret); |
| 116 |
g_assert (!gkm_transaction_get_failed (transaction)); |
| 117 |
g_assert (gkm_secret_data_get_secret (data, "my-identifier") == secret); |
| 118 |
|
| 119 |
/* Transaction complete */ |
| 120 |
gkm_transaction_complete (transaction); |
| 121 |
g_assert (!gkm_transaction_get_failed (transaction)); |
| 122 |
g_assert (gkm_secret_data_get_secret (data, "my-identifier") == secret); |
| 123 |
|
| 124 |
g_object_unref (data); |
| 125 |
g_object_unref (secret); |
| 126 |
g_object_unref (transaction); |
| 127 |
} |
| 128 |
|
| 129 |
static void |
| 130 |
test_set_transacted_replace (void) |
| 131 |
{ |
| 132 |
GkmTransaction *transaction = gkm_transaction_new (); |
| 133 |
GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); |
| 134 |
GkmSecret *old = gkm_secret_new_from_password ("old"); |
| 135 |
GkmSecret *secret = gkm_secret_new_from_password ("secret"); |
| 136 |
|
| 137 |
/* The old secret */ |
| 138 |
gkm_secret_data_set_secret (data, "my-identifier", old); |
| 139 |
g_assert (gkm_secret_data_get_secret (data, "my-identifier") == old); |
| 140 |
|
| 141 |
/* Transaction, but not complete */ |
| 142 |
gkm_secret_data_set_transacted (data, transaction, "my-identifier", secret); |
| 143 |
g_assert (!gkm_transaction_get_failed (transaction)); |
| 144 |
g_assert (gkm_secret_data_get_secret (data, "my-identifier") == secret); |
| 145 |
|
| 146 |
/* Transaction complete */ |
| 147 |
gkm_transaction_complete (transaction); |
| 148 |
g_assert (!gkm_transaction_get_failed (transaction)); |
| 149 |
g_assert (gkm_secret_data_get_secret (data, "my-identifier") == secret); |
| 150 |
|
| 151 |
g_object_unref (old); |
| 152 |
g_object_unref (data); |
| 153 |
g_object_unref (secret); |
| 154 |
g_object_unref (transaction); |
| 155 |
} |
| 156 |
|
| 157 |
static void |
| 158 |
test_set_transacted_fail (void) |
| 159 |
{ |
| 160 |
GkmTransaction *transaction = gkm_transaction_new (); |
| 161 |
GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); |
| 162 |
GkmSecret *secret = gkm_secret_new_from_password ("barn"); |
| 163 |
|
| 164 |
/* Transaction, but not complete */ |
| 165 |
gkm_secret_data_set_transacted (data, transaction, "my-identifier", secret); |
| 166 |
g_assert (!gkm_transaction_get_failed (transaction)); |
| 167 |
g_assert (gkm_secret_data_get_secret (data, "my-identifier") == secret); |
| 168 |
|
| 169 |
/* Transaction fails here */ |
| 170 |
gkm_transaction_fail (transaction, CKR_CANCEL); |
| 171 |
gkm_transaction_complete (transaction); |
| 172 |
g_assert (gkm_transaction_get_failed (transaction)); |
| 173 |
g_assert (gkm_secret_data_get_secret (data, "my-identifier") == NULL); |
| 174 |
|
| 175 |
g_object_unref (data); |
| 176 |
g_object_unref (secret); |
| 177 |
g_object_unref (transaction); |
| 178 |
} |
| 179 |
|
| 180 |
static void |
| 181 |
test_set_transacted_fail_revert (void) |
| 182 |
{ |
| 183 |
GkmTransaction *transaction = gkm_transaction_new (); |
| 184 |
GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); |
| 185 |
GkmSecret *old = gkm_secret_new_from_password ("old"); |
| 186 |
GkmSecret *secret = gkm_secret_new_from_password ("secret"); |
| 187 |
|
| 188 |
/* The old secret */ |
| 189 |
gkm_secret_data_set_secret (data, "my-identifier", old); |
| 190 |
g_assert (gkm_secret_data_get_secret (data, "my-identifier") == old); |
| 191 |
|
| 192 |
/* Transaction, but not complete */ |
| 193 |
gkm_secret_data_set_transacted (data, transaction, "my-identifier", secret); |
| 194 |
g_assert (!gkm_transaction_get_failed (transaction)); |
| 195 |
g_assert (gkm_secret_data_get_secret (data, "my-identifier") == secret); |
| 196 |
|
| 197 |
/* Transaction fails here */ |
| 198 |
gkm_transaction_fail (transaction, CKR_CANCEL); |
| 199 |
gkm_transaction_complete (transaction); |
| 200 |
g_assert (gkm_transaction_get_failed (transaction)); |
| 201 |
g_assert (gkm_secret_data_get_secret (data, "my-identifier") == old); |
| 202 |
|
| 203 |
g_object_unref (old); |
| 204 |
g_object_unref (data); |
| 205 |
g_object_unref (secret); |
| 206 |
g_object_unref (transaction); |
| 207 |
} |
| 208 |
|
| 209 |
static void |
| 210 |
test_get_set_master (void) |
| 211 |
{ |
| 212 |
GkmSecretData *data = g_object_new (GKM_TYPE_SECRET_DATA, NULL); |
| 213 |
GkmSecret *master = gkm_secret_new_from_password ("master"); |
| 214 |
GkmSecret *check; |
| 215 |
|
| 216 |
gkm_secret_data_set_master (data, master); |
| 217 |
g_object_unref (master); |
| 218 |
|
| 219 |
check = gkm_secret_data_get_master (data); |
| 220 |
g_assert (GKM_IS_SECRET (check)); |
| 221 |
g_assert (master == check); |
| 222 |
g_assert (gkm_secret_equals (check, (guchar*)"master", -1)); |
| 223 |
|
| 224 |
g_object_unref (data); |
| 225 |
} |
| 226 |
|
| 227 |
int |
| 228 |
main (int argc, char **argv) |
| 229 |
{ |
| 230 |
#if !GLIB_CHECK_VERSION(2,35,0) |
| 231 |
g_type_init (); |
| 232 |
#endif |
| 233 |
g_test_init (&argc, &argv, NULL); |
| 234 |
|
| 235 |
g_test_add_func ("/secret-store/data/new", test_new); |
| 236 |
g_test_add_func ("/secret-store/data/get_set", test_get_set); |
| 237 |
g_test_add_func ("/secret-store/data/get_raw", test_get_raw); |
| 238 |
g_test_add_func ("/secret-store/data/remove", test_remove); |
| 239 |
g_test_add_func ("/secret-store/data/set_transacted", test_set_transacted); |
| 240 |
g_test_add_func ("/secret-store/data/set_transacted_replace", test_set_transacted_replace); |
| 241 |
g_test_add_func ("/secret-store/data/set_transacted_fail", test_set_transacted_fail); |
| 242 |
g_test_add_func ("/secret-store/data/set_transacted_fail_revert", test_set_transacted_fail_revert); |
| 243 |
g_test_add_func ("/secret-store/data/get_set_master", test_get_set_master); |
| 244 |
|
| 245 |
return g_test_run (); |
| 246 |
} |