test_testing_sharedservices.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_sharedservices.c
  18. * @brief test case for testing service sharing among peers started by testing
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_testing_lib.h"
  24. #define LOG(kind,...) \
  25. GNUNET_log (kind, __VA_ARGS__)
  26. #define NUM_PEERS 4
  27. /**
  28. * The status of the test
  29. */
  30. int status;
  31. /**
  32. * The testing context
  33. */
  34. struct TestingContext
  35. {
  36. /**
  37. * The testing system
  38. */
  39. struct GNUNET_TESTING_System *system;
  40. /**
  41. * The peer which has been started by the testing system
  42. */
  43. struct GNUNET_TESTING_Peer *peers[NUM_PEERS];
  44. /**
  45. * The running configuration of the peer
  46. */
  47. struct GNUNET_CONFIGURATION_Handle *cfg;
  48. };
  49. /**
  50. * Task for shutdown
  51. *
  52. * @param cls the testing context
  53. */
  54. static void
  55. do_shutdown (void *cls)
  56. {
  57. struct TestingContext *test_ctx = cls;
  58. struct GNUNET_TESTING_Peer *peer;
  59. unsigned int cnt;
  60. GNUNET_assert (NULL != test_ctx);
  61. for (cnt = 0; cnt < NUM_PEERS; cnt++)
  62. {
  63. peer = test_ctx->peers[cnt];
  64. if (NULL == peer)
  65. continue;
  66. (void) GNUNET_TESTING_peer_stop (peer);
  67. GNUNET_TESTING_peer_destroy (peer);
  68. }
  69. if (NULL != test_ctx->cfg)
  70. GNUNET_CONFIGURATION_destroy (test_ctx->cfg);
  71. if (NULL != test_ctx->system)
  72. GNUNET_TESTING_system_destroy (test_ctx->system, GNUNET_YES);
  73. GNUNET_free (test_ctx);
  74. }
  75. /**
  76. * Main point of test execution
  77. */
  78. static void
  79. run (void *cls, char *const *args, const char *cfgfile,
  80. const struct GNUNET_CONFIGURATION_Handle *cfg)
  81. {
  82. struct TestingContext *test_ctx;
  83. char *emsg;
  84. struct GNUNET_PeerIdentity id;
  85. struct GNUNET_TESTING_SharedService ss[] = {
  86. {"peerinfo", cfg, 2},
  87. {NULL, NULL, 0}
  88. };
  89. struct GNUNET_TESTING_Peer *peer;
  90. unsigned int cnt;
  91. test_ctx = GNUNET_new (struct TestingContext);
  92. test_ctx->system =
  93. GNUNET_TESTING_system_create ("test-gnunet-testing",
  94. "127.0.0.1", NULL, ss);
  95. emsg = NULL;
  96. if (NULL == test_ctx->system)
  97. goto end;
  98. test_ctx->cfg = GNUNET_CONFIGURATION_dup (cfg);
  99. for (cnt = 0; cnt < NUM_PEERS; cnt++)
  100. {
  101. peer = GNUNET_TESTING_peer_configure (test_ctx->system,
  102. test_ctx->cfg,
  103. 0, &id, &emsg);
  104. if (NULL == peer)
  105. {
  106. if (NULL != emsg)
  107. printf ("Test failed upon error: %s", emsg);
  108. goto end;
  109. }
  110. if (GNUNET_OK != GNUNET_TESTING_peer_start (peer))
  111. {
  112. GNUNET_TESTING_peer_destroy (peer);
  113. goto end;
  114. }
  115. test_ctx->peers[cnt] = peer;
  116. }
  117. status = GNUNET_OK;
  118. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  119. &do_shutdown, test_ctx);
  120. return;
  121. end:
  122. GNUNET_SCHEDULER_add_now (&do_shutdown, test_ctx);
  123. GNUNET_free_non_null (emsg);
  124. }
  125. int main (int argc, char *argv[])
  126. {
  127. struct GNUNET_GETOPT_CommandLineOption options[] = {
  128. GNUNET_GETOPT_OPTION_END
  129. };
  130. char *const argv2[] = { "test_testing_sharedservices",
  131. "-c", "test_testing_sharedservices.conf",
  132. NULL
  133. };
  134. status = GNUNET_SYSERR;
  135. if (GNUNET_OK !=
  136. GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
  137. "test_testing_sharedservices",
  138. "test case for testing service sharing among peers started by testing",
  139. options, &run, NULL))
  140. return 1;
  141. return (GNUNET_OK == status) ? 0 : 3;
  142. }
  143. /* end of test_testing_sharedservices.c */