test_fs_directory.c 5.5 KB

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