diff options
Diffstat (limited to 'src/service/peerstore/test_peerstore_api_sync.c')
-rw-r--r-- | src/service/peerstore/test_peerstore_api_sync.c | 252 |
1 files changed, 0 insertions, 252 deletions
diff --git a/src/service/peerstore/test_peerstore_api_sync.c b/src/service/peerstore/test_peerstore_api_sync.c deleted file mode 100644 index 4e16afae8..000000000 --- a/src/service/peerstore/test_peerstore_api_sync.c +++ /dev/null | |||
@@ -1,252 +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 | */ | ||
36 | static int ok = 404; | ||
37 | |||
38 | /** | ||
39 | * Configuration we use. | ||
40 | */ | ||
41 | static const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
42 | |||
43 | /** | ||
44 | * handle to talk to the peerstore. | ||
45 | */ | ||
46 | static struct GNUNET_PEERSTORE_Handle *h; | ||
47 | |||
48 | /** | ||
49 | * Subsystem we store the value for. | ||
50 | */ | ||
51 | static const char *subsystem = "test_peerstore_api_sync"; | ||
52 | |||
53 | /** | ||
54 | * Fake PID under which we store the value. | ||
55 | */ | ||
56 | static struct GNUNET_PeerIdentity pid; | ||
57 | |||
58 | /** | ||
59 | * Test key we're storing the test value under. | ||
60 | */ | ||
61 | static const char *key = "test_peerstore_api_store_key"; | ||
62 | |||
63 | /** | ||
64 | * Test value we are storing. | ||
65 | */ | ||
66 | static 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 | */ | ||
77 | static struct GNUNET_SCHEDULER_Task *to; | ||
78 | |||
79 | /** | ||
80 | * Iterate handle | ||
81 | */ | ||
82 | static struct GNUNET_PEERSTORE_IterateContext *it; | ||
83 | |||
84 | static void | ||
85 | test_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 | */ | ||
100 | static void | ||
101 | iterate_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 | if (NULL != to) | ||
115 | { | ||
116 | GNUNET_SCHEDULER_cancel (to); | ||
117 | to = NULL; | ||
118 | } | ||
119 | GNUNET_SCHEDULER_shutdown (); | ||
120 | return; | ||
121 | } | ||
122 | /** | ||
123 | * Try again | ||
124 | */ | ||
125 | GNUNET_SCHEDULER_add_now (&test_cont, | ||
126 | NULL); | ||
127 | return; | ||
128 | } | ||
129 | rec_val = record->value; | ||
130 | GNUNET_break (0 == strcmp (rec_val, val)); | ||
131 | ok = 0; | ||
132 | } | ||
133 | |||
134 | |||
135 | static void | ||
136 | timeout_task (void *cls) | ||
137 | { | ||
138 | to = NULL; | ||
139 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
140 | "Timeout reached\n"); | ||
141 | if (NULL != it) | ||
142 | GNUNET_PEERSTORE_iterate_cancel (it); | ||
143 | it = NULL; | ||
144 | GNUNET_PEERSTORE_disconnect (h, | ||
145 | GNUNET_NO); | ||
146 | GNUNET_SCHEDULER_shutdown (); | ||
147 | return; | ||
148 | } | ||
149 | |||
150 | |||
151 | /** | ||
152 | * Run the 2nd stage of the test where we fetch the | ||
153 | * data that should have been stored. | ||
154 | * | ||
155 | * @param cls NULL | ||
156 | */ | ||
157 | static void | ||
158 | test_cont (void *cls) | ||
159 | { | ||
160 | it = GNUNET_PEERSTORE_iterate (h, | ||
161 | subsystem, | ||
162 | &pid, key, | ||
163 | &iterate_cb, | ||
164 | NULL); | ||
165 | } | ||
166 | |||
167 | |||
168 | static void | ||
169 | disc_cont (void *cls) | ||
170 | { | ||
171 | GNUNET_PEERSTORE_disconnect (h, GNUNET_YES); | ||
172 | h = GNUNET_PEERSTORE_connect (cfg); | ||
173 | GNUNET_SCHEDULER_add_now (&test_cont, | ||
174 | NULL); | ||
175 | } | ||
176 | |||
177 | |||
178 | static void | ||
179 | store_cont (void *cls, int success) | ||
180 | { | ||
181 | ok = success; | ||
182 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
183 | "Success: %s\n", | ||
184 | (GNUNET_SYSERR == ok) ? "no" : "yes"); | ||
185 | /* We need to wait a little bit to give the disconnect | ||
186 | a chance to actually finish the operation; otherwise, | ||
187 | the test may fail non-deterministically if the new | ||
188 | connection is faster than the cleanup routine of the | ||
189 | old one. */ | ||
190 | GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, | ||
191 | &disc_cont, | ||
192 | NULL); | ||
193 | } | ||
194 | |||
195 | |||
196 | /** | ||
197 | * Actually run the test. | ||
198 | */ | ||
199 | static void | ||
200 | test1 () | ||
201 | { | ||
202 | h = GNUNET_PEERSTORE_connect (cfg); | ||
203 | GNUNET_PEERSTORE_store (h, | ||
204 | subsystem, | ||
205 | &pid, | ||
206 | key, | ||
207 | val, strlen (val) + 1, | ||
208 | GNUNET_TIME_UNIT_FOREVER_ABS, | ||
209 | GNUNET_PEERSTORE_STOREOPTION_REPLACE, | ||
210 | &store_cont, NULL); | ||
211 | } | ||
212 | |||
213 | |||
214 | /** | ||
215 | * Initialize globals and launch the test. | ||
216 | * | ||
217 | * @param cls NULL | ||
218 | * @param c configuration to use | ||
219 | * @param peer handle to our peer (unused) | ||
220 | */ | ||
221 | static void | ||
222 | run (void *cls, | ||
223 | const struct GNUNET_CONFIGURATION_Handle *c, | ||
224 | struct GNUNET_TESTING_Peer *peer) | ||
225 | { | ||
226 | cfg = c; | ||
227 | memset (&pid, 1, sizeof(pid)); | ||
228 | to = GNUNET_SCHEDULER_add_delayed (TIMEOUT, | ||
229 | &timeout_task, | ||
230 | NULL); | ||
231 | GNUNET_SCHEDULER_add_now (&test1, NULL); | ||
232 | } | ||
233 | |||
234 | |||
235 | int | ||
236 | main (int argc, char *argv[]) | ||
237 | { | ||
238 | if (0 != | ||
239 | GNUNET_TESTING_service_run ("test-gnunet-peerstore-sync", | ||
240 | "peerstore", | ||
241 | "peerstore.conf", | ||
242 | &run, NULL)) | ||
243 | return 1; | ||
244 | if (0 != ok) | ||
245 | fprintf (stderr, | ||
246 | "Test failed: %d\n", | ||
247 | ok); | ||
248 | return ok; | ||
249 | } | ||
250 | |||
251 | |||
252 | /* end of test_peerstore_api_sync.c */ | ||