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