aboutsummaryrefslogtreecommitdiff
path: root/src/service/peerstore/test_peerstore_api_store.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/peerstore/test_peerstore_api_store.c')
-rw-r--r--src/service/peerstore/test_peerstore_api_store.c250
1 files changed, 250 insertions, 0 deletions
diff --git a/src/service/peerstore/test_peerstore_api_store.c b/src/service/peerstore/test_peerstore_api_store.c
new file mode 100644
index 000000000..d2ec9aad7
--- /dev/null
+++ b/src/service/peerstore/test_peerstore_api_store.c
@@ -0,0 +1,250 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 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_store.c
22 * @brief testcase for peerstore store operation
23 */
24#include "platform.h"
25#include "gnunet_util_lib.h"
26#include "gnunet_peerstore_service.h"
27#include "gnunet_testing_lib.h"
28
29
30static int ok = 1;
31
32static struct GNUNET_PEERSTORE_Handle *h;
33static struct GNUNET_PEERSTORE_IterateContext *ic;
34
35static char *subsystem = "test_peerstore_api_store";
36static struct GNUNET_PeerIdentity pid;
37static char *key = "test_peerstore_api_store_key";
38static char *val1 = "test_peerstore_api_store_val1";
39static char *val2 = "test_peerstore_api_store_val2-";
40static char *val3 = "test_peerstore_api_store_val3--";
41
42static int count = 0;
43
44
45static void
46finish (void *cls)
47{
48 GNUNET_PEERSTORE_disconnect (h);
49 GNUNET_SCHEDULER_shutdown ();
50}
51
52
53static void
54test3_cont2 (void *cls,
55 const struct GNUNET_PEERSTORE_Record *record,
56 const char *emsg)
57{
58 if (NULL != emsg)
59 return;
60 if (NULL != record)
61 {
62 GNUNET_assert ((strlen (val3) + 1) == record->value_size);
63 GNUNET_assert (0 == strcmp ((char *) val3,
64 (char *) record->value));
65 count++;
66 GNUNET_PEERSTORE_iteration_next (ic, 1);
67 return;
68 }
69 GNUNET_assert (count == 1);
70 ok = 0;
71 GNUNET_SCHEDULER_add_now (&finish, NULL);
72}
73
74
75static void
76test3_cont (void *cls,
77 int success)
78{
79 if (GNUNET_YES != success)
80 return;
81 count = 0;
82 ic = GNUNET_PEERSTORE_iteration_start (h,
83 subsystem,
84 &pid,
85 key,
86 &test3_cont2,
87 NULL);
88}
89
90
91/**
92 * Replace the previous 2 records
93 */
94static void
95test3 ()
96{
97 GNUNET_PEERSTORE_store (h,
98 subsystem,
99 &pid,
100 key,
101 val3,
102 strlen (val3) + 1,
103 GNUNET_TIME_UNIT_FOREVER_ABS,
104 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
105 &test3_cont,
106 NULL);
107}
108
109
110static void
111test2_cont2 (void *cls,
112 const struct GNUNET_PEERSTORE_Record *record,
113 const char *emsg)
114{
115 if (NULL != emsg)
116 return;
117 if (NULL != record)
118 {
119 GNUNET_assert (((strlen (val1) + 1) == record->value_size) ||
120 ((strlen (val2) + 1) == record->value_size));
121 GNUNET_assert ((0 == strcmp ((char *) val1, (char *) record->value)) ||
122 (0 == strcmp ((char *) val2, (char *) record->value)));
123 count++;
124 GNUNET_PEERSTORE_iteration_next (ic, 1);
125 return;
126 }
127 GNUNET_assert (count == 2);
128 count = 0;
129 test3 ();
130}
131
132
133static void
134test2_cont (void *cls, int success)
135{
136 if (GNUNET_YES != success)
137 return;
138 count = 0;
139 ic = GNUNET_PEERSTORE_iteration_start (h,
140 subsystem,
141 &pid, key,
142 &test2_cont2,
143 NULL);
144}
145
146
147/**
148 * Test storing a second value with the same key
149 */
150void
151test2 ()
152{
153 GNUNET_PEERSTORE_store (h,
154 subsystem,
155 &pid,
156 key,
157 val2,
158 strlen (val2) + 1,
159 GNUNET_TIME_UNIT_FOREVER_ABS,
160 GNUNET_PEERSTORE_STOREOPTION_MULTIPLE,
161 &test2_cont,
162 NULL);
163}
164
165
166static void
167test1_cont2 (void *cls,
168 const struct GNUNET_PEERSTORE_Record *record,
169 const char *emsg)
170{
171 if (NULL != emsg)
172 {
173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Error received: %s\n", emsg);
174 return;
175 }
176 if (NULL != record)
177 {
178 GNUNET_assert ((strlen (val1) + 1) == record->value_size);
179 GNUNET_assert (0 == strcmp ((char *) val1, (char *) record->value));
180 count++;
181 GNUNET_PEERSTORE_iteration_next (ic, 1);
182 return;
183 }
184 GNUNET_assert (count == 1);
185 count = 0;
186 test2 ();
187}
188
189
190static void
191test1_cont (void *cls, int success)
192{
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Store done, ret=%d...\n", success);
194 if (GNUNET_YES != success)
195 return;
196 count = 0;
197 ic = GNUNET_PEERSTORE_iteration_start (h,
198 subsystem,
199 &pid,
200 key,
201 &test1_cont2,
202 NULL);
203}
204
205
206/**
207 * Store a single record
208 */
209static void
210test1 ()
211{
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test1 start\n");
213 GNUNET_PEERSTORE_store (h,
214 subsystem,
215 &pid,
216 key,
217 val1,
218 strlen (val1) + 1,
219 GNUNET_TIME_UNIT_FOREVER_ABS,
220 GNUNET_PEERSTORE_STOREOPTION_REPLACE,
221 &test1_cont,
222 NULL);
223}
224
225
226static void
227run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg,
228 struct GNUNET_TESTING_Peer *peer)
229{
230 h = GNUNET_PEERSTORE_connect (cfg);
231 GNUNET_assert (NULL != h);
232 memset (&pid, 1, sizeof(pid));
233 test1 ();
234}
235
236
237int
238main (int argc, char *argv[])
239{
240 if (0 !=
241 GNUNET_TESTING_service_run ("test-gnunet-peerstore",
242 "peerstore",
243 "test_peerstore_api_data.conf",
244 &run, NULL))
245 return 1;
246 return ok;
247}
248
249
250/* end of test_peerstore_api_store.c */