first batch of license fixes (boring)
[oweals/gnunet.git] / src / testbed / test_testbed_api_topology.c
1 /*
2   This file is part of GNUnet
3   Copyright (C) 2008--2013 GNUnet e.V.
4
5   GNUnet is free software: you can redistribute it and/or modify it
6   under the terms of the GNU 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
16 /**
17  * @file src/testbed/test_testbed_api_topology.c
18  * @brief testing cases for testing high level testbed api helper functions
19  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
20  */
21
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24 #include "gnunet_testbed_service.h"
25
26 /**
27  * Number of peers we want to start
28  */
29 #define NUM_PEERS 10
30
31 /**
32  * Array of peers
33  */
34 static struct GNUNET_TESTBED_Peer **peers;
35
36 /**
37  * Operation handle
38  */
39 static struct GNUNET_TESTBED_Operation *op;
40
41 /**
42  * Shutdown task
43  */
44 static struct GNUNET_SCHEDULER_Task * shutdown_task;
45
46 /**
47  * Testing result
48  */
49 static int result;
50
51 /**
52  * Counter for counting overlay connections
53  */
54 static unsigned int overlay_connects;
55
56
57 /**
58  * Shutdown nicely
59  *
60  * @param cls NULL
61  */
62 static void
63 do_shutdown (void *cls)
64 {
65   shutdown_task = NULL;
66   if (NULL != op)
67   {
68     GNUNET_TESTBED_operation_done (op);
69     op = NULL;
70   }
71   GNUNET_SCHEDULER_shutdown ();
72 }
73
74 /**
75  * Controller event callback
76  *
77  * @param cls NULL
78  * @param event the controller event
79  */
80 static void
81 controller_event_cb (void *cls,
82                      const struct GNUNET_TESTBED_EventInformation *event)
83 {
84   switch (event->type)
85   {
86   case GNUNET_TESTBED_ET_CONNECT:
87     overlay_connects++;
88     if ((NUM_PEERS) == overlay_connects)
89     {
90       result = GNUNET_OK;
91       GNUNET_SCHEDULER_cancel (shutdown_task);
92       shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
93     }
94     break;
95   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
96     GNUNET_assert (NULL != event->details.operation_finished.emsg);
97     break;
98   default:
99     GNUNET_break (0);
100     if ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) &&
101         (NULL != event->details.operation_finished.emsg))
102       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
103                   "An operation failed with error: %s\n",
104                   event->details.operation_finished.emsg);
105     result = GNUNET_SYSERR;
106     GNUNET_SCHEDULER_cancel (shutdown_task);
107     shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
108   }
109 }
110
111
112 /**
113  * Signature of a main function for a testcase.
114  *
115  * @param cls closure
116  * @param h the run handle
117  * @param num_peers number of peers in 'peers'
118  * @param peers_ handle to peers run in the testbed
119  * @param links_succeeded the number of overlay link connection attempts that
120  *          succeeded
121  * @param links_failed the number of overlay link connection attempts that
122  *          failed
123  */
124 static void
125 test_master (void *cls,
126              struct GNUNET_TESTBED_RunHandle *h,
127              unsigned int num_peers,
128              struct GNUNET_TESTBED_Peer **peers_,
129              unsigned int links_succeeded,
130              unsigned int links_failed)
131 {
132   unsigned int peer;
133
134   GNUNET_assert (NULL == cls);
135   if (NULL == peers_)
136   {
137     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test due to timeout\n");
138     return;
139   }
140   GNUNET_assert (NUM_PEERS == num_peers);
141   for (peer = 0; peer < num_peers; peer++)
142     GNUNET_assert (NULL != peers_[peer]);
143   peers = peers_;
144   overlay_connects = 0;
145   op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers, NULL,
146                                                   NULL,
147                                                   NULL,
148                                                   GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
149                                                   NUM_PEERS,
150                                                   GNUNET_TESTBED_TOPOLOGY_OPTION_END);
151   GNUNET_assert (NULL != op);
152   shutdown_task =
153       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
154                                     (GNUNET_TIME_UNIT_SECONDS, 300),
155                                     do_shutdown, NULL);
156 }
157
158
159 /**
160  * Main function
161  */
162 int
163 main (int argc, char **argv)
164 {
165   uint64_t event_mask;
166
167   result = GNUNET_SYSERR;
168   event_mask = 0;
169   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
170   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
171   (void) GNUNET_TESTBED_test_run ("test_testbed_api_test",
172                                   "test_testbed_api.conf", NUM_PEERS,
173                                   event_mask, &controller_event_cb, NULL,
174                                   &test_master, NULL);
175   if (GNUNET_OK != result)
176     return 1;
177   return 0;
178 }
179
180 /* end of test_testbed_api_topology.c */