aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_channeltls.c
blob: f4f5cb447e82ba2e3a242b5e5c18045f9b3ae29d (plain)
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* Copyright (c) 2014-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

#include "orconfig.h"

#include <math.h>

#define CHANNEL_OBJECT_PRIVATE
#include "core/or/or.h"
#include "lib/net/address.h"
#include "lib/buf/buffers.h"
#include "core/or/channel.h"
#include "core/or/channeltls.h"
#include "core/mainloop/connection.h"
#include "core/or/connection_or.h"
#include "app/config/config.h"
#include "app/config/resolve_addr.h"
/* For init/free stuff */
#include "core/or/scheduler.h"
#include "lib/tls/tortls.h"

#include "core/or/or_connection_st.h"

/* Test suite stuff */
#include "test/test.h"
#include "test/fakechans.h"

/* The channeltls unit tests */
static void test_channeltls_create(void *arg);
static void test_channeltls_num_bytes_queued(void *arg);
static void test_channeltls_overhead_estimate(void *arg);

/* Mocks used by channeltls unit tests */
static size_t tlschan_buf_datalen_mock(const buf_t *buf);
static or_connection_t * tlschan_connection_or_connect_mock(
    const tor_addr_t *addr,
    uint16_t port,
    const char *digest,
    const ed25519_public_key_t *ed_id,
    channel_tls_t *tlschan);
static int tlschan_is_local_addr_mock(const tor_addr_t *addr);

/* Fake close method */
static void tlschan_fake_close_method(channel_t *chan);

/* Flags controlling behavior of channeltls unit test mocks */
static int tlschan_local = 0;
static const buf_t * tlschan_buf_datalen_mock_target = NULL;
static size_t tlschan_buf_datalen_mock_size = 0;

/* Thing to cast to fake tor_tls_t * to appease assert_connection_ok() */
static int fake_tortls = 0; /* Bleh... */

static void
test_channeltls_create(void *arg)
{
  tor_addr_t test_addr;
  channel_t *ch = NULL;
  const char test_digest[DIGEST_LEN] = {
    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
    0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14 };

  (void)arg;

  /* Set up a fake address to fake-connect to */
  test_addr.family = AF_INET;
  test_addr.addr.in_addr.s_addr = htonl(0x01020304);

  /* For this test we always want the address to be treated as non-local */
  tlschan_local = 0;
  /* Install is_local_addr() mock */
  MOCK(is_local_addr, tlschan_is_local_addr_mock);

  /* Install mock for connection_or_connect() */
  MOCK(connection_or_connect, tlschan_connection_or_connect_mock);

  /* Try connecting */
  ch = channel_tls_connect(&test_addr, 567, test_digest, NULL);
  tt_ptr_op(ch, OP_NE, NULL);

 done:
  if (ch) {
    MOCK(scheduler_release_channel, scheduler_release_channel_mock);
    /*
     * Use fake close method that doesn't try to do too much to fake
     * orconn
     */
    ch->close = tlschan_fake_close_method;
    channel_mark_for_close(ch);
    free_fake_channel(ch);
    UNMOCK(scheduler_release_channel);
  }

  UNMOCK(connection_or_connect);
  UNMOCK(is_local_addr);

  return;
}

