guix-env: some update.
[oweals/gnunet.git] / src / testbed / test_testbed_api_statistics.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 testbed/test_testbed_api_statistics.c
23  * @brief testcase for testing GNUNET_TESTBED_get_statistics() implementation
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 5
35
36 /**
37  * The array of peers; we get them from the testbed
38  */
39 static struct GNUNET_TESTBED_Peer **peers;
40
41 /**
42  * Operation handle
43  */
44 static struct GNUNET_TESTBED_Operation *op;
45
46 /**
47  * dummy pointer
48  */
49 static void *dummy_cls = (void *) 0xDEAD0001;
50
51 /**
52  * Abort task identifier
53  */
54 static struct GNUNET_SCHEDULER_Task * abort_task;
55
56 /**
57  * Global testing result
58  */
59 static int result;
60
61 /**
62  * The peers we have seen in the statistics iterator
63  */
64 static struct GNUNET_TESTBED_Peer **seen_peers;
65
66 /**
67  * Number of peers in the above array
68  */
69 static unsigned int num_seen_peers;
70
71
72 /**
73  * Fail testcase
74  */
75 #define FAIL_TEST(cond, ret) do {                               \
76     if (!(cond)) {                                              \
77       GNUNET_break(0);                                          \
78       if (NULL != abort_task)               \
79         GNUNET_SCHEDULER_cancel (abort_task);                   \
80       abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);  \
81       ret;                                                      \
82     }                                                           \
83   } while (0)
84
85
86 /**
87  * Abort task
88  *
89  * @param cls NULL
90  */
91 static void
92 do_abort (void *cls)
93 {
94   GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test timed out -- Aborting\n");
95   abort_task = NULL;
96   if (NULL != op)
97   {
98     GNUNET_TESTBED_operation_done (op);
99     op = NULL;
100   }
101   result = GNUNET_SYSERR;
102 }
103
104
105 /**
106  * Callback function to process statistic values from all peers.
107  *
108  * @param cls closure
109  * @param peer the peer the statistic belong to
110  * @param subsystem name of subsystem that created the statistic
111  * @param name the name of the datum
112  * @param value the current value
113  * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
114  * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
115  */
116 static int
117 stats_iterator (void *cls,
118                 const struct GNUNET_TESTBED_Peer *peer,
119                 const char *subsystem, const char *name, uint64_t value,
120                 int is_persistent)
121 {
122   unsigned int cnt;
123
124   FAIL_TEST (cls == dummy_cls, return GNUNET_SYSERR);
125   for (cnt = 0; cnt < num_seen_peers; cnt++)
126     FAIL_TEST (peer != seen_peers[cnt], return GNUNET_SYSERR);
127   FAIL_TEST (NULL != subsystem, return GNUNET_SYSERR);
128   FAIL_TEST (NULL != name, return GNUNET_SYSERR);
129   GNUNET_array_append (seen_peers, num_seen_peers,
130                        (struct GNUNET_TESTBED_Peer *) peer);
131   return GNUNET_SYSERR;
132 }
133
134
135 /**
136  * Callback to be called when an operation is completed
137  *
138  * @param cls the callback closure from functions generating an operation
139  * @param op the operation that has been finished
140  * @param emsg error message in case the operation has failed; will be NULL if
141  *          operation has executed successfully.
142  */
143 static void
144 op_comp_cb (void *cls,
145             struct GNUNET_TESTBED_Operation *op,
146             const char *emsg)
147 {
148   FAIL_TEST (cls == dummy_cls, return);
149   result = GNUNET_OK;
150   GNUNET_TESTBED_operation_done (op);
151   op = NULL;
152   GNUNET_SCHEDULER_cancel (abort_task);
153   GNUNET_SCHEDULER_shutdown ();
154 }
155
156
157 /**
158  * Signature of a main function for a testcase.
159  *
160  * @param cls closure
161  * @param h the run handle
162  * @param num_peers number of peers in 'peers'
163  * @param peers_ handle to peers run in the testbed
164  * @param links_succeeded the number of overlay link connection attempts that
165  *          succeeded
166  * @param links_failed the number of overlay link connection attempts that
167  *          failed
168  */
169 static void
170 test_master (void *cls,
171              struct GNUNET_TESTBED_RunHandle *h,
172              unsigned int num_peers,
173              struct GNUNET_TESTBED_Peer **peers_,
174              unsigned int links_succeeded,
175              unsigned int links_failed)
176 {
177   FAIL_TEST (NUM_PEERS == num_peers, return);
178   peers = peers_;
179   op = GNUNET_TESTBED_get_statistics (num_peers, peers,
180                                       NULL, NULL,
181                                       &stats_iterator,
182                                       &op_comp_cb,
183                                       dummy_cls);
184   abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
185                                              (GNUNET_TIME_UNIT_MINUTES, 1),
186                                              &do_abort, NULL);
187 }
188
189
190 /**
191  * Main function
192  */
193 int
194 main (int argc, char **argv)
195 {
196   (void) GNUNET_TESTBED_test_run ("test_testbed_api_statistics",
197                                   "test_testbed_api_statistics.conf",
198                                   NUM_PEERS,
199                                   1LL, NULL, NULL,
200                                   &test_master, NULL);
201   GNUNET_free_non_null (seen_peers);
202   if (GNUNET_OK != result)
203     return 1;
204   return 0;
205 }