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,
48      char *const *args,
49      const char *cfgfile, 
50      const struct GNUNET_CONFIGURATION_Handle *cfg)
51 {
52   struct GNUNET_DATACACHE_Handle *h;
53   GNUNET_HashCode k;
54   GNUNET_HashCode n;
55   unsigned int i;
56   unsigned int j;
57   char buf[3200];
58   struct GNUNET_TIME_Absolute exp;
59
60   ok = 0;
61   h = GNUNET_DATACACHE_create (cfg,
62                                "testcache");
63
64   if (h == NULL)
65     {
66       fprintf (stderr,
67                "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
68       return;
69     }
70   exp = GNUNET_TIME_absolute_get ();
71   exp.abs_value += 20 * 60 * 1000;
72   memset (buf, 1, sizeof (buf));
73   memset (&k, 0, sizeof (GNUNET_HashCode));
74   for (i = 0; i < 10; i++)
75     {
76       fprintf (stderr, ".");
77       GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
78       for (j = i; j < sizeof (buf); j += 10)
79         {
80           exp.abs_value++;
81           buf[j] = i;
82           ASSERT (GNUNET_OK == 
83                   GNUNET_DATACACHE_put (h,
84                                         &k,
85                                         j,
86                                         buf,
87                                         1+i,
88                                         exp));
89           ASSERT (0 < GNUNET_DATACACHE_get (h, 
90                                             &k, 1+i, 
91                                             NULL, NULL));
92         }
93       k = n;
94     }
95   fprintf (stderr, "\n");
96   memset (&k, 0, sizeof (GNUNET_HashCode));
97   for (i = 0; i < 10; i++)
98     {
99       fprintf (stderr, ".");
100       GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
101       if (i < 2)
102         ASSERT (0 == GNUNET_DATACACHE_get  (h, 
103                                             &k, 1+i, 
104                                             NULL, NULL));
105       if (i == 9)
106         ASSERT (0 < GNUNET_DATACACHE_get  (h, 
107                                            &k, 1+i, 
108                                            NULL, NULL));
109       k = n;
110     }
111   fprintf (stderr, "\n");
112   GNUNET_DATACACHE_destroy (h);
113   return;
114 FAILURE:
115   if (h != NULL)
116     GNUNET_DATACACHE_destroy (h);
117   ok = GNUNET_SYSERR;
118 }
119
120
121 int
122 main (int argc, char *argv[])
123 {
124   char *pos;
125   char cfg_name[128];
126   char *const xargv[] = { 
127     "test-datacache-quota",
128     "-c",
129     cfg_name,
130 #if VERBOSE
131     "-L", "DEBUG",
132 #endif
133     NULL
134   };
135   struct GNUNET_GETOPT_CommandLineOption options[] = {
136     GNUNET_GETOPT_OPTION_END
137   };
138  
139   GNUNET_log_setup ("test-datacache-quota",
140 #if VERBOSE
141                     "DEBUG",
142 #else
143                     "WARNING",
144 #endif
145                     NULL);
146
147   /* determine name of plugin to use */
148   plugin_name = argv[0];
149   while (NULL != (pos = strstr(plugin_name, "_")))
150     plugin_name = pos+1;
151   if (NULL != (pos = strstr(plugin_name, ".")))
152     pos[0] = 0;
153   else
154     pos = (char *) plugin_name;
155   
156   GNUNET_snprintf (cfg_name,
157                    sizeof (cfg_name),
158                    "test_datacache_data_%s.conf",
159                    plugin_name);
160   if (pos != plugin_name)
161     pos[0] = '.';
162   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
163                       xargv, "test-datacache-quota", "nohelp",
164                       options, &run, NULL);
165   if (ok != 0)
166     fprintf (stderr, "Missed some testcases: %d\n", ok);
167   return ok;
168 }
169
170 /* end of test_datacache_quota.c */