bacnet-bip-core 0.15.0

Portable no-std BACnet/IP behaviour (Annex J): a sans-IO node engine, BBMD tables and broadcast forwarding
Documentation
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
//! The receive rules of `Link::receive`, in their order, and the dispatch of
//! each function by mode (Annex J.1.2, J.2, J.4).

mod node_support;

use core::net::Ipv4Addr;

use bacnet_bip_codec::{BdtEntries, BdtEntry, BvlcFunction, BvlcResultCode, Bvll, FdtEntries};
use bacnet_bip_core::bbmd::Request;
use bacnet_bip_core::node::{
    Addressed, Delivery, Discard, ForBbmd, ForwardedTrust, Inbound, Link, LocalAddresses, Mode,
    Node, Path, Permissive, Received, Response,
};
use bacnet_bip_core::{Arrival, Instant, LocalSubnet};
use node_support::*;

const NPDU: &[u8] = &[0x01, 0x00, 0x10, 0x08];

fn normal() -> Link {
    link(Mode::Normal)
}

/// A delivery of [`NPDU`] a test expects. [`Delivery`] is
/// `#[non_exhaustive]`, so a test outside the crate can't build one to
/// compare with: a `Delivery` or a `Received` compares with this instead.
#[derive(Debug)]
struct Delivered {
    source: core::net::SocketAddrV4,
    group: bool,
}

impl PartialEq<Delivered> for Delivery<'_> {
    fn eq(&self, expected: &Delivered) -> bool {
        self.npdu == NPDU && self.source == expected.source && self.group == expected.group
    }
}

impl PartialEq<Delivered> for Received<'_> {
    fn eq(&self, expected: &Delivered) -> bool {
        matches!(self, Received::Deliver(delivery) if delivery == expected)
    }
}

fn delivered(source: core::net::SocketAddrV4, group: bool) -> Delivered {
    Delivered { source, group }
}

#[test]
fn a_peer_on_another_port_is_delivered_and_answered_there() {
    let link = normal();
    let peer = addr(192, 168, 1, 20, 50_000);
    let unicast = wire(Bvll::OriginalUnicastNpdu(NPDU));
    assert_eq!(
        receive(&link, &unicast, peer, UNICAST),
        delivered(peer, false)
    );
    let read = wire(Bvll::ReadBroadcastDistributionTable);
    assert_eq!(
        receive(&link, &read, peer, UNICAST),
        Received::Nak {
            to: peer,
            code: BvlcResultCode::READ_BROADCAST_DISTRIBUTION_TABLE_NAK,
            malformed: false
        }
    );
    // The node's own IP at another port is a peer, not its echo.
    let neighbour = addr(192, 168, 1, 10, 47_809);
    assert_eq!(
        receive(&link, &unicast, neighbour, UNICAST),
        delivered(neighbour, false)
    );
}

#[test]
fn an_ephemeral_local_port_is_the_nodes_own() {
    let own = addr(192, 168, 1, 10, 54_321);
    let link = Link::new(own, SUBNET, Mode::Normal).unwrap();
    let unicast = wire(Bvll::OriginalUnicastNpdu(NPDU));
    assert_eq!(
        receive(&link, &unicast, own, UNICAST),
        Received::Discarded(Discard::OwnEcho)
    );
    assert_eq!(
        receive(&link, &unicast, OWN, UNICAST),
        delivered(OWN, false)
    );
    let read = wire(Bvll::ReadForeignDeviceTable);
    assert!(matches!(
        receive(&link, &read, OWN, UNICAST),
        Received::Nak { to, .. } if to == OWN
    ));
}

#[test]
fn header_failures_are_discarded_by_reason() {
    let link = normal();
    let cases: [(&[u8], Discard); 4] = [
        (&[0x81, 0x0A, 0x00], Discard::TooShort),
        (&[0x82, 0x0A, 0x00, 0x05, 0x01], Discard::NotBip),
        // The Length field counts the whole message (Annex J.2).
        (&[0x81, 0x0A, 0x00, 0x06, 0x01], Discard::LengthMismatch),
        (&[0x81, 0x0A, 0x00, 0x04, 0x01], Discard::LengthMismatch),
    ];
    for (payload, reason) in cases {
        assert_eq!(
            receive(&link, payload, PEER, UNICAST),
            Received::Discarded(reason),
            "{payload:02X?}"
        );
    }
}

