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 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.
16 * @file gnsrecord/test_gnsrecord_crypto.c
17 * @brief testcase for block creation, verification and decryption
20 #include "gnunet_util_lib.h"
21 #include "gnunet_dnsparser_lib.h"
22 #include "gnunet_gnsrecord_lib.h"
26 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
28 #define TEST_RECORD_DATALEN 123
30 #define TEST_RECORD_DATA 'a'
32 #define TEST_REMOVE_RECORD_TYPE 4321
34 #define TEST_REMOVE_RECORD_DATALEN 255
36 #define TEST_REMOVE_RECORD_DATA 'b'
39 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
41 static struct GNUNET_GNSRECORD_Data *s_rd;
48 static struct GNUNET_GNSRECORD_Data *
49 create_record (int count)
51 struct GNUNET_GNSRECORD_Data *rd;
53 rd = GNUNET_new_array (count, struct GNUNET_GNSRECORD_Data);
54 for (unsigned int c = 0; c < count; c++)
56 rd[c].expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
57 rd[c].record_type = TEST_RECORD_TYPE;
58 rd[c].data_size = TEST_RECORD_DATALEN;
59 rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
60 memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
67 rd_decrypt_cb (void *cls,
68 unsigned int rd_count,
69 const struct GNUNET_GNSRECORD_Data *rd)
71 char rd_cmp_data[TEST_RECORD_DATALEN];
73 GNUNET_assert (RECORDS == rd_count);
74 GNUNET_assert (NULL != rd);
78 for (unsigned int c = 0; c < rd_count; c++)
80 GNUNET_assert (TEST_RECORD_TYPE == rd[c].record_type);
81 GNUNET_assert (TEST_RECORD_DATALEN == rd[c].data_size);
82 GNUNET_assert (0 == memcmp (&rd_cmp_data,
84 TEST_RECORD_DATALEN));
86 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
87 "Block was decrypted successfully \n");
96 const struct GNUNET_CONFIGURATION_Handle *cfg)
98 struct GNUNET_GNSRECORD_Block *block;
99 struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
100 struct GNUNET_HashCode query_pub;
101 struct GNUNET_HashCode query_priv;
102 struct GNUNET_TIME_Absolute expire = GNUNET_TIME_absolute_get();
104 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
105 GNUNET_assert (NULL != privkey);
107 GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
110 /* test query derivation */
111 GNUNET_GNSRECORD_query_from_private_key (privkey,
114 GNUNET_GNSRECORD_query_from_public_key (&pubkey,
117 GNUNET_assert (0 == memcmp (&query_priv,
119 sizeof (struct GNUNET_HashCode)));
121 s_name = "DUMMY.dummy.gnunet";
122 s_rd = create_record (RECORDS);
125 GNUNET_assert (NULL != (block =
126 GNUNET_GNSRECORD_block_create (privkey,
131 GNUNET_assert (GNUNET_OK ==
132 GNUNET_GNSRECORD_block_verify (block));
133 GNUNET_assert (GNUNET_OK ==
134 GNUNET_GNSRECORD_block_decrypt (block,
140 GNUNET_free (privkey);
145 main (int argc, char *argv[])
147 static char *const argvx[] = {
148 "test-gnsrecord-crypto",
151 static struct GNUNET_GETOPT_CommandLineOption options[] = {
152 GNUNET_GETOPT_OPTION_END
156 GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
158 "test-gnsrecord-crypto",
164 /* end of test_gnsrecord_crypto.c */