dead
[oweals/gnunet.git] / src / datacache / test_datacache.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2009 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 static int
37 checkIt (void *cls,
38          struct GNUNET_TIME_Absolute exp,
39          const GNUNET_HashCode * key,
40          uint32_t size, 
41          const char *data, 
42          enum GNUNET_BLOCK_Type type)
43 {
44   if (size != sizeof (GNUNET_HashCode))
45     {
46       printf ("ERROR: Invalid size\n");
47       ok = 2;
48     }
49   if (0 != memcmp (data, cls, size))
50     {
51       printf ("ERROR: Invalid data\n");
52       ok = 3;
53     }
54   return GNUNET_OK;
55 }
56
57
58 static void
59 run (void *cls,
60      struct GNUNET_SCHEDULER_Handle *sched,
61      char *const *args,
62      const char *cfgfile,
63      const struct GNUNET_CONFIGURATION_Handle *cfg)
64 {
65   struct GNUNET_DATACACHE_Handle *h;
66   GNUNET_HashCode k;
67   GNUNET_HashCode n;
68   struct GNUNET_TIME_Absolute exp;
69   unsigned int i;
70
71   ok = 0;
72   h = GNUNET_DATACACHE_create (sched,
73                                cfg,
74                                "testcache");
75
76   ASSERT (NULL != h);
77   exp = GNUNET_TIME_absolute_get ();
78   exp.value += 5 * 60 * 1000;
79   memset (&k, 0, sizeof (GNUNET_HashCode));
80   for (i = 0; i < 100; i++)
81     {
82       GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
83       ASSERT (GNUNET_OK == GNUNET_DATACACHE_put (h,
84                                                  &k,
85                                                  sizeof (GNUNET_HashCode),
86                                                  (const char *) &n,
87                                                  1+i%16,
88                                                  exp));
89       k = n;
90     }
91   memset (&k, 0, sizeof (GNUNET_HashCode));
92   for (i = 0; i < 100; i++)
93     {
94       GNUNET_CRYPTO_hash (&k, sizeof (GNUNET_HashCode), &n);
95       ASSERT (1 == 
96               GNUNET_DATACACHE_get (h, &k, 1+i%16,
97                                     &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 !=
110           GNUNET_DATACACHE_get (h, &k, 792,
111                                 &checkIt, &n));
112
113   GNUNET_DATACACHE_destroy (h);
114   ASSERT (ok == 0);
115   return;
116 FAILURE:
117   if (h != NULL)
118     GNUNET_DATACACHE_destroy (h);
119   ok = GNUNET_SYSERR;
120 }
121
122
123 static int
124 check ()
125 {
126   char *const argv[] = { "test-datacache-api",
127     "-c",
128     "test_datacache_data.conf",
129 #if VERBOSE
130     "-L", "DEBUG",
131 #endif
132     NULL
133   };
134   struct GNUNET_GETOPT_CommandLineOption options[] = {
135     GNUNET_GETOPT_OPTION_END
136   };
137   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
138                       argv, "test-datacache-api", "nohelp",
139                       options, &run, NULL);
140   if (ok != 0)
141     fprintf (stderr, "Missed some testcases: %d\n", ok);
142   return ok;
143 }
144
145
146 int
147 main (int argc, char *argv[])
148 {
149   int ret;
150   
151   GNUNET_log_setup ("test-datacache-api",
152 #if VERBOSE
153                     "DEBUG",
154 #else
155                     "WARNING",
156 #endif
157                     NULL);
158   ret = check ();
159
160   return ret;
161 }
162
163 /* end of test_datacache.c */