Line data Source code
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
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 <stef@memberwebs.com>
21 : */
22 :
23 : #include "config.h"
24 :
25 : #include "egg/egg-secure-memory.h"
26 :
27 : #include <glib.h>
28 :
29 : #include <stdlib.h>
30 : #include <stdio.h>
31 : #include <string.h>
32 :
33 :
34 433936 : EGG_SECURE_DEFINE_GLIB_GLOBALS ();
35 :
36 : /* Declared in egg-secure-memory.c */
37 : extern int egg_secure_warnings;
38 :
39 66729 : EGG_SECURE_DECLARE (tests);
40 :
41 : /*
42 : * Each test looks like (on one line):
43 : * void unit_test_xxxxx (CuTest* cu)
44 : *
45 : * Each setup looks like (on one line):
46 : * void unit_setup_xxxxx (void);
47 : *
48 : * Each teardown looks like (on one line):
49 : * void unit_teardown_xxxxx (void);
50 : *
51 : * Tests be run in the order specified here.
52 : */
53 :
54 : static gsize
55 7 : find_non_zero (gpointer mem, gsize len)
56 : {
57 : guchar *b, *e;
58 7 : gsize sz = 0;
59 34523 : for (b = (guchar*)mem, e = ((guchar*)mem) + len; b != e; ++b, ++sz) {
60 34516 : if (*b != 0x00)
61 0 : return sz;
62 : }
63 :
64 7 : return G_MAXSIZE;
65 : }
66 :
67 : static void
68 1 : test_alloc_free (void)
69 : {
70 : gpointer p;
71 : gboolean ret;
72 :
73 1 : p = egg_secure_alloc_full ("tests", 512, 0);
74 1 : g_assert (p != NULL);
75 1 : g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 512));
76 :
77 1 : memset (p, 0x67, 512);
78 :
79 1 : ret = egg_secure_check (p);
80 1 : g_assert (ret == TRUE);
81 :
82 1 : egg_secure_free_full (p, 0);
83 1 : }
84 :
85 : static void
86 1 : test_realloc_across (void)
87 : {
88 : gpointer p, p2;
89 :
90 : /* Tiny allocation */
91 1 : p = egg_secure_realloc_full ("tests", NULL, 1088, 0);
92 1 : g_assert (p != NULL);
93 1 : g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 1088));
94 :
95 : /* Reallocate to a large one, will have to have changed blocks */
96 1 : p2 = egg_secure_realloc_full ("tests", p, 16200, 0);
97 1 : g_assert (p2 != NULL);
98 1 : g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p2, 16200));
99 :
100 1 : egg_secure_free (p2);
101 1 : }
102 :
103 : static void
104 1 : test_alloc_two (void)
105 : {
106 : gpointer p, p2;
107 : gboolean ret;
108 :
109 1 : p2 = egg_secure_alloc_full ("tests", 4, 0);
110 1 : g_assert (p2 != NULL);
111 1 : g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p2, 4));
112 :
113 1 : memset (p2, 0x67, 4);
114 :
115 1 : p = egg_secure_alloc_full ("tests", 16200, 0);
116 1 : g_assert (p != NULL);
117 1 : g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 16200));
118 :
119 1 : memset (p, 0x67, 16200);
120 :
121 1 : ret = egg_secure_check (p);
122 1 : g_assert (ret == TRUE);
123 :
124 1 : egg_secure_free_full (p2, 0);
125 1 : egg_secure_free_full (p, 0);
126 1 : }
127 :
128 : static void
129 1 : test_realloc (void)
130 : {
131 1 : gchar *str = "a test string to see if realloc works properly";
132 : gpointer p, p2;
133 : gsize len;
134 :
135 1 : len = strlen (str) + 1;
136 :
137 1 : p = egg_secure_realloc_full ("tests", NULL, len, 0);
138 1 : g_assert (p != NULL);
139 1 : g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, len));
140 :
141 1 : strcpy ((gchar*)p, str);
142 :
143 1 : p2 = egg_secure_realloc_full ("tests", p, 512, 0);
144 1 : g_assert (p2 != NULL);
145 1 : g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (((gchar*)p2) + len, 512 - len));
146 :
147 1 : g_assert (strcmp (p2, str) == 0);
148 :
149 1 : p = egg_secure_realloc_full ("tests", p2, 0, 0);
150 1 : g_assert (p == NULL);
151 1 : }
152 :
153 : static void
154 1 : test_multialloc (void)
155 : {
156 : GPtrArray *memory;
157 : gpointer data;
158 : gsize size;
159 : int i, action, index;
160 :
161 : /* A predetermined seed to get a predetermined pattern */
162 1 : g_random_set_seed (15);
163 1 : memory = g_ptr_array_new ();
164 :
165 : /* Don't print "can't allocate" warnings */
166 1 : egg_secure_warnings = 0;
167 :
168 100174 : for (i = 0; TRUE; ++i) {
169 :
170 : /* Determine what we want to do */
171 100174 : if (memory->len > 0) {
172 100018 : if (i > 100000) /* Once we've done 100000 alocations start freeing */
173 173 : action = 2;
174 : else
175 99845 : action = g_random_int_range (0, 3);
176 : } else {
177 156 : action = 0; /* No allocations, so allocate */
178 : }
179 :
180 100174 : switch (action) {
181 33449 : case 0: /* Allocate some memory */
182 33449 : size = g_random_int_range (1, 16384);
183 33449 : data = egg_secure_alloc (size);
184 33449 : g_assert (data != NULL);
185 33449 : memset (data, 0xCAFEBABE, size);
186 33449 : g_ptr_array_add (memory, data);
187 33449 : break;
188 33276 : case 1: /* Reallocate some memory */
189 33276 : index = g_random_int_range (0, memory->len);
190 33276 : data = g_ptr_array_index (memory, index);
191 33276 : g_assert (data != NULL);
192 33276 : size = g_random_int_range (1, 16384);
193 33276 : data = egg_secure_realloc (data, size);
194 33276 : g_assert (data != NULL);
195 33276 : memset (data, 0xCAFEBABE, size);
196 33276 : g_ptr_array_index (memory, index) = data;
197 33276 : break;
198 33449 : case 2: /* Free some memory */
199 33449 : index = g_random_int_range (0, memory->len);
200 33449 : data = g_ptr_array_remove_index_fast (memory, index);
201 33449 : g_assert (data != NULL);
202 33449 : egg_secure_free (data);
203 33449 : break;
204 0 : default:
205 0 : g_assert_not_reached ();
206 : }
207 :
208 100174 : egg_secure_validate ();
209 :
210 100174 : if (i > 100000 && !memory->len)
211 1 : break;
212 : }
213 :
214 1 : g_assert (memory->len == 0);
215 1 : for (i = 0; i < memory->len; i++)
216 0 : egg_secure_free (memory->pdata[i]);
217 1 : g_ptr_array_free (memory, TRUE);
218 :
219 1 : egg_secure_warnings = 1;
220 1 : }
221 :
222 : static void
223 1 : test_clear (void)
224 : {
225 : gpointer p;
226 :
227 1 : p = egg_secure_alloc_full ("tests", 188, 0);
228 1 : g_assert (p != NULL);
229 1 : memset (p, 0x89, 188);
230 1 : g_assert (memchr (p, 0x89, 188) == p);
231 :
232 1 : egg_secure_clear (p, 188);
233 1 : g_assert (memchr (p, 0x89, 188) == NULL);
234 :
235 1 : egg_secure_free_full (p, 0);
236 1 : }
237 :
238 : static void
239 1 : test_strclear (void)
240 : {
241 : gchar *str;
242 :
243 1 : str = egg_secure_strdup ("secret");
244 1 : g_assert (str != NULL);
245 1 : g_assert_cmpuint (strlen (str), ==, 6);
246 1 : g_assert (strchr (str, 't') == str + 5);
247 :
248 1 : egg_secure_strclear (str);
249 1 : g_assert_cmpuint (strlen (str), ==, 6);
250 1 : g_assert (strchr (str, 't') == NULL);
251 :
252 1 : egg_secure_free_full (str, 0);
253 1 : }
254 :
255 : static void
256 1 : test_strndup (void)
257 : {
258 : gchar *str;
259 :
260 1 : str = egg_secure_strndup ("secret", 6);
261 1 : g_assert (str != NULL);
262 1 : g_assert_cmpuint (strlen (str), ==, 6);
263 :
264 1 : egg_secure_free_full (str, 0);
265 :
266 1 : str = egg_secure_strndup ("secret", 10);
267 1 : g_assert (str != NULL);
268 1 : g_assert_cmpuint (strlen (str), ==, 6);
269 :
270 1 : egg_secure_free_full (str, 0);
271 :
272 1 : str = egg_secure_strndup ("long secret", 6);
273 1 : g_assert (str != NULL);
274 1 : g_assert_cmpuint (strlen (str), ==, 6);
275 :
276 1 : egg_secure_free_full (str, 0);
277 1 : }
278 :
279 : int
280 1 : main (int argc, char **argv)
281 : {
282 1 : g_test_init (&argc, &argv, NULL);
283 :
284 1 : g_test_add_func ("/secmem/alloc_free", test_alloc_free);
285 1 : g_test_add_func ("/secmem/realloc_across", test_realloc_across);
286 1 : g_test_add_func ("/secmem/alloc_two", test_alloc_two);
287 1 : g_test_add_func ("/secmem/realloc", test_realloc);
288 1 : g_test_add_func ("/secmem/multialloc", test_multialloc);
289 1 : g_test_add_func ("/secmem/clear", test_clear);
290 1 : g_test_add_func ("/secmem/strclear", test_strclear);
291 1 : g_test_add_func ("/secmem/strndup", test_strndup);
292 :
293 1 : return g_test_run ();
294 : }
|