GNUnet 0.21.1
testing_api_cmd_barrier.c
Go to the documentation of this file.
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
26#include "platform.h"
27#include "testing.h"
32
36#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
37
38
40{
45
49 const char *label;
50};
51
52
53// FIXME Unused function
54void
57 const char *barrier_name,
58 unsigned int global_node_number,
59 unsigned int expected_reaches,
61{
62 // FIXME: avoid useless malloc!
63 struct CommandBarrierAttached *atm = GNUNET_new (struct
65 size_t msg_length = sizeof(struct CommandBarrierAttached);
66 size_t name_len;
67
68 name_len = strlen (barrier_name) + 1;
70 atm->header.size = htons ((uint16_t) msg_length);
72 atm->node_number = global_node_number;
73 memcpy (&atm[1], barrier_name, name_len);
74 write_message ((struct GNUNET_MessageHeader *) atm, msg_length);
75
76 GNUNET_free (atm);
77}
78
79
80bool
82{
83 unsigned int expected_reaches = barrier->expected_reaches;
84 unsigned int reached = barrier->reached;
85 double percentage_to_be_reached = barrier->percentage_to_be_reached;
86 unsigned int number_to_be_reached = barrier->number_to_be_reached;
87 double percentage_reached = (double) reached / expected_reaches * 100;
88
90 "%u %f %f %u %u\n",
92 percentage_to_be_reached,
93 percentage_reached,
94 number_to_be_reached,
95 reached);
96
97 return ( ( (0 < percentage_to_be_reached) &&
98 (percentage_reached >= percentage_to_be_reached) ) ||
99 ( (0 < number_to_be_reached) &&
100 (reached >= number_to_be_reached) ) );
101}
102
103
114barrier_traits (void *cls,
115 const void **ret,
116 const char *trait,
117 unsigned int index)
118{
119 struct GNUNET_TESTING_Trait traits[] = {
121 };
122
123 return GNUNET_TESTING_get_trait (traits,
124 ret,
125 trait,
126 index);
127}
128
129
136static void
138{
139 struct BarrierState *brs = cls;
140
141 GNUNET_free (brs->barrier);
142 GNUNET_free (brs);
143}
144
145
152static void
153barrier_run (void *cls,
155{
156 struct BarrierState *brs = cls;
157
159 brs->barrier);
160 brs->barrier = NULL;
161}
162
163
166 unsigned int node_number)
167{
168 struct GNUNET_HashCode hc;
170
171 GNUNET_CRYPTO_hash (&node_number,
172 sizeof(node_number),
173 &hc);
174 memcpy (&key,
175 &hc,
176 sizeof (key));
178 &key);
179}
180
181
184 double percentage_to_be_reached,
185 unsigned int number_to_be_reached)
186{
187 struct GNUNET_TESTING_Barrier *barrier;
188 struct BarrierState *bs;
189
190 bs = GNUNET_new (struct BarrierState);
191 bs->label = label;
193 barrier->name = label;
194 barrier->percentage_to_be_reached = percentage_to_be_reached;
195 barrier->number_to_be_reached = number_to_be_reached;
196 GNUNET_assert ((0 < percentage_to_be_reached &&
197 0 == number_to_be_reached) ||
198 (0 == percentage_to_be_reached &&
199 0 < number_to_be_reached));
200 bs->barrier = barrier;
205 NULL);
206}
static int ret
Final status code.
Definition: gnunet-arm.c:94
struct GNUNET_TESTING_Interpreter * is
struct GNUNET_HashCode key
The key used in the DHT.
static int write_message(uint16_t message_type, const char *data, size_t data_length)
Write message to the master process.
API to manage barriers.
struct GNUNET_TESTING_Command GNUNET_TESTING_command_new(void *cls, const char *label, GNUNET_TESTING_CommandRunRoutine run, GNUNET_TESTING_CommandCleanupRoutine cleanup, GNUNET_TESTING_CommandGetTraits traits, struct GNUNET_TESTING_AsyncContext *ac)
Create a new command.
struct GNUNET_TESTING_Trait GNUNET_TESTING_trait_end(void)
"end" of traits array.
enum GNUNET_GenericReturnValue GNUNET_TESTING_get_trait(const struct GNUNET_TESTING_Trait *traits, const void **ret, const char *trait, unsigned int index)
Obtain value of a trait from a command.
void(* GNUNET_TESTING_cmd_helper_write_cb)(struct GNUNET_MessageHeader *message, size_t msg_length)
Callback function to write messages from the helper process running on a netjail node to the master p...
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:41
void * GNUNET_CONTAINER_multishortmap_get(const struct GNUNET_CONTAINER_MultiShortmap *map, const struct GNUNET_ShortHashCode *key)
Given a key find a value in the map matching the key.
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_MESSAGE_TYPE_CMDS_HELPER_BARRIER_ATTACHED
const char * label
Our label.
struct GNUNET_TESTING_Barrier * barrier
Our barrier, set to NULL once the barrier is active.
Message send by a child loop to inform the master loop how much GNUNET_CMDS_BARRIER_REACHED messages ...
Definition: testing.h:49
uint32_t expected_reaches
How often the child loop will reach the barrier.
Definition: testing.h:58
uint32_t node_number
The number of the node the barrier is running on.
Definition: testing.h:63
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_CMDS_HELPER_BARRIER_ATTACHED.
Definition: testing.h:53
A 512-bit hashcode.
Header for all communications.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
A 256-bit hashcode.
const char * name
Name of the barrier.
Definition: testing.h:182
unsigned int number_to_be_reached
Number of commands which need to reach the barrier to change state.
Definition: testing.h:209
struct GNUNET_CONTAINER_MultiShortmap * nodes
Hash map containing the global known nodes which are not natted.
Definition: testing.h:177
double percentage_to_be_reached
Percentage of of commands which need to reach the barrier to change state.
Definition: testing.h:203
unsigned int reached
Number of commands which reached this barrier.
Definition: testing.h:197
unsigned int expected_reaches
Number of commands attached to this barrier.
Definition: testing.h:192
A command to be run by the interpreter.
struct GNUNET_TESTING_CommandLabel label
Label for the command.
Global state of the interpreter, used by a command to access information about other commands.
Node in the netjail topology.
unsigned int node_number
The overall number of the node in the whole test system.
A struct GNUNET_TESTING_Trait can be used to exchange data between cmds.
unsigned int index
Index number associated with the trait.
void GNUNET_TESTING_add_barrier_(struct GNUNET_TESTING_Interpreter *is, struct GNUNET_TESTING_Barrier *barrier)
Add a barrier to the loop.
struct GNUNET_TESTING_Command GNUNET_TESTING_cmd_barrier_create(const char *label, double percentage_to_be_reached, unsigned int number_to_be_reached)
Command to create a barrier.
static enum GNUNET_GenericReturnValue barrier_traits(void *cls, const void **ret, const char *trait, unsigned int index)
Offer internal data from a "barrier" CMD, to other commands.
static void barrier_cleanup(void *cls)
Cleanup the state from a "barrier" CMD, and possibly cancel a pending operation thereof.
struct GNUNET_TESTING_NetjailNode * GNUNET_TESTING_barrier_get_node(struct GNUNET_TESTING_Barrier *barrier, unsigned int node_number)
Getting a node from a map by global node number.
void GNUNET_TESTING_send_barrier_attach_(struct GNUNET_TESTING_Interpreter *is, const char *barrier_name, unsigned int global_node_number, unsigned int expected_reaches, GNUNET_TESTING_cmd_helper_write_cb write_message)
Send message to master loop that cmds being attached to a barrier.
#define LOG(kind,...)
Generic logging shortcut.
bool GNUNET_TESTING_barrier_crossable_(struct GNUNET_TESTING_Barrier *barrier)
This function checks, if a barrier can be crossed, which actually means that the cmd representing the...
static void barrier_run(void *cls, struct GNUNET_TESTING_Interpreter *is)
Run the command.