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