merge benchmark changes
[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
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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 #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, char *const *args, const char *cfgfile,
47      const struct GNUNET_CONFIGURATION_Handle *cfg)
48 {
49   struct GNUNET_DATACACHE_Handle *h;
50   struct GNUNET_HashCode k;
51   struct GNUNET_HashCode n;
52   unsigned int i;
53   unsigned int j;
54   char buf[3200];
55   struct GNUNET_TIME_Absolute exp;
56
57   ok = 0;
58   h = GNUNET_DATACACHE_create (cfg, "testcache");
59
60   if (h == NULL)
61   {
62     FPRINTF (stderr, "%s", "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
63     return;
64   }
65   exp = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS);
66   memset (buf, 1, sizeof (buf));
67   memset (&k, 0, sizeof (struct GNUNET_HashCode));
68   for (i = 0; i < 10; i++)
69   {
70     FPRINTF (stderr, "%s",  ".");
71     GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
72     for (j = i; j < sizeof (buf); j += 10)
73     {
74       exp.abs_value_us++;
75       buf[j] = i;
76       ASSERT (GNUNET_OK == GNUNET_DATACACHE_put (h,
77                                                  &k,
78                                                  GNUNET_YES,
79                                                  j,
80                                                  buf,
81                                                  1 + i,
82                                                  exp,
83                                                  0,
84                                                  NULL));
85       ASSERT (0 < GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
86     }
87     k = n;
88   }
89   FPRINTF (stderr, "%s",  "\n");
90   memset (&k, 0, sizeof (struct GNUNET_HashCode));
91   for (i = 0; i < 10; i++)
92   {
93     FPRINTF (stderr, "%s",  ".");
94     GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
95     if (i < 2)
96       ASSERT (0 == GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
97     if (i == 9)
98       ASSERT (0 < GNUNET_DATACACHE_get (h, &k, 1 + i, NULL, NULL));
99     k = n;
100   }
101   FPRINTF (stderr, "%s",  "\n");
102   GNUNET_DATACACHE_destroy (h);
103   return;
104 FAILURE:
105   if (h != NULL)
106     GNUNET_DATACACHE_destroy (h);
107   ok = GNUNET_SYSERR;
108 }
109
110
111 int
112 main (int argc, char *argv[])
113 {
114   char cfg_name[128];
115   char *const xargv[] = {
116     "test-datacache-quota",
117     "-c",
118     cfg_name,
119     NULL
120   };
121   struct GNUNET_GETOPT_CommandLineOption options[] = {
122     GNUNET_GETOPT_OPTION_END
123   };
124
125   GNUNET_log_setup ("test-datacache-quota",
126                     "WARNING",
127                     NULL);
128
129   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
130   GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_datacache_data_%s.conf",
131                    plugin_name);
132   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
133                       "test-datacache-quota", "nohelp", options, &run, NULL);
134   if (0 != ok)
135     FPRINTF (stderr, "Missed some testcases: %d\n", ok);
136   return ok;
137 }
138
139 /* end of test_datacache_quota.c */