test_gnunet_service_fs_p2p.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2010 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file fs/test_gnunet_service_fs_p2p.c
  19. * @brief test P2P routing using simple publish + download operation
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "fs_test_lib.h"
  24. #define VERBOSE GNUNET_NO
  25. /**
  26. * File-size we use for testing.
  27. */
  28. #define FILESIZE (1024 * 1024 * 1)
  29. /**
  30. * How long until we give up on the download?
  31. */
  32. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
  33. #define NUM_DAEMONS 2
  34. #define SEED 42
  35. static struct GNUNET_FS_TestDaemon *daemons[NUM_DAEMONS];
  36. static int ok;
  37. static struct GNUNET_TIME_Absolute start_time;
  38. static void
  39. do_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  40. {
  41. struct GNUNET_TIME_Relative del;
  42. char *fancy;
  43. GNUNET_FS_TEST_daemons_stop (NUM_DAEMONS, daemons);
  44. if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
  45. {
  46. del = GNUNET_TIME_absolute_get_duration (start_time);
  47. if (del.rel_value == 0)
  48. del.rel_value = 1;
  49. fancy =
  50. GNUNET_STRINGS_byte_size_fancy (((unsigned long long) FILESIZE) *
  51. 1000LL / del.rel_value);
  52. fprintf (stdout, "Download speed was %s/s\n", fancy);
  53. GNUNET_free (fancy);
  54. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Finished download, shutting down\n",
  55. (unsigned long long) FILESIZE);
  56. }
  57. else
  58. {
  59. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  60. "Timeout during download, shutting down with error\n");
  61. ok = 1;
  62. }
  63. }
  64. static void
  65. do_download (void *cls, const struct GNUNET_FS_Uri *uri)
  66. {
  67. if (NULL == uri)
  68. {
  69. GNUNET_FS_TEST_daemons_stop (NUM_DAEMONS, daemons);
  70. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  71. "Timeout during upload attempt, shutting down with error\n");
  72. ok = 1;
  73. return;
  74. }
  75. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Downloading %llu bytes\n",
  76. (unsigned long long) FILESIZE);
  77. start_time = GNUNET_TIME_absolute_get ();
  78. GNUNET_FS_TEST_download (daemons[0], TIMEOUT, 1, SEED, uri, VERBOSE, &do_stop,
  79. NULL);
  80. }
  81. static void
  82. do_publish (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  83. {
  84. if (0 == (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
  85. {
  86. GNUNET_FS_TEST_daemons_stop (NUM_DAEMONS, daemons);
  87. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  88. "Timeout during connect attempt, shutting down with error\n");
  89. ok = 1;
  90. return;
  91. }
  92. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Publishing %llu bytes\n",
  93. (unsigned long long) FILESIZE);
  94. GNUNET_FS_TEST_publish (daemons[1], TIMEOUT, 1, GNUNET_NO, FILESIZE, SEED,
  95. VERBOSE, &do_download, NULL);
  96. }
  97. static void
  98. do_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  99. {
  100. GNUNET_assert (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE));
  101. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  102. "Daemons started, will now try to connect them\n");
  103. GNUNET_FS_TEST_daemons_connect (daemons[0], daemons[1], TIMEOUT, &do_publish,
  104. NULL);
  105. }
  106. static void
  107. run (void *cls, char *const *args, const char *cfgfile,
  108. const struct GNUNET_CONFIGURATION_Handle *cfg)
  109. {
  110. GNUNET_FS_TEST_daemons_start ("fs_test_lib_data.conf", TIMEOUT, NUM_DAEMONS,
  111. daemons, &do_connect, NULL);
  112. }
  113. int
  114. main (int argc, char *argv[])
  115. {
  116. char *const argvx[] = {
  117. "test-gnunet-service-fs-p2p",
  118. "-c",
  119. "fs_test_lib_data.conf",
  120. #if VERBOSE
  121. "-L", "DEBUG",
  122. #endif
  123. NULL
  124. };
  125. struct GNUNET_GETOPT_CommandLineOption options[] = {
  126. GNUNET_GETOPT_OPTION_END
  127. };
  128. GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
  129. GNUNET_log_setup ("test_gnunet_service_fs_p2p",
  130. #if VERBOSE
  131. "DEBUG",
  132. #else
  133. "WARNING",
  134. #endif
  135. NULL);
  136. GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
  137. "test-gnunet-service-fs-p2p", "nohelp", options, &run,
  138. NULL);
  139. GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-lib/");
  140. return ok;
  141. }
  142. /* end of test_gnunet_service_fs_p2p.c */