test_transport_testing_restart.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2016 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 transport/test_transport_testing_restart.c
  18. * @brief test case for transport testing library:
  19. * start the peer, get the HELLO message, restart and stop the peer
  20. */
  21. #include "platform.h"
  22. #include "gnunet_transport_service.h"
  23. #include "transport-testing.h"
  24. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
  25. static struct GNUNET_SCHEDULER_Task *timeout_task;
  26. static struct GNUNET_TRANSPORT_TESTING_PeerContext *p;
  27. static struct GNUNET_TRANSPORT_TESTING_Handle *tth;
  28. static int ret;
  29. static void
  30. end ()
  31. {
  32. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  33. "Stopping peers\n");
  34. if (NULL != timeout_task)
  35. GNUNET_SCHEDULER_cancel (timeout_task);
  36. if (NULL != p)
  37. GNUNET_TRANSPORT_TESTING_stop_peer (p);
  38. if (NULL != tth)
  39. GNUNET_TRANSPORT_TESTING_done (tth);
  40. }
  41. static void
  42. end_badly ()
  43. {
  44. timeout_task = NULL;
  45. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  46. "Timeout!\n");
  47. end ();
  48. ret = GNUNET_SYSERR;
  49. }
  50. static void
  51. restart_cb (void *cls)
  52. {
  53. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  54. "Peer %u (`%s') successfully restarted\n",
  55. p->no,
  56. GNUNET_i2s (&p->id));
  57. ret = 0;
  58. GNUNET_SCHEDULER_add_now (&end,
  59. NULL);
  60. }
  61. static void
  62. restart_task ()
  63. {
  64. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  65. "Peer %u (`%s') restarting\n",
  66. p->no,
  67. GNUNET_i2s (&p->id));
  68. GNUNET_TRANSPORT_TESTING_restart_peer (p,
  69. &restart_cb,
  70. p);
  71. }
  72. static void
  73. start_cb (void *cls)
  74. {
  75. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  76. "Peer %u (`%s') successfully started\n",
  77. p->no,
  78. GNUNET_i2s (&p->id));
  79. GNUNET_SCHEDULER_add_now (&restart_task,
  80. NULL);
  81. }
  82. static void
  83. run (void *cls,
  84. char *const *args,
  85. const char *cfgfile,
  86. const struct GNUNET_CONFIGURATION_Handle *cfg)
  87. {
  88. ret = 1;
  89. tth = GNUNET_TRANSPORT_TESTING_init ();
  90. GNUNET_assert (NULL != tth);
  91. timeout_task
  92. = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  93. &end_badly,
  94. NULL);
  95. p = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  96. cfgfile,
  97. 1,
  98. NULL, /* receive cb */
  99. NULL, /* connect cb */
  100. NULL, /* disconnect cb */
  101. NULL, /* nc/nd closure */
  102. start_cb, /* startup cb */
  103. NULL); /* closure */
  104. if (NULL == p)
  105. {
  106. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  107. "Failed to start peer\n");
  108. end ();
  109. ret = 1;
  110. }
  111. }
  112. int
  113. main (int argc,
  114. char *argv[])
  115. {
  116. char *const argv_1[] = { "test_transport_testing_restart",
  117. "-c",
  118. "test_transport_api_data.conf",
  119. NULL };
  120. struct GNUNET_GETOPT_CommandLineOption options[] = {
  121. GNUNET_GETOPT_OPTION_END
  122. };
  123. GNUNET_log_setup ("test_transport_testing_restart",
  124. "WARNING",
  125. NULL);
  126. GNUNET_PROGRAM_run ((sizeof(argv_1) / sizeof(char *)) - 1,
  127. argv_1,
  128. "test_transport_testing_restart",
  129. "nohelp",
  130. options,
  131. &run,
  132. NULL);
  133. return ret;
  134. }
  135. /* end of test_transport_testing_restart.c */