1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* unit-test-memory.c: Test memory allocation functionality
4 Copyright (C) 2007 Stefan Walter
5 Copyright (C) 2012 Red Hat Inc.
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.
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.
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.
22 Author: Stef Walter <stefw@gnome.org>
27 #include "gcr/gcr-secure-memory.h"
38 find_non_zero (gpointer mem, gsize len)
42 for (b = (guchar*)mem, e = ((guchar*)mem) + len; b != e; ++b, ++sz) {
51 test_alloc_free (void)
56 p = gcr_secure_memory_alloc (512);
58 g_assert_cmpint (IS_ZERO, ==, find_non_zero (p, 512));
60 memset (p, 0x67, 512);
62 ret = gcr_secure_memory_is_secure (p);
63 g_assert (ret == TRUE);
65 gcr_secure_memory_free (p);
74 p2 = gcr_secure_memory_alloc (4);
76 g_assert_cmpint (IS_ZERO, ==, find_non_zero (p2, 4));
80 p = gcr_secure_memory_alloc (16200);
82 g_assert_cmpint (IS_ZERO, ==, find_non_zero (p, 16200));
84 memset (p, 0x67, 16200);
86 ret = gcr_secure_memory_is_secure (p);
87 g_assert (ret == TRUE);
89 gcr_secure_memory_free (p2);
90 gcr_secure_memory_free (p);
96 gchar *str = "a test string to see if realloc works properly";
100 len = strlen (str) + 1;
102 p = gcr_secure_memory_realloc (NULL, len);
103 g_assert (p != NULL);
104 g_assert_cmpint (IS_ZERO, ==, find_non_zero (p, len));
106 strcpy ((gchar*)p, str);
108 p2 = gcr_secure_memory_realloc (p, 512);
109 g_assert (p2 != NULL);
111 /* "strings not equal after realloc" */
112 g_assert_cmpstr (p2, ==, str);
114 p = gcr_secure_memory_realloc (p2, 0);
115 /* "should have freed memory" */
116 g_assert (p == NULL);
120 test_realloc_across (void)
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));
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));
134 gcr_secure_memory_free (p2);
138 main (int argc, char **argv)
140 g_test_init (&argc, &argv, NULL);
141 g_set_prgname ("test-memory");
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);
148 return g_test_run ();