GNUnet  0.20.0
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"
28 #include "gnunet_testing_ng_lib.h"
29 #include "gnunet_testing_plugin.h"
31 #include "gnunet_testing_barrier.h"
32 
36 #define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)
37 
39 {
40  /*
41  * Our barrier.
42  */
44 
45  /*
46  * Our label.
47  */
48  const char *label;
49 };
50 
51 // FIXME Unused function
52 void
54  char *barrier_name,
55  unsigned int global_node_number,
56  unsigned int expected_reaches,
59 {
60  struct CommandBarrierAttached *atm = GNUNET_new (struct
62  size_t msg_length = sizeof(struct CommandBarrierAttached);
63  size_t name_len;
64 
65  name_len = strlen (barrier_name) + 1;
67  atm->header.size = htons ((uint16_t) msg_length);
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 
77 unsigned int
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 
87  "%u %f %f %u %u\n",
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 
116 static enum GNUNET_GenericReturnValue
117 barrier_traits (void *cls,
118  const void **ret,
119  const char *trait,
120  unsigned int index)
121 {
122  struct GNUNET_TESTING_Trait traits[] = {
124  };
125 
126  /* Always return current command. */
127  return GNUNET_TESTING_get_trait (traits,
128  ret,
129  trait,
130  index);
131 }
132 
133 
140 static void
141 barrier_cleanup (void *cls)
142 {
143  struct BarrierState *brs = cls;
144 
145  GNUNET_free (brs);
146 }
147 
148 
155 static void
156 barrier_run (void *cls,
158 {
159  struct BarrierState *brs = cls;
160 
162 }
163 
164 
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 
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;
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,
201  NULL);
202 }
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
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" trait.
enum GNUNET_GenericReturnValue GNUNET_TESTING_get_trait(const struct GNUNET_TESTING_Trait *traits, const void **ret, const char *trait, unsigned int index)
Extract a trait.
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.
@ GNUNET_YES
@ GNUNET_NO
#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
struct GNUNET_TESTING_Barrier * barrier
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.
char label[127+1]
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 TST_interpreter_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.
void GNUNET_TESTING_send_barrier_attach(struct GNUNET_TESTING_Interpreter *is, 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.
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.
static void barrier_cleanup(void *cls)
Cleanup the state from a "barrier" CMD, and possibly cancel a pending operation thereof.
#define LOG(kind,...)
Generic logging shortcut.
unsigned int 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.