aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_pubsub_build.c
blob: 5f9005926cc4bf90ef7e8c2b0cfbda44f6af8b73 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/* Copyright (c) 2018-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

#define DISPATCH_PRIVATE
#define PUBSUB_PRIVATE

#include "test/test.h"

#include "lib/cc/torint.h"
#include "lib/dispatch/dispatch.h"
#include "lib/dispatch/dispatch_naming.h"
#include "lib/dispatch/dispatch_st.h"
#include "lib/dispatch/msgtypes.h"
#include "lib/pubsub/pubsub_macros.h"
#include "lib/pubsub/pubsub_build.h"
#include "lib/pubsub/pubsub_builder_st.h"

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

#include "test/log_test_helpers.h"

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

static char *
ex_int_fmt(msg_aux_data_t aux)
{
  int val = (int) aux.u64;
  char *r=NULL;
  tor_asprintf(&r, "%d", val);
  return r;
}

static char *
ex_str_fmt(msg_aux_data_t aux)
{
  return esc_for_log(aux.ptr);
}

static void
ex_str_free(msg_aux_data_t aux)
{
  tor_free_(aux.ptr);
}

static dispatch_typefns_t intfns = {
  .fmt_fn = ex_int_fmt
};

static dispatch_typefns_t stringfns = {
  .free_fn = ex_str_free,
  .fmt_fn = ex_str_fmt
};

DECLARE_MESSAGE_INT(bunch_of_coconuts, int, int);
DECLARE_PUBLISH(bunch_of_coconuts);
DECLARE_SUBSCRIBE(bunch_of_coconuts, coconut_recipient_cb);

DECLARE_MESSAGE(yes_we_have_no, string, char *);
DECLARE_PUBLISH(yes_we_have_no);
DECLARE_SUBSCRIBE(yes_we_have_no, absent_item_cb);

static void
coconut_recipient_cb(const msg_t *m, int n_coconuts)
{
  (void)m;
  (void)n_coconuts;
}

static void
absent_item_cb(const msg_t *m, const char *fruitname)
{
  (void)m;
  (void)fruitname;
}

#define FLAG_SKIP 99999

static void
seed_dispatch_builder(pubsub_builder_t *b,
                      unsigned fl1, unsigned fl2, unsigned fl3, unsigned fl4)
{
  pubsub_connector_t *c = NULL;

  {
    c = pubsub_connector_for_subsystem(b, get_subsys_id("sys1"));
    DISPATCH_REGISTER_TYPE(c, int, &intfns);
    if (fl1 != FLAG_SKIP)
      DISPATCH_ADD_PUB_(c, main, bunch_of_coconuts, fl1);
    if (fl2 != FLAG_SKIP)
      DISPATCH_ADD_SUB_(c, main, yes_we_have_no, fl2);
    pubsub_connector_free(c);
  }

  {
    c = pubsub_connector_for_subsystem(b, get_subsys_id("sys2"));
    DISPATCH_REGISTER_TYPE(c, string, &stringfns);
    if (fl3 != FLAG_SKIP)
      DISPATCH_ADD_PUB_(c, main, yes_we_have_no, fl3);
    if (fl4 != FLAG_SKIP)
      DISPATCH_ADD_SUB_(c, main, bunch_of_coconuts, fl4);
    pubsub_connector_free(c);
  }
}

static void
seed_pubsub_builder_basic(pubsub_builder_t *b)
{
  seed_dispatch_builder(b, 0, 0, 0, 0);
}

/* Regular builder with valid types and messages.
 */
