testcase to connect daemons in a clique
[oweals/gnunet.git] / src / experimentation / test_experimentation_clique.c
1 /*
2   This file is part of GNUnet
3   (C) 2008--2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18   Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file src/testbed/test_testbed_api_topology.c
23  * @brief test case to connect experimentation daemons in a clique
24  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
25  * @author Matthias Wachs
26  */
27
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_testbed_service.h"
31
32
33 /**
34  * Number of peers we want to start
35  */
36 #define NUM_PEERS 2
37
38 /**
39  * Array of peers
40  */
41 static struct GNUNET_TESTBED_Peer **peers;
42
43 /**
44  * Operation handle
45  */
46 static struct GNUNET_TESTBED_Operation *op;
47
48 /**
49  * Shutdown task
50  */
51 static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
52
53 /**
54  * Testing result
55  */
56 static int result;
57
58 /**
59  * Counter for counting overlay connections
60  */
61 static unsigned int overlay_connects;
62
63
64 /**
65  * Shutdown nicely
66  *
67  * @param cls NULL
68  * @param tc the task context
69  */
70 static void
71 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
72 {
73   shutdown_task = GNUNET_SCHEDULER_NO_TASK;
74   if (NULL != op)
75   {
76     GNUNET_TESTBED_operation_done (op);
77     op = NULL;
78   }
79   GNUNET_SCHEDULER_shutdown ();
80 }
81
82 /**
83  * Controller event callback
84  *
85  * @param cls NULL
86  * @param event the controller event
87  */
88 static void
89 controller_event_cb (void *cls,
90                      const struct GNUNET_TESTBED_EventInformation *event)
91 {
92   switch (event->type)
93   {
94   case GNUNET_TESTBED_ET_CONNECT:
95     overlay_connects++;
96     if ((NUM_PEERS * (NUM_PEERS - 1)) == overlay_connects)
97     {
98       result = GNUNET_OK;
99       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
100     }
101     break;
102   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
103     GNUNET_assert (NULL != event->details.operation_finished.emsg);
104     break;
105   default:
106     GNUNET_break (0);
107     result = GNUNET_SYSERR;
108     GNUNET_SCHEDULER_cancel (shutdown_task);
109     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
110   }
111 }
112
113
114 /**
115  * Signature of a main function for a testcase.
116  *
117  * @param cls closure
118  * @param num_peers number of peers in 'peers'
119  * @param peers_ handle to peers run in the testbed
120  * @param links_succeeded the number of overlay link connection attempts that
121  *          succeeded
122  * @param links_failed the number of overlay link connection attempts that
123  *          failed
124  */
125 static void
126 test_master (void *cls, unsigned int num_peers,
127              struct GNUNET_TESTBED_Peer **peers_,
128              unsigned int links_succeeded,
129              unsigned int links_failed)
130 {
131   unsigned int peer;
132
133   GNUNET_assert (NULL == cls);
134   GNUNET_assert (NUM_PEERS == num_peers);
135   GNUNET_assert (NULL != peers_);
136   for (peer = 0; peer < num_peers; peer++)
137     GNUNET_assert (NULL != peers_[peer]);
138   peers = peers_;
139   overlay_connects = 0;
140   op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers, NULL,
141                                                   NULL,
142                                                   NULL,
143                                                   GNUNET_TESTBED_TOPOLOGY_CLIQUE,
144                                                   /* GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI, */
145                                                   /* NUM_PEERS, */
146                                                   GNUNET_TESTBED_TOPOLOGY_OPTION_END);
147   GNUNET_assert (NULL != op);
148   shutdown_task =
149       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
150                                     (GNUNET_TIME_UNIT_SECONDS, 300),
151                                     do_shutdown, NULL);
152 }
153
154
155 /**
156  * Main function
157  */
158 int
159 main (int argc, char **argv)
160 {
161   uint64_t event_mask;
162
163   result = GNUNET_SYSERR;
164   event_mask = 0;
165   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
166   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
167   (void) GNUNET_TESTBED_test_run ("test_experimentation_clique",
168                                   "test_experimentation_clique.conf", NUM_PEERS,
169                                   event_mask, &controller_event_cb, NULL,
170                                   &test_master, NULL);
171   if (GNUNET_OK != result)
172     return 1;
173   return 0;
174 }
175
176 /* end of test_testbed_api_topology.c */