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