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