fix type
[oweals/gnunet.git] / src / namestore / test_namestore_api_blocks.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file namestore/test_namestore_api_blocks.c
22  * @brief testcase for block creation, verification and decryption
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_namestore_service.h"
27 #include "namestore.h"
28 #include "gnunet_signatures.h"
29
30 #define RECORDS 5
31
32 #define TEST_RECORD_TYPE 1234
33
34 #define TEST_RECORD_DATALEN 123
35
36 #define TEST_RECORD_DATA 'a'
37
38 #define TEST_REMOVE_RECORD_TYPE 4321
39
40 #define TEST_REMOVE_RECORD_DATALEN 255
41
42 #define TEST_REMOVE_RECORD_DATA 'b'
43
44
45 static struct GNUNET_CRYPTO_EccPrivateKey * privkey;
46
47 static struct GNUNET_NAMESTORE_RecordData *s_rd;
48
49 static char *s_name;
50
51 static int res;
52
53
54 static struct GNUNET_NAMESTORE_RecordData *
55 create_record (int count)
56 {
57   unsigned int c;
58   struct GNUNET_NAMESTORE_RecordData * rd;
59
60   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
61   for (c = 0; c < count; c++)
62   {
63     rd[c].expiration_time = GNUNET_TIME_absolute_get().abs_value_us;
64     rd[c].record_type = TEST_RECORD_TYPE;
65     rd[c].data_size = TEST_RECORD_DATALEN;
66     rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
67     memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
68   }
69   return rd;
70 }
71
72
73 static void
74 rd_decrypt_cb (void *cls,
75                                                  unsigned int rd_count,
76                                                  const struct GNUNET_NAMESTORE_RecordData *rd)
77 {
78   char rd_cmp_data[TEST_RECORD_DATALEN];
79
80   int c;
81
82   GNUNET_assert (RECORDS == rd_count);
83   GNUNET_assert (NULL != rd);
84
85   memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN);
86
87   for (c = 0; c < rd_count; c++)
88   {
89         GNUNET_assert (TEST_RECORD_TYPE == rd[c].record_type);
90         GNUNET_assert (TEST_RECORD_DATALEN == rd[c].data_size);
91         GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[c].data, TEST_RECORD_DATALEN));
92   }
93   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
94               "Block was decrypted successfully \n");
95   res = 0;
96
97 }
98
99 static void
100 run (void *cls, char *const *args, const char *cfgfile,
101      const struct GNUNET_CONFIGURATION_Handle *cfg)
102 {
103   struct GNUNET_NAMESTORE_Block *block;
104   struct GNUNET_CRYPTO_EccPublicSignKey pubkey;
105
106   /* load privat key */
107   char *hostkey_file;
108   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
109       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
110   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
111   privkey = GNUNET_CRYPTO_ecc_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_ecc_key_get_public_for_signature(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_NAMESTORE_block_create (privkey, expire,s_name, s_rd, RECORDS)));
124   GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_block_verify (block));
125   GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_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-namestore-api",
135     "-c",
136     "test_namestore_api.conf",
137     NULL
138   };
139   static struct GNUNET_GETOPT_CommandLineOption options[] = {
140     GNUNET_GETOPT_OPTION_END
141   };
142
143   res = 1;
144   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx, "test-namestore-api",
145                       "nohelp", options, &run, &res);
146   return res;
147 }
148
149 /* end of test_namestore_api_blocks.c */