static void
test_channeltls_num_bytes_queued(void *arg)
{
  tor_addr_t test_addr;
  channel_t *ch = NULL;
  const char test_digest[DIGEST_LEN] = {
    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
    0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14 };
  channel_tls_t *tlschan = NULL;
  size_t len;
  int fake_outbuf = 0, n;

  (void)arg;

  /* Set up a fake address to fake-connect to */
  test_addr.family = AF_INET;
  test_addr.addr.in_addr.s_addr = htonl(0x01020304);

  /* For this test we always want the address to be treated as non-local */
  tlschan_local = 0;
  /* Install is_local_addr() mock */
  MOCK(is_local_addr, tlschan_is_local_addr_mock);

  /* Install mock for connection_or_connect() */
  MOCK(connection_or_connect, tlschan_connection_or_connect_mock);

  /* Try connecting */
  ch = channel_tls_connect(&test_addr, 567, test_digest, NULL);
  tt_ptr_op(ch, OP_NE, NULL);

  /*
   * Next, we have to test ch->num_bytes_queued, which is
   * channel_tls_num_bytes_queued_method.  We can't mock
   * connection_get_outbuf_len() directly because it's static inline
   * in connection.h, but we can mock buf_datalen().
   */

  tt_assert(ch->num_bytes_queued != NULL);
  tlschan = BASE_CHAN_TO_TLS(ch);
  tt_ptr_op(tlschan, OP_NE, NULL);
  if (TO_CONN(tlschan->conn)->outbuf == NULL) {
    /* We need an outbuf to make sure buf_datalen() gets called */
    fake_outbuf = 1;
    TO_CONN(tlschan->conn)->outbuf = buf_new();
  }
  tlschan_buf_datalen_mock_target = TO_CONN(tlschan->conn)->outbuf;
  tlschan_buf_datalen_mock_size = 1024;
  MOCK(buf_datalen, tlschan_buf_datalen_mock);
  len = ch->num_bytes_queued(ch);
  tt_int_op(len, OP_EQ, tlschan_buf_datalen_mock_size);
  /*
   * We also cover num_cells_writeable here; since wide_circ_ids = 0 on
   * the fake tlschans, cell_network_size returns 512, and so with
   * tlschan_buf_datalen_mock_size == 1024, we should be able to write
   * ceil((OR_CONN_HIGHWATER - 1024) / 512) = ceil(OR_CONN_HIGHWATER / 512)
   * - 2 cells.
   */
  n = ch->num_cells_writeable(ch);
  tt_int_op(n, OP_EQ, CEIL_DIV(OR_CONN_HIGHWATER, 512) - 2);
  UNMOCK(buf_datalen);
  tlschan_buf_datalen_mock_target = NULL;
  tlschan_buf_datalen_mock_size = 0;
  if (fake_outbuf) {
    buf_free(TO_CONN(tlschan->conn)->outbuf);
    TO_CONN(tlschan->conn)->outbuf = NULL;
  }

 done:
  if (ch) {
    MOCK(scheduler_release_channel, scheduler_release_channel_mock);
    /*
     * Use fake close method that doesn't try to do too much to fake
     * orconn
     */
    ch->close = tlschan_fake_close_method;
    channel_mark_for_close(ch);
    free_fake_channel(ch);
    UNMOCK(scheduler_release_channel);
  }

  UNMOCK(connection_or_connect);
  UNMOCK(is_local_addr);

  return;
}

static void
test_channeltls_overhead_estimate(void *arg)
{
  tor_addr_t test_addr;
  channel_t *ch = NULL;
  const char test_digest[DIGEST_LEN] = {
    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
    0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14 };
  double r;
  channel_tls_t *tlschan = NULL;

  (void)arg;

  /* Set up a fake address to fake-connect to */
  test_addr.family = AF_INET;
  test_addr.addr.in_addr.s_addr = htonl(0x01020304);

  /* For this test we always want the address to be treated as non-local */
  tlschan_local = 0;
  /* Install is_local_addr() mock */
  MOCK(is_local_addr, tlschan_is_local_addr_mock);

  /* Install mock for connection_or_connect() */
  MOCK(connection_or_connect, tlschan_connection_or_connect_mock);

  /* Try connecting */
  ch = channel_tls_connect(&test_addr, 567, test_digest, NULL);
  tt_ptr_op(ch, OP_NE, NULL);

  /* First case: silly low ratios should get clamped to 1.0 */
  tlschan = BASE_CHAN_TO_TLS(ch);
  tt_ptr_op(tlschan, OP_NE, NULL);
  tlschan->conn->bytes_xmitted = 128;
  tlschan->conn->bytes_xmitted_by_tls = 64;
  r = ch->get_overhead_estimate(ch);
  tt_assert(fabs(r - 1.0) < 1E-12);

  tlschan->conn->bytes_xmitted_by_tls = 127;
  r = ch->get_overhead_estimate(ch);
  tt_assert(fabs(r - 1.0) < 1E-12);

  /* Now middle of the range */
  tlschan->conn->bytes_xmitted_by_tls = 192;
  r = ch->get_overhead_estimate(ch);
  tt_assert(fabs(r - 1.5) < 1E-12);

  /* Now above the 2.0 clamp */
  tlschan->conn->bytes_xmitted_by_tls = 257;
  r = ch->get_overhead_estimate(ch);
  tt_assert(fabs(r - 2.0) < 1E-12);

  tlschan->conn->bytes_xmitted_by_tls = 512;
  r = ch->get_overhead_estimate(ch);
  tt_assert(fabs(r - 2.0) < 1E-12);

 done:
  if (ch) {
    MOCK(scheduler_release_channel, scheduler_release_channel_mock);
    /*
     * Use fake close method that doesn't try to do too much to fake
     * orconn
     */
    ch->close = tlschan_fake_close_method;
    channel_mark_for_close(ch);
    free_fake_channel(ch);
    UNMOCK(scheduler_release_channel);
  }

  UNMOCK(connection_or_connect);
  UNMOCK(is_local_addr);

  return;
}

