Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / peerstore / test_peerstore_api_iterate.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013-2017 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  * @file peerstore/test_peerstore_api_iterate.c
22  * @brief testcase for peerstore iteration operation
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 struct GNUNET_PEERSTORE_Handle *h;
32 static struct GNUNET_PEERSTORE_IterateContext *ic;
33
34 static char *ss = "test_peerstore_api_iterate";
35 static struct GNUNET_PeerIdentity p1;
36 static struct GNUNET_PeerIdentity p2;
37 static char *k1 = "test_peerstore_api_iterate_key1";
38 static char *k2 = "test_peerstore_api_iterate_key2";
39 static char *k3 = "test_peerstore_api_iterate_key3";
40 static char *val = "test_peerstore_api_iterate_val";
41 static int count = 0;
42
43
44 static void
45 iter3_cb (void *cls,
46           const struct GNUNET_PEERSTORE_Record *record,
47           const char *emsg)
48 {
49   if (NULL != emsg)
50   {
51     GNUNET_PEERSTORE_iterate_cancel (ic);
52     return;
53   }
54   if (NULL != record)
55   {
56     count++;
57     return;
58   }
59   GNUNET_assert (count == 3);
60   ok = 0;
61   GNUNET_PEERSTORE_disconnect (h, GNUNET_NO);
62   GNUNET_SCHEDULER_shutdown ();
63 }
64
65
66 static void
67 iter2_cb (void *cls,
68           const struct GNUNET_PEERSTORE_Record *record,
69           const char *emsg)
70 {
71   if (NULL != emsg)
72   {
73     GNUNET_PEERSTORE_iterate_cancel (ic);
74     return;
75   }
76   if (NULL != record)
77   {
78     count++;
79     return;
80   }
81   GNUNET_assert (count == 2);
82   count = 0;
83   ic = GNUNET_PEERSTORE_iterate (h,
84                                  ss,
85                                  NULL,
86                                  NULL,
87                                  GNUNET_TIME_UNIT_FOREVER_REL,
88                                  &iter3_cb,
89                                  NULL);
90 }
91
92
93 static void
94 iter1_cb (void *cls,
95           const struct GNUNET_PEERSTORE_Record *record,
96           const char *emsg)
97 {
98   if (NULL != emsg)
99   {
100     GNUNET_PEERSTORE_iterate_cancel (ic);
101     return;
102   }
103   if (NULL != record)
104   {
105     count++;
106     return;
107   }
108   GNUNET_assert (count == 1);
109   count = 0;
110   ic = GNUNET_PEERSTORE_iterate (h,
111                                  ss,
112                                  &p1,
113                                  NULL,
114                                  GNUNET_TIME_UNIT_FOREVER_REL,
115                                  iter2_cb,
116                                  NULL);
117 }
118
119
120 static void
121 run (void *cls,
122      const struct GNUNET_CONFIGURATION_Handle *cfg,
123      struct GNUNET_TESTING_Peer *peer)
124 {
125   h = GNUNET_PEERSTORE_connect (cfg);
126   GNUNET_assert (NULL != h);
127   memset (&p1, 1, sizeof (p1));
128   memset (&p2, 2, sizeof (p2));
129   GNUNET_PEERSTORE_store (h,
130                           ss,
131                           &p1,
132                           k1,
133                           val,
134                           strlen (val) + 1,
135                           GNUNET_TIME_UNIT_FOREVER_ABS,
136                           GNUNET_PEERSTORE_STOREOPTION_REPLACE,
137                           NULL,
138                           NULL);
139   GNUNET_PEERSTORE_store (h,
140                           ss,
141                           &p1,
142                           k2,
143                           val,
144                           strlen (val) + 1,
145                           GNUNET_TIME_UNIT_FOREVER_ABS,
146                           GNUNET_PEERSTORE_STOREOPTION_REPLACE,
147                           NULL,
148                           NULL);
149   GNUNET_PEERSTORE_store (h,
150                           ss,
151                           &p2,
152                           k3,
153                           val,
154                           strlen (val) + 1,
155                           GNUNET_TIME_UNIT_FOREVER_ABS,
156                           GNUNET_PEERSTORE_STOREOPTION_REPLACE,
157                           NULL,
158                           NULL);
159   ic = GNUNET_PEERSTORE_iterate (h,
160                                  ss,
161                                  &p1,
162                                  k1,
163                                  GNUNET_TIME_UNIT_FOREVER_REL,
164                                  &iter1_cb, NULL);
165 }
166
167
168 int
169 main (int argc, char *argv[])
170 {
171   if (0 !=
172       GNUNET_TESTING_service_run ("test-gnunet-peerstore", "peerstore",
173                                   "test_peerstore_api_data.conf", &run, NULL))
174     return 1;
175   return ok;
176 }
177
178 /* end of test_peerstore_api_iterate.c */