aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_quota_compliance.c
blob: c3c46db3785dc365fa374c8f7f477f75c4e52a86 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
     This file is part of GNUnet.
     Copyright (C) 2009, 2010, 2011, 2016 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 transport/test_quota_compliance.c
 * @brief base test case for transport implementations
 *
 * This test case tests quota compliance both on transport level
 */
#include "platform.h"
#include "gnunet_transport_service.h"
#include "gnunet_ats_service.h"
#include "gauger.h"
#include "transport-testing.h"

/**
 * Testcase timeout
 */
#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 480)

#define DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 80)


static struct GNUNET_SCHEDULER_Task *measure_task;

static char *gen_cfgs[2];

static unsigned long long quota_in[] = { 10000, 10000 };

static unsigned long long quota_out[] = { 10000, 10000 };

static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;


/**
 * Note that this value must not significantly exceed
 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
 * messages may be dropped even for a reliable transport.
 */
#define TOTAL_MSGS (1024 * 32)

static unsigned long long total_bytes_recv;

static struct GNUNET_TIME_Absolute start_time;


static void
report ()
{
  unsigned long long delta;
  unsigned long long datarate;

  delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
  if (0 == delta)
    delta = 1;
  datarate = (total_bytes_recv * 1000 * 1000) / delta;

  fprintf (stderr,
           "Throughput was %llu b/s\n",
           datarate);
  ccc->global_ret = GNUNET_OK;
  if (datarate > 1.5 * quota_in[1])
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Datarate of %llu b/s significantly higher than allowed inbound quota of %llu b/s\n",
                datarate,
                quota_in[1]);
    ccc->global_ret = GNUNET_SYSERR;
  }
  if (datarate > 1.5 * quota_out[0])
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Datarate of %llu b/s significantly higher than allowed outbound quota of %llu b/s\n",
                datarate,
                quota_out[0]);
    ccc->global_ret = GNUNET_SYSERR;
  }
  if (GNUNET_OK == ccc->global_ret)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Datarate of %llu b/s complied to allowed outbound quota of %llu b/s and inbound quota of %llu b/s\n",
                datarate,
                quota_out[0],
                quota_in[1]);
  }
}


static void
custom_shutdown (void *cls)
{
  if (NULL != measure_task)
  {
    GNUNET_SCHEDULER_cancel (measure_task);
    measure_task = NULL;
  }
  report ();
}


static size_t
get_size (unsigned int iter)
{
  size_t ret;

  ret = (iter * iter * iter) % 60000;
  ret += sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage);
  return ret;
}


static void
notify_receive (void *cls,
                struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
                const struct GNUNET_PeerIdentity *sender,
                const struct GNUNET_TRANSPORT_TESTING_TestMessage *hdr)
{
  if (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (hdr->header.type))
    return;
  total_bytes_recv += ntohs (hdr->header.size);

  {
    char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));

    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "Peer %u (`%s') got message %u of size %u from peer (`%s')\n",
                receiver->no,
                ps,
                ntohl (hdr->num),
                ntohs (hdr->header.size),
                GNUNET_i2s (sender));
    GNUNET_free (ps);
  }
}


static void
measure (void *cls)
{
  static int counter;

  measure_task = NULL;
  counter++;
  if ((DURATION.rel_value_us / 1000 / 1000LL) < counter)
  {
    fprintf (stderr, "%s", ".\n");
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  fprintf (stderr, "%s", ".");
  measure_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
                                               &measure,
                                               NULL);
}


static void
start_task (void *cls)
{
  static struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
    .num_messages = TOTAL_MSGS,
    .get_size_cb = &get_size
  };

  sc.ccc = ccc;
  measure_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
                                               &measure,
                                               NULL);
  start_time = GNUNET_TIME_absolute_get ();
  GNUNET_SCHEDULER_add_now (&GNUNET_TRANSPORT_TESTING_simple_send,
                            &sc);
}


static char *
generate_config (const char *cfg_file,
                 unsigned long long quota_in,
                 unsigned long long quota_out)
{
  char *in_name;
  char *out_name;
  char *fname = NULL;
  struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();

  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CONFIGURATION_load (cfg,
                                            cfg_file));
  GNUNET_asprintf (&fname,
                   "q_in_%llu_q_out_%llu_%s",
                   quota_in,
                   quota_out,
                   cfg_file);
  GNUNET_CONFIGURATION_set_value_string (cfg,
                                         "PATHS",
                                         "DEFAULTCONFIG",
                                         fname);
  for (int c = 0; c < GNUNET_NT_COUNT; c++)
  {
    GNUNET_asprintf (&in_name,
                     "%s_QUOTA_IN",
                     GNUNET_NT_to_string (c));
    GNUNET_asprintf (&out_name,
                     "%s_QUOTA_OUT",
                     GNUNET_NT_to_string (c));
    GNUNET_CONFIGURATION_set_value_number (cfg,
                                           "ats",
                                           in_name,
                                           quota_in);
    GNUNET_CONFIGURATION_set_value_number (cfg,
                                           "ats",
                                           out_name,
                                           quota_out);
    GNUNET_free (in_name);
    GNUNET_free (out_name);
  }
  GNUNET_assert (GNUNET_OK ==
                 GNUNET_CONFIGURATION_write (cfg,
                                             fname));
  GNUNET_CONFIGURATION_destroy (cfg);
  return fname;
}


static int
check (void *cls,
       struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
       const char *test_plugin_,
       const char *test_name_,
       unsigned int num_peers,
       char *cfg_files[])
{
  struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
    .connect_continuation = &start_task,
    .config_file = "test_quota_compliance_data.conf",
    .rec = &notify_receive,
    .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
    .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
    .shutdown_task = &custom_shutdown,
    .timeout = TIMEOUT
  };

  ccc = &my_ccc;

  if (NULL != strstr (test_name_,
                      "asymmetric"))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Running asymmetric test with sending peer unlimited, receiving peer (in/out): %llu/%llu b/s \n",
                quota_in[1],
                quota_out[1]);
    quota_out[0] = 1024 * 1024 * 1024;
    quota_in[0] = 1024 * 1024 * 1024;
  }
  else
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Running symmetric test with (in/out) %llu/%llu b/s \n",
                quota_in[1],
                quota_out[1]);
  }
  for (unsigned int i = 0; i < 2; i++)
  {
    gen_cfgs[i] = generate_config (cfg_files[i],
                                   quota_in[i],
                                   quota_out[i]);
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                "Generated config file `%s'\n",
                gen_cfgs[i]);
  }

  return GNUNET_TRANSPORT_TESTING_connect_check (&my_ccc,
                                                 tth_,
                                                 test_plugin_,
                                                 test_name_,
                                                 num_peers,
                                                 gen_cfgs);
}


int
main (int argc,
      char *argv[])
{
  if (GNUNET_OK !=
      GNUNET_TRANSPORT_TESTING_main (2,
                                     &check,
                                     NULL))
  {
    GNUNET_break (0);
    return 1;
  }
  for (unsigned int i = 0; i < 2; i++)
  {
    if ((NULL != gen_cfgs[i]) &&
        (GNUNET_YES == GNUNET_DISK_file_test (gen_cfgs[i])))
    {
      GNUNET_DISK_directory_remove (gen_cfgs[i]);
      GNUNET_free (gen_cfgs[i]);
    }
  }
  return 0;
}


/* end of test_quota_compliance.c */