bacnet-objects 0.11.0

BACnet object model: traits, database, and standard object types
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
//! Intrinsic-reporting property wiring on Binary Input, Output, and Value
//! objects, including event-history coverage for #235.
//!
//! Distribution is exercised at wire level in the server tests. These tests
//! pin the object-level commissioning surface — before #229, Time_Delay
//! and Notify_Type had no arms at all and Event_Enable was readable but not
//! writable, so the transition bits were stuck at (F, F, F).

use super::*;
use bacnet_encoding::primitives::{decode_timestamp_choice, encode_timestamp_choice};
use bacnet_types::bitstring::EventTransitionBits;
use bacnet_types::enums::{ErrorClass, ErrorCode, EventState};
use bacnet_types::primitives::{Date, Time};
use bytes::BytesMut;

fn timestamp_property_value(timestamp: &BACnetTimeStamp) -> PropertyValue {
    let mut bytes = BytesMut::new();
    encode_timestamp_choice(&mut bytes, timestamp).unwrap();
    PropertyValue::ApplicationData(bytes.to_vec())
}

fn assert_timestamp_value(actual: PropertyValue, expected: &BACnetTimeStamp, context: &str) {
    let PropertyValue::ApplicationData(bytes) = actual else {
        panic!("{context}: expected timestamp ApplicationData");
    };
    let (decoded, end) = decode_timestamp_choice(&bytes, 0).unwrap();
    assert_eq!(&decoded, expected, "{context}: timestamp choice");
    assert_eq!(end, bytes.len(), "{context}: trailing timestamp bytes");
}

fn assert_timestamp_array(actual: PropertyValue, expected: &[BACnetTimeStamp; 3], context: &str) {
    let PropertyValue::List(elements) = actual else {
        panic!("{context}: expected timestamp list");
    };
    assert_eq!(elements.len(), expected.len(), "{context}: array length");
    for (slot, (actual, expected)) in elements.into_iter().zip(expected).enumerate() {
        assert_timestamp_value(actual, expected, &format!("{context} slot {}", slot + 1));
    }
}

fn assert_protocol_error(result: Result<(), Error>, code: ErrorCode) {
    match result.unwrap_err() {
        Error::Protocol {
            class,
            code: actual,
        } => {
            assert_eq!(class, ErrorClass::PROPERTY.to_raw() as u32);
            assert_eq!(actual, code.to_raw() as u32);
        }
        other => panic!("expected protocol error, got {other:?}"),
    }
}

fn assert_property_read_error(result: Result<PropertyValue, Error>, code: ErrorCode) {
    match result.unwrap_err() {
        Error::Protocol {
            class,
            code: actual,
        } => {
            assert_eq!(class, ErrorClass::PROPERTY.to_raw() as u32);
            assert_eq!(actual, code.to_raw() as u32);
        }
        other => panic!("expected protocol error, got {other:?}"),
    }
}

/// Assert a refused write came back as PROPERTY / WRITE_ACCESS_DENIED, the error
/// the generic write macro raises deliberately. Checking only `is_err` would also
/// accept UNKNOWN_PROPERTY, which is what a property falling through to the
/// catch-all arm returns — a silent loss of the denial, not a denial.
fn assert_write_access_denied(
    result: Result<(), Error>,
    property: PropertyIdentifier,
    label: &str,
) {
    match result.expect_err(&format!("{label}: {property:?} write must be refused")) {
        Error::Protocol { class, code } => {
            assert_eq!(
                class,
                ErrorClass::PROPERTY.to_raw() as u32,
                "{label}: {property:?} error class"
            );
            assert_eq!(
                code,
                ErrorCode::WRITE_ACCESS_DENIED.to_raw() as u32,
                "{label}: {property:?} error code"
            );
        }
        other => panic!("{label}: {property:?} expected WRITE_ACCESS_DENIED, got {other:?}"),
    }
}

