test_fs_unindex.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2004, 2005, 2006, 2008, 2009 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_fs_unindex.c
  19. * @brief simple testcase for simple publish + unindex operation
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_fs_service.h"
  25. #include "gnunet_testing_lib.h"
  26. /**
  27. * File-size we use for testing.
  28. */
  29. #define FILESIZE (1024 * 1024 * 2)
  30. /**
  31. * How long until we give up on transmitting the message?
  32. */
  33. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
  34. /**
  35. * How long should our test-content live?
  36. */
  37. #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
  38. static struct GNUNET_TIME_Absolute start;
  39. static struct GNUNET_FS_Handle *fs;
  40. static struct GNUNET_FS_UnindexContext *unindex;
  41. static struct GNUNET_FS_PublishContext *publish;
  42. static char *fn;
  43. static void
  44. abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  45. {
  46. GNUNET_FS_publish_stop (publish);
  47. publish = NULL;
  48. }
  49. static void
  50. abort_unindex_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  51. {
  52. GNUNET_FS_unindex_stop (unindex);
  53. unindex = NULL;
  54. GNUNET_DISK_directory_remove (fn);
  55. GNUNET_free (fn);
  56. fn = NULL;
  57. }
  58. static void *
  59. progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
  60. {
  61. switch (event->status)
  62. {
  63. case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
  64. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  65. "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
  66. (unsigned long long) event->value.publish.completed,
  67. (unsigned long long) event->value.publish.size,
  68. event->value.publish.specifics.progress.depth,
  69. (unsigned long long) event->value.publish.specifics.
  70. progress.offset);
  71. break;
  72. case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
  73. break;
  74. case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
  75. printf ("Publishing complete, %llu kbps.\n",
  76. (unsigned long long) (FILESIZE * 1000000LL /
  77. (1 +
  78. GNUNET_TIME_absolute_get_duration
  79. (start).rel_value_us) / 1024));
  80. start = GNUNET_TIME_absolute_get ();
  81. unindex = GNUNET_FS_unindex_start (fs, fn, "unindex");
  82. GNUNET_assert (unindex != NULL);
  83. break;
  84. case GNUNET_FS_STATUS_UNINDEX_COMPLETED:
  85. printf ("Unindex complete, %llu kbps.\n",
  86. (unsigned long long) (FILESIZE * 1000000LL /
  87. (1 +
  88. GNUNET_TIME_absolute_get_duration
  89. (start).rel_value_us) / 1024));
  90. GNUNET_SCHEDULER_add_continuation (&abort_unindex_task, NULL,
  91. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  92. break;
  93. case GNUNET_FS_STATUS_UNINDEX_PROGRESS:
  94. GNUNET_assert (unindex == event->value.unindex.uc);
  95. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  96. "Unindex is progressing (%llu/%llu at level %u off %llu)...\n",
  97. (unsigned long long) event->value.unindex.completed,
  98. (unsigned long long) event->value.unindex.size,
  99. event->value.unindex.specifics.progress.depth,
  100. (unsigned long long) event->value.unindex.specifics.
  101. progress.offset);
  102. break;
  103. case GNUNET_FS_STATUS_PUBLISH_ERROR:
  104. FPRINTF (stderr, "Error publishing file: %s\n",
  105. event->value.publish.specifics.error.message);
  106. GNUNET_break (0);
  107. GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
  108. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  109. break;
  110. case GNUNET_FS_STATUS_UNINDEX_ERROR:
  111. FPRINTF (stderr, "Error unindexing file: %s\n",
  112. event->value.unindex.specifics.error.message);
  113. GNUNET_SCHEDULER_add_continuation (&abort_unindex_task, NULL,
  114. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  115. break;
  116. case GNUNET_FS_STATUS_PUBLISH_START:
  117. GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
  118. GNUNET_assert (NULL == event->value.publish.pctx);
  119. GNUNET_assert (FILESIZE == event->value.publish.size);
  120. GNUNET_assert (0 == event->value.publish.completed);
  121. GNUNET_assert (1 == event->value.publish.anonymity);
  122. break;
  123. case GNUNET_FS_STATUS_PUBLISH_STOPPED:
  124. GNUNET_assert (publish == event->value.publish.pc);
  125. GNUNET_assert (FILESIZE == event->value.publish.size);
  126. GNUNET_assert (1 == event->value.publish.anonymity);
  127. GNUNET_FS_stop (fs);
  128. fs = NULL;
  129. break;
  130. case GNUNET_FS_STATUS_UNINDEX_START:
  131. GNUNET_assert (unindex == NULL);
  132. GNUNET_assert (0 == strcmp ("unindex", event->value.unindex.cctx));
  133. GNUNET_assert (0 == strcmp (fn, event->value.unindex.filename));
  134. GNUNET_assert (FILESIZE == event->value.unindex.size);
  135. GNUNET_assert (0 == event->value.unindex.completed);
  136. break;
  137. case GNUNET_FS_STATUS_UNINDEX_STOPPED:
  138. GNUNET_assert (unindex == event->value.unindex.uc);
  139. GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
  140. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  141. break;
  142. default:
  143. printf ("Unexpected event: %d\n", event->status);
  144. break;
  145. }
  146. return NULL;
  147. }
  148. static void
  149. run (void *cls,
  150. const struct GNUNET_CONFIGURATION_Handle *cfg,
  151. struct GNUNET_TESTING_Peer *peer)
  152. {
  153. const char *keywords[] = {
  154. "down_foo",
  155. "down_bar",
  156. };
  157. char *buf;
  158. struct GNUNET_CONTAINER_MetaData *meta;
  159. struct GNUNET_FS_Uri *kuri;
  160. struct GNUNET_FS_FileInformation *fi;
  161. size_t i;
  162. struct GNUNET_FS_BlockOptions bo;
  163. fn = GNUNET_DISK_mktemp ("gnunet-unindex-test-dst");
  164. fs = GNUNET_FS_start (cfg, "test-fs-unindex", &progress_cb, NULL,
  165. GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
  166. GNUNET_assert (NULL != fs);
  167. buf = GNUNET_malloc (FILESIZE);
  168. for (i = 0; i < FILESIZE; i++)
  169. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  170. GNUNET_assert (FILESIZE ==
  171. GNUNET_DISK_fn_write (fn, buf, FILESIZE,
  172. GNUNET_DISK_PERM_USER_READ |
  173. GNUNET_DISK_PERM_USER_WRITE));
  174. GNUNET_free (buf);
  175. meta = GNUNET_CONTAINER_meta_data_create ();
  176. kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
  177. bo.content_priority = 42;
  178. bo.anonymity_level = 1;
  179. bo.replication_level = 0;
  180. bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
  181. fi = GNUNET_FS_file_information_create_from_file (fs, "publish-context", fn,
  182. kuri, meta, GNUNET_YES,
  183. &bo);
  184. GNUNET_FS_uri_destroy (kuri);
  185. GNUNET_CONTAINER_meta_data_destroy (meta);
  186. GNUNET_assert (NULL != fi);
  187. start = GNUNET_TIME_absolute_get ();
  188. publish =
  189. GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
  190. GNUNET_FS_PUBLISH_OPTION_NONE);
  191. GNUNET_assert (publish != NULL);
  192. }
  193. int
  194. main (int argc, char *argv[])
  195. {
  196. if (0 != GNUNET_TESTING_peer_run ("test-fs-unindex",
  197. "test_fs_unindex_data.conf",
  198. &run, NULL))
  199. return 1;
  200. return 0;
  201. }
  202. /* end of test_fs_unindex.c */