aboutsummaryrefslogtreecommitdiff
path: root/src/service/datacache/test_datacache_quota.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/datacache/test_datacache_quota.c')
-rw-r--r--src/service/datacache/test_datacache_quota.c199
1 files changed, 199 insertions, 0 deletions
diff --git a/src/service/datacache/test_datacache_quota.c b/src/service/datacache/test_datacache_quota.c
new file mode 100644
index 000000000..6067b79fa
--- /dev/null
+++ b/src/service/datacache/test_datacache_quota.c
@@ -0,0 +1,199 @@
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_quota.c
22 * @brief Test for the quota code of 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 char *plugin_name;
40
41/**
42 * Quota is 1 MB. Each iteration of the test puts in about 1 MB of
43 * data. We do 10 iterations. Afterwards we check that the data from
44 * the first 5 iterations has all been discarded and that at least
45 * some of the data from the last iteration is still there.
46 */
47static void
48run (void *cls,
49 char *const *args,
50 const char *cfgfile,
51 const struct GNUNET_CONFIGURATION_Handle *cfg)
52{
53 struct GNUNET_DATACACHE_Handle *h;
54 struct GNUNET_HashCode k;
55 struct GNUNET_HashCode n;
56 struct GNUNET_DATACACHE_Block block;
57 char buf[3200];
58
59 (void) cls;
60 (void) args;
61 (void) cfgfile;
62 ok = 0;
63 h = GNUNET_DATACACHE_create (cfg,
64 "testcache");
65
66 if (h == NULL)
67 {
68 fprintf (stderr,
69 "%s",
70 "Failed to initialize datacache. Database likely not setup, skipping test.\n");
71 ok = 77;
72 return;
73 }
74 block.expiration_time = GNUNET_TIME_relative_to_absolute (
75 GNUNET_TIME_UNIT_HOURS);
76 memset (buf,
77 1,
78 sizeof(buf));
79 memset (&k,
80 0,
81 sizeof(struct GNUNET_HashCode));
82 for (unsigned int i = 0; i < 10; i++)
83 {
84 fprintf (stderr,
85 "%s",
86 ".");
87 GNUNET_CRYPTO_hash (&k,
88 sizeof(struct GNUNET_HashCode),
89 &n);
90 for (unsigned int j = i; j < sizeof(buf); j += 10)
91 {
92 buf[j] = i;
93 block.key = k;
94 block.data = buf;
95 block.data_size = j;
96 memset (&block.trunc_peer,
97 43,
98 sizeof (block.trunc_peer));
99 block.ro = 42;
100 block.type = (enum GNUNET_BLOCK_Type) (1 + i);
101 block.put_path = NULL;
102 block.put_path_length = 0;
103 block.ro = GNUNET_DHT_RO_RECORD_ROUTE;
104 block.expiration_time.abs_value_us++;
105 ASSERT (GNUNET_OK ==
106 GNUNET_DATACACHE_put (h,
107 42,
108 &block));
109 ASSERT (0 < GNUNET_DATACACHE_get (h,
110 &k,
111 1 + i,
112 NULL,
113 NULL));
114 }
115 k = n;
116 }
117 fprintf (stderr, "%s", "\n");
118 memset (&k,
119 0,
120 sizeof(struct GNUNET_HashCode));
121 for (unsigned int i = 0; i < 10; i++)
122 {
123 fprintf (stderr, "%s", ".");
124 GNUNET_CRYPTO_hash (&k,
125 sizeof(struct GNUNET_HashCode),
126 &n);
127 if (i < 2)
128 ASSERT (0 ==
129 GNUNET_DATACACHE_get (h,
130 &k,
131 1 + i,
132 NULL,
133 NULL));
134 if (i == 9)
135 ASSERT (0 < GNUNET_DATACACHE_get (h,
136 &k,
137 1 + i,
138 NULL,
139 NULL));
140 k = n;
141 }
142 fprintf (stderr,
143 "%s",
144 "\n");
145 GNUNET_DATACACHE_destroy (h);
146 return;
147FAILURE:
148 if (h != NULL)
149 GNUNET_DATACACHE_destroy (h);
150 ok = GNUNET_SYSERR;
151}
152
153
154int
155main (int argc,
156 char *argv[])
157{
158 char cfg_name[PATH_MAX];
159 char *const xargv[] = {
160 "test-datacache-quota",
161 "-c",
162 cfg_name,
163 NULL
164 };
165 struct GNUNET_GETOPT_CommandLineOption options[] = {
166 GNUNET_GETOPT_OPTION_END
167 };
168
169 (void) argc;
170 GNUNET_log_setup ("test-datacache-quota",
171 "WARNING",
172 NULL);
173
174 plugin_name = GNUNET_STRINGS_get_suffix_from_binary_name (argv[0]);
175 GNUNET_snprintf (cfg_name,
176 sizeof(cfg_name),
177 "test_datacache_data_%s.conf",
178 plugin_name);
179 if (GNUNET_OK != GNUNET_PROGRAM_run ((sizeof(xargv) / sizeof(char *)) - 1,
180 xargv,
181 "test-datacache-quota",
182 "nohelp",
183 options,
184 &run,
185 NULL))
186 {
187 GNUNET_free (plugin_name);
188 return 1;
189 }
190 if (0 != ok)
191 fprintf (stderr,
192 "Missed some testcases: %d\n",
193 ok);
194 GNUNET_free (plugin_name);
195 return ok;
196}
197
198
199/* end of test_datacache_quota.c */