first batch of license fixes (boring)
[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 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 /**
16  * @file gnsrecord/test_gnsrecord_crypto.c
17  * @brief testcase for block creation, verification and decryption
18  */
19 #include "platform.h"
20 #include "gnunet_util_lib.h"
21 #include "gnunet_dnsparser_lib.h"
22 #include "gnunet_gnsrecord_lib.h"
23
24 #define RECORDS 5
25
26 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
27
28 #define TEST_RECORD_DATALEN 123
29
30 #define TEST_RECORD_DATA 'a'
31
32 #define TEST_REMOVE_RECORD_TYPE 4321
33
34 #define TEST_REMOVE_RECORD_DATALEN 255
35
36 #define TEST_REMOVE_RECORD_DATA 'b'
37
38
39 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
40
41 static struct GNUNET_GNSRECORD_Data *s_rd;
42
43 static char *s_name;
44
45 static int res;
46
47
48 static struct GNUNET_GNSRECORD_Data *
49 create_record (int count)
50 {
51   struct GNUNET_GNSRECORD_Data *rd;
52
53   rd = GNUNET_new_array (count, struct GNUNET_GNSRECORD_Data);
54   for (unsigned int c = 0; c < count; c++)
55   {
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);
61   }
62   return rd;
63 }
64
65
66 static void
67 rd_decrypt_cb (void *cls,
68                unsigned int rd_count,
69                const struct GNUNET_GNSRECORD_Data *rd)
70 {
71   char rd_cmp_data[TEST_RECORD_DATALEN];
72
73   GNUNET_assert (RECORDS == rd_count);
74   GNUNET_assert (NULL != rd);
75   memset (rd_cmp_data,
76           'a',
77           TEST_RECORD_DATALEN);
78   for (unsigned int c = 0; c < rd_count; c++)
79   {
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,
83                                 rd[c].data,
84                                 TEST_RECORD_DATALEN));
85   }
86   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
87               "Block was decrypted successfully \n");
88   res = 0;
89
90 }
91
92 static void
93 run (void *cls,
94      char *const *args,
95      const char *cfgfile,
96      const struct GNUNET_CONFIGURATION_Handle *cfg)
97 {
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();
103
104   privkey = GNUNET_CRYPTO_ecdsa_key_create ();
105   GNUNET_assert (NULL != privkey);
106   /* get public key */
107   GNUNET_CRYPTO_ecdsa_key_get_public (privkey,
108                                       &pubkey);
109
110   /* test query derivation */
111   GNUNET_GNSRECORD_query_from_private_key (privkey,
112                                            "testlabel",
113                                            &query_priv);
114   GNUNET_GNSRECORD_query_from_public_key (&pubkey,
115                                           "testlabel",
116                                           &query_pub);
117   GNUNET_assert (0 == memcmp (&query_priv,
118                               &query_pub,
119                               sizeof (struct GNUNET_HashCode)));
120   /* create record */
121   s_name = "DUMMY.dummy.gnunet";
122   s_rd = create_record (RECORDS);
123
124   /* Create block */
125   GNUNET_assert (NULL != (block =
126                           GNUNET_GNSRECORD_block_create (privkey,
127                                                          expire,
128                                                          s_name,
129                                                          s_rd,
130                                                          RECORDS)));
131   GNUNET_assert (GNUNET_OK ==
132                  GNUNET_GNSRECORD_block_verify (block));
133   GNUNET_assert (GNUNET_OK ==
134                  GNUNET_GNSRECORD_block_decrypt (block,
135                                                  &pubkey,
136                                                  s_name,
137                                                  &rd_decrypt_cb,
138                                                  s_name));
139   GNUNET_free (block);
140   GNUNET_free (privkey);
141 }
142
143
144 int
145 main (int argc, char *argv[])
146 {
147   static char *const argvx[] = {
148     "test-gnsrecord-crypto",
149     NULL
150   };
151   static struct GNUNET_GETOPT_CommandLineOption options[] = {
152     GNUNET_GETOPT_OPTION_END
153   };
154
155   res = 1;
156   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
157                       argvx,
158                       "test-gnsrecord-crypto",
159                       "nohelp", options,
160                       &run, &res);
161   return res;
162 }
163
164 /* end of test_gnsrecord_crypto.c */