diff options
Diffstat (limited to 'src/gnsrecord/test_gnsrecord_crypto.c')
-rw-r--r-- | src/gnsrecord/test_gnsrecord_crypto.c | 207 |
1 files changed, 0 insertions, 207 deletions
diff --git a/src/gnsrecord/test_gnsrecord_crypto.c b/src/gnsrecord/test_gnsrecord_crypto.c deleted file mode 100644 index 92a7a9f1f..000000000 --- a/src/gnsrecord/test_gnsrecord_crypto.c +++ /dev/null | |||
@@ -1,207 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2013 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 | * @file gnsrecord/test_gnsrecord_crypto.c | ||
22 | * @brief testcase for block creation, verification and decryption | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_util_lib.h" | ||
26 | #include "gnunet_gnsrecord_lib.h" | ||
27 | |||
28 | #define RECORDS 5 | ||
29 | |||
30 | #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT | ||
31 | |||
32 | #define TEST_RECORD_DATALEN 123 | ||
33 | |||
34 | #define TEST_RECORD_DATA 'a' | ||
35 | |||
36 | #define TEST_REMOVE_RECORD_TYPE 4321 | ||
37 | |||
38 | #define TEST_REMOVE_RECORD_DATALEN 255 | ||
39 | |||
40 | #define TEST_REMOVE_RECORD_DATA 'b' | ||
41 | |||
42 | |||
43 | static struct GNUNET_GNSRECORD_Data *s_rd; | ||
44 | |||
45 | static char *s_name; | ||
46 | |||
47 | static int res; | ||
48 | |||
49 | |||
50 | static struct GNUNET_GNSRECORD_Data * | ||
51 | create_record (int count) | ||
52 | { | ||
53 | struct GNUNET_GNSRECORD_Data *rd; | ||
54 | |||
55 | rd = GNUNET_new_array (count, struct GNUNET_GNSRECORD_Data); | ||
56 | for (unsigned int c = 0; c < count; c++) | ||
57 | { | ||
58 | rd[c].expiration_time = GNUNET_TIME_absolute_get ().abs_value_us | ||
59 | + 1000000000; | ||
60 | rd[c].record_type = TEST_RECORD_TYPE; | ||
61 | rd[c].data_size = TEST_RECORD_DATALEN; | ||
62 | rd[c].data = GNUNET_malloc (TEST_RECORD_DATALEN); | ||
63 | memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN); | ||
64 | } | ||
65 | return rd; | ||
66 | } | ||
67 | |||
68 | |||
69 | static void | ||
70 | rd_decrypt_cb (void *cls, | ||
71 | unsigned int rd_count, | ||
72 | const struct GNUNET_GNSRECORD_Data *rd) | ||
73 | { | ||
74 | char rd_cmp_data[TEST_RECORD_DATALEN]; | ||
75 | |||
76 | GNUNET_assert (RECORDS == rd_count); | ||
77 | GNUNET_assert (NULL != rd); | ||
78 | memset (rd_cmp_data, | ||
79 | 'a', | ||
80 | TEST_RECORD_DATALEN); | ||
81 | for (unsigned int c = 0; c < rd_count; c++) | ||
82 | { | ||
83 | GNUNET_assert (TEST_RECORD_TYPE == rd[c].record_type); | ||
84 | GNUNET_assert (TEST_RECORD_DATALEN == rd[c].data_size); | ||
85 | GNUNET_assert (0 == memcmp (&rd_cmp_data, | ||
86 | rd[c].data, | ||
87 | TEST_RECORD_DATALEN)); | ||
88 | } | ||
89 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
90 | "Block was decrypted successfully \n"); | ||
91 | res = 0; | ||
92 | } | ||
93 | |||
94 | |||
95 | static void | ||
96 | test_with_type (struct GNUNET_IDENTITY_PrivateKey *privkey) | ||
97 | { | ||
98 | struct GNUNET_GNSRECORD_Block *block; | ||
99 | struct GNUNET_IDENTITY_PublicKey pubkey; | ||
100 | struct GNUNET_HashCode query_pub; | ||
101 | struct GNUNET_HashCode query_priv; | ||
102 | struct GNUNET_HashCode query_block; | ||
103 | struct GNUNET_TIME_Absolute expire = GNUNET_TIME_UNIT_FOREVER_ABS; | ||
104 | |||
105 | |||
106 | /* get public key */ | ||
107 | GNUNET_IDENTITY_key_get_public (privkey, | ||
108 | &pubkey); | ||
109 | |||
110 | /* test query derivation */ | ||
111 | GNUNET_GNSRECORD_query_from_private_key (privkey, | ||
112 | "testlabel", | ||
113 | &query_priv); | ||
114 | GNUNET_GNSRECORD_query_from_public_key (&pubkey, | ||
115 | "testlabel", | ||
116 | &query_pub); | ||
117 | GNUNET_assert (0 == memcmp (&query_priv, | ||
118 | &query_pub, | ||
119 | sizeof(struct GNUNET_HashCode))); | ||
120 | /* create record */ | ||
121 | s_name = "testlabel"; | ||
122 | s_rd = create_record (RECORDS); | ||
123 | |||
124 | /* Create block */ | ||
125 | GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_create (privkey, | ||
126 | expire, | ||
127 | s_name, | ||
128 | s_rd, | ||
129 | RECORDS, | ||
130 | &block)); | ||
131 | GNUNET_assert (GNUNET_OK == | ||
132 | GNUNET_GNSRECORD_query_from_block (block, | ||
133 | &query_block)); | ||
134 | GNUNET_assert (0 == memcmp (&query_pub, | ||
135 | &query_block, | ||
136 | sizeof(struct GNUNET_HashCode))); | ||
137 | |||
138 | GNUNET_assert (GNUNET_OK == | ||
139 | GNUNET_GNSRECORD_block_verify (block)); | ||
140 | GNUNET_assert (GNUNET_OK == | ||
141 | GNUNET_GNSRECORD_block_decrypt (block, | ||
142 | &pubkey, | ||
143 | s_name, | ||
144 | &rd_decrypt_cb, | ||
145 | NULL)); | ||
146 | for (int i = 0; i < RECORDS; i++) GNUNET_free(s_rd[i].data); | ||
147 | GNUNET_free (s_rd); | ||
148 | GNUNET_free (block); | ||
149 | } | ||
150 | |||
151 | |||
152 | static void | ||
153 | run (void *cls, | ||
154 | char *const *args, | ||
155 | const char *cfgfile, | ||
156 | const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
157 | { | ||
158 | struct GNUNET_IDENTITY_PrivateKey privkey; | ||
159 | struct GNUNET_IDENTITY_PrivateKey privkey_ed; | ||
160 | struct GNUNET_TIME_Absolute start; | ||
161 | struct GNUNET_TIME_Absolute end; | ||
162 | |||
163 | |||
164 | privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); | ||
165 | GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key); | ||
166 | start = GNUNET_TIME_absolute_get (); | ||
167 | test_with_type (&privkey); | ||
168 | end = GNUNET_TIME_absolute_get (); | ||
169 | printf ("Time: %llu ms\n", (unsigned long long) | ||
170 | GNUNET_TIME_absolute_get_difference (start, | ||
171 | end).rel_value_us); | ||
172 | |||
173 | privkey_ed.type = htonl (GNUNET_GNSRECORD_TYPE_EDKEY); | ||
174 | GNUNET_CRYPTO_eddsa_key_create (&privkey_ed.eddsa_key); | ||
175 | start = GNUNET_TIME_absolute_get (); | ||
176 | test_with_type (&privkey_ed); | ||
177 | end = GNUNET_TIME_absolute_get (); | ||
178 | printf ("Time: %llu ms\n", (unsigned long long) | ||
179 | GNUNET_TIME_absolute_get_difference (start, | ||
180 | end).rel_value_us); | ||
181 | |||
182 | |||
183 | } | ||
184 | |||
185 | |||
186 | int | ||
187 | main (int argc, char *argv[]) | ||
188 | { | ||
189 | static char *const argvx[] = { | ||
190 | "test-gnsrecord-crypto", | ||
191 | NULL | ||
192 | }; | ||
193 | static struct GNUNET_GETOPT_CommandLineOption options[] = { | ||
194 | GNUNET_GETOPT_OPTION_END | ||
195 | }; | ||
196 | |||
197 | res = 1; | ||
198 | GNUNET_PROGRAM_run ((sizeof(argvx) / sizeof(char *)) - 1, | ||
199 | argvx, | ||
200 | "test-gnsrecord-crypto", | ||
201 | "nohelp", options, | ||
202 | &run, &res); | ||
203 | return res; | ||
204 | } | ||
205 | |||
206 | |||
207 | /* end of test_gnsrecord_crypto.c */ | ||