2 This file is part of GNUnet.
3 (C) 2002, 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @author Christian Grothoff
23 * @file util/test_crypto_hash.c
24 * @brief Test for crypto_hash.c
27 #include "gnunet_common.h"
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_scheduler_lib.h"
31 static char block[65536];
33 #define FILENAME "testblock.dat"
40 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
42 memset (&h1, number, sizeof (GNUNET_HashCode));
43 GNUNET_CRYPTO_hash_to_enc (&h1, &enc);
44 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &h2))
46 printf ("enc2hash failed!\n");
49 if (0 != memcmp (&h1, &h2, sizeof (GNUNET_HashCode)))
59 for (i = 0; i < 255; i++)
68 static struct GNUNET_CRYPTO_AesSessionKey zskey;
69 static struct GNUNET_CRYPTO_AesInitializationVector ziv;
74 struct GNUNET_CRYPTO_AesSessionKey skey;
75 struct GNUNET_CRYPTO_AesInitializationVector iv;
77 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h1);
78 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h2);
79 if (GNUNET_CRYPTO_hash_distance_u32 (&h1, &h2) !=
80 GNUNET_CRYPTO_hash_distance_u32 (&h2, &h1))
82 GNUNET_CRYPTO_hash_difference (&h1, &h2, &d);
83 GNUNET_CRYPTO_hash_sum (&h1, &d, &s);
84 if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
86 GNUNET_CRYPTO_hash_xor (&h1, &h2, &d);
87 GNUNET_CRYPTO_hash_xor (&h1, &d, &s);
88 if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
90 if (0 != GNUNET_CRYPTO_hash_xorcmp (&s, &h2, &h1))
92 if (-1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h1))
94 if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2))
96 memset (&d, 0xF0, sizeof (d));
97 if (0 != GNUNET_CRYPTO_hash_get_bit (&d, 3))
99 if (1 != GNUNET_CRYPTO_hash_get_bit (&d, 6))
101 memset (&d, 0, sizeof (d));
102 GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv);
103 if ((0 != memcmp (&skey, &zskey, sizeof (skey) - sizeof (unsigned int))) ||
104 (0 != memcmp (&iv, &ziv, sizeof (iv))))
110 finished_task (void *cls, const GNUNET_HashCode * res)
113 GNUNET_HashCode want;
115 GNUNET_CRYPTO_hash (block, sizeof (block), &want);
116 if (0 != memcmp (res, &want, sizeof (want)))
124 file_hasher (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
126 GNUNET_assert (NULL !=
127 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
128 FILENAME, 1024, &finished_task,
139 memset (block, 42, sizeof (block) / 2);
140 memset (&block[sizeof (block) / 2], 43, sizeof (block) / 2);
141 GNUNET_assert (NULL != (f = fopen (FILENAME, "w+")));
142 GNUNET_break (sizeof (block) == fwrite (block, 1, sizeof (block), f));
143 GNUNET_break (0 == fclose (f));
145 GNUNET_SCHEDULER_run (&file_hasher, &ret);
146 GNUNET_break (0 == UNLINK (FILENAME));
152 main (int argc, char *argv[])
154 int failureCount = 0;
157 GNUNET_log_setup ("test-crypto-hash", "WARNING", NULL);
158 for (i = 0; i < 10; i++)
159 failureCount += testEncoding ();
160 failureCount += testArithmetic ();
161 failureCount += testFileHash ();
162 if (failureCount != 0)
167 /* end of hashingtest.c */