aboutsummaryrefslogtreecommitdiff
path: root/src/lib/gnsrecord/test_gnsrecord_crypto.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/gnsrecord/test_gnsrecord_crypto.c')
-rw-r--r--src/lib/gnsrecord/test_gnsrecord_crypto.c211
1 files changed, 211 insertions, 0 deletions
diff --git a/src/lib/gnsrecord/test_gnsrecord_crypto.c b/src/lib/gnsrecord/test_gnsrecord_crypto.c
new file mode 100644
index 000000000..34f5f35e5
--- /dev/null
+++ b/src/lib/gnsrecord/test_gnsrecord_crypto.c
@@ -0,0 +1,211 @@
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
43static struct GNUNET_GNSRECORD_Data *s_rd;
44
45static char *s_name;
46
47static int res;
48
49
50static struct GNUNET_GNSRECORD_Data *
51create_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
69static void
70rd_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
95static void
96test_with_type (struct GNUNET_CRYPTO_PrivateKey *privkey)
97{
98 struct GNUNET_GNSRECORD_Block *block;
99 struct GNUNET_CRYPTO_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 char* tmp_data;
105
106 /* get public key */
107 GNUNET_CRYPTO_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++)
147 {
148 tmp_data = (char*) s_rd[i].data;
149 GNUNET_free(tmp_data);
150 }
151 GNUNET_free (s_rd);
152 GNUNET_free (block);
153}
154
155
156static void
157run (void *cls,
158 char *const *args,
159 const char *cfgfile,
160 const struct GNUNET_CONFIGURATION_Handle *cfg)
161{
162 struct GNUNET_CRYPTO_PrivateKey privkey;
163 struct GNUNET_CRYPTO_PrivateKey privkey_ed;
164 struct GNUNET_TIME_Absolute start;
165 struct GNUNET_TIME_Absolute end;
166
167
168 privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
169 GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key);
170 start = GNUNET_TIME_absolute_get ();
171 test_with_type (&privkey);
172 end = GNUNET_TIME_absolute_get ();
173 printf ("Time: %llu ms\n", (unsigned long long)
174 GNUNET_TIME_absolute_get_difference (start,
175 end).rel_value_us);
176
177 privkey_ed.type = htonl (GNUNET_GNSRECORD_TYPE_EDKEY);
178 GNUNET_CRYPTO_eddsa_key_create (&privkey_ed.eddsa_key);
179 start = GNUNET_TIME_absolute_get ();
180 test_with_type (&privkey_ed);
181 end = GNUNET_TIME_absolute_get ();
182 printf ("Time: %llu ms\n", (unsigned long long)
183 GNUNET_TIME_absolute_get_difference (start,
184 end).rel_value_us);
185
186
187}
188
189
190int
191main (int argc, char *argv[])
192{
193 static char *const argvx[] = {
194 "test-gnsrecord-crypto",
195 NULL
196 };
197 static struct GNUNET_GETOPT_CommandLineOption options[] = {
198 GNUNET_GETOPT_OPTION_END
199 };
200
201 res = 1;
202 GNUNET_PROGRAM_run ((sizeof(argvx) / sizeof(char *)) - 1,
203 argvx,
204 "test-gnsrecord-crypto",
205 "nohelp", options,
206 &run, &res);
207 return res;
208}
209
210
211/* end of test_gnsrecord_crypto.c */