0201df3b3610b29ec138d2063eb8de68b14d5904
[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__, __LINE__); goto FAILURE;} } while (0)
31
32 static int ok;
33
34 /**
35  * Name of plugin under test.
36  */
37 static const char *plugin_name;
38
39 /**
40  * Quota is 1 MB.  Each iteration of the test puts in about 1 MB of
41  * data.  We do 10 iterations. Afterwards we check that the data from
42  * the first 5 iterations has all been discarded and that at least
43  * some of the data from the last iteration is still there.
44  */
45 static void
46 run (void *cls,
47      char *const *args,
48      const char *cfgfile,
49      const struct GNUNET_CONFIGURATION_Handle *cfg)
50 {
51   struct GNUNET_DATACACHE_Handle *h;
52   struct GNUNET_HashCode k;
53   struct GNUNET_HashCode n;
54   char buf[3200];
55   struct GNUNET_TIME_Absolute exp;
56
57   (void) cls;
58   (void) args;
59   (void) cfgfile;
60   ok = 0;
61   h = GNUNET_DATACACHE_create (cfg,
62                                "testcache");
63
64   if (h == NULL)
65   {
66     fprintf (stderr,
67              "%s",
68              "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
69     return;
70   }
71   exp = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
72   memset (buf, 1, sizeof (buf));
73   memset (&k, 0, sizeof (struct GNUNET_HashCode));
74   for (unsigned int i = 0; i < 10; i++)
75   {
76     fprintf (stderr,
77              "%s",
78              ".");
79     GNUNET_CRYPTO_hash (&k,
80                         sizeof (struct GNUNET_HashCode),
81                         &n);
82     for (unsigned int j = i; j < sizeof (buf); j += 10)
83     {
84       exp.abs_value_us++;
85       buf[j] = i;
86       ASSERT (GNUNET_OK ==
87               GNUNET_DATACACHE_put (h,
88                                     &k,
89                                     GNUNET_YES,
90                                     j,
91                                     buf,
92                                     1 + i,
93                                     exp,
94                                     0,
95                                     NULL));
96       ASSERT (0 < GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
97     }
98     k = n;
99   }
100   fprintf (stderr, "%s",  "\n");
101   memset (&k, 0, sizeof (struct GNUNET_HashCode));
102   for (unsigned int i = 0; i < 10; i++)
103   {
104     fprintf (stderr, "%s",  ".");
105     GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
106     if (i < 2)
107       ASSERT (0 == GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
108     if (i == 9)
109       ASSERT (0 < GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
110     k = n;
111   }
112   fprintf (stderr, "%s",  "\n");
113   GNUNET_DATACACHE_destroy (h);
114   return;
115 FAILURE:
116   if (h != NULL)
117     GNUNET_DATACACHE_destroy (h);
118   ok = GNUNET_SYSERR;
119 }
120
121
122 int
123 main (int argc,
124       char *argv[])
125 {
126   char cfg_name[PATH_MAX];
127   char *const xargv[] = {
128     "test-datacache-quota",
129     "-c",
130     cfg_name,
131     NULL
132   };
133   struct GNUNET_GETOPT_CommandLineOption options[] = {
134     GNUNET_GETOPT_OPTION_END
135   };
136
137   (void) argc;
138   GNUNET_log_setup ("test-datacache-quota",
139                     "WARNING",
140                     NULL);
141
142   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
143   GNUNET_snprintf (cfg_name,
144                    sizeof (cfg_name),
145                    "test_datacache_data_%s.conf",
146                    plugin_name);
147   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
148                       xargv,
149                       "test-datacache-quota",
150                       "nohelp",
151                       options,
152                       &run,
153                       NULL);
154   if (0 != ok)
155     fprintf (stderr,
156              "Missed some testcases: %d\n",
157              ok);
158   return ok;
159 }
160
161 /* end of test_datacache_quota.c */