| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
| 2 |
/* test-secret.c: Test gkm-secret.c |
| 3 |
|
| 4 |
Copyright (C) 2007 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 <stdlib.h> |
| 26 |
#include <stdio.h> |
| 27 |
#include <string.h> |
| 28 |
|
| 29 |
#include "gkm/gkm-secret.h" |
| 30 |
|
| 31 |
#include "egg/egg-secure-memory.h" |
| 32 |
|
| 33 |
EGG_SECURE_DEFINE_GLIB_GLOBALS (); |
| 34 |
|
| 35 |
static void |
| 36 |
test_secret (void) |
| 37 |
{ |
| 38 |
GkmSecret *secret; |
| 39 |
const gchar *password; |
| 40 |
gsize n_password; |
| 41 |
|
| 42 |
secret = gkm_secret_new ((guchar*)"test-pin", 8); |
| 43 |
g_assert (GKM_IS_SECRET (secret)); |
| 44 |
|
| 45 |
password = gkm_secret_get_password (secret, &n_password); |
| 46 |
g_assert (password); |
| 47 |
g_assert_cmpuint (n_password, ==, 8); |
| 48 |
g_assert (memcmp (password, "test-pin", 8) == 0); |
| 49 |
|
| 50 |
g_assert (gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"test-pin", 8)); |
| 51 |
g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"test-pino", 9)); |
| 52 |
g_assert (!gkm_secret_equals (secret, NULL, 0)); |
| 53 |
g_assert (gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"test-pin", -1)); |
| 54 |
g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"", 0)); |
| 55 |
|
| 56 |
g_object_unref (secret); |
| 57 |
} |
| 58 |
|
| 59 |
static void |
| 60 |
test_secret_from_login (void) |
| 61 |
{ |
| 62 |
GkmSecret *secret; |
| 63 |
const gchar *password; |
| 64 |
gsize n_password; |
| 65 |
|
| 66 |
secret = gkm_secret_new_from_login ((guchar*)"test-pin", 8); |
| 67 |
g_assert (GKM_IS_SECRET (secret)); |
| 68 |
password = gkm_secret_get_password (secret, &n_password); |
| 69 |
g_assert (password); |
| 70 |
g_assert_cmpuint (n_password, ==, 8); |
| 71 |
g_assert (memcmp (password, "test-pin", 8) == 0); |
| 72 |
g_object_unref (secret); |
| 73 |
|
| 74 |
secret = gkm_secret_new_from_login ((guchar*)"test-pin", (CK_ULONG)-1); |
| 75 |
g_assert (GKM_IS_SECRET (secret)); |
| 76 |
password = gkm_secret_get_password (secret, &n_password); |
| 77 |
g_assert (password); |
| 78 |
g_assert_cmpuint (n_password, ==, 8); |
| 79 |
g_assert (memcmp (password, "test-pin", 8) == 0); |
| 80 |
g_object_unref (secret); |
| 81 |
} |
| 82 |
|
| 83 |
static void |
| 84 |
test_null_terminated (void) |
| 85 |
{ |
| 86 |
GkmSecret *secret; |
| 87 |
const gchar *password; |
| 88 |
gsize n_password; |
| 89 |
|
| 90 |
secret = gkm_secret_new ((CK_UTF8CHAR_PTR)"null-terminated", -1); |
| 91 |
g_assert (GKM_IS_SECRET (secret)); |
| 92 |
|
| 93 |
password = gkm_secret_get_password (secret, &n_password); |
| 94 |
g_assert (password); |
| 95 |
g_assert_cmpstr (password, ==, "null-terminated"); |
| 96 |
g_assert_cmpuint (n_password, ==, strlen ("null-terminated")); |
| 97 |
|
| 98 |
g_assert (gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"null-terminated", strlen ("null-terminated"))); |
| 99 |
g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"test-pino", 9)); |
| 100 |
g_assert (!gkm_secret_equals (secret, NULL, 0)); |
| 101 |
g_assert (gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"null-terminated", -1)); |
| 102 |
g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"", 0)); |
| 103 |
|
| 104 |
g_object_unref (secret); |
| 105 |
} |
| 106 |
|
| 107 |
static void |
| 108 |
test_always_has_null (void) |
| 109 |
{ |
| 110 |
GkmSecret *secret; |
| 111 |
const guchar *memory; |
| 112 |
gsize n_memory; |
| 113 |
|
| 114 |
/* A 4 byte 'binary' secret */ |
| 115 |
secret = gkm_secret_new ((guchar*)"barn", 4); |
| 116 |
g_assert (GKM_IS_SECRET (secret)); |
| 117 |
|
| 118 |
memory = gkm_secret_get (secret, &n_memory); |
| 119 |
g_assert_cmpuint (n_memory, ==, 4); |
| 120 |
|
| 121 |
/* But it should be null-terminated anyway */ |
| 122 |
g_assert (memory[4] == 0); |
| 123 |
|
| 124 |
g_object_unref (secret); |
| 125 |
} |
| 126 |
|
| 127 |
static void |
| 128 |
test_null (void) |
| 129 |
{ |
| 130 |
GkmSecret *secret; |
| 131 |
const gchar *password; |
| 132 |
gsize n_password; |
| 133 |
|
| 134 |
secret = gkm_secret_new (NULL, 0); |
| 135 |
g_assert (GKM_IS_SECRET (secret)); |
| 136 |
|
| 137 |
password = gkm_secret_get_password (secret, &n_password); |
| 138 |
g_assert (password == NULL); |
| 139 |
g_assert_cmpuint (n_password, ==, 0); |
| 140 |
|
| 141 |
g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"null-terminated", strlen ("null-terminated"))); |
| 142 |
g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"test-pino", 9)); |
| 143 |
g_assert (gkm_secret_equals (secret, NULL, 0)); |
| 144 |
g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"null-terminated", -1)); |
| 145 |
g_assert (gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"", 0)); |
| 146 |
|
| 147 |
g_object_unref (secret); |
| 148 |
} |
| 149 |
|
| 150 |
static void |
| 151 |
test_empty (void) |
| 152 |
{ |
| 153 |
GkmSecret *secret; |
| 154 |
const gchar *password; |
| 155 |
gsize n_password; |
| 156 |
|
| 157 |
secret = gkm_secret_new ((CK_UTF8CHAR_PTR)"", 0); |
| 158 |
g_assert (GKM_IS_SECRET (secret)); |
| 159 |
|
| 160 |
password = gkm_secret_get_password (secret, &n_password); |
| 161 |
g_assert_cmpstr (password, ==, ""); |
| 162 |
g_assert_cmpuint (n_password, ==, 0); |
| 163 |
|
| 164 |
g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"null-terminated", strlen ("null-terminated"))); |
| 165 |
g_assert (!gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"test-pino", 9)); |
| 166 |
g_assert (gkm_secret_equals (secret, NULL, 0)); |
| 167 |
g_assert (gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"", -1)); |
| 168 |
g_assert (gkm_secret_equals (secret, (CK_UTF8CHAR_PTR)"", 0)); |
| 169 |
|
| 170 |
g_object_unref (secret); |
| 171 |
} |
| 172 |
|
| 173 |
static void |
| 174 |
test_equal (void) |
| 175 |
{ |
| 176 |
GkmSecret *one; |
| 177 |
GkmSecret *two; |
| 178 |
|
| 179 |
one = two = gkm_secret_new ((CK_UTF8CHAR_PTR)"funny", 5); |
| 180 |
g_assert (gkm_secret_equal (one, two)); |
| 181 |
|
| 182 |
two = gkm_secret_new_from_password ("funny"); |
| 183 |
g_assert (gkm_secret_equal (one, two)); |
| 184 |
|
| 185 |
g_object_unref (one); |
| 186 |
one = gkm_secret_new_from_password ("other"); |
| 187 |
g_assert (!gkm_secret_equal (one, two)); |
| 188 |
|
| 189 |
g_object_unref (one); |
| 190 |
g_object_unref (two); |
| 191 |
} |
| 192 |
|
| 193 |
int |
| 194 |
main (int argc, char **argv) |
| 195 |
{ |
| 196 |
#if !GLIB_CHECK_VERSION(2,35,0) |
| 197 |
g_type_init (); |
| 198 |
#endif |
| 199 |
g_test_init (&argc, &argv, NULL); |
| 200 |
|
| 201 |
g_test_add_func ("/gkm/secret/secret", test_secret); |
| 202 |
g_test_add_func ("/gkm/secret/secret_from_login", test_secret_from_login); |
| 203 |
g_test_add_func ("/gkm/secret/null_terminated", test_null_terminated); |
| 204 |
g_test_add_func ("/gkm/secret/always_has_null", test_always_has_null); |
| 205 |
g_test_add_func ("/gkm/secret/null", test_null); |
| 206 |
g_test_add_func ("/gkm/secret/empty", test_empty); |
| 207 |
g_test_add_func ("/gkm/secret/equal", test_equal); |
| 208 |
|
| 209 |
return g_test_run (); |
| 210 |
} |