remove 'illegal' (non-reentrant) log logic from signal handler
[oweals/gnunet.git] / src / datacache / test_datacache_quota.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2006, 2009, 2010 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
34 static int ok;
35
36 /**
37  * Name of plugin under test.
38  */
39 static const 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  */
47 static void
48 run (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   char buf[3200];
57   struct GNUNET_TIME_Absolute exp;
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     return;
72   }
73   exp = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
74   memset (buf, 1, sizeof(buf));
75   memset (&k, 0, sizeof(struct GNUNET_HashCode));
76   for (unsigned int i = 0; i < 10; i++)
77   {
78     fprintf (stderr,
79              "%s",
80              ".");
81     GNUNET_CRYPTO_hash (&k,
82                         sizeof(struct GNUNET_HashCode),
83                         &n);
84     for (unsigned int j = i; j < sizeof(buf); j += 10)
85     {
86       exp.abs_value_us++;
87       buf[j] = i;
88       ASSERT (GNUNET_OK ==
89               GNUNET_DATACACHE_put (h,
90                                     &k,
91                                     GNUNET_YES,
92                                     j,
93                                     buf,
94                                     1 + i,
95                                     exp,
96                                     0,
97                                     NULL));
98       ASSERT (0 < GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
99     }
100     k = n;
101   }
102   fprintf (stderr, "%s", "\n");
103   memset (&k, 0, sizeof(struct GNUNET_HashCode));
104   for (unsigned int i = 0; i < 10; i++)
105   {
106     fprintf (stderr, "%s", ".");
107     GNUNET_CRYPTO_hash (&k, sizeof(struct GNUNET_HashCode), &n);
108     if (i < 2)
109       ASSERT (0 == GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
110     if (i == 9)
111       ASSERT (0 < GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
112     k = n;
113   }
114   fprintf (stderr, "%s", "\n");
115   GNUNET_DATACACHE_destroy (h);
116   return;
117 FAILURE:
118   if (h != NULL)
119     GNUNET_DATACACHE_destroy (h);
120   ok = GNUNET_SYSERR;
121 }
122
123
124 int
125 main (int argc,
126       char *argv[])
127 {
128   char cfg_name[PATH_MAX];
129   char *const xargv[] = {
130     "test-datacache-quota",
131     "-c",
132     cfg_name,
133     NULL
134   };
135   struct GNUNET_GETOPT_CommandLineOption options[] = {
136     GNUNET_GETOPT_OPTION_END
137   };
138
139   (void) argc;
140   GNUNET_log_setup ("test-datacache-quota",
141                     "WARNING",
142                     NULL);
143
144   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
145   GNUNET_snprintf (cfg_name,
146                    sizeof(cfg_name),
147                    "test_datacache_data_%s.conf",
148                    plugin_name);
149   GNUNET_PROGRAM_run ((sizeof(xargv) / sizeof(char *)) - 1,
150                       xargv,
151                       "test-datacache-quota",
152                       "nohelp",
153                       options,
154                       &run,
155                       NULL);
156   if (0 != ok)
157     fprintf (stderr,
158              "Missed some testcases: %d\n",
159              ok);
160   return ok;
161 }
162
163
164 /* end of test_datacache_quota.c */