bacnet-services 0.15.0

BACnet service request/response encode/decode per ASHRAE 135-2020
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
//! Device management services per ASHRAE 135-2020 Clause 16.
//!
//! - DeviceCommunicationControl (Clause 16.1)
//! - ReinitializeDevice (Clause 16.4)
//! - TimeSynchronization (§16.7)
//! - UTCTimeSynchronization (§16.8)

#[cfg(not(feature = "std"))]
use alloc::string::String;

use bacnet_encoding::constructed::tagged::{
    decode_app_fixed, decode_ctx_character_string, decode_ctx_unsigned, decode_optional_ctx,
    expect_end,
};
use bacnet_encoding::primitives;
use bacnet_encoding::tags;
use bacnet_types::enums::{EnableDisable, ReinitializedState};
use bacnet_types::error::{DecodingKind, Error};
use bacnet_types::primitives::{Date, Time};
use bytes::BytesMut;

/// Longest password, in characters, the DCC and ReinitializeDevice requests
/// carry (Clauses 16.1.1.1.3 and 16.4.1.1.2).
pub const MAX_PASSWORD_CHARS: usize = 20;

/// Whether `password` is within [`MAX_PASSWORD_CHARS`]. The limit counts
/// characters, not octets, so a multi-octet UTF-8 character counts once. A
/// server or endpoint checks its configured password with it, since a longer
/// one could never be sent.
///
/// ```
/// use bacnet_services::device_mgmt::password_fits;
///
/// assert!(password_fits(&"é".repeat(20)));
/// assert!(!password_fits(&"a".repeat(21)));
/// ```
pub fn password_fits(password: &str) -> bool {
    password.chars().count() <= MAX_PASSWORD_CHARS
}

/// Whether a decoded password is longer than [`MAX_PASSWORD_CHARS`]. The
/// decoder has already converted UTF-8, UCS-2 and ISO 8859-1 contents to a
/// `String`, one `char` per character of the sender's set.
fn password_too_long(password: Option<&String>) -> bool {
    password.is_some_and(|s| !password_fits(s))
}

/// A decoded password longer than [`MAX_PASSWORD_CHARS`]: its encoding is
/// valid, only its size is outside the service's range, so a responder
/// rejects it as PARAMETER_OUT_OF_RANGE (#2085). `offset` is where the
/// password member starts.
fn password_out_of_range(offset: usize, label: &'static str) -> Error {
    Error::decoding_kind(DecodingKind::OutOfRange, offset, label)
}

/// Whether `password` can be configured or sent: at least one character
/// and at most [`MAX_PASSWORD_CHARS`] (#2094). An empty password is out of
/// the service's range; a caller omits it (`None`) instead.
///
/// ```
/// use bacnet_services::device_mgmt::password_valid;
///
/// assert!(password_valid("secret"));
/// assert!(!password_valid(""));
/// assert!(!password_valid(&"a".repeat(21)));
/// ```
pub fn password_valid(password: &str) -> bool {
    !password.is_empty() && password_fits(password)
}

/// Refuse to encode a password the peer would only refuse, so the caller
/// hears about it before anything is sent: one longer than
/// [`MAX_PASSWORD_CHARS`] (#2085), or an empty one, which the caller should
/// omit instead (#2094). `too_long` and `empty` name the request.
fn check_password(
    password: Option<&str>,
    too_long: &'static str,
    empty: &'static str,
) -> Result<(), Error> {
    match password {
        Some("") => Err(Error::encode(empty)),
        Some(pw) if !password_fits(pw) => Err(Error::encode(too_long)),
        _ => Ok(()),
    }
}

/// A decoded password, with an empty one taken as absent (#2094): some
/// devices send an empty string for "no password", and a server then treats
/// the request as one without a password.
fn present(password: Option<String>) -> Option<String> {
    password.filter(|pw| !pw.is_empty())
}

// ---------------------------------------------------------------------------
// DeviceCommunicationControlRequest
// ---------------------------------------------------------------------------

/// DeviceCommunicationControl-Request service parameters.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeviceCommunicationControlRequest {
    /// Minutes the requested state lasts; `None` means until changed by a later request.
    pub time_duration: Option<u16>,
    /// Requested communication state for the device.
    pub enable_disable: EnableDisable,
    /// Password the device may require before honoring the request, 1 to 20 characters; `None`
    /// when not supplied.
    pub password: Option<String>,
}

