psyc/store: apply state modifiers
[oweals/gnunet.git] / src / psycstore / test_plugin_psycstore.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 /*
22  * @file psycstore/test_plugin_psycstore.c
23  * @brief Test for the PSYCstore plugins.
24  * @author Gabor X Toth
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_testing_lib.h"
30 #include "gnunet_psycstore_plugin.h"
31 #include "gnunet_psycstore_service.h"
32 #include "gnunet_multicast_service.h"
33
34 #define DEBUG_PSYCSTORE GNUNET_EXTRA_LOGGING
35 #if DEBUG_PSYCSTORE
36 # define LOG_LEVEL "DEBUG"
37 #else
38 # define LOG_LEVEL "WARNING"
39 #endif
40
41 #define C2ARG(str) str, (sizeof (str) - 1)
42
43 #define LOG(kind,...)                                                          \
44   GNUNET_log_from (kind, "test-plugin-psycstore", __VA_ARGS__)
45
46 static int ok;
47
48 /**
49  * Name of plugin under test.
50  */
51 static const char *plugin_name;
52
53 static struct GNUNET_CRYPTO_EddsaPrivateKey *channel_key;
54 static struct GNUNET_CRYPTO_EcdsaPrivateKey *slave_key;
55
56 static struct GNUNET_CRYPTO_EddsaPublicKey channel_pub_key;
57 static struct GNUNET_CRYPTO_EcdsaPublicKey slave_pub_key;
58
59 /**
60  * Function called when the service shuts down.  Unloads our psycstore
61  * plugin.
62  *
63  * @param api api to unload
64  */
65 static void
66 unload_plugin (struct GNUNET_PSYCSTORE_PluginFunctions *api)
67 {
68   char *libname;
69
70   GNUNET_asprintf (&libname, "libgnunet_plugin_psycstore_%s", plugin_name);
71   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
72   GNUNET_free (libname);
73 }
74
75
76 /**
77  * Load the psycstore plugin.
78  *
79  * @param cfg configuration to pass
80  * @return NULL on error
81  */
82 static struct GNUNET_PSYCSTORE_PluginFunctions *
83 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
84 {
85   struct GNUNET_PSYCSTORE_PluginFunctions *ret;
86   char *libname;
87
88   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' psycstore plugin\n"),
89               plugin_name);
90   GNUNET_asprintf (&libname, "libgnunet_plugin_psycstore_%s", plugin_name);
91   if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
92   {
93     FPRINTF (stderr, "Failed to load plugin `%s'!\n", plugin_name);
94     return NULL;
95   }
96   GNUNET_free (libname);
97   return ret;
98 }
99
100
101 struct FragmentClosure
102 {
103   uint8_t n;
104   uint64_t flags[16];
105   struct GNUNET_MULTICAST_MessageHeader *msg[16];
106 };
107
108 static int
109 fragment_cb (void *cls, struct GNUNET_MULTICAST_MessageHeader *msg2,
110              enum GNUNET_PSYCSTORE_MessageFlags flags)
111 {
112   struct FragmentClosure *fcls = cls;
113   struct GNUNET_MULTICAST_MessageHeader *msg1 = fcls->msg[fcls->n];
114   uint64_t flags1 = fcls->flags[fcls->n++];
115   int ret;
116
117   if (flags1 == flags && msg1->header.size == msg2->header.size
118       && 0 == memcmp (msg1, msg2, ntohs (msg1->header.size)))
119   {
120     LOG (GNUNET_ERROR_TYPE_DEBUG, "Fragment %llu matches\n",
121          GNUNET_ntohll (msg1->fragment_id));
122     ret = GNUNET_YES;
123   }
124   else
125   {
126     LOG (GNUNET_ERROR_TYPE_ERROR, "Fragment %llu differs\n",
127          GNUNET_ntohll (msg1->fragment_id));
128     ret = GNUNET_SYSERR;
129   }
130
131   GNUNET_free (msg2);
132   return ret;
133 }
134
135
136 struct StateClosure {
137   size_t n;
138   char *name[16];
139   void *value[16];
140   size_t value_size[16];
141 };
142
143 static int
144 state_cb (void *cls, const char *name, const void *value, size_t value_size)
145 {
146   struct StateClosure *scls = cls;
147   const void *val = scls->value[scls->n];
148   size_t val_size = scls->value_size[scls->n++];
149
150   /* FIXME: check name */
151
152   return value_size == val_size && 0 == memcmp (value, val, val_size)
153     ? GNUNET_YES
154     : GNUNET_SYSERR;
155 }
156
157
158 static void
159 run (void *cls, char *const *args, const char *cfgfile,
160      const struct GNUNET_CONFIGURATION_Handle *cfg)
161 {
162   struct GNUNET_PSYCSTORE_PluginFunctions *db;
163
164   ok = 1;
165   db = load_plugin (cfg);
166   if (NULL == db)
167   {
168     FPRINTF (stderr,
169              "%s",
170              "Failed to initialize PSYCstore.  "
171              "Database likely not setup, skipping test.\n");
172     return;
173   }
174
175   /* Store & test membership */
176
177   channel_key = GNUNET_CRYPTO_eddsa_key_create ();
178   slave_key = GNUNET_CRYPTO_ecdsa_key_create ();
179
180   GNUNET_CRYPTO_eddsa_key_get_public (channel_key,
181                                                   &channel_pub_key);
182   GNUNET_CRYPTO_ecdsa_key_get_public (slave_key, &slave_pub_key);
183
184   GNUNET_assert (GNUNET_OK == db->membership_store (db->cls, &channel_pub_key,
185                                                     &slave_pub_key, GNUNET_YES,
186                                                     4, 2, 1));
187
188   GNUNET_assert (GNUNET_YES == db->membership_test (db->cls, &channel_pub_key,
189                                                     &slave_pub_key, 4));
190
191   GNUNET_assert (GNUNET_YES == db->membership_test (db->cls, &channel_pub_key,
192                                                     &slave_pub_key, 2));
193
194   GNUNET_assert (GNUNET_NO == db->membership_test (db->cls, &channel_pub_key,
195                                                    &slave_pub_key, 1));
196
197
198   /* Store & get messages */
199
200   struct GNUNET_MULTICAST_MessageHeader *msg
201     = GNUNET_malloc (sizeof (*msg) + sizeof (channel_pub_key));
202   GNUNET_assert (msg != NULL);
203
204   msg->header.type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE);
205   msg->header.size = htons (sizeof (*msg) + sizeof (channel_pub_key));
206
207   uint64_t fragment_id = INT64_MAX - 1;
208   msg->fragment_id = GNUNET_htonll (fragment_id);
209
210   uint64_t message_id = INT64_MAX - 10;
211   msg->message_id = GNUNET_htonll (message_id);
212
213   uint64_t group_generation = INT64_MAX - 3;
214   msg->group_generation = GNUNET_htonll (group_generation);
215
216   msg->hop_counter = htonl (9);
217   msg->fragment_offset = GNUNET_htonll (0);
218   msg->flags = htonl (GNUNET_MULTICAST_MESSAGE_LAST_FRAGMENT);
219
220   memcpy (&msg[1], &channel_pub_key, sizeof (channel_pub_key));
221
222   msg->purpose.size = htonl (ntohs (msg->header.size)
223                              - sizeof (msg->header)
224                              - sizeof (msg->hop_counter)
225                              - sizeof (msg->signature));
226   msg->purpose.purpose = htonl (234);
227   GNUNET_assert (GNUNET_OK ==
228                  GNUNET_CRYPTO_eddsa_sign (channel_key, &msg->purpose, &msg->signature));
229
230   struct FragmentClosure fcls = { 0 };
231   fcls.n = 0;
232   fcls.msg[0] = msg;
233   fcls.flags[0] = GNUNET_PSYCSTORE_MESSAGE_STATE;
234
235   GNUNET_assert (
236     GNUNET_OK == db->fragment_store (db->cls, &channel_pub_key, msg,
237                                      fcls.flags[0]));
238
239   uint64_t ret_frags = 0;
240   GNUNET_assert (
241     GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
242                                    fragment_id, fragment_id,
243                                    &ret_frags, fragment_cb, &fcls));
244   GNUNET_assert (fcls.n == 1);
245
246   // FIXME: test fragment_get_latest and message_get_latest
247
248   fcls.n = 0;
249
250   GNUNET_assert (
251     GNUNET_OK == db->message_get_fragment (db->cls, &channel_pub_key,
252                                            GNUNET_ntohll (msg->message_id),
253                                            GNUNET_ntohll (msg->fragment_offset),
254                                            fragment_cb, &fcls));
255   GNUNET_assert (fcls.n == 1);
256
257   GNUNET_assert (
258     GNUNET_OK == db->message_add_flags (db->cls, &channel_pub_key,
259                                         GNUNET_ntohll (msg->message_id),
260                                         GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED));
261
262   fcls.n = 0;
263   fcls.flags[0] |= GNUNET_PSYCSTORE_MESSAGE_STATE_APPLIED;
264
265   GNUNET_assert (
266     GNUNET_OK == db->fragment_get (db->cls, &channel_pub_key,
267                                    fragment_id, fragment_id,
268                                    &ret_frags, fragment_cb, &fcls));
269   GNUNET_assert (fcls.n == 1);
270
271   struct GNUNET_MULTICAST_MessageHeader *msg1
272     = GNUNET_malloc (sizeof (*msg1) + sizeof (channel_pub_key));
273
274   memcpy (msg1, msg, sizeof (*msg1) + sizeof (channel_pub_key));
275
276   msg1->fragment_id = GNUNET_htonll (INT64_MAX);
277   msg1->fragment_offset = GNUNET_htonll (32768);
278
279   fcls.n = 0;
280   fcls.msg[1] = msg1;
281   fcls.flags[1] = GNUNET_PSYCSTORE_MESSAGE_STATE_HASH;
282
283   GNUNET_assert (GNUNET_OK == db->fragment_store (db->cls, &channel_pub_key, msg1,
284                                                   fcls.flags[1]));
285
286   GNUNET_assert (
287     GNUNET_OK == db->message_get (db->cls, &channel_pub_key,
288                                   message_id, message_id,
289                                   &ret_frags, fragment_cb, &fcls));
290   GNUNET_assert (fcls.n == 2 && ret_frags == 2);
291
292   /* Message counters */
293
294   fragment_id = 0;
295   message_id = 0;
296   group_generation = 0;
297   GNUNET_assert (
298     GNUNET_OK == db->counters_message_get (db->cls, &channel_pub_key,
299                                            &fragment_id, &message_id,
300                                            &group_generation)
301     && fragment_id == GNUNET_ntohll (msg1->fragment_id)
302     && message_id == GNUNET_ntohll (msg1->message_id)
303     && group_generation == GNUNET_ntohll (msg1->group_generation));
304
305   /* Modify state */
306
307   message_id = GNUNET_ntohll (fcls.msg[0]->message_id) + 1;
308   GNUNET_assert (GNUNET_OK == db->state_modify_begin (db->cls, &channel_pub_key,
309                                                       message_id, 0));
310
311   GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
312                                                    GNUNET_ENV_OP_ASSIGN,
313                                                    "_foo",
314                                                    C2ARG("one two three")));
315
316   GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
317                                                    GNUNET_ENV_OP_ASSIGN,
318                                                    "_foo_bar", slave_key,
319                                                    sizeof (*slave_key)));
320
321   GNUNET_assert (GNUNET_OK == db->state_modify_end (db->cls, &channel_pub_key,
322                                                     message_id));
323
324   struct StateClosure scls = { 0 };
325   scls.n = 0;
326   scls.value[0] = "one two three";
327   scls.value_size[0] = strlen ("one two three");
328
329   GNUNET_assert (GNUNET_OK == db->state_get (db->cls, &channel_pub_key, "_foo",
330                                              state_cb, &scls));
331   GNUNET_assert (scls.n == 1);
332
333   scls.n = 0;
334   scls.value[1] = slave_key;
335   scls.value_size[1] = sizeof (*slave_key);
336
337   GNUNET_assert (GNUNET_OK == db->state_get_prefix (db->cls, &channel_pub_key,
338                                                     "_foo", state_cb, &scls));
339   GNUNET_assert (scls.n == 2);
340
341   scls.n = 0;
342   GNUNET_assert (GNUNET_NO == db->state_get_signed (db->cls, &channel_pub_key,
343                                                     state_cb, &scls));
344   GNUNET_assert (scls.n == 0);
345
346   GNUNET_assert (GNUNET_OK == db->state_update_signed (db->cls,
347                                                        &channel_pub_key));
348
349   scls.n = 0;
350   GNUNET_assert (GNUNET_YES == db->state_get_signed (db->cls, &channel_pub_key,
351                                                      state_cb, &scls));
352   GNUNET_assert (scls.n == 2);
353
354   /* State counters */
355
356   uint64_t max_state_msg_id = 0;
357   GNUNET_assert (GNUNET_OK == db->counters_state_get (db->cls, &channel_pub_key,
358                                                       &max_state_msg_id)
359                  && max_state_msg_id == message_id);
360
361   /* State sync */
362
363   scls.n = 0;
364   scls.value[0] = channel_key;
365   scls.value_size[0] = sizeof (*channel_key);
366   scls.value[1] = "three two one";
367   scls.value_size[1] = strlen ("three two one");
368
369   GNUNET_assert (GNUNET_OK == db->state_sync_begin (db->cls, &channel_pub_key));
370
371   GNUNET_assert (GNUNET_OK == db->state_sync_assign (db->cls, &channel_pub_key,
372                                                      "_sync_bar", scls.value[0],
373                                                      scls.value_size[0]));
374
375   GNUNET_assert (GNUNET_OK == db->state_sync_assign (db->cls, &channel_pub_key,
376                                                      "_sync_foo", scls.value[1],
377                                                      scls.value_size[1]));
378
379   GNUNET_assert (GNUNET_OK == db->state_sync_end (db->cls, &channel_pub_key,
380                                                   max_state_msg_id,
381                                                   INT64_MAX - 5));
382
383   GNUNET_assert (GNUNET_NO == db->state_get_prefix (db->cls, &channel_pub_key,
384                                                     "_foo", state_cb, &scls));
385   GNUNET_assert (scls.n == 0);
386
387   GNUNET_assert (GNUNET_OK == db->state_get_prefix (db->cls, &channel_pub_key,
388                                                     "_sync", state_cb, &scls));
389   GNUNET_assert (scls.n == 2);
390
391   scls.n = 0;
392   GNUNET_assert (GNUNET_OK == db->state_get_signed (db->cls, &channel_pub_key,
393                                                     state_cb, &scls));
394   GNUNET_assert (scls.n == 2);
395
396   /* Modify state after sync */
397
398   message_id = GNUNET_ntohll (fcls.msg[0]->message_id) + 6;
399   GNUNET_assert (GNUNET_OK == db->state_modify_begin (db->cls, &channel_pub_key,
400                                                       message_id,
401                                                       message_id - max_state_msg_id));
402
403   GNUNET_assert (GNUNET_OK == db->state_modify_op (db->cls, &channel_pub_key,
404                                                    GNUNET_ENV_OP_ASSIGN,
405                                                    "_sync_foo",
406                                                    C2ARG("five six seven")));
407
408   GNUNET_assert (GNUNET_OK == db->state_modify_end (db->cls, &channel_pub_key,
409                                                     message_id));
410
411   /* Reset state */
412
413   scls.n = 0;
414   GNUNET_assert (GNUNET_OK == db->state_reset (db->cls, &channel_pub_key));
415   GNUNET_assert (scls.n == 0);
416
417   ok = 0;
418
419   if (NULL != channel_key)
420   {
421     GNUNET_free (channel_key);
422     channel_key = NULL;
423   }
424   if (NULL != slave_key)
425   {
426     GNUNET_free (slave_key);
427     slave_key = NULL;
428   }
429
430   unload_plugin (db);
431 }
432
433
434 int
435 main (int argc, char *argv[])
436 {
437   char cfg_name[128];
438   char *const xargv[] = {
439     "test-plugin-psycstore",
440     "-c", cfg_name,
441     "-L", LOG_LEVEL,
442     NULL
443   };
444   struct GNUNET_GETOPT_CommandLineOption options[] = {
445     GNUNET_GETOPT_OPTION_END
446   };
447   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-psycstore-sqlite");
448   GNUNET_log_setup ("test-plugin-psycstore", LOG_LEVEL, NULL);
449   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
450   GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_plugin_psycstore_%s.conf",
451                    plugin_name);
452   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
453                       "test-plugin-psycstore", "nohelp", options, &run, NULL);
454
455   if (ok != 0)
456     FPRINTF (stderr, "Missed some testcases: %d\n", ok);
457
458 #if ! DEBUG_PSYCSTORE
459   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-psycstore-sqlite");
460 #endif
461
462   return ok;
463 }
464
465 /* end of test_plugin_psycstore.c */