diff options
Diffstat (limited to 'src/testing/testing_api_cmd_local_test_prepared.c')
-rw-r--r-- | src/testing/testing_api_cmd_local_test_prepared.c | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/src/testing/testing_api_cmd_local_test_prepared.c b/src/testing/testing_api_cmd_local_test_prepared.c new file mode 100644 index 000000000..4e915c7c0 --- /dev/null +++ b/src/testing/testing_api_cmd_local_test_prepared.c | |||
@@ -0,0 +1,168 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2021 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_api_cmd_local_test_prepared.c | ||
23 | * @brief cmd to block the interpreter loop until all peers started. | ||
24 | * @author t3sserakt | ||
25 | */ | ||
26 | #include "platform.h" | ||
27 | #include "gnunet_util_lib.h" | ||
28 | #include "gnunet_testing_ng_lib.h" | ||
29 | #include "testing_cmds.h" | ||
30 | |||
31 | /** | ||
32 | * Generic logging shortcut | ||
33 | */ | ||
34 | #define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__) | ||
35 | |||
36 | |||
37 | /** | ||
38 | * Struct to hold information for callbacks. | ||
39 | * | ||
40 | */ | ||
41 | struct LocalPreparedState | ||
42 | { | ||
43 | /** | ||
44 | * Callback to write messages to the master loop. | ||
45 | * | ||
46 | */ | ||
47 | TESTING_CMD_HELPER_write_cb write_message; | ||
48 | |||
49 | /** | ||
50 | * The message send back to the master loop. | ||
51 | * | ||
52 | */ | ||
53 | struct GNUNET_CMDS_LOCAL_TEST_PREPARED *reply; | ||
54 | |||
55 | /** | ||
56 | * Flag indicating if all local tests finished. | ||
57 | */ | ||
58 | unsigned int *all_local_tests_prepared; | ||
59 | }; | ||
60 | |||
61 | |||
62 | /** | ||
63 | * Trait function of this cmd does nothing. | ||
64 | * | ||
65 | */ | ||
66 | static int | ||
67 | local_test_prepared_traits (void *cls, | ||
68 | const void **ret, | ||
69 | const char *trait, | ||
70 | unsigned int index) | ||
71 | { | ||
72 | return GNUNET_OK; | ||
73 | } | ||
74 | |||
75 | |||
76 | /** | ||
77 | * The cleanup function of this cmd frees resources the cmd allocated. | ||
78 | * | ||
79 | */ | ||
80 | static void | ||
81 | local_test_prepared_cleanup (void *cls, | ||
82 | const struct GNUNET_TESTING_Command *cmd) | ||
83 | { | ||
84 | struct LocalPreparedState *lfs = cls; | ||
85 | |||
86 | GNUNET_free (lfs->reply); | ||
87 | GNUNET_free (lfs); | ||
88 | } | ||
89 | |||
90 | |||
91 | /** | ||
92 | * This function sends a GNUNET_MESSAGE_TYPE_CMDS_HELPER_LOCAL_TESTS_PREPARED message to the master loop. | ||
93 | * | ||
94 | */ | ||
95 | static void | ||
96 | local_test_prepared_run (void *cls, | ||
97 | const struct GNUNET_TESTING_Command *cmd, | ||
98 | struct GNUNET_TESTING_Interpreter *is) | ||
99 | { | ||
100 | struct LocalPreparedState *lfs = cls; | ||
101 | |||
102 | struct GNUNET_CMDS_LOCAL_TEST_PREPARED *reply; | ||
103 | size_t msg_length; | ||
104 | |||
105 | msg_length = sizeof(struct GNUNET_CMDS_LOCAL_TEST_PREPARED); | ||
106 | reply = GNUNET_new (struct GNUNET_CMDS_LOCAL_TEST_PREPARED); | ||
107 | reply->header.type = htons ( | ||
108 | GNUNET_MESSAGE_TYPE_CMDS_HELPER_LOCAL_TEST_PREPARED); | ||
109 | reply->header.size = htons ((uint16_t) msg_length); | ||
110 | lfs->reply = reply; | ||
111 | lfs->write_message ((struct GNUNET_MessageHeader *) reply, msg_length); | ||
112 | } | ||
113 | |||
114 | |||
115 | /** | ||
116 | * This finish function will stop the local loop without shutting down the scheduler, because we do not call the continuation, which is the interpreter_next method. | ||
117 | * | ||
118 | */ | ||
119 | static int | ||
120 | local_test_prepared_finish (void *cls, | ||
121 | GNUNET_SCHEDULER_TaskCallback cont, | ||
122 | void *cont_cls) | ||
123 | { | ||
124 | struct LocalPreparedState *lfs = cls; | ||
125 | unsigned int *ret = lfs->all_local_tests_prepared; | ||
126 | |||
127 | if (GNUNET_YES == *ret) | ||
128 | { | ||
129 | cont (cont_cls); | ||
130 | } | ||
131 | |||
132 | return *ret; | ||
133 | |||
134 | } | ||
135 | |||
136 | |||
137 | /** | ||
138 | * Create command. | ||
139 | * | ||
140 | * @param label name for command. | ||
141 | * @param write_message Callback to write messages to the master loop. | ||
142 | * @param all_local_tests_prepared Flag which will be set from outside. | ||
143 | * @return command. | ||
144 | */ | ||
145 | struct GNUNET_TESTING_Command | ||
146 | GNUNET_TESTING_cmd_local_test_prepared (const char *label, | ||
147 | TESTING_CMD_HELPER_write_cb | ||
148 | write_message, | ||
149 | unsigned int * | ||
150 | all_local_tests_prepared) | ||
151 | { | ||
152 | struct LocalPreparedState *lfs; | ||
153 | |||
154 | lfs = GNUNET_new (struct LocalPreparedState); | ||
155 | lfs->write_message = write_message; | ||
156 | lfs->all_local_tests_prepared = all_local_tests_prepared; | ||
157 | |||
158 | struct GNUNET_TESTING_Command cmd = { | ||
159 | .cls = lfs, | ||
160 | .label = label, | ||
161 | .run = &local_test_prepared_run, | ||
162 | .finish = &local_test_prepared_finish, | ||
163 | .cleanup = &local_test_prepared_cleanup, | ||
164 | .traits = &local_test_prepared_traits | ||
165 | }; | ||
166 | |||
167 | return cmd; | ||
168 | } | ||