diff options
Diffstat (limited to 'src/util/test_crypto_hash.c')
-rw-r--r-- | src/util/test_crypto_hash.c | 104 |
1 files changed, 52 insertions, 52 deletions
diff --git a/src/util/test_crypto_hash.c b/src/util/test_crypto_hash.c index e08348e53..0e8246595 100644 --- a/src/util/test_crypto_hash.c +++ b/src/util/test_crypto_hash.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 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/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @author Christian Grothoff | 22 | * @author Christian Grothoff |
@@ -31,37 +31,37 @@ static char block[65536]; | |||
31 | #define FILENAME "testblock.dat" | 31 | #define FILENAME "testblock.dat" |
32 | 32 | ||
33 | static int | 33 | static int |
34 | test (int number) | 34 | test(int number) |
35 | { | 35 | { |
36 | struct GNUNET_HashCode h1; | 36 | struct GNUNET_HashCode h1; |
37 | struct GNUNET_HashCode h2; | 37 | struct GNUNET_HashCode h2; |
38 | struct GNUNET_CRYPTO_HashAsciiEncoded enc; | 38 | struct GNUNET_CRYPTO_HashAsciiEncoded enc; |
39 | 39 | ||
40 | memset (&h1, number, sizeof (struct GNUNET_HashCode)); | 40 | memset(&h1, number, sizeof(struct GNUNET_HashCode)); |
41 | GNUNET_CRYPTO_hash_to_enc (&h1, &enc); | 41 | GNUNET_CRYPTO_hash_to_enc(&h1, &enc); |
42 | if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &h2)) | 42 | if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string((char *)&enc, &h2)) |
43 | { | 43 | { |
44 | printf ("enc2hash failed!\n"); | 44 | printf("enc2hash failed!\n"); |
45 | return 1; | 45 | return 1; |
46 | } | 46 | } |
47 | if (0 != memcmp (&h1, &h2, sizeof (struct GNUNET_HashCode))) | 47 | if (0 != memcmp(&h1, &h2, sizeof(struct GNUNET_HashCode))) |
48 | return 1; | 48 | return 1; |
49 | return 0; | 49 | return 0; |
50 | } | 50 | } |
51 | 51 | ||
52 | static int | 52 | static int |
53 | testEncoding () | 53 | testEncoding() |
54 | { | 54 | { |
55 | int i; | 55 | int i; |
56 | 56 | ||
57 | for (i = 0; i < 255; i++) | 57 | for (i = 0; i < 255; i++) |
58 | if (0 != test (i)) | 58 | if (0 != test(i)) |
59 | return 1; | 59 | return 1; |
60 | return 0; | 60 | return 0; |
61 | } | 61 | } |
62 | 62 | ||
63 | static int | 63 | static int |
64 | testArithmetic () | 64 | testArithmetic() |
65 | { | 65 | { |
66 | struct GNUNET_HashCode h1; | 66 | struct GNUNET_HashCode h1; |
67 | struct GNUNET_HashCode h2; | 67 | struct GNUNET_HashCode h2; |
@@ -70,43 +70,43 @@ testArithmetic () | |||
70 | struct GNUNET_CRYPTO_SymmetricSessionKey skey; | 70 | struct GNUNET_CRYPTO_SymmetricSessionKey skey; |
71 | struct GNUNET_CRYPTO_SymmetricInitializationVector iv; | 71 | struct GNUNET_CRYPTO_SymmetricInitializationVector iv; |
72 | 72 | ||
73 | GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h1); | 73 | GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &h1); |
74 | GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h2); | 74 | GNUNET_CRYPTO_hash_create_random(GNUNET_CRYPTO_QUALITY_WEAK, &h2); |
75 | if (GNUNET_CRYPTO_hash_distance_u32 (&h1, &h2) != | 75 | if (GNUNET_CRYPTO_hash_distance_u32(&h1, &h2) != |
76 | GNUNET_CRYPTO_hash_distance_u32 (&h2, &h1)) | 76 | GNUNET_CRYPTO_hash_distance_u32(&h2, &h1)) |
77 | return 1; | 77 | return 1; |
78 | GNUNET_CRYPTO_hash_difference (&h1, &h2, &d); | 78 | GNUNET_CRYPTO_hash_difference(&h1, &h2, &d); |
79 | GNUNET_CRYPTO_hash_sum (&h1, &d, &s); | 79 | GNUNET_CRYPTO_hash_sum(&h1, &d, &s); |
80 | if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2)) | 80 | if (0 != GNUNET_CRYPTO_hash_cmp(&s, &h2)) |
81 | return 1; | 81 | return 1; |
82 | GNUNET_CRYPTO_hash_xor (&h1, &h2, &d); | 82 | GNUNET_CRYPTO_hash_xor(&h1, &h2, &d); |
83 | GNUNET_CRYPTO_hash_xor (&h1, &d, &s); | 83 | GNUNET_CRYPTO_hash_xor(&h1, &d, &s); |
84 | if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2)) | 84 | if (0 != GNUNET_CRYPTO_hash_cmp(&s, &h2)) |
85 | return 1; | 85 | return 1; |
86 | if (0 != GNUNET_CRYPTO_hash_xorcmp (&s, &h2, &h1)) | 86 | if (0 != GNUNET_CRYPTO_hash_xorcmp(&s, &h2, &h1)) |
87 | return 1; | 87 | return 1; |
88 | if (-1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h1)) | 88 | if (-1 != GNUNET_CRYPTO_hash_xorcmp(&h1, &h2, &h1)) |
89 | return 1; | 89 | return 1; |
90 | if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2)) | 90 | if (1 != GNUNET_CRYPTO_hash_xorcmp(&h1, &h2, &h2)) |
91 | return 1; | 91 | return 1; |
92 | memset (&d, 0xF0, sizeof (d)); | 92 | memset(&d, 0xF0, sizeof(d)); |
93 | if (0 != GNUNET_CRYPTO_hash_get_bit (&d, 3)) | 93 | if (0 != GNUNET_CRYPTO_hash_get_bit(&d, 3)) |
94 | return 1; | 94 | return 1; |
95 | if (1 != GNUNET_CRYPTO_hash_get_bit (&d, 6)) | 95 | if (1 != GNUNET_CRYPTO_hash_get_bit(&d, 6)) |
96 | return 1; | 96 | return 1; |
97 | memset (&d, 0, sizeof (d)); | 97 | memset(&d, 0, sizeof(d)); |
98 | GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv); | 98 | GNUNET_CRYPTO_hash_to_aes_key(&d, &skey, &iv); |
99 | return 0; | 99 | return 0; |
100 | } | 100 | } |
101 | 101 | ||
102 | static void | 102 | static void |
103 | finished_task (void *cls, const struct GNUNET_HashCode * res) | 103 | finished_task(void *cls, const struct GNUNET_HashCode * res) |
104 | { | 104 | { |
105 | int *ret = cls; | 105 | int *ret = cls; |
106 | struct GNUNET_HashCode want; | 106 | struct GNUNET_HashCode want; |
107 | 107 | ||
108 | GNUNET_CRYPTO_hash (block, sizeof (block), &want); | 108 | GNUNET_CRYPTO_hash(block, sizeof(block), &want); |
109 | if (0 != memcmp (res, &want, sizeof (want))) | 109 | if (0 != memcmp(res, &want, sizeof(want))) |
110 | *ret = 2; | 110 | *ret = 2; |
111 | else | 111 | else |
112 | *ret = 0; | 112 | *ret = 0; |
@@ -114,43 +114,43 @@ finished_task (void *cls, const struct GNUNET_HashCode * res) | |||
114 | 114 | ||
115 | 115 | ||
116 | static void | 116 | static void |
117 | file_hasher (void *cls) | 117 | file_hasher(void *cls) |
118 | { | 118 | { |
119 | GNUNET_assert (NULL != | 119 | GNUNET_assert(NULL != |
120 | GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT, | 120 | GNUNET_CRYPTO_hash_file(GNUNET_SCHEDULER_PRIORITY_DEFAULT, |
121 | FILENAME, 1024, &finished_task, cls)); | 121 | FILENAME, 1024, &finished_task, cls)); |
122 | } | 122 | } |
123 | 123 | ||
124 | 124 | ||
125 | static int | 125 | static int |
126 | testFileHash () | 126 | testFileHash() |
127 | { | 127 | { |
128 | int ret; | 128 | int ret; |
129 | FILE *f; | 129 | FILE *f; |
130 | 130 | ||
131 | memset (block, 42, sizeof (block) / 2); | 131 | memset(block, 42, sizeof(block) / 2); |
132 | memset (&block[sizeof (block) / 2], 43, sizeof (block) / 2); | 132 | memset(&block[sizeof(block) / 2], 43, sizeof(block) / 2); |
133 | GNUNET_assert (NULL != (f = fopen (FILENAME, "w+"))); | 133 | GNUNET_assert(NULL != (f = fopen(FILENAME, "w+"))); |
134 | GNUNET_break (sizeof (block) == fwrite (block, 1, sizeof (block), f)); | 134 | GNUNET_break(sizeof(block) == fwrite(block, 1, sizeof(block), f)); |
135 | GNUNET_break (0 == fclose (f)); | 135 | GNUNET_break(0 == fclose(f)); |
136 | ret = 1; | 136 | ret = 1; |
137 | GNUNET_SCHEDULER_run (&file_hasher, &ret); | 137 | GNUNET_SCHEDULER_run(&file_hasher, &ret); |
138 | GNUNET_break (0 == unlink (FILENAME)); | 138 | GNUNET_break(0 == unlink(FILENAME)); |
139 | return ret; | 139 | return ret; |
140 | } | 140 | } |
141 | 141 | ||
142 | 142 | ||
143 | int | 143 | int |
144 | main (int argc, char *argv[]) | 144 | main(int argc, char *argv[]) |
145 | { | 145 | { |
146 | int failureCount = 0; | 146 | int failureCount = 0; |
147 | int i; | 147 | int i; |
148 | 148 | ||
149 | GNUNET_log_setup ("test-crypto-hash", "WARNING", NULL); | 149 | GNUNET_log_setup("test-crypto-hash", "WARNING", NULL); |
150 | for (i = 0; i < 10; i++) | 150 | for (i = 0; i < 10; i++) |
151 | failureCount += testEncoding (); | 151 | failureCount += testEncoding(); |
152 | failureCount += testArithmetic (); | 152 | failureCount += testArithmetic(); |
153 | failureCount += testFileHash (); | 153 | failureCount += testFileHash(); |
154 | if (failureCount != 0) | 154 | if (failureCount != 0) |
155 | return 1; | 155 | return 1; |
156 | return 0; | 156 | return 0; |