1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* unit-test-secmem.c: Test low level secure memory allocation functionality
4 Copyright (C) 2007 Stefan Walter
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.
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.
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 see <http://www.gnu.org/licenses/>.
20 Author: Stef Walter <stef@memberwebs.com>
25 #include "egg/egg-secure-memory.h"
34 EGG_SECURE_DEFINE_GLIB_GLOBALS ();
36 /* Declared in egg-secure-memory.c */
37 extern int egg_secure_warnings;
39 EGG_SECURE_DECLARE (tests);
42 * Each test looks like (on one line):
43 * void unit_test_xxxxx (CuTest* cu)
45 * Each setup looks like (on one line):
46 * void unit_setup_xxxxx (void);
48 * Each teardown looks like (on one line):
49 * void unit_teardown_xxxxx (void);
51 * Tests be run in the order specified here.
55 find_non_zero (gpointer mem, gsize len)
59 for (b = (guchar*)mem, e = ((guchar*)mem) + len; b != e; ++b, ++sz) {
68 test_alloc_free (void)
73 p = egg_secure_alloc_full ("tests", 512, 0);
75 g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 512));
77 memset (p, 0x67, 512);
79 ret = egg_secure_check (p);
80 g_assert (ret == TRUE);
82 egg_secure_free_full (p, 0);
86 test_realloc_across (void)
91 p = egg_secure_realloc_full ("tests", NULL, 1088, 0);
93 g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 1088));
95 /* Reallocate to a large one, will have to have changed blocks */
96 p2 = egg_secure_realloc_full ("tests", p, 16200, 0);
97 g_assert (p2 != NULL);
98 g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p2, 16200));
100 egg_secure_free (p2);
104 test_alloc_two (void)
109 p2 = egg_secure_alloc_full ("tests", 4, 0);
110 g_assert (p2 != NULL);
111 g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p2, 4));
113 memset (p2, 0x67, 4);
115 p = egg_secure_alloc_full ("tests", 16200, 0);
116 g_assert (p != NULL);
117 g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 16200));
119 memset (p, 0x67, 16200);
121 ret = egg_secure_check (p);
122 g_assert (ret == TRUE);
124 egg_secure_free_full (p2, 0);
125 egg_secure_free_full (p, 0);
131 gchar *str = "a test string to see if realloc works properly";
135 len = strlen (str) + 1;
137 p = egg_secure_realloc_full ("tests", NULL, len, 0);
138 g_assert (p != NULL);
139 g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, len));
141 strcpy ((gchar*)p, str);
143 p2 = egg_secure_realloc_full ("tests", p, 512, 0);
144 g_assert (p2 != NULL);
145 g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (((gchar*)p2) + len, 512 - len));
147 g_assert (strcmp (p2, str) == 0);
149 p = egg_secure_realloc_full ("tests", p2, 0, 0);
150 g_assert (p == NULL);
154 test_multialloc (void)
159 int i, action, index;
161 /* A predetermined seed to get a predetermined pattern */
162 g_random_set_seed (15);
163 memory = g_ptr_array_new ();
165 /* Don't print "can't allocate" warnings */
166 egg_secure_warnings = 0;
168 for (i = 0; TRUE; ++i) {
170 /* Determine what we want to do */
171 if (memory->len > 0) {
172 if (i > 100000) /* Once we've done 100000 alocations start freeing */
175 action = g_random_int_range (0, 3);
177 action = 0; /* No allocations, so allocate */
181 case 0: /* Allocate some memory */
182 size = g_random_int_range (1, 16384);
183 data = egg_secure_alloc (size);
184 g_assert (data != NULL);
185 memset (data, 0xCAFEBABE, size);
186 g_ptr_array_add (memory, data);
188 case 1: /* Reallocate some memory */
189 index = g_random_int_range (0, memory->len);
190 data = g_ptr_array_index (memory, index);
191 g_assert (data != NULL);
192 size = g_random_int_range (1, 16384);
193 data = egg_secure_realloc (data, size);
194 g_assert (data != NULL);
195 memset (data, 0xCAFEBABE, size);
196 g_ptr_array_index (memory, index) = data;
198 case 2: /* Free some memory */
199 index = g_random_int_range (0, memory->len);
200 data = g_ptr_array_remove_index_fast (memory, index);
201 g_assert (data != NULL);
202 egg_secure_free (data);
205 g_assert_not_reached ();
208 egg_secure_validate ();
210 if (i > 100000 && !memory->len)
214 g_assert (memory->len == 0);
215 g_ptr_array_free (memory, TRUE);
217 egg_secure_warnings = 1;
225 p = egg_secure_alloc_full ("tests", 188, 0);
226 g_assert (p != NULL);
227 memset (p, 0x89, 188);
228 g_assert (memchr (p, 0x89, 188) == p);
230 egg_secure_clear (p, 188);
231 g_assert (memchr (p, 0x89, 188) == NULL);
233 egg_secure_free_full (p, 0);
241 str = egg_secure_strdup ("secret");
242 g_assert (str != NULL);
243 g_assert_cmpuint (strlen (str), ==, 6);
244 g_assert (strchr (str, 't') == str + 5);
246 egg_secure_strclear (str);
247 g_assert_cmpuint (strlen (str), ==, 6);
248 g_assert (strchr (str, 't') == NULL);
250 egg_secure_free_full (str, 0);
254 main (int argc, char **argv)
256 g_test_init (&argc, &argv, NULL);
258 g_test_add_func ("/secmem/alloc_free", test_alloc_free);
259 g_test_add_func ("/secmem/realloc_across", test_realloc_across);
260 g_test_add_func ("/secmem/alloc_two", test_alloc_two);
261 g_test_add_func ("/secmem/realloc", test_realloc);
262 g_test_add_func ("/secmem/multialloc", test_multialloc);
263 g_test_add_func ("/secmem/clear", test_clear);
264 g_test_add_func ("/secmem/strclear", test_strclear);
266 return g_test_run ();