Wait on actual HANDLE, not the structure that contains it
[oweals/gnunet.git] / src / peerstore / test_peerstore_api_sync.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C)
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 peerstore/test_peerstore_api_sync.c
22  * @brief testcase for peerstore sync before disconnect feature
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include "gnunet_testing_lib.h"
27 #include "gnunet_peerstore_service.h"
28
29 static int ok = 1;
30
31 static const struct GNUNET_CONFIGURATION_Handle *cfg;
32
33 static struct GNUNET_PEERSTORE_Handle *h;
34
35 static char *subsystem = "test_peerstore_api_sync";
36 static struct GNUNET_PeerIdentity pid;
37 static char *key = "test_peerstore_api_store_key";
38 static char *val = "test_peerstore_api_store_val";
39
40 static int
41 iterate_cb (void *cls, const struct GNUNET_PEERSTORE_Record *record,
42             const char *emsg)
43 {
44   const char *rec_val;
45
46   GNUNET_break (NULL == emsg);
47   if (NULL == record)
48   {
49     GNUNET_PEERSTORE_disconnect (h, GNUNET_NO);
50     GNUNET_SCHEDULER_shutdown ();
51     return GNUNET_YES;
52   }
53   rec_val = record->value;
54   GNUNET_break (0 == strcmp (rec_val, val));
55   ok = 0;
56   return GNUNET_YES;
57 }
58
59
60 static void
61 test1 ()
62 {
63   GNUNET_PEERSTORE_store (h, subsystem, &pid, key, val, strlen (val) + 1,
64                           GNUNET_TIME_UNIT_FOREVER_ABS,
65                           GNUNET_PEERSTORE_STOREOPTION_REPLACE, NULL, NULL);
66   GNUNET_PEERSTORE_disconnect (h, GNUNET_YES);
67   h = GNUNET_PEERSTORE_connect (cfg);
68   GNUNET_PEERSTORE_iterate (h, subsystem, &pid, key,
69                             GNUNET_TIME_UNIT_FOREVER_REL, &iterate_cb, NULL);
70 }
71
72
73 static void
74 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *c,
75      struct GNUNET_TESTING_Peer *peer)
76 {
77   cfg = c;
78   h = GNUNET_PEERSTORE_connect (cfg);
79   GNUNET_assert (NULL != h);
80   memset (&pid, 1, sizeof (pid));
81   test1 ();
82 }
83
84
85 int
86 main (int argc, char *argv[])
87 {
88   if (0 !=
89       GNUNET_TESTING_service_run ("test-gnunet-peerstore", "peerstore",
90                                   "test_peerstore_api_data.conf", &run, NULL))
91     return 1;
92   return ok;
93 }
94
95 /* end of test_peerstore_api_store.c */