impl DeviceCommunicationControlRequest {
    /// Append the ASN.1 encoding of the request to `buf`. Fails, leaving `buf` unchanged, if the
    /// password is empty (omit it instead) or longer than [`MAX_PASSWORD_CHARS`] characters;
    /// fails if it is unencodable.
    pub fn encode(&self, buf: &mut BytesMut) -> Result<(), Error> {
        check_password(
            self.password.as_deref(),
            "DCC password exceeds the 20-character limit",
            "DCC password is empty; omit it (None) to send no password",
        )?;
        // [0] time-duration (optional)
        if let Some(dur) = self.time_duration {
            primitives::encode_ctx_unsigned(buf, 0, dur as u64);
        }
        // [1] enable-disable
        primitives::encode_ctx_enumerated(buf, 1, self.enable_disable.to_raw());
        // [2] password (optional)
        if let Some(ref pw) = self.password {
            primitives::encode_ctx_character_string(buf, 2, pw)?;
        }
        Ok(())
    }

    /// Decode the request from `data`; errors on malformed input, octets after the members, an
    /// out-of-range duration or a password longer than 20 characters. An empty password decodes
    /// as none.
    pub fn decode(data: &[u8]) -> Result<Self, Error> {
        // [0] time-duration (optional)
        let (time_duration, offset) =
            decode_optional_ctx(data, 0, 0, "DCC time-duration", decode_ctx_unsigned::<u16>)?;

        // [1] enable-disable
        let (enable_disable, offset) =
            decode_ctx_unsigned::<u32>(data, offset, 1, "DCC enable-disable")?;
        let enable_disable = EnableDisable::from_raw(enable_disable);

        // [2] password (optional, max 20 characters), then nothing
        let (password, end) =
            decode_optional_ctx(data, offset, 2, "DCC password", decode_ctx_character_string)?;
        expect_end(data, end, end, "DCC")?;
        if password_too_long(password.as_ref()) {
            return Err(password_out_of_range(
                offset,
                "DCC password exceeds 20 characters",
            ));
        }

        Ok(Self {
            time_duration,
            enable_disable,
            password: present(password),
        })
    }
}

// ---------------------------------------------------------------------------
// ReinitializeDeviceRequest
// ---------------------------------------------------------------------------

/// ReinitializeDevice-Request service parameters.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReinitializeDeviceRequest {
    /// Restart or backup/restore action the device is asked to perform.
    pub reinitialized_state: ReinitializedState,
    /// Password the device may require before honoring the request, 1 to 20 characters; `None`
    /// when not supplied.
    pub password: Option<String>,
}

impl ReinitializeDeviceRequest {
    /// Append the ASN.1 encoding of the request to `buf`. Fails, leaving `buf` unchanged, if the
    /// password is empty (omit it instead) or longer than [`MAX_PASSWORD_CHARS`] characters;
    /// fails if it is unencodable.
    pub fn encode(&self, buf: &mut BytesMut) -> Result<(), Error> {
        check_password(
            self.password.as_deref(),
            "ReinitializeDevice password exceeds the 20-character limit",
            "ReinitializeDevice password is empty; omit it (None) to send no password",
        )?;
        // [0] reinitialized-state
        primitives::encode_ctx_enumerated(buf, 0, self.reinitialized_state.to_raw());
        // [1] password (optional)
        if let Some(ref pw) = self.password {
            primitives::encode_ctx_character_string(buf, 1, pw)?;
        }
        Ok(())
    }

    /// Decode the request from `data`; errors on missing or malformed fields, on octets after
    /// them and on a password longer than 20 characters. An empty password decodes as none.
    pub fn decode(data: &[u8]) -> Result<Self, Error> {
        // [0] reinitialized-state
        let (state, offset) = decode_ctx_unsigned::<u32>(data, 0, 0, "Reinitialize state")?;
        let reinitialized_state = ReinitializedState::from_raw(state);

        // [1] password (optional, max 20 characters), then nothing
        let (password, end) = decode_optional_ctx(
            data,
            offset,
            1,
            "ReinitializeDevice password",
            decode_ctx_character_string,
        )?;
        expect_end(data, end, end, "ReinitializeDevice")?;
        if password_too_long(password.as_ref()) {
            return Err(password_out_of_range(
                offset,
                "ReinitializeDevice password exceeds 20 characters",
            ));
        }

        Ok(Self {
            reinitialized_state,
            password: present(password),
        })
    }
}

