aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_crypto_hash.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_crypto_hash.c')
-rw-r--r--src/util/test_crypto_hash.c168
1 files changed, 0 insertions, 168 deletions
diff --git a/src/util/test_crypto_hash.c b/src/util/test_crypto_hash.c
deleted file mode 100644
index d22e1f5d3..000000000
--- a/src/util/test_crypto_hash.c
+++ /dev/null
@@ -1,168 +0,0 @@
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 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
29static char block[65536];
30
31#define FILENAME "testblock.dat"
32
33static int
34test (int number)
35{
36 struct GNUNET_HashCode h1;
37 struct GNUNET_HashCode h2;
38 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
39
40 memset (&h1, number, sizeof(struct GNUNET_HashCode));
41 GNUNET_CRYPTO_hash_to_enc (&h1, &enc);
42 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &h2))
43 {
44 printf ("enc2hash failed!\n");
45 return 1;
46 }
47 if (0 != memcmp (&h1, &h2, sizeof(struct GNUNET_HashCode)))
48 return 1;
49 return 0;
50}
51
52
53static int
54testEncoding ()
55{
56 int i;
57
58 for (i = 0; i < 255; i++)
59 if (0 != test (i))
60 return 1;
61 return 0;
62}
63
64
65static int
66testArithmetic ()
67{
68 struct GNUNET_HashCode h1;
69 struct GNUNET_HashCode h2;
70 struct GNUNET_HashCode d;
71 struct GNUNET_HashCode s;
72 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
73 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
74
75 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h1);
76 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h2);
77 if (GNUNET_CRYPTO_hash_distance_u32 (&h1, &h2) !=
78 GNUNET_CRYPTO_hash_distance_u32 (&h2, &h1))
79 return 1;
80 GNUNET_CRYPTO_hash_difference (&h1, &h2, &d);
81 GNUNET_CRYPTO_hash_sum (&h1, &d, &s);
82 if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
83 return 1;
84 GNUNET_CRYPTO_hash_xor (&h1, &h2, &d);
85 GNUNET_CRYPTO_hash_xor (&h1, &d, &s);
86 if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
87 return 1;
88 if (0 != GNUNET_CRYPTO_hash_xorcmp (&s, &h2, &h1))
89 return 1;
90 if (-1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h1))
91 return 1;
92 if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2))
93 return 1;
94 memset (&d, 0x40, sizeof(d));
95 if (0 != GNUNET_CRYPTO_hash_get_bit_rtl (&d, 3))
96 return 1;
97 if (1 != GNUNET_CRYPTO_hash_get_bit_rtl (&d, 6))
98 return 1;
99 memset (&d, 0x02, sizeof(d));
100 if (0 != GNUNET_CRYPTO_hash_get_bit_ltr (&d, 3))
101 return 1;
102 if (1 != GNUNET_CRYPTO_hash_get_bit_ltr (&d, 6))
103 return 1;
104 memset (&d, 0, sizeof(d));
105 GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv);
106 return 0;
107}
108
109
110static void
111finished_task (void *cls, const struct GNUNET_HashCode *res)
112{
113 int *ret = cls;
114 struct GNUNET_HashCode want;
115
116 GNUNET_CRYPTO_hash (block, sizeof(block), &want);
117 if (0 != memcmp (res, &want, sizeof(want)))
118 *ret = 2;
119 else
120 *ret = 0;
121}
122
123
124static void
125file_hasher (void *cls)
126{
127 GNUNET_assert (NULL !=
128 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
129 FILENAME, 1024, &finished_task, cls));
130}
131
132
133static int
134testFileHash ()
135{
136 int ret;
137 FILE *f;
138
139 memset (block, 42, sizeof(block) / 2);
140 memset (&block[sizeof(block) / 2], 43, sizeof(block) / 2);
141 GNUNET_assert (NULL != (f = fopen (FILENAME, "w+")));
142 GNUNET_break (sizeof(block) == fwrite (block, 1, sizeof(block), f));
143 GNUNET_break (0 == fclose (f));
144 ret = 1;
145 GNUNET_SCHEDULER_run (&file_hasher, &ret);
146 GNUNET_break (0 == unlink (FILENAME));
147 return ret;
148}
149
150
151int
152main (int argc, char *argv[])
153{
154 int failureCount = 0;
155 int i;
156
157 GNUNET_log_setup ("test-crypto-hash", "WARNING", NULL);
158 for (i = 0; i < 10; i++)
159 failureCount += testEncoding ();
160 failureCount += testArithmetic ();
161 failureCount += testFileHash ();
162 if (failureCount != 0)
163 return 1;
164 return 0;
165}
166
167
168/* end of hashingtest.c */