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