test_fs_directory.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2005, 2006, 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_directory.c
  18. * @brief Test for fs_directory.c
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #if HAVE_EXTRACTOR_H
  23. #include <extractor.h>
  24. #endif
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_fs_service.h"
  27. #include "fs_api.h"
  28. #define ABORT() { fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
  29. return 1; }
  30. struct PCLS
  31. {
  32. struct GNUNET_FS_Uri **uri;
  33. struct GNUNET_CONTAINER_MetaData **md;
  34. unsigned int pos;
  35. unsigned int max;
  36. };
  37. static void
  38. processor (void *cls, const char *filename, const struct GNUNET_FS_Uri *uri,
  39. const struct GNUNET_CONTAINER_MetaData *md, size_t length,
  40. const void *data)
  41. {
  42. struct PCLS *p = cls;
  43. int i;
  44. if (NULL == uri)
  45. return; /* ignore directory's meta data */
  46. for (i = 0; i < p->max; i++)
  47. {
  48. if (GNUNET_CONTAINER_meta_data_test_equal (p->md[i], md) &&
  49. GNUNET_FS_uri_test_equal (p->uri[i], uri))
  50. {
  51. p->pos++;
  52. return;
  53. }
  54. }
  55. fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__);
  56. }
  57. static int
  58. testDirectory (unsigned int i)
  59. {
  60. struct GNUNET_FS_DirectoryBuilder *db;
  61. char *data;
  62. size_t dlen;
  63. struct GNUNET_FS_Uri **uris;
  64. struct GNUNET_CONTAINER_MetaData **mds;
  65. struct GNUNET_CONTAINER_MetaData *meta;
  66. struct PCLS cls;
  67. char *emsg;
  68. int p;
  69. int q;
  70. char uri[512];
  71. char txt[128];
  72. int ret = 0;
  73. struct GNUNET_TIME_Absolute start;
  74. const char *s;
  75. cls.max = i;
  76. uris = GNUNET_malloc (sizeof(struct GNUNET_FS_Uri *) * i);
  77. mds = GNUNET_malloc (sizeof(struct GNUNET_CONTAINER_MetaData *) * i);
  78. meta = GNUNET_CONTAINER_meta_data_create ();
  79. GNUNET_CONTAINER_meta_data_insert (meta, "<test>", EXTRACTOR_METATYPE_TITLE,
  80. EXTRACTOR_METAFORMAT_UTF8, "text/plain",
  81. "A title", strlen ("A title") + 1);
  82. GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
  83. EXTRACTOR_METATYPE_AUTHOR_NAME,
  84. EXTRACTOR_METAFORMAT_UTF8, "text/plain",
  85. "An author", strlen ("An author") + 1);
  86. for (p = 0; p < i; p++)
  87. {
  88. mds[p] = GNUNET_CONTAINER_meta_data_create ();
  89. for (q = 0; q <= p; q++)
  90. {
  91. GNUNET_snprintf (txt, sizeof(txt), "%u -- %u\n", p, q);
  92. GNUNET_CONTAINER_meta_data_insert (mds[p], "<test>",
  93. #if HAVE_EXTRACTOR_H && HAVE_LIBEXTRACTOR
  94. q % EXTRACTOR_metatype_get_max (),
  95. #else
  96. q % 128,
  97. #endif
  98. EXTRACTOR_METAFORMAT_UTF8,
  99. "text/plain", txt, strlen (txt) + 1);
  100. }
  101. GNUNET_snprintf (uri, sizeof(uri),
  102. "gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.%u",
  103. p);
  104. emsg = NULL;
  105. uris[p] = GNUNET_FS_uri_parse (uri, &emsg);
  106. if (uris[p] == NULL)
  107. {
  108. GNUNET_CONTAINER_meta_data_destroy (mds[p]);
  109. while (--p > 0)
  110. {
  111. GNUNET_CONTAINER_meta_data_destroy (mds[p]);
  112. GNUNET_FS_uri_destroy (uris[p]);
  113. }
  114. GNUNET_free (mds);
  115. GNUNET_free (uris);
  116. GNUNET_free (emsg);
  117. GNUNET_CONTAINER_meta_data_destroy (meta);
  118. ABORT (); /* error in testcase */
  119. }
  120. GNUNET_assert (emsg == NULL);
  121. }
  122. start = GNUNET_TIME_absolute_get ();
  123. db = GNUNET_FS_directory_builder_create (meta);
  124. for (p = 0; p < i; p++)
  125. GNUNET_FS_directory_builder_add (db, uris[p], mds[p], NULL);
  126. GNUNET_FS_directory_builder_finish (db, &dlen, (void **) &data);
  127. s = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
  128. (start),
  129. GNUNET_YES);
  130. fprintf (stdout,
  131. "Creating directory with %u entries and total size %llu took %s\n",
  132. i, (unsigned long long) dlen, s);
  133. if (i < 100)
  134. {
  135. cls.pos = 0;
  136. cls.uri = uris;
  137. cls.md = mds;
  138. GNUNET_FS_directory_list_contents (dlen, data, 0, &processor, &cls);
  139. GNUNET_assert (cls.pos == i);
  140. }
  141. GNUNET_free (data);
  142. GNUNET_CONTAINER_meta_data_destroy (meta);
  143. for (p = 0; p < i; p++)
  144. {
  145. GNUNET_CONTAINER_meta_data_destroy (mds[p]);
  146. GNUNET_FS_uri_destroy (uris[p]);
  147. }
  148. GNUNET_free (uris);
  149. GNUNET_free (mds);
  150. return ret;
  151. }
  152. int
  153. main (int argc, char *argv[])
  154. {
  155. int failureCount = 0;
  156. int i;
  157. GNUNET_log_setup ("test_fs_directory",
  158. #if VERBOSE
  159. "DEBUG",
  160. #else
  161. "WARNING",
  162. #endif
  163. NULL);
  164. for (i = 17; i < 1000; i *= 2)
  165. failureCount += testDirectory (i);
  166. if (failureCount != 0)
  167. return 1;
  168. return 0;
  169. }
  170. /* end of test_fs_directory.c */