make fs tests build against new testbed library -- they do not pass yet
[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 #include "gnunet_testing_lib-new.h"
29
30 #define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0)
31
32 static int ok;
33
34 /**
35  * Name of plugin under test.
36  */
37 static const char *plugin_name;
38
39
40 static int
41 checkIt (void *cls, struct GNUNET_TIME_Absolute exp,
42          const struct GNUNET_HashCode * key, size_t size, const char *data,
43          enum GNUNET_BLOCK_Type type)
44 {
45   if (size != sizeof (struct GNUNET_HashCode))
46   {
47     GNUNET_break (0);
48     ok = 2;
49   }
50   if (0 != memcmp (data, cls, size))
51   {
52     GNUNET_break (0);
53     ok = 3;
54   }
55   return GNUNET_OK;
56 }
57
58
59 static void
60 run (void *cls, char *const *args, const char *cfgfile,
61      const struct GNUNET_CONFIGURATION_Handle *cfg)
62 {
63   struct GNUNET_DATACACHE_Handle *h;
64   struct GNUNET_HashCode k;
65   struct GNUNET_HashCode n;
66   struct GNUNET_TIME_Absolute exp;
67   unsigned int i;
68
69   ok = 0;
70   h = GNUNET_DATACACHE_create (cfg, "testcache");
71   if (h == NULL)
72   {
73     FPRINTF (stderr,
74              "%s", 
75              "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
76     return;
77   }
78   exp = GNUNET_TIME_absolute_get ();
79   exp.abs_value += 5 * 60 * 1000;
80   memset (&k, 0, sizeof (struct GNUNET_HashCode));
81   for (i = 0; i < 100; i++)
82   {
83     GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
84     ASSERT (GNUNET_OK ==
85             GNUNET_DATACACHE_put (h, &k, sizeof (struct GNUNET_HashCode),
86                                   (const char *) &n, 1 + i % 16, exp));
87     k = n;
88   }
89   memset (&k, 0, sizeof (struct GNUNET_HashCode));
90   for (i = 0; i < 100; i++)
91   {
92     GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
93     ASSERT (1 == GNUNET_DATACACHE_get (h, &k, 1 + i % 16, &checkIt, &n));
94     k = n;
95   }
96
97   memset (&k, 42, sizeof (struct GNUNET_HashCode));
98   GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
99   ASSERT (GNUNET_OK ==
100           GNUNET_DATACACHE_put (h, &k, sizeof (struct GNUNET_HashCode),
101                                 (const char *) &n, 792,
102                                 GNUNET_TIME_UNIT_FOREVER_ABS));
103   ASSERT (0 != GNUNET_DATACACHE_get (h, &k, 792, &checkIt, &n));
104
105   GNUNET_DATACACHE_destroy (h);
106   ASSERT (ok == 0);
107   return;
108 FAILURE:
109   if (h != NULL)
110     GNUNET_DATACACHE_destroy (h);
111   ok = GNUNET_SYSERR;
112 }
113
114
115 int
116 main (int argc, char *argv[])
117 {
118   char cfg_name[128];
119   char *const xargv[] = {
120     "test-datacache",
121     "-c",
122     cfg_name,
123     NULL
124   };
125   struct GNUNET_GETOPT_CommandLineOption options[] = {
126     GNUNET_GETOPT_OPTION_END
127   };
128
129   GNUNET_log_setup ("test-datacache",
130                     "WARNING",
131                     NULL);
132   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
133   GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_datacache_data_%s.conf",
134                    plugin_name);
135   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
136                       "test-datacache", "nohelp", options, &run, NULL);
137   if (0 != ok)
138     FPRINTF (stderr, "Missed some testcases: %d\n", ok);
139   return ok;
140 }
141
142 /* end of test_datacache.c */