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