/// Every property that commissions intrinsic reporting must survive a network
/// write and read back, and be advertised in both `property_list` and
/// `is_writable_property` so the PICS matches dispatch.
fn assert_event_properties_round_trip(object: &mut dyn BACnetObject, label: &str) {
    for (property, value) in [
        (
            PropertyIdentifier::EVENT_ENABLE,
            PropertyValue::BitString {
                unused_bits: 5,
                data: vec![EventTransitionBits::all().to_bacnet()],
            },
        ),
        (
            PropertyIdentifier::NOTIFY_TYPE,
            PropertyValue::Enumerated(1),
        ),
        (
            PropertyIdentifier::NOTIFICATION_CLASS,
            PropertyValue::Unsigned(42),
        ),
        (PropertyIdentifier::TIME_DELAY, PropertyValue::Unsigned(7)),
        (
            PropertyIdentifier::TIME_DELAY_NORMAL,
            PropertyValue::Unsigned(9),
        ),
    ] {
        object
            .write_property(property, None, value.clone(), None)
            .unwrap_or_else(|e| panic!("{label}: {property:?} write rejected: {e:?}"));
        assert_eq!(
            object.read_property(property, None).unwrap(),
            value,
            "{label}: {property:?} must read back what was written"
        );
        assert!(
            object.property_list().contains(&property),
            "{label}: {property:?} must be advertised in Property_List"
        );
        assert!(
            object.is_writable_property(property),
            "{label}: {property:?} must be advertised writable"
        );
    }

    // Time_Delay_Normal mirrors Time_Delay's write validation: Unsigned within
    // the u32 span or refused with the Clause 15.9.1.3 pairings, and a refused
    // write leaves the stored value untouched.
    assert_protocol_error(
        object.write_property(
            PropertyIdentifier::TIME_DELAY_NORMAL,
            None,
            PropertyValue::Enumerated(9),
            None,
        ),
        ErrorCode::INVALID_DATA_TYPE,
    );
    assert_protocol_error(
        object.write_property(
            PropertyIdentifier::TIME_DELAY_NORMAL,
            None,
            PropertyValue::Unsigned(u32::MAX as u64 + 1),
            None,
        ),
        ErrorCode::VALUE_OUT_OF_RANGE,
    );
    assert_eq!(
        object
            .read_property(PropertyIdentifier::TIME_DELAY_NORMAL, None)
            .unwrap(),
        PropertyValue::Unsigned(9),
        "{label}: refused Time_Delay_Normal writes must leave the value untouched"
    );

    // A single set bit must land on the transition it names, not on whichever
    // bit a mask error happens to select. TO_NORMAL is wire bit 2 (0x20).
    object
        .write_property(
            PropertyIdentifier::EVENT_ENABLE,
            None,
            PropertyValue::BitString {
                unused_bits: 5,
                data: vec![EventTransitionBits::TO_NORMAL.to_bacnet()],
            },
            None,
        )
        .unwrap();
    assert_eq!(
        object
            .read_property(PropertyIdentifier::EVENT_ENABLE, None)
            .unwrap(),
        PropertyValue::BitString {
            unused_bits: 5,
            data: vec![0x20],
        },
        "{label}: TO_NORMAL alone must read back as wire bit 2"
    );

    // Acked_Transitions is readable but never writable: the alarm-
    // acknowledgment process maintains it from event-state transitions and
    // acknowledgment indications, and an indication ORs the bit in where a
    // property write would assign — a write could fabricate and erase
    // acknowledgments alike.
    assert_write_access_denied(
        object.write_property(
            PropertyIdentifier::ACKED_TRANSITIONS,
            None,
            PropertyValue::BitString {
                unused_bits: 5,
                data: vec![EventTransitionBits::TO_OFFNORMAL.to_bacnet()],
            },
            None,
        ),
        PropertyIdentifier::ACKED_TRANSITIONS,
        label,
    );
    assert!(!object.is_writable_property(PropertyIdentifier::ACKED_TRANSITIONS));
    assert!(object
        .property_list()
        .contains(&PropertyIdentifier::ACKED_TRANSITIONS));

    // Event_State is maintained by the detector, never assignable from the
    // network — a write arm would let a client fake an alarm state.
    assert_write_access_denied(
        object.write_property(
            PropertyIdentifier::EVENT_STATE,
            None,
            PropertyValue::Enumerated(EventState::OFFNORMAL.to_raw()),
            None,
        ),
        PropertyIdentifier::EVENT_STATE,
        label,
    );
    assert!(!object.is_writable_property(PropertyIdentifier::EVENT_STATE));
}

