Use upstream tag
[platform/upstream/libsecret.git] / egg / test-secmem.c
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 #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 EGG_SECURE_DEFINE_GLIB_GLOBALS ();
35
36 /* Declared in egg-secure-memory.c */
37 extern int egg_secure_warnings;
38
39 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 find_non_zero (gpointer mem, gsize len)
56 {
57         guchar *b, *e;
58         gsize sz = 0;
59         for (b = (guchar*)mem, e = ((guchar*)mem) + len; b != e; ++b, ++sz) {
60                 if (*b != 0x00)
61                         return sz;
62         }
63
64         return G_MAXSIZE;
65 }
66
67 static void
68 test_alloc_free (void)
69 {
70         gpointer p;
71         gboolean ret;
72
73         p = egg_secure_alloc_full ("tests", 512, 0);
74         g_assert (p != NULL);
75         g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 512));
76
77         memset (p, 0x67, 512);
78
79         ret = egg_secure_check (p);
80         g_assert (ret == TRUE);
81
82         egg_secure_free_full (p, 0);
83 }
84
85 static void
86 test_realloc_across (void)
87 {
88         gpointer p, p2;
89
90         /* Tiny allocation */
91         p = egg_secure_realloc_full ("tests", NULL, 1088, 0);
92         g_assert (p != NULL);
93         g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 1088));
94
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));
99
100         egg_secure_free (p2);
101 }
102
103 static void
104 test_alloc_two (void)
105 {
106         gpointer p, p2;
107         gboolean ret;
108
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));
112
113         memset (p2, 0x67, 4);
114
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));
118
119         memset (p, 0x67, 16200);
120
121         ret = egg_secure_check (p);
122         g_assert (ret == TRUE);
123
124         egg_secure_free_full (p2, 0);
125         egg_secure_free_full (p, 0);
126 }
127
128 static void
129 test_realloc (void)
130 {
131         gchar *str = "a test string to see if realloc works properly";
132         gpointer p, p2;
133         gsize len;
134
135         len = strlen (str) + 1;
136
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));
140
141         strcpy ((gchar*)p, str);
142
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));
146
147         g_assert (strcmp (p2, str) == 0);
148
149         p = egg_secure_realloc_full ("tests", p2, 0, 0);
150         g_assert (p == NULL);
151 }
152
153 static void
154 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         g_random_set_seed (15);
163         memory = g_ptr_array_new ();
164
165         /* Don't print "can't allocate" warnings */
166         egg_secure_warnings = 0;
167
168         for (i = 0; TRUE; ++i) {
169
170                 /* Determine what we want to do */
171                 if (memory->len > 0) {
172                         if (i > 100000) /* Once we've done 100000 alocations start freeing */
173                                 action = 2;
174                         else
175                                 action = g_random_int_range (0, 3);
176                 } else {
177                         action = 0; /* No allocations, so allocate */
178                 }
179
180                 switch (action) {
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);
187                         break;
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;
197                         break;
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);
203                         break;
204                 default:
205                         g_assert_not_reached ();
206                 }
207
208                 egg_secure_validate ();
209
210                 if (i > 100000 && !memory->len)
211                         break;
212         }
213
214         g_assert (memory->len == 0);
215         g_ptr_array_free (memory, TRUE);
216
217         egg_secure_warnings = 1;
218 }
219
220 static void
221 test_clear (void)
222 {
223         gpointer p;
224
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);
229
230         egg_secure_clear (p, 188);
231         g_assert (memchr (p, 0x89, 188) == NULL);
232
233         egg_secure_free_full (p, 0);
234 }
235
236 static void
237 test_strclear (void)
238 {
239         gchar *str;
240
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);
245
246         egg_secure_strclear (str);
247         g_assert_cmpuint (strlen (str), ==, 6);
248         g_assert (strchr (str, 't') == NULL);
249
250         egg_secure_free_full (str, 0);
251 }
252
253 int
254 main (int argc, char **argv)
255 {
256         g_test_init (&argc, &argv, NULL);
257
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);
265
266         return g_test_run ();
267 }