use NULL value in load_path_suffix to NOT load any files
[oweals/gnunet.git] / src / gnsrecord / test_gnsrecord_crypto.c
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_CRYPTO_EcdsaPrivateKey *privkey;
45
46 static struct GNUNET_GNSRECORD_Data *s_rd;
47
48 static char *s_name;
49
50 static int res;
51
52
53 static struct GNUNET_GNSRECORD_Data *
54 create_record (int count)
55 {
56   struct GNUNET_GNSRECORD_Data *rd;
57
58   rd = GNUNET_new_array (count, struct GNUNET_GNSRECORD_Data);
59   for (unsigned int c = 0; c < count; c++)
60   {
61     rd[c].expiration_time = GNUNET_TIME_absolute_get ().abs_value_us
62                             + 1000000000;
63     rd[c].record_type = TEST_RECORD_TYPE;
64     rd[c].data_size = TEST_RECORD_DATALEN;
65     rd[c].data = GNUNET_malloc (TEST_RECORD_DATALEN);
66     memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
67   }
68   return rd;
69 }
70
71
72 static void
73 rd_decrypt_cb (void *cls,
74                unsigned int rd_count,
75                const struct GNUNET_GNSRECORD_Data *rd)
76 {
77   char rd_cmp_data[TEST_RECORD_DATALEN];
78
79   GNUNET_assert (RECORDS == rd_count);
80   GNUNET_assert (NULL != rd);
81   memset (rd_cmp_data,
82           'a',
83           TEST_RECORD_DATALEN);
84   for (unsigned int c = 0; c < rd_count; c++)
85   {
86     GNUNET_assert (TEST_RECORD_TYPE == rd[c].record_type);
87     GNUNET_assert (TEST_RECORD_DATALEN == rd[c].data_size);
88     GNUNET_assert (0 == memcmp (&rd_cmp_data,
89                                 rd[c].data,
90                                 TEST_RECORD_DATALEN));
91   }
92   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
93               "Block was decrypted successfully \n");
94   res = 0;
95 }
96
97
98 static void
99 run (void *cls,
100      char *const *args,
101      const char *cfgfile,
102      const struct GNUNET_CONFIGURATION_Handle *cfg)
103 {
104   struct GNUNET_GNSRECORD_Block *block;
105   struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
106   struct GNUNET_HashCode query_pub;
107   struct GNUNET_HashCode query_priv;
108   struct GNUNET_TIME_Absolute expire = GNUNET_TIME_absolute_get ();
109
110   privkey = GNUNET_CRYPTO_ecdsa_key_create ();
111   GNUNET_assert (NULL != privkey);
112   /* get public key */
113   GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
114                                       &pubkey);
115
116   /* test query derivation */
117   GNUNET_GNSRECORD_query_from_private_key (privkey,
118                                            "testlabel",
119                                            &query_priv);
120   GNUNET_GNSRECORD_query_from_public_key (&pubkey,
121                                           "testlabel",
122                                           &query_pub);
123   GNUNET_assert (0 == memcmp (&query_priv,
124                               &query_pub,
125                               sizeof(struct GNUNET_HashCode)));
126   /* create record */
127   s_name = "DUMMY.dummy.gnunet";
128   s_rd = create_record (RECORDS);
129
130   /* Create block */
131   GNUNET_assert (NULL != (block =
132                             GNUNET_GNSRECORD_block_create (privkey,
133                                                            expire,
134                                                            s_name,
135                                                            s_rd,
136                                                            RECORDS)));
137   GNUNET_assert (GNUNET_OK ==
138                  GNUNET_GNSRECORD_block_verify (block));
139   GNUNET_assert (GNUNET_OK ==
140                  GNUNET_GNSRECORD_block_decrypt (block,
141                                                  &pubkey,
142                                                  s_name,
143                                                  &rd_decrypt_cb,
144                                                  s_name));
145   GNUNET_free (block);
146   GNUNET_free (privkey);
147 }
148
149
150 int
151 main (int argc, char *argv[])
152 {
153   static char *const argvx[] = {
154     "test-gnsrecord-crypto",
155     NULL
156   };
157   static struct GNUNET_GETOPT_CommandLineOption options[] = {
158     GNUNET_GETOPT_OPTION_END
159   };
160
161   res = 1;
162   GNUNET_PROGRAM_run ((sizeof(argvx) / sizeof(char *)) - 1,
163                       argvx,
164                       "test-gnsrecord-crypto",
165                       "nohelp", options,
166                       &run, &res);
167   return res;
168 }
169
170
171 /* end of test_gnsrecord_crypto.c */