aboutsummaryrefslogtreecommitdiff
path: root/src/cadet/test_cadeT_util.c
blob: bf65aca323c4e5fcd91795966deb4620b43aafed (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
/*
     This file is part of GNUnet.
     Copyright (C) 2009 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 cadet/test_cadeT_util.c
 * @brief testcase for cadet.c
 * @author xrs
 */

#include <test_cadeT_util.h>

/**
 * Testbed operation for connecting to the services. 
 */
static struct GNUNET_TESTBED_Operation *testbed_to_svc[REQUESTED_PEERS];

/**
 * Testbed operation for requesting peer information.
 */
static struct GNUNET_TESTBED_Operation *testbed_info_req[REQUESTED_PEERS];

/**
 * Port name kown by the two peers.
 */
static struct GNUNET_HashCode hashed_portname;

/**
 * Port handle.
 */
struct GNUNET_CADET_Port *port;

/**
 * Result of the test.
 */
int test_result = 0;

/**
 * Counter for gathering peerinformation.
 */
static int peerinfo_cnt = 0;

/************************** TESBED MANAGEMENT *****************************/

static void
shutdown_task (void *cls)
{
  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%s\n", __func__);

  for (int i=0; i<REQUESTED_PEERS; i++)
  {
    GNUNET_TESTBED_operation_done (testbed_to_svc[i]);
    GNUNET_TESTBED_operation_done (testbed_info_req[i]);
  }
}

static void
timeout ()
{
  GNUNET_SCHEDULER_shutdown ();
}

static void
disconnect_from_peer (void *cls,
                      void *op_result)
{
  struct GNUNET_CADET_Handle *cadet = op_result;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%s\n", __func__);

  if (0 == strncmp ("listening_peer", cls, sizeof (cls)))
    GNUNET_CADET_close_port (port);

  GNUNET_CADET_disconnect (cadet);
}

static void 
disconnect_channel (void *cls,
                    const struct GNUNET_CADET_Channel *channel)
{
  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%s\n", __func__);
}

static void *
setup_initiating_peer (void *cls,
                       const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct GNUNET_CADET_Handle *cadet;
  struct GNUNET_CADET_Channel *channel;

  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%s\n", __func__);

  cadet = GNUNET_CADET_connect (cfg);
  test_peers[0].cadet = cadet;

  if (NULL == cadet)
    GNUNET_SCHEDULER_shutdown ();

  channel = GNUNET_CADET_channel_create (cadet,
                                         NULL,
                                         &test_peers[1].id,
                                         &hashed_portname,
                                         NULL,
                                         &disconnect_channel,
                                         NULL);
  test_peers[0].channel = channel;

  return cadet;
}

static void *
handle_port_connects (void *cls,
                      struct GNUNET_CADET_Channel *channel,
                      const struct GNUNET_PeerIdentity *source)
{
  test_peers[1].channel = channel;
  return NULL;
}

static void 
handle_port_disconnects (void *cls, 
                         const struct GNUNET_CADET_Channel *channel)
{
}

static void *
setup_listening_peer (void *cls,
                      const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  struct GNUNET_CADET_Handle *cadet;
  struct GNUNET_MQ_MessageHandler msg_handlers[] = {
    GNUNET_MQ_hd_var_size (message,
                           GNUNET_MESSAGE_TYPE_DUMMY,
                           struct GNUNET_MessageHeader,
                           NULL),
    GNUNET_MQ_handler_end ()
  };

  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%s\n", __func__);
  
  cadet = GNUNET_CADET_connect (cfg);
  test_peers[1].cadet = cadet;

  if (NULL == cadet)
    GNUNET_SCHEDULER_shutdown ();

  GNUNET_CRYPTO_hash (PORTNAME, sizeof(PORTNAME), &hashed_portname);
  port = GNUNET_CADET_open_port (cadet, &hashed_portname,
                                 &handle_port_connects,
                                 NULL,
                                 NULL,
                                 &handle_port_disconnects,
                                 msg_handlers);

  return cadet;
}

static void
check_test_readyness (void *cls,
                      struct GNUNET_TESTBED_Operation *op,
                      void *ca_result,
                      const char *emsg)
{
  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%s\n", __func__);

  if (NULL != test_peers[0].cadet && NULL != test_peers[1].cadet)
    run_test();
}

static int
peerinfo_complete ()
{
  return (REQUESTED_PEERS == ++peerinfo_cnt) ? GNUNET_YES : GNUNET_NO;
}

static void
connect_to_service (void *cb_cls,
                    struct GNUNET_TESTBED_Operation *op,
                    const struct GNUNET_TESTBED_PeerInformation *pinfo,
                    const char *emsg)
{
  struct TEST_PEERS *test_peer = cb_cls;

  // Store peer ID.
  test_peer->id = *(pinfo->result.id);

  if (peerinfo_complete())
  {
    testbed_to_svc[1] = 
      GNUNET_TESTBED_service_connect (NULL, test_peers[1].testbed_peer,
                                      "cadet", 
                                      &check_test_readyness, NULL,
                                      &setup_listening_peer,
                                      &disconnect_from_peer, "listening_peer");
    testbed_to_svc[0] = 
      GNUNET_TESTBED_service_connect (NULL, test_peers[0].testbed_peer,
                                      "cadet",
                                      &check_test_readyness, NULL,
                                      &setup_initiating_peer,
                                      &disconnect_from_peer, "initiating_peer");
  }
}

void
prepare_test (void *cls,
              struct GNUNET_TESTBED_RunHandle *h,
              unsigned int num_peers,
              struct GNUNET_TESTBED_Peer **peers,
              unsigned int links_succeeded,
              unsigned int links_failed)
{
  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%s\n", __func__);

  GNUNET_assert (GNUNET_NO == links_failed);
  GNUNET_assert (REQUESTED_PEERS == num_peers);

  for (int i=0; i<REQUESTED_PEERS; i++)
  {
    test_peers[i].ready = GNUNET_NO;
    test_peers[i].idx = i;
    test_peers[i].testbed_peer = peers[i];
  }

  testbed_info_req[0] = GNUNET_TESTBED_peer_get_information (peers[0],
                                                             GNUNET_TESTBED_PIT_IDENTITY,
                                                             &connect_to_service,
                                                             &test_peers[0]);
  testbed_info_req[1] = GNUNET_TESTBED_peer_get_information (peers[1],
                                                             GNUNET_TESTBED_PIT_IDENTITY,
                                                             &connect_to_service,
                                                             &test_peers[1]);

  GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);

  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, TIMEOUT_IN_SEC), 
                                &timeout, NULL);
}