diff options
Diffstat (limited to 'src/lib/util/test_crypto_hash.c')
-rw-r--r-- | src/lib/util/test_crypto_hash.c | 218 |
1 files changed, 218 insertions, 0 deletions
diff --git a/src/lib/util/test_crypto_hash.c b/src/lib/util/test_crypto_hash.c new file mode 100644 index 000000000..337694a89 --- /dev/null +++ b/src/lib/util/test_crypto_hash.c | |||
@@ -0,0 +1,218 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2002, 2003, 2004, 2006, 2009, 2022 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 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @author Christian Grothoff | ||
23 | * @file util/test_crypto_hash.c | ||
24 | * @brief Test for crypto_hash.c | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunet_util_lib.h" | ||
29 | |||
30 | static char block[65536]; | ||
31 | |||
32 | #define FILENAME "testblock.dat" | ||
33 | |||
34 | static int | ||
35 | test (int number) | ||
36 | { | ||
37 | struct GNUNET_HashCode h1; | ||
38 | struct GNUNET_HashCode h2; | ||
39 | struct GNUNET_CRYPTO_HashAsciiEncoded enc; | ||
40 | |||
41 | memset (&h1, | ||
42 | number, | ||
43 | sizeof(struct GNUNET_HashCode)); | ||
44 | GNUNET_CRYPTO_hash_to_enc (&h1, | ||
45 | &enc); | ||
46 | if (GNUNET_OK != | ||
47 | GNUNET_CRYPTO_hash_from_string ((char *) &enc, | ||
48 | &h2)) | ||
49 | { | ||
50 | printf ("enc2hash failed!\n"); | ||
51 | return 1; | ||
52 | } | ||
53 | if (0 != GNUNET_memcmp (&h1, | ||
54 | &h2)) | ||
55 | return 1; | ||
56 | return 0; | ||
57 | } | ||
58 | |||
59 | |||
60 | static int | ||
61 | test_encoding (void) | ||
62 | { | ||
63 | for (int i = 0; i < 255; i++) | ||
64 | if (0 != test (i)) | ||
65 | return 1; | ||
66 | return 0; | ||
67 | } | ||
68 | |||
69 | |||
70 | static int | ||
71 | test_arithmetic (void) | ||
72 | { | ||
73 | struct GNUNET_HashCode h1; | ||
74 | struct GNUNET_HashCode h2; | ||
75 | struct GNUNET_HashCode d; | ||
76 | struct GNUNET_HashCode s; | ||
77 | struct GNUNET_CRYPTO_SymmetricSessionKey skey; | ||
78 | struct GNUNET_CRYPTO_SymmetricInitializationVector iv; | ||
79 | |||
80 | GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, | ||
81 | &h1); | ||
82 | GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, | ||
83 | &h2); | ||
84 | if (GNUNET_CRYPTO_hash_distance_u32 (&h1, | ||
85 | &h2) != | ||
86 | GNUNET_CRYPTO_hash_distance_u32 (&h2, | ||
87 | &h1)) | ||
88 | return 1; | ||
89 | GNUNET_CRYPTO_hash_difference (&h1, | ||
90 | &h2, | ||
91 | &d); | ||
92 | GNUNET_CRYPTO_hash_sum (&h1, | ||
93 | &d, | ||
94 | &s); | ||
95 | if (0 != | ||
96 | GNUNET_CRYPTO_hash_cmp (&s, | ||
97 | &h2)) | ||
98 | return 1; | ||
99 | GNUNET_CRYPTO_hash_xor (&h1, | ||
100 | &h2, | ||
101 | &d); | ||
102 | GNUNET_CRYPTO_hash_xor (&h1, | ||
103 | &d, | ||
104 | &s); | ||
105 | if (0 != | ||
106 | GNUNET_CRYPTO_hash_cmp (&s, | ||
107 | &h2)) | ||
108 | return 1; | ||
109 | if (0 != | ||
110 | GNUNET_CRYPTO_hash_xorcmp (&s, | ||
111 | &h2, | ||
112 | &h1)) | ||
113 | return 1; | ||
114 | if (-1 != | ||
115 | GNUNET_CRYPTO_hash_xorcmp (&h1, | ||
116 | &h2, | ||
117 | &h1)) | ||
118 | return 1; | ||
119 | if (1 != | ||
120 | GNUNET_CRYPTO_hash_xorcmp (&h1, | ||
121 | &h2, | ||
122 | &h2)) | ||
123 | return 1; | ||
124 | memset (&d, | ||
125 | 0, | ||
126 | sizeof(d)); | ||
127 | GNUNET_CRYPTO_hash_to_aes_key (&d, | ||
128 | &skey, | ||
129 | &iv); | ||
130 | memset (&h1, | ||
131 | 0, | ||
132 | sizeof (h1)); | ||
133 | h1.bits[1] = htonl (0x00200000); /* 32 + 8 + 2 = 42 MSB bits cleared */ | ||
134 | GNUNET_assert (42 == | ||
135 | GNUNET_CRYPTO_hash_count_leading_zeros (&h1)); | ||
136 | GNUNET_assert (512 - 42 - 1 == | ||
137 | GNUNET_CRYPTO_hash_count_tailing_zeros (&h1)); | ||
138 | return 0; | ||
139 | } | ||
140 | |||
141 | |||
142 | static void | ||
143 | finished_task (void *cls, | ||
144 | const struct GNUNET_HashCode *res) | ||
145 | { | ||
146 | int *ret = cls; | ||
147 | struct GNUNET_HashCode want; | ||
148 | |||
149 | GNUNET_CRYPTO_hash (block, | ||
150 | sizeof(block), | ||
151 | &want); | ||
152 | if (0 != GNUNET_memcmp (res, | ||
153 | &want)) | ||
154 | *ret = 2; | ||
155 | else | ||
156 | *ret = 0; | ||
157 | } | ||
158 | |||
159 | |||
160 | static void | ||
161 | file_hasher (void *cls) | ||
162 | { | ||
163 | GNUNET_assert (NULL != | ||
164 | GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | ||
165 | FILENAME, | ||
166 | 1024, | ||
167 | &finished_task, | ||
168 | cls)); | ||
169 | } | ||
170 | |||
171 | |||
172 | static int | ||
173 | test_file_hash (void) | ||
174 | { | ||
175 | int ret; | ||
176 | FILE *f; | ||
177 | |||
178 | memset (block, | ||
179 | 42, | ||
180 | sizeof(block) / 2); | ||
181 | memset (&block[sizeof(block) / 2], | ||
182 | 43, | ||
183 | sizeof(block) / 2); | ||
184 | GNUNET_assert (NULL != (f = fopen (FILENAME, "w+"))); | ||
185 | GNUNET_break (sizeof(block) == | ||
186 | fwrite (block, | ||
187 | 1, | ||
188 | sizeof(block), | ||
189 | f)); | ||
190 | GNUNET_break (0 == fclose (f)); | ||
191 | ret = 1; | ||
192 | GNUNET_SCHEDULER_run (&file_hasher, | ||
193 | &ret); | ||
194 | GNUNET_break (0 == unlink (FILENAME)); | ||
195 | return ret; | ||
196 | } | ||
197 | |||
198 | |||
199 | int | ||
200 | main (int argc, | ||
201 | char *argv[]) | ||
202 | { | ||
203 | int failureCount = 0; | ||
204 | |||
205 | GNUNET_log_setup ("test-crypto-hash", | ||
206 | "WARNING", | ||
207 | NULL); | ||
208 | for (int i = 0; i < 10; i++) | ||
209 | failureCount += test_encoding (); | ||
210 | failureCount += test_arithmetic (); | ||
211 | failureCount += test_file_hash (); | ||
212 | if (0 != failureCount) | ||
213 | return 1; | ||
214 | return 0; | ||
215 | } | ||
216 | |||
217 | |||
218 | /* end of test_crypto_hash.c */ | ||