test_fs_unindex.c 7.5 KB

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