d643e6ed9799a5ee04a233c60919a33b4bbd68fa
[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   for (i = 0; i < p->max; i++)
55     {
56       if (GNUNET_CONTAINER_meta_data_test_equal (p->md[i],
57                                                  md) &&
58           GNUNET_FS_uri_test_equal (p->uri[i], uri))
59         {
60           p->pos++;
61           return;
62         }
63     }
64   fprintf (stderr, "Error at %s:%d\n", __FILE__, __LINE__);
65 }
66
67 static int
68 testDirectory (unsigned int i)
69 {
70   char *data;
71   unsigned long long dlen;
72   struct GNUNET_FS_Uri **uris;
73   struct GNUNET_CONTAINER_MetaData **mds;
74   struct GNUNET_CONTAINER_MetaData *meta;
75   struct PCLS cls;
76   char *emsg;
77   int p;
78   int q;
79   char uri[512];
80   char txt[128];
81   int ret = 0;
82
83   cls.max = i;
84   uris = GNUNET_malloc (sizeof (struct GNUNET_FS_Uri*) * i);
85   mds = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_MetaData*) * i);
86   for (p = 0; p < i; p++)
87     {
88       mds[p] = GNUNET_CONTAINER_meta_data_create ();
89       for (q = 0; q <= p; q++)
90         {
91           GNUNET_snprintf (txt, sizeof(txt), "%u -- %u\n", p, q);
92           GNUNET_CONTAINER_meta_data_insert (mds[p],
93                                    q %
94                                    EXTRACTOR_getHighestKeywordTypeNumber
95                                    (), txt);
96         }
97       GNUNET_snprintf (uri,
98                        sizeof(uri),
99                        "gnunet://ecrs/chk/C282GG70GKK41O4551011DO413KFBVTVMQG1OG30I0K4045N0G41HAPB82G680A02JRVVFO8URVRU2F159011DO41000000022RG820.RNVVVVOOLCLK065B5D04HTNVNSIB2AI022RG8200HSLK1CO1000ATQ98824DMA2032LIMG50CG0K057NVUVG200000H000004400000.%u",
100                        p);
101       uris[p] = GNUNET_FS_uri_parse (uri, &emsg);
102       if (uris[p] == NULL)
103         {
104           GNUNET_CONTAINER_meta_data_destroy (mds[p]);
105           while (--p > 0)
106             {
107               GNUNET_CONTAINER_meta_data_destroy (mds[p]);
108               GNUNET_FS_uri_destroy (uris[p]);
109             }
110           GNUNET_free (mds);
111           GNUNET_free (uris);
112           ABORT ();             /* error in testcase */
113         }
114     }
115   meta = GNUNET_CONTAINER_meta_data_create ();
116   GNUNET_CONTAINER_meta_data_insert (meta, EXTRACTOR_TITLE, "A title");
117   GNUNET_CONTAINER_meta_data_insert (meta, EXTRACTOR_AUTHOR, "An author");
118   if (GNUNET_OK !=
119       GNUNET_FS_directory_create (&dlen, &data, i, uris, mds, meta))
120     {
121       GNUNET_CONTAINER_meta_data_destroy (meta);
122       for (p = 0; p < i; p++)
123         {
124           GNUNET_CONTAINER_meta_data_destroy (mds[p]);
125           GNUNET_FS_uri_destroy (uris[p]);
126         }
127       GNUNET_free (uris);
128       GNUNET_free (mds);
129       ABORT ();
130     }
131   cls.pos = 0;
132   cls.uri = uris;
133   cls.md = mds;
134   GNUNET_FS_directory_list_contents (dlen, data, 0, 
135                                      &processor, &cls);
136   GNUNET_assert (cls.pos == i);
137   GNUNET_free (data);
138   GNUNET_CONTAINER_meta_data_destroy (meta);
139   for (p = 0; p < i; p++)
140     {
141       GNUNET_CONTAINER_meta_data_destroy (mds[p]);
142       GNUNET_FS_uri_destroy (uris[p]);
143     }
144   GNUNET_free (uris);
145   GNUNET_free (mds);
146   return ret;
147 }
148
149 int
150 main (int argc, char *argv[])
151 {
152   int failureCount = 0;
153   int i;
154
155   for (i = 17; i < 2000; i *= 2)
156     {
157       fprintf (stderr, ".");
158       failureCount += testDirectory (i);
159     }
160   fprintf (stderr, "\n");
161
162   if (failureCount != 0)
163     return 1;
164   return 0;
165 }
166
167 /* end of test_fs_directory.c */