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