first batch of license fixes (boring)
[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 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
16 /**
17  * @author Christian Grothoff
18  * @file util/test_crypto_hash.c
19  * @brief Test for crypto_hash.c
20  */
21 #include "platform.h"
22 #include "gnunet_util_lib.h"
23
24 static char block[65536];
25
26 #define FILENAME "testblock.dat"
27
28 static int
29 test (int number)
30 {
31   struct GNUNET_HashCode h1;
32   struct GNUNET_HashCode h2;
33   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
34
35   memset (&h1, number, sizeof (struct GNUNET_HashCode));
36   GNUNET_CRYPTO_hash_to_enc (&h1, &enc);
37   if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &h2))
38   {
39     printf ("enc2hash failed!\n");
40     return 1;
41   }
42   if (0 != memcmp (&h1, &h2, sizeof (struct GNUNET_HashCode)))
43     return 1;
44   return 0;
45 }
46
47 static int
48 testEncoding ()
49 {
50   int i;
51
52   for (i = 0; i < 255; i++)
53     if (0 != test (i))
54       return 1;
55   return 0;
56 }
57
58 static int
59 testArithmetic ()
60 {
61   struct GNUNET_HashCode h1;
62   struct GNUNET_HashCode h2;
63   struct GNUNET_HashCode d;
64   struct GNUNET_HashCode s;
65   struct GNUNET_CRYPTO_SymmetricSessionKey skey;
66   struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
67
68   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h1);
69   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h2);
70   if (GNUNET_CRYPTO_hash_distance_u32 (&h1, &h2) !=
71       GNUNET_CRYPTO_hash_distance_u32 (&h2, &h1))
72     return 1;
73   GNUNET_CRYPTO_hash_difference (&h1, &h2, &d);
74   GNUNET_CRYPTO_hash_sum (&h1, &d, &s);
75   if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
76     return 1;
77   GNUNET_CRYPTO_hash_xor (&h1, &h2, &d);
78   GNUNET_CRYPTO_hash_xor (&h1, &d, &s);
79   if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
80     return 1;
81   if (0 != GNUNET_CRYPTO_hash_xorcmp (&s, &h2, &h1))
82     return 1;
83   if (-1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h1))
84     return 1;
85   if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2))
86     return 1;
87   memset (&d, 0xF0, sizeof (d));
88   if (0 != GNUNET_CRYPTO_hash_get_bit (&d, 3))
89     return 1;
90   if (1 != GNUNET_CRYPTO_hash_get_bit (&d, 6))
91     return 1;
92   memset (&d, 0, sizeof (d));
93   GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv);
94   return 0;
95 }
96
97 static void
98 finished_task (void *cls, const struct GNUNET_HashCode * res)
99 {
100   int *ret = cls;
101   struct GNUNET_HashCode want;
102
103   GNUNET_CRYPTO_hash (block, sizeof (block), &want);
104   if (0 != memcmp (res, &want, sizeof (want)))
105     *ret = 2;
106   else
107     *ret = 0;
108 }
109
110
111 static void
112 file_hasher (void *cls)
113 {
114   GNUNET_assert (NULL !=
115                  GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
116                                           FILENAME, 1024, &finished_task, cls));
117 }
118
119
120 static int
121 testFileHash ()
122 {
123   int ret;
124   FILE *f;
125
126   memset (block, 42, sizeof (block) / 2);
127   memset (&block[sizeof (block) / 2], 43, sizeof (block) / 2);
128   GNUNET_assert (NULL != (f = FOPEN (FILENAME, "w+")));
129   GNUNET_break (sizeof (block) == fwrite (block, 1, sizeof (block), f));
130   GNUNET_break (0 == FCLOSE (f));
131   ret = 1;
132   GNUNET_SCHEDULER_run (&file_hasher, &ret);
133   GNUNET_break (0 == UNLINK (FILENAME));
134   return ret;
135 }
136
137
138 int
139 main (int argc, char *argv[])
140 {
141   int failureCount = 0;
142   int i;
143
144   GNUNET_log_setup ("test-crypto-hash", "WARNING", NULL);
145   for (i = 0; i < 10; i++)
146     failureCount += testEncoding ();
147   failureCount += testArithmetic ();
148   failureCount += testFileHash ();
149   if (failureCount != 0)
150     return 1;
151   return 0;
152 }
153
154 /* end of hashingtest.c */