2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file gnsrecord/test_gnsrecord_crypto.c
22 * @brief testcase for block creation, verification and decryption
25 #include "gnunet_util_lib.h"
26 #include "gnunet_dnsparser_lib.h"
27 #include "gnunet_gnsrecord_lib.h"
31 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
33 #define TEST_RECORD_DATALEN 123
35 #define TEST_RECORD_DATA 'a'
37 #define TEST_REMOVE_RECORD_TYPE 4321
39 #define TEST_REMOVE_RECORD_DATALEN 255
41 #define TEST_REMOVE_RECORD_DATA 'b'
44 static struct GNUNET_GNSRECORD_Data *s_rd;
51 static struct GNUNET_GNSRECORD_Data *
52 create_record (int count)
54 struct GNUNET_GNSRECORD_Data *rd;
56 rd = GNUNET_new_array (count, struct GNUNET_GNSRECORD_Data);
57 for (unsigned int c = 0; c < count; c++)
59 rd[c].expiration_time = GNUNET_TIME_absolute_get ().abs_value_us
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);
71 rd_decrypt_cb (void *cls,
72 unsigned int rd_count,
73 const struct GNUNET_GNSRECORD_Data *rd)
75 char rd_cmp_data[TEST_RECORD_DATALEN];
77 GNUNET_assert (RECORDS == rd_count);
78 GNUNET_assert (NULL != rd);
82 for (unsigned int c = 0; c < rd_count; c++)
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,
88 TEST_RECORD_DATALEN));
90 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91 "Block was decrypted successfully \n");
100 const struct GNUNET_CONFIGURATION_Handle *cfg)
102 struct GNUNET_GNSRECORD_Block *block;
103 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
104 struct GNUNET_HashCode query_pub;
105 struct GNUNET_HashCode query_priv;
106 struct GNUNET_TIME_Absolute expire = GNUNET_TIME_absolute_get ();
107 struct GNUNET_CRYPTO_EcdsaPrivateKey privkey;
110 GNUNET_CRYPTO_ecdsa_key_create (&privkey);
112 GNUNET_CRYPTO_ecdsa_key_get_public (&privkey,
115 /* test query derivation */
116 GNUNET_GNSRECORD_query_from_private_key (&privkey,
119 GNUNET_GNSRECORD_query_from_public_key (&pubkey,
122 GNUNET_assert (0 == memcmp (&query_priv,
124 sizeof(struct GNUNET_HashCode)));
126 s_name = "DUMMY.dummy.gnunet";
127 s_rd = create_record (RECORDS);
130 GNUNET_assert (NULL != (block =
131 GNUNET_GNSRECORD_block_create (&privkey,
136 GNUNET_assert (GNUNET_OK ==
137 GNUNET_GNSRECORD_block_verify (block));
138 GNUNET_assert (GNUNET_OK ==
139 GNUNET_GNSRECORD_block_decrypt (block,
149 main (int argc, char *argv[])
151 static char *const argvx[] = {
152 "test-gnsrecord-crypto",
155 static struct GNUNET_GETOPT_CommandLineOption options[] = {
156 GNUNET_GETOPT_OPTION_END
160 GNUNET_PROGRAM_run ((sizeof(argvx) / sizeof(char *)) - 1,
162 "test-gnsrecord-crypto",
169 /* end of test_gnsrecord_crypto.c */