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