-next test
[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 const struct GNUNET_SCHEDULER_TaskContext *tc)
60 {
61   GNUNET_SCHEDULER_shutdown ();
62 }
63
64
65 /**
66  * Controller event callback
67  *
68  * @param cls NULL
69  * @param event the controller event
70  */
71 static void
72 controller_event_cb (void *cls,
73                      const struct GNUNET_TESTBED_EventInformation *event)
74 {
75   switch (event->type)
76   {
77   case GNUNET_TESTBED_ET_CONNECT:
78     GNUNET_assert (event->details.peer_connect.peer1 == peers[0]);
79     GNUNET_assert (event->details.peer_connect.peer2 == peers[1]);
80     GNUNET_TESTBED_operation_done (op);
81     op = GNUNET_TESTBED_peer_get_information (peers[0],
82                                               GNUNET_TESTBED_PIT_IDENTITY);
83     break;
84   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
85     GNUNET_assert (event->details.operation_finished.operation == op);
86     GNUNET_assert (NULL == event->details.operation_finished.op_cls);
87     GNUNET_assert (NULL == event->details.operation_finished.emsg);
88     GNUNET_assert (GNUNET_TESTBED_PIT_IDENTITY ==
89                    event->details.operation_finished.pit);
90     GNUNET_assert (NULL != event->details.operation_finished.op_result.pid);
91     GNUNET_TESTBED_operation_done (op);
92     result = GNUNET_OK;
93     GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
94     break;
95   default:
96     GNUNET_assert (0);
97   }  
98 }
99
100
101 /**
102  * Signature of a main function for a testcase.
103  *
104  * @param cls closure
105  * @param num_peers number of peers in 'peers'
106  * @param peers handle to peers run in the testbed
107  */
108 static void
109 test_master (void *cls, unsigned int num_peers,
110              struct GNUNET_TESTBED_Peer **peers_)
111 {
112   unsigned int peer;
113
114   GNUNET_assert (NULL == cls);
115   GNUNET_assert (NUM_PEERS == num_peers);
116   GNUNET_assert (NULL != peers_);
117   for (peer = 0; peer < num_peers; peer++)
118     GNUNET_assert (NULL != peers_[peer]);
119   peers = peers_;
120   op = GNUNET_TESTBED_overlay_connect (NULL, peers[0], peers[1]);  
121 }
122
123
124 /**
125  * Main function
126  */
127 int
128 main (int argc, char **argv)
129 {
130   uint64_t event_mask;
131
132   result = GNUNET_SYSERR;
133   event_mask = 0;
134   event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
135   event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
136   GNUNET_TESTBED_test_run ("test_testbed_api_test", "test_testbed_api.conf",
137                            NUM_PEERS, event_mask, &controller_event_cb, NULL,
138                            &test_master, NULL);
139   if (GNUNET_OK != result)
140     return 1;
141   return 0;
142 }
143
144 /* end of test_testbed_api_test.c */