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