test_testbed_api.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. This file is part of GNUnet
  3. (C) 2008--2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file testbed/test_testbed_api.c
  19. * @brief testcases for the testbed api
  20. * @author Sree Harsha Totakura
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_arm_service.h"
  25. #include "gnunet_testing_lib.h"
  26. #include "gnunet_testbed_service.h"
  27. /**
  28. * Generic logging shortcut
  29. */
  30. #define LOG(kind,...) \
  31. GNUNET_log (kind, __VA_ARGS__)
  32. /**
  33. * Relative time seconds shorthand
  34. */
  35. #define TIME_REL_SECS(sec) \
  36. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
  37. /**
  38. * Our localhost
  39. */
  40. static struct GNUNET_TESTBED_Host *host;
  41. /**
  42. * The controller process
  43. */
  44. static struct GNUNET_TESTBED_ControllerProc *cp;
  45. /**
  46. * The controller handle
  47. */
  48. static struct GNUNET_TESTBED_Controller *controller;
  49. /**
  50. * A neighbouring host
  51. */
  52. static struct GNUNET_TESTBED_Host *neighbour;
  53. /**
  54. * Handle for neighbour registration
  55. */
  56. static struct GNUNET_TESTBED_HostRegistrationHandle *reg_handle;
  57. /**
  58. * Handle for a peer
  59. */
  60. static struct GNUNET_TESTBED_Peer *peer;
  61. /**
  62. * Handle to configuration
  63. */
  64. static struct GNUNET_CONFIGURATION_Handle *cfg;
  65. /**
  66. * Handle to operation
  67. */
  68. static struct GNUNET_TESTBED_Operation *operation;
  69. /**
  70. * Handle to peer's ARM service
  71. */
  72. static struct GNUNET_ARM_Handle *arm_handle;
  73. /**
  74. * Abort task identifier
  75. */
  76. static GNUNET_SCHEDULER_TaskIdentifier abort_task;
  77. /**
  78. * The testing result
  79. */
  80. static int result;
  81. /**
  82. * Enumeration of sub testcases
  83. */
  84. enum Test
  85. {
  86. /**
  87. * Test cases which are not covered by the below ones
  88. */
  89. OTHER,
  90. /**
  91. * Test where we get a peer config from controller
  92. */
  93. PEER_GETCONFIG,
  94. /**
  95. * Test where we connect to a service running on the peer
  96. */
  97. PEER_SERVICE_CONNECT,
  98. /**
  99. * Test where we get a peer's identity from controller
  100. */
  101. PEER_DESTROY,
  102. };
  103. /**
  104. * Testing status
  105. */
  106. static enum Test sub_test;
  107. /**
  108. * Shutdown nicely
  109. *
  110. * @param cls NULL
  111. * @param tc the task context
  112. */
  113. static void
  114. do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  115. {
  116. LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down...\n");
  117. if (GNUNET_SCHEDULER_NO_TASK != abort_task)
  118. GNUNET_SCHEDULER_cancel (abort_task);
  119. if (NULL != reg_handle)
  120. GNUNET_TESTBED_cancel_registration (reg_handle);
  121. if (NULL != controller)
  122. GNUNET_TESTBED_controller_disconnect (controller);
  123. if (NULL != cfg)
  124. GNUNET_CONFIGURATION_destroy (cfg);
  125. if (NULL != cp)
  126. GNUNET_TESTBED_controller_stop (cp);
  127. if (NULL != neighbour)
  128. GNUNET_TESTBED_host_destroy (neighbour);
  129. if (NULL != host)
  130. GNUNET_TESTBED_host_destroy (host);
  131. }
  132. /**
  133. * shortcut to exit during failure
  134. */
  135. #define FAIL_TEST(cond, ret) do { \
  136. if (!(cond)) { \
  137. GNUNET_break(0); \
  138. if (GNUNET_SCHEDULER_NO_TASK != abort_task) \
  139. GNUNET_SCHEDULER_cancel (abort_task); \
  140. abort_task = GNUNET_SCHEDULER_NO_TASK; \
  141. GNUNET_SCHEDULER_add_now (do_shutdown, NULL); \
  142. ret; \
  143. } \
  144. } while (0)
  145. /**
  146. * abort task to run on test timed out
  147. *
  148. * @param cls NULL
  149. * @param tc the task context
  150. */
  151. static void
  152. do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  153. {
  154. LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
  155. abort_task = GNUNET_SCHEDULER_NO_TASK;
  156. do_shutdown (cls, tc);
  157. }
  158. /**
  159. * Adapter function called to establish a connection to
  160. * a service.
  161. *
  162. * @param cls closure
  163. * @param cfg configuration of the peer to connect to; will be available until
  164. * GNUNET_TESTBED_operation_done() is called on the operation returned
  165. * from GNUNET_TESTBED_service_connect()
  166. * @return service handle to return in 'op_result', NULL on error
  167. */
  168. static void *
  169. arm_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
  170. {
  171. FAIL_TEST (NULL == cls, return NULL);
  172. FAIL_TEST (OTHER == sub_test, return NULL);
  173. sub_test = PEER_SERVICE_CONNECT;
  174. arm_handle = GNUNET_ARM_connect (cfg, NULL, NULL);
  175. return arm_handle;
  176. }
  177. /**
  178. * Adapter function called to destroy a connection to
  179. * a service.
  180. *
  181. * @param cls closure
  182. * @param op_result service handle returned from the connect adapter
  183. */
  184. static void
  185. arm_disconnect_adapter (void *cls, void *op_result)
  186. {
  187. FAIL_TEST (NULL != op_result, return);
  188. FAIL_TEST (op_result == arm_handle, return);
  189. GNUNET_ARM_disconnect_and_free (arm_handle);
  190. arm_handle = NULL;
  191. FAIL_TEST (PEER_SERVICE_CONNECT == sub_test, return);
  192. FAIL_TEST (NULL != operation, return);
  193. operation = GNUNET_TESTBED_peer_stop (NULL, peer, NULL, NULL);
  194. FAIL_TEST (NULL != operation, return);
  195. }
  196. /**
  197. * Callback to be called when a service connect operation is completed
  198. *
  199. * @param cls the callback closure from functions generating an operation
  200. * @param op the operation that has been finished
  201. * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
  202. * @param emsg error message in case the operation has failed; will be NULL if
  203. * operation has executed successfully.
  204. */
  205. static void
  206. service_connect_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
  207. void *ca_result, const char *emsg)
  208. {
  209. switch (sub_test)
  210. {
  211. case PEER_SERVICE_CONNECT:
  212. FAIL_TEST (operation == op, return);
  213. FAIL_TEST (NULL == emsg, return);
  214. FAIL_TEST (NULL == cls, return);
  215. FAIL_TEST (ca_result == arm_handle, return);
  216. GNUNET_TESTBED_operation_done (operation); /* This results in call to
  217. * disconnect adapter */
  218. break;
  219. default:
  220. FAIL_TEST (0, return);
  221. }
  222. }
  223. /**
  224. * Callback to be called when the requested peer information is available
  225. *
  226. * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
  227. * @param op the operation this callback corresponds to
  228. * @param pinfo the result; will be NULL if the operation has failed
  229. * @param emsg error message if the operation has failed; will be NULL if the
  230. * operation is successfull
  231. */
  232. static void
  233. peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
  234. const struct GNUNET_TESTBED_PeerInformation *pinfo,
  235. const char *emsg)
  236. {
  237. switch (sub_test)
  238. {
  239. case PEER_GETCONFIG:
  240. FAIL_TEST (NULL != pinfo, return);
  241. FAIL_TEST (NULL == emsg, return);
  242. FAIL_TEST (NULL == cb_cls, return);
  243. FAIL_TEST (operation == op, return);
  244. FAIL_TEST (GNUNET_TESTBED_PIT_CONFIGURATION == pinfo->pit, return);
  245. FAIL_TEST (NULL != pinfo->result.cfg, return);
  246. sub_test = PEER_DESTROY;
  247. GNUNET_TESTBED_operation_done (operation);
  248. operation = GNUNET_TESTBED_peer_destroy (peer);
  249. break;
  250. default:
  251. FAIL_TEST (0, return);
  252. }
  253. }
  254. /**
  255. * Signature of the event handler function called by the
  256. * respective event controller.
  257. *
  258. * @param cls closure
  259. * @param event information about the event
  260. */
  261. static void
  262. controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
  263. {
  264. switch (event->type)
  265. {
  266. case GNUNET_TESTBED_ET_OPERATION_FINISHED:
  267. switch (sub_test)
  268. {
  269. case PEER_DESTROY:
  270. FAIL_TEST (event->op == operation, return);
  271. FAIL_TEST (NULL == event->op_cls, return);
  272. FAIL_TEST (NULL == event->details.operation_finished.emsg, return);
  273. FAIL_TEST (NULL == event->details.operation_finished.generic, return);
  274. GNUNET_TESTBED_operation_done (operation);
  275. GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  276. break;
  277. case PEER_SERVICE_CONNECT:
  278. FAIL_TEST (event->op == operation, return);
  279. FAIL_TEST (NULL == event->op_cls, return);
  280. FAIL_TEST (NULL == event->details.operation_finished.emsg, return);
  281. FAIL_TEST (NULL != arm_handle, return);
  282. FAIL_TEST (event->details.operation_finished.generic == arm_handle, return);
  283. break;
  284. default:
  285. FAIL_TEST (0, return);
  286. break;
  287. }
  288. break;
  289. case GNUNET_TESTBED_ET_PEER_START:
  290. FAIL_TEST (event->details.peer_start.host == host, return);
  291. FAIL_TEST (event->details.peer_start.peer == peer, return);
  292. FAIL_TEST (OTHER == sub_test, return);
  293. GNUNET_TESTBED_operation_done (operation);
  294. operation =
  295. GNUNET_TESTBED_service_connect (NULL, peer, "dht",
  296. &service_connect_comp_cb, NULL,
  297. &arm_connect_adapter,
  298. &arm_disconnect_adapter, NULL);
  299. FAIL_TEST (NULL != operation, return);
  300. break;
  301. case GNUNET_TESTBED_ET_PEER_STOP:
  302. FAIL_TEST (event->details.peer_stop.peer == peer, return);
  303. FAIL_TEST (PEER_SERVICE_CONNECT == sub_test, return);
  304. result = GNUNET_YES;
  305. sub_test = PEER_GETCONFIG;
  306. GNUNET_TESTBED_operation_done (operation);
  307. operation =
  308. GNUNET_TESTBED_peer_get_information (peer,
  309. GNUNET_TESTBED_PIT_CONFIGURATION,
  310. &peerinfo_cb, NULL);
  311. break;
  312. default:
  313. FAIL_TEST (0, return); /* We should never reach this state */
  314. }
  315. }
  316. /**
  317. * Functions of this signature are called when a peer has been successfully
  318. * created
  319. *
  320. * @param cls the closure from GNUNET_TESTBED_peer_create()
  321. * @param peer the handle for the created peer; NULL on any error during
  322. * creation
  323. * @param emsg NULL if peer is not NULL; else MAY contain the error description
  324. */
  325. static void
  326. peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
  327. {
  328. struct GNUNET_TESTBED_Peer **peer_ptr;
  329. peer_ptr = cls;
  330. FAIL_TEST (NULL != peer, return);
  331. FAIL_TEST (NULL != peer_ptr, return);
  332. *peer_ptr = peer;
  333. GNUNET_TESTBED_operation_done (operation);
  334. operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
  335. FAIL_TEST (NULL != operation, return);
  336. }
  337. /**
  338. * Callback which will be called to after a host registration succeeded or failed
  339. *
  340. * @param cls the host which has been registered
  341. * @param emsg the error message; NULL if host registration is successful
  342. */
  343. static void
  344. registration_comp (void *cls, const char *emsg)
  345. {
  346. FAIL_TEST (cls == neighbour, return);
  347. reg_handle = NULL;
  348. operation =
  349. GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb,
  350. &peer);
  351. FAIL_TEST (NULL != operation, return);
  352. }
  353. /**
  354. * Callback to signal successfull startup of the controller process
  355. *
  356. * @param cls the closure from GNUNET_TESTBED_controller_start()
  357. * @param cfg the configuration with which the controller has been started;
  358. * NULL if status is not GNUNET_OK
  359. * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
  360. * GNUNET_TESTBED_controller_stop() shouldn't be called in this case
  361. */
  362. static void
  363. status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg_, int status)
  364. {
  365. uint64_t event_mask;
  366. if (GNUNET_OK != status)
  367. {
  368. cp = NULL;
  369. FAIL_TEST (0, return);
  370. return;
  371. }
  372. event_mask = 0;
  373. event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
  374. event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
  375. event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
  376. event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
  377. controller =
  378. GNUNET_TESTBED_controller_connect (host, event_mask, &controller_cb,
  379. NULL);
  380. FAIL_TEST (NULL != controller, return);
  381. neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 0);
  382. FAIL_TEST (NULL != neighbour, return);
  383. reg_handle =
  384. GNUNET_TESTBED_register_host (controller, neighbour, &registration_comp,
  385. neighbour);
  386. FAIL_TEST (NULL != reg_handle, return);
  387. }
  388. /**
  389. * Main run function.
  390. *
  391. * @param cls NULL
  392. * @param args arguments passed to GNUNET_PROGRAM_run
  393. * @param cfgfile the path to configuration file
  394. * @param cfg the configuration file handle
  395. */
  396. static void
  397. run (void *cls, char *const *args, const char *cfgfile,
  398. const struct GNUNET_CONFIGURATION_Handle *config)
  399. {
  400. cfg = GNUNET_CONFIGURATION_dup (config);
  401. host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
  402. FAIL_TEST (NULL != host, return);
  403. cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb,
  404. NULL);
  405. abort_task =
  406. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  407. (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
  408. NULL);
  409. }
  410. /**
  411. * Main function
  412. */
  413. int
  414. main (int argc, char **argv)
  415. {
  416. int ret;
  417. char *const argv2[] = { "test_testbed_api",
  418. "-c", "test_testbed_api.conf",
  419. NULL
  420. };
  421. struct GNUNET_GETOPT_CommandLineOption options[] = {
  422. GNUNET_GETOPT_OPTION_END
  423. };
  424. result = GNUNET_SYSERR;
  425. ret =
  426. GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
  427. "test_testbed_api", "nohelp", options, &run, NULL);
  428. if ((GNUNET_OK != ret) || (GNUNET_OK != result))
  429. return 1;
  430. return 0;
  431. }
  432. /* end of test_testbed_api.c */