2 This file is part of GNUnet.
3 Copyright (C) 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.
16 * @file datacache/test_datacache.c
17 * @brief Test for the datacache implementations.
21 #include "gnunet_util_lib.h"
22 #include "gnunet_datacache_lib.h"
23 #include "gnunet_testing_lib.h"
25 #define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0)
30 * Name of plugin under test.
32 static const char *plugin_name;
37 const struct GNUNET_HashCode *key,
38 size_t size, const char *data,
39 enum GNUNET_BLOCK_Type type,
40 struct GNUNET_TIME_Absolute exp,
41 unsigned int path_len,
42 const struct GNUNET_PeerIdentity *path)
44 if (size != sizeof (struct GNUNET_HashCode))
49 if (0 != memcmp (data, cls, size))
59 run (void *cls, char *const *args, const char *cfgfile,
60 const struct GNUNET_CONFIGURATION_Handle *cfg)
62 struct GNUNET_DATACACHE_Handle *h;
63 struct GNUNET_HashCode k;
64 struct GNUNET_HashCode n;
65 struct GNUNET_TIME_Absolute exp;
69 h = GNUNET_DATACACHE_create (cfg, "testcache");
74 "Failed to initialize datacache. Database likely not setup, skipping test.\n");
75 ok = 77; /* mark test as skipped */
78 exp = GNUNET_TIME_absolute_get ();
79 exp.abs_value_us += 5 * 60 * 1000 * 1000LL;
80 memset (&k, 0, sizeof (struct GNUNET_HashCode));
81 for (i = 0; i < 100; i++)
83 GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
85 GNUNET_DATACACHE_put (h,
88 sizeof (struct GNUNET_HashCode),
89 (const char *) &n, 1 + i % 16, exp,
93 memset (&k, 0, sizeof (struct GNUNET_HashCode));
94 for (i = 0; i < 100; i++)
96 GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
97 ASSERT (1 == GNUNET_DATACACHE_get (h, &k, 1 + i % 16, &checkIt, &n));
101 memset (&k, 42, sizeof (struct GNUNET_HashCode));
102 GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
104 GNUNET_DATACACHE_put (h,
107 sizeof (struct GNUNET_HashCode),
108 (const char *) &n, 792,
109 GNUNET_TIME_UNIT_FOREVER_ABS,
111 ASSERT (0 != GNUNET_DATACACHE_get (h, &k, 792, &checkIt, &n));
113 GNUNET_DATACACHE_destroy (h);
118 GNUNET_DATACACHE_destroy (h);
124 main (int argc, char *argv[])
127 char *const xargv[] = {
133 struct GNUNET_GETOPT_CommandLineOption options[] = {
134 GNUNET_GETOPT_OPTION_END
137 GNUNET_log_setup ("test-datacache",
140 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
141 GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_datacache_data_%s.conf",
143 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
144 "test-datacache", "nohelp", options, &run, NULL);
145 if ( (0 != ok) && (77 != ok) )
146 FPRINTF (stderr, "Missed some testcases: %d\n", ok);
150 /* end of test_datacache.c */