aboutsummaryrefslogtreecommitdiff
path: root/src/service/testing/testing_api_cmd_barrier.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/testing/testing_api_cmd_barrier.c')
-rw-r--r--src/service/testing/testing_api_cmd_barrier.c202
1 files changed, 202 insertions, 0 deletions
diff --git a/src/service/testing/testing_api_cmd_barrier.c b/src/service/testing/testing_api_cmd_barrier.c
new file mode 100644
index 000000000..c9c43b178
--- /dev/null
+++ b/src/service/testing/testing_api_cmd_barrier.c
@@ -0,0 +1,202 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2022 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_barrier.c
23 * @brief Barrier functionality.
24 * @author t3sserakt
25 */
26#include "platform.h"
27#include "testing.h"
28#include "gnunet_testing_ng_lib.h"
29#include "gnunet_testing_plugin.h"
30#include "gnunet_testing_netjail_lib.h"
31#include "gnunet_testing_barrier.h"
32
33/**
34 * Generic logging shortcut
35 */
36#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
37
38struct BarrierState
39{
40 /*
41 * Our barrier.
42 */
43 struct GNUNET_TESTING_Barrier *barrier;
44
45 /*
46 * Our label.
47 */
48 const char *label;
49};
50
51// FIXME Unused function
52void
53GNUNET_TESTING_send_barrier_attach (struct GNUNET_TESTING_Interpreter *is,
54 char *barrier_name,
55 unsigned int global_node_number,
56 unsigned int expected_reaches,
57 GNUNET_TESTING_cmd_helper_write_cb
58 write_message)
59{
60 struct CommandBarrierAttached *atm = GNUNET_new (struct
61 CommandBarrierAttached);
62 size_t msg_length = sizeof(struct CommandBarrierAttached);
63 size_t name_len;
64
65 name_len = strlen (barrier_name) + 1;
66 atm->header.type = htons (GNUNET_MESSAGE_TYPE_CMDS_HELPER_BARRIER_ATTACHED);
67 atm->header.size = htons ((uint16_t) msg_length);
68 atm->expected_reaches = expected_reaches;
69 atm->node_number = global_node_number;
70 memcpy (&atm[1], barrier_name, name_len);
71 write_message ((struct GNUNET_MessageHeader *) atm, msg_length);
72
73 GNUNET_free (atm);
74}
75
76
77unsigned int
78GNUNET_TESTING_barrier_crossable (struct GNUNET_TESTING_Barrier *barrier)
79{
80 unsigned int expected_reaches = barrier->expected_reaches;
81 unsigned int reached = barrier->reached;
82 double percentage_to_be_reached = barrier->percentage_to_be_reached;
83 unsigned int number_to_be_reached = barrier->number_to_be_reached;
84 double percentage_reached = (double) reached / expected_reaches * 100;
85
86 LOG (GNUNET_ERROR_TYPE_DEBUG,
87 "%u %f %f %u %u\n",
88 expected_reaches,
89 percentage_to_be_reached,
90 percentage_reached,
91 number_to_be_reached,
92 reached);
93
94 if (((0 < percentage_to_be_reached) &&
95 (percentage_reached >= percentage_to_be_reached)) ||
96 ((0 < number_to_be_reached) && (reached >= number_to_be_reached)))
97 {
98 return GNUNET_YES;
99 }
100 else
101 {
102 return GNUNET_NO;
103 }
104}
105
106
107/**
108 * Offer internal data from a "barrier" CMD, to other commands.
109 *
110 * @param cls closure.
111 * @param[out] ret result.
112 * @param trait name of the trait.
113 * @param index index number of the object to offer.
114 * @return #GNUNET_OK on success.
115 */
116static enum GNUNET_GenericReturnValue
117barrier_traits (void *cls,
118 const void **ret,
119 const char *trait,
120 unsigned int index)
121{
122 struct GNUNET_TESTING_Trait traits[] = {
123 GNUNET_TESTING_trait_end ()
124 };
125
126 /* Always return current command. */
127 return GNUNET_TESTING_get_trait (traits,
128 ret,
129 trait,
130 index);
131}
132
133
134/**
135 * Cleanup the state from a "barrier" CMD, and possibly
136 * cancel a pending operation thereof.
137 *
138 * @param cls closure.
139 */
140static void
141barrier_cleanup (void *cls)
142{
143 struct BarrierState *brs = cls;
144
145 GNUNET_free (brs);
146}
147
148
149/**
150 * Run the command.
151 *
152 * @param cls closure.
153 * @param is the interpreter state.
154 */
155static void
156barrier_run (void *cls,
157 struct GNUNET_TESTING_Interpreter *is)
158{
159 struct BarrierState *brs = cls;
160
161 TST_interpreter_add_barrier (is, brs->barrier);
162}
163
164
165struct GNUNET_TESTING_NetjailNode *
166GNUNET_TESTING_barrier_get_node (struct GNUNET_TESTING_Barrier *barrier,
167 unsigned int node_number)
168{
169 struct GNUNET_HashCode hc;
170 struct GNUNET_ShortHashCode key;
171
172 GNUNET_CRYPTO_hash (&(node_number), sizeof(node_number), &hc);
173 memcpy (&key,
174 &hc,
175 sizeof (key));
176 return GNUNET_CONTAINER_multishortmap_get (barrier->nodes, &key);
177}
178
179
180struct GNUNET_TESTING_Command
181GNUNET_TESTING_cmd_barrier_create (const char *label,
182 double percentage_to_be_reached,
183 unsigned int number_to_be_reached)
184{
185 struct GNUNET_TESTING_Barrier *barrier;
186 struct BarrierState *bs;
187
188 bs = GNUNET_new (struct BarrierState);
189 bs->label = label;
190 barrier = GNUNET_new (struct GNUNET_TESTING_Barrier);
191 barrier->name = label;
192 barrier->percentage_to_be_reached = percentage_to_be_reached;
193 barrier->number_to_be_reached = number_to_be_reached;
194 GNUNET_assert ((0 < percentage_to_be_reached && 0 == number_to_be_reached) ||
195 (0 == percentage_to_be_reached && 0 < number_to_be_reached));
196 bs->barrier = barrier;
197 return GNUNET_TESTING_command_new (bs, label,
198 &barrier_run,
199 &barrier_cleanup,
200 &barrier_traits,
201 NULL);
202}