aboutsummaryrefslogtreecommitdiff
path: root/src/testing/testing_api_cmd_barrier.c
blob: 9dd154059c5f3897ddf7c2f9eff0c0e3d1ed9726 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
      This file is part of GNUnet
      Copyright (C) 2022 GNUnet e.V.

      GNUnet is free software: you can redistribute it and/or modify it
      under the terms of the GNU Affero General Public License as published
      by the Free Software Foundation, either version 3 of the License,
      or (at your option) any later version.

      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Affero General Public License for more details.

      You should have received a copy of the GNU Affero General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.

     SPDX-License-Identifier: AGPL3.0-or-later
 */

/**
 * @file testing/testing_api_cmd_barrier.c
 * @brief Barrier functionality.
 * @author t3sserakt
 */
#include "platform.h"
#include "testing.h"
#include "gnunet_testing_ng_lib.h"
#include "gnunet_testing_plugin.h"
#include "gnunet_testing_netjail_lib.h"
#include "gnunet_testing_barrier.h"

/**
 * Generic logging shortcut
 */
#define LOG(kind, ...) GNUNET_log (kind, __VA_ARGS__)

struct BarrierState
{
  /*
   * Our barrier.
   */
  struct GNUNET_TESTING_Barrier *barrier;

  /*
 * Our label.
 */
  const char *label;
};

// FIXME Unused function
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)
{
  struct CommandBarrierAttached *atm = GNUNET_new (struct
                                                   CommandBarrierAttached);
  size_t msg_length = sizeof(struct CommandBarrierAttached);
  size_t name_len;

  name_len = strlen (barrier_name) + 1;
  atm->header.type = htons (GNUNET_MESSAGE_TYPE_CMDS_HELPER_BARRIER_ATTACHED);
  atm->header.size = htons ((uint16_t) msg_length);
  atm->expected_reaches = expected_reaches;
  atm->node_number = global_node_number;
  memcpy (&atm[1], barrier_name, name_len);
  write_message ((struct GNUNET_MessageHeader *) atm, msg_length);

  GNUNET_free (atm);
}


unsigned int
GNUNET_TESTING_barrier_crossable (struct GNUNET_TESTING_Barrier *barrier)
{
  unsigned int expected_reaches = barrier->expected_reaches;
  unsigned int reached = barrier->reached;
  double percentage_to_be_reached = barrier->percentage_to_be_reached;
  unsigned int number_to_be_reached = barrier->number_to_be_reached;
  double percentage_reached = (double) reached / expected_reaches * 100;

  LOG (GNUNET_ERROR_TYPE_DEBUG,
       "%u %f %f %u %u\n",
       expected_reaches,
       percentage_to_be_reached,
       percentage_reached,
       number_to_be_reached,
       reached);

  if (((0 < percentage_to_be_reached) &&
       (percentage_reached >= percentage_to_be_reached)) ||
      ((0 < number_to_be_reached) && (reached >= number_to_be_reached)))
  {
    return GNUNET_YES;
  }
  else
  {
    return GNUNET_NO;
  }
}


/**
 * Offer internal data from a "barrier" CMD, to other commands.
 *
 * @param cls closure.
 * @param[out] ret result.
 * @param trait name of the trait.
 * @param index index number of the object to offer.
 * @return #GNUNET_OK on success.
 */
static enum GNUNET_GenericReturnValue
barrier_traits (void *cls,
                const void **ret,
                const char *trait,
                unsigned int index)
{
  struct GNUNET_TESTING_Trait traits[] = {
    GNUNET_TESTING_trait_end ()
  };

  /* Always return current command.  */
  return GNUNET_TESTING_get_trait (traits,
                                   ret,
                                   trait,
                                   index);
}

/**
 * Cleanup the state from a "barrier" CMD, and possibly
 * cancel a pending operation thereof.
 *
 * @param cls closure.
 */
static void
barrier_cleanup (void *cls)
{
  struct BarrierState *brs = cls;

  GNUNET_free (brs);
}

/**
 * Run the command.
 *
 * @param cls closure.
 * @param is the interpreter state.
 */
static void
barrier_run (void *cls,
             struct GNUNET_TESTING_Interpreter *is)
{
  struct BarrierState *brs = cls;

  TST_interpreter_add_barrier (is, brs->barrier);
}

struct GNUNET_TESTING_NetjailNode *
GNUNET_TESTING_barrier_get_node (struct GNUNET_TESTING_Barrier *barrier,
                                 unsigned int node_number)
{
  struct GNUNET_HashCode hc;
  struct GNUNET_ShortHashCode key;

  GNUNET_CRYPTO_hash (&(node_number), sizeof(node_number), &hc);
  memcpy (&key,
          &hc,
          sizeof (key));
  return GNUNET_CONTAINER_multishortmap_get (barrier->nodes, &key);
}


struct GNUNET_TESTING_Command
GNUNET_TESTING_cmd_barrier_create (const char *label,
                                   double percentage_to_be_reached,
                                   unsigned int number_to_be_reached)
{
  struct GNUNET_TESTING_Barrier *barrier;
  struct BarrierState *bs;

  bs = GNUNET_new (struct BarrierState);
  bs->label = label;
  barrier = GNUNET_new (struct GNUNET_TESTING_Barrier);
  barrier->name = label;
  barrier->percentage_to_be_reached = percentage_to_be_reached;
  barrier->number_to_be_reached = number_to_be_reached;
  GNUNET_assert ((0 < percentage_to_be_reached && 0 == number_to_be_reached) ||
                 (0 ==  percentage_to_be_reached && 0 < number_to_be_reached));
  bs->barrier = barrier;
  return GNUNET_TESTING_command_new (bs, label,
                                     &barrier_run,
                                     &barrier_cleanup,
                                     &barrier_traits,
                                     NULL);
}