stop peer before destroying it
[oweals/gnunet.git] / src / testbed / test_testbed_api_test.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_test.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 25
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 /**
53  * Shutdown nicely
54  *
55  * @param cls NULL
56  * @param tc the task context
57  */
58 static void
59 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
60 {
61   GNUNET_SCHEDULER_shutdown ();
62 }
63
64
65 /**
66  * Callback to be called when the requested peer information is available
67  *
68  * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
69  * @param op the operation this callback corresponds to
70  * @param pinfo the result; will be NULL if the operation has failed
71  * @param emsg error message if the operation has failed; will be NULL if the
72  *          operation is successfull
73  */
74 static void 
75 peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op_,
76              const struct GNUNET_TESTBED_PeerInformation *pinfo,
77              const char *emsg)
78 {
79   GNUNET_assert (op == op_);
80   GNUNET_assert (NULL == cb_cls);
81   GNUNET_assert (NULL == emsg);
82   GNUNET_assert (GNUNET_TESTBED_PIT_IDENTITY == pinfo->pit);
83   GNUNET_assert (NULL != pinfo->result.id);
84   GNUNET_TESTBED_operation_done (op);
85   result = GNUNET_OK;
86   GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
87 }
88
89
90 /**
91  * Callback to be called when an operation is completed
92  *
93  * @param cls the callback closure from functions generating an operation
94  * @param op the operation that has been finished
95  * @param emsg error message in case the operation has failed; will be NULL if
96  *          operation has executed successfully.
97  */
98 static void 
99 op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op_, const char *emsg)
100 {
101   GNUNET_assert (NULL == cls);
102   GNUNET_assert (op == op_);
103   GNUNET_assert (NULL == emsg);
104   GNUNET_TESTBED_operation_done (op);
105   op = GNUNET_TESTBED_peer_get_information (peers[0],
106                                             GNUNET_TESTBED_PIT_IDENTITY,
107                                             &peerinfo_cb, NULL);
108 }
109
110
111 /**
112  * Controller event callback
113  *
114  * @param cls NULL
115  * @param event the controller event
116  */
117 static void
118 controller_event_cb (void *cls,
119                      const struct GNUNET_TESTBED_EventInformation *event)
120 {
121   switch (event->type)
122   {
123   case GNUNET_TESTBED_ET_CONNECT:
124     GNUNET_assert (event->details.peer_connect.peer1 == peers[0]);
125     GNUNET_assert (event->details.peer_connect.peer2 == peers[1]);    
126     break;
127   default:
128     GNUNET_assert (0);
129   }  
130 }
131
132
133 /**
134  * Signature of a main function for a testcase.
135  *
136  * @param cls closure
137  * @param num_peers number of peers in 'peers'
138  * @param peers handle to peers run in the testbed
139  */
140 static void
141 test_master (void *cls, unsigned int num_peers,
142              struct GNUNET_TESTBED_Peer **peers_)
143 {
144   unsigned int peer;
145
146   GNUNET_assert (NULL == cls);
147   GNUNET_assert (NUM_PEERS == num_peers);
148   GNUNET_assert (NULL != peers_);
149   for (peer = 0; peer < num_peers; peer++)
150     GNUNET_assert (NULL != peers_[peer]);
151   peers = peers_;
152   op = GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peers[0], peers[1]);
153 }
154
155
156 /**
157  * Main function
158  */
159 int
160 main (int argc, char **argv)
161 {
162   uint64_t event_mask;
163
164   result = GNUNET_SYSERR;
165   event_mask = 0;
166   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
167   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
168   GNUNET_TESTBED_test_run ("test_testbed_api_test", "test_testbed_api.conf",
169                            NUM_PEERS, 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_test.c */