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