diff options
author | Martin Schanzenbach <schanzen@gnunet.org> | 2022-03-18 23:06:40 +0100 |
---|---|---|
committer | Martin Schanzenbach <schanzen@gnunet.org> | 2022-03-18 23:06:40 +0100 |
commit | a29a2844a447cee8533199980fcce1f74aa7f8dc (patch) | |
tree | bb6df8743e841d50ada79893a403cec16e11dfcb /src/namestore | |
parent | 4f3c8d098ae6d039effdd314018257b9ca2cfb94 (diff) | |
download | gnunet-a29a2844a447cee8533199980fcce1f74aa7f8dc.tar.gz gnunet-a29a2844a447cee8533199980fcce1f74aa7f8dc.zip |
-forgot test file
Diffstat (limited to 'src/namestore')
-rw-r--r-- | src/namestore/test_namestore_api_store_locking.c | 283 |
1 files changed, 283 insertions, 0 deletions
diff --git a/src/namestore/test_namestore_api_store_locking.c b/src/namestore/test_namestore_api_store_locking.c new file mode 100644 index 000000000..a80cad523 --- /dev/null +++ b/src/namestore/test_namestore_api_store_locking.c | |||
@@ -0,0 +1,283 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2012 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 namestore/test_namestore_api_store_locking.c | ||
22 | * @brief testcase for namestore_api.c: store a record, locking | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_namestore_service.h" | ||
26 | #include "gnunet_testing_lib.h" | ||
27 | #include "gnunet_dnsparser_lib.h" | ||
28 | |||
29 | #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT | ||
30 | |||
31 | #define TEST_RECORD_DATALEN 123 | ||
32 | |||
33 | #define TEST_RECORD_DATA 'a' | ||
34 | |||
35 | #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100) | ||
36 | |||
37 | |||
38 | static struct GNUNET_NAMESTORE_Handle *nsh; | ||
39 | |||
40 | static struct GNUNET_NAMESTORE_Handle *nsh_alt; | ||
41 | |||
42 | static struct GNUNET_SCHEDULER_Task *endbadly_task; | ||
43 | |||
44 | static struct GNUNET_IDENTITY_PrivateKey privkey; | ||
45 | |||
46 | static struct GNUNET_IDENTITY_PublicKey pubkey; | ||
47 | |||
48 | static int res; | ||
49 | |||
50 | static struct GNUNET_NAMESTORE_QueueEntry *nsqe; | ||
51 | |||
52 | static struct GNUNET_NAMESTORE_QueueEntry *nsqe_alt; | ||
53 | |||
54 | |||
55 | static void | ||
56 | cleanup () | ||
57 | { | ||
58 | if (NULL != nsh) | ||
59 | { | ||
60 | GNUNET_NAMESTORE_disconnect (nsh); | ||
61 | nsh = NULL; | ||
62 | } | ||
63 | if (NULL != nsh_alt) | ||
64 | { | ||
65 | GNUNET_NAMESTORE_disconnect (nsh_alt); | ||
66 | nsh_alt = NULL; | ||
67 | } | ||
68 | GNUNET_SCHEDULER_shutdown (); | ||
69 | } | ||
70 | |||
71 | |||
72 | /** | ||
73 | * Re-establish the connection to the service. | ||
74 | * | ||
75 | * @param cls handle to use to re-connect. | ||
76 | */ | ||
77 | static void | ||
78 | endbadly (void *cls) | ||
79 | { | ||
80 | if (NULL != nsqe) | ||
81 | { | ||
82 | GNUNET_NAMESTORE_cancel (nsqe); | ||
83 | nsqe = NULL; | ||
84 | } | ||
85 | cleanup (); | ||
86 | res = 1; | ||
87 | } | ||
88 | |||
89 | |||
90 | static void | ||
91 | end (void *cls) | ||
92 | { | ||
93 | cleanup (); | ||
94 | res = 0; | ||
95 | } | ||
96 | |||
97 | |||
98 | static void | ||
99 | open_alt_second_failed (void *cls) | ||
100 | { | ||
101 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
102 | "Got did not get lock when I should...\n"); | ||
103 | nsqe_alt = NULL; | ||
104 | GNUNET_SCHEDULER_cancel (endbadly_task); | ||
105 | endbadly_task = NULL; | ||
106 | GNUNET_SCHEDULER_add_now (&endbadly, NULL); | ||
107 | } | ||
108 | |||
109 | |||
110 | void | ||
111 | open_alt_second_cont (void *cls, | ||
112 | const struct | ||
113 | GNUNET_IDENTITY_PrivateKey *zone, | ||
114 | const char *label, | ||
115 | unsigned int rd_count, | ||
116 | const struct GNUNET_GNSRECORD_Data *rd) | ||
117 | { | ||
118 | nsqe_alt = NULL; | ||
119 | GNUNET_SCHEDULER_cancel (endbadly_task); | ||
120 | endbadly_task = NULL; | ||
121 | GNUNET_SCHEDULER_add_now (&end, NULL); | ||
122 | } | ||
123 | |||
124 | |||
125 | static void | ||
126 | put_cont (void *cls, int32_t success, const char *emsg) | ||
127 | { | ||
128 | const char *name = cls; | ||
129 | |||
130 | nsqe = NULL; | ||
131 | GNUNET_assert (NULL != cls); | ||
132 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
133 | "Name store added record for `%s': %s\n", | ||
134 | name, | ||
135 | (success == GNUNET_OK) ? "SUCCESS" : "FAIL"); | ||
136 | /* This should not work */ | ||
137 | nsqe_alt = GNUNET_NAMESTORE_records_open (nsh_alt, | ||
138 | &privkey, | ||
139 | name, | ||
140 | &open_alt_second_failed, | ||
141 | NULL, | ||
142 | &open_alt_second_cont, | ||
143 | NULL); | ||
144 | } | ||
145 | |||
146 | static void | ||
147 | open_alt_failed (void *cls) | ||
148 | { | ||
149 | struct GNUNET_GNSRECORD_Data rd; | ||
150 | const char *name = "dummy"; | ||
151 | |||
152 | nsqe_alt = NULL; | ||
153 | rd.expiration_time = GNUNET_TIME_absolute_get ().abs_value_us; | ||
154 | rd.record_type = TEST_RECORD_TYPE; | ||
155 | rd.data_size = TEST_RECORD_DATALEN; | ||
156 | rd.data = GNUNET_malloc (TEST_RECORD_DATALEN); | ||
157 | rd.flags = 0; | ||
158 | memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN); | ||
159 | |||
160 | nsqe = GNUNET_NAMESTORE_records_commit (nsh, | ||
161 | &privkey, | ||
162 | name, | ||
163 | 1, | ||
164 | &rd, | ||
165 | &put_cont, | ||
166 | (void *) name); | ||
167 | |||
168 | GNUNET_free_nz ((void *) rd.data); | ||
169 | |||
170 | } | ||
171 | |||
172 | |||
173 | void | ||
174 | open_alt_cont (void *cls, | ||
175 | const struct | ||
176 | GNUNET_IDENTITY_PrivateKey *zone, | ||
177 | const char *label, | ||
178 | unsigned int rd_count, | ||
179 | const struct GNUNET_GNSRECORD_Data *rd) | ||
180 | { | ||
181 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
182 | "Got lock when I should not...\n"); | ||
183 | nsqe_alt = NULL; | ||
184 | GNUNET_SCHEDULER_cancel (endbadly_task); | ||
185 | endbadly_task = NULL; | ||
186 | GNUNET_SCHEDULER_add_now (&endbadly, NULL); | ||
187 | } | ||
188 | |||
189 | void | ||
190 | open_cont (void *cls, | ||
191 | const struct | ||
192 | GNUNET_IDENTITY_PrivateKey *zone, | ||
193 | const char *label, | ||
194 | unsigned int rd_count, | ||
195 | const struct GNUNET_GNSRECORD_Data *rd) | ||
196 | { | ||
197 | const char *name = "dummy"; | ||
198 | /* Record set does not exist */ | ||
199 | GNUNET_assert (NULL == rd); | ||
200 | nsqe = NULL; | ||
201 | endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, | ||
202 | &endbadly, NULL); | ||
203 | |||
204 | /* This should not work */ | ||
205 | nsqe_alt = GNUNET_NAMESTORE_records_open (nsh_alt, | ||
206 | &privkey, | ||
207 | name, | ||
208 | &open_alt_failed, | ||
209 | NULL, | ||
210 | &open_alt_cont, | ||
211 | NULL); | ||
212 | |||
213 | } | ||
214 | |||
215 | |||
216 | static void | ||
217 | open_cont_failed (void *cls) | ||
218 | { | ||
219 | nsqe = NULL; | ||
220 | GNUNET_SCHEDULER_cancel (endbadly_task); | ||
221 | endbadly_task = NULL; | ||
222 | GNUNET_SCHEDULER_add_now (&endbadly, NULL); | ||
223 | } | ||
224 | |||
225 | |||
226 | static void | ||
227 | run (void *cls, | ||
228 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
229 | struct GNUNET_TESTING_Peer *peer) | ||
230 | { | ||
231 | const char *name = "dummy"; | ||
232 | privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY); | ||
233 | GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key); | ||
234 | GNUNET_IDENTITY_key_get_public (&privkey, &pubkey); | ||
235 | |||
236 | |||
237 | nsh = GNUNET_NAMESTORE_connect (cfg); | ||
238 | GNUNET_break (NULL != nsh); | ||
239 | nsh_alt = GNUNET_NAMESTORE_connect (cfg); | ||
240 | GNUNET_break (NULL != nsh_alt); | ||
241 | |||
242 | nsqe = GNUNET_NAMESTORE_records_open (nsh, | ||
243 | &privkey, | ||
244 | name, | ||
245 | &open_cont_failed, | ||
246 | NULL, | ||
247 | &open_cont, | ||
248 | NULL); | ||
249 | if (NULL == nsqe) | ||
250 | { | ||
251 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
252 | _ ("Namestore cannot store no block\n")); | ||
253 | } | ||
254 | } | ||
255 | |||
256 | |||
257 | #include "test_common.c" | ||
258 | |||
259 | |||
260 | int | ||
261 | main (int argc, char *argv[]) | ||
262 | { | ||
263 | const char *plugin_name; | ||
264 | char *cfg_name; | ||
265 | |||
266 | SETUP_CFG (plugin_name, cfg_name); | ||
267 | res = 1; | ||
268 | if (0 != | ||
269 | GNUNET_TESTING_peer_run ("test-namestore-api", | ||
270 | cfg_name, | ||
271 | &run, | ||
272 | NULL)) | ||
273 | { | ||
274 | res = 1; | ||
275 | } | ||
276 | GNUNET_DISK_purge_cfg_dir (cfg_name, | ||
277 | "GNUNET_TEST_HOME"); | ||
278 | GNUNET_free (cfg_name); | ||
279 | return res; | ||
280 | } | ||
281 | |||
282 | |||
283 | /* end of test_namestore_api_store.c */ | ||