#[test]
fn forwarded_npdu_is_delivered_from_its_origin() {
    let link = normal();
    let forwarded = wire(Bvll::ForwardedNpdu {
        origin: REMOTE,
        npdu: NPDU,
    });
    for to in [UNICAST, TO_BROADCAST] {
        assert_eq!(
            receive(&link, &forwarded, BBMD, to),
            delivered(REMOTE, true)
        );
    }
}

#[test]
fn original_functions_must_fit_their_destination() {
    let link = normal();
    let unicast = wire(Bvll::OriginalUnicastNpdu(NPDU));
    let broadcast = wire(Bvll::OriginalBroadcastNpdu(NPDU));
    let both = Addressed {
        node: true,
        broadcast: true,
    };
    let mismatch = Received::Discarded(Discard::DestinationMismatch);
    assert_eq!(receive(&link, &unicast, PEER, TO_BROADCAST), mismatch);
    assert_eq!(receive(&link, &broadcast, PEER, UNICAST), mismatch);
    assert_eq!(
        receive(&link, &unicast, PEER, Addressed::default()),
        mismatch
    );
    assert_eq!(
        receive(&link, &unicast, PEER, UNICAST),
        delivered(PEER, false)
    );
    assert_eq!(
        receive(&link, &broadcast, PEER, TO_BROADCAST),
        delivered(PEER, true)
    );
    // Where the broadcast address is the node's own, the function decides.
    assert_eq!(receive(&link, &unicast, PEER, both), delivered(PEER, false));
    assert_eq!(
        receive(&link, &broadcast, PEER, both),
        delivered(PEER, true)
    );
    // Management traffic is point to point.
    assert_eq!(
        receive(&link, &result(SUCCESS), PEER, TO_BROADCAST),
        mismatch
    );
}

#[test]
fn unknown_functions_are_dropped_unanswered() {
    for mode in [Mode::Normal, Mode::Foreign(foreign()), Mode::Bbmd] {
        let link = link(mode);
        for function in [0x0C, 0x0D, 0x80, 0xFF] {
            let frame = raw(function, &[0xAA, 0xBB]);
            assert_eq!(
                receive(&link, &frame, PEER, UNICAST),
                Received::Discarded(Discard::UnknownFunction),
                "{function:#04X}"
            );
        }
    }
}

#[test]
fn group_sources_and_origins_are_dropped() {
    let link = normal();
    let unicast = wire(Bvll::OriginalUnicastNpdu(NPDU));
    for source in [
        addr(255, 255, 255, 255, PORT),
        addr(224, 0, 0, 1, PORT),
        addr(239, 255, 0, 1, 50_000),
        // The broadcast IP, at any port.
        addr(192, 168, 1, 255, PORT),
        addr(192, 168, 1, 255, 50_000),
    ] {
        assert_eq!(
            receive(&link, &unicast, source, UNICAST),
            Received::Discarded(Discard::GroupSource),
            "{source}"
        );
    }
    for origin in [
        BROADCAST,
        addr(192, 168, 1, 255, 1),
        addr(224, 0, 0, 9, PORT),
    ] {
        let forwarded = wire(Bvll::ForwardedNpdu { origin, npdu: NPDU });
        assert_eq!(
            receive(&link, &forwarded, BBMD, UNICAST),
            Received::Discarded(Discard::GroupOrigin),
            "{origin}"
        );
    }
    // A loopback broadcast IP, as loopback tests set up, is a group only at
    // the network's port.
    let loopback = LocalSubnet::new(addr(127, 0, 0, 1, PORT));
    let link = Link::new(addr(127, 0, 0, 1, 47_809), loopback, Mode::Normal).unwrap();
    let peer = addr(127, 0, 0, 1, 47_810);
    assert_eq!(
        receive(&link, &unicast, peer, UNICAST),
        delivered(peer, false)
    );
    let forwarded = wire(Bvll::ForwardedNpdu {
        origin: addr(127, 0, 0, 1, PORT),
        npdu: NPDU,
    });
    assert_eq!(
        receive(&link, &forwarded, peer, UNICAST),
        Received::Discarded(Discard::GroupOrigin)
    );
}

