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
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
use super::mutation_tests::MutationAuthorizer;
use super::*;
use crate::mutation::{MutationDecisionCounters, MutationServiceCounters};
use crate::server::requests::mutation_tests::{
    Fixture, SOURCE, apdu, assert_denied, cases, oid, route, value, wpm,
};
use crate::server::test_transport::TestTransport;
use bacnet_network::layer::ReceivedApdu;
use bacnet_objects::device::{DeviceConfig, DeviceObject};
use bacnet_services::device_mgmt::DeviceCommunicationControlRequest;
use bacnet_services::read_property::ReadPropertyRequest;
use bacnet_services::wpm::{WriteAccessSpecification, WritePropertyMultipleError};
use bacnet_transport::port::TransportProvenance;
use bacnet_types::enums::EnableDisable;
use std::sync::atomic::AtomicUsize;

#[path = "mutation_bip_principal_tests.rs"]
mod bip_principal_tests;
#[path = "mutation_runtime_tests.rs"]
mod runtime_tests;

async fn server(
    policy: MutationPolicy,
    authorizer: Option<MutationAuthorizer>,
) -> BACnetServer<TestTransport> {
    let fixture = Fixture::new(authorizer);
    let db = std::mem::take(&mut *fixture.db.write().await);
    let config = ServerConfig {
        mutation_policy: policy,
        ..fixture.config
    };
    BACnetServer::start(config, db, TestTransport::new())
        .await
        .unwrap()
}

fn request(service: ConfirmedServiceChoice, bytes: Bytes, id: u8) -> ConfirmedRequestPdu {
    ConfirmedRequestPdu {
        segmented: false,
        more_follows: false,
        segmented_response_accepted: false,
        max_segments: None,
        max_apdu_length: 1476,
        invoke_id: id,
        sequence_number: None,
        proposed_window_size: None,
        service_choice: service,
        service_request: bytes,
    }
}

async fn dispatch(
    server: &BACnetServer<TestTransport>,
    service: ConfirmedServiceChoice,
    bytes: Bytes,
    id: u8,
) -> Option<Bytes> {
    let (tx, rx) = oneshot::channel();
    BACnetServer::dispatch(
        &server.test_dispatch_context(),
        SOURCE,
        Apdu::ConfirmedRequest(request(service, bytes, id)),
        ReceivedApdu {
            router_response: None,
            direct_response: None,
            apdu: Bytes::new(),
            source_mac: MacAddr::from_slice(SOURCE),
            ingress_network: None,
            source_network: route(),
            link_layer_group: false,
            is_group: false,
            global_broadcast: false,
            data_attributes: vec![],
            provenance: TransportProvenance::unverified(),
            reply_tx: Some(tx),
        },
    )
    .await;
    tokio::time::timeout(Duration::from_secs(2), rx)
        .await
        .expect("handler completed")
        .ok()
}

async fn snapshot(
    server: &BACnetServer<TestTransport>,
) -> (
    Vec<(ObjectIdentifier, PropertyIdentifier, PropertyValue)>,
    Vec<u8>,
    usize,
) {
    let db = server.db.read().await;
    let mut objects = db.list_objects();
    objects.sort_by_key(|id| (id.object_type().to_raw(), id.instance_number()));
    let mut values = Vec::new();
    for id in objects {
        let object = db.get(&id).unwrap();
        for property in [
            PropertyIdentifier::OBJECT_NAME,
            PropertyIdentifier::DESCRIPTION,
            PropertyIdentifier::PRESENT_VALUE,
            PropertyIdentifier::ALARM_VALUES,
            PropertyIdentifier::FILE_SIZE,
            PropertyIdentifier::ARCHIVE,
        ] {
            if let Ok(value) = object.read_property(property, None) {
                values.push((id, property, value));
            }
        }
    }
    let file = db
        .get(&oid(ObjectType::FILE, 1))
        .unwrap()
        .file_storage_internal()
        .unwrap()
        .read_stream(0, 100)
        .unwrap()
        .data;
    (values, file, server.cov_table.read().await.len())
}