static void
test_pubsub_build_types_ok(void *arg)
{
  (void)arg;
  pubsub_builder_t *b = NULL;
  dispatch_t *dispatcher = NULL;
  pubsub_connector_t *c = NULL;
  pubsub_items_t *items = NULL;

  b = pubsub_builder_new();
  seed_pubsub_builder_basic(b);

  dispatcher = pubsub_builder_finalize(b, &items);
  b = NULL;
  tt_assert(dispatcher);
  tt_assert(items);
  tt_int_op(smartlist_len(items->items), OP_EQ, 4);

  // Make sure that the bindings got build correctly.
  SMARTLIST_FOREACH_BEGIN(items->items, pubsub_cfg_t *, item) {
    if (item->is_publish) {
      tt_assert(item->pub_binding);
      tt_ptr_op(item->pub_binding->dispatch_ptr, OP_EQ, dispatcher);
    }
  } SMARTLIST_FOREACH_END(item);

  tt_int_op(dispatcher->n_types, OP_GE, 2);
  tt_assert(dispatcher->typefns);

  tt_assert(dispatcher->typefns[get_msg_type_id("int")].fmt_fn == ex_int_fmt);
  tt_assert(dispatcher->typefns[get_msg_type_id("string")].fmt_fn ==
            ex_str_fmt);

  // Now clear the bindings, like we would do before freeing the
  // the dispatcher.
  pubsub_items_clear_bindings(items);
  SMARTLIST_FOREACH_BEGIN(items->items, pubsub_cfg_t *, item) {
    if (item->is_publish) {
      tt_assert(item->pub_binding);
      tt_ptr_op(item->pub_binding->dispatch_ptr, OP_EQ, NULL);
    }
  } SMARTLIST_FOREACH_END(item);

 done:
  pubsub_connector_free(c);
  pubsub_builder_free(b);
  dispatch_free(dispatcher);
  pubsub_items_free(items);
}

/* We fail if the same type is defined in two places with different functions.
 */
static void
test_pubsub_build_types_decls_conflict(void *arg)
{
  (void)arg;
  pubsub_builder_t *b = NULL;
  dispatch_t *dispatcher = NULL;
  pubsub_connector_t *c = NULL;

  b = pubsub_builder_new();
  seed_pubsub_builder_basic(b);
  {
    c = pubsub_connector_for_subsystem(b, get_subsys_id("sys3"));
    // Extra declaration of int: we don't allow this.
    DISPATCH_REGISTER_TYPE(c, int, &stringfns);
    pubsub_connector_free(c);
  }

  setup_full_capture_of_logs(LOG_WARN);
  dispatcher = pubsub_builder_finalize(b, NULL);
  b = NULL;
  tt_assert(dispatcher == NULL);
  // expect_log_msg_containing("(int) declared twice"); // XXXX

 done:
  pubsub_connector_free(c);
  pubsub_builder_free(b);
  dispatch_free(dispatcher);
  teardown_capture_of_logs();
}

/* If a message ID exists but nobody is publishing or subscribing to it,
 * that's okay. */
static void
test_pubsub_build_unused_message(void *arg)
{
  (void)arg;
  pubsub_builder_t *b = NULL;
  dispatch_t *dispatcher = NULL;

  b = pubsub_builder_new();
  seed_pubsub_builder_basic(b);

  // This message isn't actually generated by anyone, but that will be fine:
  // we just log it at info.
  get_message_id("unused");
  setup_capture_of_logs(LOG_INFO);

  dispatcher = pubsub_builder_finalize(b, NULL);
  b = NULL;
  tt_assert(dispatcher);
  expect_log_msg_containing(
     "Nobody is publishing or subscribing to message");

 done:
  pubsub_builder_free(b);
  dispatch_free(dispatcher);
  teardown_capture_of_logs();
}

/* Publishing or subscribing to a message with no subscribers / publishers
 * should fail and warn. */
static void
test_pubsub_build_missing_pubsub(void *arg)
{
  (void)arg;
  pubsub_builder_t *b = NULL;
  dispatch_t *dispatcher = NULL;

  b = pubsub_builder_new();
  seed_dispatch_builder(b, 0, 0, FLAG_SKIP, FLAG_SKIP);

  setup_full_capture_of_logs(LOG_WARN);
  dispatcher = pubsub_builder_finalize(b, NULL);
  b = NULL;
  tt_assert(dispatcher == NULL);

  expect_log_msg_containing(
       "Message \"bunch_of_coconuts\" has publishers, but no subscribers.");
  expect_log_msg_containing(
       "Message \"yes_we_have_no\" has subscribers, but no publishers.");

 done:
  pubsub_builder_free(b);
  dispatch_free(dispatcher);
  teardown_capture_of_logs();
}

