glitch in the license text detected by hyazinthe, thank you!
[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 it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14 */
15 /*
16  * @file datacache/test_datacache.c
17  * @brief Test for the datacache implementations.
18  * @author Nils Durner
19  */
20 #include "platform.h"
21 #include "gnunet_util_lib.h"
22 #include "gnunet_datacache_lib.h"
23 #include "gnunet_testing_lib.h"
24
25 #define ASSERT(x) do { if (! (x)) { printf("Error at %s:%d\n", __FILE__, __LINE__); goto FAILURE;} } while (0)
26
27 static int ok;
28
29 /**
30  * Name of plugin under test.
31  */
32 static const char *plugin_name;
33
34
35 static int
36 checkIt (void *cls,
37          const struct GNUNET_HashCode *key,
38          size_t size, const char *data,
39          enum GNUNET_BLOCK_Type type,
40          struct GNUNET_TIME_Absolute exp,
41          unsigned int path_len,
42          const struct GNUNET_PeerIdentity *path)
43 {
44   if (size != sizeof (struct GNUNET_HashCode))
45   {
46     GNUNET_break (0);
47     ok = 2;
48   }
49   if (0 != memcmp (data, cls, size))
50   {
51     GNUNET_break (0);
52     ok = 3;
53   }
54   return GNUNET_OK;
55 }
56
57
58 static void
59 run (void *cls, char *const *args, const char *cfgfile,
60      const struct GNUNET_CONFIGURATION_Handle *cfg)
61 {
62   struct GNUNET_DATACACHE_Handle *h;
63   struct GNUNET_HashCode k;
64   struct GNUNET_HashCode n;
65   struct GNUNET_TIME_Absolute exp;
66   unsigned int i;
67
68   ok = 0;
69   h = GNUNET_DATACACHE_create (cfg, "testcache");
70   if (h == NULL)
71   {
72     FPRINTF (stderr,
73              "%s",
74              "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
75     ok = 77; /* mark test as skipped */
76     return;
77   }
78   exp = GNUNET_TIME_absolute_get ();
79   exp.abs_value_us += 5 * 60 * 1000 * 1000LL;
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,
86                                   &k,
87                                   GNUNET_YES,
88                                   sizeof (struct GNUNET_HashCode),
89                                   (const char *) &n, 1 + i % 16, exp,
90                                   0, NULL));
91     k = n;
92   }
93   memset (&k, 0, sizeof (struct GNUNET_HashCode));
94   for (i = 0; i < 100; i++)
95   {
96     GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
97     ASSERT (1 == GNUNET_DATACACHE_get (h, &k, 1 + i % 16, &checkIt, &n));
98     k = n;
99   }
100
101   memset (&k, 42, sizeof (struct GNUNET_HashCode));
102   GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
103   ASSERT (GNUNET_OK ==
104           GNUNET_DATACACHE_put (h,
105                                 &k,
106                                 GNUNET_YES,
107                                 sizeof (struct GNUNET_HashCode),
108                                 (const char *) &n, 792,
109                                 GNUNET_TIME_UNIT_FOREVER_ABS,
110                                 0, NULL));
111   ASSERT (0 != GNUNET_DATACACHE_get (h, &k, 792, &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 int
124 main (int argc, char *argv[])
125 {
126   char cfg_name[128];
127   char *const xargv[] = {
128     "test-datacache",
129     "-c",
130     cfg_name,
131     NULL
132   };
133   struct GNUNET_GETOPT_CommandLineOption options[] = {
134     GNUNET_GETOPT_OPTION_END
135   };
136
137   GNUNET_log_setup ("test-datacache",
138                     "WARNING",
139                     NULL);
140   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
141   GNUNET_snprintf (cfg_name, sizeof (cfg_name), "test_datacache_data_%s.conf",
142                    plugin_name);
143   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
144                       "test-datacache", "nohelp", options, &run, NULL);
145   if ( (0 != ok) && (77 != ok) )
146     FPRINTF (stderr, "Missed some testcases: %d\n", ok);
147   return ok;
148 }
149
150 /* end of test_datacache.c */