test_testbed_underlay.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, "Peers 0 and 2 should not get connected\n");
  64. else
  65. {
  66. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers 0 and 2 not connected: %s. Success!\n", emsg);
  67. result = GNUNET_OK;
  68. }
  69. GNUNET_SCHEDULER_shutdown ();
  70. }
  71. /**
  72. * Signature of a main function for a testcase.
  73. *
  74. * @param cls closure
  75. * @param h the run handle
  76. * @param num_peers number of peers in 'peers'
  77. * @param peers_ handle to peers run in the testbed
  78. * @param links_succeeded the number of overlay link connection attempts that
  79. * succeeded
  80. * @param links_failed the number of overlay link connection attempts that
  81. * failed
  82. */
  83. static void
  84. test_master (void *cls,
  85. struct GNUNET_TESTBED_RunHandle *h,
  86. unsigned int num_peers,
  87. struct GNUNET_TESTBED_Peer **peers_,
  88. unsigned int links_succeeded,
  89. unsigned int links_failed)
  90. {
  91. GNUNET_assert (NULL == cls);
  92. if (NULL == peers_)
  93. {
  94. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test due to timeout\n");
  95. GNUNET_SCHEDULER_shutdown ();
  96. return;
  97. }
  98. GNUNET_assert (NUM_PEERS == num_peers);
  99. op = GNUNET_TESTBED_overlay_connect (NULL,
  100. &overlay_connect_status,
  101. NULL,
  102. peers_[0],
  103. peers_[2]);
  104. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
  105. 60),
  106. &do_shutdown, NULL);
  107. }
  108. #ifndef PATH_MAX
  109. /**
  110. * Assumed maximum path length (for the log file name).
  111. */
  112. #define PATH_MAX 4096
  113. #endif
  114. /**
  115. * Main function
  116. */
  117. int
  118. main (int argc, char **argv)
  119. {
  120. struct GNUNET_CONFIGURATION_Handle *cfg;
  121. char pwd[PATH_MAX];
  122. char *dbfile;
  123. uint64_t event_mask;
  124. result = GNUNET_SYSERR;
  125. event_mask = 0;
  126. cfg = GNUNET_CONFIGURATION_create ();
  127. GNUNET_assert (GNUNET_YES ==
  128. GNUNET_CONFIGURATION_parse (cfg,
  129. "test_testbed_underlay.conf.in"));
  130. if (NULL == getcwd (pwd, PATH_MAX))
  131. return 1;
  132. GNUNET_assert (0 < GNUNET_asprintf (&dbfile, "%s/%s", pwd,
  133. "test-underlay.sqlite"));
  134. GNUNET_CONFIGURATION_set_value_string (cfg, "TESTBED-UNDERLAY","DBFILE", dbfile);
  135. GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_write
  136. (cfg, "test_testbed_underlay.conf"));
  137. GNUNET_CONFIGURATION_destroy (cfg);
  138. cfg = NULL;
  139. GNUNET_free (dbfile);
  140. dbfile = NULL;
  141. (void) GNUNET_TESTBED_test_run ("test_testbed_underlay",
  142. "test_testbed_underlay.conf", NUM_PEERS,
  143. event_mask, NULL, NULL,
  144. &test_master, NULL);
  145. (void) unlink ("test_testbed_underlay.conf");
  146. if (GNUNET_OK != result)
  147. return 1;
  148. return 0;
  149. }