// ---------------------------------------------------------------------------
// TimeSynchronizationRequest
// ---------------------------------------------------------------------------

/// TimeSynchronization-Request service parameters (APPLICATION-tagged).
///
/// Used for both TimeSynchronization and UTCTimeSynchronization.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TimeSynchronizationRequest {
    /// Calendar date to synchronize to.
    pub date: Date,
    /// Time of day to synchronize to.
    pub time: Time,
}

impl TimeSynchronizationRequest {
    /// Append the application-tagged encoding of the date and time to `buf`.
    pub fn encode(&self, buf: &mut BytesMut) {
        primitives::encode_app_date(buf, &self.date);
        primitives::encode_app_time(buf, &self.time);
    }

    /// Decode the request from `data`; errors on malformed or truncated input.
    pub fn decode(data: &[u8]) -> Result<Self, Error> {
        let (date, offset) = decode_app_fixed(data, 0, tags::app_tag::DATE, 4, "TimeSync date")?;
        let date = Date::decode(date)?;
        let (time, end) = decode_app_fixed(data, offset, tags::app_tag::TIME, 4, "TimeSync time")?;
        let time = Time::decode(time)?;
        expect_end(data, end, end, "TimeSync")?;

        Ok(Self { date, time })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn encode_dcc(time_duration: Option<u64>, enable_disable: u64) -> BytesMut {
        let mut buf = BytesMut::new();
        if let Some(duration) = time_duration {
            primitives::encode_ctx_unsigned(&mut buf, 0, duration);
        }
        primitives::encode_ctx_unsigned(&mut buf, 1, enable_disable);
        buf
    }

    fn encode_reinitialize(state: u64) -> BytesMut {
        let mut buf = BytesMut::new();
        primitives::encode_ctx_unsigned(&mut buf, 0, state);
        buf
    }

    #[test]
    fn dcc_round_trip() {
        let req = DeviceCommunicationControlRequest {
            time_duration: Some(60),
            enable_disable: EnableDisable::DISABLE,
            password: Some("secret".into()),
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf).unwrap();
        let decoded = DeviceCommunicationControlRequest::decode(&buf).unwrap();
        assert_eq!(req, decoded);
    }

    #[test]
    fn dcc_no_optionals() {
        let req = DeviceCommunicationControlRequest {
            time_duration: None,
            enable_disable: EnableDisable::ENABLE,
            password: None,
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf).unwrap();
        let decoded = DeviceCommunicationControlRequest::decode(&buf).unwrap();
        assert_eq!(req, decoded);
    }

    #[test]
    fn dcc_values_must_fit_field_widths() {
        // The shared readers' wording names the member, its tag and the value.
        for (duration, enable_disable, field, value) in [
            (Some(65_536), 0, "time-duration: [0]", 65_536_u64),
            (None, 4_294_967_296, "enable-disable: [1]", 4_294_967_296),
            (Some(u64::MAX), 0, "time-duration: [0]", u64::MAX),
            (None, u64::MAX, "enable-disable: [1]", u64::MAX),
        ] {
            let encoded = encode_dcc(duration, enable_disable);
            let error = DeviceCommunicationControlRequest::decode(&encoded).unwrap_err();
            assert!(
                error
                    .to_string()
                    .contains(&format!("DCC {field} value {value} exceeds")),
                "unexpected error for {field} {value}: {error}"
            );
        }

        let mut leading_zero = BytesMut::new();
        tags::encode_tag(&mut leading_zero, 0, tags::TagClass::Context, 3);
        leading_zero.extend_from_slice(&[0, 0xff, 0xff]);
        tags::encode_tag(&mut leading_zero, 1, tags::TagClass::Context, 5);
        leading_zero.extend_from_slice(&[0, 0xff, 0xff, 0xff, 0xff]);
        let decoded = DeviceCommunicationControlRequest::decode(&leading_zero).unwrap();
        assert_eq!(decoded.time_duration, Some(u16::MAX));
        assert_eq!(decoded.enable_disable.to_raw(), u32::MAX);
    }

    #[test]
    fn dcc_requires_enable_disable_context_tag() {
        let mut application_tag = BytesMut::new();
        primitives::encode_app_enumerated(&mut application_tag, 0);
        assert!(DeviceCommunicationControlRequest::decode(&application_tag).is_err());

        let mut wrong_context_tag = BytesMut::new();
        primitives::encode_ctx_enumerated(&mut wrong_context_tag, 2, 0);
        assert!(DeviceCommunicationControlRequest::decode(&wrong_context_tag).is_err());
    }

    #[test]
    fn reinitialize_round_trip() {
        let req = ReinitializeDeviceRequest {
            reinitialized_state: ReinitializedState::WARMSTART,
            password: Some("admin".into()),
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf).unwrap();
        let decoded = ReinitializeDeviceRequest::decode(&buf).unwrap();
        assert_eq!(req, decoded);
    }

    #[test]
    fn reinitialize_state_must_fit_u32() {
        for value in [4_294_967_296, 4_294_967_297, u64::MAX] {
            let encoded = encode_reinitialize(value);
            let error = ReinitializeDeviceRequest::decode(&encoded).unwrap_err();
            assert!(
                error.to_string().contains(&format!(
                    "Reinitialize state: [0] value {value} exceeds u32"
                )),
                "unexpected error for state {value}: {error}"
            );
        }

        let mut leading_zero = BytesMut::new();
        tags::encode_tag(&mut leading_zero, 0, tags::TagClass::Context, 5);
        leading_zero.extend_from_slice(&[0, 0xff, 0xff, 0xff, 0xff]);
        let decoded = ReinitializeDeviceRequest::decode(&leading_zero).unwrap();
        assert_eq!(decoded.reinitialized_state.to_raw(), u32::MAX);
    }

    #[test]
    fn reinitialize_requires_state_context_tag() {
        let mut application_tag = BytesMut::new();
        primitives::encode_app_enumerated(&mut application_tag, 0);
        assert!(ReinitializeDeviceRequest::decode(&application_tag).is_err());

        let mut wrong_context_tag = BytesMut::new();
        primitives::encode_ctx_enumerated(&mut wrong_context_tag, 1, 0);
        assert!(ReinitializeDeviceRequest::decode(&wrong_context_tag).is_err());
    }

    #[test]
    fn time_sync_round_trip() {
        let req = TimeSynchronizationRequest {
            date: Date {
                year: 124,
                month: 6,
                day: 15,
                day_of_week: 6,
            },
            time: Time {
                hour: 14,
                minute: 30,
                second: 0,
                hundredths: 0,
            },
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        let decoded = TimeSynchronizationRequest::decode(&buf).unwrap();
        assert_eq!(req, decoded);
    }

    #[test]
    fn time_sync_requires_exact_application_tags_and_no_trailing_data() {
        let mut wrong_date = BytesMut::new();
        primitives::encode_app_time(
            &mut wrong_date,
            &Time {
                hour: 1,
                minute: 2,
                second: 3,
                hundredths: 4,
            },
        );
        primitives::encode_app_time(
            &mut wrong_date,
            &Time {
                hour: 1,
                minute: 2,
                second: 3,
                hundredths: 4,
            },
        );
        assert!(TimeSynchronizationRequest::decode(&wrong_date).is_err());

        let request = TimeSynchronizationRequest {
            date: Date {
                year: 124,
                month: 7,
                day: 4,
                day_of_week: 4,
            },
            time: Time {
                hour: 9,
                minute: 15,
                second: 0,
                hundredths: 0,
            },
        };
        let mut trailing = BytesMut::new();
        request.encode(&mut trailing);
        trailing.extend_from_slice(&[0]);
        assert!(TimeSynchronizationRequest::decode(&trailing).is_err());
    }

    // -----------------------------------------------------------------------
    // Malformed-input decode error tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_decode_dcc_empty_input() {
        assert!(DeviceCommunicationControlRequest::decode(&[]).is_err());
    }

    #[test]
    fn test_decode_dcc_truncated_1_byte() {
        let req = DeviceCommunicationControlRequest {
            time_duration: Some(60),
            enable_disable: EnableDisable::DISABLE,
            password: Some("secret".into()),
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf).unwrap();
        assert!(DeviceCommunicationControlRequest::decode(&buf[..1]).is_err());
    }

    #[test]
    fn test_decode_dcc_truncated_3_bytes() {
        let req = DeviceCommunicationControlRequest {
            time_duration: Some(60),
            enable_disable: EnableDisable::DISABLE,
            password: Some("secret".into()),
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf).unwrap();
        assert!(DeviceCommunicationControlRequest::decode(&buf[..3]).is_err());
    }

    #[test]
    fn test_decode_dcc_invalid_tag() {
        assert!(DeviceCommunicationControlRequest::decode(&[0xFF, 0xFF, 0xFF]).is_err());
    }

    #[test]
    fn test_decode_reinitialize_empty_input() {
        assert!(ReinitializeDeviceRequest::decode(&[]).is_err());
    }

    #[test]
    fn test_decode_reinitialize_truncated_1_byte() {
        let req = ReinitializeDeviceRequest {
            reinitialized_state: ReinitializedState::WARMSTART,
            password: Some("admin".into()),
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf).unwrap();
        assert!(ReinitializeDeviceRequest::decode(&buf[..1]).is_err());
    }

    #[test]
    fn test_decode_reinitialize_invalid_tag() {
        assert!(ReinitializeDeviceRequest::decode(&[0xFF, 0xFF, 0xFF]).is_err());
    }

    #[test]
    fn test_decode_time_sync_empty_input() {
        assert!(TimeSynchronizationRequest::decode(&[]).is_err());
    }

    #[test]
    fn test_decode_time_sync_truncated_1_byte() {
        let req = TimeSynchronizationRequest {
            date: Date {
                year: 124,
                month: 6,
                day: 15,
                day_of_week: 6,
            },
            time: Time {
                hour: 14,
                minute: 30,
                second: 0,
                hundredths: 0,
            },
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        assert!(TimeSynchronizationRequest::decode(&buf[..1]).is_err());
    }

    #[test]
    fn test_decode_time_sync_truncated_3_bytes() {
        let req = TimeSynchronizationRequest {
            date: Date {
                year: 124,
                month: 6,
                day: 15,
                day_of_week: 6,
            },
            time: Time {
                hour: 14,
                minute: 30,
                second: 0,
                hundredths: 0,
            },
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        assert!(TimeSynchronizationRequest::decode(&buf[..3]).is_err());
    }

    #[test]
    fn test_decode_time_sync_truncated_half() {
        let req = TimeSynchronizationRequest {
            date: Date {
                year: 124,
                month: 6,
                day: 15,
                day_of_week: 6,
            },
            time: Time {
                hour: 14,
                minute: 30,
                second: 0,
                hundredths: 0,
            },
        };
        let mut buf = BytesMut::new();
        req.encode(&mut buf);
        let half = buf.len() / 2;
        assert!(TimeSynchronizationRequest::decode(&buf[..half]).is_err());
    }

    #[test]
    fn test_decode_time_sync_invalid_tag() {
        assert!(TimeSynchronizationRequest::decode(&[0xFF, 0xFF, 0xFF]).is_err());
    }

    /// Encode a DCC (enable) and a ReinitializeDevice (coldstart) request whose
    /// password member holds `charset` then `payload`, uninterpreted.
    fn raw_password_requests(charset: u8, payload: &[u8]) -> [BytesMut; 2] {
        let len = u32::try_from(payload.len()).unwrap() + 1;
        let mut dcc = encode_dcc(None, 0);
        tags::encode_tag(&mut dcc, 2, tags::TagClass::Context, len);
        let mut reinit = encode_reinitialize(0);
        tags::encode_tag(&mut reinit, 1, tags::TagClass::Context, len);
        for buf in [&mut dcc, &mut reinit] {
            buf.extend_from_slice(&[charset]);
            buf.extend_from_slice(payload);
        }
        [dcc, reinit]
    }

    /// Decode both requests and return their passwords, or `None` for each
    /// one refused.
    fn decoded_passwords([dcc, reinit]: [BytesMut; 2]) -> [Option<String>; 2] {
        [
            DeviceCommunicationControlRequest::decode(&dcc)
                .ok()
                .and_then(|r| r.password),
            ReinitializeDeviceRequest::decode(&reinit)
                .ok()
                .and_then(|r| r.password),
        ]
    }

    #[test]
    fn password_limit_counts_characters_not_octets() {
        // 20 characters each, 40 to 80 octets in UTF-8 (#2060).
        for ch in ["é", "パ", "😀", "a"] {
            let twenty = ch.repeat(20);
            let dcc = DeviceCommunicationControlRequest {
                time_duration: None,
                enable_disable: EnableDisable::ENABLE,
                password: Some(twenty.clone()),
            };
            let reinit = ReinitializeDeviceRequest {
                reinitialized_state: ReinitializedState::WARMSTART,
                password: Some(twenty),
            };
            let mut buf = BytesMut::new();
            dcc.encode(&mut buf).unwrap();
            assert_eq!(DeviceCommunicationControlRequest::decode(&buf).unwrap(), dcc);
            buf.clear();
            reinit.encode(&mut buf).unwrap();
            assert_eq!(ReinitializeDeviceRequest::decode(&buf).unwrap(), reinit);

            // The encoders refuse 21 characters (#2085), so the decoder
            // sees them as raw UTF-8 (character set 0).
            let twenty_one = ch.repeat(21);
            assert_eq!(
                decoded_passwords(raw_password_requests(0, twenty_one.as_bytes())),
                [None, None]
            );
        }
    }

    #[test]
    fn encoders_refuse_a_password_over_the_limit() {
        for ch in ["é", "パ", "😀", "a"] {
            let dcc = DeviceCommunicationControlRequest {
                time_duration: Some(5),
                enable_disable: EnableDisable::DISABLE,
                password: Some(ch.repeat(21)),
            };
            let reinit = ReinitializeDeviceRequest {
                reinitialized_state: ReinitializedState::COLDSTART,
                password: Some(ch.repeat(21)),
            };
            let mut buf = BytesMut::new();
            let err = dcc.encode(&mut buf).unwrap_err();
            assert!(matches!(err, Error::Encode(_)), "{err:?}");
            assert!(err.to_string().contains("20-character limit"), "{err}");
            assert!(buf.is_empty());
            let err = reinit.encode(&mut buf).unwrap_err();
            assert!(matches!(err, Error::Encode(_)), "{err:?}");
            assert!(err.to_string().contains("20-character limit"), "{err}");
            assert!(buf.is_empty());
        }
    }

    #[test]
    fn password_limit_counts_characters_of_the_sent_character_set() {
        // ISO 8859-1: one octet a character, even above 0x7F (two UTF-8 octets).
        let latin1 = [0xe9; 21];
        let expected = "é".repeat(20);
        assert_eq!(
            decoded_passwords(raw_password_requests(5, &latin1[..20])),
            [Some(expected.clone()), Some(expected)]
        );
        assert_eq!(
            decoded_passwords(raw_password_requests(5, &latin1)),
            [None, None]
        );
        // UCS-2: two octets a character (U+30D1, three UTF-8 octets).
        let ucs2 = [0x30, 0xd1].repeat(21);
        let expected = "\u{30d1}".repeat(20);
        assert_eq!(
            decoded_passwords(raw_password_requests(4, &ucs2[..40])),
            [Some(expected.clone()), Some(expected)]
        );
        assert_eq!(decoded_passwords(raw_password_requests(4, &ucs2)), [None, None]);
    }

    /// #2094: the encoders refuse an empty password, telling the caller to
    /// omit it, and leave the buffer untouched.
    #[test]
    fn encoders_refuse_an_empty_password() {
        let dcc = DeviceCommunicationControlRequest {
            time_duration: None,
            enable_disable: EnableDisable::ENABLE,
            password: Some(String::new()),
        };
        let reinit = ReinitializeDeviceRequest {
            reinitialized_state: ReinitializedState::WARMSTART,
            password: Some(String::new()),
        };
        let mut buf = BytesMut::new();
        for err in [
            dcc.encode(&mut buf).unwrap_err(),
            reinit.encode(&mut buf).unwrap_err(),
        ] {
            assert!(matches!(err, Error::Encode(_)), "{err:?}");
            assert!(err.to_string().contains("omit it"), "{err}");
        }
        assert!(buf.is_empty());
    }

    /// #2094: an empty password from a peer decodes as no password, in every
    /// character set, so a server checks the request as one without it.
    #[test]
    fn decoders_take_an_empty_password_as_absent() {
        for charset in [0, 4, 5] {
            let [dcc, reinit] = raw_password_requests(charset, &[]);
            let dcc = DeviceCommunicationControlRequest::decode(&dcc).unwrap();
            assert_eq!(dcc.password, None);
            assert_eq!(dcc.enable_disable, EnableDisable::ENABLE);
            let reinit = ReinitializeDeviceRequest::decode(&reinit).unwrap();
            assert_eq!(reinit.password, None);
            assert_eq!(reinit.reinitialized_state, ReinitializedState::COLDSTART);
        }
        // One character is still a password.
        assert_eq!(
            decoded_passwords(raw_password_requests(0, b"x")),
            [Some("x".into()), Some("x".into())]
        );
    }
}