fix #3869: outdated FSF address
[oweals/gnunet.git] / src / gnsrecord / test_gnsrecord_crypto.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 Christian Grothoff (and other contributing authors)
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   unsigned int c;
56   struct GNUNET_GNSRECORD_Data *rd;
57
58   rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
59   for (c = 0; c < count; c++)
60   {
61     rd[c].expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 1000000000;
62     rd[c].record_type = TEST_RECORD_TYPE;
63     rd[c].data_size = TEST_RECORD_DATALEN;
64     rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
65     memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
66   }
67   return rd;
68 }
69
70
71 static void
72 rd_decrypt_cb (void *cls,
73                unsigned int rd_count,
74                const struct GNUNET_GNSRECORD_Data *rd)
75 {
76   char rd_cmp_data[TEST_RECORD_DATALEN];
77   int c;
78
79   GNUNET_assert (RECORDS == rd_count);
80   GNUNET_assert (NULL != rd);
81
82   memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN);
83   for (c = 0; c < rd_count; c++)
84   {
85     GNUNET_assert (TEST_RECORD_TYPE == rd[c].record_type);
86     GNUNET_assert (TEST_RECORD_DATALEN == rd[c].data_size);
87     GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[c].data, TEST_RECORD_DATALEN));
88   }
89   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
90               "Block was decrypted successfully \n");
91   res = 0;
92
93 }
94
95 static void
96 run (void *cls, char *const *args, const char *cfgfile,
97      const struct GNUNET_CONFIGURATION_Handle *cfg)
98 {
99   struct GNUNET_GNSRECORD_Block *block;
100   struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
101
102   /* load privat key */
103   char *hostkey_file;
104   GNUNET_asprintf(&hostkey_file,
105                   "zonefiles%s%s",
106                   DIR_SEPARATOR_STR,
107                   "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
109               "Using zonekey file `%s'\n",
110               hostkey_file);
111   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
112   GNUNET_free (hostkey_file);
113   GNUNET_assert (privkey != NULL);
114   struct GNUNET_TIME_Absolute expire = GNUNET_TIME_absolute_get();
115   /* get public key */
116   GNUNET_CRYPTO_ecdsa_key_get_public(privkey, &pubkey);
117
118   /* create record */
119   s_name = "DUMMY.dummy.gnunet";
120   s_rd = create_record (RECORDS);
121
122   /* Create block */
123   GNUNET_assert (NULL != (block = GNUNET_GNSRECORD_block_create (privkey, expire,s_name, s_rd, RECORDS)));
124   GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_verify (block));
125   GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt (block, &pubkey, s_name, &rd_decrypt_cb, s_name));
126
127   GNUNET_free (block);
128 }
129
130
131 int
132 main (int argc, char *argv[])
133 {
134   static char *const argvx[] = { "test-gnsrecord-crypto",
135     NULL
136   };
137   static struct GNUNET_GETOPT_CommandLineOption options[] = {
138     GNUNET_GETOPT_OPTION_END
139   };
140
141   res = 1;
142   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx, "test-namestore-api",
143                       "nohelp", options, &run, &res);
144   return res;
145 }
146
147 /* end of test_gnsrecord_crypto.c */