first step to remove plibc
[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      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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   (void) key;
50   (void) type;
51   (void) exp;
52   (void) path_len;
53   (void) path;
54   if (size != sizeof (struct GNUNET_HashCode))
55   {
56     GNUNET_break (0);
57     ok = 2;
58   }
59   if (0 != memcmp (data, cls, size))
60   {
61     GNUNET_break (0);
62     ok = 3;
63   }
64   return GNUNET_OK;
65 }
66
67
68 static void
69 run (void *cls,
70      char *const *args,
71      const char *cfgfile,
72      const struct GNUNET_CONFIGURATION_Handle *cfg)
73 {
74   struct GNUNET_DATACACHE_Handle *h;
75   struct GNUNET_HashCode k;
76   struct GNUNET_HashCode n;
77   struct GNUNET_TIME_Absolute exp;
78
79   (void) cls;
80   (void) args;
81   (void) cfgfile;
82   ok = 0;
83   h = GNUNET_DATACACHE_create (cfg,
84                                "testcache");
85   if (h == NULL)
86   {
87     fprintf (stderr,
88              "%s",
89              "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
90     ok = 77; /* mark test as skipped */
91     return;
92   }
93   exp = GNUNET_TIME_absolute_get ();
94   exp.abs_value_us += 5 * 60 * 1000 * 1000LL;
95   memset (&k, 0, sizeof (struct GNUNET_HashCode));
96   for (unsigned int i = 0; i < 100; i++)
97   {
98     GNUNET_CRYPTO_hash (&k, sizeof (struct GNUNET_HashCode), &n);
99     ASSERT (GNUNET_OK ==
100             GNUNET_DATACACHE_put (h,
101                                   &k,
102                                   GNUNET_YES,
103                                   sizeof (struct GNUNET_HashCode),
104                                   (const char *) &n, 1 + i % 16, exp,
105                                   0, NULL));
106     k = n;
107   }
108   memset (&k,
109           0,
110           sizeof (struct GNUNET_HashCode));
111   for (unsigned int i = 0; i < 100; i++)
112   {
113     GNUNET_CRYPTO_hash (&k,
114                         sizeof (struct GNUNET_HashCode),
115                         &n);
116     ASSERT (1 == GNUNET_DATACACHE_get (h,
117                                        &k,
118                                        1 + i % 16,
119                                        &checkIt,
120                                        &n));
121     k = n;
122   }
123
124   memset (&k,
125           42,
126           sizeof (struct GNUNET_HashCode));
127   GNUNET_CRYPTO_hash (&k,
128                       sizeof (struct GNUNET_HashCode),
129                       &n);
130   ASSERT (GNUNET_OK ==
131           GNUNET_DATACACHE_put (h,
132                                 &k,
133                                 GNUNET_YES,
134                                 sizeof (struct GNUNET_HashCode),
135                                 (const char *) &n,
136                                 792,
137                                 GNUNET_TIME_UNIT_FOREVER_ABS,
138                                 0,
139                                 NULL));
140   ASSERT (0 != GNUNET_DATACACHE_get (h,
141                                      &k,
142                                      792,
143                                      &checkIt,
144                                      &n));
145   GNUNET_DATACACHE_destroy (h);
146   ASSERT (ok == 0);
147   return;
148 FAILURE:
149   if (h != NULL)
150     GNUNET_DATACACHE_destroy (h);
151   ok = GNUNET_SYSERR;
152 }
153
154
155 int
156 main (int argc, char *argv[])
157 {
158   char cfg_name[PATH_MAX];
159   char *const xargv[] = {
160     "test-datacache",
161     "-c",
162     cfg_name,
163     NULL
164   };
165   struct GNUNET_GETOPT_CommandLineOption options[] = {
166     GNUNET_GETOPT_OPTION_END
167   };
168
169   (void) argc;
170   GNUNET_log_setup ("test-datacache",
171                     "WARNING",
172                     NULL);
173   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
174   GNUNET_snprintf (cfg_name,
175                    sizeof (cfg_name),
176                    "test_datacache_data_%s.conf",
177                    plugin_name);
178   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1,
179                       xargv,
180                       "test-datacache",
181                       "nohelp",
182                       options,
183                       &run,
184                       NULL);
185   if ( (0 != ok) && (77 != ok) )
186     fprintf (stderr,
187              "Missed some testcases: %d\n",
188              ok);
189   return ok;
190 }
191
192 /* end of test_datacache.c */