/* Make sure that a stub publisher or subscriber prevents an error from
 * happening even if there are no other publishers/subscribers for a message
 */
static void
test_pubsub_build_stub_pubsub(void *arg)
{
  (void)arg;
  pubsub_builder_t *b = NULL;
  dispatch_t *dispatcher = NULL;

  b = pubsub_builder_new();
  seed_dispatch_builder(b, 0, 0, DISP_FLAG_STUB, DISP_FLAG_STUB);

  dispatcher = pubsub_builder_finalize(b, NULL);
  b = NULL;
  tt_assert(dispatcher);

  // 1 subscriber.
  tt_int_op(1, OP_EQ,
            dispatcher->table[get_message_id("yes_we_have_no")]->n_enabled);
  // no subscribers
  tt_ptr_op(NULL, OP_EQ,
            dispatcher->table[get_message_id("bunch_of_coconuts")]);

 done:
  pubsub_builder_free(b);
  dispatch_free(dispatcher);
}

/* Only one channel per msg id. */
static void
test_pubsub_build_channels_conflict(void *arg)
{
  (void)arg;
  pubsub_builder_t *b = NULL;
  dispatch_t *dispatcher = NULL;
  pubsub_connector_t *c = NULL;

  b = pubsub_builder_new();
  seed_pubsub_builder_basic(b);
  pub_binding_t btmp;

  {
    c = pubsub_connector_for_subsystem(b, get_subsys_id("problems"));
    /* Usually the DISPATCH_ADD_PUB macro would keep us from using
     * the wrong channel */
    pubsub_add_pub_(c, &btmp, get_channel_id("hithere"),
                    get_message_id("bunch_of_coconuts"),
                    get_msg_type_id("int"),
                    0 /* flags */,
                    "somewhere.c", 22);
    pubsub_connector_free(c);
  };

  setup_full_capture_of_logs(LOG_WARN);
  dispatcher = pubsub_builder_finalize(b, NULL);
  b = NULL;
  tt_assert(dispatcher == NULL);

  expect_log_msg_containing("Message \"bunch_of_coconuts\" is associated "
                            "with multiple inconsistent channels.");

 done:
  pubsub_builder_free(b);
  dispatch_free(dispatcher);
  teardown_capture_of_logs();
}

/* Only one type per msg id. */
static void
test_pubsub_build_types_conflict(void *arg)
{
  (void)arg;
  pubsub_builder_t *b = NULL;
  dispatch_t *dispatcher = NULL;
  pubsub_connector_t *c = NULL;

  b = pubsub_builder_new();
  seed_pubsub_builder_basic(b);
  pub_binding_t btmp;

  {
    c = pubsub_connector_for_subsystem(b, get_subsys_id("problems"));
    /* Usually the DISPATCH_ADD_PUB macro would keep us from using
     * the wrong channel */
    pubsub_add_pub_(c, &btmp, get_channel_id("hithere"),
                    get_message_id("bunch_of_coconuts"),
                    get_msg_type_id("string"),
                    0 /* flags */,
                    "somewhere.c", 22);
    pubsub_connector_free(c);
  };

  setup_full_capture_of_logs(LOG_WARN);
  dispatcher = pubsub_builder_finalize(b, NULL);
  b = NULL;
  tt_assert(dispatcher == NULL);

  expect_log_msg_containing("Message \"bunch_of_coconuts\" is associated "
                            "with multiple inconsistent message types.");

 done:
  pubsub_builder_free(b);
  dispatch_free(dispatcher);
  teardown_capture_of_logs();
}

