summaryrefslogtreecommitdiffstats
path: root/security/libgnome-keyring/files/patch-egg_tests_test-secmem.c
blob: 1781524d2789ee6ae63375d0e3c4dfbf3b5342f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
--- egg/tests/test-secmem.c.orig    Mon Sep  5 07:16:09 2011
+++ egg/tests/test-secmem.c Sat Oct 27 14:36:16 2012
@@ -23,7 +23,7 @@
 
 #include "config.h"
 
-#include "egg-secure-memory.h"
+#include "egg/egg-secure-memory.h"
 
 #include <glib.h>
 
@@ -31,11 +31,14 @@
 #include <stdio.h>
 #include <string.h>
 
-EGG_SECURE_GLIB_DEFINITIONS ();
 
+EGG_SECURE_DEFINE_GLIB_GLOBALS ();
+
 /* Declared in egg-secure-memory.c */
 extern int egg_secure_warnings;
 
+EGG_SECURE_DECLARE (tests);
+
 /*
  * Each test looks like (on one line):
  *     void unit_test_xxxxx (CuTest* cu)
@@ -68,7 +71,7 @@ test_alloc_free (void)
    gpointer p;
    gboolean ret;
 
-   p = egg_secure_alloc_full (512, 0);
+   p = egg_secure_alloc_full ("tests", 512, 0);
    g_assert (p != NULL);
    g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 512));
 
@@ -86,14 +89,16 @@ test_realloc_across (void)
    gpointer p, p2;
 
    /* Tiny allocation */
-   p = egg_secure_realloc_full (NULL, 1088, 0);
+   p = egg_secure_realloc_full ("tests", NULL, 1088, 0);
    g_assert (p != NULL);
    g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 1088));
 
    /* Reallocate to a large one, will have to have changed blocks */
-   p2 = egg_secure_realloc_full (p, 16200, 0);
+   p2 = egg_secure_realloc_full ("tests", p, 16200, 0);
    g_assert (p2 != NULL);
    g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p2, 16200));
+
+   egg_secure_free (p2);
 }
 
 static void
@@ -102,13 +107,13 @@ test_alloc_two (void)
    gpointer p, p2;
    gboolean ret;
 
-   p2 = egg_secure_alloc_full (4, 0);
+   p2 = egg_secure_alloc_full ("tests", 4, 0);
    g_assert (p2 != NULL);
    g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p2, 4));
 
    memset (p2, 0x67, 4);
 
-   p = egg_secure_alloc_full (16200, 0);
+   p = egg_secure_alloc_full ("tests", 16200, 0);
    g_assert (p != NULL);
    g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, 16200));
 
@@ -130,19 +135,19 @@ test_realloc (void)
 
    len = strlen (str) + 1;
 
-   p = egg_secure_realloc_full (NULL, len, 0);
+   p = egg_secure_realloc_full ("tests", NULL, len, 0);
    g_assert (p != NULL);
    g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (p, len));
 
    strcpy ((gchar*)p, str);
 
-   p2 = egg_secure_realloc_full (p, 512, 0);
+   p2 = egg_secure_realloc_full ("tests", p, 512, 0);
    g_assert (p2 != NULL);
    g_assert_cmpint (G_MAXSIZE, ==, find_non_zero (((gchar*)p2) + len, 512 - len));
 
    g_assert (strcmp (p2, str) == 0);
 
-   p = egg_secure_realloc_full (p2, 0, 0);
+   p = egg_secure_realloc_full ("tests", p2, 0, 0);
    g_assert (p == NULL);
 }
 
@@ -154,9 +159,6 @@ test_multialloc (void)
    gsize size;
    int i, action, index;
 
-   if (g_test_quick ())
-       return;
-
    /* A predetermined seed to get a predetermined pattern */
    g_random_set_seed (15);
    memory = g_ptr_array_new ();
@@ -180,26 +182,25 @@ test_multialloc (void)
        case 0: /* Allocate some memory */
            size = g_random_int_range (1, 16384);
            data = egg_secure_alloc (size);
-           g_assert (data);
+           g_assert (data != NULL);
            memset (data, 0xCAFEBABE, size);
            g_ptr_array_add (memory, data);
            break;
        case 1: /* Reallocate some memory */
            index = g_random_int_range (0, memory->len);
            data = g_ptr_array_index (memory, index);
-           g_assert (data);
+           g_assert (data != NULL);
            size = g_random_int_range (1, 16384);
            data = egg_secure_realloc (data, size);
-           g_assert (data);
+           g_assert (data != NULL);
            memset (data, 0xCAFEBABE, size);
            g_ptr_array_index (memory, index) = data;
            break;
        case 2: /* Free some memory */
            index = g_random_int_range (0, memory->len);
-           data = g_ptr_array_index (memory, index);
-           g_assert (data);
+           data = g_ptr_array_remove_index_fast (memory, index);
+           g_assert (data != NULL);
            egg_secure_free (data);
-           g_ptr_array_remove_index_fast (memory, index);
            break;
        default:
            g_assert_not_reached ();
@@ -212,6 +213,8 @@ test_multialloc (void)
    }
 
    g_assert (memory->len == 0);
+   for (i = 0; i < memory->len; i++)
+       egg_secure_free (memory->pdata[i]);
    g_ptr_array_free (memory, TRUE);
 
    egg_secure_warnings = 1;
@@ -222,7 +225,7 @@ test_clear (void)
 {
    gpointer p;
 
-   p = egg_secure_alloc_full (188, 0);
+   p = egg_secure_alloc_full ("tests", 188, 0);
    g_assert (p != NULL);
    memset (p, 0x89, 188);
    g_assert (memchr (p, 0x89, 188) == p);
@@ -255,9 +258,9 @@ main (int argc, char **argv)
 {
    g_test_init (&argc, &argv, NULL);
 
-   g_test_add_func ("/secmem/alloc-free", test_alloc_free);
-   g_test_add_func ("/secmem/realloc-across", test_realloc_across);
-   g_test_add_func ("/secmem/alloc-two", test_alloc_two);
+   g_test_add_func ("/secmem/alloc_free", test_alloc_free);
+   g_test_add_func ("/secmem/realloc_across", test_realloc_across);
+   g_test_add_func ("/secmem/alloc_two", test_alloc_two);
    g_test_add_func ("/secmem/realloc", test_realloc);
    g_test_add_func ("/secmem/multialloc", test_multialloc);
    g_test_add_func ("/secmem/clear", test_clear);