bacnet-server 0.15.0

BACnet server: APDU dispatch and service handlers
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
//! Time synchronization handler controls.
//!
//! The async tests run on tokio's paused clock, which the time-sync limiter
//! reads ([`runtime_clock::now`], #1550). Time moves only when a test
//! advances it, so its windows close at exact instants and a stalled runner
//! can't close one under a test.
use super::*;
use crate::server::test_transport::{SendMode, StartMode, TestTransport};
use bacnet_network::layer::ReceivedApdu;
use bacnet_transport::port::ReceivedNpdu;
use bacnet_transport::port::TransportProvenance;
use bacnet_types::primitives::{Date, Time};
use std::sync::atomic::AtomicUsize;

/// Any send panics: time sync must be silent on the wire. Without an inbound
/// link, startup panics too.
fn silent(incoming: Option<mpsc::Receiver<ReceivedNpdu>>) -> TestTransport {
    let builder = TestTransport::builder()
        .local_mac(&[99])
        .unicast(SendMode::Panic("time sync must be silent on wire"))
        .broadcast(SendMode::Panic("time sync must be silent on wire"));
    match incoming {
        Some(incoming) => builder.inbound(incoming),
        None => builder.start(StartMode::Panic("silent transport has no inbound link")),
    }
    .build()
}

fn date() -> Date {
    Date {
        year: 124,
        month: 7,
        day: 4,
        day_of_week: 4,
    }
}
fn time(hour: u8) -> Time {
    Time {
        hour,
        minute: 0,
        second: 0,
        hundredths: 0,
    }
}
fn encoded(hour: u8) -> Bytes {
    let mut bytes = BytesMut::new();
    TimeSynchronizationRequest {
        date: date(),
        time: time(hour),
    }
    .encode(&mut bytes);
    bytes.freeze()
}
fn received(kind: u8) -> ReceivedApdu {
    ReceivedApdu {
        router_response: None,
        direct_response: None,
        apdu: Bytes::new(),
        source_mac: MacAddr::from_slice(if kind == 2 { &[2, 3, 4, 5, 6, 7] } else { &[1] }),
        ingress_network: None,
        source_network: (kind == 1).then(|| NpduAddress {
            network: 7,
            mac_address: MacAddr::from_slice(&[8]),
        }),
        link_layer_group: false,
        is_group: false,
        global_broadcast: false,
        data_attributes: Vec::new(),
        provenance: TransportProvenance::unverified(),
        reply_tx: None,
    }
}
fn clock() -> Arc<ServerClock> {
    let clock = Arc::new(ServerClock::new(ClockConfig::new(300, true).unwrap()));
    clock.synchronize(date(), time(9), false).unwrap();
    clock
}
fn config(policy: TimeSyncPolicy, callbacks: &Arc<AtomicUsize>) -> ServerConfig {
    let callbacks = callbacks.clone();
    ServerConfig {
        time_sync_policy: policy,
        on_time_sync: Some(Arc::new(move |_| {
            callbacks.fetch_add(1, Ordering::SeqCst);
        })),
        ..Default::default()
    }
}
async fn dispatch(
    config: &ServerConfig,
    clock: &Arc<ServerClock>,
    limiter: &Arc<TimeSyncLimiter>,
    is_utc: bool,
    received: &ReceivedApdu,
    data: Bytes,
) {
    BACnetServer::<TestTransport>::handle_unconfirmed_request(
        &UnconfirmedServices {
            time_sync_limiter: Arc::clone(limiter),
            clock: Some(Arc::clone(clock)),
            ..UnconfirmedServices::for_test(
                Arc::new(NetworkLayer::new(silent(None))),
                config.clone(),
            )
        },
        UnconfirmedRequestPdu {
            service_choice: if is_utc {
                UnconfirmedServiceChoice::UTC_TIME_SYNCHRONIZATION
            } else {
                UnconfirmedServiceChoice::TIME_SYNCHRONIZATION
            },
            service_request: data,
        },
        received,
    )
    .await;
}

