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