indentation
[oweals/gnunet.git] / src / fs / test_fs_directory.c
1 /*
2      This file is part of GNUnet.
3      (C) 2005, 2006, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file fs/test_fs_directory.c
23  * @brief Test for fs_directory.c
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include <extractor.h>
29 #include "gnunet_util_lib.h"
30 #include "gnunet_fs_service.h"
31 #include "fs.h"
32
33 #define ABORT() { fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); return 1; }
34
35 struct PCLS
36 {
37   struct GNUNET_FS_Uri **uri;
38   struct GNUNET_CONTAINER_MetaData **md;
39   unsigned int pos;
40   unsigned int max;
41 };
42
43 static void
44 processor (void *cls,
45            const char *filename,
46            const struct GNUNET_FS_Uri *uri,
47            const struct GNUNET_CONTAINER_MetaData *md,
48            size_t length, const void *data)
49 {
50   struct PCLS *p = cls;
51   int i;
52
53   if (NULL == uri)
54     return;                     /* ignore directory's meta data */
55   for (i = 0; i < p->max; i++)
56   {
57     if (GNUNET_CONTAINER_meta_data_test_equal (p->md[i],
58                                                md) &&
59         GNUNET_FS_uri_test_equal (p->uri[i], uri))
60     {
61       p->pos++;
62       return;
63     }
64   }
65   fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__);
66 }
67
68 static int
69 testDirectory (unsigned int i)
70 {
71   struct GNUNET_FS_DirectoryBuilder *db;
72   char *data;
73   size_t dlen;
74   struct GNUNET_FS_Uri **uris;
75   struct GNUNET_CONTAINER_MetaData **mds;
76   struct GNUNET_CONTAINER_MetaData *meta;
77   struct PCLS cls;
78   char *emsg;
79   int p;
80   int q;
81   char uri[512];
82   char txt[128];
83   int ret = 0;
84   struct GNUNET_TIME_Absolute start;
85   char *s;
86
87   cls.max = i;
88   uris = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri *) * i);
89   mds = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_MetaData *) * i);
90   meta = GNUNET_CONTAINER_meta_data_create ();
91   GNUNET_CONTAINER_meta_data_insert (meta,
92                                      "<test>",
93                                      EXTRACTOR_METATYPE_TITLE,
94                                      EXTRACTOR_METAFORMAT_UTF8,
95                                      "text/plain",
96                                      "A title", strlen ("A title") + 1);
97   GNUNET_CONTAINER_meta_data_insert (meta,
98                                      "<test>",
99                                      EXTRACTOR_METATYPE_AUTHOR_NAME,
100                                      EXTRACTOR_METAFORMAT_UTF8,
101                                      "text/plain",
102                                      "An author", strlen ("An author") + 1);
103   for (p = 0; p < i; p++)
104   {
105     mds[p] = GNUNET_CONTAINER_meta_data_create ();
106     for (q = 0; q <= p; q++)
107     {
108       GNUNET_snprintf (txt, sizeof (txt), "%u -- %u\n", p, q);
109       GNUNET_CONTAINER_meta_data_insert (mds[p],
110                                          "<test>",
111                                          q % EXTRACTOR_metatype_get_max (),
112                                          EXTRACTOR_METAFORMAT_UTF8,
113                                          "text/plain", txt, strlen (txt) + 1);
114     }
115     GNUNET_snprintf (uri,
116                      sizeof (uri),
117                      "gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.%u",
118                      p);
119     emsg = NULL;
120     uris[p] = GNUNET_FS_uri_parse (uri, &emsg);
121     if (uris[p] == NULL)
122     {
123       GNUNET_CONTAINER_meta_data_destroy (mds[p]);
124       while (--p > 0)
125       {
126         GNUNET_CONTAINER_meta_data_destroy (mds[p]);
127         GNUNET_FS_uri_destroy (uris[p]);
128       }
129       GNUNET_free (mds);
130       GNUNET_free (uris);
131       GNUNET_free (emsg);
132       GNUNET_CONTAINER_meta_data_destroy (meta);
133       ABORT ();                 /* error in testcase */
134     }
135     GNUNET_assert (emsg == NULL);
136   }
137   start = GNUNET_TIME_absolute_get ();
138   db = GNUNET_FS_directory_builder_create (meta);
139   for (p = 0; p < i; p++)
140     GNUNET_FS_directory_builder_add (db, uris[p], mds[p], NULL);
141   GNUNET_FS_directory_builder_finish (db, &dlen, (void **) &data);
142   s = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration
143                                               (start));
144   fprintf (stdout,
145            "Creating directory with %u entires and total size %llu took %s\n",
146            i, (unsigned long long) dlen, s);
147   GNUNET_free (s);
148   if (i < 100)
149   {
150     cls.pos = 0;
151     cls.uri = uris;
152     cls.md = mds;
153     GNUNET_FS_directory_list_contents (dlen, data, 0, &processor, &cls);
154     GNUNET_assert (cls.pos == i);
155   }
156   GNUNET_free (data);
157   GNUNET_CONTAINER_meta_data_destroy (meta);
158   for (p = 0; p < i; p++)
159   {
160     GNUNET_CONTAINER_meta_data_destroy (mds[p]);
161     GNUNET_FS_uri_destroy (uris[p]);
162   }
163   GNUNET_free (uris);
164   GNUNET_free (mds);
165   return ret;
166 }
167
168
169 int
170 main (int argc, char *argv[])
171 {
172   int failureCount = 0;
173   int i;
174
175   GNUNET_log_setup ("test_fs_directory",
176 #if VERBOSE
177                     "DEBUG",
178 #else
179                     "WARNING",
180 #endif
181                     NULL);
182   for (i = 17; i < 1000; i *= 2)
183     failureCount += testDirectory (i);
184   if (failureCount != 0)
185     return 1;
186   return 0;
187 }
188
189 /* end of test_fs_directory.c */