1#include <base/mem.h>
2#include <base/net.h>
3#include <base/secure.h>
4
5#include <gtest/gtest.h>
6
7static bool mem_is_null(const void *block, size_t size)
8{
9 const unsigned char *bytes = (const unsigned char *)block;
10 size_t i;
11 for(i = 0; i < size; i++)
12 {
13 if(bytes[i] != 0)
14 {
15 return false;
16 }
17 }
18 return true;
19}
20
21TEST(Memory, BaseTypes)
22{
23 void *aVoid[123];
24 secure_random_fill(bytes: aVoid, length: sizeof(aVoid));
25 EXPECT_FALSE(mem_is_null(aVoid, sizeof(aVoid)));
26 mem_zero(block: &aVoid, size: sizeof(aVoid));
27 EXPECT_TRUE(mem_is_null(aVoid, sizeof(aVoid)));
28 secure_random_fill(bytes: aVoid, length: sizeof(aVoid));
29 EXPECT_FALSE(mem_is_null(aVoid, sizeof(aVoid)));
30 mem_zero(block: &aVoid[0], size: 123 * sizeof(void *));
31 EXPECT_TRUE(mem_is_null(aVoid, sizeof(aVoid)));
32
33 secure_random_fill(bytes: aVoid, length: sizeof(aVoid));
34 EXPECT_FALSE(mem_is_null(aVoid, sizeof(aVoid)));
35 mem_zero(block: aVoid, size: sizeof(aVoid));
36 EXPECT_TRUE(mem_is_null(aVoid, sizeof(aVoid)));
37
38 int aInt[512];
39 secure_random_fill(bytes: aInt, length: sizeof(aInt));
40 EXPECT_FALSE(mem_is_null(aInt, sizeof(aInt)));
41 mem_zero(block: &aInt, size: sizeof(aInt));
42 EXPECT_TRUE(mem_is_null(aInt, sizeof(aInt)));
43 secure_random_fill(bytes: aInt, length: sizeof(aInt));
44 EXPECT_FALSE(mem_is_null(aInt, sizeof(aInt)));
45 mem_zero(block: &aInt[0], size: sizeof(aInt));
46 EXPECT_TRUE(mem_is_null(aInt, sizeof(aInt)));
47
48 secure_random_fill(bytes: aInt, length: sizeof(aInt));
49 EXPECT_FALSE(mem_is_null(aInt, sizeof(aInt)));
50 mem_zero(block: aInt, size: sizeof(aInt));
51 EXPECT_TRUE(mem_is_null(aInt, sizeof(aInt)));
52
53 int *apInt[512];
54 secure_random_fill(bytes: apInt, length: sizeof(apInt));
55 EXPECT_FALSE(mem_is_null(apInt, sizeof(apInt)));
56 mem_zero(block: &apInt, size: sizeof(apInt));
57 EXPECT_TRUE(mem_is_null(apInt, sizeof(apInt)));
58
59 secure_random_fill(bytes: apInt, length: sizeof(apInt));
60 EXPECT_FALSE(mem_is_null(apInt, sizeof(apInt)));
61 mem_zero(block: apInt, size: sizeof(apInt));
62 EXPECT_TRUE(mem_is_null(apInt, sizeof(apInt)));
63
64 int aaInt[10][20];
65 secure_random_fill(bytes: aaInt, length: sizeof(aaInt));
66 EXPECT_FALSE(mem_is_null(aaInt, sizeof(aaInt)));
67 mem_zero(block: &aaInt, size: sizeof(aaInt));
68 EXPECT_TRUE(mem_is_null(aaInt, sizeof(aaInt)));
69 secure_random_fill(bytes: aaInt, length: sizeof(aaInt));
70 EXPECT_FALSE(mem_is_null(aaInt, sizeof(aaInt)));
71 mem_zero(block: &aaInt[0], size: sizeof(aaInt));
72 EXPECT_TRUE(mem_is_null(aaInt, sizeof(aaInt)));
73 secure_random_fill(bytes: aaInt, length: sizeof(aaInt));
74 EXPECT_FALSE(mem_is_null(aaInt, sizeof(aaInt)));
75 mem_zero(block: &aaInt[0][0], size: sizeof(aaInt));
76 EXPECT_TRUE(mem_is_null(aaInt, sizeof(aaInt)));
77
78 secure_random_fill(bytes: aaInt, length: sizeof(aaInt));
79 EXPECT_FALSE(mem_is_null(aaInt, sizeof(aaInt)));
80 mem_zero(block: aaInt, size: sizeof(aaInt));
81 EXPECT_TRUE(mem_is_null(aaInt, sizeof(aaInt)));
82 secure_random_fill(bytes: aaInt, length: sizeof(aaInt));
83 EXPECT_FALSE(mem_is_null(aaInt, sizeof(aaInt)));
84 mem_zero(block: aaInt[0], size: sizeof(aaInt));
85 EXPECT_TRUE(mem_is_null(aaInt, sizeof(aaInt)));
86
87 int *aapInt[10][20];
88 secure_random_fill(bytes: aapInt, length: sizeof(aapInt));
89 EXPECT_FALSE(mem_is_null(aapInt, sizeof(aapInt)));
90 mem_zero(block: &aapInt, size: sizeof(aapInt));
91 EXPECT_TRUE(mem_is_null(aapInt, sizeof(aapInt)));
92 secure_random_fill(bytes: aapInt, length: sizeof(aapInt));
93 EXPECT_FALSE(mem_is_null(aapInt, sizeof(aapInt)));
94 mem_zero(block: &aapInt[0], size: sizeof(aapInt));
95 EXPECT_TRUE(mem_is_null(aapInt, sizeof(aapInt)));
96
97 secure_random_fill(bytes: aapInt, length: sizeof(aapInt));
98 EXPECT_FALSE(mem_is_null(aapInt, sizeof(aapInt)));
99 mem_zero(block: aapInt, size: sizeof(aapInt));
100 EXPECT_TRUE(mem_is_null(aapInt, sizeof(aapInt)));
101 secure_random_fill(bytes: aapInt, length: sizeof(aapInt));
102 EXPECT_FALSE(mem_is_null(aapInt, sizeof(aapInt)));
103 mem_zero(block: aapInt[0], size: sizeof(aapInt));
104 EXPECT_TRUE(mem_is_null(aapInt, sizeof(aapInt)));
105}
106
107TEST(Memory, PodTypes)
108{
109 NETADDR aAddr[123];
110 secure_random_fill(bytes: aAddr, length: sizeof(aAddr));
111 EXPECT_FALSE(mem_is_null(aAddr, sizeof(aAddr)));
112 mem_zero(block: &aAddr, size: sizeof(aAddr));
113 EXPECT_TRUE(mem_is_null(aAddr, sizeof(aAddr)));
114 secure_random_fill(bytes: aAddr, length: sizeof(aAddr));
115 EXPECT_FALSE(mem_is_null(aAddr, sizeof(aAddr)));
116 mem_zero(block: &aAddr[0], size: sizeof(aAddr));
117 EXPECT_TRUE(mem_is_null(aAddr, sizeof(aAddr)));
118
119 secure_random_fill(bytes: aAddr, length: sizeof(aAddr));
120 EXPECT_FALSE(mem_is_null(aAddr, sizeof(aAddr)));
121 mem_zero(block: aAddr, size: sizeof(aAddr));
122 EXPECT_TRUE(mem_is_null(aAddr, sizeof(aAddr)));
123
124 NETADDR *apAddr[123];
125 secure_random_fill(bytes: apAddr, length: sizeof(apAddr));
126 EXPECT_FALSE(mem_is_null(apAddr, sizeof(apAddr)));
127 mem_zero(block: (NETADDR **)apAddr, size: sizeof(apAddr));
128 EXPECT_TRUE(mem_is_null(apAddr, sizeof(apAddr)));
129 secure_random_fill(bytes: apAddr, length: sizeof(apAddr));
130 EXPECT_FALSE(mem_is_null(apAddr, sizeof(apAddr)));
131 mem_zero(block: &apAddr, size: sizeof(apAddr));
132 EXPECT_TRUE(mem_is_null(apAddr, sizeof(apAddr)));
133 secure_random_fill(bytes: apAddr, length: sizeof(apAddr));
134 EXPECT_FALSE(mem_is_null(apAddr, sizeof(apAddr)));
135 mem_zero(block: &apAddr[0], size: sizeof(apAddr));
136 EXPECT_TRUE(mem_is_null(apAddr, sizeof(apAddr)));
137 secure_random_fill(bytes: apAddr, length: sizeof(apAddr));
138 EXPECT_FALSE(mem_is_null(apAddr, sizeof(apAddr)));
139 mem_zero(block: apAddr, size: sizeof(apAddr));
140 EXPECT_TRUE(mem_is_null(apAddr, sizeof(apAddr)));
141
142 // 2D arrays
143 NETADDR aaAddr[10][20];
144 secure_random_fill(bytes: aaAddr, length: sizeof(aaAddr));
145 EXPECT_FALSE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
146 mem_zero(block: &aaAddr, size: sizeof(aaAddr));
147 EXPECT_TRUE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
148 secure_random_fill(bytes: aaAddr, length: sizeof(aaAddr));
149 EXPECT_FALSE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
150 mem_zero(block: &aaAddr[0], size: sizeof(aaAddr));
151 EXPECT_TRUE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
152 secure_random_fill(bytes: aaAddr, length: sizeof(aaAddr));
153 EXPECT_FALSE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
154 mem_zero(block: &aaAddr[0][0], size: sizeof(aaAddr));
155 EXPECT_TRUE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
156
157 secure_random_fill(bytes: aaAddr, length: sizeof(aaAddr));
158 EXPECT_FALSE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
159 mem_zero(block: aaAddr, size: sizeof(aaAddr));
160 EXPECT_TRUE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
161 secure_random_fill(bytes: aaAddr, length: sizeof(aaAddr));
162 EXPECT_FALSE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
163 mem_zero(block: aaAddr[0], size: sizeof(aaAddr));
164 EXPECT_TRUE(mem_is_null((NETADDR *)aaAddr, sizeof(aaAddr)));
165
166 // 2D pointer arrays
167 NETADDR *aapAddr[10][20];
168 secure_random_fill(bytes: aapAddr, length: sizeof(aapAddr));
169 EXPECT_FALSE(mem_is_null(aapAddr, sizeof(aapAddr)));
170 mem_zero(block: &aapAddr, size: sizeof(aapAddr));
171 EXPECT_TRUE(mem_is_null(aapAddr, sizeof(aapAddr)));
172 secure_random_fill(bytes: aapAddr, length: sizeof(aapAddr));
173 EXPECT_FALSE(mem_is_null(aapAddr, sizeof(aapAddr)));
174 mem_zero(block: &aapAddr[0], size: sizeof(aapAddr));
175 EXPECT_TRUE(mem_is_null(aapAddr, sizeof(aapAddr)));
176 secure_random_fill(bytes: aapAddr, length: sizeof(aapAddr));
177 EXPECT_FALSE(mem_is_null(aapAddr, sizeof(aapAddr)));
178 mem_zero(block: &aapAddr[0][0], size: sizeof(aapAddr));
179 EXPECT_TRUE(mem_is_null(aapAddr, sizeof(aapAddr)));
180
181 secure_random_fill(bytes: aapAddr, length: sizeof(aapAddr));
182 EXPECT_FALSE(mem_is_null(aapAddr, sizeof(aapAddr)));
183 mem_zero(block: aapAddr, size: sizeof(aapAddr));
184 EXPECT_TRUE(mem_is_null(aapAddr, sizeof(aapAddr)));
185 secure_random_fill(bytes: aapAddr, length: sizeof(aapAddr));
186 EXPECT_FALSE(mem_is_null(aapAddr, sizeof(aapAddr)));
187 mem_zero(block: aapAddr[0], size: sizeof(aapAddr));
188 EXPECT_TRUE(mem_is_null(aapAddr, sizeof(aapAddr)));
189}
190