paragraph for gnunet devs that don't know how to use the web
[oweals/gnunet.git] / src / namestore / test_namestore_api_remove_not_existing_record.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 Affero 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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file namestore/test_namestore_api_remove_not_existing_record.c
20  * @brief testcase for namestore_api.c
21  */
22 #include "platform.h"
23 #include "gnunet_namestore_service.h"
24 #include "gnunet_testing_lib.h"
25
26 #define TEST_RECORD_TYPE 1234
27
28 #define TEST_RECORD_DATALEN 123
29
30 #define TEST_RECORD_DATA 'a'
31
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
33
34
35 static struct GNUNET_NAMESTORE_Handle *nsh;
36
37 static struct GNUNET_SCHEDULER_Task * endbadly_task;
38
39 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
40
41 static struct GNUNET_CRYPTO_EcdsaPublicKey pubkey;
42
43 static int res;
44
45 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
46
47
48 static void
49 cleanup ()
50 {
51   if (NULL != nsh)
52   {
53     GNUNET_NAMESTORE_disconnect (nsh);
54     nsh = NULL;
55   }
56   if (NULL != privkey)
57   {
58     GNUNET_free (privkey);
59     privkey = NULL;
60   }
61   GNUNET_SCHEDULER_shutdown ();
62 }
63
64
65 /**
66  * Re-establish the connection to the service.
67  *
68  * @param cls handle to use to re-connect.
69  */
70 static void
71 endbadly (void *cls)
72 {
73   if (NULL != nsqe)
74   {
75     GNUNET_NAMESTORE_cancel (nsqe);
76     nsqe = NULL;
77   }
78   cleanup ();
79   res = 1;
80 }
81
82
83 static void
84 end (void *cls)
85 {
86   cleanup ();
87   res = 0;
88 }
89
90
91 static void
92 put_cont (void *cls,
93           int32_t success,
94           const char *emsg)
95 {
96   GNUNET_assert (NULL != cls);
97   nsqe = NULL;
98   if (endbadly_task != NULL)
99   {
100     GNUNET_SCHEDULER_cancel (endbadly_task);
101     endbadly_task = NULL;
102   }
103   switch (success)
104   {
105     case GNUNET_NO:
106       /* We expected GNUNET_NO, since record was not found */
107       GNUNET_SCHEDULER_add_now (&end, NULL);
108       break;
109     case GNUNET_OK:
110       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111                   "Namestore could remove non-existing record: `%s'\n",
112                   (NULL !=emsg) ? emsg : "");
113       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
114       break;
115     case GNUNET_SYSERR:
116     default:
117       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118                   "Namestore failed: `%s'\n",
119                   (NULL !=emsg) ? emsg : "");
120       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
121       break;
122   }
123 }
124
125
126 static void
127 run (void *cls,
128      const struct GNUNET_CONFIGURATION_Handle *cfg,
129      struct GNUNET_TESTING_Peer *peer)
130 {
131   const char * name = "dummy.dummy.gnunet";
132
133   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
134                                                 &endbadly,
135                                                 NULL);
136   privkey = GNUNET_CRYPTO_ecdsa_key_create ();
137   GNUNET_assert (privkey != NULL);
138   GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
139
140   nsh = GNUNET_NAMESTORE_connect (cfg);
141   GNUNET_break (NULL != nsh);
142   nsqe = GNUNET_NAMESTORE_records_store (nsh, privkey, name,
143                                          0, NULL,
144                                          &put_cont, (void *) name);
145   if (NULL == nsqe)
146   {
147     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
148               _("Namestore cannot store no block\n"));
149   }
150 }
151
152
153 int
154 main (int argc, char *argv[])
155 {
156   const char *plugin_name;
157   char *cfg_name;
158
159   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
160   GNUNET_asprintf (&cfg_name,
161                    "test_namestore_api_%s.conf",
162                    plugin_name);
163   GNUNET_DISK_purge_cfg_dir (cfg_name,
164                              "GNUNET_TEST_HOME");
165   res = 1;
166   if (0 !=
167       GNUNET_TESTING_peer_run ("test-namestore-api-remove-non-existing-record",
168                                cfg_name,
169                                &run,
170                                NULL))
171   {
172     res = 1;
173   }
174   GNUNET_DISK_purge_cfg_dir (cfg_name,
175                              "GNUNET_TEST_HOME");
176   GNUNET_free (cfg_name);
177   return res;
178 }
179
180 /* end of test_namestore_api_remove_not_existing_record.c */