#[test]
fn own_echoes_are_dropped() {
    let echo = Received::Discarded(Discard::OwnEcho);
    let own_origin = wire(Bvll::ForwardedNpdu {
        origin: OWN,
        npdu: NPDU,
    });
    for mode in [Mode::Normal, Mode::Foreign(foreign()), Mode::Bbmd] {
        let link = link(mode);
        let unicast = wire(Bvll::OriginalUnicastNpdu(NPDU));
        assert_eq!(receive(&link, &unicast, OWN, UNICAST), echo);
        let broadcast = wire(Bvll::OriginalBroadcastNpdu(NPDU));
        assert_eq!(receive(&link, &broadcast, OWN, TO_BROADCAST), echo);
        assert_eq!(receive(&link, &own_origin, BBMD, UNICAST), echo);
        let distribute = wire(Bvll::DistributeBroadcastToNetwork(NPDU));
        assert_eq!(receive(&link, &distribute, OWN, UNICAST), echo);
    }
    // A BBMD's own rebroadcast keeps its remote origin.
    let rebroadcast = wire(Bvll::ForwardedNpdu {
        origin: REMOTE,
        npdu: NPDU,
    });
    assert_eq!(
        receive(&link(Mode::Bbmd), &rebroadcast, OWN, TO_BROADCAST),
        echo
    );
    // A node that is no BBMD delivers that one: only its origin matters.
    assert_eq!(
        receive(&normal(), &rebroadcast, OWN, TO_BROADCAST),
        delivered(REMOTE, true)
    );
}

fn on_listener(link: &Link, payload: &[u8], to: Addressed, other_interface: bool) -> Discard {
    let datagram = with(datagram(payload, PEER, to), |datagram| {
        datagram.path = Path::Listener { other_interface };
    });
    match receive_datagram(link, &datagram) {
        Received::Discarded(reason) => reason,
        Received::Deliver(_) => Discard::Truncated,
        other => panic!("{other:?}"),
    }
}

#[test]
fn listeners_keep_only_this_links_broadcasts() {
    let link = normal();
    let broadcast = wire(Bvll::OriginalBroadcastNpdu(NPDU));
    let unicast = wire(Bvll::OriginalUnicastNpdu(NPDU));
    let mismatch = |other_interface| Discard::ListenerMismatch { other_interface };
    assert_eq!(
        on_listener(&link, &unicast, UNICAST, false),
        mismatch(false)
    );
    // Whatever it holds: a listener reads nothing it doesn't keep.
    assert_eq!(on_listener(&link, &[0x00], UNICAST, false), mismatch(false));
    assert_eq!(
        on_listener(&link, &broadcast, TO_BROADCAST, true),
        mismatch(true)
    );
    let both = Addressed {
        node: true,
        broadcast: true,
    };
    assert_eq!(on_listener(&link, &unicast, both, false), mismatch(false));
    let kept = with(datagram(&broadcast, PEER, TO_BROADCAST), |datagram| {
        datagram.path = Path::Listener {
            other_interface: false,
        };
    });
    assert_eq!(receive_datagram(&link, &kept), delivered(PEER, true));
}

#[test]
fn wildcard_binds_take_unicast_only_at_own_addresses() {
    let own_ips = [Ipv4Addr::new(10, 0, 0, 5), Ipv4Addr::new(192, 168, 1, 10)];
    let bound = LocalAddresses::new(
        Ipv4Addr::new(192, 168, 1, 10),
        Ipv4Addr::new(192, 168, 1, 255),
    );
    let wildcard = with(bound, |local| local.wildcard = Some(&own_ips));
    let classify = |ip: [u8; 4], os, local: &LocalAddresses<'_>| {
        let addressed = Addressed::classify(Ipv4Addr::from(ip), os, local);
        (addressed.node, addressed.broadcast)
    };
    assert_eq!(classify([10, 0, 0, 5], None, &wildcard), (true, false));
    assert_eq!(classify([10, 0, 0, 6], None, &wildcard), (false, false));
    assert_eq!(classify([192, 168, 1, 255], None, &wildcard), (false, true));
    assert_eq!(
        classify([255, 255, 255, 255], None, &wildcard),
        (false, true)
    );
    assert_eq!(classify([224, 0, 0, 1], None, &wildcard), (false, false));
    assert_eq!(
        classify([10, 0, 0, 5], Some(true), &wildcard),
        (false, false)
    );
    assert_eq!(
        classify([10, 0, 0, 5], Some(false), &wildcard),
        (true, false)
    );
    assert_eq!(
        classify([192, 168, 1, 255], Some(false), &wildcard),
        (false, false)
    );
    assert_eq!(classify([192, 168, 1, 10], None, &bound), (true, false));
    assert_eq!(classify([10, 0, 0, 5], None, &bound), (false, false));
    // A wildcard bind never counts the broadcast address as its own, even
    // when the host lists it.
    let odd = [Ipv4Addr::new(192, 168, 1, 255)];
    let listed = with(wildcard, |local| local.wildcard = Some(&odd));
    assert_eq!(classify([192, 168, 1, 255], None, &listed), (false, true));
    // A bound socket whose broadcast address is its own, as in loopback
    // tests, has both.
    let loopback = LocalAddresses::new(Ipv4Addr::LOCALHOST, Ipv4Addr::LOCALHOST);
    assert_eq!(classify([127, 0, 0, 1], None, &loopback), (true, true));
}