fn expected(
    service: ConfirmedServiceChoice,
    row: MutationServiceCounters,
) -> MutationDecisionCounters {
    let mut counters = MutationDecisionCounters::default();
    *match service {
        ConfirmedServiceChoice::WRITE_PROPERTY => &mut counters.write_property,
        ConfirmedServiceChoice::WRITE_PROPERTY_MULTIPLE => &mut counters.write_property_multiple,
        ConfirmedServiceChoice::CREATE_OBJECT => &mut counters.create_object,
        ConfirmedServiceChoice::DELETE_OBJECT => &mut counters.delete_object,
        ConfirmedServiceChoice::ADD_LIST_ELEMENT => &mut counters.add_list_element,
        ConfirmedServiceChoice::REMOVE_LIST_ELEMENT => &mut counters.remove_list_element,
        ConfirmedServiceChoice::ATOMIC_WRITE_FILE => &mut counters.atomic_write_file,
        ConfirmedServiceChoice::SUBSCRIBE_COV => &mut counters.subscribe_cov,
        ConfirmedServiceChoice::SUBSCRIBE_COV_PROPERTY => &mut counters.subscribe_cov_property,
        ConfirmedServiceChoice::SUBSCRIBE_COV_PROPERTY_MULTIPLE => {
            &mut counters.subscribe_cov_property_multiple
        }
        _ => panic!("not a covered mutation"),
    } = row;
    counters
}

#[tokio::test]
async fn mutation_policy_matrix_all_ten_decisions_and_post_issuance_reauthorization() {
    assert_eq!(
        ServerConfig::default().mutation_policy,
        MutationPolicy::Permissive
    );
    for policy in [MutationPolicy::Permissive, MutationPolicy::DenyAll] {
        for callback in 0..4 {
            for (service, bytes, _) in cases() {
                let calls = Arc::new(AtomicUsize::new(0));
                let seen = calls.clone();
                let authorizer: Option<MutationAuthorizer> = (callback != 0).then(|| {
                    Arc::new(move |_: &MutationAuthorizationContext| {
                        seen.fetch_add(1, Ordering::Relaxed);
                        assert_ne!(callback, 3, "authorizer panic");
                        callback == 1
                    }) as MutationAuthorizer
                });
                let mut server = server(policy, authorizer).await;
                assert_eq!(
                    server.mutation_decision_counters(),
                    MutationDecisionCounters::default()
                );
                let before = snapshot(&server).await;
                let response = dispatch(&server, service, bytes.clone(), 1).await.unwrap();
                let allowed = policy == MutationPolicy::Permissive && callback <= 1;
                if allowed {
                    assert!(
                        matches!(apdu(response), Apdu::SimpleAck(_) | Apdu::ComplexAck(_)),
                        "{service:?}"
                    );
                    assert_ne!(snapshot(&server).await, before, "{service:?}");
                } else {
                    assert_denied(response, service, 1);
                    assert_eq!(snapshot(&server).await, before, "{service:?}");
                    assert!(server.test_network().transport().sent().is_empty());
                }
                let counters = expected(
                    service,
                    MutationServiceCounters {
                        allow_total: u64::from(allowed),
                        deny_total: u64::from(!allowed),
                        policy_deny_total: u64::from(policy == MutationPolicy::DenyAll),
                    },
                );
                assert_eq!(server.mutation_decision_counters(), counters, "{service:?}");
                // The first encoded reply handoff ended ordinary transaction
                // ownership. Reuse runs the same policy again against current state.
                let after_first = snapshot(&server).await;
                let reused = dispatch(&server, service, bytes, 1).await.unwrap();
                if !allowed {
                    assert_denied(reused, service, 1);
                    assert_eq!(snapshot(&server).await, before, "{service:?}");
                } else if matches!(
                    service,
                    ConfirmedServiceChoice::CREATE_OBJECT | ConfirmedServiceChoice::DELETE_OBJECT
                ) {
                    let Apdu::Error(error) = apdu(reused) else {
                        panic!("repeated object operation must see current state")
                    };
                    assert_eq!(error.error_class, ErrorClass::OBJECT);
                    assert_eq!(
                        error.error_code,
                        if service == ConfirmedServiceChoice::CREATE_OBJECT {
                            ErrorCode::OBJECT_IDENTIFIER_ALREADY_EXISTS
                        } else {
                            ErrorCode::UNKNOWN_OBJECT
                        }
                    );
                    assert_eq!(snapshot(&server).await, after_first);
                } else if service == ConfirmedServiceChoice::REMOVE_LIST_ELEMENT {
                    // The element went with the first removal (#1027).
                    let Apdu::Error(error) = apdu(reused) else {
                        panic!("repeated removal must see current state")
                    };
                    let body =
                        bacnet_services::list_manipulation::ChangeListError::try_from(&error)
                            .unwrap();
                    assert_eq!(
                        (
                            body.error_class,
                            body.error_code,
                            body.first_failed_element_number
                        ),
                        (ErrorClass::SERVICES, ErrorCode::LIST_ELEMENT_NOT_FOUND, 1)
                    );
                    assert_eq!(snapshot(&server).await, after_first);
                } else {
                    assert!(
                        matches!(apdu(reused), Apdu::SimpleAck(_) | Apdu::ComplexAck(_)),
                        "{service:?}"
                    );
                    if service == ConfirmedServiceChoice::ATOMIC_WRITE_FILE {
                        assert_eq!(
                            snapshot(&server).await.1,
                            [after_first.1.as_slice(), b"changed"].concat()
                        );
                    }
                }
                let twice = expected(
                    service,
                    MutationServiceCounters {
                        allow_total: 2 * u64::from(allowed),
                        deny_total: 2 * u64::from(!allowed),
                        policy_deny_total: 2 * u64::from(policy == MutationPolicy::DenyAll),
                    },
                );
                assert_eq!(server.mutation_decision_counters(), twice, "{service:?}");
                assert_eq!(
                    calls.load(Ordering::Relaxed),
                    2 * usize::from(policy == MutationPolicy::Permissive && callback != 0)
                );
                server.stop().await.unwrap();
                assert_eq!(server.mutation_decision_counters(), twice);
            }
        }
    }
}

