aboutsummaryrefslogtreecommitdiff
path: root/src/service/datacache/test_datacache.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/datacache/test_datacache.c')
-rw-r--r--src/service/datacache/test_datacache.c210
1 files changed, 210 insertions, 0 deletions
diff --git a/src/service/datacache/test_datacache.c b/src/service/datacache/test_datacache.c
new file mode 100644
index 000000000..7b7270609
--- /dev/null
+++ b/src/service/datacache/test_datacache.c
@@ -0,0 +1,210 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2006, 2009, 2010, 2022 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 datacache/test_datacache.c
22 * @brief Test for the datacache implementations.
23 * @author Nils Durner
24 */
25#include "platform.h"
26#include "gnunet_util_lib.h"
27#include "gnunet_datacache_lib.h"
28#include "gnunet_testing_lib.h"
29
30#define ASSERT(x) do { if (! (x)) { printf ("Error at %s:%d\n", __FILE__, \
31 __LINE__); goto FAILURE; \
32 } } while (0)
33
34static int ok;
35
36/**
37 * Name of plugin under test.
38 */
39static const char *plugin_name;
40
41
42static enum GNUNET_GenericReturnValue
43checkIt (void *cls,
44 const struct GNUNET_DATACACHE_Block *block)
45{
46 if (block->data_size != sizeof(struct GNUNET_HashCode))
47 {
48 GNUNET_break (0);
49 ok = 2;
50 }
51 if (0 != memcmp (block->data,
52 cls,
53 block->data_size))
54 {
55 GNUNET_break (0);
56 ok = 3;
57 }
58 return GNUNET_OK;
59}
60
61
62static void
63run (void *cls,
64 char *const *args,
65 const char *cfgfile,
66 const struct GNUNET_CONFIGURATION_Handle *cfg)
67{
68 struct GNUNET_DATACACHE_Handle *h;
69 struct GNUNET_DATACACHE_Block block;
70 struct GNUNET_HashCode k;
71 struct GNUNET_HashCode n;
72
73 (void) cls;
74 (void) args;
75 (void) cfgfile;
76 ok = 0;
77 h = GNUNET_DATACACHE_create (cfg,
78 "testcache");
79 if (NULL == h)
80 {
81 fprintf (stderr,
82 "%s",
83 "Failed to initialize datacache. Database likely not setup, skipping test.\n");
84 ok = 77; /* mark test as skipped */
85 return;
86 }
87 block.expiration_time = GNUNET_TIME_absolute_get ();
88 block.expiration_time.abs_value_us += 5 * 60 * 1000 * 1000LL;
89 memset (&k,
90 0,
91 sizeof(struct GNUNET_HashCode));
92 for (unsigned int i = 0; i < 100; i++)
93 {
94 GNUNET_CRYPTO_hash (&k,
95 sizeof(struct GNUNET_HashCode),
96 &n);
97 block.key = k;
98 block.data = &n;
99 block.data_size = sizeof (n);
100 memset (&block.trunc_peer,
101 43,
102 sizeof (block.trunc_peer));
103 block.ro = 42;
104 block.type = (enum GNUNET_BLOCK_Type) (1 + i % 16);
105 block.put_path = NULL;
106 block.put_path_length = 0;
107 block.ro = GNUNET_DHT_RO_RECORD_ROUTE;
108 ASSERT (GNUNET_OK ==
109 GNUNET_DATACACHE_put (h,
110 42,
111 &block));
112 k = n;
113 }
114 memset (&k,
115 0,
116 sizeof(struct GNUNET_HashCode));
117 for (unsigned int i = 0; i < 100; i++)
118 {
119 GNUNET_CRYPTO_hash (&k,
120 sizeof(struct GNUNET_HashCode),
121 &n);
122 ASSERT (1 == GNUNET_DATACACHE_get (h,
123 &k,
124 (enum GNUNET_BLOCK_Type) (1 + i % 16),
125 &checkIt,
126 &n));
127 k = n;
128 }
129
130 memset (&k,
131 42,
132 sizeof(struct GNUNET_HashCode));
133 GNUNET_CRYPTO_hash (&k,
134 sizeof(struct GNUNET_HashCode),
135 &n);
136 block.key = k;
137 block.data = &n;
138 block.data_size = sizeof (n);
139 block.ro = 42;
140 memset (&block.trunc_peer,
141 44,
142 sizeof (block.trunc_peer));
143 block.type = (enum GNUNET_BLOCK_Type) 792;
144 block.put_path = NULL;
145 block.put_path_length = 0;
146 block.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS;
147 block.ro = GNUNET_DHT_RO_RECORD_ROUTE;
148 ASSERT (GNUNET_OK ==
149 GNUNET_DATACACHE_put (h,
150 42,
151 &block));
152 ASSERT (0 != GNUNET_DATACACHE_get (h,
153 &k,
154 792,
155 &checkIt,
156 &n));
157 GNUNET_DATACACHE_destroy (h);
158 ASSERT (ok == 0);
159 return;
160FAILURE:
161 if (h != NULL)
162 GNUNET_DATACACHE_destroy (h);
163 ok = GNUNET_SYSERR;
164}
165
166
167int
168main (int argc, char *argv[])
169{
170 char cfg_name[PATH_MAX];
171 char *const xargv[] = {
172 "test-datacache",
173 "-c",
174 cfg_name,
175 NULL
176 };
177 struct GNUNET_GETOPT_CommandLineOption options[] = {
178 GNUNET_GETOPT_OPTION_END
179 };
180
181 (void) argc;
182 GNUNET_log_setup ("test-datacache",
183 "WARNING",
184 NULL);
185 plugin_name = GNUNET_STRINGS_get_suffix_from_binary_name (argv[0]);
186 GNUNET_snprintf (cfg_name,
187 sizeof(cfg_name),
188 "test_datacache_data_%s.conf",
189 plugin_name);
190 if (GNUNET_OK != GNUNET_PROGRAM_run ((sizeof(xargv) / sizeof(char *)) - 1,
191 xargv,
192 "test-datacache",
193 "nohelp",
194 options,
195 &run,
196 NULL))
197 {
198 GNUNET_free (plugin_name);
199 return 1;
200 }
201 if ((0 != ok) && (77 != ok))
202 fprintf (stderr,
203 "Missed some testcases: %d\n",
204 ok);
205 GNUNET_free (plugin_name);
206 return ok;
207}
208
209
210/* end of test_datacache.c */