remove legacy core api code (now dead)
[oweals/gnunet.git] / src / multicast / test_multicast_multipeer.c
1 /*
2  * This file is part of GNUnet
3  * Copyright (C) 2013 GNUnet e.V.
4  *
5  * GNUnet is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 3, or (at your
8  * 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  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with GNUnet; see the file COPYING.  If not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 /**
22  * @file multicast/test_multicast_multipeers.c
23  * @brief Tests for the Multicast API with multiple peers.
24  * @author xrs
25  */
26
27 #include <inttypes.h>
28
29 #include "platform.h"
30 #include "gnunet_crypto_lib.h"
31 #include "gnunet_common.h"
32 #include "gnunet_util_lib.h"
33 #include "gnunet_testbed_service.h"
34 #include "gnunet_multicast_service.h"
35
36 #define NUM_PEERS 2
37
38 static struct GNUNET_TESTBED_Operation *peer0;
39 static struct GNUNET_TESTBED_Operation *peer1;
40
41 static struct GNUNET_SCHEDULER_Task *timeout_tid;
42
43 struct GNUNET_CRYPTO_EddsaPrivateKey *group_key;
44 struct GNUNET_CRYPTO_EddsaPublicKey group_pub_key;
45
46 struct GNUNET_CRYPTO_EcdsaPrivateKey *member1_key;
47 struct GNUNET_CRYPTO_EcdsaPublicKey member1_pub_key;
48
49
50 enum
51 {
52   TEST_INIT          = 0,
53   TEST_ORIGIN_START  = 1,
54   TEST_MEMBER_JOIN   = 2,
55 } test;
56
57
58 /**
59  * Global result for testcase.
60  */
61 static int result;
62
63
64 /**
65  * Function run on CTRL-C or shutdown (i.e. success/timeout/etc.).
66  * Cleans up.
67  */
68 static void
69 shutdown_task (void *cls)
70 {
71   if (NULL != peer0)
72   {
73     GNUNET_TESTBED_operation_done (peer0);
74     peer0 = NULL;
75   }
76   if (NULL != timeout_tid)
77     {
78       GNUNET_SCHEDULER_cancel (timeout_tid);
79       timeout_tid = NULL;
80     }
81 }
82
83
84 static void
85 timeout_task (void *cls)
86 {
87   timeout_tid = NULL;
88   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
89               "Timeout!\n");
90   result = GNUNET_SYSERR;
91   GNUNET_SCHEDULER_shutdown ();
92 }
93
94
95 static void
96 origin_recv_replay_msg (void *cls,
97                         const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
98                         uint64_t message_id,
99                         uint64_t fragment_offset,
100                         uint64_t flags,
101                         struct GNUNET_MULTICAST_ReplayHandle *rh)
102 {
103   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
104               "Test #%u: origin_recv_replay_msg()\n", test);
105   GNUNET_assert (0);
106 }
107
108
109 static void
110 member_recv_replay_msg (void *cls,
111                         const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
112                         uint64_t message_id,
113                         uint64_t fragment_offset,
114                         uint64_t flags,
115                         struct GNUNET_MULTICAST_ReplayHandle *rh)
116 {
117   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
118               "Test #%u: member_recv_replay_msg()\n", test);
119   GNUNET_assert (0);
120 }
121
122
123 static void
124 origin_recv_replay_frag (void *cls,
125                          const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
126                          uint64_t fragment_id,
127                          uint64_t flags,
128                          struct GNUNET_MULTICAST_ReplayHandle *rh)
129 {
130   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
131               "Test #%u: origin_recv_replay_frag()"
132               " - fragment_id=%" PRIu64 " flags=%" PRIu64 "\n",
133               test, fragment_id, flags);
134 }
135
136
137 /**
138  * Test: origin receives join request
139  */
140 static void
141 origin_recv_join_request (void *cls,
142                           const struct GNUNET_CRYPTO_EcdsaPublicKey *mem_key,
143                           const struct GNUNET_MessageHeader *join_msg,
144                           struct GNUNET_MULTICAST_JoinHandle *jh)
145 {
146   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
147               "Test #%u: origin_recv_join_request()\n", test);
148 }
149
150
151 static void
152 origin_recv_request (void *cls,
153                      const struct GNUNET_MULTICAST_RequestHeader *req)
154 {
155   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
156               "Test #%u: origin_recv_request()\n",
157               test);
158 }
159
160
161 static void
162 origin_recv_message (void *cls,
163                      const struct GNUNET_MULTICAST_MessageHeader *msg)
164 {
165   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
166               "Test #%u: origin_recv_message()\n",
167               test);
168 }
169
170
171 static void
172 service_close_peer0 (void *cls,
173                      void *op_result)
174 {
175   struct GNUNET_MULTICAST_Origin *orig = op_result;
176
177   GNUNET_MULTICAST_origin_stop (orig, NULL, NULL);
178 }
179
180
181 /**
182  * Function run when service multicast has started and is providing us
183  * with a configuration file.
184  */
185 static void *
186 service_conf_peer0 (void *cls,
187                     const struct GNUNET_CONFIGURATION_Handle *cfg)
188 {
189   group_key = GNUNET_CRYPTO_eddsa_key_create ();
190   GNUNET_CRYPTO_eddsa_key_get_public (group_key, &group_pub_key);
191
192   return GNUNET_MULTICAST_origin_start (cfg, group_key, 0,
193                                         origin_recv_join_request,
194                                         origin_recv_replay_frag,
195                                         origin_recv_replay_msg,
196                                         origin_recv_request,
197                                         origin_recv_message,
198                                         NULL);
199 }
200
201
202 /**
203  * Test logic of peer "0" being origin starts here.
204  *
205  * @param cls closure, for the example: NULL
206  * @param op should be equal to "dht_op"
207  * @param ca_result result of the connect operation, the
208  *        connection to the DHT service
209  * @param emsg error message, if testbed somehow failed to
210  *        connect to the DHT.
211  */
212 static void
213 service_connect_peer0 (void *cls,
214                        struct GNUNET_TESTBED_Operation *op,
215                        void *ca_result,
216                        const char *emsg)
217 {
218   struct GNUNET_MULTICAST_Origin *orig = ca_result;
219
220   /* Connection to service successful. Here we'd usually do something with
221    * the service. */
222   result = GNUNET_OK;
223   GNUNET_SCHEDULER_shutdown (); /* Also kills the testbed */
224 }
225
226
227 /**
228  * Main function inovked from TESTBED once all of the
229  * peers are up and running.  This one then connects
230  * just to the multicast service of peer 0 and 1.
231  * Peer 0 is going to be origin.
232  * Peer 1 is going to be one member.
233  * Origin will start a multicast group and the member will try to join it.
234  * After that we execute some multicast test.
235  *
236  * @param cls closure
237  * @param h the run handle
238  * @param peers started peers for the test
239  * @param num_peers size of the 'peers' array
240  * @param links_succeeded number of links between peers that were created
241  * @param links_failed number of links testbed was unable to establish
242  */
243 static void
244 run (void *cls,
245      struct GNUNET_TESTBED_RunHandle *h,
246      unsigned int num_peers,
247      struct GNUNET_TESTBED_Peer **peers,
248      unsigned int links_succeeded,
249      unsigned int links_failed)
250 {
251   /* Testbed is ready with peers running and connected in a pre-defined overlay
252      topology (FIXME)  */
253
254   /* connect to a peers service */
255   peer0 = GNUNET_TESTBED_service_connect
256       (NULL,                    /* Closure for operation */
257        peers[0],                /* The peer whose service to connect to */
258        "multicast",             /* The name of the service */
259        &service_connect_peer0,   /* callback to call after a handle to service
260                                    is opened */
261        NULL,                    /* closure for the above callback */
262        &service_conf_peer0,      /* callback to call with peer's configuration;
263                                    this should open the needed service connection */
264        &service_close_peer0,     /* callback to be called when closing the
265                                    opened service connection */
266        NULL);                   /* closure for the above two callbacks */
267   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
268   timeout_tid = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
269                                               &timeout_task, NULL);
270 }
271
272
273 int
274 main (int argc, char *argv[])
275 {
276   int ret;
277
278   result = GNUNET_SYSERR;
279   ret = GNUNET_TESTBED_test_run
280       ("test-multicast-multipeer",  /* test case name */
281        "test_multicast.conf", /* template configuration */
282        NUM_PEERS,       /* number of peers to start */
283        0LL, /* Event mask - set to 0 for no event notifications */
284        NULL, /* Controller event callback */
285        NULL, /* Closure for controller event callback */
286        run, /* continuation callback to be called when testbed setup is complete */
287        NULL); /* Closure for the test_master callback */
288   if ( (GNUNET_OK != ret) || (GNUNET_OK != result) )
289     return 1;
290   return 0;
291 }
292
293 /* end of test_multicast_multipeer.c */