Merge remote-tracking branch 'origin/master' into credentials
[oweals/gnunet.git] / src / datacache / test_datacache.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2006, 2009, 2010 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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.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,
42          const struct GNUNET_HashCode *key,
43          size_t size, const char *data,
44          enum GNUNET_BLOCK_Type type,
45          struct GNUNET_TIME_Absolute exp,
46          unsigned int path_len,
47          const struct GNUNET_PeerIdentity *path)
48 {
49   if (size != sizeof (struct GNUNET_HashCode))
50   {
51     GNUNET_break (0);
52     ok = 2;
53   }
54   if (0 != memcmp (data, cls, size))
55   {
56     GNUNET_break (0);
57     ok = 3;
58   }
59   return GNUNET_OK;
60 }
61
62
63 static void
64 run (void *cls, char *const *args, const char *cfgfile,
65      const struct GNUNET_CONFIGURATION_Handle *cfg)
66 {
67   struct GNUNET_DATACACHE_Handle *h;
68   struct GNUNET_HashCode k;
69   struct GNUNET_HashCode n;
70   struct GNUNET_TIME_Absolute exp;
71   unsigned int i;
72
73   ok = 0;
74   h = GNUNET_DATACACHE_create (cfg, "testcache");
75   if (h == NULL)
76   {
77     FPRINTF (stderr,
78              "%s",
79              "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
80     ok = 77; /* mark test as skipped */
81     return;
82   }
83   exp = GNUNET_TIME_absolute_get ();
84   exp.abs_value_us += 5 * 60 * 1000 * 1000LL;
85   memset (&k, 0, sizeof (struct GNUNET_HashCode));
86   for (i = 0; i < 100; i++)
87   {
88     GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
89     ASSERT (GNUNET_OK ==
90             GNUNET_DATACACHE_put (h, &k, sizeof (struct GNUNET_HashCode),
91                                   (const char *) &n, 1 + i % 16, exp,
92                                   0, NULL));
93     k = n;
94   }
95   memset (&k, 0, sizeof (struct GNUNET_HashCode));
96   for (i = 0; i < 100; i++)
97   {
98     GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
99     ASSERT (1 == GNUNET_DATACACHE_get (h, &k, 1 + i % 16, &checkIt, &n));
100     k = n;
101   }
102
103   memset (&k, 42, sizeof (struct GNUNET_HashCode));
104   GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
105   ASSERT (GNUNET_OK ==
106           GNUNET_DATACACHE_put (h, &k, sizeof (struct GNUNET_HashCode),
107                                 (const char *) &n, 792,
108                                 GNUNET_TIME_UNIT_FOREVER_ABS,
109                                 0, NULL));
110   ASSERT (0 != GNUNET_DATACACHE_get (h, &k, 792, &checkIt, &n));
111
112   GNUNET_DATACACHE_destroy (h);
113   ASSERT (ok == 0);
114   return;
115 FAILURE:
116   if (h != NULL)
117     GNUNET_DATACACHE_destroy (h);
118   ok = GNUNET_SYSERR;
119 }
120
121
122 int
123 main (int argc, char *argv[])
124 {
125   char cfg_name[128];
126   char *const xargv[] = {
127     "test-datacache",
128     "-c",
129     cfg_name,
130     NULL
131   };
132   struct GNUNET_GETOPT_CommandLineOption options[] = {
133     GNUNET_GETOPT_OPTION_END
134   };
135
136   GNUNET_log_setup ("test-datacache",
137                     "WARNING",
138                     NULL);
139   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
140   GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_datacache_data_%s.conf",
141                    plugin_name);
142   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
143                       "test-datacache", "nohelp", options, &run, NULL);
144   if ( (0 != ok) && (77 != ok) )
145     FPRINTF (stderr, "Missed some testcases: %d\n", ok);
146   return ok;
147 }
148
149 /* end of test_datacache.c */