#[test]
fn bi_event_properties_round_trip_and_match_pics() {
    let mut bi = BinaryInputObject::new(1, "BI-1").unwrap();
    assert_event_properties_round_trip(&mut bi, "BI");
}

#[test]
fn bv_event_properties_round_trip_and_match_pics() {
    let mut bv = BinaryValueObject::new(1, "BV-1").unwrap();
    assert_event_properties_round_trip(&mut bv, "BV");
}

/// Clause 13.3: "If no value is available for this parameter, then it takes on
/// the value of the pTimeDelay parameter." An object that was never written a
/// Time_Delay_Normal reads back the effective (fallback) delay.
#[test]
fn binary_time_delay_normal_defaults_to_time_delay_when_unwritten() {
    for (mut object, label) in [
        (
            Box::new(BinaryInputObject::new(1, "BI-1").unwrap()) as Box<dyn BACnetObject>,
            "BI",
        ),
        (
            Box::new(BinaryOutputObject::new(1, "BO-1").unwrap()) as Box<dyn BACnetObject>,
            "BO",
        ),
        (
            Box::new(BinaryValueObject::new(1, "BV-1").unwrap()) as Box<dyn BACnetObject>,
            "BV",
        ),
    ] {
        assert_eq!(
            object
                .read_property(PropertyIdentifier::TIME_DELAY_NORMAL, None)
                .unwrap(),
            object
                .read_property(PropertyIdentifier::TIME_DELAY, None)
                .unwrap(),
            "{label}: unwritten Time_Delay_Normal reads back Time_Delay"
        );
        object
            .write_property(
                PropertyIdentifier::TIME_DELAY,
                None,
                PropertyValue::Unsigned(11),
                None,
            )
            .unwrap();
        assert_eq!(
            object
                .read_property(PropertyIdentifier::TIME_DELAY_NORMAL, None)
                .unwrap(),
            PropertyValue::Unsigned(11),
            "{label}: the fallback tracks Time_Delay"
        );
    }
}

#[test]
fn binary_alarm_value_round_trips_and_matches_pics() {
    for object in [
        &mut BinaryInputObject::new(1, "BI-1").unwrap() as &mut dyn BACnetObject,
        &mut BinaryValueObject::new(1, "BV-1").unwrap() as &mut dyn BACnetObject,
    ] {
        assert_eq!(
            object
                .read_property(PropertyIdentifier::ALARM_VALUE, None)
                .unwrap(),
            PropertyValue::Enumerated(1)
        );
        object
            .write_property(
                PropertyIdentifier::ALARM_VALUE,
                None,
                PropertyValue::Enumerated(0),
                None,
            )
            .unwrap();
        assert_eq!(
            object
                .read_property(PropertyIdentifier::ALARM_VALUE, None)
                .unwrap(),
            PropertyValue::Enumerated(0)
        );
        assert!(object
            .property_list()
            .contains(&PropertyIdentifier::ALARM_VALUE));
        assert!(object.is_writable_property(PropertyIdentifier::ALARM_VALUE));
        assert!(!object
            .property_list()
            .contains(&PropertyIdentifier::ALARM_VALUES));
        assert_protocol_error(
            object.write_property(
                PropertyIdentifier::ALARM_VALUE,
                None,
                PropertyValue::Unsigned(1),
                None,
            ),
            ErrorCode::INVALID_DATA_TYPE,
        );
        assert_eq!(
            object
                .read_property(PropertyIdentifier::ALARM_VALUE, None)
                .unwrap(),
            PropertyValue::Enumerated(0),
            "a rejected wrong-type write must preserve Alarm_Value"
        );
        assert_protocol_error(
            object.write_property(
                PropertyIdentifier::ALARM_VALUE,
                None,
                PropertyValue::Enumerated(2),
                None,
            ),
            ErrorCode::VALUE_OUT_OF_RANGE,
        );
    }

    assert_protocol_error(
        BinaryInputObject::new(2, "BI-2")
            .unwrap()
            .read_property(PropertyIdentifier::ALARM_VALUES, None)
            .map(|_| ()),
        ErrorCode::UNKNOWN_PROPERTY,
    );
}

