aboutsummaryrefslogtreecommitdiff
path: root/src/core/test_core_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/test_core_api.c')
-rw-r--r--src/core/test_core_api.c344
1 files changed, 172 insertions, 172 deletions
diff --git a/src/core/test_core_api.c b/src/core/test_core_api.c
index 1e268c3a6..d29a5f04d 100644
--- a/src/core/test_core_api.c
+++ b/src/core/test_core_api.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file core/test_core_api.c 21 * @file core/test_core_api.c
22 * @brief testcase for core_api.c 22 * @brief testcase for core_api.c
@@ -32,8 +32,7 @@
32 32
33#define MTYPE 12345 33#define MTYPE 12345
34 34
35struct PeerContext 35struct PeerContext {
36{
37 struct GNUNET_CONFIGURATION_Handle *cfg; 36 struct GNUNET_CONFIGURATION_Handle *cfg;
38 struct GNUNET_CORE_Handle *ch; 37 struct GNUNET_CORE_Handle *ch;
39 struct GNUNET_PeerIdentity id; 38 struct GNUNET_PeerIdentity id;
@@ -56,18 +55,18 @@ static int ok;
56 55
57#define OKPP \ 56#define OKPP \
58 do \ 57 do \
59 { \ 58 { \
60 ok++; \ 59 ok++; \
61 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, \ 60 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, \
62 "Now at stage %u at %s:%u\n", \ 61 "Now at stage %u at %s:%u\n", \
63 ok, \ 62 ok, \
64 __FILE__, \ 63 __FILE__, \
65 __LINE__); \ 64 __LINE__); \
66 } while (0) 65 } while (0)
67 66
68 67
69static void 68static void
70offer_hello_done (void *cls) 69offer_hello_done(void *cls)
71{ 70{
72 struct PeerContext *p = cls; 71 struct PeerContext *p = cls;
73 72
@@ -76,263 +75,264 @@ offer_hello_done (void *cls)
76 75
77 76
78static void 77static void
79process_hello (void *cls, const struct GNUNET_MessageHeader *message) 78process_hello(void *cls, const struct GNUNET_MessageHeader *message)
80{ 79{
81 struct PeerContext *p = cls; 80 struct PeerContext *p = cls;
82 81
83 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 82 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
84 "Received (my) HELLO from transport service\n"); 83 "Received (my) HELLO from transport service\n");
85 GNUNET_assert (message != NULL); 84 GNUNET_assert(message != NULL);
86 if ((p == &p1) && (NULL == p2.oh)) 85 if ((p == &p1) && (NULL == p2.oh))
87 p2.oh = 86 p2.oh =
88 GNUNET_TRANSPORT_offer_hello (p2.cfg, message, &offer_hello_done, &p2); 87 GNUNET_TRANSPORT_offer_hello(p2.cfg, message, &offer_hello_done, &p2);
89 if ((p == &p2) && (NULL == p1.oh)) 88 if ((p == &p2) && (NULL == p1.oh))
90 p1.oh = 89 p1.oh =
91 GNUNET_TRANSPORT_offer_hello (p1.cfg, message, &offer_hello_done, &p1); 90 GNUNET_TRANSPORT_offer_hello(p1.cfg, message, &offer_hello_done, &p1);
92} 91}
93 92
94 93
95static void 94static void
96terminate_peer (struct PeerContext *p) 95terminate_peer(struct PeerContext *p)
97{ 96{
98 if (NULL != p->ch) 97 if (NULL != p->ch)
99 { 98 {
100 GNUNET_CORE_disconnect (p->ch); 99 GNUNET_CORE_disconnect(p->ch);
101 p->ch = NULL; 100 p->ch = NULL;
102 } 101 }
103 if (NULL != p->ghh) 102 if (NULL != p->ghh)
104 { 103 {
105 GNUNET_TRANSPORT_hello_get_cancel (p->ghh); 104 GNUNET_TRANSPORT_hello_get_cancel(p->ghh);
106 p->ghh = NULL; 105 p->ghh = NULL;
107 } 106 }
108 if (NULL != p->oh) 107 if (NULL != p->oh)
109 { 108 {
110 GNUNET_TRANSPORT_offer_hello_cancel (p->oh); 109 GNUNET_TRANSPORT_offer_hello_cancel(p->oh);
111 p->oh = NULL; 110 p->oh = NULL;
112 } 111 }
113 if (NULL != p->ats_sh) 112 if (NULL != p->ats_sh)
114 { 113 {
115 GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh); 114 GNUNET_ATS_connectivity_suggest_cancel(p->ats_sh);
116 p->ats_sh = NULL; 115 p->ats_sh = NULL;
117 } 116 }
118 if (NULL != p->ats) 117 if (NULL != p->ats)
119 { 118 {
120 GNUNET_ATS_connectivity_done (p->ats); 119 GNUNET_ATS_connectivity_done(p->ats);
121 p->ats = NULL; 120 p->ats = NULL;
122 } 121 }
123} 122}
124 123
125 124
126static void 125static void
127terminate_task (void *cls) 126terminate_task(void *cls)
128{ 127{
129 GNUNET_assert (ok == 6); 128 GNUNET_assert(ok == 6);
130 terminate_peer (&p1); 129 terminate_peer(&p1);
131 terminate_peer (&p2); 130 terminate_peer(&p2);
132 ok = 0; 131 ok = 0;
133} 132}
134 133
135 134
136static void 135static void
137terminate_task_error (void *cls) 136terminate_task_error(void *cls)
138{ 137{
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ENDING ANGRILY %u\n", ok); 138 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "ENDING ANGRILY %u\n", ok);
140 GNUNET_break (0); 139 GNUNET_break(0);
141 terminate_peer (&p1); 140 terminate_peer(&p1);
142 terminate_peer (&p2); 141 terminate_peer(&p2);
143 ok = 42; 142 ok = 42;
144} 143}
145 144
146 145
147static void * 146static void *
148connect_notify (void *cls, 147connect_notify(void *cls,
149 const struct GNUNET_PeerIdentity *peer, 148 const struct GNUNET_PeerIdentity *peer,
150 struct GNUNET_MQ_Handle *mq) 149 struct GNUNET_MQ_Handle *mq)
151{ 150{
152 struct PeerContext *pc = cls; 151 struct PeerContext *pc = cls;
153 struct GNUNET_MQ_Envelope *env; 152 struct GNUNET_MQ_Envelope *env;
154 struct GNUNET_MessageHeader *msg; 153 struct GNUNET_MessageHeader *msg;
155 154
156 if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity))) 155 if (0 == memcmp(&pc->id, peer, sizeof(struct GNUNET_PeerIdentity)))
157 return (void *) peer; 156 return (void *)peer;
158 GNUNET_assert (pc->connect_status == 0); 157 GNUNET_assert(pc->connect_status == 0);
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
160 "Encrypted connection established to peer `%s'\n", 159 "Encrypted connection established to peer `%s'\n",
161 GNUNET_i2s (peer)); 160 GNUNET_i2s(peer));
162 pc->connect_status = 1; 161 pc->connect_status = 1;
163 if (pc == &p1) 162 if (pc == &p1)
164 { 163 {
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 164 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
166 "Asking core (1) for transmission to peer `%s'\n", 165 "Asking core (1) for transmission to peer `%s'\n",
167 GNUNET_i2s (&p2.id)); 166 GNUNET_i2s(&p2.id));
168 env = GNUNET_MQ_msg (msg, MTYPE); 167 env = GNUNET_MQ_msg(msg, MTYPE);
169 /* enable corking for this test */ 168 /* enable corking for this test */
170 GNUNET_MQ_env_set_options (env, 169 GNUNET_MQ_env_set_options(env,
171 GNUNET_MQ_PRIO_BEST_EFFORT | 170 GNUNET_MQ_PRIO_BEST_EFFORT |
172 GNUNET_MQ_PREF_CORK_ALLOWED); 171 GNUNET_MQ_PREF_CORK_ALLOWED);
173 /* now actually transmit message */ 172 /* now actually transmit message */
174 GNUNET_assert (ok == 4); 173 GNUNET_assert(ok == 4);
175 OKPP; 174 OKPP;
176 GNUNET_MQ_send (mq, env); 175 GNUNET_MQ_send(mq, env);
177 } 176 }
178 return (void *) peer; 177 return (void *)peer;
179} 178}
180 179
181 180
182static void 181static void
183disconnect_notify (void *cls, 182disconnect_notify(void *cls,
184 const struct GNUNET_PeerIdentity *peer, 183 const struct GNUNET_PeerIdentity *peer,
185 void *internal_cls) 184 void *internal_cls)
186{ 185{
187 struct PeerContext *pc = cls; 186 struct PeerContext *pc = cls;
188 187
189 if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity))) 188 if (0 == memcmp(&pc->id, peer, sizeof(struct GNUNET_PeerIdentity)))
190 return; 189 return;
191 pc->connect_status = 0; 190 pc->connect_status = 0;
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 191 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
193 "Encrypted connection to `%s' cut\n", 192 "Encrypted connection to `%s' cut\n",
194 GNUNET_i2s (peer)); 193 GNUNET_i2s(peer));
195} 194}
196 195
197 196
198static void 197static void
199handle_test (void *cls, const struct GNUNET_MessageHeader *message) 198handle_test(void *cls, const struct GNUNET_MessageHeader *message)
200{ 199{
201 const struct GNUNET_PeerIdentity *peer = cls; 200 const struct GNUNET_PeerIdentity *peer = cls;
202 201
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 202 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
204 "Receiving message from `%s'.\n", 203 "Receiving message from `%s'.\n",
205 GNUNET_i2s (peer)); 204 GNUNET_i2s(peer));
206 GNUNET_assert (ok == 5); 205 GNUNET_assert(ok == 5);
207 OKPP; 206 OKPP;
208 GNUNET_SCHEDULER_cancel (err_task); 207 GNUNET_SCHEDULER_cancel(err_task);
209 err_task = GNUNET_SCHEDULER_add_now (&terminate_task, NULL); 208 err_task = GNUNET_SCHEDULER_add_now(&terminate_task, NULL);
210} 209}
211 210
212 211
213static void 212static void
214init_notify (void *cls, const struct GNUNET_PeerIdentity *my_identity) 213init_notify(void *cls, const struct GNUNET_PeerIdentity *my_identity)
215{ 214{
216 struct PeerContext *p = cls; 215 struct PeerContext *p = cls;
217 struct GNUNET_MQ_MessageHandler handlers[] = 216 struct GNUNET_MQ_MessageHandler handlers[] =
218 {GNUNET_MQ_hd_fixed_size (test, MTYPE, struct GNUNET_MessageHeader, NULL), 217 { GNUNET_MQ_hd_fixed_size(test, MTYPE, struct GNUNET_MessageHeader, NULL),
219 GNUNET_MQ_handler_end ()}; 218 GNUNET_MQ_handler_end() };
220 219
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 220 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
222 "Core connection to `%s' established\n", 221 "Core connection to `%s' established\n",
223 GNUNET_i2s (my_identity)); 222 GNUNET_i2s(my_identity));
224 p->id = *my_identity; 223 p->id = *my_identity;
225 if (cls == &p1) 224 if (cls == &p1)
226 { 225 {
227 GNUNET_assert (ok == 2); 226 GNUNET_assert(ok == 2);
228 OKPP; 227 OKPP;
229 /* connect p2 */ 228 /* connect p2 */
230 p2.ch = GNUNET_CORE_connect (p2.cfg, 229 p2.ch = GNUNET_CORE_connect(p2.cfg,
231 &p2, 230 &p2,
232 &init_notify, 231 &init_notify,
233 &connect_notify, 232 &connect_notify,
234 &disconnect_notify, 233 &disconnect_notify,
235 handlers); 234 handlers);
236 } 235 }
237 else 236 else
238 { 237 {
239 GNUNET_assert (ok == 3); 238 GNUNET_assert(ok == 3);
240 OKPP; 239 OKPP;
241 GNUNET_assert (cls == &p2); 240 GNUNET_assert(cls == &p2);
242 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats, &p2.id, 1); 241 p1.ats_sh = GNUNET_ATS_connectivity_suggest(p1.ats, &p2.id, 1);
243 } 242 }
244} 243}
245 244
246 245
247static void 246static void
248setup_peer (struct PeerContext *p, const char *cfgname) 247setup_peer(struct PeerContext *p, const char *cfgname)
249{ 248{
250 char *binary; 249 char *binary;
251 250
252 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm"); 251 binary = GNUNET_OS_get_libexec_binary_path("gnunet-service-arm");
253 p->cfg = GNUNET_CONFIGURATION_create (); 252 p->cfg = GNUNET_CONFIGURATION_create();
254 p->arm_proc = GNUNET_OS_start_process (GNUNET_YES, 253 p->arm_proc = GNUNET_OS_start_process(GNUNET_YES,
255 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, 254 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
256 NULL, 255 NULL,
257 NULL, 256 NULL,
258 NULL, 257 NULL,
259 binary, 258 binary,
260 "gnunet-service-arm", 259 "gnunet-service-arm",
261 "-c", 260 "-c",
262 cfgname, 261 cfgname,
263 NULL); 262 NULL);
264 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname)); 263 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_load(p->cfg, cfgname));
265 p->ats = GNUNET_ATS_connectivity_init (p->cfg); 264 p->ats = GNUNET_ATS_connectivity_init(p->cfg);
266 GNUNET_assert (NULL != p->ats); 265 GNUNET_assert(NULL != p->ats);
267 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg, 266 p->ghh = GNUNET_TRANSPORT_hello_get(p->cfg,
268 GNUNET_TRANSPORT_AC_ANY, 267 GNUNET_TRANSPORT_AC_ANY,
269 &process_hello, 268 &process_hello,
270 p); 269 p);
271 GNUNET_free (binary); 270 GNUNET_free(binary);
272} 271}
273 272
274 273
275static void 274static void
276run (void *cls, 275run(void *cls,
277 char *const *args, 276 char *const *args,
278 const char *cfgfile, 277 const char *cfgfile,
279 const struct GNUNET_CONFIGURATION_Handle *cfg) 278 const struct GNUNET_CONFIGURATION_Handle *cfg)
280{ 279{
281 struct GNUNET_MQ_MessageHandler handlers[] = 280 struct GNUNET_MQ_MessageHandler handlers[] =
282 {GNUNET_MQ_hd_fixed_size (test, MTYPE, struct GNUNET_MessageHeader, NULL), 281 { GNUNET_MQ_hd_fixed_size(test, MTYPE, struct GNUNET_MessageHeader, NULL),
283 GNUNET_MQ_handler_end ()}; 282 GNUNET_MQ_handler_end() };
284 283
285 GNUNET_assert (ok == 1); 284 GNUNET_assert(ok == 1);
286 OKPP; 285 OKPP;
287 setup_peer (&p1, "test_core_api_peer1.conf"); 286 setup_peer(&p1, "test_core_api_peer1.conf");
288 setup_peer (&p2, "test_core_api_peer2.conf"); 287 setup_peer(&p2, "test_core_api_peer2.conf");
289 err_task = GNUNET_SCHEDULER_add_delayed ( 288 err_task = GNUNET_SCHEDULER_add_delayed(
290 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300), 289 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 300),
291 &terminate_task_error, 290 &terminate_task_error,
292 NULL); 291 NULL);
293 p1.ch = GNUNET_CORE_connect (p1.cfg, 292 p1.ch = GNUNET_CORE_connect(p1.cfg,
294 &p1, 293 &p1,
295 &init_notify, 294 &init_notify,
296 &connect_notify, 295 &connect_notify,
297 &disconnect_notify, 296 &disconnect_notify,
298 handlers); 297 handlers);
299} 298}
300 299
301 300
302static void 301static void
303stop_arm (struct PeerContext *p) 302stop_arm(struct PeerContext *p)
304{ 303{
305 if (0 != GNUNET_OS_process_kill (p->arm_proc, GNUNET_TERM_SIG)) 304 if (0 != GNUNET_OS_process_kill(p->arm_proc, GNUNET_TERM_SIG))
306 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); 305 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
307 if (GNUNET_OK != GNUNET_OS_process_wait (p->arm_proc)) 306 if (GNUNET_OK != GNUNET_OS_process_wait(p->arm_proc))
308 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid"); 307 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "waitpid");
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 308 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
310 "ARM process %u stopped\n", 309 "ARM process %u stopped\n",
311 GNUNET_OS_process_get_pid (p->arm_proc)); 310 GNUNET_OS_process_get_pid(p->arm_proc));
312 GNUNET_OS_process_destroy (p->arm_proc); 311 GNUNET_OS_process_destroy(p->arm_proc);
313 p->arm_proc = NULL; 312 p->arm_proc = NULL;
314 GNUNET_CONFIGURATION_destroy (p->cfg); 313 GNUNET_CONFIGURATION_destroy(p->cfg);
315} 314}
316 315
317 316
318int 317int
319main (int argc, char *argv1[]) 318main(int argc, char *argv1[])
320{ 319{
321 char *const argv[] = {"test-core-api", "-c", "test_core_api_data.conf", NULL}; 320 char *const argv[] = { "test-core-api", "-c", "test_core_api_data.conf", NULL };
322 struct GNUNET_GETOPT_CommandLineOption options[] = {GNUNET_GETOPT_OPTION_END}; 321 struct GNUNET_GETOPT_CommandLineOption options[] = { GNUNET_GETOPT_OPTION_END };
322
323 ok = 1; 323 ok = 1;
324 GNUNET_log_setup ("test-core-api", "WARNING", NULL); 324 GNUNET_log_setup("test-core-api", "WARNING", NULL);
325 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, 325 GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *)) - 1,
326 argv, 326 argv,
327 "test-core-api", 327 "test-core-api",
328 "nohelp", 328 "nohelp",
329 options, 329 options,
330 &run, 330 &run,
331 &ok); 331 &ok);
332 stop_arm (&p1); 332 stop_arm(&p1);
333 stop_arm (&p2); 333 stop_arm(&p2);
334 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1"); 334 GNUNET_DISK_directory_remove("/tmp/test-gnunet-core-peer-1");
335 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2"); 335 GNUNET_DISK_directory_remove("/tmp/test-gnunet-core-peer-2");
336 336
337 return ok; 337 return ok;
338} 338}