/* The same module can't publish and subscribe the same message */
static void
test_pubsub_build_pubsub_same(void *arg)
{
  (void)arg;
  pubsub_builder_t *b = NULL;
  dispatch_t *dispatcher = NULL;
  pubsub_connector_t *c = NULL;

  b = pubsub_builder_new();
  seed_pubsub_builder_basic(b);

  {
    c = pubsub_connector_for_subsystem(b, get_subsys_id("sys1"));
    // already publishing this.
    DISPATCH_ADD_SUB(c, main, bunch_of_coconuts);
    pubsub_connector_free(c);
  };

  setup_full_capture_of_logs(LOG_WARN);
  dispatcher = pubsub_builder_finalize(b, NULL);
  b = NULL;
  tt_assert(dispatcher == NULL);

  expect_log_msg_containing("Message \"bunch_of_coconuts\" is published "
                            "and subscribed by the same subsystem \"sys1\".");

 done:
  pubsub_builder_free(b);
  dispatch_free(dispatcher);
  teardown_capture_of_logs();
}

/* More than one subsystem may publish or subscribe, and that's okay. */
static void
test_pubsub_build_pubsub_multi(void *arg)
{
  (void)arg;
  pubsub_builder_t *b = NULL;
  dispatch_t *dispatcher = NULL;
  pubsub_connector_t *c = NULL;

  b = pubsub_builder_new();
  seed_pubsub_builder_basic(b);
  pub_binding_t btmp;

  {
    c = pubsub_connector_for_subsystem(b, get_subsys_id("sys3"));
    DISPATCH_ADD_SUB(c, main, bunch_of_coconuts);
    pubsub_add_pub_(c, &btmp, get_channel_id("main"),
                    get_message_id("yes_we_have_no"),
                    get_msg_type_id("string"),
                    0 /* flags */,
                    "somewhere.c", 22);
    pubsub_connector_free(c);
  };

  dispatcher = pubsub_builder_finalize(b, NULL);
  b = NULL;
  tt_assert(dispatcher);

  // 1 subscribers
  tt_int_op(1, OP_EQ,
            dispatcher->table[get_message_id("yes_we_have_no")]->n_enabled);
  // 2 subscribers.
  dtbl_entry_t *ent =
    dispatcher->table[get_message_id("bunch_of_coconuts")];
  tt_int_op(2, OP_EQ, ent->n_enabled);
  tt_int_op(2, OP_EQ, ent->n_fns);
  tt_ptr_op(ent->rcv[0].fn, OP_EQ, recv_fn__bunch_of_coconuts);
  tt_ptr_op(ent->rcv[1].fn, OP_EQ, recv_fn__bunch_of_coconuts);

 done:
  pubsub_builder_free(b);
  dispatch_free(dispatcher);
}

static void
some_other_coconut_hook(const msg_t *m)
{
  (void)m;
}

/* Subscribe hooks should be build correctly when there are a bunch of
 * them. */
static void
test_pubsub_build_sub_many(void *arg)
{
  (void)arg;
  pubsub_builder_t *b = NULL;
  dispatch_t *dispatcher = NULL;
  pubsub_connector_t *c = NULL;
  char *sysname = NULL;
  b = pubsub_builder_new();
  seed_pubsub_builder_basic(b);

  int i;
  for (i = 1; i < 100; ++i) {
    tor_asprintf(&sysname, "system%d",i);
    c = pubsub_connector_for_subsystem(b, get_subsys_id(sysname));
    if (i % 7) {
      DISPATCH_ADD_SUB(c, main, bunch_of_coconuts);
    } else {
      pubsub_add_sub_(c, some_other_coconut_hook,
                      get_channel_id("main"),
                      get_message_id("bunch_of_coconuts"),
                      get_msg_type_id("int"),
                      0 /* flags */,
                      "somewhere.c", 22);
    }
    pubsub_connector_free(c);
    tor_free(sysname);
  };

  dispatcher = pubsub_builder_finalize(b, NULL);
  b = NULL;
  tt_assert(dispatcher);

  dtbl_entry_t *ent =
    dispatcher->table[get_message_id("bunch_of_coconuts")];
  tt_int_op(100, OP_EQ, ent->n_enabled);
  tt_int_op(100, OP_EQ, ent->n_fns);
  tt_ptr_op(ent->rcv[0].fn, OP_EQ, recv_fn__bunch_of_coconuts);
  tt_ptr_op(ent->rcv[1].fn, OP_EQ, recv_fn__bunch_of_coconuts);
  tt_ptr_op(ent->rcv[76].fn, OP_EQ, recv_fn__bunch_of_coconuts);
  tt_ptr_op(ent->rcv[77].fn, OP_EQ, some_other_coconut_hook);
  tt_ptr_op(ent->rcv[78].fn, OP_EQ, recv_fn__bunch_of_coconuts);

 done:
  pubsub_builder_free(b);
  dispatch_free(dispatcher);
  tor_free(sysname);
}

