test_testbed_underlay.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_underlay.c
  18. * @brief testcase binary for testing testbed underlay restrictions
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_testbed_service.h"
  24. /**
  25. * Number of peers we start in this test case
  26. */
  27. #define NUM_PEERS 3
  28. /**
  29. * Result of this test case
  30. */
  31. static int result;
  32. static struct GNUNET_TESTBED_Operation *op;
  33. /**
  34. * Shutdown testcase
  35. *
  36. * @param cls NULL
  37. * @param tc scheduler task context
  38. */
  39. static void
  40. do_shutdown (void *cls)
  41. {
  42. if (NULL != op)
  43. GNUNET_TESTBED_operation_done (op);
  44. op = NULL;
  45. }
  46. /**
  47. * Callback to be called when an operation is completed
  48. *
  49. * @param cls the callback closure from functions generating an operation
  50. * @param op the operation that has been finished
  51. * @param emsg error message in case the operation has failed; will be NULL if
  52. * operation has executed successfully.
  53. */
  54. static void
  55. overlay_connect_status (void *cls,
  56. struct GNUNET_TESTBED_Operation *op_,
  57. const char *emsg)
  58. {
  59. GNUNET_assert (op_ == op);
  60. GNUNET_TESTBED_operation_done (op);
  61. op = NULL;
  62. if (NULL == emsg)
  63. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  64. "Peers 0 and 2 should not get connected\n");
  65. else
  66. {
  67. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  68. "Peers 0 and 2 not connected: %s. Success!\n", emsg);
  69. result = GNUNET_OK;
  70. }
  71. GNUNET_SCHEDULER_shutdown ();
  72. }
  73. /**
  74. * Signature of a main function for a testcase.
  75. *
  76. * @param cls closure
  77. * @param h the run handle
  78. * @param num_peers number of peers in 'peers'
  79. * @param peers_ handle to peers run in the testbed
  80. * @param links_succeeded the number of overlay link connection attempts that
  81. * succeeded
  82. * @param links_failed the number of overlay link connection attempts that
  83. * failed
  84. */
  85. static void
  86. test_master (void *cls,
  87. struct GNUNET_TESTBED_RunHandle *h,
  88. unsigned int num_peers,
  89. struct GNUNET_TESTBED_Peer **peers_,
  90. unsigned int links_succeeded,
  91. unsigned int links_failed)
  92. {
  93. GNUNET_assert (NULL == cls);
  94. if (NULL == peers_)
  95. {
  96. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test due to timeout\n");
  97. GNUNET_SCHEDULER_shutdown ();
  98. return;
  99. }
  100. GNUNET_assert (NUM_PEERS == num_peers);
  101. op = GNUNET_TESTBED_overlay_connect (NULL,
  102. &overlay_connect_status,
  103. NULL,
  104. peers_[0],
  105. peers_[2]);
  106. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
  107. GNUNET_TIME_UNIT_SECONDS,
  108. 60),
  109. &do_shutdown, NULL);
  110. }
  111. #ifndef PATH_MAX
  112. /**
  113. * Assumed maximum path length (for the log file name).
  114. */
  115. #define PATH_MAX 4096
  116. #endif
  117. /**
  118. * Main function
  119. */
  120. int
  121. main (int argc, char **argv)
  122. {
  123. struct GNUNET_CONFIGURATION_Handle *cfg;
  124. char pwd[PATH_MAX];
  125. char *dbfile;
  126. uint64_t event_mask;
  127. result = GNUNET_SYSERR;
  128. event_mask = 0;
  129. cfg = GNUNET_CONFIGURATION_create ();
  130. GNUNET_assert (GNUNET_YES ==
  131. GNUNET_CONFIGURATION_parse (cfg,
  132. "test_testbed_underlay.conf.in"));
  133. if (NULL == getcwd (pwd, PATH_MAX))
  134. return 1;
  135. GNUNET_assert (0 < GNUNET_asprintf (&dbfile, "%s/%s", pwd,
  136. "test-underlay.sqlite"));
  137. GNUNET_CONFIGURATION_set_value_string (cfg, "TESTBED-UNDERLAY", "DBFILE",
  138. dbfile);
  139. GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_write
  140. (cfg, "test_testbed_underlay.conf"));
  141. GNUNET_CONFIGURATION_destroy (cfg);
  142. cfg = NULL;
  143. GNUNET_free (dbfile);
  144. dbfile = NULL;
  145. (void) GNUNET_TESTBED_test_run ("test_testbed_underlay",
  146. "test_testbed_underlay.conf", NUM_PEERS,
  147. event_mask, NULL, NULL,
  148. &test_master, NULL);
  149. (void) unlink ("test_testbed_underlay.conf");
  150. if (GNUNET_OK != result)
  151. return 1;
  152. return 0;
  153. }