#[tokio::test(start_paused = true)]
async fn time_sync_disabled_empty_allowlist_and_step_denials_leave_clock_and_observer_untouched() {
    for policy in [
        TimeSyncPolicy {
            enabled: false,
            ..Default::default()
        },
        TimeSyncPolicy {
            source_restriction: Some(TimeSyncSourceRestriction::new(vec![]).unwrap()),
            ..Default::default()
        },
        TimeSyncPolicy {
            max_step: Some(Duration::from_secs(60)),
            ..Default::default()
        },
    ] {
        for kind in 0..3 {
            for is_utc in [false, true] {
                let clock = clock();
                let calls = Arc::new(AtomicUsize::new(0));
                let config = config(policy.clone(), &calls);
                let limiter = Arc::new(TimeSyncLimiter::new(policy.clone()));
                let context = received(kind);
                // Both forward and backward corrections exceed the cap.
                for local_hour in [8, 10] {
                    dispatch(
                        &config,
                        &clock,
                        &limiter,
                        is_utc,
                        &context,
                        encoded(local_hour + if is_utc { 4 } else { 0 }),
                    )
                    .await;
                    let frame = clock.read_clock().unwrap();
                    assert_eq!(frame.local_date, date());
                    assert_eq!((frame.local_time.hour, frame.local_time.minute), (9, 0));
                    assert_eq!(calls.load(Ordering::SeqCst), 0);
                }
            }
        }
    }
}

#[tokio::test(start_paused = true)]
async fn time_sync_allowlist_accepts_exact_direct_routed_and_sc_vmac_with_callback_provenance() {
    for kind in 0..3 {
        for is_utc in [false, true] {
            let clock = clock();
            let context = received(kind);
            let source = match &context.source_network {
                Some(source) => TimeSyncSource::Routed {
                    network: source.network,
                    address: source.mac_address.to_vec(),
                },
                None => TimeSyncSource::Direct(context.source_mac.to_vec()),
            };
            let policy = TimeSyncPolicy {
                source_restriction: Some(TimeSyncSourceRestriction::new(vec![source]).unwrap()),
                ..Default::default()
            };
            let expected_mac = context.source_mac.clone();
            let expected_route = context.source_network.clone();
            let data = encoded(if is_utc { 14 } else { 10 });
            let expected_data = data.clone();
            let seen = Arc::new(AtomicUsize::new(0));
            let seen_callback = seen.clone();
            let observed_clock = clock.clone();
            let config = ServerConfig {
                time_sync_policy: policy.clone(),
                on_time_sync: Some(Arc::new(move |event| {
                    assert_eq!(event.source_mac, expected_mac);
                    assert_eq!(event.source_network, expected_route);
                    assert_eq!(event.raw_service_data, expected_data);
                    assert_eq!(event.is_utc, is_utc);
                    assert_eq!(observed_clock.read_clock().unwrap().local_time.hour, 10);
                    seen_callback.fetch_add(1, Ordering::SeqCst);
                })),
                ..Default::default()
            };
            let limiter = Arc::new(TimeSyncLimiter::new(policy));
            let mut other = received(kind);
            if let Some(source) = &mut other.source_network {
                source.network += 1;
            } else {
                other.source_mac = MacAddr::from_slice(&[42]);
            }
            dispatch(&config, &clock, &limiter, is_utc, &other, data.clone()).await;
            assert_eq!(seen.load(Ordering::SeqCst), 0);
            assert_eq!(clock.read_clock().unwrap().local_time.hour, 9);
            dispatch(&config, &clock, &limiter, is_utc, &context, data).await;
            assert_eq!(seen.load(Ordering::SeqCst), 1);
        }
    }
}

#[tokio::test(start_paused = true)]
async fn time_sync_malformed_denied_sources_and_oversteps_do_not_starve_valid_requests() {
    let policy = TimeSyncPolicy {
        source_restriction: Some(
            TimeSyncSourceRestriction::new(vec![TimeSyncSource::Direct(vec![1])]).unwrap(),
        ),
        max_step: Some(Duration::from_secs(3600)),
        per_source_rate: Some(TimeSyncRateLimit {
            max_per_second: 1.0,
            burst_capacity: 1,
        }),
        global_rate: Some(TimeSyncRateLimit {
            max_per_second: 1.0,
            burst_capacity: 1,
        }),
        coalesce_window: Duration::from_secs(60),
        ..Default::default()
    };
    let clock = clock();
    let calls = Arc::new(AtomicUsize::new(0));
    let config = config(policy.clone(), &calls);
    let limiter = Arc::new(TimeSyncLimiter::new(policy));
    for is_utc in [false, true] {
        dispatch(
            &config,
            &clock,
            &limiter,
            is_utc,
            &received(0),
            Bytes::from_static(&[0xff]),
        )
        .await;
        let mut invalid_date = encoded(10).to_vec();
        invalid_date[1] = Date::UNSPECIFIED;
        dispatch(
            &config,
            &clock,
            &limiter,
            is_utc,
            &received(0),
            invalid_date.into(),
        )
        .await;
        dispatch(&config, &clock, &limiter, is_utc, &received(2), encoded(10)).await;
        dispatch(&config, &clock, &limiter, is_utc, &received(0), encoded(20)).await;
    }
    assert_eq!(calls.load(Ordering::SeqCst), 0);
    dispatch(&config, &clock, &limiter, false, &received(0), encoded(10)).await;
    assert_eq!(calls.load(Ordering::SeqCst), 1);
    // UTC and local share rate/coalescing state. A second valid correction drops.
    dispatch(&config, &clock, &limiter, true, &received(0), encoded(15)).await;
    assert_eq!(calls.load(Ordering::SeqCst), 1);
    assert_eq!(clock.read_clock().unwrap().local_time.hour, 10);
}