/* It's fine to declare the excl flag. */
static void
test_pubsub_build_excl_ok(void *arg)
{
  (void)arg;
  pubsub_builder_t *b = NULL;
  dispatch_t *dispatcher = NULL;

  b = pubsub_builder_new();
  // Try one excl/excl pair and one excl/non pair.
  seed_dispatch_builder(b, DISP_FLAG_EXCL, 0,
                        DISP_FLAG_EXCL, DISP_FLAG_EXCL);

  dispatcher = pubsub_builder_finalize(b, NULL);
  b = NULL;
  tt_assert(dispatcher);

  // 1 subscribers
  tt_int_op(1, OP_EQ,
            dispatcher->table[get_message_id("yes_we_have_no")]->n_enabled);
  // 1 subscriber.
  tt_int_op(1, OP_EQ,
            dispatcher->table[get_message_id("bunch_of_coconuts")]->n_enabled);

 done:
  pubsub_builder_free(b);
  dispatch_free(dispatcher);
}

/* but if you declare the excl flag, you need to mean it. */
static void
test_pubsub_build_excl_bad(void *arg)
{
  (void)arg;
  pubsub_builder_t *b = NULL;
  dispatch_t *dispatcher = NULL;
  pubsub_connector_t *c = NULL;

  b = pubsub_builder_new();
  seed_dispatch_builder(b, DISP_FLAG_EXCL, DISP_FLAG_EXCL,
                        0, 0);

  {
    c = pubsub_connector_for_subsystem(b, get_subsys_id("sys3"));
    DISPATCH_ADD_PUB_(c, main, bunch_of_coconuts, 0);
    DISPATCH_ADD_SUB_(c, main, yes_we_have_no, 0);
    pubsub_connector_free(c);
  };

  setup_full_capture_of_logs(LOG_WARN);
  dispatcher = pubsub_builder_finalize(b, NULL);
  b = NULL;
  tt_assert(dispatcher == NULL);

  expect_log_msg_containing("has multiple publishers, but at least one is "
                            "marked as exclusive.");
  expect_log_msg_containing("has multiple subscribers, but at least one is "
                            "marked as exclusive.");

 done:
  pubsub_builder_free(b);
  dispatch_free(dispatcher);
  teardown_capture_of_logs();
}

#define T(name, flags)                                          \
  { #name, test_pubsub_build_ ## name , (flags), NULL, NULL }

struct testcase_t pubsub_build_tests[] = {
  T(types_ok, TT_FORK),
  T(types_decls_conflict, TT_FORK),
  T(unused_message, TT_FORK),
  T(missing_pubsub, TT_FORK),
  T(stub_pubsub, TT_FORK),
  T(channels_conflict, TT_FORK),
  T(types_conflict, TT_FORK),
  T(pubsub_same, TT_FORK),
  T(pubsub_multi, TT_FORK),
  T(sub_many, TT_FORK),
  T(excl_ok, TT_FORK),
  T(excl_bad, TT_FORK),
  END_OF_TESTCASES
};