aboutsummaryrefslogtreecommitdiff
path: root/src/service/peerstore/test_peerstore_api_iterate.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/peerstore/test_peerstore_api_iterate.c')
-rw-r--r--src/service/peerstore/test_peerstore_api_iterate.c206
1 files changed, 206 insertions, 0 deletions
diff --git a/src/service/peerstore/test_peerstore_api_iterate.c b/src/service/peerstore/test_peerstore_api_iterate.c
new file mode 100644
index 000000000..ca3fdf133
--- /dev/null
+++ b/src/service/peerstore/test_peerstore_api_iterate.c
@@ -0,0 +1,206 @@
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 SPDX-License-Identifier: AGPL3.0-or-later
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
29static int ok = 1;
30
31static struct GNUNET_PEERSTORE_Handle *h;
32static struct GNUNET_PEERSTORE_IterateContext *ic;
33
34static char *ss = "test_peerstore_api_iterate";
35static struct GNUNET_PeerIdentity p1;
36static struct GNUNET_PeerIdentity p2;
37static char *k1 = "test_peerstore_api_iterate_key1";
38static char *k2 = "test_peerstore_api_iterate_key2";
39static char *k3 = "test_peerstore_api_iterate_key3";
40static char *val = "test_peerstore_api_iterate_val";
41static int count = 0;
42
43static void
44finish (void *cls)
45{
46 GNUNET_PEERSTORE_disconnect (h);
47 GNUNET_SCHEDULER_shutdown ();
48}
49
50
51static void
52iter3_cb (void *cls,
53 const struct GNUNET_PEERSTORE_Record *record,
54 const char *emsg)
55{
56 if (NULL != emsg)
57 {
58 GNUNET_PEERSTORE_iteration_stop (ic);
59 return;
60 }
61 if (NULL != record)
62 {
63 count++;
64 GNUNET_PEERSTORE_iteration_next (ic, 1);
65 return;
66 }
67 GNUNET_assert (count == 3);
68 ok = 0;
69 GNUNET_SCHEDULER_add_now (&finish, NULL);
70}
71
72
73static void
74iter2_cb (void *cls,
75 const struct GNUNET_PEERSTORE_Record *record,
76 const char *emsg)
77{
78 if (NULL != emsg)
79 {
80 GNUNET_PEERSTORE_iteration_stop (ic);
81 return;
82 }
83 if (NULL != record)
84 {
85 count++;
86 GNUNET_PEERSTORE_iteration_next (ic, 1);
87 return;
88 }
89 GNUNET_assert (count == 2);
90 count = 0;
91 ic = GNUNET_PEERSTORE_iteration_start (h,
92 ss,
93 NULL,
94 NULL,
95 &iter3_cb,
96 NULL);
97}
98
99
100static void
101iter1_cb (void *cls,
102 const struct GNUNET_PEERSTORE_Record *record,
103 const char *emsg)
104{
105 if (NULL != emsg)
106 {
107 GNUNET_PEERSTORE_iteration_stop (ic);
108 return;
109 }
110 if (NULL != record)
111 {
112 count++;
113 GNUNET_PEERSTORE_iteration_next (ic, 1);
114 return;
115 }
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%u is count\n", count);
117 GNUNET_assert (count == 1);
118 count = 0;
119 ic = GNUNET_PEERSTORE_iteration_start (h,
120 ss,
121 &p1,
122 NULL,
123 &iter2_cb,
124 NULL);
125}
126
127
128static void
129store_cont (void *cls, int success)
130{
131 GNUNET_assert (GNUNET_OK == success);
132 if (0 == count)
133 {
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 &store_cont,
143 NULL);
144 }
145 else if (1 == count)
146 {
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 &store_cont,
156 NULL);
157 }
158 else
159 {
160 count = 0;
161 ic = GNUNET_PEERSTORE_iteration_start (h,
162 ss,
163 &p1,
164 k1,
165 &iter1_cb, NULL);
166 return;
167 }
168 count++;
169}
170
171
172static void
173run (void *cls,
174 const struct GNUNET_CONFIGURATION_Handle *cfg,
175 struct GNUNET_TESTING_Peer *peer)
176{
177 h = GNUNET_PEERSTORE_connect (cfg);
178 GNUNET_assert (NULL != h);
179 memset (&p1, 1, sizeof(p1));
180 memset (&p2, 2, sizeof(p2));
181 count = 0;
182 GNUNET_PEERSTORE_store (h,
183 ss,
184 &p1,
185 k1,
186 val,
187 strlen (val) + 1,
188 GNUNET_TIME_UNIT_FOREVER_ABS,
189 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
190 &store_cont,
191 NULL);
192}
193
194
195int
196main (int argc, char *argv[])
197{
198 if (0 !=
199 GNUNET_TESTING_service_run ("test-gnunet-peerstore", "peerstore",
200 "test_peerstore_api_data.conf", &run, NULL))
201 return 1;
202 return ok;
203}
204
205
206/* end of test_peerstore_api_iterate.c */