diff options
author | t3sserakt <t3ss@posteo.de> | 2021-03-04 10:41:01 +0100 |
---|---|---|
committer | t3sserakt <t3ss@posteo.de> | 2021-03-04 10:42:39 +0100 |
commit | e5d31aa52e4983bffce3eca6ca601bbb8f4a302a (patch) | |
tree | 1878b4c6595b4b3d402988c1df772adf48908622 /src/testing | |
parent | 46111957db87028c369ea4792b9c533a9dd09a33 (diff) | |
download | gnunet-e5d31aa52e4983bffce3eca6ca601bbb8f4a302a.tar.gz gnunet-e5d31aa52e4983bffce3eca6ca601bbb8f4a302a.zip |
- first try to integrate GNU Taler command style testing
Diffstat (limited to 'src/testing')
-rw-r--r-- | src/testing/Makefile.am | 25 | ||||
-rw-r--r-- | src/testing/test_testing_hello_world.c | 61 | ||||
-rw-r--r-- | src/testing/testing_api_cmd_batch.c | 257 | ||||
-rw-r--r-- | src/testing/testing_api_cmd_hello_world.c | 113 | ||||
-rw-r--r-- | src/testing/testing_api_cmd_hello_world_birth.c | 160 | ||||
-rw-r--r-- | src/testing/testing_api_loop.c | 652 | ||||
-rw-r--r-- | src/testing/testing_api_trait_cmd.c | 79 | ||||
-rw-r--r-- | src/testing/testing_api_trait_process.c | 81 | ||||
-rw-r--r-- | src/testing/testing_api_traits.c | 81 |
9 files changed, 1508 insertions, 1 deletions
diff --git a/src/testing/Makefile.am b/src/testing/Makefile.am index 2b599f55b..f7aa1f896 100644 --- a/src/testing/Makefile.am +++ b/src/testing/Makefile.am | |||
@@ -15,7 +15,14 @@ lib_LTLIBRARIES = \ | |||
15 | libgnunettesting.la | 15 | libgnunettesting.la |
16 | 16 | ||
17 | libgnunettesting_la_SOURCES = \ | 17 | libgnunettesting_la_SOURCES = \ |
18 | testing.c | 18 | testing.c \ |
19 | testing_api_cmd_batch.c \ | ||
20 | testing_api_cmd_hello_world.c \ | ||
21 | testing_api_cmd_hello_world_birth.c \ | ||
22 | testing_api_loop.c \ | ||
23 | testing_api_trait_cmd.c \ | ||
24 | testing_api_trait_process.c \ | ||
25 | testing_api_traits.c | ||
19 | libgnunettesting_la_LIBADD = \ | 26 | libgnunettesting_la_LIBADD = \ |
20 | $(top_builddir)/src/arm/libgnunetarm.la \ | 27 | $(top_builddir)/src/arm/libgnunetarm.la \ |
21 | $(top_builddir)/src/util/libgnunetutil.la \ | 28 | $(top_builddir)/src/util/libgnunetutil.la \ |
@@ -48,8 +55,10 @@ list_keys_LDADD = \ | |||
48 | 55 | ||
49 | 56 | ||
50 | check_PROGRAMS = \ | 57 | check_PROGRAMS = \ |
58 | test_testing_hello_world \ | ||
51 | test_testing_portreservation \ | 59 | test_testing_portreservation \ |
52 | test_testing_servicestartup \ | 60 | test_testing_servicestartup \ |
61 | test_testing_ng_peerstartup \ | ||
53 | test_testing_peerstartup \ | 62 | test_testing_peerstartup \ |
54 | test_testing_peerstartup2 \ | 63 | test_testing_peerstartup2 \ |
55 | test_testing_sharedservices | 64 | test_testing_sharedservices |
@@ -57,18 +66,32 @@ check_PROGRAMS = \ | |||
57 | if ENABLE_TEST_RUN | 66 | if ENABLE_TEST_RUN |
58 | AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME; | 67 | AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME; |
59 | TESTS = \ | 68 | TESTS = \ |
69 | test_testing_hello_world \ | ||
60 | test_testing_portreservation \ | 70 | test_testing_portreservation \ |
71 | test_testing_ng_peerstartup \ | ||
61 | test_testing_peerstartup \ | 72 | test_testing_peerstartup \ |
62 | test_testing_peerstartup2 \ | 73 | test_testing_peerstartup2 \ |
63 | test_testing_servicestartup | 74 | test_testing_servicestartup |
64 | endif | 75 | endif |
65 | 76 | ||
77 | test_testing_helloworld_SOURCES = \ | ||
78 | test_testing_hello_world | ||
79 | test_testing_portreservation_LDADD = \ | ||
80 | libgnunettesting.la \ | ||
81 | $(top_builddir)/src/util/libgnunetutil.la | ||
82 | |||
66 | test_testing_portreservation_SOURCES = \ | 83 | test_testing_portreservation_SOURCES = \ |
67 | test_testing_portreservation.c | 84 | test_testing_portreservation.c |
68 | test_testing_portreservation_LDADD = \ | 85 | test_testing_portreservation_LDADD = \ |
69 | libgnunettesting.la \ | 86 | libgnunettesting.la \ |
70 | $(top_builddir)/src/util/libgnunetutil.la | 87 | $(top_builddir)/src/util/libgnunetutil.la |
71 | 88 | ||
89 | test_testing_ng_peerstartup_SOURCES = \ | ||
90 | test_testing_ng_peerstartup.c | ||
91 | test_testing_ng_peerstartup_LDADD = \ | ||
92 | libgnunettesting.la \ | ||
93 | $(top_builddir)/src/util/libgnunetutil.la | ||
94 | |||
72 | test_testing_peerstartup_SOURCES = \ | 95 | test_testing_peerstartup_SOURCES = \ |
73 | test_testing_peerstartup.c | 96 | test_testing_peerstartup.c |
74 | test_testing_peerstartup_LDADD = \ | 97 | test_testing_peerstartup_LDADD = \ |
diff --git a/src/testing/test_testing_hello_world.c b/src/testing/test_testing_hello_world.c new file mode 100644 index 000000000..689d04ed3 --- /dev/null +++ b/src/testing/test_testing_hello_world.c | |||
@@ -0,0 +1,61 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2008, 2009, 2012 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 | /** | ||
22 | * @file testing/test_testing_hello_world.c | ||
23 | * @brief hello world test case | ||
24 | * @author t3sserakt | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_testing_ng_lib.h" | ||
28 | |||
29 | /** | ||
30 | * Main function that will tell the interpreter what commands to | ||
31 | * run. | ||
32 | * | ||
33 | * @param cls closure | ||
34 | */ | ||
35 | static void | ||
36 | run (void *cls, | ||
37 | struct GNUNET_TESTING_Interpreter *is) | ||
38 | { | ||
39 | struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); | ||
40 | |||
41 | struct GNUNET_TESTING_Command commands[] = { | ||
42 | GNUNET_TESTING_cmd_hello_world_birth ("hello-world-birth-0", | ||
43 | &now), | ||
44 | GNUNET_TESTING_cmd_hello_world ("hello-world-0",""), | ||
45 | GNUNET_TESTING_cmd_end () | ||
46 | }; | ||
47 | |||
48 | GNUNET_TESTING_run (is, | ||
49 | commands); | ||
50 | } | ||
51 | |||
52 | int | ||
53 | main (int argc, | ||
54 | char *const *argv) | ||
55 | { | ||
56 | return GNUNET_TESTING_setup (&run, | ||
57 | NULL, | ||
58 | NULL, | ||
59 | NULL, | ||
60 | GNUNET_NO); | ||
61 | } | ||
diff --git a/src/testing/testing_api_cmd_batch.c b/src/testing/testing_api_cmd_batch.c new file mode 100644 index 000000000..74a763718 --- /dev/null +++ b/src/testing/testing_api_cmd_batch.c | |||
@@ -0,0 +1,257 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2008, 2009, 2012 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 | /** | ||
22 | * @file testing/testing_api_cmd_batch.c | ||
23 | * @brief Implement batch-execution of CMDs. | ||
24 | * @author Marcello Stanisci (GNU Taler testing) | ||
25 | * @author t3sserakt | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_testing_ng_lib.h" | ||
29 | |||
30 | |||
31 | /** | ||
32 | * State for a "batch" CMD. | ||
33 | */ | ||
34 | struct BatchState | ||
35 | { | ||
36 | /** | ||
37 | * CMDs batch. | ||
38 | */ | ||
39 | struct GNUNET_TESTING_Command *batch; | ||
40 | |||
41 | /** | ||
42 | * Internal command pointer. | ||
43 | */ | ||
44 | unsigned int batch_ip; | ||
45 | }; | ||
46 | |||
47 | |||
48 | /** | ||
49 | * Run the command. | ||
50 | * | ||
51 | * @param cls closure. | ||
52 | * @param cmd the command being executed. | ||
53 | * @param is the interpreter state. | ||
54 | */ | ||
55 | static void | ||
56 | batch_run (void *cls, | ||
57 | const struct GNUNET_TESTING_Command *cmd, | ||
58 | struct GNUNET_TESTING_Interpreter *is) | ||
59 | { | ||
60 | struct BatchState *bs = cls; | ||
61 | |||
62 | if (NULL != bs->batch[bs->batch_ip].label) | ||
63 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
64 | "Running batched command: %s\n", | ||
65 | bs->batch[bs->batch_ip].label); | ||
66 | |||
67 | /* hit end command, leap to next top-level command. */ | ||
68 | if (NULL == bs->batch[bs->batch_ip].label) | ||
69 | { | ||
70 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
71 | "Exiting from batch: %s\n", | ||
72 | cmd->label); | ||
73 | GNUNET_TESTING_interpreter_next (is); | ||
74 | return; | ||
75 | } | ||
76 | bs->batch[bs->batch_ip].start_time | ||
77 | = bs->batch[bs->batch_ip].last_req_time | ||
78 | = GNUNET_TIME_absolute_get (); | ||
79 | bs->batch[bs->batch_ip].num_tries = 1; | ||
80 | bs->batch[bs->batch_ip].run (bs->batch[bs->batch_ip].cls, | ||
81 | &bs->batch[bs->batch_ip], | ||
82 | is); | ||
83 | } | ||
84 | |||
85 | |||
86 | /** | ||
87 | * Cleanup the state from a "reserve status" CMD, and possibly | ||
88 | * cancel a pending operation thereof. | ||
89 | * | ||
90 | * @param cls closure. | ||
91 | * @param cmd the command which is being cleaned up. | ||
92 | */ | ||
93 | static void | ||
94 | batch_cleanup (void *cls, | ||
95 | const struct GNUNET_TESTING_Command *cmd) | ||
96 | { | ||
97 | struct BatchState *bs = cls; | ||
98 | |||
99 | (void) cmd; | ||
100 | for (unsigned int i = 0; | ||
101 | NULL != bs->batch[i].label; | ||
102 | i++) | ||
103 | bs->batch[i].cleanup (bs->batch[i].cls, | ||
104 | &bs->batch[i]); | ||
105 | GNUNET_free (bs->batch); | ||
106 | GNUNET_free (bs); | ||
107 | } | ||
108 | |||
109 | |||
110 | /** | ||
111 | * Offer internal data from a "batch" CMD, to other commands. | ||
112 | * | ||
113 | * @param cls closure. | ||
114 | * @param[out] ret result. | ||
115 | * @param trait name of the trait. | ||
116 | * @param index index number of the object to offer. | ||
117 | * @return #GNUNET_OK on success. | ||
118 | */ | ||
119 | static int | ||
120 | batch_traits (void *cls, | ||
121 | const void **ret, | ||
122 | const char *trait, | ||
123 | unsigned int index) | ||
124 | { | ||
125 | #define CURRENT_CMD_INDEX 0 | ||
126 | #define BATCH_INDEX 1 | ||
127 | |||
128 | struct BatchState *bs = cls; | ||
129 | |||
130 | struct GNUNET_TESTING_Trait traits[] = { | ||
131 | GNUNET_TESTING_make_trait_cmd | ||
132 | (CURRENT_CMD_INDEX, &bs->batch[bs->batch_ip]), | ||
133 | GNUNET_TESTING_make_trait_cmd | ||
134 | (BATCH_INDEX, bs->batch), | ||
135 | GNUNET_TESTING_trait_end () | ||
136 | }; | ||
137 | |||
138 | /* Always return current command. */ | ||
139 | return GNUNET_TESTING_get_trait (traits, | ||
140 | ret, | ||
141 | trait, | ||
142 | index); | ||
143 | } | ||
144 | |||
145 | |||
146 | /** | ||
147 | * Create a "batch" command. Such command takes a | ||
148 | * end_CMD-terminated array of CMDs and executed them. | ||
149 | * Once it hits the end CMD, it passes the control | ||
150 | * to the next top-level CMD, regardless of it being | ||
151 | * another batch or ordinary CMD. | ||
152 | * | ||
153 | * @param label the command label. | ||
154 | * @param batch array of CMDs to execute. | ||
155 | * | ||
156 | * @return the command. | ||
157 | */ | ||
158 | struct GNUNET_TESTING_Command | ||
159 | GNUNET_TESTING_cmd_batch (const char *label, | ||
160 | struct GNUNET_TESTING_Command *batch) | ||
161 | { | ||
162 | struct BatchState *bs; | ||
163 | unsigned int i; | ||
164 | |||
165 | bs = GNUNET_new (struct BatchState); | ||
166 | |||
167 | /* Get number of commands. */ | ||
168 | for (i = 0; NULL != batch[i].label; i++) | ||
169 | /* noop */ | ||
170 | ; | ||
171 | |||
172 | bs->batch = GNUNET_new_array (i + 1, | ||
173 | struct GNUNET_TESTING_Command); | ||
174 | memcpy (bs->batch, | ||
175 | batch, | ||
176 | sizeof (struct GNUNET_TESTING_Command) * i); | ||
177 | { | ||
178 | struct GNUNET_TESTING_Command cmd = { | ||
179 | .cls = bs, | ||
180 | .label = label, | ||
181 | .run = &batch_run, | ||
182 | .cleanup = &batch_cleanup, | ||
183 | .traits = &batch_traits | ||
184 | }; | ||
185 | |||
186 | return cmd; | ||
187 | } | ||
188 | } | ||
189 | |||
190 | |||
191 | /** | ||
192 | * Advance internal pointer to next command. | ||
193 | * | ||
194 | * @param is interpreter state. | ||
195 | */ | ||
196 | void | ||
197 | GNUNET_TESTING_cmd_batch_next (struct GNUNET_TESTING_Interpreter *is) | ||
198 | { | ||
199 | struct BatchState *bs = is->commands[is->ip].cls; | ||
200 | |||
201 | if (NULL == bs->batch[bs->batch_ip].label) | ||
202 | { | ||
203 | is->commands[is->ip].finish_time = GNUNET_TIME_absolute_get (); | ||
204 | is->ip++; | ||
205 | return; | ||
206 | } | ||
207 | bs->batch[bs->batch_ip].finish_time = GNUNET_TIME_absolute_get (); | ||
208 | bs->batch_ip++; | ||
209 | } | ||
210 | |||
211 | |||
212 | /** | ||
213 | * Test if this command is a batch command. | ||
214 | * | ||
215 | * @return false if not, true if it is a batch command | ||
216 | */ | ||
217 | int | ||
218 | GNUNET_TESTING_cmd_is_batch (const struct GNUNET_TESTING_Command *cmd) | ||
219 | { | ||
220 | return cmd->run == &batch_run; | ||
221 | } | ||
222 | |||
223 | |||
224 | /** | ||
225 | * Obtain what command the batch is at. | ||
226 | * | ||
227 | * @return cmd current batch command | ||
228 | */ | ||
229 | struct GNUNET_TESTING_Command * | ||
230 | GNUNET_TESTING_cmd_batch_get_current (const struct GNUNET_TESTING_Command *cmd) | ||
231 | { | ||
232 | struct BatchState *bs = cmd->cls; | ||
233 | |||
234 | GNUNET_assert (cmd->run == &batch_run); | ||
235 | return &bs->batch[bs->batch_ip]; | ||
236 | } | ||
237 | |||
238 | |||
239 | /** | ||
240 | * Set what command the batch should be at. | ||
241 | * | ||
242 | * @param cmd current batch command | ||
243 | * @param new_ip where to move the IP | ||
244 | */ | ||
245 | void | ||
246 | GNUNET_TESTING_cmd_batch_set_current (const struct GNUNET_TESTING_Command *cmd, | ||
247 | unsigned int new_ip) | ||
248 | { | ||
249 | struct BatchState *bs = cmd->cls; | ||
250 | |||
251 | /* sanity checks */ | ||
252 | GNUNET_assert (cmd->run == &batch_run); | ||
253 | for (unsigned int i = 0; i < new_ip; i++) | ||
254 | GNUNET_assert (NULL != bs->batch[i].label); | ||
255 | /* actual logic */ | ||
256 | bs->batch_ip = new_ip; | ||
257 | } | ||
diff --git a/src/testing/testing_api_cmd_hello_world.c b/src/testing/testing_api_cmd_hello_world.c new file mode 100644 index 000000000..1915f7da7 --- /dev/null +++ b/src/testing/testing_api_cmd_hello_world.c | |||
@@ -0,0 +1,113 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2008, 2009, 2012 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 | /** | ||
22 | * @file testing/testing_api_cmd_hello_world.c | ||
23 | * @brief implementation of a hello world command. | ||
24 | * @author t3sserakt | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_testing_ng_lib.h" | ||
28 | |||
29 | struct HelloWorldState | ||
30 | { | ||
31 | char *message; | ||
32 | }; | ||
33 | |||
34 | /** | ||
35 | * | ||
36 | * | ||
37 | * @param cls closure | ||
38 | * @param cmd current CMD being cleaned up. | ||
39 | */ | ||
40 | static void | ||
41 | hello_world_cleanup (void *cls, | ||
42 | const struct GNUNET_TESTING_Command *cmd) | ||
43 | { | ||
44 | struct HelloWorldState *hs = cls; | ||
45 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
46 | "Cleaning up message %s", | ||
47 | hs->message); | ||
48 | } | ||
49 | |||
50 | /** | ||
51 | * | ||
52 | * | ||
53 | * @param cls closure. | ||
54 | * @param[out] ret result | ||
55 | * @param trait name of the trait. | ||
56 | * @param index index number of the object to offer. | ||
57 | * @return #GNUNET_OK on success. | ||
58 | */ | ||
59 | static int | ||
60 | hello_world_traits (void *cls, | ||
61 | const void **ret, | ||
62 | const char *trait, | ||
63 | unsigned int index) | ||
64 | { | ||
65 | return GNUNET_OK; | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * Run the "hello world" CMD. | ||
70 | * | ||
71 | * @param cls closure. | ||
72 | * @param cmd CMD being run. | ||
73 | * @param is interpreter state. | ||
74 | */ | ||
75 | static void | ||
76 | hello_world_run (void *cls, | ||
77 | const struct GNUNET_TESTING_Command *cmd, | ||
78 | struct GNUNET_TESTING_Interpreter *is) | ||
79 | { | ||
80 | struct HelloWorldState *hs = cls; | ||
81 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
82 | "%s", | ||
83 | hs->message); | ||
84 | GNUNET_TESTING_get_trait_what_am_i (cmd, | ||
85 | hs->message); | ||
86 | } | ||
87 | |||
88 | /** | ||
89 | * Create command. | ||
90 | * | ||
91 | * @param label name for command. | ||
92 | * @param message initial message. | ||
93 | * @return command. | ||
94 | */ | ||
95 | struct GNUNET_TESTING_Command | ||
96 | GNUNET_TESTING_cmd_hello_world (const char *label, | ||
97 | char *message) | ||
98 | { | ||
99 | struct HelloWorldState *hs; | ||
100 | |||
101 | hs = GNUNET_new (struct HelloWorldState); | ||
102 | hs->message = "Hello World, I am nobody!"; | ||
103 | |||
104 | struct GNUNET_TESTING_Command cmd = { | ||
105 | .cls = hs, | ||
106 | .label = label, | ||
107 | .run = &hello_world_run, | ||
108 | .cleanup = &hello_world_cleanup, | ||
109 | .traits = &hello_world_traits | ||
110 | }; | ||
111 | |||
112 | return cmd; | ||
113 | } | ||
diff --git a/src/testing/testing_api_cmd_hello_world_birth.c b/src/testing/testing_api_cmd_hello_world_birth.c new file mode 100644 index 000000000..a3157b6de --- /dev/null +++ b/src/testing/testing_api_cmd_hello_world_birth.c | |||
@@ -0,0 +1,160 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2008, 2009, 2012 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 | /** | ||
22 | * @file testing/testing_api_cmd_hello_world.c | ||
23 | * @brief implementation of a hello world command. | ||
24 | * @author t3sserakt | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_testing_ng_lib.h" | ||
29 | |||
30 | struct HelloWorldBirthState | ||
31 | { | ||
32 | struct GNUNET_TIME_Absolute *date; | ||
33 | char *what_am_i; | ||
34 | }; | ||
35 | |||
36 | /** | ||
37 | * | ||
38 | * | ||
39 | * @param cls closure | ||
40 | * @param cmd current CMD being cleaned up. | ||
41 | */ | ||
42 | static void | ||
43 | hello_world_birth_cleanup (void *cls, | ||
44 | const struct GNUNET_TESTING_Command *cmd) | ||
45 | { | ||
46 | struct HelloWorldBirthState *hbs = cls; | ||
47 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
48 | "Finished birth of %s", | ||
49 | hbs->what_am_i); | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * | ||
54 | * | ||
55 | * @param cls closure. | ||
56 | * @param[out] ret result | ||
57 | * @param trait name of the trait. | ||
58 | * @param index index number of the object to offer. | ||
59 | * @return #GNUNET_OK on success. | ||
60 | */ | ||
61 | static int | ||
62 | hello_world_birth_traits (void *cls, | ||
63 | const void **ret, | ||
64 | const char *trait, | ||
65 | unsigned int index) | ||
66 | { | ||
67 | struct HelloWorldBirthState *hbs = cls; | ||
68 | struct GNUNET_TESTING_Trait traits[] = { | ||
69 | { | ||
70 | .index = 0, | ||
71 | .trait_name = "what_am_i", | ||
72 | .ptr = (const void *) hbs->what_am_i, | ||
73 | }, | ||
74 | GNUNET_TESTING_trait_end () | ||
75 | }; | ||
76 | |||
77 | return GNUNET_TESTING_get_trait (traits, | ||
78 | ret, | ||
79 | trait, | ||
80 | index); | ||
81 | } | ||
82 | |||
83 | /** | ||
84 | * Run the "hello world" CMD. | ||
85 | * | ||
86 | * @param cls closure. | ||
87 | * @param cmd CMD being run. | ||
88 | * @param is interpreter state. | ||
89 | */ | ||
90 | static void | ||
91 | hello_world_birth_run (void *cls, | ||
92 | const struct GNUNET_TESTING_Command *cmd, | ||
93 | struct GNUNET_TESTING_Interpreter *is) | ||
94 | { | ||
95 | struct HelloWorldBirthState *hbs = cls; | ||
96 | struct GNUNET_TIME_Relative relativ; | ||
97 | |||
98 | relativ = GNUNET_TIME_absolute_get_difference (*hbs->date, | ||
99 | GNUNET_TIME_absolute_get ()); | ||
100 | |||
101 | if (0 == relativ.rel_value_us % 10) | ||
102 | { | ||
103 | hbs->what_am_i = "Hello World, I am a creature!"; | ||
104 | } | ||
105 | else if (0 == relativ.rel_value_us % 2) | ||
106 | { | ||
107 | hbs->what_am_i = "Hello World, I am a girl!"; | ||
108 | } | ||
109 | else | ||
110 | { | ||
111 | hbs->what_am_i = "Hello World, I am a boy!"; | ||
112 | } | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * Offer data from trait | ||
117 | * | ||
118 | * @param cmd command to extract the url from. | ||
119 | * @param pt which url is to be picked, in case | ||
120 | * multiple are offered. | ||
121 | * @param[out] url where to write the url. | ||
122 | * @return #GNUNET_OK on success. | ||
123 | */ | ||
124 | int | ||
125 | GNUNET_TESTING_get_trait_what_am_i (const struct | ||
126 | GNUNET_TESTING_Command *cmd, | ||
127 | char *what_am_i) | ||
128 | { | ||
129 | return cmd->traits (cmd->cls, | ||
130 | (const void **) what_am_i, | ||
131 | "what_am_i", | ||
132 | (unsigned int) 0); | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * Create command. | ||
137 | * | ||
138 | * @param label name for command. | ||
139 | * @param now when the command was started. | ||
140 | * @return command. | ||
141 | */ | ||
142 | struct GNUNET_TESTING_Command | ||
143 | GNUNET_TESTING_cmd_hello_world_birth (const char *label, | ||
144 | struct GNUNET_TIME_Absolute *now) | ||
145 | { | ||
146 | struct HelloWorldBirthState *hbs; | ||
147 | |||
148 | hbs = GNUNET_new (struct HelloWorldBirthState); | ||
149 | hbs->date = now; | ||
150 | |||
151 | struct GNUNET_TESTING_Command cmd = { | ||
152 | .cls = hbs, | ||
153 | .label = label, | ||
154 | .run = &hello_world_birth_run, | ||
155 | .cleanup = &hello_world_birth_cleanup, | ||
156 | .traits = &hello_world_birth_traits | ||
157 | }; | ||
158 | |||
159 | return cmd; | ||
160 | } | ||
diff --git a/src/testing/testing_api_loop.c b/src/testing/testing_api_loop.c new file mode 100644 index 000000000..f32f4061e --- /dev/null +++ b/src/testing/testing_api_loop.c | |||
@@ -0,0 +1,652 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2008, 2009, 2012 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 | /** | ||
22 | * @file testing/testing_api_loop.c | ||
23 | * @brief main interpreter loop for testcases | ||
24 | * @author Christian Grothoff (GNU Taler testing) | ||
25 | * @author Marcello Stanisci (GNU Taler testing) | ||
26 | * @author t3sserakt | ||
27 | */ | ||
28 | #include "platform.h" | ||
29 | #include "gnunet_testing_ng_lib.h" | ||
30 | |||
31 | /** | ||
32 | * Pipe used to communicate child death via signal. | ||
33 | * Must be global, as used in signal handler! | ||
34 | */ | ||
35 | static struct GNUNET_DISK_PipeHandle *sigpipe; | ||
36 | |||
37 | /** | ||
38 | * Lookup command by label. | ||
39 | * | ||
40 | * @param is interpreter state to search | ||
41 | * @param label label to look for | ||
42 | * @return NULL if command was not found | ||
43 | */ | ||
44 | const struct GNUNET_TESTING_Command * | ||
45 | GNUNET_TESTING_interpreter_lookup_command (struct | ||
46 | GNUNET_TESTING_Interpreter *is, | ||
47 | const char *label) | ||
48 | { | ||
49 | if (NULL == label) | ||
50 | { | ||
51 | GNUNET_log (GNUNET_ERROR_TYPE_WARNING, | ||
52 | "Attempt to lookup command for empty label\n"); | ||
53 | return NULL; | ||
54 | } | ||
55 | /* Search backwards as we most likely reference recent commands */ | ||
56 | for (int i = is->ip; i >= 0; i--) | ||
57 | { | ||
58 | const struct GNUNET_TESTING_Command *cmd = &is->commands[i]; | ||
59 | |||
60 | /* Give precedence to top-level commands. */ | ||
61 | if ( (NULL != cmd->label) && | ||
62 | (0 == strcmp (cmd->label, | ||
63 | label)) ) | ||
64 | return cmd; | ||
65 | |||
66 | if (GNUNET_TESTING_cmd_is_batch (cmd)) | ||
67 | { | ||
68 | #define BATCH_INDEX 1 | ||
69 | struct GNUNET_TESTING_Command *batch; | ||
70 | struct GNUNET_TESTING_Command *current; | ||
71 | struct GNUNET_TESTING_Command *icmd; | ||
72 | const struct GNUNET_TESTING_Command *match; | ||
73 | |||
74 | current = GNUNET_TESTING_cmd_batch_get_current (cmd); | ||
75 | GNUNET_assert (GNUNET_OK == | ||
76 | GNUNET_TESTING_get_trait_cmd (cmd, | ||
77 | BATCH_INDEX, | ||
78 | &batch)); | ||
79 | /* We must do the loop forward, but we can find the last match */ | ||
80 | match = NULL; | ||
81 | for (unsigned int j = 0; | ||
82 | NULL != (icmd = &batch[j])->label; | ||
83 | j++) | ||
84 | { | ||
85 | if (current == icmd) | ||
86 | break; /* do not go past current command */ | ||
87 | if ( (NULL != icmd->label) && | ||
88 | (0 == strcmp (icmd->label, | ||
89 | label)) ) | ||
90 | match = icmd; | ||
91 | } | ||
92 | if (NULL != match) | ||
93 | return match; | ||
94 | } | ||
95 | } | ||
96 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
97 | "Command not found: %s\n", | ||
98 | label); | ||
99 | return NULL; | ||
100 | |||
101 | } | ||
102 | |||
103 | |||
104 | /** | ||
105 | * Run the main interpreter loop that performs exchange operations. | ||
106 | * | ||
107 | * @param cls contains the `struct InterpreterState` | ||
108 | */ | ||
109 | static void | ||
110 | interpreter_run (void *cls); | ||
111 | |||
112 | |||
113 | /** | ||
114 | * Current command is done, run the next one. | ||
115 | */ | ||
116 | void | ||
117 | GNUNET_TESTING_interpreter_next (struct GNUNET_TESTING_Interpreter *is) | ||
118 | { | ||
119 | static unsigned long long ipc; | ||
120 | static struct GNUNET_TIME_Absolute last_report; | ||
121 | struct GNUNET_TESTING_Command *cmd = &is->commands[is->ip]; | ||
122 | |||
123 | if (GNUNET_SYSERR == is->result) | ||
124 | return; /* ignore, we already failed! */ | ||
125 | if (GNUNET_TESTING_cmd_is_batch (cmd)) | ||
126 | { | ||
127 | GNUNET_TESTING_cmd_batch_next (is); | ||
128 | } | ||
129 | else | ||
130 | { | ||
131 | cmd->finish_time = GNUNET_TIME_absolute_get (); | ||
132 | is->ip++; | ||
133 | } | ||
134 | if (0 == (ipc % 1000)) | ||
135 | { | ||
136 | if (0 != ipc) | ||
137 | GNUNET_log (GNUNET_ERROR_TYPE_MESSAGE, | ||
138 | "Interpreter executed 1000 instructions in %s\n", | ||
139 | GNUNET_STRINGS_relative_time_to_string ( | ||
140 | GNUNET_TIME_absolute_get_duration (last_report), | ||
141 | GNUNET_YES)); | ||
142 | last_report = GNUNET_TIME_absolute_get (); | ||
143 | } | ||
144 | ipc++; | ||
145 | is->task = GNUNET_SCHEDULER_add_now (&interpreter_run, | ||
146 | is); | ||
147 | } | ||
148 | |||
149 | |||
150 | /** | ||
151 | * Current command failed, clean up and fail the test case. | ||
152 | * | ||
153 | * @param is interpreter of the test | ||
154 | */ | ||
155 | void | ||
156 | GNUNET_TESTING_interpreter_fail (struct GNUNET_TESTING_Interpreter *is) | ||
157 | { | ||
158 | struct GNUNET_TESTING_Command *cmd = &is->commands[is->ip]; | ||
159 | |||
160 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
161 | "Failed at command `%s'\n", | ||
162 | cmd->label); | ||
163 | while (GNUNET_TESTING_cmd_is_batch (cmd)) | ||
164 | { | ||
165 | cmd = GNUNET_TESTING_cmd_batch_get_current (cmd); | ||
166 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
167 | "Batch is at command `%s'\n", | ||
168 | cmd->label); | ||
169 | } | ||
170 | is->result = GNUNET_SYSERR; | ||
171 | GNUNET_SCHEDULER_shutdown (); | ||
172 | } | ||
173 | |||
174 | |||
175 | /** | ||
176 | * Create command array terminator. | ||
177 | * | ||
178 | * @return a end-command. | ||
179 | */ | ||
180 | struct GNUNET_TESTING_Command | ||
181 | GNUNET_TESTING_cmd_end (void) | ||
182 | { | ||
183 | static struct GNUNET_TESTING_Command cmd; | ||
184 | cmd.label = NULL; | ||
185 | |||
186 | return cmd; | ||
187 | } | ||
188 | |||
189 | |||
190 | /** | ||
191 | * Obtain current label. | ||
192 | */ | ||
193 | const char * | ||
194 | GNUNET_TESTING_interpreter_get_current_label (struct | ||
195 | GNUNET_TESTING_Interpreter *is) | ||
196 | { | ||
197 | struct GNUNET_TESTING_Command *cmd = &is->commands[is->ip]; | ||
198 | |||
199 | return cmd->label; | ||
200 | } | ||
201 | |||
202 | |||
203 | /** | ||
204 | * Run the main interpreter loop that performs exchange operations. | ||
205 | * | ||
206 | * @param cls contains the `struct GNUNET_TESTING_Interpreter` | ||
207 | */ | ||
208 | static void | ||
209 | interpreter_run (void *cls) | ||
210 | { | ||
211 | struct GNUNET_TESTING_Interpreter *is = cls; | ||
212 | struct GNUNET_TESTING_Command *cmd = &is->commands[is->ip]; | ||
213 | |||
214 | is->task = NULL; | ||
215 | |||
216 | if (NULL == cmd->label) | ||
217 | { | ||
218 | |||
219 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
220 | "Running command END\n"); | ||
221 | is->result = GNUNET_OK; | ||
222 | GNUNET_SCHEDULER_shutdown (); | ||
223 | return; | ||
224 | } | ||
225 | |||
226 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
227 | "Running command `%s'\n", | ||
228 | cmd->label); | ||
229 | cmd->start_time | ||
230 | = cmd->last_req_time | ||
231 | = GNUNET_TIME_absolute_get (); | ||
232 | cmd->num_tries = 1; | ||
233 | cmd->run (cmd->cls, | ||
234 | cmd, | ||
235 | is); | ||
236 | } | ||
237 | |||
238 | |||
239 | /** | ||
240 | * Function run when the test terminates (good or bad). | ||
241 | * Cleans up our state. | ||
242 | * | ||
243 | * @param cls the interpreter state. | ||
244 | */ | ||
245 | static void | ||
246 | do_shutdown (void *cls) | ||
247 | { | ||
248 | struct GNUNET_TESTING_Interpreter *is = cls; | ||
249 | struct GNUNET_TESTING_Command *cmd; | ||
250 | const char *label; | ||
251 | |||
252 | label = is->commands[is->ip].label; | ||
253 | if (NULL == label) | ||
254 | label = "END"; | ||
255 | |||
256 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
257 | "Executing shutdown at `%s'\n", | ||
258 | label); | ||
259 | |||
260 | for (unsigned int j = 0; | ||
261 | NULL != (cmd = &is->commands[j])->label; | ||
262 | j++) | ||
263 | cmd->cleanup (cmd->cls, | ||
264 | cmd); | ||
265 | |||
266 | if (NULL != is->task) | ||
267 | { | ||
268 | GNUNET_SCHEDULER_cancel (is->task); | ||
269 | is->task = NULL; | ||
270 | } | ||
271 | if (NULL != is->timeout_task) | ||
272 | { | ||
273 | GNUNET_SCHEDULER_cancel (is->timeout_task); | ||
274 | is->timeout_task = NULL; | ||
275 | } | ||
276 | if (NULL != is->child_death_task) | ||
277 | { | ||
278 | GNUNET_SCHEDULER_cancel (is->child_death_task); | ||
279 | is->child_death_task = NULL; | ||
280 | } | ||
281 | GNUNET_free (is->commands); | ||
282 | } | ||
283 | |||
284 | |||
285 | /** | ||
286 | * Function run when the test terminates (good or bad) with timeout. | ||
287 | * | ||
288 | * @param cls NULL | ||
289 | */ | ||
290 | static void | ||
291 | do_timeout (void *cls) | ||
292 | { | ||
293 | struct GNUNET_TESTING_Interpreter *is = cls; | ||
294 | |||
295 | is->timeout_task = NULL; | ||
296 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
297 | "Terminating test due to timeout\n"); | ||
298 | GNUNET_SCHEDULER_shutdown (); | ||
299 | } | ||
300 | |||
301 | |||
302 | /** | ||
303 | * Task triggered whenever we receive a SIGCHLD (child | ||
304 | * process died). | ||
305 | * | ||
306 | * @param cls closure | ||
307 | */ | ||
308 | static void | ||
309 | maint_child_death (void *cls) | ||
310 | { | ||
311 | struct GNUNET_TESTING_Interpreter *is = cls; | ||
312 | struct GNUNET_TESTING_Command *cmd = &is->commands[is->ip]; | ||
313 | const struct GNUNET_DISK_FileHandle *pr; | ||
314 | struct GNUNET_OS_Process **processp; | ||
315 | char c[16]; | ||
316 | enum GNUNET_OS_ProcessStatusType type; | ||
317 | unsigned long code; | ||
318 | |||
319 | if (GNUNET_TESTING_cmd_is_batch (cmd)) | ||
320 | { | ||
321 | struct GNUNET_TESTING_Command *batch_cmd; | ||
322 | |||
323 | GNUNET_assert (GNUNET_OK == | ||
324 | GNUNET_TESTING_get_trait_cmd (cmd, | ||
325 | 0, | ||
326 | &batch_cmd)); | ||
327 | cmd = batch_cmd; | ||
328 | } | ||
329 | |||
330 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
331 | "Got SIGCHLD for `%s'.\n", | ||
332 | cmd->label); | ||
333 | is->child_death_task = NULL; | ||
334 | pr = GNUNET_DISK_pipe_handle (sigpipe, | ||
335 | GNUNET_DISK_PIPE_END_READ); | ||
336 | GNUNET_break (0 < | ||
337 | GNUNET_DISK_file_read (pr, | ||
338 | &c, | ||
339 | sizeof (c))); | ||
340 | if (GNUNET_OK != | ||
341 | GNUNET_TESTING_get_trait_process (cmd, | ||
342 | 0, | ||
343 | &processp)) | ||
344 | { | ||
345 | GNUNET_break (0); | ||
346 | GNUNET_TESTING_interpreter_fail (is); | ||
347 | return; | ||
348 | } | ||
349 | |||
350 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
351 | "Got the dead child process handle, waiting for termination ...\n"); | ||
352 | GNUNET_OS_process_wait_status (*processp, | ||
353 | &type, | ||
354 | &code); | ||
355 | GNUNET_OS_process_destroy (*processp); | ||
356 | *processp = NULL; | ||
357 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
358 | "... definitively terminated\n"); | ||
359 | switch (type) | ||
360 | { | ||
361 | case GNUNET_OS_PROCESS_UNKNOWN: | ||
362 | GNUNET_break (0); | ||
363 | GNUNET_TESTING_interpreter_fail (is); | ||
364 | return; | ||
365 | case GNUNET_OS_PROCESS_RUNNING: | ||
366 | GNUNET_break (0); | ||
367 | GNUNET_TESTING_interpreter_fail (is); | ||
368 | return; | ||
369 | case GNUNET_OS_PROCESS_STOPPED: | ||
370 | GNUNET_break (0); | ||
371 | GNUNET_TESTING_interpreter_fail (is); | ||
372 | return; | ||
373 | case GNUNET_OS_PROCESS_EXITED: | ||
374 | if (0 != code) | ||
375 | { | ||
376 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
377 | "Process exited with unexpected status %u\n", | ||
378 | (unsigned int) code); | ||
379 | GNUNET_TESTING_interpreter_fail (is); | ||
380 | return; | ||
381 | } | ||
382 | break; | ||
383 | case GNUNET_OS_PROCESS_SIGNALED: | ||
384 | GNUNET_break (0); | ||
385 | GNUNET_TESTING_interpreter_fail (is); | ||
386 | return; | ||
387 | } | ||
388 | |||
389 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, | ||
390 | "Dead child, go on with next command.\n"); | ||
391 | GNUNET_TESTING_interpreter_next (is); | ||
392 | } | ||
393 | |||
394 | |||
395 | /** | ||
396 | * Wait until we receive SIGCHLD signal. | ||
397 | * Then obtain the process trait of the current | ||
398 | * command, wait on the the zombie and continue | ||
399 | * with the next command. | ||
400 | */ | ||
401 | void | ||
402 | GNUNET_TESTING_wait_for_sigchld (struct GNUNET_TESTING_Interpreter *is) | ||
403 | { | ||
404 | const struct GNUNET_DISK_FileHandle *pr; | ||
405 | |||
406 | GNUNET_assert (NULL == is->child_death_task); | ||
407 | pr = GNUNET_DISK_pipe_handle (sigpipe, | ||
408 | GNUNET_DISK_PIPE_END_READ); | ||
409 | is->child_death_task | ||
410 | = GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL, | ||
411 | pr, | ||
412 | &maint_child_death, | ||
413 | is); | ||
414 | } | ||
415 | |||
416 | |||
417 | /** | ||
418 | * Run the testsuite. Note, CMDs are copied into | ||
419 | * the interpreter state because they are _usually_ | ||
420 | * defined into the "run" method that returns after | ||
421 | * having scheduled the test interpreter. | ||
422 | * | ||
423 | * @param is the interpreter state | ||
424 | * @param commands the list of command to execute | ||
425 | * @param timeout how long to wait | ||
426 | */ | ||
427 | void | ||
428 | GNUNET_TESTING_run2 (struct GNUNET_TESTING_Interpreter *is, | ||
429 | struct GNUNET_TESTING_Command *commands, | ||
430 | struct GNUNET_TIME_Relative timeout) | ||
431 | { | ||
432 | unsigned int i; | ||
433 | |||
434 | if (NULL != is->timeout_task) | ||
435 | { | ||
436 | GNUNET_SCHEDULER_cancel (is->timeout_task); | ||
437 | is->timeout_task = NULL; | ||
438 | } | ||
439 | /* get the number of commands */ | ||
440 | for (i = 0; NULL != commands[i].label; i++) | ||
441 | ; | ||
442 | is->commands = GNUNET_new_array (i + 1, | ||
443 | struct GNUNET_TESTING_Command); | ||
444 | memcpy (is->commands, | ||
445 | commands, | ||
446 | sizeof (struct GNUNET_TESTING_Command) * i); | ||
447 | is->timeout_task = GNUNET_SCHEDULER_add_delayed | ||
448 | (timeout, | ||
449 | &do_timeout, | ||
450 | is); | ||
451 | GNUNET_SCHEDULER_add_shutdown (&do_shutdown, is); | ||
452 | is->task = GNUNET_SCHEDULER_add_now (&interpreter_run, is); | ||
453 | } | ||
454 | |||
455 | |||
456 | /** | ||
457 | * Run the testsuite. Note, CMDs are copied into | ||
458 | * the interpreter state because they are _usually_ | ||
459 | * defined into the "run" method that returns after | ||
460 | * having scheduled the test interpreter. | ||
461 | * | ||
462 | * @param is the interpreter state | ||
463 | * @param commands the list of command to execute | ||
464 | */ | ||
465 | void | ||
466 | GNUNET_TESTING_run (struct GNUNET_TESTING_Interpreter *is, | ||
467 | struct GNUNET_TESTING_Command *commands) | ||
468 | { | ||
469 | GNUNET_TESTING_run2 (is, | ||
470 | commands, | ||
471 | GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, | ||
472 | 5)); | ||
473 | } | ||
474 | |||
475 | |||
476 | /** | ||
477 | * Information used by the wrapper around the main | ||
478 | * "run" method. | ||
479 | */ | ||
480 | struct MainContext | ||
481 | { | ||
482 | /** | ||
483 | * Main "run" method. | ||
484 | */ | ||
485 | GNUNET_TESTING_Main main_cb; | ||
486 | |||
487 | /** | ||
488 | * Closure for @e main_cb. | ||
489 | */ | ||
490 | void *main_cb_cls; | ||
491 | |||
492 | /** | ||
493 | * Interpreter state. | ||
494 | */ | ||
495 | struct GNUNET_TESTING_Interpreter *is; | ||
496 | }; | ||
497 | |||
498 | |||
499 | /** | ||
500 | * Signal handler called for SIGCHLD. Triggers the | ||
501 | * respective handler by writing to the trigger pipe. | ||
502 | */ | ||
503 | static void | ||
504 | sighandler_child_death (void) | ||
505 | { | ||
506 | static char c; | ||
507 | int old_errno = errno; /* back-up errno */ | ||
508 | |||
509 | GNUNET_break (1 == GNUNET_DISK_file_write | ||
510 | (GNUNET_DISK_pipe_handle (sigpipe, | ||
511 | GNUNET_DISK_PIPE_END_WRITE), | ||
512 | &c, sizeof (c))); | ||
513 | errno = old_errno; /* restore errno */ | ||
514 | } | ||
515 | |||
516 | |||
517 | /** | ||
518 | * Initialize scheduler loop and curl context for the testcase, | ||
519 | * and responsible to run the "run" method. | ||
520 | * | ||
521 | * @param cls closure, typically the "run" method, the | ||
522 | * interpreter state and a closure for "run". | ||
523 | */ | ||
524 | static void | ||
525 | main_wrapper_exchange_agnostic (void *cls) | ||
526 | { | ||
527 | struct MainContext *main_ctx = cls; | ||
528 | |||
529 | main_ctx->main_cb (main_ctx->main_cb_cls, | ||
530 | main_ctx->is); | ||
531 | } | ||
532 | |||
533 | |||
534 | /** | ||
535 | * Function run when the test is aborted before we launch the actual | ||
536 | * interpreter. Cleans up our state. | ||
537 | * | ||
538 | * @param cls the main context | ||
539 | */ | ||
540 | static void | ||
541 | do_abort (void *cls) | ||
542 | { | ||
543 | struct MainContext *main_ctx = cls; | ||
544 | struct GNUNET_TESTING_Interpreter *is = main_ctx->is; | ||
545 | |||
546 | is->timeout_task = NULL; | ||
547 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
548 | "Executing abort prior to interpreter launch\n"); | ||
549 | } | ||
550 | |||
551 | |||
552 | /** | ||
553 | * Initialize scheduler loop and curl context for the testcase, | ||
554 | * and responsible to run the "run" method. | ||
555 | * | ||
556 | * @param cls a `struct MainContext *` | ||
557 | */ | ||
558 | static void | ||
559 | main_wrapper_exchange_connect (void *cls) | ||
560 | { | ||
561 | struct MainContext *main_ctx = cls; | ||
562 | struct GNUNET_TESTING_Interpreter *is = main_ctx->is; | ||
563 | char *exchange_url; | ||
564 | |||
565 | if (GNUNET_OK != | ||
566 | GNUNET_CONFIGURATION_get_value_string (is->cfg, | ||
567 | "exchange", | ||
568 | "BASE_URL", | ||
569 | &exchange_url)) | ||
570 | { | ||
571 | GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, | ||
572 | "exchange", | ||
573 | "BASE_URL"); | ||
574 | return; | ||
575 | } | ||
576 | is->timeout_task = GNUNET_SCHEDULER_add_shutdown (&do_abort, | ||
577 | main_ctx); | ||
578 | is->working = GNUNET_YES; | ||
579 | |||
580 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
581 | "Starting main test loop\n"); | ||
582 | main_ctx->main_cb (main_ctx->main_cb_cls, | ||
583 | is); | ||
584 | } | ||
585 | |||
586 | |||
587 | /** | ||
588 | * Install signal handlers plus schedules the main wrapper | ||
589 | * around the "run" method. | ||
590 | * | ||
591 | * @param main_cb the "run" method which contains all the | ||
592 | * commands. | ||
593 | * @param main_cb_cls a closure for "run", typically NULL. | ||
594 | * @param cfg configuration to use | ||
595 | * @param exchanged exchange process handle: will be put in the | ||
596 | * state as some commands - e.g. revoke - need to send | ||
597 | * signal to it, for example to let it know to reload the | ||
598 | * key state.. if NULL, the interpreter will run without | ||
599 | * trying to connect to the exchange first. | ||
600 | * @param exchange_connect #GNUNET_YES if the test should connect | ||
601 | * to the exchange, #GNUNET_NO otherwise | ||
602 | * @return #GNUNET_OK if all is okay, != #GNUNET_OK otherwise. | ||
603 | * non-GNUNET_OK codes are #GNUNET_SYSERR most of the | ||
604 | * times. | ||
605 | */ | ||
606 | int | ||
607 | GNUNET_TESTING_setup (GNUNET_TESTING_Main main_cb, | ||
608 | void *main_cb_cls, | ||
609 | const struct GNUNET_CONFIGURATION_Handle *cfg, | ||
610 | struct GNUNET_OS_Process *exchanged, | ||
611 | int exchange_connect) | ||
612 | { | ||
613 | struct GNUNET_TESTING_Interpreter is; | ||
614 | struct MainContext main_ctx = { | ||
615 | .main_cb = main_cb, | ||
616 | .main_cb_cls = main_cb_cls, | ||
617 | /* needed to init the curl ctx */ | ||
618 | .is = &is, | ||
619 | }; | ||
620 | struct GNUNET_SIGNAL_Context *shc_chld; | ||
621 | |||
622 | memset (&is, | ||
623 | 0, | ||
624 | sizeof (is)); | ||
625 | is.exchanged = exchanged; | ||
626 | is.cfg = cfg; | ||
627 | sigpipe = GNUNET_DISK_pipe (GNUNET_DISK_PF_NONE); | ||
628 | GNUNET_assert (NULL != sigpipe); | ||
629 | shc_chld = GNUNET_SIGNAL_handler_install | ||
630 | (GNUNET_SIGCHLD, | ||
631 | &sighandler_child_death); | ||
632 | |||
633 | |||
634 | /* Blocking */ | ||
635 | if (GNUNET_YES == exchange_connect) | ||
636 | GNUNET_SCHEDULER_run (&main_wrapper_exchange_connect, | ||
637 | &main_ctx); | ||
638 | else | ||
639 | GNUNET_SCHEDULER_run (&main_wrapper_exchange_agnostic, | ||
640 | &main_ctx); | ||
641 | if (NULL != is.final_cleanup_cb) | ||
642 | is.final_cleanup_cb (is.final_cleanup_cb_cls); | ||
643 | GNUNET_SIGNAL_handler_uninstall (shc_chld); | ||
644 | GNUNET_DISK_pipe_close (sigpipe); | ||
645 | sigpipe = NULL; | ||
646 | GNUNET_free (is.auditor_url); | ||
647 | GNUNET_free (is.exchange_url); | ||
648 | return is.result; | ||
649 | } | ||
650 | |||
651 | |||
652 | /* end of testing_api_loop.c */ | ||
diff --git a/src/testing/testing_api_trait_cmd.c b/src/testing/testing_api_trait_cmd.c new file mode 100644 index 000000000..67b26cb3e --- /dev/null +++ b/src/testing/testing_api_trait_cmd.c | |||
@@ -0,0 +1,79 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2008, 2009, 2012 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 | /** | ||
22 | * @file testing/testing_api_trait_cmd.c | ||
23 | * @brief offers CMDs as traits. | ||
24 | * @author Marcello Stanisci (GNU Taler testing) | ||
25 | * @author t3sserakt | ||
26 | */ | ||
27 | #include "platform.h" | ||
28 | #include "gnunet_testing_ng_lib.h" | ||
29 | |||
30 | #define GNUNET_TESTING_TRAIT_CMD "cmd" | ||
31 | |||
32 | |||
33 | /** | ||
34 | * Obtain a command from @a cmd. | ||
35 | * | ||
36 | * @param cmd command to extract the command from. | ||
37 | * @param index always zero. Commands offering this | ||
38 | * kind of traits do not need this index. For | ||
39 | * example, a "batch" CMD returns always the | ||
40 | * CMD currently being executed. | ||
41 | * @param[out] _cmd where to write the wire details. | ||
42 | * @return #GNUNET_OK on success. | ||
43 | */ | ||
44 | int | ||
45 | GNUNET_TESTING_get_trait_cmd (const struct GNUNET_TESTING_Command *cmd, | ||
46 | unsigned int index, | ||
47 | struct GNUNET_TESTING_Command **_cmd) | ||
48 | { | ||
49 | return cmd->traits (cmd->cls, | ||
50 | (const void **) _cmd, | ||
51 | GNUNET_TESTING_TRAIT_CMD, | ||
52 | index); | ||
53 | } | ||
54 | |||
55 | |||
56 | /** | ||
57 | * Offer a command in a trait. | ||
58 | * | ||
59 | * @param index always zero. Commands offering this | ||
60 | * kind of traits do not need this index. For | ||
61 | * example, a "meta" CMD returns always the | ||
62 | * CMD currently being executed. | ||
63 | * @param cmd wire details to offer. | ||
64 | * @return the trait. | ||
65 | */ | ||
66 | struct GNUNET_TESTING_Trait | ||
67 | GNUNET_TESTING_make_trait_cmd (unsigned int index, | ||
68 | const struct GNUNET_TESTING_Command *cmd) | ||
69 | { | ||
70 | struct GNUNET_TESTING_Trait ret = { | ||
71 | .index = index, | ||
72 | .trait_name = GNUNET_TESTING_TRAIT_CMD, | ||
73 | .ptr = (const struct GNUNET_TESTING_Command *) cmd | ||
74 | }; | ||
75 | return ret; | ||
76 | } | ||
77 | |||
78 | |||
79 | /* end of testing_api_trait_cmd.c */ | ||
diff --git a/src/testing/testing_api_trait_process.c b/src/testing/testing_api_trait_process.c new file mode 100644 index 000000000..7d694e35d --- /dev/null +++ b/src/testing/testing_api_trait_process.c | |||
@@ -0,0 +1,81 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2008, 2009, 2012 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 | /** | ||
22 | * @file testing/testing_api_trait_process.c | ||
23 | * @brief trait offering process handles. | ||
24 | * @author Christian Grothoff (GNU Taler testing) | ||
25 | * @author Marcello Stanisci (GNU Taler testing) | ||
26 | * @author t3sserakt | ||
27 | */ | ||
28 | #include "platform.h" | ||
29 | #include "gnunet_testing_ng_lib.h" | ||
30 | |||
31 | #define GNUNET_TESTING_TRAIT_PROCESS "process" | ||
32 | |||
33 | |||
34 | /** | ||
35 | * Obtain location where a command stores a pointer to a process. | ||
36 | * | ||
37 | * @param cmd command to extract trait from. | ||
38 | * @param index which process to pick if @a cmd | ||
39 | * has multiple on offer. | ||
40 | * @param[out] processp set to the address of the pointer to the | ||
41 | * process. | ||
42 | * @return #GNUNET_OK on success. | ||
43 | */ | ||
44 | int | ||
45 | GNUNET_TESTING_get_trait_process | ||
46 | (const struct GNUNET_TESTING_Command *cmd, | ||
47 | unsigned int index, | ||
48 | struct GNUNET_OS_Process ***processp) | ||
49 | { | ||
50 | return cmd->traits (cmd->cls, | ||
51 | (const void **) processp, | ||
52 | GNUNET_TESTING_TRAIT_PROCESS, | ||
53 | index); | ||
54 | } | ||
55 | |||
56 | |||
57 | /** | ||
58 | * Offer location where a command stores a pointer to a process. | ||
59 | * | ||
60 | * @param index offered location index number, in case there are | ||
61 | * multiple on offer. | ||
62 | * @param processp process location to offer. | ||
63 | * | ||
64 | * @return the trait. | ||
65 | */ | ||
66 | struct GNUNET_TESTING_Trait | ||
67 | GNUNET_TESTING_make_trait_process | ||
68 | (unsigned int index, | ||
69 | struct GNUNET_OS_Process **processp) | ||
70 | { | ||
71 | struct GNUNET_TESTING_Trait ret = { | ||
72 | .index = index, | ||
73 | .trait_name = GNUNET_TESTING_TRAIT_PROCESS, | ||
74 | .ptr = (const void *) processp | ||
75 | }; | ||
76 | |||
77 | return ret; | ||
78 | } | ||
79 | |||
80 | |||
81 | /* end of testing_api_trait_process.c */ | ||
diff --git a/src/testing/testing_api_traits.c b/src/testing/testing_api_traits.c new file mode 100644 index 000000000..3af839b78 --- /dev/null +++ b/src/testing/testing_api_traits.c | |||
@@ -0,0 +1,81 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2008, 2009, 2012 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 | /** | ||
22 | * @file testing/testing_api_traits.c | ||
23 | * @brief loop for trait resolution | ||
24 | * @author Christian Grothoff (GNU Taler testing) | ||
25 | * @author Marcello Stanisci (GNU Taler testing) | ||
26 | * @author t3sserakt | ||
27 | */ | ||
28 | #include "platform.h" | ||
29 | #include "gnunet_testing_ng_lib.h" | ||
30 | |||
31 | |||
32 | /** | ||
33 | * End a trait array. Usually, commands offer several traits, | ||
34 | * and put them in arrays. | ||
35 | */ | ||
36 | struct GNUNET_TESTING_Trait | ||
37 | GNUNET_TESTING_trait_end () | ||
38 | { | ||
39 | struct GNUNET_TESTING_Trait end = { | ||
40 | .index = 0, | ||
41 | .trait_name = NULL, | ||
42 | .ptr = NULL | ||
43 | }; | ||
44 | |||
45 | return end; | ||
46 | } | ||
47 | |||
48 | |||
49 | /** | ||
50 | * Pick the chosen trait from the traits array. | ||
51 | * | ||
52 | * @param traits the traits array. | ||
53 | * @param ret where to store the result. | ||
54 | * @param trait type of the trait to extract. | ||
55 | * @param index index number of the object to extract. | ||
56 | * @return #GNUNET_OK if no error occurred, #GNUNET_SYSERR otherwise. | ||
57 | */ | ||
58 | int | ||
59 | GNUNET_TESTING_get_trait (const struct GNUNET_TESTING_Trait *traits, | ||
60 | const void **ret, | ||
61 | const char *trait, | ||
62 | unsigned int index) | ||
63 | { | ||
64 | for (unsigned int i = 0; NULL != traits[i].trait_name; i++) | ||
65 | { | ||
66 | if ( (0 == strcmp (trait, traits[i].trait_name)) && | ||
67 | (index == traits[i].index) ) | ||
68 | { | ||
69 | *ret = (void *) traits[i].ptr; | ||
70 | return GNUNET_OK; | ||
71 | } | ||
72 | } | ||
73 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
74 | "Trait %s/%u not found.\n", | ||
75 | trait, index); | ||
76 | |||
77 | return GNUNET_SYSERR; | ||
78 | } | ||
79 | |||
80 | |||
81 | /* end of testing_api_traits.c */ | ||