#[test]
fn fresh_binary_input_is_default_armed_for_active() {
    let mut bi = BinaryInputObject::new(1, "BI-1").unwrap();
    bi.set_present_value(1);
    let outcome = bi
        .evaluate_intrinsic_reporting()
        .expect("ACTIVE is the default Alarm_Value");
    assert_eq!(outcome.change.to, EventState::OFFNORMAL);
}

#[test]
fn binary_event_history_is_listed_readable_and_read_only() {
    let timestamps = PropertyValue::List(vec![
        timestamp_property_value(&BACnetTimeStamp::SequenceNumber(0)),
        timestamp_property_value(&BACnetTimeStamp::SequenceNumber(0)),
        timestamp_property_value(&BACnetTimeStamp::SequenceNumber(0)),
    ]);
    let messages = PropertyValue::List(vec![
        PropertyValue::CharacterString(String::new()),
        PropertyValue::CharacterString(String::new()),
        PropertyValue::CharacterString(String::new()),
    ]);
    for (mut object, label) in [
        (
            Box::new(BinaryInputObject::new(1, "BI-1").unwrap()) as Box<dyn BACnetObject>,
            "BI",
        ),
        (
            Box::new(BinaryOutputObject::new(1, "BO-1").unwrap()) as Box<dyn BACnetObject>,
            "BO",
        ),
        (
            Box::new(BinaryValueObject::new(1, "BV-1").unwrap()) as Box<dyn BACnetObject>,
            "BV",
        ),
    ] {
        for (property, expected) in [
            (PropertyIdentifier::EVENT_TIME_STAMPS, &timestamps),
            (PropertyIdentifier::EVENT_MESSAGE_TEXTS, &messages),
        ] {
            assert!(object.property_list().contains(&property), "{label}");
            assert_eq!(object.read_property(property, None).unwrap(), *expected);
            assert_eq!(
                object.read_property(property, Some(0)).unwrap(),
                PropertyValue::Unsigned(3)
            );
            let second = match property {
                p if p == PropertyIdentifier::EVENT_TIME_STAMPS => {
                    timestamp_property_value(&BACnetTimeStamp::SequenceNumber(0))
                }
                _ => PropertyValue::CharacterString(String::new()),
            };
            assert_eq!(object.read_property(property, Some(2)).unwrap(), second);
            assert_property_read_error(
                object.read_property(property, Some(4)),
                ErrorCode::INVALID_ARRAY_INDEX,
            );
            assert_write_access_denied(
                object.write_property(property, None, expected.clone(), None),
                property,
                label,
            );
            assert!(!object.is_writable_property(property));
        }
    }
}

