test_transport_testing_startstop.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_startstop.c
  18. * @brief test case for transport testing library:
  19. * start the peer, get the HELLO message 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. start_cb (void *cls)
  52. {
  53. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  54. "Peer %u (`%s') successfully started\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. run (void *cls,
  63. char *const *args,
  64. const char *cfgfile,
  65. const struct GNUNET_CONFIGURATION_Handle *cfg)
  66. {
  67. ret = 1;
  68. tth = GNUNET_TRANSPORT_TESTING_init ();
  69. GNUNET_assert (NULL != tth);
  70. timeout_task =
  71. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
  72. &end_badly,
  73. NULL);
  74. p = GNUNET_TRANSPORT_TESTING_start_peer (tth,
  75. cfgfile,
  76. 1,
  77. NULL, /* receive cb */
  78. NULL, /* connect cb */
  79. NULL, /* disconnect cb */
  80. NULL, /* nc/nd closure */
  81. &start_cb, /* startup cb */
  82. NULL); /* closure */
  83. if (NULL == p)
  84. {
  85. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Failed to start peer\n");
  86. if (timeout_task != NULL)
  87. GNUNET_SCHEDULER_cancel (timeout_task);
  88. timeout_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  89. }
  90. }
  91. int
  92. main (int argc, char *argv[])
  93. {
  94. char *const argv_1[] = { "test_transport_testing",
  95. "-c",
  96. "test_transport_api_data.conf",
  97. NULL };
  98. struct GNUNET_GETOPT_CommandLineOption options[] = {
  99. GNUNET_GETOPT_OPTION_END
  100. };
  101. GNUNET_log_setup ("test_transport_testing_startstop",
  102. "WARNING",
  103. NULL);
  104. GNUNET_PROGRAM_run ((sizeof(argv_1) / sizeof(char *)) - 1,
  105. argv_1,
  106. "test_transport_testing_startstop", "nohelp",
  107. options,
  108. &run,
  109. &ret);
  110. return ret;
  111. }
  112. /* end of test_transport_testing_startstop.c */