2 This file is part of GNUnet.
3 Copyright (C) 2002, 2003, 2004, 2006, 2009 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @author Christian Grothoff
21 * @file util/test_crypto_hash.c
22 * @brief Test for crypto_hash.c
25 #include "gnunet_util_lib.h"
27 static char block[65536];
29 #define FILENAME "testblock.dat"
34 struct GNUNET_HashCode h1;
35 struct GNUNET_HashCode h2;
36 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
38 memset (&h1, number, sizeof (struct GNUNET_HashCode));
39 GNUNET_CRYPTO_hash_to_enc (&h1, &enc);
40 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &h2))
42 printf ("enc2hash failed!\n");
45 if (0 != memcmp (&h1, &h2, sizeof (struct GNUNET_HashCode)))
55 for (i = 0; i < 255; i++)
64 struct GNUNET_HashCode h1;
65 struct GNUNET_HashCode h2;
66 struct GNUNET_HashCode d;
67 struct GNUNET_HashCode s;
68 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
69 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
71 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h1);
72 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h2);
73 if (GNUNET_CRYPTO_hash_distance_u32 (&h1, &h2) !=
74 GNUNET_CRYPTO_hash_distance_u32 (&h2, &h1))
76 GNUNET_CRYPTO_hash_difference (&h1, &h2, &d);
77 GNUNET_CRYPTO_hash_sum (&h1, &d, &s);
78 if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
80 GNUNET_CRYPTO_hash_xor (&h1, &h2, &d);
81 GNUNET_CRYPTO_hash_xor (&h1, &d, &s);
82 if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
84 if (0 != GNUNET_CRYPTO_hash_xorcmp (&s, &h2, &h1))
86 if (-1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h1))
88 if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2))
90 memset (&d, 0xF0, sizeof (d));
91 if (0 != GNUNET_CRYPTO_hash_get_bit (&d, 3))
93 if (1 != GNUNET_CRYPTO_hash_get_bit (&d, 6))
95 memset (&d, 0, sizeof (d));
96 GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv);
101 finished_task (void *cls, const struct GNUNET_HashCode * res)
104 struct GNUNET_HashCode want;
106 GNUNET_CRYPTO_hash (block, sizeof (block), &want);
107 if (0 != memcmp (res, &want, sizeof (want)))
115 file_hasher (void *cls)
117 GNUNET_assert (NULL !=
118 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
119 FILENAME, 1024, &finished_task, cls));
129 memset (block, 42, sizeof (block) / 2);
130 memset (&block[sizeof (block) / 2], 43, sizeof (block) / 2);
131 GNUNET_assert (NULL != (f = FOPEN (FILENAME, "w+")));
132 GNUNET_break (sizeof (block) == fwrite (block, 1, sizeof (block), f));
133 GNUNET_break (0 == FCLOSE (f));
135 GNUNET_SCHEDULER_run (&file_hasher, &ret);
136 GNUNET_break (0 == UNLINK (FILENAME));
142 main (int argc, char *argv[])
144 int failureCount = 0;
147 GNUNET_log_setup ("test-crypto-hash", "WARNING", NULL);
148 for (i = 0; i < 10; i++)
149 failureCount += testEncoding ();
150 failureCount += testArithmetic ();
151 failureCount += testFileHash ();
152 if (failureCount != 0)
157 /* end of hashingtest.c */