-remove debug message
[oweals/gnunet.git] / src / gnsrecord / gnunet-gnsrecord-tvg.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2020 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 /**
22  * @file util/gnunet-gns-tvg.c
23  * @brief Generate test vectors for GNS.
24  * @author Martin Schanzenbach
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_signatures.h"
29 #include "gnunet_gns_service.h"
30 #include "gnunet_gnsrecord_lib.h"
31 #include "gnunet_dnsparser_lib.h"
32 #include "gnunet_testing_lib.h"
33 #include <inttypes.h>
34
35 #define TEST_RECORD_LABEL "test"
36 #define TEST_RECORD_A "1.2.3.4"
37 #define TEST_RRCOUNT 2
38
39 static void
40 print_bytes (void *buf,
41              size_t buf_len,
42              int fold)
43 {
44   int i;
45
46   for (i = 0; i < buf_len; i++)
47   {
48     if ((0 != i) && (0 != fold) && (i % fold == 0))
49       printf ("\n");
50     printf ("%02x", ((unsigned char*) buf)[i]);
51   }
52   printf ("\n");
53 }
54
55
56 static void
57 print_record (const struct GNUNET_GNSRECORD_Data *rd)
58 {
59
60   fprintf (stdout,
61            "EXPIRATION: %" PRIu64 "\n", rd->expiration_time);
62   fprintf (stdout,
63            "DATA_SIZE: %zu\n", rd->data_size);
64   fprintf (stdout,
65            "TYPE: %d\n", rd->record_type);
66   fprintf (stdout,
67            "FLAGS: %d\n", rd->flags);
68   fprintf (stdout,
69            "DATA:\n");
70   print_bytes ((char*) rd->data, rd->data_size, 8);
71   fprintf (stdout, "\n");
72 }
73
74
75 /**
76  * Main function that will be run.
77  *
78  * @param cls closure
79  * @param args remaining command-line arguments
80  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
81  * @param cfg configuration
82  */
83 static void
84 run (void *cls,
85      char *const *args,
86      const char *cfgfile,
87      const struct GNUNET_CONFIGURATION_Handle *cfg)
88 {
89   struct GNUNET_GNSRECORD_Data rd[2];
90   struct GNUNET_TIME_Absolute exp_abs = GNUNET_TIME_absolute_get ();
91   struct GNUNET_GNSRECORD_Block *rrblock;
92   char *bdata;
93   struct GNUNET_CRYPTO_EcdsaPrivateKey id_priv;
94   struct GNUNET_CRYPTO_EcdsaPublicKey id_pub;
95   struct GNUNET_CRYPTO_EcdsaPrivateKey pkey_data_p;
96   struct GNUNET_CRYPTO_EcdsaPublicKey pkey_data;
97   void *data;
98   size_t data_size;
99   char *rdata;
100   size_t rdata_size;
101
102   GNUNET_CRYPTO_ecdsa_key_create (&id_priv);
103   GNUNET_CRYPTO_ecdsa_key_get_public (&id_priv,
104                                       &id_pub);
105   fprintf (stdout, "Zone private key (d, little-endian scalar):\n");
106   print_bytes (&id_priv, sizeof(id_priv), 0);
107   fprintf (stdout, "\n");
108   fprintf (stdout, "Zone public key (zk):\n");
109   print_bytes (&id_pub, sizeof(id_pub), 0);
110   fprintf (stdout, "\n");
111
112   GNUNET_CRYPTO_ecdsa_key_create (&pkey_data_p);
113   GNUNET_CRYPTO_ecdsa_key_get_public (&pkey_data_p,
114                                       &pkey_data);
115   fprintf (stdout,
116            "Label: %s\nRRCOUNT: %d\n\n", TEST_RECORD_LABEL, TEST_RRCOUNT);
117   memset (rd, 0, sizeof (struct GNUNET_GNSRECORD_Data) * 2);
118   GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_string_to_value (
119                    GNUNET_DNSPARSER_TYPE_A, TEST_RECORD_A, &data, &data_size));
120   rd[0].data = data;
121   rd[0].data_size = data_size;
122   rd[0].expiration_time = exp_abs.abs_value_us;
123   rd[0].record_type = GNUNET_DNSPARSER_TYPE_A;
124   fprintf (stdout, "Record #0\n");
125   print_record (&rd[0]);
126
127   rd[1].data = &pkey_data;
128   rd[1].data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
129   rd[1].expiration_time = exp_abs.abs_value_us;
130   rd[1].record_type = GNUNET_GNSRECORD_TYPE_PKEY;
131   rd[1].flags = GNUNET_GNSRECORD_RF_PRIVATE;
132   fprintf (stdout, "Record #1\n");
133   print_record (&rd[1]);
134
135   rdata_size = GNUNET_GNSRECORD_records_get_size (2,
136                                                   rd);
137   rdata = GNUNET_malloc (rdata_size);
138   GNUNET_GNSRECORD_records_serialize (2,
139                                       rd,
140                                       rdata_size,
141                                       rdata);
142   fprintf (stdout, "RDATA:\n");
143   print_bytes (rdata, rdata_size, 8);
144   fprintf (stdout, "\n");
145   rrblock = GNUNET_GNSRECORD_block_create (&id_priv,
146                                            exp_abs,
147                                            TEST_RECORD_LABEL,
148                                            rd,
149                                            TEST_RRCOUNT);
150   size_t bdata_size = ntohl (rrblock->purpose.size)
151                       - sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
152                       - sizeof(struct GNUNET_TIME_AbsoluteNBO);
153   size_t rrblock_size = ntohl (rrblock->purpose.size)
154                         + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
155                         + sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
156
157   bdata = (char*) &rrblock[1];
158   fprintf (stdout, "BDATA:\n");
159   print_bytes (bdata, bdata_size, 8);
160   fprintf (stdout, "\n");
161   fprintf (stdout, "RRBLOCK:\n");
162   print_bytes (rrblock, rrblock_size, 8);
163   fprintf (stdout, "\n");
164
165 }
166
167
168 /**
169  * The main function of the test vector generation tool.
170  *
171  * @param argc number of arguments from the command line
172  * @param argv command line arguments
173  * @return 0 ok, 1 on error
174  */
175 int
176 main (int argc,
177       char *const *argv)
178 {
179   const struct GNUNET_GETOPT_CommandLineOption options[] = {
180     GNUNET_GETOPT_OPTION_END
181   };
182
183   GNUNET_assert (GNUNET_OK ==
184                  GNUNET_log_setup ("gnunet-gns-tvg",
185                                    "INFO",
186                                    NULL));
187   if (GNUNET_OK !=
188       GNUNET_PROGRAM_run (argc, argv,
189                           "gnunet-gns-tvg",
190                           "Generate test vectors for GNS",
191                           options,
192                           &run, NULL))
193     return 1;
194   return 0;
195 }
196
197
198 /* end of gnunet-gns-tvg.c */