test_fs_file_information.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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_file_information.c
  18. * @brief simple testcase for file_information operations
  19. * @author Christian Grothoff
  20. *
  21. * TODO:
  22. * - test that metatdata, etc. are all correct (for example,
  23. * there is a known bug with dirname never being set that is
  24. * not detected!)
  25. * - need to iterate over file-information structure
  26. * - other API functions may not yet be tested (such as
  27. * filedata-from-callback)
  28. */
  29. #include "platform.h"
  30. #include "gnunet_util_lib.h"
  31. #include "gnunet_fs_service.h"
  32. /**
  33. * File-size we use for testing.
  34. */
  35. #define FILESIZE (1024 * 1024 * 2)
  36. /**
  37. * How long should our test-content live?
  38. */
  39. #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
  40. static int
  41. mycleaner (void *cls, struct GNUNET_FS_FileInformation *fi, uint64_t length,
  42. struct GNUNET_CONTAINER_MetaData *meta, struct GNUNET_FS_Uri **uri,
  43. struct GNUNET_FS_BlockOptions *bo, int *do_index, void **client_info)
  44. {
  45. return GNUNET_OK;
  46. }
  47. static void
  48. run (void *cls, char *const *args, const char *cfgfile,
  49. const struct GNUNET_CONFIGURATION_Handle *cfg)
  50. {
  51. const char *keywords[] = {
  52. "down_foo",
  53. "down_bar",
  54. };
  55. char *fn1;
  56. char *fn2;
  57. char *buf;
  58. struct GNUNET_CONTAINER_MetaData *meta;
  59. struct GNUNET_FS_Uri *kuri;
  60. struct GNUNET_FS_FileInformation *fi1;
  61. struct GNUNET_FS_FileInformation *fi2;
  62. struct GNUNET_FS_FileInformation *fidir;
  63. struct GNUNET_FS_Handle *fs;
  64. size_t i;
  65. struct GNUNET_FS_BlockOptions bo;
  66. fs = GNUNET_FS_start (cfg, "test-fs-file-information", NULL, NULL,
  67. GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
  68. fn1 = GNUNET_DISK_mktemp ("gnunet-file_information-test-dst");
  69. buf = GNUNET_malloc (FILESIZE);
  70. for (i = 0; i < FILESIZE; i++)
  71. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  72. GNUNET_assert (FILESIZE ==
  73. GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
  74. GNUNET_DISK_PERM_USER_READ
  75. | GNUNET_DISK_PERM_USER_WRITE));
  76. GNUNET_free (buf);
  77. fn2 = GNUNET_DISK_mktemp ("gnunet-file_information-test-dst");
  78. buf = GNUNET_malloc (FILESIZE);
  79. for (i = 0; i < FILESIZE; i++)
  80. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  81. GNUNET_assert (FILESIZE ==
  82. GNUNET_DISK_fn_write (fn2, buf, FILESIZE,
  83. GNUNET_DISK_PERM_USER_READ
  84. | GNUNET_DISK_PERM_USER_WRITE));
  85. GNUNET_free (buf);
  86. meta = GNUNET_CONTAINER_meta_data_create ();
  87. kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
  88. bo.content_priority = 42;
  89. bo.anonymity_level = 1;
  90. bo.replication_level = 0;
  91. bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
  92. fi1 =
  93. GNUNET_FS_file_information_create_from_file (fs,
  94. "file_information-context1",
  95. fn1, kuri, meta, GNUNET_YES,
  96. &bo);
  97. GNUNET_assert (fi1 != NULL);
  98. fi2 =
  99. GNUNET_FS_file_information_create_from_file (fs,
  100. "file_information-context2",
  101. fn2, kuri, meta, GNUNET_YES,
  102. &bo);
  103. GNUNET_assert (fi2 != NULL);
  104. fidir =
  105. GNUNET_FS_file_information_create_empty_directory (fs,
  106. "file_information-context-dir",
  107. kuri, meta, &bo, NULL);
  108. GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi1));
  109. GNUNET_assert (GNUNET_OK == GNUNET_FS_file_information_add (fidir, fi2));
  110. GNUNET_FS_uri_destroy (kuri);
  111. GNUNET_CONTAINER_meta_data_destroy (meta);
  112. GNUNET_assert (NULL != fidir);
  113. /* FIXME: test more of API! */
  114. GNUNET_FS_file_information_destroy (fidir, &mycleaner, NULL);
  115. GNUNET_DISK_directory_remove (fn1);
  116. GNUNET_DISK_directory_remove (fn2);
  117. GNUNET_free_non_null (fn1);
  118. GNUNET_free_non_null (fn2);
  119. GNUNET_FS_stop (fs);
  120. }
  121. int
  122. main (int argc, char *argv[])
  123. {
  124. char *const argvx[] = {
  125. "test-fs-file_information",
  126. "-c",
  127. "test_fs_file_information_data.conf",
  128. NULL
  129. };
  130. struct GNUNET_GETOPT_CommandLineOption options[] = {
  131. GNUNET_GETOPT_OPTION_END
  132. };
  133. GNUNET_log_setup ("test_fs_file_information",
  134. "WARNING",
  135. NULL);
  136. GNUNET_PROGRAM_run ((sizeof(argvx) / sizeof(char *)) - 1, argvx,
  137. "test-fs-file_information", "nohelp", options, &run,
  138. NULL);
  139. return 0;
  140. }
  141. /* end of test_fs_file_information.c */