aboutsummaryrefslogtreecommitdiff
path: root/src/messenger/test_messenger_comm0.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/messenger/test_messenger_comm0.c')
-rw-r--r--src/messenger/test_messenger_comm0.c252
1 files changed, 252 insertions, 0 deletions
diff --git a/src/messenger/test_messenger_comm0.c b/src/messenger/test_messenger_comm0.c
new file mode 100644
index 000000000..631b5b2c9
--- /dev/null
+++ b/src/messenger/test_messenger_comm0.c
@@ -0,0 +1,252 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2020 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 messenger/test_messenger_comm0.c
22 * @author Tobias Frisch
23 * @brief Test for the messenger service using cadet API.
24 */
25#include <stdio.h>
26#include "platform.h"
27#include "gnunet_util_lib.h"
28#include "gnunet_testbed_logger_service.h"
29#include "gnunet_testbed_service.h"
30#include "gnunet_testing_lib.h"
31#include "gnunet_messenger_service.h"
32
33/**
34 * How long until we really give up on a particular testcase portion?
35 */
36#define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
37 60)
38
39/**
40 * How long until we give up on any particular operation (and retry)?
41 */
42#define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
43
44static int status = 1;
45
46static struct GNUNET_SCHEDULER_Task *die_task = NULL;
47static struct GNUNET_SCHEDULER_Task *op_task = NULL;
48
49static void
50end (void *cls)
51{
52 die_task = NULL;
53
54 if (op_task)
55 {
56 GNUNET_SCHEDULER_cancel (op_task);
57 op_task = NULL;
58 }
59
60 GNUNET_SCHEDULER_shutdown ();
61 status = 0;
62}
63
64
65static void
66end_badly (void *cls)
67{
68 fprintf (stderr, "Testcase failed (timeout).\n");
69
70 end (NULL);
71 status = 1;
72}
73
74static void
75end_operation (void *cls)
76{
77 op_task = NULL;
78
79 fprintf (stderr, "Testcase failed (operation: '%s').\n", cls? (const char*) cls : "unknown");
80
81 if (die_task)
82 GNUNET_SCHEDULER_cancel (die_task);
83
84 end (NULL);
85 status = 1;
86}
87
88static void
89end_error (void *cls)
90{
91 op_task = NULL;
92
93 fprintf (stderr, "Testcase failed (error: '%s').\n", cls? (const char*) cls : "unknown");
94 GNUNET_free(cls);
95
96 if (die_task)
97 GNUNET_SCHEDULER_cancel (die_task);
98
99 end (NULL);
100 status = 1;
101}
102
103/**
104 * Function called whenever a message is received or sent.
105 *
106 * @param cls Closure
107 * @param room Room
108 * @param message Message
109 * @param hash Hash of message
110 */
111static void
112on_message (void *cls, const struct GNUNET_MESSENGER_Room *room, const struct GNUNET_MESSENGER_Message *message,
113 const struct GNUNET_HashCode *hash)
114{
115 // TODO
116}
117
118/**
119 * Function called when an identity is retrieved.
120 *
121 * @param cls Closure
122 * @param handle Handle of messenger service
123 */
124static void
125on_identity (void *cls, struct GNUNET_MESSENGER_Handle *handle)
126{
127 // TODO
128}
129
130static void
131on_peer (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
132 const struct GNUNET_TESTBED_PeerInformation *pinfo,
133 const char *emsg)
134{
135 if (emsg)
136 {
137 op_task = GNUNET_SCHEDULER_add_now (&end_error, GNUNET_strdup(emsg));
138 return;
139 }
140
141 if (pinfo->pit != GNUNET_TESTBED_PIT_CONFIGURATION)
142 {
143 op_task = GNUNET_SCHEDULER_add_now (&end_operation, "config");
144 return;
145 }
146
147 struct GNUNET_MESSENGER_Handle *handle;
148 struct GNUNET_MESSENGER_Room *room;
149
150 fprintf (stderr, "MSG: connect\n");
151
152 handle = GNUNET_MESSENGER_connect(pinfo->result.cfg, "tester", &on_identity, NULL, &on_message, NULL);
153
154 struct GNUNET_HashCode hash;
155 GNUNET_CRYPTO_hash("test", 4, &hash);
156
157 fprintf (stderr, "MSG: open\n");
158
159 room = GNUNET_MESSENGER_open_room(handle, &hash);
160
161 fprintf (stderr, "MSG: close\n");
162
163 GNUNET_MESSENGER_close_room(room);
164
165 fprintf (stderr, "MSG: disconnect\n");
166
167 GNUNET_MESSENGER_disconnect(handle);
168
169 GNUNET_TESTBED_operation_done(op);
170
171}
172
173/**
174 * Main function for a peer of the testcase.
175 *
176 * @param cls Closure
177 * @param event Information about the event
178 */
179static void
180run (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
181{
182 if (GNUNET_TESTBED_ET_PEER_START != event->type)
183 {
184 op_task = GNUNET_SCHEDULER_add_now (&end_operation, "start");
185 return;
186 }
187
188 GNUNET_TESTBED_peer_get_information(event->details.peer_start.peer,
189 GNUNET_TESTBED_PIT_CONFIGURATION,
190 on_peer, event->details.peer_start.peer);
191
192 fprintf (stderr, "MSG: barrier\n");
193
194 GNUNET_TESTBED_barrier_wait("exit", NULL, NULL);
195
196 fprintf (stderr, "MSG: exit\n");
197}
198
199static void
200exit_status (void *cls, const char *name,
201 struct GNUNET_TESTBED_Barrier *barrier,
202 enum GNUNET_TESTBED_BarrierStatus status,
203 const char *emsg)
204{
205 if (emsg)
206 {
207 op_task = GNUNET_SCHEDULER_add_now (&end_error, GNUNET_strdup(emsg));
208 return;
209 }
210
211 if (GNUNET_TESTBED_BARRIERSTATUS_ERROR == status)
212 {
213 op_task = GNUNET_SCHEDULER_add_now (&end_operation, "exit");
214 return;
215 }
216 else if (GNUNET_TESTBED_BARRIERSTATUS_CROSSED == status)
217 GNUNET_SCHEDULER_add_now(&end, NULL);
218}
219
220static void
221init (void *cls, struct GNUNET_TESTBED_RunHandle *h, unsigned int num_peers,
222 struct GNUNET_TESTBED_Peer **peers, unsigned int links_succeeded,
223 unsigned int links_failed)
224{
225 die_task = GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT, &end_badly, NULL);
226
227 struct GNUNET_TESTBED_Controller *controller;
228
229 controller = GNUNET_TESTBED_run_get_controller_handle(h);
230
231 GNUNET_TESTBED_barrier_init(controller, "exit", num_peers, exit_status, NULL);
232}
233
234/**
235 * The main function.
236 *
237 * @param argc number of arguments from the command line
238 * @param argv command line arguments
239 * @return 0 ok, 1 on error
240 */
241int
242main(int argc, char **argv)
243{
244 if (GNUNET_OK != GNUNET_TESTBED_test_run("test-messenger-comm0",
245 "test_messenger_api.conf",
246 2, 0,
247 &run, NULL,
248 &init, NULL))
249 return 1;
250
251 return status;
252}