notes
[oweals/gnunet.git] / src / util / test_crypto_hash.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2002, 2003, 2004, 2006, 2009 GNUnet e.V.
4
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.
9
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.
14     
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/>.
17 */
18
19 /**
20  * @author Christian Grothoff
21  * @file util/test_crypto_hash.c
22  * @brief Test for crypto_hash.c
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26
27 static char block[65536];
28
29 #define FILENAME "testblock.dat"
30
31 static int
32 test (int number)
33 {
34   struct GNUNET_HashCode h1;
35   struct GNUNET_HashCode h2;
36   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
37
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))
41   {
42     printf ("enc2hash failed!\n");
43     return 1;
44   }
45   if (0 != memcmp (&h1, &h2, sizeof (struct GNUNET_HashCode)))
46     return 1;
47   return 0;
48 }
49
50 static int
51 testEncoding ()
52 {
53   int i;
54
55   for (i = 0; i < 255; i++)
56     if (0 != test (i))
57       return 1;
58   return 0;
59 }
60
61 static int
62 testArithmetic ()
63 {
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;
70
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))
75     return 1;
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))
79     return 1;
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))
83     return 1;
84   if (0 != GNUNET_CRYPTO_hash_xorcmp (&s, &h2, &h1))
85     return 1;
86   if (-1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h1))
87     return 1;
88   if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2))
89     return 1;
90   memset (&d, 0xF0, sizeof (d));
91   if (0 != GNUNET_CRYPTO_hash_get_bit (&d, 3))
92     return 1;
93   if (1 != GNUNET_CRYPTO_hash_get_bit (&d, 6))
94     return 1;
95   memset (&d, 0, sizeof (d));
96   GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv);
97   return 0;
98 }
99
100 static void
101 finished_task (void *cls, const struct GNUNET_HashCode * res)
102 {
103   int *ret = cls;
104   struct GNUNET_HashCode want;
105
106   GNUNET_CRYPTO_hash (block, sizeof (block), &want);
107   if (0 != memcmp (res, &want, sizeof (want)))
108     *ret = 2;
109   else
110     *ret = 0;
111 }
112
113
114 static void
115 file_hasher (void *cls)
116 {
117   GNUNET_assert (NULL !=
118                  GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
119                                           FILENAME, 1024, &finished_task, cls));
120 }
121
122
123 static int
124 testFileHash ()
125 {
126   int ret;
127   FILE *f;
128
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));
134   ret = 1;
135   GNUNET_SCHEDULER_run (&file_hasher, &ret);
136   GNUNET_break (0 == UNLINK (FILENAME));
137   return ret;
138 }
139
140
141 int
142 main (int argc, char *argv[])
143 {
144   int failureCount = 0;
145   int i;
146
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)
153     return 1;
154   return 0;
155 }
156
157 /* end of hashingtest.c */