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