test_fs_file_information.c 5.5 KB

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