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