reduce loop counters to more practical levels
[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
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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_gnsrecord_lib.h"
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_CRYPTO_EcdsaPrivateKey * privkey;
44
45 static struct GNUNET_GNSRECORD_Data *s_rd;
46
47 static char *s_name;
48
49 static int res;
50
51
52 static struct GNUNET_GNSRECORD_Data *
53 create_record (int count)
54 {
55   struct GNUNET_GNSRECORD_Data *rd;
56
57   rd = GNUNET_new_array (count, struct GNUNET_GNSRECORD_Data);
58   for (unsigned int c = 0; c < count; c++)
59   {
60     rd[c].expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
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);
65   }
66   return rd;
67 }
68
69
70 static void
71 rd_decrypt_cb (void *cls,
72                unsigned int rd_count,
73                const struct GNUNET_GNSRECORD_Data *rd)
74 {
75   char rd_cmp_data[TEST_RECORD_DATALEN];
76
77   GNUNET_assert (RECORDS == rd_count);
78   GNUNET_assert (NULL != rd);
79   memset (rd_cmp_data,
80           'a',
81           TEST_RECORD_DATALEN);
82   for (unsigned int c = 0; c < rd_count; c++)
83   {
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,
87                                 rd[c].data,
88                                 TEST_RECORD_DATALEN));
89   }
90   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
91               "Block was decrypted successfully \n");
92   res = 0;
93
94 }
95
96 static void
97 run (void *cls,
98      char *const *args,
99      const char *cfgfile,
100      const struct GNUNET_CONFIGURATION_Handle *cfg)
101 {
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
108   privkey = GNUNET_CRYPTO_ecdsa_key_create ();
109   GNUNET_assert (NULL != privkey);
110   /* get public key */
111   GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
112                                       &pubkey);
113
114   /* test query derivation */
115   GNUNET_GNSRECORD_query_from_private_key (privkey,
116                                            "testlabel",
117                                            &query_priv);
118   GNUNET_GNSRECORD_query_from_public_key (&pubkey,
119                                           "testlabel",
120                                           &query_pub);
121   GNUNET_assert (0 == memcmp (&query_priv,
122                               &query_pub,
123                               sizeof (struct GNUNET_HashCode)));
124   /* create record */
125   s_name = "DUMMY.dummy.gnunet";
126   s_rd = create_record (RECORDS);
127
128   /* Create block */
129   GNUNET_assert (NULL != (block =
130                           GNUNET_GNSRECORD_block_create (privkey,
131                                                          expire,
132                                                          s_name,
133                                                          s_rd,
134                                                          RECORDS)));
135   GNUNET_assert (GNUNET_OK ==
136                  GNUNET_GNSRECORD_block_verify (block));
137   GNUNET_assert (GNUNET_OK ==
138                  GNUNET_GNSRECORD_block_decrypt (block,
139                                                  &pubkey,
140                                                  s_name,
141                                                  &rd_decrypt_cb,
142                                                  s_name));
143   GNUNET_free (block);
144   GNUNET_free (privkey);
145 }
146
147
148 int
149 main (int argc, char *argv[])
150 {
151   static char *const argvx[] = {
152     "test-gnsrecord-crypto",
153     NULL
154   };
155   static struct GNUNET_GETOPT_CommandLineOption options[] = {
156     GNUNET_GETOPT_OPTION_END
157   };
158
159   res = 1;
160   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
161                       argvx,
162                       "test-gnsrecord-crypto",
163                       "nohelp", options,
164                       &run, &res);
165   return res;
166 }
167
168 /* end of test_gnsrecord_crypto.c */