diff options
Diffstat (limited to 'src/testing')
-rw-r--r-- | src/testing/testing_api_cmd_start_peer.c | 296 | ||||
-rw-r--r-- | src/testing/testing_api_cmd_system_create.c | 110 | ||||
-rw-r--r-- | src/testing/testing_api_loop.c | 3 |
3 files changed, 409 insertions, 0 deletions
diff --git a/src/testing/testing_api_cmd_start_peer.c b/src/testing/testing_api_cmd_start_peer.c new file mode 100644 index 000000000..0f8c618b8 --- /dev/null +++ b/src/testing/testing_api_cmd_start_peer.c | |||
@@ -0,0 +1,296 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2021 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 | /** | ||
22 | * @file testing_api_cmd_start_peer.c | ||
23 | * @brief cmd to start a peer. | ||
24 | * @author t3sserakt | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_testing_ng_lib.h" | ||
29 | |||
30 | |||
31 | struct StartPeerState | ||
32 | { | ||
33 | /** | ||
34 | * Receive callback | ||
35 | */ | ||
36 | struct GNUNET_MQ_MessageHandler *handlers; | ||
37 | |||
38 | const char *cfgname; | ||
39 | |||
40 | /** | ||
41 | * Peer's configuration | ||
42 | */ | ||
43 | struct GNUNET_CONFIGURATION_Handle *cfg; | ||
44 | |||
45 | struct GNUNET_TESTING_Peer *peer; | ||
46 | |||
47 | /** | ||
48 | * Peer identity | ||
49 | */ | ||
50 | struct GNUNET_PeerIdentity id; | ||
51 | |||
52 | /** | ||
53 | * Peer's transport service handle | ||
54 | */ | ||
55 | struct GNUNET_TRANSPORT_CoreHandle *th; | ||
56 | |||
57 | /** | ||
58 | * Application handle | ||
59 | */ | ||
60 | struct GNUNET_TRANSPORT_ApplicationHandle *ah; | ||
61 | |||
62 | /** | ||
63 | * Peer's PEERSTORE Handle | ||
64 | */ | ||
65 | struct GNUNET_PEERSTORE_Handle *ph; | ||
66 | |||
67 | /** | ||
68 | * Hello get task | ||
69 | */ | ||
70 | struct GNUNET_SCHEDULER_Task *rh_task; | ||
71 | |||
72 | /** | ||
73 | * Peer's transport get hello handle to retrieve peer's HELLO message | ||
74 | */ | ||
75 | struct GNUNET_PEERSTORE_IterateContext *pic; | ||
76 | |||
77 | /** | ||
78 | * Hello | ||
79 | */ | ||
80 | char *hello; | ||
81 | |||
82 | /** | ||
83 | * Hello size | ||
84 | */ | ||
85 | size_t hello_size; | ||
86 | |||
87 | char *m; | ||
88 | |||
89 | char *n; | ||
90 | |||
91 | unsigned int finished; | ||
92 | }; | ||
93 | |||
94 | |||
95 | static void | ||
96 | retrieve_hello (void *cls); | ||
97 | |||
98 | static void | ||
99 | hello_iter_cb (void *cb_cls, | ||
100 | const struct GNUNET_PEERSTORE_Record *record, | ||
101 | const char *emsg) | ||
102 | { | ||
103 | struct StartPeerState *sps = cb_cls; | ||
104 | if (NULL == record) | ||
105 | { | ||
106 | sps->pic = NULL; | ||
107 | sps->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, p); | ||
108 | return; | ||
109 | } | ||
110 | // Check record type et al? | ||
111 | sps->hello_size = record->value_size; | ||
112 | sps->hello = GNUNET_malloc (sps->hello_size); | ||
113 | memcpy (sps->hello, record->value, sps->hello_size); | ||
114 | p->hello[p->hello_size - 1] = '\0'; | ||
115 | |||
116 | GNUNET_PEERSTORE_iterate_cancel (sps->pic); | ||
117 | sps->pic = NULL; | ||
118 | sps->finished = GNUNET_YES; | ||
119 | } | ||
120 | |||
121 | |||
122 | static void | ||
123 | retrieve_hello (void *cls) | ||
124 | { | ||
125 | struct StartPeerState *sps = cls; | ||
126 | sps->rh_task = NULL; | ||
127 | sps->pic = GNUNET_PEERSTORE_iterate (sps->ph, | ||
128 | "transport", | ||
129 | &sps->id, | ||
130 | GNUNET_PEERSTORE_TRANSPORT_HELLO_KEY, | ||
131 | hello_iter_cb, | ||
132 | sps); | ||
133 | |||
134 | } | ||
135 | |||
136 | static int | ||
137 | start_peer_finish (void *cls, | ||
138 | GNUNET_SCHEDULER_TaskCallback cont, | ||
139 | void *cont_cls) | ||
140 | { | ||
141 | struct StartPeerState *sps = cls; | ||
142 | |||
143 | return sps->finished; | ||
144 | } | ||
145 | |||
146 | |||
147 | static void | ||
148 | start_peer_run (void *cls, | ||
149 | const struct GNUNET_TESTING_Command *cmd, | ||
150 | struct GNUNET_TESTING_Interpreter *is) | ||
151 | { | ||
152 | struct StartPeerState *sps = cls; | ||
153 | char *emsg = NULL; | ||
154 | struct GNUNET_PeerIdentity dummy; | ||
155 | |||
156 | if (GNUNET_NO == GNUNET_DISK_file_test (sps->cfgname)) | ||
157 | { | ||
158 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
159 | "File not found: `%s'\n", | ||
160 | cfgname); | ||
161 | return NULL; | ||
162 | } | ||
163 | |||
164 | if (NULL != handlers) | ||
165 | { | ||
166 | for (i = 0; NULL != handlers[i].cb; i++) | ||
167 | ; | ||
168 | sps->handlers = GNUNET_new_array (i + 1, | ||
169 | struct GNUNET_MQ_MessageHandler); | ||
170 | GNUNET_memcpy (sps->handlers, | ||
171 | handlers, | ||
172 | i * sizeof(struct GNUNET_MQ_MessageHandler)); | ||
173 | } | ||
174 | sps->cfg = GNUNET_CONFIGURATION_create (); | ||
175 | GNUNET_assert (GNUNET_OK == | ||
176 | GNUNET_CONFIGURATION_load (sps->cfg, sps->cfgname)); | ||
177 | if (GNUNET_SYSERR == | ||
178 | GNUNET_TESTING_configuration_create (tl_system, | ||
179 | sps->cfg)) | ||
180 | { | ||
181 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
182 | "Testing library failed to create unique configuration based on `%s'\n", | ||
183 | sps->cfgname); | ||
184 | GNUNET_CONFIGURATION_destroy (sps->cfg); | ||
185 | GNUNET_TESTING_interpreter_fail (); | ||
186 | } | ||
187 | |||
188 | sps->peer = GNUNET_TESTING_peer_configure (tth->tl_system, | ||
189 | p->cfg, | ||
190 | p->no, | ||
191 | NULL, | ||
192 | &emsg); | ||
193 | if (NULL == sps->peer) | ||
194 | { | ||
195 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
196 | "Testing library failed to create unique configuration based on `%s': `%s'\n", | ||
197 | cfgname, | ||
198 | emsg); | ||
199 | GNUNET_free (emsg); | ||
200 | GNUNET_TESTING_interpreter_fail (); | ||
201 | } | ||
202 | |||
203 | if (GNUNET_OK != GNUNET_TESTING_peer_start (p->peer)) | ||
204 | { | ||
205 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
206 | "Testing library failed to create unique configuration based on `%s'\n", | ||
207 | cfgname); | ||
208 | GNUNET_free (emsg); | ||
209 | GNUNET_TESTING_interpreter_fail (); | ||
210 | } | ||
211 | |||
212 | memset (&dummy, | ||
213 | '\0', | ||
214 | sizeof(dummy)); | ||
215 | GNUNET_TESTING_peer_get_identity (sps->peer, | ||
216 | &sps->id); | ||
217 | if (0 == memcmp (&dummy, | ||
218 | &sps->id, | ||
219 | sizeof(struct GNUNET_PeerIdentity))) | ||
220 | { | ||
221 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
222 | "Testing library failed to obtain peer identity for peer %s_%s\n", | ||
223 | p->no); | ||
224 | GNUNET_free (emsg); | ||
225 | GNUNET_TESTING_interpreter_fail (); | ||
226 | } | ||
227 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
228 | "Peer %u configured with identity `%s'\n", | ||
229 | p->no, | ||
230 | GNUNET_i2s_full (&p->id)); | ||
231 | sps->th = GNUNET_TRANSPORT_core_connect (p->cfg, | ||
232 | NULL, | ||
233 | handlers, | ||
234 | p, | ||
235 | ¬ify_connect, | ||
236 | ¬ify_disconnect); | ||
237 | if (NULL == sps->th) | ||
238 | { | ||
239 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
240 | "Failed to connect to transport service for peer `%s': `%s'\n", | ||
241 | cfgname, | ||
242 | emsg); | ||
243 | GNUNET_free (emsg); | ||
244 | GNUNET_TESTING_interpreter_fail (); | ||
245 | } | ||
246 | sps->ph = GNUNET_PEERSTORE_connect (p->cfg); | ||
247 | if (NULL == sps->th) | ||
248 | { | ||
249 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
250 | "Failed to connect to peerstore service for peer `%s': `%s'\n", | ||
251 | cfgname, | ||
252 | emsg); | ||
253 | GNUNET_free (emsg); | ||
254 | GNUNET_TESTING_interpreter_fail (); | ||
255 | } | ||
256 | sps->ah = GNUNET_TRANSPORT_application_init (p->cfg); | ||
257 | if (NULL == sps->ah) | ||
258 | { | ||
259 | LOG (GNUNET_ERROR_TYPE_ERROR, | ||
260 | "Failed to initialize the TRANSPORT application suggestion client handle for peer `%s': `%s'\n", | ||
261 | cfgname, | ||
262 | emsg); | ||
263 | GNUNET_free (emsg); | ||
264 | GNUNET_TESTING_interpreter_fail (); | ||
265 | } | ||
266 | p->rh_task = GNUNET_SCHEDULER_add_now (retrieve_hello, p); | ||
267 | } | ||
268 | |||
269 | |||
270 | /** | ||
271 | * Create command. | ||
272 | * | ||
273 | * @param label name for command. | ||
274 | * @return command. | ||
275 | */ | ||
276 | struct GNUNET_TESTING_Command | ||
277 | GNUNET_TESTING_cmd_start_peer (const char *label, | ||
278 | char *m, | ||
279 | char *n) | ||
280 | { | ||
281 | struct StartPeerState *sps; | ||
282 | |||
283 | sps = GNUNET_new (struct StartPeerState); | ||
284 | sps->m = m; | ||
285 | sps->n = n; | ||
286 | |||
287 | struct GNUNET_TESTING_Command cmd = { | ||
288 | .cls = sps, | ||
289 | .label = label, | ||
290 | .run = &start_peer_run, | ||
291 | .cleanup = &start_peer_cleanup, | ||
292 | .traits = &start_peer_traits | ||
293 | }; | ||
294 | |||
295 | return cmd; | ||
296 | } | ||
diff --git a/src/testing/testing_api_cmd_system_create.c b/src/testing/testing_api_cmd_system_create.c new file mode 100644 index 000000000..ad0aa4f90 --- /dev/null +++ b/src/testing/testing_api_cmd_system_create.c | |||
@@ -0,0 +1,110 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2021 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 | /** | ||
22 | * @file testing_api_cmd_system_create.c | ||
23 | * @brief cmd to create a testing system handle. | ||
24 | * @author t3sserakt | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_testing_ng_lib.h" | ||
29 | |||
30 | struct TestSystemState | ||
31 | { | ||
32 | struct GNUNET_TESTING_System *test_system; | ||
33 | }; | ||
34 | |||
35 | |||
36 | static void | ||
37 | system_create_run (void *cls, | ||
38 | const struct GNUNET_TESTING_Command *cmd, | ||
39 | struct GNUNET_TESTING_Interpreter *is) | ||
40 | { | ||
41 | struct TestSystemState *tss = cls; | ||
42 | |||
43 | tss->test_system = GNUNET_TESTING_system_create (tss->testdir, | ||
44 | NULL, | ||
45 | NULL, | ||
46 | NULL); | ||
47 | } | ||
48 | |||
49 | static int | ||
50 | system_create_traits (void *cls, | ||
51 | const void **ret, | ||
52 | const char *trait, | ||
53 | unsigned int index) | ||
54 | { | ||
55 | struct TestSystemState *tss = cls; | ||
56 | struct GNUNET_TESTING_System *test_system = tss->test_system; | ||
57 | |||
58 | struct GNUNET_TESTING_Trait traits[] = { | ||
59 | { | ||
60 | .index = 0, | ||
61 | .trait_name = "test_system", | ||
62 | .ptr = (const void *) test_system, | ||
63 | }, | ||
64 | GNUNET_TESTING_trait_end () | ||
65 | }; | ||
66 | |||
67 | return GNUNET_TESTING_get_trait (traits, | ||
68 | ret, | ||
69 | trait, | ||
70 | index); | ||
71 | } | ||
72 | |||
73 | |||
74 | int | ||
75 | GNUNET_TESTING_get_trait_test_system (const struct | ||
76 | GNUNET_TESTING_Command *cmd, | ||
77 | struct GNUNET_TESTING_System **test_system) | ||
78 | { | ||
79 | return cmd->traits (cmd->cls, | ||
80 | (const void **) test_system, | ||
81 | "test_system", | ||
82 | (unsigned int) 0); | ||
83 | } | ||
84 | |||
85 | |||
86 | /** | ||
87 | * Create command. | ||
88 | * | ||
89 | * @param label name for command. | ||
90 | * @return command. | ||
91 | */ | ||
92 | struct GNUNET_TESTING_Command | ||
93 | GNUNET_TESTING_cmd_system_create (const char *label, | ||
94 | const char *testdir) | ||
95 | { | ||
96 | struct TestSystemState *tss; | ||
97 | |||
98 | tss = GNUNET_new (struct TestSystemState); | ||
99 | tss->testdir = testdir; | ||
100 | |||
101 | struct GNUNET_TESTING_Command cmd = { | ||
102 | .cls = tss, | ||
103 | .label = label, | ||
104 | .run = &system_create_run, | ||
105 | .cleanup = &system_create_cleanup, | ||
106 | .traits = &system_create_traits | ||
107 | }; | ||
108 | |||
109 | return cmd; | ||
110 | } | ||
diff --git a/src/testing/testing_api_loop.c b/src/testing/testing_api_loop.c index 49ed48063..7b7dc010a 100644 --- a/src/testing/testing_api_loop.c +++ b/src/testing/testing_api_loop.c | |||
@@ -332,6 +332,9 @@ GNUNET_TESTING_interpreter_fail () | |||
332 | if (GNUNET_SYSERR == is->result) | 332 | if (GNUNET_SYSERR == is->result) |
333 | return; /* ignore, we already failed! */ | 333 | return; /* ignore, we already failed! */ |
334 | 334 | ||
335 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
336 | "interpreter_fail!\n"); | ||
337 | |||
335 | if (NULL != cmd) | 338 | if (NULL != cmd) |
336 | { | 339 | { |
337 | while (GNUNET_TESTING_cmd_is_batch (cmd)) | 340 | while (GNUNET_TESTING_cmd_is_batch (cmd)) |