#[test]
fn the_policy_sees_covered_requests_before_their_payload() {
    let link = normal();
    let mut refusing = Recording::new(false, true);
    // Each with one stray octet: refused before the payload is read, so
    // rate limited, not malformed.
    for function in [0x02, 0x05, 0x06, 0x08] {
        let frame = raw(function, &[0x01]);
        let datagram = datagram(&frame, PEER, UNICAST);
        assert_eq!(
            link.receive(&datagram, Instant::ZERO, &mut refusing),
            Received::Discarded(Discard::RateLimited)
        );
    }
    // Write-BDT, Distribute-Broadcast-To-Network and the data functions are
    // never asked about.
    let write = wire(Bvll::WriteBroadcastDistributionTable(
        BdtEntries::from_entries(&[]),
    ));
    let distribute = wire(Bvll::DistributeBroadcastToNetwork(NPDU));
    let unicast = wire(Bvll::OriginalUnicastNpdu(NPDU));
    for frame in [&write, &distribute, &unicast] {
        let datagram = datagram(frame, PEER, UNICAST);
        assert!(!matches!(
            link.receive(&datagram, Instant::ZERO, &mut refusing),
            Received::Discarded(_)
        ));
    }
    assert_eq!(
        refusing.asked,
        [
            BvlcFunction::READ_BROADCAST_DISTRIBUTION_TABLE,
            BvlcFunction::REGISTER_FOREIGN_DEVICE,
            BvlcFunction::READ_FOREIGN_DEVICE_TABLE,
            BvlcFunction::DELETE_FOREIGN_DEVICE_TABLE_ENTRY,
        ]
    );
    // A table ACK the policy refuses is a response it didn't accept.
    let rows = [BdtEntry::new(PEER, Ipv4Addr::BROADCAST)];
    let ack = wire(Bvll::ReadBroadcastDistributionTableAck(
        BdtEntries::from_entries(&rows),
    ));
    let mut picky = Recording::new(true, false);
    let datagram = datagram(&ack, PEER, UNICAST);
    assert!(matches!(
        link.receive(&datagram, Instant::ZERO, &mut picky),
        Received::Response(Response {
            accepted: false,
            message: Ok(_),
            ..
        })
    ));
    let fdt_ack = wire(Bvll::ReadForeignDeviceTableAck(FdtEntries::from_entries(
        &[],
    )));
    assert!(matches!(
        receive(&link, &fdt_ack, PEER, UNICAST),
        Received::Response(Response { accepted: true, .. })
    ));
}

#[test]
fn responses_go_on_to_be_matched_malformed_or_not() {
    let link = normal();
    let success = result(SUCCESS);
    let received = receive(&link, &success, BBMD, UNICAST);
    let Received::Response(response) = received else {
        panic!("{received:?}");
    };
    assert_eq!(
        (
            response.peer,
            response.function,
            response.message,
            response.accepted
        ),
        (
            BBMD,
            BvlcFunction::BVLC_RESULT,
            Ok(Bvll::BvlcResult(SUCCESS)),
            true
        )
    );
    for frame in [
        raw(0x00, &[0x00]),
        raw(0x03, &[0x00; 7]),
        raw(0x07, &[0x00; 11]),
    ] {
        assert!(matches!(
            receive(&link, &frame, BBMD, UNICAST),
            Received::Response(Response {
                message: Err(_),
                accepted: false,
                ..
            })
        ));
    }
    // A malformed data function is dropped.
    assert_eq!(
        receive(&link, &raw(0x04, &[1, 2, 3]), BBMD, UNICAST),
        Received::Discarded(Discard::MalformedPayload)
    );
}

#[test]
fn truncation_is_checked_first() {
    let link = normal();
    let source = addr(255, 255, 255, 255, PORT);
    let truncated = with(datagram(&[0x00], source, Addressed::default()), |d| {
        d.truncated = true;
        d.path = Path::Listener {
            other_interface: true,
        };
    });
    assert_eq!(
        receive_datagram(&link, &truncated),
        Received::Discarded(Discard::Truncated)
    );
    // Then the group source, before the listener.
    let grouped = with(truncated, |d| d.truncated = false);
    assert_eq!(
        receive_datagram(&link, &grouped),
        Received::Discarded(Discard::GroupSource)
    );
}

