ng
[oweals/gnunet.git] / src / util / test_crypto_hash.c
1 /*
2      This file is part of GNUnet.
3      (C) 2002, 2003, 2004, 2006, 2009 Christian Grothoff (and other contributing authors)
4
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 2, or (at your
8      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      General Public License for more details.
14
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.
19 */
20
21 /**
22  * @author Christian Grothoff
23  * @file util/test_crypto_hash.c
24  * @brief Test for crypto_hash.c
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_scheduler_lib.h"
30
31 static char block[65536];
32
33 #define FILENAME "testblock.dat"
34
35 static int
36 test (int number)
37 {
38   GNUNET_HashCode h1;
39   GNUNET_HashCode h2;
40   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
41
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))
45     {
46       printf ("enc2hash failed!\n");
47       return 1;
48     }
49   if (0 != memcmp (&h1, &h2, sizeof (GNUNET_HashCode)))
50     return 1;
51   return 0;
52 }
53
54 static int
55 testEncoding ()
56 {
57   int i;
58   for (i = 0; i < 255; i++)
59     if (0 != test (i))
60       return 1;
61   return 0;
62 }
63
64 static int
65 testArithmetic ()
66 {
67   static struct GNUNET_CRYPTO_AesSessionKey zskey;
68   static struct GNUNET_CRYPTO_AesInitializationVector ziv;
69   GNUNET_HashCode h1;
70   GNUNET_HashCode h2;
71   GNUNET_HashCode d;
72   GNUNET_HashCode s;
73   struct GNUNET_CRYPTO_AesSessionKey skey;
74   struct GNUNET_CRYPTO_AesInitializationVector iv;
75
76   GNUNET_CRYPTO_hash_create_random (&h1);
77   GNUNET_CRYPTO_hash_create_random (&h2);
78   if (GNUNET_CRYPTO_hash_distance_u32 (&h1, &h2) !=
79       GNUNET_CRYPTO_hash_distance_u32 (&h2, &h1))
80     return 1;
81   GNUNET_CRYPTO_hash_difference (&h1, &h2, &d);
82   GNUNET_CRYPTO_hash_sum (&h1, &d, &s);
83   if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
84     return 1;
85   GNUNET_CRYPTO_hash_xor (&h1, &h2, &d);
86   GNUNET_CRYPTO_hash_xor (&h1, &d, &s);
87   if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
88     return 1;
89   if (0 != GNUNET_CRYPTO_hash_xorcmp (&s, &h2, &h1))
90     return 1;
91   if (-1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h1))
92     return 1;
93   if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2))
94     return 1;
95   memset (&d, 0xF0, sizeof (d));
96   if (0 != GNUNET_CRYPTO_hash_get_bit (&d, 3))
97     return 1;
98   if (1 != GNUNET_CRYPTO_hash_get_bit (&d, 6))
99     return 1;
100   memset (&d, 0, sizeof (d));
101   GNUNET_CRYPTO_hash_to_AES_key (&d, &skey, &iv);
102   if ((0 != memcmp (&skey, &zskey, sizeof (skey) - sizeof (unsigned int))) ||
103       (0 != memcmp (&iv, &ziv, sizeof (iv))))
104     return 1;
105   return 0;
106 }
107
108 static void
109 finished_task (void *cls, const GNUNET_HashCode * res)
110 {
111   int *ret = cls;
112   GNUNET_HashCode want;
113
114   GNUNET_CRYPTO_hash (block, sizeof (block), &want);
115   if (0 != memcmp (res, &want, sizeof (want)))
116     *ret = 2;
117   else
118     *ret = 0;
119 }
120
121
122 static void
123 file_hasher (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
124 {
125   GNUNET_CRYPTO_hash_file (tc->sched,
126                            GNUNET_SCHEDULER_PRIORITY_DEFAULT,
127                            GNUNET_NO, FILENAME, 1024, &finished_task, cls);
128 }
129
130
131 static int
132 testFileHash ()
133 {
134   int ret;
135   FILE *f;
136
137   memset (block, 42, sizeof (block) / 2);
138   memset (&block[sizeof (block) / 2], 43, sizeof (block) / 2);
139   GNUNET_assert (NULL != (f = fopen (FILENAME, "w+")));
140   GNUNET_break (sizeof (block) == fwrite (block, 1, sizeof (block), f));
141   GNUNET_break (0 == fclose (f));
142   ret = 1;
143   GNUNET_SCHEDULER_run (&file_hasher, &ret);
144   GNUNET_break (0 == UNLINK (FILENAME));
145   return ret;
146 }
147
148
149 int
150 main (int argc, char *argv[])
151 {
152   int failureCount = 0;
153   int i;
154
155   GNUNET_log_setup ("test-crypto-hash", "WARNING", NULL);
156   for (i = 0; i < 10; i++)
157     failureCount += testEncoding ();
158   failureCount += testArithmetic ();
159   failureCount += testFileHash ();
160   if (failureCount != 0)
161     return 1;
162   return 0;
163 }
164
165 /* end of hashingtest.c */