35547ebc2fcbf568a0d34d1bd72241290303bd50
[oweals/gnunet.git] / src / gnsrecord / perf_gnsrecord_crypto.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2018 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 /**
19  * @file gnsrecord/test_gnsrecord_crypto.c
20  * @brief testcase for block creation, verification and decryption
21  */
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_gnsrecord_lib.h"
25
26 #define ROUNDS 1000
27
28 #define RECORDS 5
29
30 #define TEST_RECORD_TYPE 1234
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
43 static struct GNUNET_GNSRECORD_Data *
44 create_record (int count)
45 {
46   struct GNUNET_GNSRECORD_Data *rd;
47
48   rd = GNUNET_new_array (count,
49                          struct GNUNET_GNSRECORD_Data);
50   for (unsigned int c = 0; c < count; c++)
51   {
52     rd[c].expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
53     rd[c].record_type = TEST_RECORD_TYPE;
54     rd[c].data_size = TEST_RECORD_DATALEN;
55     rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
56     memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
57   }
58   return rd;
59 }
60
61
62 static void
63 run (void *cls,
64      char *const *args,
65      const char *cfgfile,
66      const struct GNUNET_CONFIGURATION_Handle *cfg)
67 {
68   struct GNUNET_GNSRECORD_Block *block;
69   struct GNUNET_HashCode query;
70   struct GNUNET_GNSRECORD_Data *s_rd;
71   const char *s_name;
72   struct GNUNET_TIME_Absolute start_time;
73   struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
74   struct GNUNET_TIME_Absolute expire;
75
76   (void) cls;
77   (void) args;
78   (void) cfgfile;
79   (void) cfg;
80   expire = GNUNET_TIME_absolute_get();
81   privkey = GNUNET_CRYPTO_ecdsa_key_create ();
82   GNUNET_assert (NULL != privkey);
83
84   /* test block creation */
85   s_name = "DUMMY.dummy.gnunet";
86   s_rd = create_record (RECORDS);
87   start_time = GNUNET_TIME_absolute_get ();
88   for (unsigned int i=0;i<ROUNDS;i++)
89   {
90     GNUNET_assert (NULL != (block =
91                             GNUNET_GNSRECORD_block_create2 (privkey,
92                                                             expire,
93                                                             s_name,
94                                                             s_rd,
95                                                             RECORDS)));
96     GNUNET_GNSRECORD_query_from_private_key (privkey,
97                                              s_name,
98                                              &query);
99     GNUNET_free (block);
100   }
101   fprintf (stderr,
102            "Took %s to produce %u GNS blocks for the DHT\n",
103            GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start_time),
104                                                    GNUNET_YES),
105            ROUNDS);
106   for (unsigned int i=0;i<RECORDS;i++)
107     GNUNET_free ((void *) s_rd[i].data);
108   GNUNET_free (s_rd);
109   GNUNET_free (privkey);
110 }
111
112
113 int
114 main (int argc, char *argv[])
115 {
116   static char *const argvx[] = {
117     "perf-gnsrecord-crypto",
118     NULL
119   };
120   static struct GNUNET_GETOPT_CommandLineOption options[] = {
121     GNUNET_GETOPT_OPTION_END
122   };
123
124   if (GNUNET_OK !=
125       GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
126                           argvx,
127                           "perf-gnsrecord-crypto",
128                           "nohelp", options,
129                           &run,
130                           NULL))
131     return 1;
132   return 0;
133 }
134
135 /* end of test_gnsrecord_crypto.c */