/// A datagram is dropped, never delivered on a guess, when its destination
/// doesn't fit its function, when the host couldn't say what its
/// destination was (#1678), when it was cut short, and when its Length
/// field is wrong.
///
/// Ported from ACE IoT's bacnet-router-adapters
/// (ACE-IoT-Solutions/rusty-bacnet#5), thanks @acedrew, where it is
/// `ingress_fails_closed_for_destination_mismatch_missing_metadata_and_truncation`.
#[test]
fn ingress_fails_closed_for_destination_mismatch_missing_metadata_and_truncation() {
    let unicast = wire(Bvll::OriginalUnicastNpdu(NPDU));
    let dropped = |reason| Received::Discarded(reason);
    assert_eq!(
        receive(&normal(), &unicast, PEER, TO_BROADCAST),
        dropped(Discard::DestinationMismatch)
    );

    // No destination: dropped in every mode, whatever the function, the
    // socket or the sender, and before the frame is read.
    let broadcast = wire(Bvll::OriginalBroadcastNpdu(NPDU));
    let forwarded = wire(Bvll::ForwardedNpdu {
        origin: REMOTE,
        npdu: NPDU,
    });
    let register = wire(Bvll::RegisterForeignDevice { ttl: 60 });
    let modes = [
        Mode::Normal,
        Mode::Bbmd,
        Mode::Foreign(with(foreign(), |f| f.trust = ForwardedTrust::AnySender)),
    ];
    let paths = [
        Path::Primary,
        Path::Listener {
            other_interface: false,
        },
    ];
    for mode in modes {
        let link = link(mode);
        for payload in [&unicast, &broadcast, &forwarded, &register, &vec![0x81]] {
            for (path, source) in paths.into_iter().zip([PEER, BBMD]) {
                let unknown = with(datagram(payload, source, UNICAST), |d| {
                    d.to = None;
                    d.path = path;
                });
                assert_eq!(
                    receive_datagram(&link, &unknown),
                    dropped(Discard::UnknownDestination),
                    "{mode:?} {path:?} {payload:02x?}"
                );
            }
        }
    }
    // The node engine delivers nothing from one, and has nothing to do.
    let mut node: Node = Node::new(normal(), Instant::ZERO).unwrap();
    let unknown = with(datagram(&unicast, PEER, UNICAST), |d| d.to = None);
    assert_eq!(
        node.receive(&unknown, Instant::ZERO, &mut Permissive),
        Inbound::Discarded(Discard::UnknownDestination)
    );
    assert!(drain(&mut node).is_empty());

    // A cut-short datagram is dropped as that, even with no destination;
    // a group source comes next.
    let truncated = with(unknown, |d| d.truncated = true);
    assert_eq!(
        receive_datagram(&normal(), &truncated),
        dropped(Discard::Truncated)
    );
    let grouped = with(unknown, |d| d.source = BROADCAST);
    assert_eq!(
        receive_datagram(&normal(), &grouped),
        dropped(Discard::GroupSource)
    );

    // A Length field one past the datagram's length.
    let mut long = unicast.clone();
    long[3] += 1;
    assert_eq!(
        receive(&normal(), &long, PEER, UNICAST),
        dropped(Discard::LengthMismatch)
    );
}

#[test]
fn foreign_trust_takes_forwards_only_from_the_bbmd_by_unicast() {
    let forwarded = wire(Bvll::ForwardedNpdu {
        origin: REMOTE,
        npdu: NPDU,
    });
    let untrusted = Received::Discarded(Discard::ForwardedUntrusted);
    let strict = link(Mode::Foreign(foreign()));
    assert_eq!(
        receive(&strict, &forwarded, BBMD, UNICAST),
        delivered(REMOTE, true)
    );
    assert_eq!(receive(&strict, &forwarded, BBMD, TO_BROADCAST), untrusted);
    assert_eq!(receive(&strict, &forwarded, PEER, UNICAST), untrusted);
    // The BBMD's IP at another port is another sender.
    let other_port = addr(10, 0, 0, 1, 47_809);
    assert_eq!(receive(&strict, &forwarded, other_port, UNICAST), untrusted);
    let mut open = foreign();
    open.trust = ForwardedTrust::AnySender;
    let open = link(Mode::Foreign(open));
    assert_eq!(
        receive(&open, &forwarded, PEER, TO_BROADCAST),
        delivered(REMOTE, true)
    );
    assert_eq!(
        receive(&normal(), &forwarded, PEER, UNICAST),
        delivered(REMOTE, true)
    );
}

