minimal build
[platform/upstream/gcr.git] / gcr / tests / test-secure-memory.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* unit-test-memory.c: Test memory allocation functionality
3
4    Copyright (C) 2007 Stefan Walter
5    Copyright (C) 2012 Red Hat Inc.
6
7    The Gnome Keyring Library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11
12    The Gnome Keyring Library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public
18    License along with the Gnome Library; see the file COPYING.LIB.  If not,
19    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.
21
22    Author: Stef Walter <stefw@gnome.org>
23 */
24
25 #include "config.h"
26
27 #include "gcr/gcr-secure-memory.h"
28
29 #include <glib.h>
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #define IS_ZERO -1
36
37 static int
38 find_non_zero (gpointer mem, gsize len)
39 {
40         guchar *b, *e;
41         gsize sz = 0;
42         for (b = (guchar*)mem, e = ((guchar*)mem) + len; b != e; ++b, ++sz) {
43                 if (*b != 0x00)
44                         return (int)sz;
45         }
46
47         return -1;
48 }
49
50 static void
51 test_alloc_free (void)
52 {
53         gpointer p;
54         gboolean ret;
55
56         p = gcr_secure_memory_alloc (512);
57         g_assert (p != NULL);
58         g_assert_cmpint (IS_ZERO, ==, find_non_zero (p, 512));
59
60         memset (p, 0x67, 512);
61
62         ret = gcr_secure_memory_is_secure (p);
63         g_assert (ret == TRUE);
64
65         gcr_secure_memory_free (p);
66 }
67
68 static void
69 test_alloc_two (void)
70 {
71         gpointer p, p2;
72         gboolean ret;
73
74         p2 = gcr_secure_memory_alloc (4);
75         g_assert(p2 != NULL);
76         g_assert_cmpint (IS_ZERO, ==, find_non_zero (p2, 4));
77
78         memset (p2, 0x67, 4);
79
80         p = gcr_secure_memory_alloc (16200);
81         g_assert (p != NULL);
82         g_assert_cmpint (IS_ZERO, ==, find_non_zero (p, 16200));
83
84         memset (p, 0x67, 16200);
85
86         ret = gcr_secure_memory_is_secure (p);
87         g_assert (ret == TRUE);
88
89         gcr_secure_memory_free (p2);
90         gcr_secure_memory_free (p);
91 }
92
93 static void
94 test_realloc (void)
95 {
96         gchar *str = "a test string to see if realloc works properly";
97         gpointer p, p2;
98         gsize len;
99
100         len = strlen (str) + 1;
101
102         p = gcr_secure_memory_realloc (NULL, len);
103         g_assert (p != NULL);
104         g_assert_cmpint (IS_ZERO, ==, find_non_zero (p, len));
105
106         strcpy ((gchar*)p, str);
107
108         p2 = gcr_secure_memory_realloc (p, 512);
109         g_assert (p2 != NULL);
110
111         /* "strings not equal after realloc" */
112         g_assert_cmpstr (p2, ==, str);
113
114         p = gcr_secure_memory_realloc (p2, 0);
115         /* "should have freed memory" */
116         g_assert (p == NULL);
117 }
118
119 static void
120 test_realloc_across (void)
121 {
122         gpointer p, p2;
123
124         /* Tiny allocation */
125         p = gcr_secure_memory_realloc (NULL, 1088);
126         g_assert (p != NULL);
127         g_assert_cmpint (IS_ZERO, ==, find_non_zero (p, 1088));
128
129         /* Reallocate to a large one, will have to have changed blocks */
130         p2 = gcr_secure_memory_realloc (p, 16200);
131         g_assert (p2 != NULL);
132         g_assert_cmpint (IS_ZERO, ==, find_non_zero (p2, 16200));
133
134         gcr_secure_memory_free (p2);
135 }
136
137 int
138 main (int argc, char **argv)
139 {
140         g_test_init (&argc, &argv, NULL);
141         g_set_prgname ("test-memory");
142
143         g_test_add_func ("/memory/alloc-free", test_alloc_free);
144         g_test_add_func ("/memory/alloc-two", test_alloc_two);
145         g_test_add_func ("/memory/realloc", test_realloc);
146         g_test_add_func ("/memory/realloc-across", test_realloc_across);
147
148         return g_test_run ();
149 }