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 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Author: Stef Walter <stef@memberwebs.com>
26 #include "egg/egg-secure-memory.h"
35 EGG_SECURE_DEFINE_GLIB_GLOBALS ();
37 /* Declared in egg-secure-memory.c */
38 extern int egg_secure_warnings;
40 EGG_SECURE_DECLARE (tests);
43 * Each test looks like (on one line):
44 * void unit_test_xxxxx (CuTest* cu)
46 * Each setup looks like (on one line):
47 * void unit_setup_xxxxx (void);
49 * Each teardown looks like (on one line):
50 * void unit_teardown_xxxxx (void);
52 * Tests be run in the order specified here.
56 find_non_zero (gpointer mem, gsize len)
60 for (b = (guchar*)mem, e = ((guchar*)mem) + len; b != e; ++b, ++sz) {
69 test_alloc_free (void)
74 p = egg_secure_alloc_full ("tests", 512, 0);
76 g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 512));
78 memset (p, 0x67, 512);
80 ret = egg_secure_check (p);
81 g_assert (ret == TRUE);
83 egg_secure_free_full (p, 0);
87 test_realloc_across (void)
92 p = egg_secure_realloc_full ("tests", NULL, 1088, 0);
94 g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 1088));
96 /* Reallocate to a large one, will have to have changed blocks */
97 p2 = egg_secure_realloc_full ("tests", p, 16200, 0);
98 g_assert (p2 != NULL);
99 g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p2, 16200));
101 egg_secure_free (p2);
105 test_alloc_two (void)
110 p2 = egg_secure_alloc_full ("tests", 4, 0);
111 g_assert (p2 != NULL);
112 g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p2, 4));
114 memset (p2, 0x67, 4);
116 p = egg_secure_alloc_full ("tests", 16200, 0);
117 g_assert (p != NULL);
118 g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 16200));
120 memset (p, 0x67, 16200);
122 ret = egg_secure_check (p);
123 g_assert (ret == TRUE);
125 egg_secure_free_full (p2, 0);
126 egg_secure_free_full (p, 0);
132 gchar *str = "a test string to see if realloc works properly";
136 len = strlen (str) + 1;
138 p = egg_secure_realloc_full ("tests", NULL, len, 0);
139 g_assert (p != NULL);
140 g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, len));
142 strcpy ((gchar*)p, str);
144 p2 = egg_secure_realloc_full ("tests", p, 512, 0);
145 g_assert (p2 != NULL);
146 g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (((gchar*)p2) + len, 512 - len));
148 g_assert (strcmp (p2, str) == 0);
150 p = egg_secure_realloc_full ("tests", p2, 0, 0);
151 g_assert (p == NULL);
155 test_multialloc (void)
160 int i, action, index;
162 /* A predetermined seed to get a predetermined pattern */
163 g_random_set_seed (15);
164 memory = g_ptr_array_new ();
166 /* Don't print "can't allocate" warnings */
167 egg_secure_warnings = 0;
169 for (i = 0; TRUE; ++i) {
171 /* Determine what we want to do */
172 if (memory->len > 0) {
173 if (i > 100000) /* Once we've done 100000 alocations start freeing */
176 action = g_random_int_range (0, 3);
178 action = 0; /* No allocations, so allocate */
182 case 0: /* Allocate some memory */
183 size = g_random_int_range (1, 16384);
184 data = egg_secure_alloc (size);
185 g_assert (data != NULL);
186 memset (data, 0xCAFEBABE, size);
187 g_ptr_array_add (memory, data);
189 case 1: /* Reallocate some memory */
190 index = g_random_int_range (0, memory->len);
191 data = g_ptr_array_index (memory, index);
192 g_assert (data != NULL);
193 size = g_random_int_range (1, 16384);
194 data = egg_secure_realloc (data, size);
195 g_assert (data != NULL);
196 memset (data, 0xCAFEBABE, size);
197 g_ptr_array_index (memory, index) = data;
199 case 2: /* Free some memory */
200 index = g_random_int_range (0, memory->len);
201 data = g_ptr_array_remove_index_fast (memory, index);
202 g_assert (data != NULL);
203 egg_secure_free (data);
206 g_assert_not_reached ();
209 egg_secure_validate ();
211 if (i > 100000 && !memory->len)
215 g_assert (memory->len == 0);
216 g_ptr_array_free (memory, TRUE);
218 egg_secure_warnings = 1;
226 p = egg_secure_alloc_full ("tests", 188, 0);
227 g_assert (p != NULL);
228 memset (p, 0x89, 188);
229 g_assert (memchr (p, 0x89, 188) == p);
231 egg_secure_clear (p, 188);
232 g_assert (memchr (p, 0x89, 188) == NULL);
234 egg_secure_free_full (p, 0);
242 str = egg_secure_strdup ("secret");
243 g_assert (str != NULL);
244 g_assert_cmpuint (strlen (str), ==, 6);
245 g_assert (strchr (str, 't') == str + 5);
247 egg_secure_strclear (str);
248 g_assert_cmpuint (strlen (str), ==, 6);
249 g_assert (strchr (str, 't') == NULL);
251 egg_secure_free_full (str, 0);
255 main (int argc, char **argv)
257 g_test_init (&argc, &argv, NULL);
259 g_test_add_func ("/secmem/alloc_free", test_alloc_free);
260 g_test_add_func ("/secmem/realloc_across", test_realloc_across);
261 g_test_add_func ("/secmem/alloc_two", test_alloc_two);
262 g_test_add_func ("/secmem/realloc", test_realloc);
263 g_test_add_func ("/secmem/multialloc", test_multialloc);
264 g_test_add_func ("/secmem/clear", test_clear);
265 g_test_add_func ("/secmem/strclear", test_strclear);
267 return g_test_run ();