2 * This file is part of GNUnet.
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.
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.
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.
21 * @file sensor/test_gnunet-service-sensor_reporting.c
22 * @brief testcase for gnunet-service-sensor_reporting.c
25 #include "gnunet_util_lib.h"
26 #include "gnunet_testbed_service.h"
27 #include "gnunet_sensor_util_lib.h"
29 #include "gnunet_peerstore_service.h"
30 #include "gnunet_sensor_service.h"
33 * Number of peers to start for the test
40 #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1)
43 * How long to wait between starting everything and forcing anomalies to give
44 * the peer enough time to stabilize.
46 #define ANOMALY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
49 * Information about a test peer
57 struct TestPeer *prev;
62 struct TestPeer *next;
65 * TESTBED information about the peer
67 struct GNUNET_TESTBED_Peer *testbed_peer;
72 struct GNUNET_PeerIdentity peer_id;
75 * Peerstore watch context for this peer's anomaly reports
77 struct GNUNET_PEERSTORE_WatchContext *wc;
80 * TESTBED operation connecting us to sensor service
82 struct GNUNET_TESTBED_Operation *sensor_op;
85 * Sensor service handle
87 struct GNUNET_SENSOR_Handle *sensor;
90 * GNUNET scheduler task that forces the anomaly after a stabilization delay
92 struct GNUNET_SCHEDULER_Task * delay_task;
99 static const char *testname = "test_gnunet-service-sensor_reporting";
102 * Name of GNUNET config file used in this test
104 static const char *cfg_filename = "test_gnunet-service-sensor_reporting.conf";
109 static const char *sensor_name = "test-sensor-statistics";
112 * Path to read test sensor from
114 static const char *sensor_path_src = "test_sensors/test-sensor-statistics";
117 * Path to write new test sensor to
119 static const char *sensor_path_dest =
120 "/tmp/test-gnunet-service-sensor-reporting/test-sensor-statistics";
123 * Head of DLL of peers
125 static struct TestPeer *peer_head;
128 * Tail of DLL of peers
130 static struct TestPeer *peer_tail;
133 * Number of peers started and got information for
135 static int started_peers = 0;
138 * Number of peers reported anomalies with full list of anomalous neighbors
140 static int reported_peers = 0;
143 * TESTBED operation connecting us to peerstore service
145 static struct GNUNET_TESTBED_Operation *peerstore_op;
148 * Handle to the peerstore service
150 static struct GNUNET_PEERSTORE_Handle *peerstore;
153 * Task used to shutdown / expire the test
155 static struct GNUNET_SCHEDULER_Task * shutdown_task;
158 * Status of the test to be returned by main()
164 destroy_peer (struct TestPeer *peer)
166 if (NULL != peer->delay_task)
168 GNUNET_SCHEDULER_cancel (peer->delay_task);
169 peer->delay_task = NULL;
171 if (NULL != peer->sensor_op)
173 GNUNET_TESTBED_operation_done (peer->sensor_op);
174 peer->sensor_op = NULL;
176 if (NULL != peer->wc)
178 GNUNET_PEERSTORE_watch_cancel (peer->wc);
188 * @param cls Closure (unused)
189 * @param tc Task context (unused)
192 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
194 struct TestPeer *peer;
199 GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, peer);
203 if (NULL != peerstore_op)
205 GNUNET_TESTBED_operation_done (peerstore_op);
208 GNUNET_SCHEDULER_shutdown ();
213 * Write new temp sensor directory with a sensor updated with collection point
217 write_new_sensor_dir (struct TestPeer *cp_peer)
219 struct GNUNET_CONFIGURATION_Handle *sensorcfg;
221 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_test (sensor_path_src));
222 sensorcfg = GNUNET_CONFIGURATION_create ();
223 GNUNET_assert (GNUNET_SYSERR !=
224 GNUNET_CONFIGURATION_parse (sensorcfg, sensor_path_src));
225 GNUNET_CONFIGURATION_set_value_string (sensorcfg, sensor_name,
227 GNUNET_i2s_full (&cp_peer->peer_id));
228 GNUNET_assert (GNUNET_OK ==
229 GNUNET_DISK_directory_create_for_file (sensor_path_dest));
230 GNUNET_CONFIGURATION_write (sensorcfg, sensor_path_dest);
231 GNUNET_CONFIGURATION_destroy (sensorcfg);
236 * Function called by PEERSTORE for each matching record.
239 * @param record peerstore record information
240 * @param emsg error message, or NULL if no errors
241 * @return #GNUNET_YES to continue iterating, #GNUNET_NO to stop
244 peerstore_watch_cb (void *cls,
245 const struct GNUNET_PEERSTORE_Record *record,
248 struct TestPeer *peer = cls;
249 struct GNUNET_SENSOR_DashboardAnomalyEntry *anomaly;
251 GNUNET_assert (NULL != record);
252 GNUNET_assert (record->value_size ==
253 sizeof (struct GNUNET_SENSOR_DashboardAnomalyEntry));
254 anomaly = record->value;
256 GNUNET_CRYPTO_cmp_peer_identity (&peer->peer_id,
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Peerstore watch got an anomaly report from peer `%s':\n"
260 "Anomalous: %d\n" "Anomalous neigbors: %f.\n",
261 GNUNET_i2s (&peer->peer_id), anomaly->anomalous,
262 anomaly->anomalous_neighbors);
263 if (1 == anomaly->anomalous_neighbors)
265 if (reported_peers == NUM_PEERS)
268 GNUNET_SCHEDULER_cancel (shutdown_task);
269 shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
276 * Task that pushes fake anomalies to running peers
279 force_anomaly_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
281 struct TestPeer *peer = cls;
283 peer->delay_task = NULL;
284 GNUNET_SENSOR_force_anomaly (peer->sensor, (char *) sensor_name, GNUNET_YES,
290 * Callback to be called when sensor service connect operation is completed
292 * @param cls the callback closure from functions generating an operation
293 * @param op the operation that has been finished
294 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
295 * @param emsg error message in case the operation has failed; will be NULL if
296 * operation has executed successfully.
299 sensor_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
300 void *ca_result, const char *emsg)
302 struct TestPeer *peer = cls;
303 struct GNUNET_SENSOR_Handle *sensor = ca_result;
305 peer->sensor = sensor;
307 GNUNET_SCHEDULER_add_delayed (ANOMALY_DELAY, &force_anomaly_task, peer);
312 * Adapter function called to establish a connection to sensor service.
315 * @param cfg configuration of the peer to connect to; will be available until
316 * GNUNET_TESTBED_operation_done() is called on the operation returned
317 * from GNUNET_TESTBED_service_connect()
318 * @return service handle to return in 'op_result', NULL on error
321 sensor_connect_adapter (void *cls,
322 const struct GNUNET_CONFIGURATION_Handle *cfg)
324 struct GNUNET_SENSOR_Handle *sensor;
326 sensor = GNUNET_SENSOR_connect (cfg);
332 * Adapter function called to destroy a connection to sensor service.
335 * @param op_result service handle returned from the connect adapter
338 sensor_disconnect_adapter (void *cls, void *op_result)
340 struct GNUNET_SENSOR_Handle *sensor = op_result;
342 GNUNET_SENSOR_disconnect (sensor);
347 * Callback to be called when sensor service is started
349 * @param cls the callback closure from functions generating an operation
350 * @param op the operation that has been finished
351 * @param emsg error message in case the operation has failed; will be NULL if
352 * operation has executed successfully.
355 sensor_service_started (void *cls, struct GNUNET_TESTBED_Operation *op,
358 struct TestPeer *peer = cls;
362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
366 GNUNET_TESTBED_service_connect (NULL, peer->testbed_peer, "sensor",
367 &sensor_connect_cb, peer,
368 &sensor_connect_adapter,
369 &sensor_disconnect_adapter, NULL);
370 GNUNET_TESTBED_operation_done (op);
375 * Callback to be called when peerstore service connect operation is completed
377 * @param cls the callback closure from functions generating an operation
378 * @param op the operation that has been finished
379 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
380 * @param emsg error message in case the operation has failed; will be NULL if
381 * operation has executed successfully.
384 peerstore_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
385 void *ca_result, const char *emsg)
387 struct TestPeer *peer;
392 GNUNET_PEERSTORE_watch (peerstore, "sensordashboard-anomalies",
393 &peer->peer_id, sensor_name, &peerstore_watch_cb,
395 /* Start sensor service */
396 GNUNET_TESTBED_peer_manage_service (NULL, peer->testbed_peer, "sensor",
397 &sensor_service_started, peer, 1);
404 * Adapter function called to establish a connection to peerstore service.
407 * @param cfg configuration of the peer to connect to; will be available until
408 * GNUNET_TESTBED_operation_done() is called on the operation returned
409 * from GNUNET_TESTBED_service_connect()
410 * @return service handle to return in 'op_result', NULL on error
413 peerstore_connect_adapter (void *cls,
414 const struct GNUNET_CONFIGURATION_Handle *cfg)
416 peerstore = GNUNET_PEERSTORE_connect (cfg);
417 GNUNET_assert (NULL != peerstore);
423 * Adapter function called to destroy a connection to peerstore service.
426 * @param op_result service handle returned from the connect adapter
429 peerstore_disconnect_adapter (void *cls, void *op_result)
431 GNUNET_PEERSTORE_disconnect (peerstore, GNUNET_NO);
438 * Callback to be called when dashboard service is started
440 * @param cls the callback closure from functions generating an operation
441 * @param op the operation that has been finished
442 * @param emsg error message in case the operation has failed; will be NULL if
443 * operation has executed successfully.
446 dashboard_started (void *cls, struct GNUNET_TESTBED_Operation *op,
451 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ERROR: %s.\n", emsg);
454 GNUNET_TESTBED_operation_done (op);
455 /* Connect to peerstore service on first peer */
457 GNUNET_TESTBED_service_connect (NULL, peer_head->testbed_peer,
458 "peerstore", &peerstore_connect_cb, NULL,
459 &peerstore_connect_adapter,
460 &peerstore_disconnect_adapter, NULL);
465 * Callback to be called when the requested peer information is available
467 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
468 * @param op the operation this callback corresponds to
469 * @param pinfo the result; will be NULL if the operation has failed
470 * @param emsg error message if the operation has failed; will be NULL if the
471 * operation is successfull
474 peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
475 const struct GNUNET_TESTBED_PeerInformation *pinfo,
478 struct GNUNET_TESTBED_Peer *testbed_peer = cb_cls;
479 struct TestPeer *peer;
481 peer = GNUNET_new (struct TestPeer);
483 peer->testbed_peer = testbed_peer;
484 peer->delay_task = NULL;
485 GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id);
486 if (NULL == peer_head) /* First peer (collection point) */
488 /* Rewrite sensor with collection point peer id */
489 write_new_sensor_dir (peer);
491 GNUNET_CONTAINER_DLL_insert_tail (peer_head, peer_tail, peer);
493 if (NUM_PEERS == started_peers)
495 /* Start dashboard service on first peer */
496 GNUNET_TESTBED_peer_manage_service (NULL, peer_head->testbed_peer,
497 "sensordashboard", &dashboard_started,
500 GNUNET_TESTBED_operation_done (op);
505 * Signature of a main function for a testcase.
508 * @param h the run handle
509 * @param num_peers number of peers in 'peers'
510 * @param peers handle to peers run in the testbed. NULL upon timeout (see
511 * GNUNET_TESTBED_test_run()).
512 * @param links_succeeded the number of overlay link connection attempts that
514 * @param links_failed the number of overlay link connection attempts that
516 * @see GNUNET_TESTBED_test_run()
519 test_master (void *cls, struct GNUNET_TESTBED_RunHandle *h,
520 unsigned int num_peers, struct GNUNET_TESTBED_Peer **peers,
521 unsigned int links_succeeded, unsigned int links_failed)
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "%d peers started. %d links succeeded. %d links failed.\n",
527 num_peers, links_succeeded, links_failed);
528 GNUNET_assert (NUM_PEERS == num_peers);
529 GNUNET_assert (0 == links_failed);
530 /* Schedule test timeout */
532 GNUNET_SCHEDULER_add_delayed (TEST_TIMEOUT, &do_shutdown, NULL);
533 /* Collect peer information */
534 for (i = 0; i < num_peers; i++)
536 GNUNET_TESTBED_peer_get_information (peers[i],
537 GNUNET_TESTBED_PIT_CONFIGURATION,
538 &peer_info_cb, peers[i]);
544 main (int argc, char *argv[])
546 GNUNET_log_setup (testname, "INFO", NULL);
548 GNUNET_TESTBED_test_run (testname, cfg_filename, NUM_PEERS, 0, NULL, NULL,