#[test]
fn mutation_policy_builders_preserve_policies_and_defaults() {
    assert_eq!(
        BACnetServer::bip_builder().config.mutation_policy,
        MutationPolicy::Permissive
    );
    assert_eq!(
        BACnetServer::<TestTransport>::generic_builder()
            .config
            .mutation_policy,
        MutationPolicy::Permissive
    );
    let generic = BACnetServer::<TestTransport>::generic_builder()
        .policy(crate::FnPolicy::new().mutation(|_| true))
        .mutation_policy(MutationPolicy::DenyAll)
        .config;
    let bip = BACnetServer::bip_builder()
        .mutation_policy(MutationPolicy::DenyAll)
        .policy(crate::FnPolicy::new().mutation(|_| true))
        .config;
    for config in [generic, bip] {
        assert_eq!(config.mutation_policy, MutationPolicy::DenyAll);
        assert!(config.policy.is_some());
        assert!(format!("{config:?}").contains("mutation_policy: DenyAll"));
    }
    #[cfg(feature = "sc-tls")]
    {
        assert_eq!(
            BACnetServer::sc_builder().config.mutation_policy,
            MutationPolicy::Permissive
        );
        let config = BACnetServer::sc_builder()
            .mutation_policy(MutationPolicy::DenyAll)
            .policy(crate::FnPolicy::new().mutation(|_| true))
            .config;
        assert_eq!(config.mutation_policy, MutationPolicy::DenyAll);
        assert!(config.policy.is_some());
    }
}

fn descriptions() -> Bytes {
    wpm(vec![WriteAccessSpecification {
        object_identifier: oid(ObjectType::BINARY_VALUE, 1),
        list_of_properties: ["first", "second", "third"]
            .map(|text| BACnetPropertyValue {
                property_identifier: PropertyIdentifier::DESCRIPTION,
                property_array_index: None,
                value: value(PropertyValue::CharacterString(text.into())),
                priority: None,
            })
            .to_vec(),
    }])
}

