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