adding a GNUNET_memcmp_priv for constant-time comparing of data; fixes #6152 (modulo...
[oweals/gnunet.git] / src / regex / test_regex_api.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 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 regex/test_regex_api.c
22  * @brief base test case for regex api (and DHT functions)
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_testing_lib.h"
28 #include "gnunet_regex_service.h"
29
30
31 /**
32  * How long until we really give up on a particular testcase portion?
33  */
34 #define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
35                                                      600)
36
37 /**
38  * How long until we give up on any particular operation (and retry)?
39  */
40 #define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
41
42
43 static struct GNUNET_REGEX_Announcement *a;
44
45 static struct GNUNET_REGEX_Search *s;
46
47 static int ok = 1;
48
49 static struct GNUNET_SCHEDULER_Task *die_task;
50
51
52 static void
53 end (void *cls)
54 {
55   die_task = NULL;
56   GNUNET_REGEX_announce_cancel (a);
57   a = NULL;
58   GNUNET_REGEX_search_cancel (s);
59   s = NULL;
60   ok = 0;
61 }
62
63
64 static void
65 end_badly ()
66 {
67   die_task = NULL;
68   fprintf (stderr, "%s", "Testcase failed (timeout).\n");
69   GNUNET_REGEX_announce_cancel (a);
70   a = NULL;
71   GNUNET_REGEX_search_cancel (s);
72   s = NULL;
73   ok = 1;
74 }
75
76
77 /**
78  * Search callback function, invoked for every result that was found.
79  *
80  * @param cls Closure provided in GNUNET_REGEX_search.
81  * @param id Peer providing a regex that matches the string.
82  * @param get_path Path of the get request.
83  * @param get_path_length Lenght of get_path.
84  * @param put_path Path of the put request.
85  * @param put_path_length Length of the put_path.
86  */
87 static void
88 found_cb (void *cls,
89           const struct GNUNET_PeerIdentity *id,
90           const struct GNUNET_PeerIdentity *get_path,
91           unsigned int get_path_length,
92           const struct GNUNET_PeerIdentity *put_path,
93           unsigned int put_path_length)
94 {
95   GNUNET_SCHEDULER_cancel (die_task);
96   die_task =
97     GNUNET_SCHEDULER_add_now (&end, NULL);
98 }
99
100
101 static void
102 run (void *cls,
103      const struct GNUNET_CONFIGURATION_Handle *cfg,
104      struct GNUNET_TESTING_Peer *peer)
105 {
106   die_task =
107     GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT,
108                                   &end_badly, NULL);
109   a = GNUNET_REGEX_announce (cfg,
110                              "my long prefix - hello world(0|1)*",
111                              GNUNET_TIME_relative_multiply (
112                                GNUNET_TIME_UNIT_SECONDS,
113                                5),
114                              1);
115   s = GNUNET_REGEX_search (cfg,
116                            "my long prefix - hello world0101",
117                            &found_cb, NULL);
118 }
119
120
121 int
122 main (int argc, char *argv[])
123 {
124   if (0 != GNUNET_TESTING_peer_run ("test-regex-api",
125                                     "test_regex_api_data.conf",
126                                     &run, NULL))
127     return 1;
128   return ok;
129 }
130
131
132 /* end of test_regex_api.c */