From 41c3bd8aabf10fd3e893319bc9c622c3d268cad9 Mon Sep 17 00:00:00 2001 From: Omar Tarabai Date: Mon, 18 Aug 2014 11:57:41 +0000 Subject: sensor: update to test case + fix --- src/sensor/Makefile.am | 3 +- src/sensor/gnunet-service-sensor_reporting.c | 58 ++++++++------ src/sensor/test_gnunet-service-sensor_reporting.c | 88 ++++++++++++++++++++-- .../test_gnunet-service-sensor_reporting.conf | 10 ++- 4 files changed, 126 insertions(+), 33 deletions(-) diff --git a/src/sensor/Makefile.am b/src/sensor/Makefile.am index 4c19fc7f4..305b91aad 100644 --- a/src/sensor/Makefile.am +++ b/src/sensor/Makefile.am @@ -83,7 +83,8 @@ libgnunet_plugin_sensor_model_gaussian_la_DEPENDENCIES = \ libgnunetsensor.la check_PROGRAMS = \ - test_sensor_api + test_sensor_api \ + test_gnunet-service-sensor_reporting if ENABLE_TEST_RUN AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH; diff --git a/src/sensor/gnunet-service-sensor_reporting.c b/src/sensor/gnunet-service-sensor_reporting.c index a2015f48e..6267e9f2a 100644 --- a/src/sensor/gnunet-service-sensor_reporting.c +++ b/src/sensor/gnunet-service-sensor_reporting.c @@ -480,7 +480,7 @@ get_cadet_peer (struct GNUNET_PeerIdentity pid) cadetp->channel = GNUNET_CADET_channel_create (cadet, cadetp, &pid, GNUNET_APPLICATION_TYPE_SENSORDASHBOARD, - GNUNET_CADET_OPTION_DEFAULT); + GNUNET_CADET_OPTION_RELIABLE); cadetp->mq = GNUNET_CADET_mq_create (cadetp->channel); GNUNET_CONTAINER_DLL_insert (cadetp_head, cadetp_tail, cadetp); return cadetp; @@ -585,9 +585,10 @@ handle_anomaly_report (void *cls, const struct GNUNET_PeerIdentity *other, { struct GNUNET_SENSOR_AnomalyReportMessage *arm; struct GNUNET_SENSOR_SensorInfo *sensor; - struct AnomalyInfo *ai; + struct AnomalyInfo *my_anomaly_info; struct CadetPeer *cadetp; - int peer_in_list; + int peer_anomalous; + int peer_in_anomalous_list; arm = (struct GNUNET_SENSOR_AnomalyReportMessage *) message; sensor = GNUNET_CONTAINER_multihashmap_get (sensors, &arm->sensorname_hash); @@ -600,34 +601,39 @@ handle_anomaly_report (void *cls, const struct GNUNET_PeerIdentity *other, GNUNET_i2s (other)); return GNUNET_OK; } - ai = get_anomaly_info_by_sensor (sensor); - GNUNET_assert (NULL != ai); - peer_in_list = - GNUNET_CONTAINER_multipeermap_contains (ai->anomalous_neighbors, other); - if (GNUNET_YES == ai->anomalous) + my_anomaly_info = get_anomaly_info_by_sensor (sensor); + GNUNET_assert (NULL != my_anomaly_info); + peer_in_anomalous_list = + GNUNET_CONTAINER_multipeermap_contains + (my_anomaly_info->anomalous_neighbors, other); + peer_anomalous = ntohs (arm->anomalous); + if (GNUNET_YES == peer_anomalous) { - if (GNUNET_YES == peer_in_list) + if (GNUNET_YES == peer_in_anomalous_list) /* repeated positive report */ GNUNET_break_op (0); else - GNUNET_CONTAINER_multipeermap_put (ai->anomalous_neighbors, other, NULL, + GNUNET_CONTAINER_multipeermap_put (my_anomaly_info->anomalous_neighbors, + other, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); } else { - if (GNUNET_NO == peer_in_list) + if (GNUNET_NO == peer_in_anomalous_list) /* repeated negative report */ GNUNET_break_op (0); else - GNUNET_CONTAINER_multipeermap_remove_all (ai->anomalous_neighbors, other); + GNUNET_CONTAINER_multipeermap_remove_all + (my_anomaly_info->anomalous_neighbors, other); } - /* Send anomaly update to collection point */ - if (NULL != ai->sensor->collection_point && - GNUNET_YES == ai->sensor->report_anomalies) + /* Send anomaly update to collection point only if I have the same anomaly */ + if (GNUNET_YES == my_anomaly_info->anomalous && + NULL != sensor->collection_point && + GNUNET_YES == sensor->report_anomalies) { LOG (GNUNET_ERROR_TYPE_DEBUG, - "Sending anomaly report to collection point `%s'.\n", - GNUNET_i2s (ai->sensor->collection_point)); - cadetp = get_cadet_peer (*ai->sensor->collection_point); - send_anomaly_report (cadetp->mq, ai, GNUNET_NO); + "Neighbor update triggered sending anomaly report to collection point `%s'.\n", + GNUNET_i2s (sensor->collection_point)); + cadetp = get_cadet_peer (*sensor->collection_point); + send_anomaly_report (cadetp->mq, my_anomaly_info, GNUNET_NO); } return GNUNET_OK; } @@ -687,6 +693,8 @@ core_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity *peer) if (0 == GNUNET_CRYPTO_cmp_peer_identity (&mypeerid, peer)) return; + LOG (GNUNET_ERROR_TYPE_DEBUG, "Core peer `%s' disconnected.\n", + GNUNET_i2s (peer)); neighborhood--; corep = corep_head; while (NULL != corep) @@ -716,6 +724,8 @@ core_connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer) if (0 == GNUNET_CRYPTO_cmp_peer_identity (&mypeerid, peer)) return; + LOG (GNUNET_ERROR_TYPE_DEBUG, "Connected to core peer `%s'.\n", + GNUNET_i2s (peer)); neighborhood++; corep = GNUNET_new (struct CorePeer); corep->peer_id = (struct GNUNET_PeerIdentity *) peer; @@ -725,11 +735,13 @@ core_connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer) ai = ai_head; while (NULL != ai) { - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Updating newly connected neighbor `%s' with anomalous sensor.\n", - GNUNET_i2s (peer)); if (GNUNET_YES == ai->anomalous) + { + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Updating newly connected neighbor `%s' with anomalous sensor.\n", + GNUNET_i2s (peer)); send_anomaly_report (corep->mq, ai, GNUNET_YES); + } ai = ai->next; } } @@ -839,7 +851,7 @@ SENSOR_reporting_anomaly_update (struct GNUNET_SENSOR_SensorInfo *sensor, GNUNET_YES == ai->sensor->report_anomalies) { LOG (GNUNET_ERROR_TYPE_DEBUG, - "Sending anomaly report to collection point `%s'.\n", + "Local anomaly update triggered sending anomaly report to collection point `%s'.\n", GNUNET_i2s (ai->sensor->collection_point)); cadetp = get_cadet_peer (*ai->sensor->collection_point); send_anomaly_report (cadetp->mq, ai, GNUNET_NO); diff --git a/src/sensor/test_gnunet-service-sensor_reporting.c b/src/sensor/test_gnunet-service-sensor_reporting.c index 969ab16ec..3905e8876 100644 --- a/src/sensor/test_gnunet-service-sensor_reporting.c +++ b/src/sensor/test_gnunet-service-sensor_reporting.c @@ -39,6 +39,12 @@ */ #define TEST_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 1) +/** + * How long to wait between starting everything and forcing anomalies to give + * the peer enough time to stabilize. + */ +#define ANOMALY_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3) + /** * Information about a test peer */ @@ -75,6 +81,16 @@ struct TestPeer */ struct GNUNET_TESTBED_Operation *sensor_op; + /** + * Sensor service handle + */ + struct GNUNET_SENSOR_Handle *sensor; + + /** + * GNUNET scheduler task that forces the anomaly after a stabilization delay + */ + GNUNET_SCHEDULER_TaskIdentifier delay_task; + }; /** @@ -118,15 +134,20 @@ static struct TestPeer *peer_tail; */ static int started_peers = 0; +/** + * Number of peers reported anomalies with full list of anomalous neighbors + */ +static int reported_peers = 0; + /** * TESTBED operation connecting us to peerstore service */ -struct GNUNET_TESTBED_Operation *peerstore_op; +static struct GNUNET_TESTBED_Operation *peerstore_op; /** * Handle to the peerstore service */ -struct GNUNET_PEERSTORE_Handle *peerstore; +static struct GNUNET_PEERSTORE_Handle *peerstore; /** * Task used to shutdown / expire the test @@ -139,6 +160,28 @@ static GNUNET_SCHEDULER_TaskIdentifier shutdown_task; static int ok = 1; +static void +destroy_peer (struct TestPeer *peer) +{ + if (GNUNET_SCHEDULER_NO_TASK != peer->delay_task) + { + GNUNET_SCHEDULER_cancel (peer->delay_task); + peer->delay_task = GNUNET_SCHEDULER_NO_TASK; + } + if (NULL != peer->sensor_op) + { + GNUNET_TESTBED_operation_done (peer->sensor_op); + peer->sensor_op = NULL; + } + if (NULL != peer->wc) + { + GNUNET_PEERSTORE_watch_cancel (peer->wc); + peer->wc = NULL; + } + GNUNET_free (peer); +} + + /** * Shutdown task * @@ -148,7 +191,15 @@ static int ok = 1; static void do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { - //TODO: destroy list of peers + struct TestPeer *peer; + + peer = peer_head; + while (NULL != peer) + { + GNUNET_CONTAINER_DLL_remove (peer_head, peer_tail, peer); + destroy_peer (peer); + peer = peer_head; + } if (NULL != peerstore_op) { GNUNET_TESTBED_operation_done (peerstore_op); @@ -162,7 +213,7 @@ do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) * Write new temp sensor directory with a sensor updated with collection point * peer id */ -void +static void write_new_sensor_dir (struct TestPeer *cp_peer) { struct GNUNET_CONFIGURATION_Handle *sensorcfg; @@ -208,11 +259,32 @@ peerstore_watch_cb (void *cls, struct GNUNET_PEERSTORE_Record *record, "Anomalous: %d\n" "Anomalous neigbors: %f.\n", GNUNET_i2s (&peer->peer_id), anomaly->anomalous, anomaly->anomalous_neighbors); - //TODO + if (1 == anomaly->anomalous_neighbors) + reported_peers++; + if (reported_peers == NUM_PEERS) + { + ok = 0; + GNUNET_SCHEDULER_cancel (shutdown_task); + shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); + } return GNUNET_YES; } +/** + * Task that pushes fake anomalies to running peers + */ +static void +force_anomaly_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + struct TestPeer *peer = cls; + + peer->delay_task = GNUNET_SCHEDULER_NO_TASK; + GNUNET_SENSOR_force_anomaly (peer->sensor, (char *) sensor_name, GNUNET_YES, + NULL, NULL); +} + + /** * Callback to be called when sensor service connect operation is completed * @@ -229,8 +301,9 @@ sensor_connect_cb (void *cls, struct GNUNET_TESTBED_Operation *op, struct TestPeer *peer = cls; struct GNUNET_SENSOR_Handle *sensor = ca_result; - GNUNET_SENSOR_force_anomaly (sensor, (char *) sensor_name, GNUNET_YES, NULL, - NULL); + peer->sensor = sensor; + peer->delay_task = + GNUNET_SCHEDULER_add_delayed (ANOMALY_DELAY, &force_anomaly_task, peer); } @@ -399,6 +472,7 @@ peer_info_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op, peer = GNUNET_new (struct TestPeer); peer->testbed_peer = testbed_peer; + peer->delay_task = GNUNET_SCHEDULER_NO_TASK; GNUNET_CRYPTO_get_peer_identity (pinfo->result.cfg, &peer->peer_id); if (NULL == peer_head) /* First peer (collection point) */ { diff --git a/src/sensor/test_gnunet-service-sensor_reporting.conf b/src/sensor/test_gnunet-service-sensor_reporting.conf index 4dc073856..0d1907c9e 100644 --- a/src/sensor/test_gnunet-service-sensor_reporting.conf +++ b/src/sensor/test_gnunet-service-sensor_reporting.conf @@ -2,7 +2,7 @@ OVERLAY_TOPOLOGY = CLIQUE [arm] -DEFAULTSERVICES = core +DEFAULTSERVICES = topology dht cadet [sensor] SENSOR_DIR = /tmp/test-gnunet-service-sensor-reporting/ @@ -10,4 +10,10 @@ SENSOR_DIR = /tmp/test-gnunet-service-sensor-reporting/ START_MONITORING = NO START_REPORTING = YES START_ANALYSIS = NO -START_UPDATE = NO \ No newline at end of file +START_UPDATE = NO + +[transport] +PLUGINS = unix + +[nat] +USE_LOCALADDR = YES -- cgit v1.2.3