indentation
[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, const char *data, enum GNUNET_BLOCK_Type type)
46 {
47   if (size != sizeof (GNUNET_HashCode))
48   {
49     printf ("ERROR: Invalid size\n");
50     ok = 2;
51   }
52   if (0 != memcmp (data, cls, size))
53   {
54     printf ("ERROR: Invalid data\n");
55     ok = 3;
56   }
57   return GNUNET_OK;
58 }
59
60
61 static void
62 run (void *cls,
63      char *const *args,
64      const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
65 {
66   struct GNUNET_DATACACHE_Handle *h;
67   GNUNET_HashCode k;
68   GNUNET_HashCode n;
69   struct GNUNET_TIME_Absolute exp;
70   unsigned int i;
71
72   ok = 0;
73   h = GNUNET_DATACACHE_create (cfg, "testcache");
74   if (h == NULL)
75   {
76     fprintf (stderr,
77              "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
78     return;
79   }
80   exp = GNUNET_TIME_absolute_get ();
81   exp.abs_value += 5 * 60 * 1000;
82   memset (&k, 0, sizeof (GNUNET_HashCode));
83   for (i = 0; i < 100; i++)
84   {
85     GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
86     ASSERT (GNUNET_OK == GNUNET_DATACACHE_put (h,
87                                                &k,
88                                                sizeof (GNUNET_HashCode),
89                                                (const char *) &n,
90                                                1 + i % 16, exp));
91     k = n;
92   }
93   memset (&k, 0, sizeof (GNUNET_HashCode));
94   for (i = 0; i < 100; i++)
95   {
96     GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
97     ASSERT (1 == GNUNET_DATACACHE_get (h, &k, 1 + i % 16, &checkIt, &n));
98     k = n;
99   }
100
101   memset (&k, 42, sizeof (GNUNET_HashCode));
102   GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
103   ASSERT (GNUNET_OK == GNUNET_DATACACHE_put (h,
104                                              &k,
105                                              sizeof (GNUNET_HashCode),
106                                              (const char *) &n,
107                                              792,
108                                              GNUNET_TIME_UNIT_FOREVER_ABS));
109   ASSERT (0 != GNUNET_DATACACHE_get (h, &k, 792, &checkIt, &n));
110
111   GNUNET_DATACACHE_destroy (h);
112   ASSERT (ok == 0);
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
127   char *const xargv[] = {
128     "test-datacache",
129     "-c",
130     cfg_name,
131 #if VERBOSE
132     "-L", "DEBUG",
133 #endif
134     NULL
135   };
136   struct GNUNET_GETOPT_CommandLineOption options[] = {
137     GNUNET_GETOPT_OPTION_END
138   };
139
140   GNUNET_log_setup ("test-datacache",
141 #if VERBOSE
142                     "DEBUG",
143 #else
144                     "WARNING",
145 #endif
146                     NULL);
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", plugin_name);
159   if (pos != plugin_name)
160     pos[0] = '.';
161   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
162                       xargv, "test-datacache", "nohelp", options, &run, NULL);
163   if (ok != 0)
164     fprintf (stderr, "Missed some testcases: %d\n", ok);
165   return ok;
166 }
167
168 /* end of test_datacache.c */