aboutsummaryrefslogtreecommitdiff
path: root/src/sensor/test_gnunet-service-sensor_reporting.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sensor/test_gnunet-service-sensor_reporting.c')
-rw-r--r--src/sensor/test_gnunet-service-sensor_reporting.c469
1 files changed, 469 insertions, 0 deletions
diff --git a/src/sensor/test_gnunet-service-sensor_reporting.c b/src/sensor/test_gnunet-service-sensor_reporting.c
new file mode 100644
index 000000000..969ab16ec
--- /dev/null
+++ b/src/sensor/test_gnunet-service-sensor_reporting.c
@@ -0,0 +1,469 @@
1 /*
2 * This file is part of GNUnet.
3 * (C)
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 * @file sensor/test_gnunet-service-sensor_reporting.c
22 * @brief testcase for gnunet-service-sensor_reporting.c
23 */
24#include "platform.h"
25#include "gnunet_util_lib.h"
26#include "gnunet_testbed_service.h"
27#include "gnunet_sensor_util_lib.h"
28#include "sensor.h"
29#include "gnunet_peerstore_service.h"
30#include "gnunet_sensor_service.h"
31
32/**
33 * Number of peers to start for the test
34 */
35#define NUM_PEERS 2
36
37/**
38 * Test timeout
39 */
40#define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
41
42/**
43 * Information about a test peer
44 */
45struct TestPeer
46{
47
48 /**
49 * DLL
50 */
51 struct TestPeer *prev;
52
53 /**
54 * DLL
55 */
56 struct TestPeer *next;
57
58 /**
59 * TESTBED information about the peer
60 */
61 struct GNUNET_TESTBED_Peer *testbed_peer;
62
63 /**
64 * Peer indentity
65 */
66 struct GNUNET_PeerIdentity peer_id;
67
68 /**
69 * Peerstore watch context for this peer's anomaly reports
70 */
71 struct GNUNET_PEERSTORE_WatchContext *wc;
72
73 /**
74 * TESTBED operation connecting us to sensor service
75 */
76 struct GNUNET_TESTBED_Operation *sensor_op;
77
78};
79
80/**
81 * Test name
82 */
83const static char *testname = "test_gnunet-service-sensor_reporting";
84
85/**
86 * Name of GNUNET config file used in this test
87 */
88const static char *cfg_filename = "test_gnunet-service-sensor_reporting.conf";
89
90/**
91 * Test sensor name
92 */
93const static char *sensor_name = "test-sensor-statistics";
94
95/**
96 * Path to read test sensor from
97 */
98const static char *sensor_path_src = "test_sensors/test-sensor-statistics";
99
100/**
101 * Path to write new test sensor to
102 */
103const static char *sensor_path_dest =
104 "/tmp/test-gnunet-service-sensor-reporting/test-sensor-statistics";
105
106/**
107 * Head of DLL of peers
108 */
109static struct TestPeer *peer_head;
110
111/**
112 * Tail of DLL of peers
113 */
114static struct TestPeer *peer_tail;
115
116/**
117 * Number of peers started and got information for
118 */
119static int started_peers = 0;
120
121/**
122 * TESTBED operation connecting us to peerstore service
123 */
124struct GNUNET_TESTBED_Operation *peerstore_op;
125
126/**
127 * Handle to the peerstore service
128 */
129struct GNUNET_PEERSTORE_Handle *peerstore;
130
131/**
132 * Task used to shutdown / expire the test
133 */
134static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
135
136/**
137 * Status of the test to be returned by main()
138 */
139static int ok = 1;
140
141
142/**
143 * Shutdown task
144 *
145 * @param cls Closure (unused)
146 * @param tc Task context (unused)
147 */
148static void
149do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
150{
151 //TODO: destroy list of peers
152 if (NULL != peerstore_op)
153 {
154 GNUNET_TESTBED_operation_done (peerstore_op);
155 peerstore_op = NULL;
156 }
157 GNUNET_SCHEDULER_shutdown ();
158}
159
160
161/**
162 * Write new temp sensor directory with a sensor updated with collection point
163 * peer id
164 */
165void
166write_new_sensor_dir (struct TestPeer *cp_peer)
167{
168 struct GNUNET_CONFIGURATION_Handle *sensorcfg;
169
170 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_test (sensor_path_src));
171 sensorcfg = GNUNET_CONFIGURATION_create ();
172 GNUNET_assert (GNUNET_SYSERR !=
173 GNUNET_CONFIGURATION_parse (sensorcfg, sensor_path_src));
174 GNUNET_CONFIGURATION_set_value_string (sensorcfg, sensor_name,
175 "COLLECTION_POINT",
176 GNUNET_i2s_full (&cp_peer->peer_id));
177 GNUNET_assert (GNUNET_OK ==
178 GNUNET_DISK_directory_create_for_file (sensor_path_dest));
179 GNUNET_CONFIGURATION_write (sensorcfg, sensor_path_dest);
180 GNUNET_CONFIGURATION_destroy (sensorcfg);
181}
182
183
184/**
185 * Function called by PEERSTORE for each matching record.
186 *
187 * @param cls closure
188 * @param record peerstore record information
189 * @param emsg error message, or NULL if no errors
190 * @return #GNUNET_YES to continue iterating, #GNUNET_NO to stop
191 */
192static int
193peerstore_watch_cb (void *cls, struct GNUNET_PEERSTORE_Record *record,
194 char *emsg)
195{
196 struct TestPeer *peer = cls;
197 struct GNUNET_SENSOR_DashboardAnomalyEntry *anomaly;
198
199 GNUNET_assert (NULL != record);
200 GNUNET_assert (record->value_size ==
201 sizeof (struct GNUNET_SENSOR_DashboardAnomalyEntry));
202 anomaly = record->value;
203 GNUNET_assert (0 ==
204 GNUNET_CRYPTO_cmp_peer_identity (&peer->peer_id,
205 record->peer));
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207 "Peerstore watch got an anomaly report from peer `%s':\n"
208 "Anomalous: %d\n" "Anomalous neigbors: %f.\n",
209 GNUNET_i2s (&peer->peer_id), anomaly->anomalous,
210 anomaly->anomalous_neighbors);
211 //TODO
212 return GNUNET_YES;
213}
214
215
216/**
217 * Callback to be called when sensor service connect operation is completed
218 *
219 * @param cls the callback closure from functions generating an operation
220 * @param op the operation that has been finished
221 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
222 * @param emsg error message in case the operation has failed; will be NULL if
223 * operation has executed successfully.
224 */
225static void
226sensor_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
227 void *ca_result, const char *emsg)
228{
229 struct TestPeer *peer = cls;
230 struct GNUNET_SENSOR_Handle *sensor = ca_result;
231
232 GNUNET_SENSOR_force_anomaly (sensor, (char *) sensor_name, GNUNET_YES, NULL,
233 NULL);
234}
235
236
237/**
238 * Adapter function called to establish a connection to sensor service.
239 *
240 * @param cls closure
241 * @param cfg configuration of the peer to connect to; will be available until
242 * GNUNET_TESTBED_operation_done() is called on the operation returned
243 * from GNUNET_TESTBED_service_connect()
244 * @return service handle to return in 'op_result', NULL on error
245 */
246static void *
247sensor_connect_adapter (void *cls,
248 const struct GNUNET_CONFIGURATION_Handle *cfg)
249{
250 struct GNUNET_SENSOR_Handle *sensor;
251
252 sensor = GNUNET_SENSOR_connect (cfg);
253 return sensor;
254}
255
256
257/**
258 * Adapter function called to destroy a connection to sensor service.
259 *
260 * @param cls closure
261 * @param op_result service handle returned from the connect adapter
262 */
263static void
264sensor_disconnect_adapter (void *cls, void *op_result)
265{
266 struct GNUNET_SENSOR_Handle *sensor = op_result;
267
268 GNUNET_SENSOR_disconnect (sensor);
269}
270
271
272/**
273 * Callback to be called when sensor service is started
274 *
275 * @param cls the callback closure from functions generating an operation
276 * @param op the operation that has been finished
277 * @param emsg error message in case the operation has failed; will be NULL if
278 * operation has executed successfully.
279 */
280static void
281sensor_service_started (void *cls, struct GNUNET_TESTBED_Operation *op,
282 const char *emsg)
283{
284 struct TestPeer *peer = cls;
285
286 GNUNET_assert (NULL == emsg);
287 peer->sensor_op =
288 GNUNET_TESTBED_service_connect (NULL, peer->testbed_peer, "sensor",
289 &sensor_connect_cb, peer,
290 &sensor_connect_adapter,
291 &sensor_disconnect_adapter, NULL);
292 GNUNET_TESTBED_operation_done (op);
293}
294
295
296/**
297 * Callback to be called when peerstore service connect operation is completed
298 *
299 * @param cls the callback closure from functions generating an operation
300 * @param op the operation that has been finished
301 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
302 * @param emsg error message in case the operation has failed; will be NULL if
303 * operation has executed successfully.
304 */
305static void
306peerstore_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
307 void *ca_result, const char *emsg)
308{
309 struct TestPeer *peer;
310
311 peer = peer_head;
312 while (NULL != peer)
313 {
314 GNUNET_PEERSTORE_watch (peerstore, "sensordashboard-anomalies",
315 &peer->peer_id, sensor_name, &peerstore_watch_cb,
316 peer);
317 /* Start sensor service */
318 GNUNET_TESTBED_peer_manage_service (NULL, peer->testbed_peer, "sensor",
319 &sensor_service_started, peer, 1);
320 peer = peer->next;
321 }
322}
323
324
325/**
326 * Adapter function called to establish a connection to peerstore service.
327 *
328 * @param cls closure
329 * @param cfg configuration of the peer to connect to; will be available until
330 * GNUNET_TESTBED_operation_done() is called on the operation returned
331 * from GNUNET_TESTBED_service_connect()
332 * @return service handle to return in 'op_result', NULL on error
333 */
334static void *
335peerstore_connect_adapter (void *cls,
336 const struct GNUNET_CONFIGURATION_Handle *cfg)
337{
338 peerstore = GNUNET_PEERSTORE_connect (cfg);
339 GNUNET_assert (NULL != peerstore);
340 return peerstore;
341}
342
343
344/**
345 * Adapter function called to destroy a connection to peerstore service.
346 *
347 * @param cls closure
348 * @param op_result service handle returned from the connect adapter
349 */
350static void
351peerstore_disconnect_adapter (void *cls, void *op_result)
352{
353 GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_NO);
354 peerstore = NULL;
355 peerstore_op = NULL;
356}
357
358
359/**
360 * Callback to be called when dashboard service is started
361 *
362 * @param cls the callback closure from functions generating an operation
363 * @param op the operation that has been finished
364 * @param emsg error message in case the operation has failed; will be NULL if
365 * operation has executed successfully.
366 */
367static void
368dashboard_started (void *cls, struct GNUNET_TESTBED_Operation *op,
369 const char *emsg)
370{
371 GNUNET_assert (NULL == emsg);
372 GNUNET_TESTBED_operation_done (op);
373 /* Connect to peerstore service on first peer */
374 peerstore_op =
375 GNUNET_TESTBED_service_connect (NULL, peer_head->testbed_peer,
376 "peerstore", &peerstore_connect_cb, NULL,
377 &peerstore_connect_adapter,
378 &peerstore_disconnect_adapter, NULL);
379}
380
381
382/**
383 * Callback to be called when the requested peer information is available
384 *
385 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
386 * @param op the operation this callback corresponds to
387 * @param pinfo the result; will be NULL if the operation has failed
388 * @param emsg error message if the operation has failed; will be NULL if the
389 * operation is successfull
390 */
391static void
392peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
393 const struct GNUNET_TESTBED_PeerInformation *pinfo,
394 const char *emsg)
395{
396 struct GNUNET_TESTBED_Peer *testbed_peer = cb_cls;
397 struct TestPeer *peer;
398
399 peer = GNUNET_new (struct TestPeer);
400
401 peer->testbed_peer = testbed_peer;
402 GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id);
403 if (NULL == peer_head) /* First peer (collection point) */
404 {
405 /* Rewrite sensor with collection point peer id */
406 write_new_sensor_dir (peer);
407 }
408 GNUNET_CONTAINER_DLL_insert_tail (peer_head, peer_tail, peer);
409 started_peers++;
410 if (NUM_PEERS == started_peers)
411 {
412 /* Start dashboard service on first peer */
413 GNUNET_TESTBED_peer_manage_service (NULL, peer_head->testbed_peer,
414 "sensordashboard", &dashboard_started,
415 NULL, 1);
416 }
417 GNUNET_TESTBED_operation_done (op);
418}
419
420
421/**
422 * Signature of a main function for a testcase.
423 *
424 * @param cls closure
425 * @param h the run handle
426 * @param num_peers number of peers in 'peers'
427 * @param peers handle to peers run in the testbed. NULL upon timeout (see
428 * GNUNET_TESTBED_test_run()).
429 * @param links_succeeded the number of overlay link connection attempts that
430 * succeeded
431 * @param links_failed the number of overlay link connection attempts that
432 * failed
433 * @see GNUNET_TESTBED_test_run()
434 */
435static void
436test_master (void *cls, struct GNUNET_TESTBED_RunHandle *h,
437 unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers,
438 unsigned int links_succeeded, unsigned int links_failed)
439{
440 int i;
441
442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
443 "%d peers started. %d links succeeded. %d links failed.\n",
444 num_peers, links_succeeded, links_failed);
445 GNUNET_assert (NUM_PEERS == num_peers);
446 GNUNET_assert (0 == links_failed);
447 /* Schedule test timeout */
448 shutdown_task =
449 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &do_shutdown, NULL);
450 /* Collect peer information */
451 for (i = 0; i < num_peers; i++)
452 {
453 GNUNET_TESTBED_peer_get_information (peers[i],
454 GNUNET_TESTBED_PIT_CONFIGURATION,
455 &peer_info_cb, peers[i]);
456 }
457}
458
459
460int
461main (int argc, char *argv[])
462{
463 GNUNET_log_setup (testname, "INFO", NULL);
464 if (GNUNET_OK ==
465 GNUNET_TESTBED_test_run (testname, cfg_filename, NUM_PEERS, 0, NULL, NULL,
466 &test_master, NULL))
467 return ok;
468 return 1;
469}