towards handling suboperations during overlay connect
[oweals/gnunet.git] / src / testbed / test_testbed_api_topology.c
1 /*
2   This file is part of GNUnet
3   (C) 2008--2012 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 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_common.h"
29 #include "gnunet_testbed_service.h"
30
31 /**
32  * Number of peers we want to start
33  */
34 #define NUM_PEERS 5
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  * 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  * @param tc the task context
62  */
63 static void
64 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
65 {
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 - 1) == overlay_connects)
89     {
90       result = GNUNET_OK;
91       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
92     }
93     break;
94   default:
95     GNUNET_break (0);
96     if ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) && 
97         (NULL != event->details.operation_finished.emsg))
98       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
99                   "An operation failed with error: %s\n",
100                   event->details.operation_finished.emsg);
101     result = GNUNET_SYSERR;
102     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
103   }  
104 }
105
106
107 /**
108  * Signature of a main function for a testcase.
109  *
110  * @param cls closure
111  * @param num_peers number of peers in 'peers'
112  * @param peers handle to peers run in the testbed
113  */
114 static void
115 test_master (void *cls, unsigned int num_peers,
116              struct GNUNET_TESTBED_Peer **peers_)
117 {
118   unsigned int peer;
119
120   GNUNET_assert (NULL == cls);
121   GNUNET_assert (NUM_PEERS == num_peers);
122   GNUNET_assert (NULL != peers_);
123   for (peer = 0; peer < num_peers; peer++)
124     GNUNET_assert (NULL != peers_[peer]);
125   peers = peers_;
126   overlay_connects = 0;
127   op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers,
128                                                   GNUNET_TESTBED_TOPOLOGY_LINE);
129   GNUNET_assert (NULL != op);
130 }
131
132
133 /**
134  * Main function
135  */
136 int
137 main (int argc, char **argv)
138 {
139   uint64_t event_mask;
140
141   result = GNUNET_SYSERR;
142   event_mask = 0;
143   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
144   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
145   GNUNET_TESTBED_test_run ("test_testbed_api_test", "test_testbed_api.conf",
146                            NUM_PEERS, event_mask, &controller_event_cb, NULL,
147                            &test_master, NULL);
148   if (GNUNET_OK != result)
149     return 1;
150   return 0;
151 }
152
153 /* end of test_testbed_api_topology.c */