-fix
[oweals/gnunet.git] / src / datacache / test_datacache_quota.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2009, 2010 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
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
29 #define VERBOSE GNUNET_NO
30
31 #define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0)
32
33 static int ok;
34
35 /**
36  * Name of plugin under test.
37  */
38 static const char *plugin_name;
39
40 /**
41  * Quota is 1 MB.  Each iteration of the test puts in about 1 MB of
42  * data.  We do 10 iterations. Afterwards we check that the data from
43  * the first 5 iterations has all been discarded and that at least
44  * some of the data from the last iteration is still there.
45  */
46 static void
47 run (void *cls, char *const *args, const char *cfgfile,
48      const struct GNUNET_CONFIGURATION_Handle *cfg)
49 {
50   struct GNUNET_DATACACHE_Handle *h;
51   GNUNET_HashCode k;
52   GNUNET_HashCode n;
53   unsigned int i;
54   unsigned int j;
55   char buf[3200];
56   struct GNUNET_TIME_Absolute exp;
57
58   ok = 0;
59   h = GNUNET_DATACACHE_create (cfg, "testcache");
60
61   if (h == NULL)
62   {
63     FPRINTF (stderr, "%s", "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
64     return;
65   }
66   exp = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
67   memset (buf, 1, sizeof (buf));
68   memset (&k, 0, sizeof (GNUNET_HashCode));
69   for (i = 0; i < 10; i++)
70   {
71     FPRINTF (stderr, "%s",  ".");
72     GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
73     for (j = i; j < sizeof (buf); j += 10)
74     {
75       exp.abs_value++;
76       buf[j] = i;
77       ASSERT (GNUNET_OK == GNUNET_DATACACHE_put (h, &k, j, buf, 1 + i, exp));
78       ASSERT (0 < GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
79     }
80     k = n;
81   }
82   FPRINTF (stderr, "%s",  "\n");
83   memset (&k, 0, sizeof (GNUNET_HashCode));
84   for (i = 0; i < 10; i++)
85   {
86     FPRINTF (stderr, "%s",  ".");
87     GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
88     if (i < 2)
89       ASSERT (0 == GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
90     if (i == 9)
91       ASSERT (0 < GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
92     k = n;
93   }
94   FPRINTF (stderr, "%s",  "\n");
95   GNUNET_DATACACHE_destroy (h);
96   return;
97 FAILURE:
98   if (h != NULL)
99     GNUNET_DATACACHE_destroy (h);
100   ok = GNUNET_SYSERR;
101 }
102
103
104 int
105 main (int argc, char *argv[])
106 {
107   char *pos;
108   char cfg_name[128];
109
110   char *const xargv[] = {
111     "test-datacache-quota",
112     "-c",
113     cfg_name,
114 #if VERBOSE
115     "-L", "DEBUG",
116 #endif
117     NULL
118   };
119   struct GNUNET_GETOPT_CommandLineOption options[] = {
120     GNUNET_GETOPT_OPTION_END
121   };
122
123   GNUNET_log_setup ("test-datacache-quota",
124 #if VERBOSE
125                     "DEBUG",
126 #else
127                     "WARNING",
128 #endif
129                     NULL);
130
131   /* determine name of plugin to use */
132   plugin_name = argv[0];
133   while (NULL != (pos = strstr (plugin_name, "_")))
134     plugin_name = pos + 1;
135   if (NULL != (pos = strstr (plugin_name, ".")))
136     pos[0] = 0;
137   else
138     pos = (char *) plugin_name;
139
140   GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_datacache_data_%s.conf",
141                    plugin_name);
142   if (pos != plugin_name)
143     pos[0] = '.';
144   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
145                       "test-datacache-quota", "nohelp", options, &run, NULL);
146   if (ok != 0)
147     FPRINTF (stderr, "Missed some testcases: %d\n", ok);
148   return ok;
149 }
150
151 /* end of test_datacache_quota.c */