-fixing IO-error bug in namestore tests on buildbots, simplifying testcases by using...
[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 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_RsaPrivateKey * privkey;
46
47 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pubkey;
48
49 static struct GNUNET_NAMESTORE_RecordData *s_rd;
50
51 static char *s_name;
52
53 static int res;
54
55
56 static struct GNUNET_NAMESTORE_RecordData *
57 create_record (int count)
58 {
59   int c;
60   struct GNUNET_NAMESTORE_RecordData * rd;
61   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
62
63   for (c = 0; c < RECORDS; c++)
64   {
65   rd[c].expiration = GNUNET_TIME_absolute_get();
66   rd[c].record_type = TEST_RECORD_TYPE;
67   rd[c].data_size = TEST_RECORD_DATALEN;
68   rd[c].data = GNUNET_malloc(TEST_RECORD_DATALEN);
69   memset ((char *) rd[c].data, TEST_RECORD_DATA, TEST_RECORD_DATALEN);
70   }
71
72   return rd;
73 }
74
75
76 static void
77 run (void *cls, char *const *args, const char *cfgfile,
78      const struct GNUNET_CONFIGURATION_Handle *cfg)
79 {
80   struct GNUNET_CRYPTO_RsaSignature * signature;
81
82   /* load privat key */
83   char *hostkey_file;
84   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
85       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
86   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
87   privkey = GNUNET_CRYPTO_rsa_key_create_from_file(hostkey_file);
88   GNUNET_free (hostkey_file);
89   GNUNET_assert (privkey != NULL);
90   struct GNUNET_TIME_Absolute expire = GNUNET_TIME_absolute_get();
91   /* get public key */
92   GNUNET_CRYPTO_rsa_key_get_public(privkey, &pubkey);
93
94   int res_c;
95   int res_w;
96
97   /* create record */
98   s_name = "dummy.dummy.gnunet";
99   s_rd = create_record (RECORDS);
100
101   signature = GNUNET_NAMESTORE_create_signature (privkey, expire, s_name, s_rd, RECORDS);
102   GNUNET_assert (signature != NULL);
103
104   res_c = GNUNET_NAMESTORE_verify_signature(&pubkey, expire, s_name, RECORDS, s_rd, signature);
105   GNUNET_break (res == GNUNET_OK);
106
107   GNUNET_free (signature);
108
109   signature = GNUNET_NAMESTORE_create_signature (privkey, expire, s_name, s_rd, RECORDS);
110   GNUNET_break (signature != NULL);
111
112   GNUNET_log_skip(1, GNUNET_NO);
113   res_w = GNUNET_NAMESTORE_verify_signature(&pubkey, expire, s_name, RECORDS - 1, s_rd, signature);
114   GNUNET_break (res_w == GNUNET_SYSERR);
115
116   GNUNET_free (signature);
117
118   if ((res_c == GNUNET_OK) && (res_w == GNUNET_SYSERR))
119     res = 0;
120   else
121     res = 1;
122
123 }
124
125
126 int
127 main (int argc, char *argv[])
128 {
129   static char *const argvx[] = { "test-namestore-api",
130     "-c",
131     "test_namestore_api.conf",
132     NULL
133   };
134   static struct GNUNET_GETOPT_CommandLineOption options[] = {
135     GNUNET_GETOPT_OPTION_END
136   };
137
138   res = 1;
139   GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx, "test-namestore-api",
140                       "nohelp", options, &run, &res);
141   return res;
142 }
143
144 /* end of test_namestore_api_sign_verify.c */