11typedef bool test_func();
13static int test_no = 0;
19static const char seed1[] =
"This is a test";
20static const char seed2[] =
"Lorem ipsum dolor sit amet";
22static const uint64_t counter1 = 0;
23static const uint64_t counter2 = 123456;
24static const uint64_t counter3 = 987654321123456789;
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
38#define RUN_TEST(x) run_test(#x, &x)
40static void run_test(
const char*
name, test_func* func) {
41 printf(
"[%2i] %-40s ... ", ++test_no,
name);
42 printf(func() ?
"PASSED\n" :
"SKIPPED\n");
45static bool test_alloc() {
46 ctx_int = hashx_alloc(HASHX_TYPE_INTERPRETED);
47 assert(ctx_int != NULL);
51static bool test_free() {
58static bool test_make1() {
59 hashx_result result = hashx_make(ctx_int, seed1,
sizeof(seed1));
60 assert(result == HASHX_OK);
64static bool test_hash_ctr1() {
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);
75 assert(equals_hex(hash,
"aebdd50aa67c93afb82a4c534603b65e46decd584c55161c526ebc099415ccf1"));
82static bool test_hash_ctr2() {
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"));
97static bool test_make2() {
98 hashx_result result = hashx_make(ctx_int, seed2,
sizeof(seed2));
99 assert(result == HASHX_OK);
103static bool test_hash_ctr3() {
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"));
118static bool test_hash_ctr4() {
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"));
133static bool test_hash_block1() {
137#ifndef HASHX_BLOCK_MODE
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"));
148static bool test_alloc_compiler() {
149 ctx_cmp = hashx_alloc(HASHX_TYPE_COMPILED);
150 assert(ctx_cmp != NULL);
154static bool test_make3() {
155 hashx_result result = hashx_make(ctx_cmp, seed2,
sizeof(seed2));
156 if (result == HASHX_FAIL_COMPILE) {
159 assert(result == HASHX_OK);
163static bool test_compiler_ctr1() {
164#ifndef HASHX_BLOCK_MODE
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) {
174 assert(result == HASHX_OK);
175 assert(hashes_equal(hash1, hash2));
182static bool test_compiler_ctr2() {
183#ifndef HASHX_BLOCK_MODE
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) {
193 assert(result == HASHX_OK);
194 assert(hashes_equal(hash1, hash2));
201static bool test_compiler_block1() {
202#ifndef HASHX_BLOCK_MODE
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) {
214 assert(result == HASHX_OK);
215 assert(hashes_equal(hash1, hash2));
220static bool test_alloc_automatic() {
221 ctx_auto = hashx_alloc(HASHX_TRY_COMPILE);
222 assert(ctx_auto != NULL);
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;
237static bool test_bad_seeds() {
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);
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);
273 printf(
"\nAll tests were successful\n");
int main(int argc, char *argv[])