2 This file is part of GNUnet.
3 Copyright (C) 2003, 2004, 2006, 2009, 2010 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/test_container_meta_data.c
23 * @brief Test for container_meta_data.c
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
30 #define ABORT(m) { fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); if (m != NULL) GNUNET_CONTAINER_meta_data_destroy(m); return 1; }
36 struct GNUNET_CONTAINER_MetaData *m;
42 m = GNUNET_CONTAINER_meta_data_create ();
44 GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_TITLE,
45 EXTRACTOR_METAFORMAT_UTF8,
46 "text/plain", "TestTitle",
47 strlen ("TestTitle") + 1))
50 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
51 EXTRACTOR_METATYPE_AUTHOR_NAME,
52 EXTRACTOR_METAFORMAT_UTF8,
53 "text/plain", "TestTitle",
54 strlen ("TestTitle") + 1))
56 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_TITLE, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */
58 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_insert (m, "<test>", EXTRACTOR_METATYPE_AUTHOR_NAME, EXTRACTOR_METAFORMAT_UTF8, "text/plain", "TestTitle", strlen ("TestTitle") + 1)) /* dup! */
60 if (2 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
63 GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_AUTHOR_NAME,
64 "TestTitle", strlen ("TestTitle") + 1))
66 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_AUTHOR_NAME, "TestTitle", strlen ("TestTitle") + 1)) /* already gone */
68 if (1 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
71 GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_TITLE,
72 "TestTitle", strlen ("TestTitle") + 1))
74 if (GNUNET_OK == GNUNET_CONTAINER_meta_data_delete (m, EXTRACTOR_METATYPE_TITLE, "TestTitle", strlen ("TestTitle") + 1)) /* already gone */
76 if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
78 for (j = 0; j < i; j++)
80 GNUNET_snprintf (val, sizeof (val), "%s.%d",
81 "A teststring that should compress well.", j);
83 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
84 EXTRACTOR_METATYPE_UNKNOWN,
85 EXTRACTOR_METAFORMAT_UTF8,
86 "text/plain", val, strlen (val) + 1))
89 if (i != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
92 size = GNUNET_CONTAINER_meta_data_get_serialized_size (m);
95 GNUNET_CONTAINER_meta_data_serialize (m, &sval, size,
96 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL))
98 GNUNET_free_non_null (sval);
101 GNUNET_CONTAINER_meta_data_destroy (m);
102 m = GNUNET_CONTAINER_meta_data_deserialize (sval, size);
106 for (j = 0; j < i; j++)
108 GNUNET_snprintf (val,
111 "A teststring that should compress well.",
114 GNUNET_CONTAINER_meta_data_delete (m,
115 EXTRACTOR_METATYPE_UNKNOWN,
122 if (0 != GNUNET_CONTAINER_meta_data_iterate (m, NULL, NULL))
124 GNUNET_CONTAINER_meta_data_destroy (m);
132 struct GNUNET_CONTAINER_MetaData *meta;
136 unsigned long long size;
138 meta = GNUNET_CONTAINER_meta_data_create ();
139 for (q = 0; q <= i; q++)
141 GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q);
142 GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
143 q % 42 /* EXTRACTOR_metatype_get_max () */,
144 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
145 txt, strlen (txt) + 1);
147 size = GNUNET_CONTAINER_meta_data_get_serialized_size (meta);
148 data = GNUNET_malloc (size * 4);
150 GNUNET_CONTAINER_meta_data_serialize (meta, &data, size * 4,
151 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL))
156 GNUNET_CONTAINER_meta_data_destroy (meta);
165 struct GNUNET_CONTAINER_MetaData *m;
169 m = GNUNET_CONTAINER_meta_data_create ();
171 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
172 EXTRACTOR_METATYPE_UNKNOWN,
173 EXTRACTOR_METAFORMAT_UTF8,
174 "text/plain", "link",
175 strlen ("link") + 1))
178 GNUNET_CONTAINER_meta_data_insert (m, "<test>",
179 EXTRACTOR_METATYPE_FILENAME,
180 EXTRACTOR_METAFORMAT_UTF8,
181 "text/plain", "lib-link.m4",
182 strlen ("lib-link.m4") + 1))
186 GNUNET_CONTAINER_meta_data_serialize (m, &val, (size_t) - 1,
187 GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL);
188 GNUNET_CONTAINER_meta_data_destroy (m);
189 m = GNUNET_CONTAINER_meta_data_deserialize (val, size);
193 GNUNET_CONTAINER_meta_data_destroy (m);
201 struct GNUNET_CONTAINER_MetaData *meta;
202 struct GNUNET_CONTAINER_MetaData *meta2;
207 unsigned char *thumb;
209 meta = GNUNET_CONTAINER_meta_data_create ();
210 meta2 = GNUNET_CONTAINER_meta_data_create ();
211 for (q = 0; q <= i; q++)
213 GNUNET_snprintf (txt, 128, "%u -- %u\n", i, q);
214 GNUNET_CONTAINER_meta_data_insert (meta, "<test>",
215 EXTRACTOR_METATYPE_UNKNOWN,
216 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
217 "TestTitle", strlen ("TestTitle") + 1);
218 GNUNET_CONTAINER_meta_data_insert (meta2, "<test>",
219 EXTRACTOR_METATYPE_UNKNOWN,
220 EXTRACTOR_METAFORMAT_UTF8, "text/plain",
221 "TestTitle", strlen ("TestTitle") + 1);
224 //check meta_data_test_equal
225 if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal (meta, meta2))
227 GNUNET_CONTAINER_meta_data_destroy (meta2);
231 //check meta_data_clear
232 GNUNET_CONTAINER_meta_data_clear (meta2);
233 if (0 != GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL))
235 GNUNET_CONTAINER_meta_data_destroy (meta2);
238 // check equal branch in meta_data_test_equal
239 if (GNUNET_YES != GNUNET_CONTAINER_meta_data_test_equal (meta, meta))
241 GNUNET_CONTAINER_meta_data_destroy (meta2);
244 // check "count" branch in meta_data_test_equal
245 if (GNUNET_NO != GNUNET_CONTAINER_meta_data_test_equal (meta, meta2))
247 GNUNET_CONTAINER_meta_data_destroy (meta2);
251 // check meta_data_add_publication_date
252 GNUNET_CONTAINER_meta_data_add_publication_date (meta2);
254 // check meta_data_merge
255 GNUNET_CONTAINER_meta_data_clear (meta2);
256 GNUNET_CONTAINER_meta_data_merge (meta2, meta);
257 if (100 == GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL))
259 GNUNET_CONTAINER_meta_data_destroy (meta2);
263 // check meta_data_get_by_type
264 GNUNET_CONTAINER_meta_data_clear (meta2);
267 GNUNET_CONTAINER_meta_data_get_by_type (meta2,
268 EXTRACTOR_METATYPE_UNKNOWN)))
270 GNUNET_CONTAINER_meta_data_destroy (meta2);
276 GNUNET_CONTAINER_meta_data_get_by_type (meta, EXTRACTOR_METATYPE_UNKNOWN);
277 GNUNET_assert (NULL != str);
280 GNUNET_CONTAINER_meta_data_destroy (meta2);
289 GNUNET_CONTAINER_meta_data_get_by_type (meta,
290 EXTRACTOR_METATYPE_PUBLICATION_DATE)))
293 GNUNET_CONTAINER_meta_data_destroy (meta2);
297 //check meta_data_get_first_by_types
299 GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
300 EXTRACTOR_METATYPE_UNKNOWN,
302 GNUNET_assert (NULL != str);
305 GNUNET_CONTAINER_meta_data_destroy (meta2);
311 //check meta_data_get_thumbnail
312 if (GNUNET_CONTAINER_meta_data_get_thumbnail (meta, &thumb) != 0)
315 GNUNET_CONTAINER_meta_data_destroy (meta2);
318 GNUNET_CONTAINER_meta_data_destroy (meta2);
319 //check meta_data_duplicate
320 meta2 = GNUNET_CONTAINER_meta_data_duplicate (meta);
321 if (200 == GNUNET_CONTAINER_meta_data_iterate (meta2, NULL, NULL))
323 GNUNET_CONTAINER_meta_data_destroy (meta2);
326 GNUNET_CONTAINER_meta_data_destroy (meta2);
327 GNUNET_CONTAINER_meta_data_destroy (meta);
333 main (int argc, char *argv[])
335 int failureCount = 0;
338 GNUNET_log_setup ("test-container-meta-data", "WARNING", NULL);
339 for (i = 0; i < 255; i++)
340 failureCount += testMeta (i);
341 for (i = 1; i < 255; i++)
342 failureCount += testMetaMore (i);
343 failureCount += testMetaLink ();
350 if (failureCount != 0)
356 /* end of test_container_meta_data.c */