#[tokio::test]
async fn mutation_wpm_counts_elements_not_objects_requests_or_unvisited_suffixes() {
    let service = ConfirmedServiceChoice::WRITE_PROPERTY_MULTIPLE;
    for policy in [MutationPolicy::Permissive, MutationPolicy::DenyAll] {
        for callback in 0..4 {
            let calls = Arc::new(AtomicUsize::new(0));
            let seen = calls.clone();
            let authorizer: Option<MutationAuthorizer> = (callback != 0).then(|| {
                Arc::new(move |_: &MutationAuthorizationContext| {
                    let n = seen.fetch_add(1, Ordering::Relaxed);
                    if n == 1 && callback >= 2 {
                        assert_ne!(callback, 3, "second element panic");
                        return false;
                    }
                    true
                }) as MutationAuthorizer
            });
            let mut server = server(policy, authorizer).await;
            let before = snapshot(&server).await;
            let response = dispatch(&server, service, descriptions(), 1).await.unwrap();
            let hard = policy == MutationPolicy::DenyAll;
            let denied = hard || callback >= 2;
            if denied {
                assert_denied(response.clone(), service, 1);
                let Apdu::Error(error) = apdu(response) else {
                    unreachable!()
                };
                let detail = WritePropertyMultipleError::from_error_pdu(&error).unwrap();
                assert_eq!(
                    detail.first_failed_write_attempt.object_identifier,
                    oid(ObjectType::BINARY_VALUE, 1)
                );
                assert_eq!(
                    detail.first_failed_write_attempt.property_identifier,
                    PropertyIdentifier::DESCRIPTION.to_raw()
                );
            } else {
                assert!(matches!(apdu(response), Apdu::SimpleAck(_)));
            }
            if hard {
                assert_eq!(snapshot(&server).await, before);
            } else {
                let db = server.db.read().await;
                let description = db
                    .get(&oid(ObjectType::BINARY_VALUE, 1))
                    .unwrap()
                    .read_property(PropertyIdentifier::DESCRIPTION, None)
                    .unwrap();
                assert_eq!(
                    description,
                    PropertyValue::CharacterString(if denied { "first" } else { "third" }.into())
                );
            }
            assert_eq!(
                server.mutation_decision_counters(),
                expected(
                    service,
                    MutationServiceCounters {
                        allow_total: if hard {
                            0
                        } else if denied {
                            1
                        } else {
                            3
                        },
                        deny_total: u64::from(denied),
                        policy_deny_total: u64::from(hard),
                    }
                )
            );
            assert_eq!(
                calls.load(Ordering::Relaxed),
                if hard || callback == 0 {
                    0
                } else if denied {
                    2
                } else {
                    3
                }
            );
            server.stop().await.unwrap();
        }
    }
}

#[tokio::test]
async fn mutation_pre_gate_failures_do_not_count() {
    for policy in [MutationPolicy::Permissive, MutationPolicy::DenyAll] {
        let mut server = server(policy, Some(Arc::new(|_| panic!("must not authorize")))).await;
        for (index, (service, bytes, _)) in cases().into_iter().enumerate() {
            let malformed = bytes.slice(..1);
            let response = dispatch(&server, service, malformed, index as u8)
                .await
                .unwrap();
            assert!(matches!(apdu(response), Apdu::Error(_) | Apdu::Reject(_)));
        }
        let response = dispatch(
            &server,
            ConfirmedServiceChoice::WRITE_PROPERTY_MULTIPLE,
            Bytes::new(),
            20,
        )
        .await
        .unwrap();
        assert!(matches!(apdu(response), Apdu::SimpleAck(_)));
        assert_eq!(
            server.mutation_decision_counters(),
            MutationDecisionCounters::default()
        );
        server.stop().await.unwrap();
    }
}