#[tokio::test(start_paused = true)]
async fn time_sync_panicking_observer_keeps_change_and_live_ingress_continues() {
    let (tx, rx) = mpsc::channel(8);
    let (observed, mut observations) = mpsc::unbounded_channel();
    let config = ServerConfig {
        on_time_sync: Some(Arc::new(move |event| {
            observed.send(event).unwrap();
            panic!("test observer failure");
        })),
        ..Default::default()
    };
    let mut server = BACnetServer::start(config, ObjectDatabase::new(), silent(Some(rx)))
        .await
        .unwrap();
    for (is_utc, hour) in [(false, 10), (true, 11), (false, 12)] {
        let mut npdu = BytesMut::from(&[1, 0][..]);
        encode_apdu(
            &mut npdu,
            &Apdu::UnconfirmedRequest(UnconfirmedRequestPdu {
                service_choice: if is_utc {
                    UnconfirmedServiceChoice::UTC_TIME_SYNCHRONIZATION
                } else {
                    UnconfirmedServiceChoice::TIME_SYNCHRONIZATION
                },
                service_request: encoded(hour),
            }),
        )
        .unwrap();
        tx.send(ReceivedNpdu {
            direct_response: None,
            npdu: npdu.freeze(),
            source_mac: MacAddr::from_slice(&[2, 3, 4, 5, 6, 7]),
            link_layer_group: false,
            data_attributes: Vec::new(),
            provenance: TransportProvenance::unverified(),
            reply_tx: None,
        })
        .await
        .unwrap();
        let event = tokio::time::timeout(Duration::from_secs(2), observations.recv())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(event.is_utc, is_utc);
        assert_eq!(
            server
                ._clock
                .as_ref()
                .unwrap()
                .read_clock()
                .unwrap()
                .local_time
                .hour,
            hour
        );
    }
    server.stop().await.unwrap();
}

#[test]
fn time_sync_callback_unwind_is_contained_without_poisoning_limiter() {
    let clock = clock();
    let config = ServerConfig {
        on_time_sync: Some(Arc::new(|_| panic!("test observer failure"))),
        ..Default::default()
    };
    let limiter = TimeSyncLimiter::new(config.time_sync_policy.clone());
    for (is_utc, local_hour) in [(false, 10), (true, 11)] {
        apply_time_sync_request(
            Some(&clock),
            &config,
            &limiter,
            encoded(local_hour + if is_utc { 4 } else { 0 }),
            is_utc,
            &received(2),
            None,
        )
        .unwrap();
        assert_eq!(clock.read_clock().unwrap().local_time.hour, local_hour);
    }
}

/// Time synchronization at 10:00 from link MAC `mac`, relayed with `source`
/// as SNET/SADR when one is given, to a server that knows `number` as its
/// network's and lists only `entry`. Whether the clock took it.
async fn synced_from(
    entry: TimeSyncSource,
    number: Option<u16>,
    mac: &[u8],
    source: Option<NpduAddress>,
) -> bool {
    use bacnet_types::network_number::NetworkNumber;
    let policy = TimeSyncPolicy {
        source_restriction: Some(TimeSyncSourceRestriction::new(vec![entry]).unwrap()),
        ..Default::default()
    };
    let network = Arc::new(NetworkLayer::new(silent(None)));
    if let Some(number) = number {
        network
            .local_network_number()
            .publish(NetworkNumber::configured(number).unwrap());
    }
    let clock = clock();
    let mut received = received(0);
    received.source_mac = MacAddr::from_slice(mac);
    received.source_network = source;
    BACnetServer::<TestTransport>::handle_unconfirmed_request(
        &UnconfirmedServices {
            time_sync_limiter: Arc::new(TimeSyncLimiter::new(policy.clone())),
            clock: Some(Arc::clone(&clock)),
            ..UnconfirmedServices::for_test(network, config(policy, &Arc::new(AtomicUsize::new(0))))
        },
        UnconfirmedRequestPdu {
            service_choice: UnconfirmedServiceChoice::TIME_SYNCHRONIZATION,
            service_request: encoded(10),
        },
        &received,
    )
    .await;
    match clock.read_clock().unwrap().local_time.hour {
        10 => true,
        9 => false,
        hour => panic!("the clock moved to {hour}:00"),
    }
}

