test_fs_list_indexed.c 8.4 KB

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