macro_rules! assert_seeded_binary_event_history {
    ($object:expr, $label:literal) => {{
        let mut object = $object;
        let expected_timestamps = [
            BACnetTimeStamp::Time(Time {
                hour: 1,
                minute: 2,
                second: 3,
                hundredths: 4,
            }),
            BACnetTimeStamp::SequenceNumber(22),
            BACnetTimeStamp::DateTime {
                date: Date {
                    year: 126,
                    month: 7,
                    day: 30,
                    day_of_week: 4,
                },
                time: Time {
                    hour: 12,
                    minute: 34,
                    second: 56,
                    hundredths: 78,
                },
            },
        ];
        object.event_history.time_stamps = expected_timestamps.clone();
        object.event_history.message_texts = ["offnormal".into(), "fault".into(), "normal".into()];

        assert_timestamp_array(
            object
                .read_property(PropertyIdentifier::EVENT_TIME_STAMPS, None)
                .unwrap(),
            &expected_timestamps,
            concat!($label, " timestamps"),
        );
        assert_eq!(
            object
                .read_property(PropertyIdentifier::EVENT_MESSAGE_TEXTS, None)
                .unwrap(),
            PropertyValue::List(vec![
                PropertyValue::CharacterString("offnormal".into()),
                PropertyValue::CharacterString("fault".into()),
                PropertyValue::CharacterString("normal".into()),
            ]),
            "{} messages",
            $label
        );

        for property in [
            PropertyIdentifier::EVENT_TIME_STAMPS,
            PropertyIdentifier::EVENT_MESSAGE_TEXTS,
        ] {
            assert_eq!(
                object.read_property(property, Some(0)).unwrap(),
                PropertyValue::Unsigned(3),
                "{} {property:?} count",
                $label
            );
        }
        for (slot, expected) in expected_timestamps.iter().enumerate() {
            assert_timestamp_value(
                object
                    .read_property(PropertyIdentifier::EVENT_TIME_STAMPS, Some(slot as u32 + 1))
                    .unwrap(),
                expected,
                &format!("{} timestamp slot {}", $label, slot + 1),
            );
        }
        for (slot, expected) in ["offnormal", "fault", "normal"].into_iter().enumerate() {
            assert_eq!(
                object
                    .read_property(
                        PropertyIdentifier::EVENT_MESSAGE_TEXTS,
                        Some(slot as u32 + 1),
                    )
                    .unwrap(),
                PropertyValue::CharacterString(expected.into()),
                "{} message slot {}",
                $label,
                slot + 1
            );
        }
        for property in [
            PropertyIdentifier::EVENT_TIME_STAMPS,
            PropertyIdentifier::EVENT_MESSAGE_TEXTS,
        ] {
            for index in [4, u32::MAX] {
                assert_property_read_error(
                    object.read_property(property, Some(index)),
                    ErrorCode::INVALID_ARRAY_INDEX,
                );
            }
        }
    }};
}

#[test]
fn binary_seeded_event_history_preserves_array_order_and_timestamp_choices() {
    assert_seeded_binary_event_history!(BinaryInputObject::new(1, "BI-1").unwrap(), "BI");
    assert_seeded_binary_event_history!(BinaryOutputObject::new(1, "BO-1").unwrap(), "BO");
    assert_seeded_binary_event_history!(BinaryValueObject::new(1, "BV-1").unwrap(), "BV");
}

macro_rules! assert_binary_history_reset {
    ($object:expr, $label:literal) => {{
        let mut object = $object;
        object.event_history.time_stamps[0] = BACnetTimeStamp::SequenceNumber(91);
        object.event_history.message_texts[1] = "seeded".into();
        object
            .write_property(
                PropertyIdentifier::EVENT_DETECTION_ENABLE,
                None,
                PropertyValue::Boolean(false),
                None,
            )
            .unwrap();
        assert_eq!(
            object
                .read_property(PropertyIdentifier::EVENT_TIME_STAMPS, None)
                .unwrap(),
            PropertyValue::List(vec![
                timestamp_property_value(
                    &BACnetTimeStamp::SequenceNumber(0)
                );
                3
            ]),
            "{} timestamp reset",
            $label
        );
        assert_eq!(
            object
                .read_property(PropertyIdentifier::EVENT_MESSAGE_TEXTS, None)
                .unwrap(),
            PropertyValue::List(vec![PropertyValue::CharacterString(String::new()); 3]),
            "{} message reset",
            $label
        );
    }};
}

