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