aboutsummaryrefslogtreecommitdiff
path: root/src/peerstore/test_peerstore_api_sync.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/peerstore/test_peerstore_api_sync.c')
-rw-r--r--src/peerstore/test_peerstore_api_sync.c253
1 files changed, 0 insertions, 253 deletions
diff --git a/src/peerstore/test_peerstore_api_sync.c b/src/peerstore/test_peerstore_api_sync.c
deleted file mode 100644
index 5057c98b5..000000000
--- a/src/peerstore/test_peerstore_api_sync.c
+++ /dev/null
@@ -1,253 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2015 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_sync.c
22 * @brief testcase for peerstore sync-on-disconnect feature. Stores
23 * a value just before disconnecting, and then checks that
24 * this value is actually stored.
25 * @author Omar Tarabai
26 * @author Christian Grothoff (minor fix, comments)
27 */
28#include "platform.h"
29#include "gnunet_util_lib.h"
30#include "gnunet_testing_lib.h"
31#include "gnunet_peerstore_service.h"
32
33/**
34 * Overall result, 0 for success.
35 */
36static int ok = 404;
37
38/**
39 * Configuration we use.
40 */
41static const struct GNUNET_CONFIGURATION_Handle *cfg;
42
43/**
44 * handle to talk to the peerstore.
45 */
46static struct GNUNET_PEERSTORE_Handle *h;
47
48/**
49 * Subsystem we store the value for.
50 */
51static const char *subsystem = "test_peerstore_api_sync";
52
53/**
54 * Fake PID under which we store the value.
55 */
56static struct GNUNET_PeerIdentity pid;
57
58/**
59 * Test key we're storing the test value under.
60 */
61static const char *key = "test_peerstore_api_store_key";
62
63/**
64 * Test value we are storing.
65 */
66static const char *val = "test_peerstore_api_store_val";
67
68
69/**
70 * Timeout
71 */
72#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
73
74/**
75 * Timeout task
76 */
77static struct GNUNET_SCHEDULER_Task *to;
78
79/**
80 * Iterate handle
81 */
82static struct GNUNET_PEERSTORE_IterateContext *it;
83
84static void
85test_cont (void *cls);
86
87/**
88 * Function that should be called with the result of the
89 * lookup, and finally once with NULL to signal the end
90 * of the iteration.
91 *
92 * Upon the first call, we set "ok" to success. On the
93 * second call (end of iteration) we terminate the test.
94 *
95 * @param cls NULL
96 * @param record the information stored in the peerstore
97 * @param emsg any error message
98 * @return #GNUNET_YES (all good, continue)
99 */
100static void
101iterate_cb (void *cls,
102 const struct GNUNET_PEERSTORE_Record *record,
103 const char *emsg)
104{
105 const char *rec_val;
106
107 GNUNET_break (NULL == emsg);
108 if (NULL == record)
109 {
110 it = NULL;
111 if (0 == ok)
112 {
113 GNUNET_PEERSTORE_disconnect (h,
114 GNUNET_NO);
115 if (NULL != to)
116 {
117 GNUNET_SCHEDULER_cancel (to);
118 to = NULL;
119 }
120 GNUNET_SCHEDULER_shutdown ();
121 return;
122 }
123 /**
124 * Try again
125 */
126 GNUNET_SCHEDULER_add_now (&test_cont,
127 NULL);
128 return;
129 }
130 rec_val = record->value;
131 GNUNET_break (0 == strcmp (rec_val, val));
132 ok = 0;
133}
134
135
136static void
137timeout_task (void *cls)
138{
139 to = NULL;
140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
141 "Timeout reached\n");
142 if (NULL != it)
143 GNUNET_PEERSTORE_iterate_cancel (it);
144 it = NULL;
145 GNUNET_PEERSTORE_disconnect (h,
146 GNUNET_NO);
147 GNUNET_SCHEDULER_shutdown ();
148 return;
149}
150
151
152/**
153 * Run the 2nd stage of the test where we fetch the
154 * data that should have been stored.
155 *
156 * @param cls NULL
157 */
158static void
159test_cont (void *cls)
160{
161 it = GNUNET_PEERSTORE_iterate (h,
162 subsystem,
163 &pid, key,
164 &iterate_cb,
165 NULL);
166}
167
168
169static void
170disc_cont (void *cls)
171{
172 GNUNET_PEERSTORE_disconnect (h, GNUNET_YES);
173 h = GNUNET_PEERSTORE_connect (cfg);
174 GNUNET_SCHEDULER_add_now (&test_cont,
175 NULL);
176}
177
178
179static void
180store_cont (void *cls, int success)
181{
182 ok = success;
183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
184 "Success: %s\n",
185 (GNUNET_SYSERR == ok) ? "no" : "yes");
186 /* We need to wait a little bit to give the disconnect
187 a chance to actually finish the operation; otherwise,
188 the test may fail non-deterministically if the new
189 connection is faster than the cleanup routine of the
190 old one. */
191 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
192 &disc_cont,
193 NULL);
194}
195
196
197/**
198 * Actually run the test.
199 */
200static void
201test1 ()
202{
203 h = GNUNET_PEERSTORE_connect (cfg);
204 GNUNET_PEERSTORE_store (h,
205 subsystem,
206 &pid,
207 key,
208 val, strlen (val) + 1,
209 GNUNET_TIME_UNIT_FOREVER_ABS,
210 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
211 &store_cont, NULL);
212}
213
214
215/**
216 * Initialize globals and launch the test.
217 *
218 * @param cls NULL
219 * @param c configuration to use
220 * @param peer handle to our peer (unused)
221 */
222static void
223run (void *cls,
224 const struct GNUNET_CONFIGURATION_Handle *c,
225 struct GNUNET_TESTING_Peer *peer)
226{
227 cfg = c;
228 memset (&pid, 1, sizeof(pid));
229 to = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
230 &timeout_task,
231 NULL);
232 GNUNET_SCHEDULER_add_now (&test1, NULL);
233}
234
235
236int
237main (int argc, char *argv[])
238{
239 if (0 !=
240 GNUNET_TESTING_service_run ("test-gnunet-peerstore-sync",
241 "peerstore",
242 "peerstore.conf",
243 &run, NULL))
244 return 1;
245 if (0 != ok)
246 fprintf (stderr,
247 "Test failed: %d\n",
248 ok);
249 return ok;
250}
251
252
253/* end of test_peerstore_api_sync.c */