test_gnunet-service-sensor_reporting.c 15 KB

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