remove 'illegal' (non-reentrant) log logic from signal handler
[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__, \
31                                             __LINE__); goto FAILURE; \
32                        } } while (0)
33
34 static int ok;
35
36 /**
37  * Name of plugin under test.
38  */
39 static const char *plugin_name;
40
41
42 static int
43 checkIt (void *cls,
44          const struct GNUNET_HashCode *key,
45          size_t size, const char *data,
46          enum GNUNET_BLOCK_Type type,
47          struct GNUNET_TIME_Absolute exp,
48          unsigned int path_len,
49          const struct GNUNET_PeerIdentity *path)
50 {
51   (void) key;
52   (void) type;
53   (void) exp;
54   (void) path_len;
55   (void) path;
56   if (size != sizeof(struct GNUNET_HashCode))
57   {
58     GNUNET_break (0);
59     ok = 2;
60   }
61   if (0 != memcmp (data, cls, size))
62   {
63     GNUNET_break (0);
64     ok = 3;
65   }
66   return GNUNET_OK;
67 }
68
69
70 static void
71 run (void *cls,
72      char *const *args,
73      const char *cfgfile,
74      const struct GNUNET_CONFIGURATION_Handle *cfg)
75 {
76   struct GNUNET_DATACACHE_Handle *h;
77   struct GNUNET_HashCode k;
78   struct GNUNET_HashCode n;
79   struct GNUNET_TIME_Absolute exp;
80
81   (void) cls;
82   (void) args;
83   (void) cfgfile;
84   ok = 0;
85   h = GNUNET_DATACACHE_create (cfg,
86                                "testcache");
87   if (h == NULL)
88   {
89     fprintf (stderr,
90              "%s",
91              "Failed to initialize datacache.  Database likely not setup, skipping test.\n");
92     ok = 77;   /* mark test as skipped */
93     return;
94   }
95   exp = GNUNET_TIME_absolute_get ();
96   exp.abs_value_us += 5 * 60 * 1000 * 1000LL;
97   memset (&k, 0, sizeof(struct GNUNET_HashCode));
98   for (unsigned int i = 0; i < 100; i++)
99   {
100     GNUNET_CRYPTO_hash (&k, sizeof(struct GNUNET_HashCode), &n);
101     ASSERT (GNUNET_OK ==
102             GNUNET_DATACACHE_put (h,
103                                   &k,
104                                   GNUNET_YES,
105                                   sizeof(struct GNUNET_HashCode),
106                                   (const char *) &n, 1 + i % 16, exp,
107                                   0, NULL));
108     k = n;
109   }
110   memset (&k,
111           0,
112           sizeof(struct GNUNET_HashCode));
113   for (unsigned int i = 0; i < 100; i++)
114   {
115     GNUNET_CRYPTO_hash (&k,
116                         sizeof(struct GNUNET_HashCode),
117                         &n);
118     ASSERT (1 == GNUNET_DATACACHE_get (h,
119                                        &k,
120                                        1 + i % 16,
121                                        &checkIt,
122                                        &n));
123     k = n;
124   }
125
126   memset (&k,
127           42,
128           sizeof(struct GNUNET_HashCode));
129   GNUNET_CRYPTO_hash (&k,
130                       sizeof(struct GNUNET_HashCode),
131                       &n);
132   ASSERT (GNUNET_OK ==
133           GNUNET_DATACACHE_put (h,
134                                 &k,
135                                 GNUNET_YES,
136                                 sizeof(struct GNUNET_HashCode),
137                                 (const char *) &n,
138                                 792,
139                                 GNUNET_TIME_UNIT_FOREVER_ABS,
140                                 0,
141                                 NULL));
142   ASSERT (0 != GNUNET_DATACACHE_get (h,
143                                      &k,
144                                      792,
145                                      &checkIt,
146                                      &n));
147   GNUNET_DATACACHE_destroy (h);
148   ASSERT (ok == 0);
149   return;
150 FAILURE:
151   if (h != NULL)
152     GNUNET_DATACACHE_destroy (h);
153   ok = GNUNET_SYSERR;
154 }
155
156
157 int
158 main (int argc, char *argv[])
159 {
160   char cfg_name[PATH_MAX];
161   char *const xargv[] = {
162     "test-datacache",
163     "-c",
164     cfg_name,
165     NULL
166   };
167   struct GNUNET_GETOPT_CommandLineOption options[] = {
168     GNUNET_GETOPT_OPTION_END
169   };
170
171   (void) argc;
172   GNUNET_log_setup ("test-datacache",
173                     "WARNING",
174                     NULL);
175   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
176   GNUNET_snprintf (cfg_name,
177                    sizeof(cfg_name),
178                    "test_datacache_data_%s.conf",
179                    plugin_name);
180   GNUNET_PROGRAM_run ((sizeof(xargv) / sizeof(char *)) - 1,
181                       xargv,
182                       "test-datacache",
183                       "nohelp",
184                       options,
185                       &run,
186                       NULL);
187   if ((0 != ok) && (77 != ok))
188     fprintf (stderr,
189              "Missed some testcases: %d\n",
190              ok);
191   return ok;
192 }
193
194
195 /* end of test_datacache.c */