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