static size_t
tlschan_buf_datalen_mock(const buf_t *buf)
{
  if (buf != NULL && buf == tlschan_buf_datalen_mock_target) {
    return tlschan_buf_datalen_mock_size;
  } else {
    return buf_datalen__real(buf);
  }
}

static or_connection_t *
tlschan_connection_or_connect_mock(const tor_addr_t *addr,
                                   uint16_t port,
                                   const char *digest,
                                   const ed25519_public_key_t *ed_id,
                                   channel_tls_t *tlschan)
{
  or_connection_t *result = NULL;
  (void) ed_id; // XXXX Not yet used.

  tt_ptr_op(addr, OP_NE, NULL);
  tt_uint_op(port, OP_NE, 0);
  tt_ptr_op(digest, OP_NE, NULL);
  tt_ptr_op(tlschan, OP_NE, NULL);

  /* Make a fake orconn */
  result = tor_malloc_zero(sizeof(*result));
  result->base_.magic = OR_CONNECTION_MAGIC;
  result->base_.state = OR_CONN_STATE_OPEN;
  result->base_.type = CONN_TYPE_OR;
  result->base_.socket_family = addr->family;
  result->base_.address = tor_strdup("<fake>");
  memcpy(&(result->base_.addr), addr, sizeof(tor_addr_t));
  result->base_.port = port;
  memcpy(result->identity_digest, digest, DIGEST_LEN);
  result->chan = tlschan;
  memcpy(&(result->real_addr), addr, sizeof(tor_addr_t));
  result->tls = (tor_tls_t *)((void *)(&fake_tortls));

 done:
  return result;
}

static void
tlschan_fake_close_method(channel_t *chan)
{
  channel_tls_t *tlschan = NULL;

  tt_ptr_op(chan, OP_NE, NULL);
  tt_int_op(chan->magic, OP_EQ, TLS_CHAN_MAGIC);

  tlschan = BASE_CHAN_TO_TLS(chan);
  tt_ptr_op(tlschan, OP_NE, NULL);

  /* Just free the fake orconn */
  tor_free(tlschan->conn->base_.address);
  tor_free(tlschan->conn);

  channel_closed(chan);

 done:
  return;
}

static int
tlschan_is_local_addr_mock(const tor_addr_t *addr)
{
  tt_ptr_op(addr, OP_NE, NULL);

 done:
  return tlschan_local;
}

struct testcase_t channeltls_tests[] = {
  { "create", test_channeltls_create, TT_FORK, NULL, NULL },
  { "num_bytes_queued", test_channeltls_num_bytes_queued,
    TT_FORK, NULL, NULL },
  { "overhead_estimate", test_channeltls_overhead_estimate,
    TT_FORK, NULL, NULL },
  END_OF_TESTCASES
};