test_fs_publish_persistence.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_publish_persistence.c
  18. * @brief simple testcase for persistence of simple publish 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 const struct GNUNET_CONFIGURATION_Handle *cfg;
  40. static struct GNUNET_FS_PublishContext *publish;
  41. static struct GNUNET_FS_PublishContext *publish;
  42. static char *fn1;
  43. static char *fn2;
  44. static int err;
  45. static struct GNUNET_SCHEDULER_Task *rtask;
  46. static void
  47. abort_publish_task (void *cls)
  48. {
  49. GNUNET_FS_publish_stop (publish);
  50. publish = NULL;
  51. GNUNET_DISK_directory_remove (fn1);
  52. GNUNET_free (fn1);
  53. fn1 = NULL;
  54. GNUNET_DISK_directory_remove (fn2);
  55. GNUNET_free (fn2);
  56. fn2 = NULL;
  57. GNUNET_FS_stop (fs);
  58. fs = NULL;
  59. if (NULL != rtask)
  60. {
  61. GNUNET_SCHEDULER_cancel (rtask);
  62. rtask = NULL;
  63. }
  64. }
  65. static void *
  66. progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
  67. static void
  68. restart_fs_task (void *cls)
  69. {
  70. rtask = NULL;
  71. GNUNET_FS_stop (fs);
  72. fs = GNUNET_FS_start (cfg, "test-fs-publish-persistence",
  73. &progress_cb, NULL,
  74. GNUNET_FS_FLAGS_PERSISTENCE,
  75. GNUNET_FS_OPTIONS_END);
  76. }
  77. /**
  78. * Consider scheduling the restart-task.
  79. * Only runs the restart task once per event
  80. * category.
  81. *
  82. * @param ev type of the event to consider
  83. */
  84. static void
  85. consider_restart (int ev)
  86. {
  87. static int prev[32];
  88. static int off;
  89. int i;
  90. for (i = 0; i < off; i++)
  91. if (prev[i] == ev)
  92. return;
  93. prev[off++] = ev;
  94. rtask =
  95. GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
  96. &restart_fs_task, NULL);
  97. }
  98. static void *
  99. progress_cb (void *cls,
  100. const struct GNUNET_FS_ProgressInfo *event)
  101. {
  102. void *ret;
  103. ret = NULL;
  104. switch (event->status)
  105. {
  106. case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
  107. ret = event->value.publish.cctx;
  108. printf ("Publish complete, %llu kbps.\n",
  109. (unsigned long long) (FILESIZE * 1000000LL
  110. / (1
  111. + GNUNET_TIME_absolute_get_duration
  112. (start).rel_value_us) / 1024));
  113. if ((NULL != event->value.publish.cctx) &&
  114. (0 == strcmp ("publish-context-dir", event->value.publish.cctx)))
  115. GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
  116. break;
  117. case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
  118. ret = event->value.publish.cctx;
  119. return ret;
  120. case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
  121. consider_restart (event->status);
  122. ret = event->value.publish.cctx;
  123. GNUNET_assert (publish == event->value.publish.pc);
  124. #if VERBOSE
  125. printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
  126. (unsigned long long) event->value.publish.completed,
  127. (unsigned long long) event->value.publish.size,
  128. event->value.publish.specifics.progress.depth,
  129. (unsigned long long) event->value.publish.specifics.
  130. progress.offset);
  131. #endif
  132. break;
  133. case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
  134. if (event->value.publish.pc == publish)
  135. publish = NULL;
  136. break;
  137. case GNUNET_FS_STATUS_PUBLISH_RESUME:
  138. if (NULL == publish)
  139. {
  140. GNUNET_assert (GNUNET_YES ==
  141. GNUNET_FS_file_information_is_directory (event->
  142. value.publish.
  143. fi));
  144. publish = event->value.publish.pc;
  145. return "publish-context-dir";
  146. }
  147. break;
  148. case GNUNET_FS_STATUS_PUBLISH_ERROR:
  149. ret = event->value.publish.cctx;
  150. fprintf (stderr, "Error publishing file: %s\n",
  151. event->value.publish.specifics.error.message);
  152. err = 1;
  153. GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
  154. break;
  155. case GNUNET_FS_STATUS_PUBLISH_START:
  156. consider_restart (event->status);
  157. publish = event->value.publish.pc;
  158. ret = event->value.publish.cctx;
  159. if (0 == strcmp ("publish-context1", event->value.publish.cctx))
  160. {
  161. GNUNET_assert (0 ==
  162. strcmp ("publish-context-dir", event->value.publish.pctx));
  163. GNUNET_assert (FILESIZE == event->value.publish.size);
  164. GNUNET_assert (0 == event->value.publish.completed);
  165. GNUNET_assert (1 == event->value.publish.anonymity);
  166. }
  167. else if (0 == strcmp ("publish-context2", event->value.publish.cctx))
  168. {
  169. GNUNET_assert (0 ==
  170. strcmp ("publish-context-dir", event->value.publish.pctx));
  171. GNUNET_assert (FILESIZE == event->value.publish.size);
  172. GNUNET_assert (0 == event->value.publish.completed);
  173. GNUNET_assert (2 == event->value.publish.anonymity);
  174. }
  175. else if (0 == strcmp ("publish-context-dir", event->value.publish.cctx))
  176. {
  177. GNUNET_assert (0 == event->value.publish.completed);
  178. GNUNET_assert (3 == event->value.publish.anonymity);
  179. }
  180. else
  181. GNUNET_assert (0);
  182. break;
  183. case GNUNET_FS_STATUS_PUBLISH_STOPPED:
  184. consider_restart (event->status);
  185. if ((NULL != event->value.publish.cctx) &&
  186. (0 == strcmp ("publish-context-dir", event->value.publish.cctx)))
  187. GNUNET_assert (publish == event->value.publish.pc);
  188. break;
  189. default:
  190. printf ("Unexpected event: %d\n", event->status);
  191. break;
  192. }
  193. return ret;
  194. }
  195. static void
  196. run (void *cls,
  197. const struct GNUNET_CONFIGURATION_Handle *c,
  198. struct GNUNET_TESTING_Peer *peer)
  199. {
  200. const char *keywords[] = {
  201. "down_foo",
  202. "down_bar",
  203. };
  204. char *buf;
  205. struct GNUNET_CONTAINER_MetaData *meta;
  206. struct GNUNET_FS_Uri *kuri;
  207. struct GNUNET_FS_FileInformation *fi1;
  208. struct GNUNET_FS_FileInformation *fi2;
  209. struct GNUNET_FS_FileInformation *fidir;
  210. size_t i;
  211. struct GNUNET_FS_BlockOptions bo;
  212. cfg = c;
  213. fs = GNUNET_FS_start (cfg, "test-fs-publish-persistence", &progress_cb, NULL,
  214. GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
  215. GNUNET_assert (NULL != fs);
  216. fn1 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
  217. buf = GNUNET_malloc (FILESIZE);
  218. for (i = 0; i < FILESIZE; i++)
  219. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  220. (void) GNUNET_DISK_directory_remove (fn1);
  221. GNUNET_assert (GNUNET_OK ==
  222. GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
  223. GNUNET_DISK_PERM_USER_READ
  224. | GNUNET_DISK_PERM_USER_WRITE));
  225. GNUNET_free (buf);
  226. fn2 = GNUNET_DISK_mktemp ("gnunet-publish-test-dst");
  227. buf = GNUNET_malloc (FILESIZE);
  228. for (i = 0; i < FILESIZE; i++)
  229. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  230. (void) GNUNET_DISK_directory_remove (fn2);
  231. GNUNET_assert (GNUNET_OK ==
  232. GNUNET_DISK_fn_write (fn2, buf, FILESIZE,
  233. GNUNET_DISK_PERM_USER_READ
  234. | GNUNET_DISK_PERM_USER_WRITE));
  235. GNUNET_free (buf);
  236. meta = GNUNET_CONTAINER_meta_data_create ();
  237. kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
  238. bo.content_priority = 42;
  239. bo.anonymity_level = 1;
  240. bo.replication_level = 0;
  241. bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
  242. fi1 =
  243. GNUNET_FS_file_information_create_from_file (fs, "publish-context1", fn1,
  244. kuri, meta, GNUNET_YES, &bo);
  245. GNUNET_assert (NULL != fi1);
  246. bo.anonymity_level = 2;
  247. fi2 =
  248. GNUNET_FS_file_information_create_from_file (fs, "publish-context2", fn2,
  249. kuri, meta, GNUNET_YES, &bo);
  250. GNUNET_assert (NULL != fi2);
  251. bo.anonymity_level = 3;
  252. fidir =
  253. GNUNET_FS_file_information_create_empty_directory (fs,
  254. "publish-context-dir",
  255. kuri, meta, &bo, NULL);
  256. GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
  257. GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
  258. GNUNET_FS_uri_destroy (kuri);
  259. GNUNET_CONTAINER_meta_data_destroy (meta);
  260. GNUNET_assert (NULL != fidir);
  261. start = GNUNET_TIME_absolute_get ();
  262. GNUNET_FS_publish_start (fs, fidir, NULL, NULL, NULL,
  263. GNUNET_FS_PUBLISH_OPTION_NONE);
  264. GNUNET_assert (publish != NULL);
  265. }
  266. int
  267. main (int argc, char *argv[])
  268. {
  269. if (0 != GNUNET_TESTING_peer_run ("test-fs-publish-persistence",
  270. "test_fs_publish_data.conf",
  271. &run, NULL))
  272. return 1;
  273. return err;
  274. }
  275. /* end of test_fs_publish_persistence.c */