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