test_gnunet_service_fs_migration.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010, 2012, 2015 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 fs/test_gnunet_service_fs_migration.c
  18. * @brief test content migration between two peers
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "fs_test_lib.h"
  23. #include "gnunet_testbed_service.h"
  24. #define VERBOSE GNUNET_NO
  25. /**
  26. * File-size we use for testing.
  27. */
  28. #define FILESIZE (2 * 32 * 1024)
  29. /**
  30. * How long until we give up on transmitting the message?
  31. */
  32. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
  33. /**
  34. * How long do we give the peers for content migration?
  35. */
  36. #define MIGRATION_DELAY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
  37. 90)
  38. #define SEED 42
  39. static struct GNUNET_TESTBED_Peer *daemons[2];
  40. static int ok;
  41. static struct GNUNET_TIME_Absolute start_time;
  42. static struct GNUNET_TESTBED_Operation *op;
  43. struct DownloadContext
  44. {
  45. char *fn;
  46. struct GNUNET_FS_Uri *uri;
  47. };
  48. static void
  49. do_stop (void *cls)
  50. {
  51. struct GNUNET_TIME_Relative del;
  52. char *fancy;
  53. GNUNET_SCHEDULER_shutdown ();
  54. if (0 ==
  55. GNUNET_TIME_absolute_get_remaining (GNUNET_TIME_absolute_add (start_time,
  56. TIMEOUT)).
  57. rel_value_us)
  58. {
  59. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  60. "Timeout during download, shutting down with error\n");
  61. ok = 1;
  62. }
  63. else
  64. {
  65. del = GNUNET_TIME_absolute_get_duration (start_time);
  66. if (del.rel_value_us == 0)
  67. del.rel_value_us = 1;
  68. fancy =
  69. GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE)
  70. * 1000000LL / del.rel_value_us);
  71. fprintf (stdout,
  72. "Download speed was %s/s\n",
  73. fancy);
  74. GNUNET_free (fancy);
  75. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  76. "Finished download, shutting down\n");
  77. }
  78. }
  79. static void
  80. do_download (void *cls,
  81. const char *emsg)
  82. {
  83. struct DownloadContext *dc = cls;
  84. struct GNUNET_FS_Uri *uri = dc->uri;
  85. GNUNET_TESTBED_operation_done (op);
  86. op = NULL;
  87. if (NULL != dc->fn)
  88. {
  89. GNUNET_DISK_directory_remove (dc->fn);
  90. GNUNET_free (dc->fn);
  91. }
  92. GNUNET_free (dc);
  93. if (NULL != emsg)
  94. {
  95. GNUNET_SCHEDULER_shutdown ();
  96. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  97. "Failed to stop source daemon: %s\n",
  98. emsg);
  99. GNUNET_FS_uri_destroy (uri);
  100. ok = 1;
  101. return;
  102. }
  103. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  104. "Downloading %llu bytes\n",
  105. (unsigned long long) FILESIZE);
  106. start_time = GNUNET_TIME_absolute_get ();
  107. GNUNET_FS_TEST_download (daemons[0],
  108. TIMEOUT,
  109. 1,
  110. SEED,
  111. uri,
  112. VERBOSE,
  113. &do_stop,
  114. NULL);
  115. GNUNET_FS_uri_destroy (uri);
  116. }
  117. static void
  118. stop_source_peer (void *cls)
  119. {
  120. struct DownloadContext *dc = cls;
  121. /* FIXME: We should not interact with testbed when shutting down */
  122. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  123. "Stopping source peer\n");
  124. op = GNUNET_TESTBED_peer_stop (NULL,
  125. daemons[1],
  126. &do_download, dc);
  127. GNUNET_assert (NULL != op);
  128. }
  129. static void
  130. do_wait (void *cls,
  131. const struct GNUNET_FS_Uri *uri,
  132. const char *fn)
  133. {
  134. struct DownloadContext *dc;
  135. if (NULL == uri)
  136. {
  137. GNUNET_SCHEDULER_shutdown ();
  138. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  139. "Timeout during upload attempt, shutting down with error\n");
  140. ok = 1;
  141. return;
  142. }
  143. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  144. "Waiting to allow content to migrate\n");
  145. dc = GNUNET_new (struct DownloadContext);
  146. dc->uri = GNUNET_FS_uri_dup (uri);
  147. if (NULL != fn)
  148. dc->fn = GNUNET_strdup (fn);
  149. (void) GNUNET_SCHEDULER_add_delayed (MIGRATION_DELAY,
  150. &stop_source_peer,
  151. dc);
  152. }
  153. static void
  154. do_publish (void *cls,
  155. struct GNUNET_TESTBED_RunHandle *h,
  156. unsigned int num_peers,
  157. struct GNUNET_TESTBED_Peer **peers,
  158. unsigned int links_succeeded,
  159. unsigned int links_failed)
  160. {
  161. unsigned int i;
  162. GNUNET_assert (2 == num_peers);
  163. for (i = 0; i < num_peers; i++)
  164. daemons[i] = peers[i];
  165. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  166. "Publishing %llu bytes\n",
  167. (unsigned long long) FILESIZE);
  168. GNUNET_FS_TEST_publish (daemons[1], TIMEOUT, 1, GNUNET_NO,
  169. FILESIZE, SEED,
  170. VERBOSE, &do_wait, NULL);
  171. }
  172. int
  173. main (int argc,
  174. char *argv[])
  175. {
  176. (void) GNUNET_TESTBED_test_run ("test-gnunet-service-fs-migration",
  177. "fs_test_lib_data.conf",
  178. 2,
  179. 0, NULL, NULL,
  180. &do_publish,
  181. NULL);
  182. GNUNET_DISK_directory_remove ("/tmp/test-gnunet-service-fs-migration/");
  183. return ok;
  184. }
  185. /* end of test_gnunet_service_fs_migration.c */