#[tokio::test]
async fn mutation_wpm_malformed_suffix_counts_only_reached_prefix() {
    for authorizer in [
        None,
        Some(Arc::new(|_: &MutationAuthorizationContext| true) as MutationAuthorizer),
    ] {
        let mut server = server(MutationPolicy::Permissive, authorizer).await;
        let mut bytes = BytesMut::from(descriptions().as_ref());
        bytes.extend_from_slice(&[0x0c]); // incomplete next object after three writes
        let service = ConfirmedServiceChoice::WRITE_PROPERTY_MULTIPLE;
        let response = dispatch(&server, service, bytes.freeze(), 1).await.unwrap();
        let Apdu::Error(error) = apdu(response) else {
            panic!("expected suffix Error")
        };
        assert_eq!(error.error_code, ErrorCode::INVALID_TAG);
        assert_eq!(
            server.mutation_decision_counters(),
            expected(
                service,
                MutationServiceCounters {
                    allow_total: 3,
                    ..Default::default()
                }
            )
        );
        server.stop().await.unwrap();
    }
}

#[tokio::test]
async fn mutation_deny_all_does_not_gate_reads_discovery_or_dcc() {
    let mut server = server(
        MutationPolicy::DenyAll,
        Some(Arc::new(|_| panic!("not a mutation"))),
    )
    .await;
    let mut data = BytesMut::new();
    ReadPropertyRequest {
        object_identifier: oid(ObjectType::BINARY_VALUE, 1),
        property_identifier: PropertyIdentifier::PRESENT_VALUE,
        property_array_index: None,
    }
    .encode(&mut data);
    assert!(matches!(
        apdu(
            dispatch(
                &server,
                ConfirmedServiceChoice::READ_PROPERTY,
                data.freeze(),
                1
            )
            .await
            .unwrap()
        ),
        Apdu::ComplexAck(_)
    ));
    server
        .db
        .write()
        .await
        .add(Box::new(
            DeviceObject::new(DeviceConfig::default()).unwrap(),
        ))
        .unwrap();
    BACnetServer::<TestTransport>::handle_unconfirmed_request(
        &UnconfirmedServices {
            db: Arc::clone(&server.db),
            comm_state: Arc::clone(&server.comm_state),
            device_bindings: Arc::clone(&server.device_bindings),
            discovery_limiter: Arc::clone(&server.discovery_limiter),
            time_sync_limiter: Arc::clone(&server.time_sync_limiter),
            notification_transactions: Arc::clone(&server.notification_transactions),
            ..UnconfirmedServices::for_test(
                Arc::clone(server.test_network()),
                server.config.clone(),
            )
        },
        UnconfirmedRequestPdu {
            service_choice: UnconfirmedServiceChoice::WHO_IS,
            service_request: Bytes::new(),
        },
        &ReceivedApdu {
            router_response: None,
            direct_response: None,
            apdu: Bytes::new(),
            source_mac: MacAddr::from_slice(SOURCE),
            ingress_network: None,
            source_network: route(),
            link_layer_group: false,
            is_group: false,
            global_broadcast: false,
            data_attributes: vec![],
            provenance: TransportProvenance::unverified(),
            reply_tx: None,
        },
    )
    .await;
    let sent = server
        .test_network()
        .transport()
        .sent()
        .lock()
        .pop()
        .unwrap()
        .npdu;
    let Apdu::UnconfirmedRequest(i_am) = apdu(sent) else {
        panic!("expected I-Am")
    };
    assert_eq!(i_am.service_choice, UnconfirmedServiceChoice::I_AM);
    server.config_mut().dcc_policy = DccPolicy::RequirePassword;
    server.config_mut().dcc_password = Some("boundary-test".into());
    server.comm_state.set_for_test(DccState::DisableInitiation);
    let mut data = BytesMut::new();
    DeviceCommunicationControlRequest {
        time_duration: None,
        enable_disable: EnableDisable::ENABLE,
        password: Some("boundary-test".into()),
    }
    .encode(&mut data)
    .unwrap();
    assert!(matches!(
        apdu(
            dispatch(
                &server,
                ConfirmedServiceChoice::DEVICE_COMMUNICATION_CONTROL,
                data.freeze(),
                2
            )
            .await
            .unwrap()
        ),
        Apdu::SimpleAck(_)
    ));
    assert_eq!(server.comm_state.get(), DccState::Enable);
    assert_eq!(server.dcc_outcome_counters().accepted_total, 1);
    assert_eq!(
        server.mutation_decision_counters(),
        MutationDecisionCounters::default()
    );
    server.stop().await.unwrap();
}