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