test_fs_publish.c 7.9 KB

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