31a9869f3e2713e7ff1a555ccc589905d2202359
[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 2, 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,
49            const void *data)
50 {
51   struct PCLS *p = cls;
52   int i;
53
54   if (NULL == uri)
55     return; /* ignore directory's meta data */
56   for (i = 0; i < p->max; i++)
57     {
58       if (GNUNET_CONTAINER_meta_data_test_equal (p->md[i],
59                                                  md) &&
60           GNUNET_FS_uri_test_equal (p->uri[i], uri))
61         {
62           p->pos++;
63           return;
64         }
65     }
66   fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__);
67 }
68
69 static int
70 testDirectory (unsigned int i)
71 {
72   struct GNUNET_FS_DirectoryBuilder *db;
73   char *data;
74   size_t dlen;
75   struct GNUNET_FS_Uri **uris;
76   struct GNUNET_CONTAINER_MetaData **mds;
77   struct GNUNET_CONTAINER_MetaData *meta;
78   struct PCLS cls;
79   char *emsg;
80   int p;
81   int q;
82   char uri[512];
83   char txt[128];
84   int ret = 0;
85   struct GNUNET_TIME_Absolute start;
86   char *s;
87
88   cls.max = i;
89   uris = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri*) * i);
90   mds = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_MetaData*) * i);
91   meta = GNUNET_CONTAINER_meta_data_create ();
92   GNUNET_CONTAINER_meta_data_insert (meta, EXTRACTOR_TITLE, "A title");
93   GNUNET_CONTAINER_meta_data_insert (meta, EXTRACTOR_AUTHOR, "An author");
94   db = GNUNET_FS_directory_builder_create (meta);
95   for (p = 0; p < i; p++)
96     {
97       mds[p] = GNUNET_CONTAINER_meta_data_create ();
98       for (q = 0; q <= p; q++)
99         {
100           GNUNET_snprintf (txt, sizeof(txt), "%u -- %u\n", p, q);
101           GNUNET_CONTAINER_meta_data_insert (mds[p],
102                                    q %
103                                    EXTRACTOR_getHighestKeywordTypeNumber
104                                    (), txt);
105         }
106       GNUNET_snprintf (uri,
107                        sizeof(uri),
108                        "gnunet://fs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.%u",
109                        p);
110       uris[p] = GNUNET_FS_uri_parse (uri, &emsg);
111       if (uris[p] == NULL)
112         {
113           GNUNET_CONTAINER_meta_data_destroy (mds[p]);
114           while (--p > 0)
115             {
116               GNUNET_CONTAINER_meta_data_destroy (mds[p]);
117               GNUNET_FS_uri_destroy (uris[p]);
118             }
119           GNUNET_free (mds);
120           GNUNET_free (uris);
121           ABORT ();             /* error in testcase */
122         }
123       GNUNET_FS_directory_builder_add (db, uris[p], mds[p], NULL);
124     }
125   start = GNUNET_TIME_absolute_get ();
126   GNUNET_FS_directory_builder_finish (db,
127                                       &dlen,
128                                       (void**) &data);
129   s = GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start));
130   fprintf (stdout,
131            "Creating directory with %u entires took %s\n",
132            i,
133            s);
134   GNUNET_free (s);
135   if (i < 1000)
136     {
137       cls.pos = 0;
138       cls.uri = uris;
139       cls.md = mds;
140       GNUNET_FS_directory_list_contents (dlen, data, 0, 
141                                          &processor, &cls);
142       GNUNET_assert (cls.pos == i);
143     }
144   GNUNET_free (data);
145   GNUNET_CONTAINER_meta_data_destroy (meta);
146   for (p = 0; p < i; p++)
147     {
148       GNUNET_CONTAINER_meta_data_destroy (mds[p]);
149       GNUNET_FS_uri_destroy (uris[p]);
150     }
151   GNUNET_free (uris);
152   GNUNET_free (mds);
153   return ret;
154 }
155
156
157 int
158 main (int argc, char *argv[])
159 {
160   int failureCount = 0;
161   int i;
162
163   GNUNET_log_setup ("test_fs_directory", 
164 #if VERBOSE
165                     "DEBUG",
166 #else
167                     "WARNING",
168 #endif
169                     NULL);
170   for (i = 17; i < 10000; i *= 2)
171     failureCount += testDirectory (i);    
172   fprintf (stderr, "\n");
173
174   if (failureCount != 0)
175     return 1;
176   return 0;
177 }
178
179 /* end of test_fs_directory.c */