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