Tor 0.4.9.2-alpha-dev
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
tests.c
1/* Copyright (c) 2020 tevador <tevador@gmail.com> */
2/* See LICENSE for licensing information */
3
4#ifdef NDEBUG
5#undef NDEBUG
6#endif
7
8#include <assert.h>
9#include "test_utils.h"
10
11typedef bool test_func();
12
13static int test_no = 0;
14
15static hashx_ctx* ctx_int = NULL;
16static hashx_ctx* ctx_cmp = NULL;
17static hashx_ctx* ctx_auto = NULL;
18
19static const char seed1[] = "This is a test";
20static const char seed2[] = "Lorem ipsum dolor sit amet";
21
22static const uint64_t counter1 = 0;
23static const uint64_t counter2 = 123456;
24static const uint64_t counter3 = 987654321123456789;
25
26#ifdef HASHX_BLOCK_MODE
27static const unsigned char long_input[] = {
28 0x0b, 0x0b, 0x98, 0xbe, 0xa7, 0xe8, 0x05, 0xe0, 0x01, 0x0a, 0x21, 0x26,
29 0xd2, 0x87, 0xa2, 0xa0, 0xcc, 0x83, 0x3d, 0x31, 0x2c, 0xb7, 0x86, 0x38,
30 0x5a, 0x7c, 0x2f, 0x9d, 0xe6, 0x9d, 0x25, 0x53, 0x7f, 0x58, 0x4a, 0x9b,
31 0xc9, 0x97, 0x7b, 0x00, 0x00, 0x00, 0x00, 0x66, 0x6f, 0xd8, 0x75, 0x3b,
32 0xf6, 0x1a, 0x86, 0x31, 0xf1, 0x29, 0x84, 0xe3, 0xfd, 0x44, 0xf4, 0x01,
33 0x4e, 0xca, 0x62, 0x92, 0x76, 0x81, 0x7b, 0x56, 0xf3, 0x2e, 0x9b, 0x68,
34 0xbd, 0x82, 0xf4, 0x16
35};
36#endif
37
38#define RUN_TEST(x) run_test(#x, &x)
39
40static void run_test(const char* name, test_func* func) {
41 printf("[%2i] %-40s ... ", ++test_no, name);
42 printf(func() ? "PASSED\n" : "SKIPPED\n");
43}
44
45static bool test_alloc() {
46 ctx_int = hashx_alloc(HASHX_TYPE_INTERPRETED);
47 assert(ctx_int != NULL);
48 return true;
49}
50
51static bool test_free() {
52 hashx_free(ctx_int);
53 hashx_free(ctx_cmp);
54 hashx_free(ctx_auto);
55 return true;
56}
57
58static bool test_make1() {
59 hashx_result result = hashx_make(ctx_int, seed1, sizeof(seed1));
60 assert(result == HASHX_OK);
61 return true;
62}
63
64static bool test_hash_ctr1() {
65#ifdef HASHX_SALT
66 return false;
67#endif
68#ifndef HASHX_BLOCK_MODE
69 char hash[HASHX_SIZE];
70 hashx_result result = hashx_exec(ctx_int, counter2, hash);
71 assert(result == HASHX_OK);
72 /* printf("\n");
73 output_hex(hash, HASHX_SIZE);
74 printf("\n"); */
75 assert(equals_hex(hash, "aebdd50aa67c93afb82a4c534603b65e46decd584c55161c526ebc099415ccf1"));
76 return true;
77#else
78 return false;
79#endif
80}
81
82static bool test_hash_ctr2() {
83#ifdef HASHX_SALT
84 return false;
85#endif
86#ifndef HASHX_BLOCK_MODE
87 char hash[HASHX_SIZE];
88 hashx_result result = hashx_exec(ctx_int, counter1, hash);
89 assert(result == HASHX_OK);
90 assert(equals_hex(hash, "2b2f54567dcbea98fdb5d5e5ce9a65983c4a4e35ab1464b1efb61e83b7074bb2"));
91 return true;
92#else
93 return false;
94#endif
95}
96
97static bool test_make2() {
98 hashx_result result = hashx_make(ctx_int, seed2, sizeof(seed2));
99 assert(result == HASHX_OK);
100 return true;
101}
102
103static bool test_hash_ctr3() {
104#ifdef HASHX_SALT
105 return false;
106#endif
107#ifndef HASHX_BLOCK_MODE
108 char hash[HASHX_SIZE];
109 hashx_result result = hashx_exec(ctx_int, counter2, hash);
110 assert(result == HASHX_OK);
111 assert(equals_hex(hash, "ab3d155bf4bbb0aa3a71b7801089826186e44300e6932e6ffd287cf302bbb0ba"));
112 return true;
113#else
114 return false;
115#endif
116}
117
118static bool test_hash_ctr4() {
119#ifdef HASHX_SALT
120 return false;
121#endif
122#ifndef HASHX_BLOCK_MODE
123 char hash[HASHX_SIZE];
124 hashx_result result = hashx_exec(ctx_int, counter3, hash);
125 assert(result == HASHX_OK);
126 assert(equals_hex(hash, "8dfef0497c323274a60d1d93292b68d9a0496379ba407b4341cf868a14d30113"));
127 return true;
128#else
129 return false;
130#endif
131}
132
133static bool test_hash_block1() {
134#ifdef HASHX_SALT
135 return false;
136#endif
137#ifndef HASHX_BLOCK_MODE
138 return false;
139#else
140 char hash[HASHX_SIZE];
141 hashx_result result = hashx_exec(ctx_int, long_input, sizeof(long_input), hash);
142 assert(result == HASHX_OK);
143 assert(equals_hex(hash, "d0b232b832459501ca1ac9dc0429fd931414ead7624a457e375a43ea3e5e737a"));
144 return true;
145#endif
146}
147
148static bool test_alloc_compiler() {
149 ctx_cmp = hashx_alloc(HASHX_TYPE_COMPILED);
150 assert(ctx_cmp != NULL);
151 return true;
152}
153
154static bool test_make3() {
155 hashx_result result = hashx_make(ctx_cmp, seed2, sizeof(seed2));
156 if (result == HASHX_FAIL_COMPILE) {
157 return false;
158 }
159 assert(result == HASHX_OK);
160 return true;
161}
162
163static bool test_compiler_ctr1() {
164#ifndef HASHX_BLOCK_MODE
165 hashx_result result;
166 char hash1[HASHX_SIZE];
167 char hash2[HASHX_SIZE];
168 result = hashx_exec(ctx_int, counter2, hash1);
169 assert(result == HASHX_OK);
170 result = hashx_exec(ctx_cmp, counter2, hash2);
171 if (result == HASHX_FAIL_UNPREPARED) {
172 return false;
173 }
174 assert(result == HASHX_OK);
175 assert(hashes_equal(hash1, hash2));
176 return true;
177#else
178 return false;
179#endif
180}
181
182static bool test_compiler_ctr2() {
183#ifndef HASHX_BLOCK_MODE
184 hashx_result result;
185 char hash1[HASHX_SIZE];
186 char hash2[HASHX_SIZE];
187 result = hashx_exec(ctx_int, counter1, hash1);
188 assert(result == HASHX_OK);
189 result = hashx_exec(ctx_cmp, counter1, hash2);
190 if (result == HASHX_FAIL_UNPREPARED) {
191 return false;
192 }
193 assert(result == HASHX_OK);
194 assert(hashes_equal(hash1, hash2));
195 return true;
196#else
197 return false;
198#endif
199}
200
201static bool test_compiler_block1() {
202#ifndef HASHX_BLOCK_MODE
203 return false;
204#else
205 hashx_result result;
206 char hash1[HASHX_SIZE];
207 char hash2[HASHX_SIZE];
208 result = hashx_exec(ctx_int, long_input, sizeof(long_input), hash1);
209 assert(result == HASHX_OK);
210 result = hashx_exec(ctx_cmp, long_input, sizeof(long_input), hash2);
211 if (result == HASHX_FAIL_UNPREPARED) {
212 return false;
213 }
214 assert(result == HASHX_OK);
215 assert(hashes_equal(hash1, hash2));
216 return true;
217#endif
218}
219
220static bool test_alloc_automatic() {
221 ctx_auto = hashx_alloc(HASHX_TRY_COMPILE);
222 assert(ctx_auto != NULL);
223 return true;
224}
225
226static bool test_auto_fallback() {
227 hashx_result result = hashx_make(ctx_auto, seed2, sizeof(seed2));
228 assert(result == HASHX_OK);
229 hashx_type actual_type = (hashx_type)-1;
230 result = hashx_query_type(ctx_auto, &actual_type);
231 assert(result == HASHX_OK);
232 assert(actual_type == HASHX_TYPE_INTERPRETED ||
233 actual_type == HASHX_TYPE_COMPILED);
234 return actual_type == HASHX_TYPE_INTERPRETED;
235}
236
237static bool test_bad_seeds() {
238#ifdef HASHX_SALT
239 return false;
240#else
241 hashx_result result;
242 result = hashx_make(ctx_auto, "\xf8\x05\x00\x00", 4);
243 assert(result == HASHX_OK);
244 result = hashx_make(ctx_auto, "\xf9\x05\x00\x00", 4);
245 assert(result == HASHX_FAIL_SEED);
246 result = hashx_make(ctx_auto, "\x5d\x93\x02\x00", 4);
247 assert(result == HASHX_FAIL_SEED);
248 result = hashx_make(ctx_auto, "\x5e\x93\x02\x00", 4);
249 assert(result == HASHX_OK);
250 return true;
251#endif
252}
253
254int main() {
255 RUN_TEST(test_alloc);
256 RUN_TEST(test_make1);
257 RUN_TEST(test_hash_ctr1);
258 RUN_TEST(test_hash_ctr2);
259 RUN_TEST(test_make2);
260 RUN_TEST(test_hash_ctr3);
261 RUN_TEST(test_hash_ctr4);
262 RUN_TEST(test_alloc_compiler);
263 RUN_TEST(test_make3);
264 RUN_TEST(test_compiler_ctr1);
265 RUN_TEST(test_compiler_ctr2);
266 RUN_TEST(test_hash_block1);
267 RUN_TEST(test_compiler_block1);
268 RUN_TEST(test_alloc_automatic);
269 RUN_TEST(test_auto_fallback);
270 RUN_TEST(test_bad_seeds);
271 RUN_TEST(test_free);
272
273 printf("\nAll tests were successful\n");
274 return 0;
275}
const char * name
Definition: config.c:2471
int main(int argc, char *argv[])
Definition: tor_main.c:25