test_testing_peerstartup2.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. This file is part of GNUnet
  3. Copyright (C) 2008, 2009, 2012 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 testing/test_testing_new_peerstartup.c
  18. * @brief test case for testing peer startup and shutdown using new testing
  19. * library
  20. * @author Sree Harsha Totakura
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_testing_lib.h"
  25. #define LOG(kind,...) \
  26. GNUNET_log (kind, __VA_ARGS__)
  27. #define FAIL_TEST(cond) \
  28. do { \
  29. if (!(cond)) { \
  30. GNUNET_break (0); \
  31. if (GNUNET_OK == status) { \
  32. status = GNUNET_SYSERR; \
  33. } \
  34. } \
  35. } while (0) \
  36. /**
  37. * The status of the test
  38. */
  39. int status;
  40. /**
  41. * The testing context
  42. */
  43. struct TestingContext
  44. {
  45. /**
  46. * The testing system
  47. */
  48. struct GNUNET_TESTING_System *system;
  49. /**
  50. * The peer which has been started by the testing system
  51. */
  52. struct GNUNET_TESTING_Peer *peer;
  53. /**
  54. * The running configuration of the peer
  55. */
  56. struct GNUNET_CONFIGURATION_Handle *cfg;
  57. /**
  58. * State
  59. */
  60. enum {
  61. PEER_INIT,
  62. PEER_STARTED,
  63. PEER_STOPPED
  64. } state;
  65. };
  66. static void
  67. do_shutdown2 (void *cls)
  68. {
  69. struct TestingContext *test_ctx = cls;
  70. if (NULL != test_ctx->peer)
  71. GNUNET_TESTING_peer_destroy (test_ctx->peer);
  72. if (NULL != test_ctx->cfg)
  73. GNUNET_CONFIGURATION_destroy (test_ctx->cfg);
  74. if (NULL != test_ctx->system)
  75. GNUNET_TESTING_system_destroy (test_ctx->system, GNUNET_YES);
  76. GNUNET_free (test_ctx);
  77. }
  78. /**
  79. * Task for shutdown
  80. *
  81. * @param cls the testing context
  82. */
  83. static void
  84. do_shutdown (void *cls);
  85. static void
  86. peer_status_cb (void *cls, struct GNUNET_TESTING_Peer *peer, int success)
  87. {
  88. struct TestingContext *test_ctx = cls;
  89. switch (test_ctx->state)
  90. {
  91. case PEER_INIT:
  92. FAIL_TEST (0);
  93. break;
  94. case PEER_STARTED:
  95. FAIL_TEST (GNUNET_YES == success);
  96. test_ctx->state = PEER_STOPPED;
  97. GNUNET_SCHEDULER_add_now (&do_shutdown2, cls);
  98. break;
  99. case PEER_STOPPED:
  100. FAIL_TEST (0);
  101. }
  102. }
  103. /**
  104. * Task for shutdown
  105. *
  106. * @param cls the testing context
  107. */
  108. static void
  109. do_shutdown (void *cls)
  110. {
  111. struct TestingContext *test_ctx = cls;
  112. GNUNET_assert (NULL != test_ctx);
  113. if (NULL != test_ctx->peer)
  114. {
  115. FAIL_TEST (GNUNET_OK ==
  116. GNUNET_TESTING_peer_stop_async (test_ctx->peer,
  117. &peer_status_cb,
  118. test_ctx));
  119. }
  120. else
  121. do_shutdown2 (test_ctx);
  122. }
  123. /**
  124. * Main point of test execution
  125. */
  126. static void
  127. run (void *cls, char *const *args, const char *cfgfile,
  128. const struct GNUNET_CONFIGURATION_Handle *cfg)
  129. {
  130. struct TestingContext *test_ctx;
  131. char *emsg;
  132. struct GNUNET_PeerIdentity id;
  133. test_ctx = GNUNET_new (struct TestingContext);
  134. test_ctx->system =
  135. GNUNET_TESTING_system_create ("test-gnunet-testing",
  136. "127.0.0.1", NULL, NULL);
  137. emsg = NULL;
  138. if (NULL == test_ctx->system)
  139. goto end;
  140. test_ctx->cfg = GNUNET_CONFIGURATION_dup (cfg);
  141. test_ctx->peer =
  142. GNUNET_TESTING_peer_configure (test_ctx->system,
  143. test_ctx->cfg,
  144. 0, &id, &emsg);
  145. if (NULL == test_ctx->peer)
  146. {
  147. if (NULL != emsg)
  148. printf ("Test failed upon error: %s", emsg);
  149. goto end;
  150. }
  151. if (GNUNET_OK != GNUNET_TESTING_peer_start (test_ctx->peer))
  152. goto end;
  153. test_ctx->state = PEER_STARTED;
  154. FAIL_TEST (GNUNET_OK ==
  155. GNUNET_TESTING_peer_stop_async (test_ctx->peer,
  156. &peer_status_cb,
  157. test_ctx));
  158. GNUNET_TESTING_peer_stop_async_cancel (test_ctx->peer);
  159. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  160. &do_shutdown, test_ctx);
  161. return;
  162. end:
  163. FAIL_TEST (0);
  164. GNUNET_SCHEDULER_add_now (&do_shutdown, test_ctx);
  165. GNUNET_free_non_null (emsg);
  166. }
  167. int main (int argc, char *argv[])
  168. {
  169. struct GNUNET_GETOPT_CommandLineOption options[] = {
  170. GNUNET_GETOPT_OPTION_END
  171. };
  172. status = GNUNET_OK;
  173. if (GNUNET_OK !=
  174. GNUNET_PROGRAM_run (argc, argv,
  175. "test_testing_new_peerstartup",
  176. "test case for peerstartup using new testing library",
  177. options, &run, NULL))
  178. return 1;
  179. return (GNUNET_OK == status) ? 0 : 1;
  180. }
  181. /* end of test_testing_peerstartup.c */