renaming GNUNET_TIME_relative_get_forever and GNUNET_TIME_absolute_get_forever method...
[oweals/gnunet.git] / src / namestore / test_namestore_api_sign_verify.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_sign_verify.c
22  * @brief testcase for signing and verifying
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 VERBOSE GNUNET_NO
31
32 #define RECORDS 5
33 #define TEST_RECORD_TYPE 1234
34 #define TEST_RECORD_DATALEN 123
35 #define TEST_RECORD_DATA 'a'
36
37 #define TEST_REMOVE_RECORD_TYPE 4321
38 #define TEST_REMOVE_RECORD_DATALEN 255
39 #define TEST_REMOVE_RECORD_DATA 'b'
40
41 static struct GNUNET_CRYPTO_RsaPrivateKey * privkey;
42 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
43 struct GNUNET_CRYPTO_RsaSignature s_signature;
44 struct GNUNET_NAMESTORE_RecordData *s_rd;
45 static char *s_name;
46
47 static int res;
48
49 static struct GNUNET_NAMESTORE_RecordData *
50 create_record (int count)
51 {
52   int c;
53   struct GNUNET_NAMESTORE_RecordData * rd;
54   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
55
56   for (c = 0; c < RECORDS; c++)
57   {
58   rd[c].expiration = GNUNET_TIME_absolute_get();
59   rd[c].record_type = TEST_RECORD_TYPE;
60   rd[c].data_size = TEST_RECORD_DATALEN;
61   rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
62   memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
63   }
64
65   return rd;
66 }
67
68
69
70 static void
71 run (void *cls, char *const *args, const char *cfgfile,
72      const struct GNUNET_CONFIGURATION_Handle *cfg)
73 {
74   struct GNUNET_CRYPTO_RsaSignature * signature;
75
76   /* load privat key */
77   char *hostkey_file;
78   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
79       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
80   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
81   privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
82   GNUNET_free (hostkey_file);
83   GNUNET_assert (privkey != NULL);
84   struct GNUNET_TIME_Absolute expire = GNUNET_TIME_absolute_get();
85   /* get public key */
86   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
87
88   int res_c;
89   int res_w;
90
91   /* create record */
92   s_name = "dummy.dummy.gnunet";
93   s_rd = create_record (RECORDS);
94
95   signature = GNUNET_NAMESTORE_create_signature (privkey, expire, s_name, s_rd, RECORDS);
96   GNUNET_assert (signature != NULL);
97
98   res_c = GNUNET_NAMESTORE_verify_signature(&pubkey, expire, s_name, RECORDS, s_rd, signature);
99   GNUNET_break (res == GNUNET_OK);
100
101   GNUNET_free (signature);
102
103   signature = GNUNET_NAMESTORE_create_signature (privkey, expire, s_name, s_rd, RECORDS);
104   GNUNET_break (signature != NULL);
105
106   GNUNET_log_skip(1, GNUNET_NO);
107   res_w = GNUNET_NAMESTORE_verify_signature(&pubkey, expire, s_name, RECORDS - 1, s_rd, signature);
108   GNUNET_break (res_w == GNUNET_SYSERR);
109
110   GNUNET_free (signature);
111
112   if ((res_c == GNUNET_OK) && (res_w == GNUNET_SYSERR))
113     res = 0;
114   else
115     res = 1;
116
117 }
118
119 static int
120 check ()
121 {
122   static char *const argv[] = { "test-namestore-api",
123     "-c",
124     "test_namestore_api.conf",
125 #if VERBOSE
126     "-L", "DEBUG",
127 #endif
128     NULL
129   };
130   static struct GNUNET_GETOPT_CommandLineOption options[] = {
131     GNUNET_GETOPT_OPTION_END
132   };
133
134   res = 1;
135   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, "test-namestore-api",
136                       "nohelp", options, &run, &res);
137   return res;
138 }
139
140 int
141 main (int argc, char *argv[])
142 {
143   int ret;
144
145   ret = check ();
146
147   return ret;
148 }
149
150 /* end of test_namestore_api_sign_verify.c */