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