#[test]
fn foreign_trust_counts_a_forward_to_its_own_broadcast_address_as_unicast() {
    // A foreign device whose broadcast address is its own, as in a loopback
    // test, sees every datagram flagged as both (#1851).
    let forwarded = wire(Bvll::ForwardedNpdu {
        origin: REMOTE,
        npdu: NPDU,
    });
    let untrusted = Received::Discarded(Discard::ForwardedUntrusted);
    let both = Addressed {
        node: true,
        broadcast: true,
    };
    let strict = link(Mode::Foreign(foreign()));
    assert_eq!(
        receive(&strict, &forwarded, BBMD, both),
        delivered(REMOTE, true)
    );
    // Anyone else's is still dropped, and so is a broadcast from the BBMD.
    assert_eq!(receive(&strict, &forwarded, PEER, both), untrusted);
    assert_eq!(receive(&strict, &forwarded, BBMD, TO_BROADCAST), untrusted);
    // An OS-reported group delivery is never the node's unicast.
    let local = LocalAddresses::new(*OWN.ip(), *OWN.ip());
    let grouped = Addressed::classify(*OWN.ip(), Some(true), &local);
    assert_eq!(receive(&strict, &forwarded, BBMD, grouped), untrusted);
    // A broadcast listener keeps no unicast; the primary socket reads it.
    let on_listener = with(datagram(&forwarded, BBMD, both), |datagram| {
        datagram.path = Path::Listener {
            other_interface: false,
        };
    });
    assert_eq!(
        receive_datagram(&strict, &on_listener),
        Received::Discarded(Discard::ListenerMismatch {
            other_interface: false
        })
    );
    // Outside foreign mode both flags still make it a broadcast.
    let received = receive(&link(Mode::Bbmd), &forwarded, BBMD, both);
    let Received::Bbmd(ForBbmd::Forwarded { arrival, .. }) = received else {
        panic!("{received:?}");
    };
    assert_eq!(arrival, Arrival::Broadcast);
}

#[test]
fn bbmd_mode_hands_over_its_share() {
    let link = link(Mode::Bbmd);
    let broadcast = wire(Bvll::OriginalBroadcastNpdu(NPDU));
    let received = receive(&link, &broadcast, PEER, TO_BROADCAST);
    let Received::Bbmd(ForBbmd::OriginalBroadcast(delivery)) = received else {
        panic!("{received:?}");
    };
    assert_eq!(delivery, delivered(PEER, true));
    let forwarded = wire(Bvll::ForwardedNpdu {
        origin: REMOTE,
        npdu: NPDU,
    });
    for (to, arrival) in [
        (UNICAST, Arrival::Unicast),
        (TO_BROADCAST, Arrival::Broadcast),
    ] {
        assert_eq!(
            receive(&link, &forwarded, BBMD, to),
            Received::Bbmd(ForBbmd::Forwarded {
                sender: BBMD,
                arrival,
                origin: REMOTE,
                npdu: NPDU
            })
        );
    }
    let shares = [
        (
            wire(Bvll::DistributeBroadcastToNetwork(NPDU)),
            ForBbmd::Distribute {
                sender: PEER,
                npdu: NPDU,
            },
        ),
        (
            wire(Bvll::RegisterForeignDevice { ttl: 90 }),
            ForBbmd::Management(Request::Register {
                sender: PEER,
                ttl: 90,
            }),
        ),
        (
            wire(Bvll::ReadBroadcastDistributionTable),
            ForBbmd::Management(Request::ReadBdt { sender: PEER }),
        ),
        (
            wire(Bvll::ReadForeignDeviceTable),
            ForBbmd::Management(Request::ReadFdt { sender: PEER }),
        ),
        (
            wire(Bvll::DeleteForeignDeviceTableEntry(REMOTE)),
            ForBbmd::Management(Request::DeleteFdtEntry {
                sender: PEER,
                entry: REMOTE,
            }),
        ),
    ];
    for (payload, share) in shares {
        assert_eq!(
            receive(&link, &payload, PEER, UNICAST),
            Received::Bbmd(share)
        );
    }
    // Unicast data stays the node's.
    let unicast = wire(Bvll::OriginalUnicastNpdu(NPDU));
    assert_eq!(
        receive(&link, &unicast, PEER, UNICAST),
        delivered(PEER, false)
    );
}