#[test]
fn binary_detection_disable_resets_each_event_history() {
    assert_binary_history_reset!(BinaryInputObject::new(1, "BI-1").unwrap(), "BI");
    assert_binary_history_reset!(BinaryOutputObject::new(1, "BO-1").unwrap(), "BO");
    assert_binary_history_reset!(BinaryValueObject::new(1, "BV-1").unwrap(), "BV");
}

// ──────────────────────────────────────────────────────────────────────────
// #255 — Notify_Type production validation and Event_Enable bit-string
// width validation on the shared write macro.
// ──────────────────────────────────────────────────────────────────────────

/// BACnetNotifyType is a closed {alarm(0), event(1), ack-notification(2)}
/// production (Clause 21). An out-of-production write is PROPERTY /
/// VALUE_OUT_OF_RANGE (Clause 15.9.1.3) and leaves the stored value untouched.
#[test]
fn bi_notify_type_rejects_out_of_production_values() {
    let mut bi = BinaryInputObject::new(1, "BI-1").unwrap();
    assert_eq!(
        bi.read_property(PropertyIdentifier::NOTIFY_TYPE, None)
            .unwrap(),
        PropertyValue::Enumerated(0)
    );

    for out_of_production in [3u32, 99, u32::MAX] {
        assert_protocol_error(
            bi.write_property(
                PropertyIdentifier::NOTIFY_TYPE,
                None,
                PropertyValue::Enumerated(out_of_production),
                None,
            ),
            ErrorCode::VALUE_OUT_OF_RANGE,
        );
        assert_eq!(
            bi.read_property(PropertyIdentifier::NOTIFY_TYPE, None)
                .unwrap(),
            PropertyValue::Enumerated(0),
            "a refused Notify_Type write must leave the value untouched ({out_of_production})"
        );
    }
    for in_production in [0u32, 1, 2] {
        bi.write_property(
            PropertyIdentifier::NOTIFY_TYPE,
            None,
            PropertyValue::Enumerated(in_production),
            None,
        )
        .expect("named Notify_Type values must be accepted");
    }
}

/// BACnetEventTransitionBits is a 3-bit production (Clause 21); its canonical
/// encoding is one content octet with 5 unused bits. A write declaring any
/// other shape is PROPERTY / INVALID_DATA_ENCODING (Clause 15.9.1.3), not a
/// value to mask and normalize.
#[test]
fn bi_event_enable_rejects_noncanonical_bit_strings() {
    let mut bi = BinaryInputObject::new(1, "BI-1").unwrap();
    let canonical = bi
        .read_property(PropertyIdentifier::EVENT_ENABLE, None)
        .unwrap();

    for (unused_bits, data) in [
        (0u8, vec![0xFFu8]),     // 8-bit string where the production defines 3
        (5u8, vec![0xFF, 0xFF]), // two content octets
        (4u8, vec![0xF0u8]),     // half-octet string
        (5u8, vec![]),           // no content octet
    ] {
        assert_protocol_error(
            bi.write_property(
                PropertyIdentifier::EVENT_ENABLE,
                None,
                PropertyValue::BitString { unused_bits, data },
                None,
            ),
            ErrorCode::INVALID_DATA_ENCODING,
        );
        assert_eq!(
            bi.read_property(PropertyIdentifier::EVENT_ENABLE, None)
                .unwrap(),
            canonical,
            "a refused Event_Enable write must leave the value untouched"
        );
    }

    // The canonical full-width shape stays accepted.
    bi.write_property(
        PropertyIdentifier::EVENT_ENABLE,
        None,
        PropertyValue::BitString {
            unused_bits: 5,
            data: vec![0xA0], // to-offnormal + to-normal, MSB-first
        },
        None,
    )
    .unwrap();
    assert_eq!(
        bi.read_property(PropertyIdentifier::EVENT_ENABLE, None)
            .unwrap(),
        PropertyValue::BitString {
            unused_bits: 5,
            data: vec![0xA0],
        }
    );
}