test_testbed_api_3peers_3controllers.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2008--2013 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file testbed/test_testbed_api_3peers_3controllers.c
  18. * @brief testcases for the testbed api: 3 peers are configured, started and
  19. * connected together. Each peer resides on its own controller.
  20. * @author Sree Harsha Totakura
  21. */
  22. /**
  23. * The testing architecture is:
  24. * A
  25. * / \
  26. * / \
  27. * B === C
  28. * A is the master controller and B, C are slave controllers. B links to C
  29. * laterally.
  30. * Peers are mapped to controllers in the following relations:
  31. * Peer Controller
  32. * 1 A
  33. * 2 B
  34. * 3 C
  35. *
  36. */
  37. #include "platform.h"
  38. #include "gnunet_util_lib.h"
  39. #include "gnunet_testing_lib.h"
  40. #include "gnunet_testbed_service.h"
  41. /**
  42. * Generic logging shortcut
  43. */
  44. #define LOG(kind,...) \
  45. GNUNET_log (kind, __VA_ARGS__)
  46. /**
  47. * Relative time seconds shorthand
  48. */
  49. #define TIME_REL_SECS(sec) \
  50. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
  51. /**
  52. * Peer context
  53. */
  54. struct PeerContext
  55. {
  56. /**
  57. * The peer handle
  58. */
  59. struct GNUNET_TESTBED_Peer *peer;
  60. /**
  61. * Operations involving this peer
  62. */
  63. struct GNUNET_TESTBED_Operation *operation;
  64. /**
  65. * set to GNUNET_YES when peer is started
  66. */
  67. int is_running;
  68. };
  69. /**
  70. * Our localhost
  71. */
  72. static struct GNUNET_TESTBED_Host *host;
  73. /**
  74. * The controller process of one controller
  75. */
  76. static struct GNUNET_TESTBED_ControllerProc *cp1;
  77. /**
  78. * A neighbouring host
  79. */
  80. static struct GNUNET_TESTBED_Host *neighbour1;
  81. /**
  82. * Another neighbouring host
  83. */
  84. static struct GNUNET_TESTBED_Host *neighbour2;
  85. /**
  86. * Handle for neighbour registration
  87. */
  88. static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
  89. /**
  90. * The controller handle of one controller
  91. */
  92. static struct GNUNET_TESTBED_Controller *controller1;
  93. /**
  94. * peer 1
  95. */
  96. static struct PeerContext peer1;
  97. /**
  98. * peer2
  99. */
  100. static struct PeerContext peer2;
  101. /**
  102. * peer3
  103. */
  104. static struct PeerContext peer3;
  105. /**
  106. * Handle to starting configuration
  107. */
  108. static struct GNUNET_CONFIGURATION_Handle *cfg;
  109. /**
  110. * Handle to slave controller C's configuration, used to establish lateral link from
  111. * master controller
  112. */
  113. static struct GNUNET_CONFIGURATION_Handle *cfg2;
  114. /**
  115. * Handle to operations involving both peers
  116. */
  117. static struct GNUNET_TESTBED_Operation *common_operation;
  118. /**
  119. * The handle for whether a host is habitable or not
  120. */
  121. struct GNUNET_TESTBED_HostHabitableCheckHandle *hc_handle;
  122. /**
  123. * Abort task identifier
  124. */
  125. static struct GNUNET_SCHEDULER_Task * abort_task;
  126. /**
  127. * Delayed connect job identifier
  128. */
  129. static struct GNUNET_SCHEDULER_Task * delayed_connect_task;
  130. /**
  131. * Different stages in testing
  132. */
  133. enum Stage
  134. {
  135. /**
  136. * Initial stage
  137. */
  138. INIT,
  139. /**
  140. * Controller 1 has started
  141. */
  142. CONTROLLER1_UP,
  143. /**
  144. * peer1 is created
  145. */
  146. PEER1_CREATED,
  147. /**
  148. * peer1 is started
  149. */
  150. PEER1_STARTED,
  151. /**
  152. * Controller 2 has started
  153. */
  154. CONTROLLER2_UP,
  155. /**
  156. * peer2 is created
  157. */
  158. PEER2_CREATED,
  159. /**
  160. * peer2 is started
  161. */
  162. PEER2_STARTED,
  163. /**
  164. * Controller 3 has started
  165. */
  166. CONTROLLER3_UP,
  167. /**
  168. * Peer3 is created
  169. */
  170. PEER3_CREATED,
  171. /**
  172. * Peer3 started
  173. */
  174. PEER3_STARTED,
  175. /**
  176. * peer1 and peer2 are connected
  177. */
  178. PEERS_1_2_CONNECTED,
  179. /**
  180. * peer2 and peer3 are connected
  181. */
  182. PEERS_2_3_CONNECTED,
  183. /**
  184. * Peers are connected once again (this should not fail as they are already connected)
  185. */
  186. PEERS_CONNECTED_2,
  187. /**
  188. * peers are stopped
  189. */
  190. PEERS_STOPPED,
  191. /**
  192. * Final success stage
  193. */
  194. SUCCESS,
  195. /**
  196. * Optional stage for marking test to be skipped
  197. */
  198. SKIP
  199. };
  200. /**
  201. * The testing result
  202. */
  203. static enum Stage result;
  204. /**
  205. * Shutdown nicely
  206. *
  207. * @param cls NULL
  208. */
  209. static void
  210. do_shutdown (void *cls)
  211. {
  212. if (NULL != abort_task)
  213. GNUNET_SCHEDULER_cancel (abort_task);
  214. if (NULL != hc_handle)
  215. GNUNET_TESTBED_is_host_habitable_cancel (hc_handle);
  216. GNUNET_assert (NULL == delayed_connect_task);
  217. if (NULL != common_operation)
  218. GNUNET_TESTBED_operation_done (common_operation);
  219. if (NULL != reg_handle)
  220. GNUNET_TESTBED_cancel_registration (reg_handle);
  221. if (NULL != controller1)
  222. GNUNET_TESTBED_controller_disconnect (controller1);
  223. GNUNET_CONFIGURATION_destroy (cfg);
  224. if (NULL != cfg2)
  225. GNUNET_CONFIGURATION_destroy (cfg2);
  226. if (NULL != cp1)
  227. GNUNET_TESTBED_controller_stop (cp1);
  228. if (NULL != host)
  229. GNUNET_TESTBED_host_destroy (host);
  230. if (NULL != neighbour1)
  231. GNUNET_TESTBED_host_destroy (neighbour1);
  232. if (NULL != neighbour2)
  233. GNUNET_TESTBED_host_destroy (neighbour2);
  234. }
  235. /**
  236. * abort task to run on test timed out
  237. *
  238. * @param cls NULL
  239. */
  240. static void
  241. do_abort (void *cls)
  242. {
  243. LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
  244. abort_task = NULL;
  245. if (NULL != delayed_connect_task)
  246. {
  247. GNUNET_SCHEDULER_cancel (delayed_connect_task);
  248. delayed_connect_task = NULL;
  249. }
  250. do_shutdown (cls);
  251. }
  252. static void
  253. abort_test ()
  254. {
  255. if (NULL != abort_task)
  256. GNUNET_SCHEDULER_cancel (abort_task);
  257. abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
  258. }
  259. /**
  260. * Callback to be called when an operation is completed
  261. *
  262. * @param cls the callback closure from functions generating an operation
  263. * @param op the operation that has been finished
  264. * @param emsg error message in case the operation has failed; will be NULL if
  265. * operation has executed successfully.
  266. */
  267. static void
  268. op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg);
  269. /**
  270. * task for delaying a connect
  271. *
  272. * @param cls NULL
  273. */
  274. static void
  275. do_delayed_connect (void *cls)
  276. {
  277. delayed_connect_task = NULL;
  278. if (NULL != common_operation)
  279. {
  280. GNUNET_break (0);
  281. abort_test ();
  282. return;
  283. }
  284. common_operation =
  285. GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer1.peer,
  286. peer2.peer);
  287. }
  288. /**
  289. * Callback to be called when an operation is completed
  290. *
  291. * @param cls the callback closure from functions generating an operation
  292. * @param op the operation that has been finished
  293. * @param emsg error message in case the operation has failed; will be NULL if
  294. * operation has executed successfully.
  295. */
  296. static void
  297. op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op, const char *emsg)
  298. {
  299. if (common_operation != op)
  300. {
  301. GNUNET_break (0);
  302. abort_test ();
  303. return;
  304. }
  305. switch (result)
  306. {
  307. case PEER3_STARTED:
  308. case PEERS_2_3_CONNECTED:
  309. case PEERS_1_2_CONNECTED:
  310. break;
  311. default:
  312. GNUNET_break (0);
  313. abort_test ();
  314. return;
  315. }
  316. if ((NULL != peer1.operation) || (NULL != peer2.operation) ||
  317. (NULL != peer3.operation))
  318. {
  319. GNUNET_break (0);
  320. abort_test ();
  321. return;
  322. }
  323. }
  324. /**
  325. * Functions of this signature are called when a peer has been successfully
  326. * created
  327. *
  328. * @param cls NULL
  329. * @param peer the handle for the created peer; NULL on any error during
  330. * creation
  331. * @param emsg NULL if peer is not NULL; else MAY contain the error description
  332. */
  333. static void
  334. peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
  335. {
  336. switch (result)
  337. {
  338. case CONTROLLER1_UP:
  339. if ((NULL == peer1.operation) || (NULL == peer) || (NULL != peer1.peer))
  340. {
  341. GNUNET_break (0);
  342. abort_test ();
  343. return;
  344. }
  345. peer1.peer = peer;
  346. GNUNET_TESTBED_operation_done (peer1.operation);
  347. result = PEER1_CREATED;
  348. peer1.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
  349. break;
  350. case CONTROLLER2_UP:
  351. if ((NULL == peer2.operation) || (NULL == peer) || (NULL != peer2.peer))
  352. {
  353. GNUNET_break (0);
  354. abort_test ();
  355. return;
  356. }
  357. peer2.peer = peer;
  358. GNUNET_TESTBED_operation_done (peer2.operation);
  359. result = PEER2_CREATED;
  360. peer2.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
  361. break;
  362. case CONTROLLER3_UP:
  363. if ((NULL == peer3.operation) || (NULL == peer) || (NULL != peer3.peer))
  364. {
  365. GNUNET_break (0);
  366. abort_test ();
  367. return;
  368. }
  369. peer3.peer = peer;
  370. GNUNET_TESTBED_operation_done (peer3.operation);
  371. result = PEER3_CREATED;
  372. peer3.operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
  373. break;
  374. default:
  375. GNUNET_break (0);
  376. abort_test ();
  377. return;
  378. }
  379. }
  380. /**
  381. * Signature of the event handler function called by the
  382. * respective event controller.
  383. *
  384. * @param cls closure
  385. * @param event information about the event
  386. */
  387. static void
  388. controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
  389. {
  390. switch (event->type)
  391. {
  392. case GNUNET_TESTBED_ET_OPERATION_FINISHED:
  393. if ((NULL != event->op_cls) ||
  394. (NULL != event->details.operation_finished.emsg))
  395. {
  396. GNUNET_break (0);
  397. abort_test ();
  398. return;
  399. }
  400. switch (result)
  401. {
  402. case PEERS_STOPPED:
  403. if (NULL != event->details.operation_finished.generic)
  404. {
  405. GNUNET_break (0);
  406. abort_test ();
  407. return;
  408. }
  409. if (event->op == peer1.operation)
  410. {
  411. GNUNET_TESTBED_operation_done (peer1.operation);
  412. peer1.operation = NULL;
  413. peer1.peer = NULL;
  414. }
  415. else if (event->op == peer2.operation)
  416. {
  417. GNUNET_TESTBED_operation_done (peer2.operation);
  418. peer2.operation = NULL;
  419. peer2.peer = NULL;
  420. }
  421. else if (event->op == peer3.operation)
  422. {
  423. GNUNET_TESTBED_operation_done (peer3.operation);
  424. peer3.operation = NULL;
  425. peer3.peer = NULL;
  426. }
  427. else
  428. {
  429. GNUNET_break (0);
  430. abort_test ();
  431. return;
  432. }
  433. if ((NULL == peer1.peer) && (NULL == peer2.peer) && (NULL == peer3.peer))
  434. {
  435. result = SUCCESS;
  436. GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  437. }
  438. break;
  439. case PEER1_STARTED:
  440. if ((NULL != event->details.operation_finished.generic) ||
  441. (NULL == common_operation))
  442. {
  443. GNUNET_break (0);
  444. abort_test ();
  445. return;
  446. }
  447. GNUNET_TESTBED_operation_done (common_operation);
  448. common_operation = NULL;
  449. result = CONTROLLER2_UP;
  450. peer2.operation =
  451. GNUNET_TESTBED_peer_create (controller1, neighbour1, cfg,
  452. &peer_create_cb, NULL);
  453. if (NULL == peer2.operation)
  454. {
  455. GNUNET_break (0);
  456. abort_test ();
  457. return;
  458. }
  459. break;
  460. case PEER2_STARTED:
  461. if ((NULL != event->details.operation_finished.generic) ||
  462. (NULL == common_operation))
  463. {
  464. GNUNET_break (0);
  465. abort_test ();
  466. return;
  467. }
  468. GNUNET_TESTBED_operation_done (common_operation);
  469. common_operation = NULL;
  470. result = CONTROLLER3_UP;
  471. peer3.operation =
  472. GNUNET_TESTBED_peer_create (controller1, neighbour2, cfg,
  473. &peer_create_cb, NULL);
  474. if (NULL == peer3.operation)
  475. {
  476. GNUNET_break (0);
  477. abort_test ();
  478. return;
  479. }
  480. break;
  481. default:
  482. GNUNET_break (0);
  483. abort_test ();
  484. return;
  485. }
  486. break;
  487. case GNUNET_TESTBED_ET_PEER_START:
  488. switch (result)
  489. {
  490. case PEER1_CREATED:
  491. if (event->details.peer_start.host != host)
  492. {
  493. GNUNET_break (0);
  494. abort_test ();
  495. return;
  496. }
  497. peer1.is_running = GNUNET_YES;
  498. GNUNET_TESTBED_operation_done (peer1.operation);
  499. peer1.operation = NULL;
  500. result = PEER1_STARTED;
  501. common_operation =
  502. GNUNET_TESTBED_controller_link (NULL, controller1, neighbour1, NULL,
  503. GNUNET_YES);
  504. break;
  505. case PEER2_CREATED:
  506. if (event->details.peer_start.host != neighbour1)
  507. {
  508. GNUNET_break (0);
  509. abort_test ();
  510. return;
  511. }
  512. peer2.is_running = GNUNET_YES;
  513. GNUNET_TESTBED_operation_done (peer2.operation);
  514. peer2.operation = NULL;
  515. result = PEER2_STARTED;
  516. if (NULL != common_operation)
  517. {
  518. GNUNET_break (0);
  519. abort_test ();
  520. return;
  521. }
  522. common_operation =
  523. GNUNET_TESTBED_controller_link (NULL, controller1, neighbour2, NULL,
  524. GNUNET_YES);
  525. if (NULL == common_operation)
  526. {
  527. GNUNET_break (0);
  528. abort_test ();
  529. return;
  530. }
  531. break;
  532. case PEER3_CREATED:
  533. if (event->details.peer_start.host != neighbour2)
  534. {
  535. GNUNET_break (0);
  536. abort_test ();
  537. return;
  538. }
  539. peer3.is_running = GNUNET_YES;
  540. GNUNET_TESTBED_operation_done (peer3.operation);
  541. peer3.operation = NULL;
  542. result = PEER3_STARTED;
  543. common_operation =
  544. GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
  545. peer1.peer);
  546. break;
  547. default:
  548. GNUNET_break (0);
  549. abort_test ();
  550. return;
  551. }
  552. break;
  553. case GNUNET_TESTBED_ET_PEER_STOP:
  554. if (PEERS_CONNECTED_2 != result)
  555. {
  556. GNUNET_break (0);
  557. abort_test ();
  558. return;
  559. }
  560. if (event->details.peer_stop.peer == peer1.peer)
  561. {
  562. peer1.is_running = GNUNET_NO;
  563. GNUNET_TESTBED_operation_done (peer1.operation);
  564. }
  565. else if (event->details.peer_stop.peer == peer2.peer)
  566. {
  567. peer2.is_running = GNUNET_NO;
  568. GNUNET_TESTBED_operation_done (peer2.operation);
  569. }
  570. else if (event->details.peer_stop.peer == peer3.peer)
  571. {
  572. peer3.is_running = GNUNET_NO;
  573. GNUNET_TESTBED_operation_done (peer3.operation);
  574. }
  575. else
  576. {
  577. GNUNET_break (0);
  578. abort_test ();
  579. return;
  580. }
  581. if ((GNUNET_NO == peer1.is_running) && (GNUNET_NO == peer2.is_running) &&
  582. (GNUNET_NO == peer3.is_running))
  583. {
  584. result = PEERS_STOPPED;
  585. peer1.operation = GNUNET_TESTBED_peer_destroy (peer1.peer);
  586. peer2.operation = GNUNET_TESTBED_peer_destroy (peer2.peer);
  587. peer3.operation = GNUNET_TESTBED_peer_destroy (peer3.peer);
  588. }
  589. break;
  590. case GNUNET_TESTBED_ET_CONNECT:
  591. if ((NULL != peer1.operation) || (NULL != peer2.operation) ||
  592. (NULL != peer3.operation) || (NULL == common_operation))
  593. {
  594. GNUNET_break (0);
  595. abort_test ();
  596. return;
  597. }
  598. switch (result)
  599. {
  600. case PEER3_STARTED:
  601. if ((event->details.peer_connect.peer1 != peer2.peer) ||
  602. (event->details.peer_connect.peer2 != peer1.peer))
  603. {
  604. GNUNET_break (0);
  605. abort_test ();
  606. return;
  607. }
  608. GNUNET_TESTBED_operation_done (common_operation);
  609. common_operation = NULL;
  610. result = PEERS_1_2_CONNECTED;
  611. LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected\n");
  612. common_operation =
  613. GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peer2.peer,
  614. peer3.peer);
  615. break;
  616. case PEERS_1_2_CONNECTED:
  617. if ((event->details.peer_connect.peer1 != peer2.peer) ||
  618. (event->details.peer_connect.peer2 != peer3.peer))
  619. {
  620. GNUNET_break (0);
  621. abort_test ();
  622. return;
  623. }
  624. GNUNET_TESTBED_operation_done (common_operation);
  625. common_operation = NULL;
  626. result = PEERS_2_3_CONNECTED;
  627. delayed_connect_task =
  628. GNUNET_SCHEDULER_add_delayed (TIME_REL_SECS (3), &do_delayed_connect,
  629. NULL);
  630. break;
  631. case PEERS_2_3_CONNECTED:
  632. if ((event->details.peer_connect.peer1 != peer1.peer) ||
  633. (event->details.peer_connect.peer2 != peer2.peer))
  634. {
  635. GNUNET_break (0);
  636. abort_test ();
  637. return;
  638. }
  639. GNUNET_TESTBED_operation_done (common_operation);
  640. common_operation = NULL;
  641. result = PEERS_CONNECTED_2;
  642. LOG (GNUNET_ERROR_TYPE_DEBUG, "Peers connected again\n");
  643. peer1.operation = GNUNET_TESTBED_peer_stop (NULL, peer1.peer, NULL, NULL);
  644. peer2.operation = GNUNET_TESTBED_peer_stop (NULL, peer2.peer, NULL, NULL);
  645. peer3.operation = GNUNET_TESTBED_peer_stop (NULL, peer3.peer, NULL, NULL);
  646. break;
  647. default:
  648. GNUNET_break (0);
  649. abort_test ();
  650. return;
  651. }
  652. break;
  653. default:
  654. GNUNET_break (0);
  655. abort_test ();
  656. return;
  657. }
  658. }
  659. /**
  660. * Callback which will be called to after a host registration succeeded or failed
  661. *
  662. * @param cls the host which has been registered
  663. * @param emsg the error message; NULL if host registration is successful
  664. */
  665. static void
  666. registration_comp (void *cls, const char *emsg)
  667. {
  668. reg_handle = NULL;
  669. if (cls == neighbour1)
  670. {
  671. neighbour2 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, cfg, 0);
  672. if (NULL == neighbour2)
  673. {
  674. GNUNET_break (0);
  675. abort_test ();
  676. return;
  677. }
  678. reg_handle =
  679. GNUNET_TESTBED_register_host (controller1, neighbour2,
  680. &registration_comp, neighbour2);
  681. if (NULL == reg_handle)
  682. {
  683. GNUNET_break (0);
  684. abort_test ();
  685. return;
  686. }
  687. return;
  688. }
  689. if (cls != neighbour2)
  690. {
  691. GNUNET_break (0);
  692. abort_test ();
  693. return;
  694. }
  695. peer1.operation =
  696. GNUNET_TESTBED_peer_create (controller1, host, cfg, &peer_create_cb,
  697. &peer1);
  698. if (NULL == peer1.operation)
  699. {
  700. GNUNET_break (0);
  701. abort_test ();
  702. return;
  703. }
  704. }
  705. /**
  706. * Callback to signal successfull startup of the controller process
  707. *
  708. * @param cls the closure from GNUNET_TESTBED_controller_start()
  709. * @param cfg the configuration with which the controller has been started;
  710. * NULL if status is not GNUNET_OK
  711. * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
  712. * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
  713. */
  714. static void
  715. status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
  716. int status)
  717. {
  718. uint64_t event_mask;
  719. if (GNUNET_OK != status)
  720. {
  721. GNUNET_break (0);
  722. cp1 = NULL;
  723. abort_test ();
  724. return;
  725. }
  726. event_mask = 0;
  727. event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
  728. event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
  729. event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
  730. event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
  731. switch (result)
  732. {
  733. case INIT:
  734. controller1 =
  735. GNUNET_TESTBED_controller_connect (host, event_mask,
  736. &controller_cb, NULL);
  737. if (NULL == controller1)
  738. {
  739. GNUNET_break (0);
  740. abort_test ();
  741. return;
  742. }
  743. result = CONTROLLER1_UP;
  744. neighbour1 = GNUNET_TESTBED_host_create ("127.0.0.1", NULL, cfg, 0);
  745. if (NULL == neighbour1)
  746. {
  747. GNUNET_break (0);
  748. abort_test ();
  749. return;
  750. }
  751. reg_handle =
  752. GNUNET_TESTBED_register_host (controller1, neighbour1,
  753. &registration_comp, neighbour1);
  754. if (NULL == reg_handle)
  755. {
  756. GNUNET_break (0);
  757. abort_test ();
  758. return;
  759. }
  760. break;
  761. default:
  762. GNUNET_break (0);
  763. abort_test ();
  764. return;
  765. }
  766. }
  767. /**
  768. * Callbacks of this type are called by GNUNET_TESTBED_is_host_habitable to
  769. * inform whether the given host is habitable or not. The Handle returned by
  770. * GNUNET_TESTBED_is_host_habitable() is invalid after this callback is called
  771. *
  772. * @param cls NULL
  773. * @param host the host whose status is being reported; will be NULL if the host
  774. * given to GNUNET_TESTBED_is_host_habitable() is NULL
  775. * @param status #GNUNET_YES if it is habitable; #GNUNET_NO if not
  776. */
  777. static void
  778. host_habitable_cb (void *cls,
  779. const struct GNUNET_TESTBED_Host *_host,
  780. int status)
  781. {
  782. hc_handle = NULL;
  783. if (GNUNET_NO == status)
  784. {
  785. (void) PRINTF ("%s",
  786. "Unable to run the test as this system is not configured "
  787. "to use password less SSH logins to localhost.\n"
  788. "Skipping test\n");
  789. GNUNET_SCHEDULER_cancel (abort_task);
  790. abort_task = NULL;
  791. GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  792. result = SKIP;
  793. return;
  794. }
  795. cp1 =
  796. GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb, NULL);
  797. }
  798. /**
  799. * Main run function.
  800. *
  801. * @param cls NULL
  802. * @param args arguments passed to GNUNET_PROGRAM_run
  803. * @param cfgfile the path to configuration file
  804. * @param cfg the configuration file handle
  805. */
  806. static void
  807. run (void *cls, char *const *args, const char *cfgfile,
  808. const struct GNUNET_CONFIGURATION_Handle *config)
  809. {
  810. cfg = GNUNET_CONFIGURATION_dup (config);
  811. host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
  812. if (NULL == host)
  813. {
  814. GNUNET_break (0);
  815. abort_test ();
  816. return;
  817. }
  818. if (NULL ==
  819. (hc_handle =
  820. GNUNET_TESTBED_is_host_habitable (host, config, &host_habitable_cb,
  821. NULL)))
  822. {
  823. GNUNET_TESTBED_host_destroy (host);
  824. host = NULL;
  825. (void) PRINTF ("%s",
  826. "Unable to run the test as this system is not configured "
  827. "to use password less SSH logins to localhost.\n"
  828. "Skipping test\n");
  829. result = SKIP;
  830. return;
  831. }
  832. abort_task =
  833. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  834. (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
  835. NULL);
  836. }
  837. /**
  838. * Main function
  839. */
  840. int
  841. main (int argc, char **argv)
  842. {
  843. char *const argv2[] = { "test_testbed_api_3peers_3controllers",
  844. "-c", "test_testbed_api.conf",
  845. NULL
  846. };
  847. struct GNUNET_GETOPT_CommandLineOption options[] = {
  848. GNUNET_GETOPT_OPTION_END
  849. };
  850. int ret;
  851. result = INIT;
  852. ret =
  853. GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
  854. "test_testbed_api_3peers_3controllers", "nohelp",
  855. options, &run, NULL);
  856. if (GNUNET_OK != ret)
  857. return 1;
  858. switch (result)
  859. {
  860. case SUCCESS:
  861. return 0;
  862. case SKIP:
  863. return 77; /* Mark test as skipped */
  864. default:
  865. return 1;
  866. }
  867. }
  868. /* end of test_testbed_api_3peers_3controllers.c */