aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_dispatch.c
blob: 886f45868e9ab36ffb791fe10bcbdf8fe2d5f63b (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
/* Copyright (c) 2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */

#define DISPATCH_PRIVATE

#include "test/test.h"

#include "lib/dispatch/dispatch.h"
#include "lib/dispatch/dispatch_cfg.h"
#include "lib/dispatch/dispatch_st.h"
#include "lib/dispatch/msgtypes.h"

#include "lib/log/escape.h"
#include "lib/malloc/malloc.h"
#include "lib/string/printf.h"

#include <stdio.h>
#include <string.h>

static dispatch_t *dispatcher_in_use=NULL;

/* Construct an empty dispatch_t. */
static void
test_dispatch_empty(void *arg)
{
  (void)arg;

  dispatch_t *d=NULL;
  dispatch_cfg_t *cfg=NULL;

  cfg = dcfg_new();
  d = dispatch_new(cfg);
  tt_assert(d);

 done:
  dispatch_free(d);
  dcfg_free(cfg);
}

static int total_recv1_simple = 0;
static int total_recv2_simple = 0;

static void
simple_recv1(const msg_t *m)
{
  total_recv1_simple += m->aux_data__.u64;
}

static char *recv2_received = NULL;

static void
simple_recv2(const msg_t *m)
{
  tor_free(recv2_received);
  recv2_received = dispatch_fmt_msg_data(dispatcher_in_use, m);

  total_recv2_simple += m->aux_data__.u64*10;
}

/* Construct a dispatch_t with two messages, make sure that they both get
 * delivered. */
static void
test_dispatch_simple(void *arg)
{
  (void)arg;

  dispatch_t *d=NULL;
  dispatch_cfg_t *cfg=NULL;
  int r;

  cfg = dcfg_new();
  r = dcfg_msg_set_type(cfg,0,0);
  r += dcfg_msg_set_chan(cfg,0,0);
  r += dcfg_add_recv(cfg,0,1,simple_recv1);
  r += dcfg_msg_set_type(cfg,1,0);
  r += dcfg_msg_set_chan(cfg,1,0);
  r += dcfg_add_recv(cfg,1,1,simple_recv2);
  r += dcfg_add_recv(cfg,1,1,simple_recv2); /* second copy */
  tt_int_op(r, OP_EQ, 0);

  d = dispatch_new(cfg);
  tt_assert(d);
  dispatcher_in_use = d;

  msg_aux_data_t data = {.u64 = 7};
  r = dispatch_send(d, 99, 0, 0, 0, data);
  tt_int_op(r, OP_EQ, 0);
  tt_int_op(total_recv1_simple, OP_EQ, 0);

  r = dispatch_flush(d, 0, INT_MAX);
  tt_int_op(r, OP_EQ, 0);
  tt_int_op(total_recv1_simple, OP_EQ, 7);
  tt_int_op(total_recv2_simple, OP_EQ, 0);

  total_recv1_simple = 0;
  r = dispatch_send(d, 99, 0, 1, 0, data);
  tt_int_op(r, OP_EQ, 0);
  r = dispatch_flush(d, 0, INT_MAX);
  tt_int_op(total_recv1_simple, OP_EQ, 0);
  tt_int_op(total_recv2_simple, OP_EQ, 140);

  tt_str_op(recv2_received, OP_EQ, "<>"); // no format function was set.

 done:
  dispatch_free(d);
  dcfg_free(cfg);
  tor_free(recv2_received);
}

struct coord { int x; int y; };
static void
free_coord(msg_aux_data_t d)
{
  tor_free(d.ptr);
}
static char *
fmt_coord(msg_aux_data_t d)
{
  char *v;
  struct coord *c = d.ptr;
  tor_asprintf(&v, "[%d, %d]", c->x, c->y);
  return v;
}
static dispatch_typefns_t coord_fns = {
  .fmt_fn = fmt_coord,
  .free_fn = free_coord,
};
static void
alert_run_immediate(dispatch_t *d, channel_id_t ch, void *arg)
{
  (void)arg;
  dispatch_flush(d, ch, INT_MAX);
}

static char *received_data=NULL;

static void
recv_typed_data(const msg_t *m)
{
  tor_free(received_data);
  received_data = dispatch_fmt_msg_data(dispatcher_in_use, m);
}

static void
test_dispatch_with_types(void *arg)
{
  (void)arg;

  dispatch_t *d=NULL;
  dispatch_cfg_t *cfg=NULL;
  int r;

  cfg = dcfg_new();
  r = dcfg_msg_set_type(cfg,5,3);
  r += dcfg_msg_set_chan(cfg,5,2);
  r += dcfg_add_recv(cfg,5,0,recv_typed_data);
  r += dcfg_type_set_fns(cfg,3,&coord_fns);
  tt_int_op(r, OP_EQ, 0);

  d = dispatch_new(cfg);
  tt_assert(d);
  dispatcher_in_use = d;

  /* Make this message get run immediately. */
  r = dispatch_set_alert_fn(d, 2, alert_run_immediate, NULL);
  tt_int_op(r, OP_EQ, 0);

  struct coord *xy = tor_malloc(sizeof(*xy));
  xy->x = 13;
  xy->y = 37;
  msg_aux_data_t data = {.ptr = xy};
  r = dispatch_send(d, 99/*sender*/, 2/*channel*/, 5/*msg*/, 3/*type*/, data);
  tt_int_op(r, OP_EQ, 0);
  tt_str_op(received_data, OP_EQ, "[13, 37]");

 done:
  dispatch_free(d);
  dcfg_free(cfg);
  tor_free(received_data);
  dispatcher_in_use = NULL;
}

static void
test_dispatch_bad_type_setup(void *arg)
{
  (void)arg;
  static dispatch_typefns_t fns;
  dispatch_cfg_t *cfg = dcfg_new();

  tt_int_op(0, OP_EQ, dcfg_type_set_fns(cfg, 7, &coord_fns));

  fns = coord_fns;
  fns.fmt_fn = NULL;
  tt_int_op(-1, OP_EQ, dcfg_type_set_fns(cfg, 7, &fns));

  fns = coord_fns;
  fns.free_fn = NULL;
  tt_int_op(-1, OP_EQ, dcfg_type_set_fns(cfg, 7, &fns));

  fns = coord_fns;
  tt_int_op(0, OP_EQ, dcfg_type_set_fns(cfg, 7, &fns));

 done:
  dcfg_free(cfg);
}

#define T(name)                                                 \
  { #name, test_dispatch_ ## name, TT_FORK, NULL, NULL }

struct testcase_t dispatch_tests[] = {
  T(empty),
  T(simple),
  T(with_types),
  T(bad_type_setup),
  END_OF_TESTCASES
};