test_fs_unindex_persistence.c 8.8 KB

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