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