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