/// A routed allowlist entry on the server's own network number also names
/// the station's direct requests once that number is known (#1458); only
/// the routed entry widens.
#[tokio::test(start_paused = true)]
async fn time_sync_routed_entry_on_this_network_names_the_stations_direct_request() {
    const THIS_NETWORK: u16 = 7;
    let (station, other_station, router) = ([10, 0, 0, 3], [10, 0, 0, 4], [10, 0, 0, 9]);
    let routed_entry = || TimeSyncSource::Routed {
        network: THIS_NETWORK,
        address: station.to_vec(),
    };
    let relayed = |network| {
        Some(NpduAddress {
            network,
            mac_address: MacAddr::from_slice(&station),
        })
    };
    let known = Some(THIS_NETWORK);
    assert!(synced_from(routed_entry(), known, &station, None).await);
    assert!(synced_from(routed_entry(), known, &router, relayed(THIS_NETWORK)).await);
    // Unknown number, another number, another station: no direct match.
    assert!(!synced_from(routed_entry(), None, &station, None).await);
    assert!(!synced_from(routed_entry(), Some(THIS_NETWORK + 1), &station, None).await);
    assert!(!synced_from(routed_entry(), known, &other_station, None).await);
    // A direct entry still names no routed claim of the same station.
    let direct = || TimeSyncSource::Direct(station.to_vec());
    assert!(synced_from(direct(), known, &station, None).await);
    assert!(!synced_from(direct(), known, &router, relayed(THIS_NETWORK)).await);
}

/// Each window the limiter keeps closes exactly at its length (#1550): a
/// repeat 1 ms inside a coalescing window, or 1 ms before a rate bucket earns
/// its next token, is refused and leaves the clock alone; one at the edge is
/// taken. The request path reads the clock itself, so this needs the limiter
/// on tokio's paused clock.
#[tokio::test(start_paused = true)]
async fn time_sync_windows_close_exactly_at_their_length() {
    let rate = Some(TimeSyncRateLimit {
        max_per_second: 1.0,
        burst_capacity: 1,
    });
    let window = Duration::from_secs(2);
    let token = Duration::from_secs(1);
    // The global rate goes first, while the paused clock still reads the
    // instant the runtime started: a global bucket built on the system clock,
    // which has run on since, would then hold its next token back past the
    // edge.
    let cases = [
        (
            "global rate",
            TimeSyncPolicy {
                global_rate: rate,
                ..Default::default()
            },
            token,
        ),
        (
            "source rate",
            TimeSyncPolicy {
                per_source_rate: rate,
                ..Default::default()
            },
            token,
        ),
        (
            "source coalescing",
            TimeSyncPolicy {
                coalesce_window: window,
                ..Default::default()
            },
            window,
        ),
        (
            "global coalescing",
            TimeSyncPolicy {
                global_coalesce_window: window,
                ..Default::default()
            },
            window,
        ),
    ];
    for (name, policy, edge) in cases {
        let clock = clock();
        let calls = Arc::new(AtomicUsize::new(0));
        let config = config(policy.clone(), &calls);
        let limiter = Arc::new(TimeSyncLimiter::new(policy));
        let synced = || {
            (
                calls.load(Ordering::SeqCst),
                clock.read_clock().unwrap().local_time.hour,
            )
        };
        dispatch(&config, &clock, &limiter, false, &received(0), encoded(10)).await;
        assert_eq!(synced(), (1, 10), "{name}: first request");

        tokio::time::advance(edge - Duration::from_millis(1)).await;
        dispatch(&config, &clock, &limiter, false, &received(0), encoded(11)).await;
        assert_eq!(synced(), (1, 10), "{name}: 1 ms inside");

        tokio::time::advance(Duration::from_millis(1)).await;
        dispatch(&config, &clock, &limiter, false, &received(0), encoded(12)).await;
        assert_eq!(synced(), (2, 12), "{name}: at the edge");
    }
}