sccp-protocol 0.3.4

Cisco SCCP (Skinny Client Control Protocol) codec/server
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
//! Typed payloads returned by phone-hosted service applications.
//!
//! The SCCP application envelope carries routing identifiers separately from
//! its payload. Execute responses use documented XML schemas, while interactive
//! input and menu callbacks use a relative route followed by a standard URL
//! query string.

use std::fmt;

use percent_encoding::percent_decode_str;
use serde::{Deserialize, Serialize};
use thiserror::Error;

use super::xml::{self as phone_xml, PhoneXmlError};
use crate::types::{ApplicationId, CallReference, LineInstance, TransactionId};

/// Maximum application payload accepted from an envelope, in bytes.
pub const MAX_PHONE_SERVICE_DATA_BYTES: usize = 2_000;
/// Maximum number of result items in an execute response.
pub const MAX_PHONE_SERVICE_RESPONSE_ITEMS: usize = 3;
/// Maximum number of decoded components in a submission route.
pub const MAX_PHONE_SERVICE_ROUTE_SEGMENTS: usize = 32;
/// Maximum UTF-8 byte length of one decoded route component.
pub const MAX_PHONE_SERVICE_ROUTE_COMPONENT_BYTES: usize = 1_024;
/// Maximum number of ordered name/value pairs retained from a submission.
pub const MAX_PHONE_SERVICE_SUBMITTED_VALUES: usize = 32;
/// Maximum UTF-8 byte length of a decoded submission parameter name.
pub const MAX_PHONE_SERVICE_PARAMETER_NAME_BYTES: usize = 128;
/// Maximum UTF-8 byte length of a decoded submission parameter value.
pub const MAX_PHONE_SERVICE_PARAMETER_VALUE_BYTES: usize = 1_024;
/// Maximum character count of the data field in an execute result item.
pub const MAX_PHONE_SERVICE_RESPONSE_DATA_CHARS: usize = 256;
/// Maximum character count of the URL field in an execute result item.
pub const MAX_PHONE_SERVICE_RESPONSE_URL_CHARS: usize = 256;
/// Maximum character count of a structured application error message.
pub const MAX_PHONE_SERVICE_ERROR_MESSAGE_CHARS: usize = 256;

/// Envelope direction used to choose the permitted payload grammar.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PhoneServiceMessageKind {
    /// An application-data envelope, which may contain an interactive submission.
    Data,
    /// A response envelope, which accepts only structured responses or opaque data.
    Response,
}

/// Identifiers copied from the SCCP application envelope rather than inferred
/// from the submitted payload.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PhoneServiceRouting {
    pub application_id: ApplicationId,
    pub line_instance: LineInstance,
    pub call_reference: CallReference,
    pub transaction_id: TransactionId,
}

/// Additional selectors carried only by the extended application envelope.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct PhoneServiceExtendedRouting {
    /// Sender-defined continuation marker retained without interpretation.
    pub sequence_flag: u32,
    /// Sender-defined display ordering hint retained without interpretation.
    pub display_priority: u32,
    /// Conference association from the extended envelope, or zero when absent.
    pub conference_id: u32,
    /// Instance discriminator for applications with concurrent executions.
    pub application_instance_id: u32,
    /// Sender-defined routing selector retained without interpretation.
    pub routing: u32,
}

/// One decoded application envelope and its typed or preserved payload.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PhoneServiceEvent {
    /// Determines which payload grammar was accepted.
    pub kind: PhoneServiceMessageKind,
    pub routing: PhoneServiceRouting,
    /// Extended fields when the envelope used the extended message form.
    pub extended: Option<PhoneServiceExtendedRouting>,
    /// Decoded payload or exact bounded bytes for an unsupported payload.
    pub payload: PhoneServicePayload,
}

/// Supported application payloads after envelope decoding.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PhoneServicePayload {
    ExecuteResponse(CiscoIpPhoneResponse),
    Error(CiscoIpPhoneError),
    Submission(PhoneServiceSubmission),
    /// A syntactically valid but unsupported XML schema, a non-XML response,
    /// or binary application data. The protocol boundary already limits it.
    Opaque(Vec<u8>),
}

/// A bounded collection of results for previously requested execute actions.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename = "CiscoIPPhoneResponse")]
pub struct CiscoIpPhoneResponse {
    #[serde(rename = "ResponseItem", default)]
    pub items: Vec<CiscoIpPhoneResponseItem>,
}

impl CiscoIpPhoneResponse {
    /// Enforces the response item count and text bounds before serialization.
    pub fn validate(&self) -> Result<(), PhoneXmlError> {
        if self.items.len() > MAX_PHONE_SERVICE_RESPONSE_ITEMS {
            return Err(PhoneXmlError::LimitExceeded {
                kind: "phone-service response items",
                actual: self.items.len(),
                maximum: MAX_PHONE_SERVICE_RESPONSE_ITEMS,
            });
        }
        for item in &self.items {
            validate_response_text(
                "phone-service response data",
                &item.data,
                MAX_PHONE_SERVICE_RESPONSE_DATA_CHARS,
            )?;
            validate_response_text(
                "phone-service response URL",
                &item.url,
                MAX_PHONE_SERVICE_RESPONSE_URL_CHARS,
            )?;
        }
        Ok(())
    }
}

/// Extensible execute result code that preserves unknown numeric values.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct PhoneExecuteStatus(pub u32);

impl PhoneExecuteStatus {
    pub const OK: Self = Self(0);
    pub const ERROR: Self = Self(1);
    pub const URI_NOT_FOUND: Self = Self(4);
    pub const NO_ACTIVE_CALL: Self = Self(6);

    pub const fn get(self) -> u32 {
        self.0
    }
}

/// Result metadata for one requested execute action.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct CiscoIpPhoneResponseItem {
    #[serde(rename = "@Status", alias = "Status")]
    pub status: PhoneExecuteStatus,
    #[serde(rename = "@Data", alias = "Data")]
    /// Result text constrained to [`MAX_PHONE_SERVICE_RESPONSE_DATA_CHARS`] characters.
    pub data: String,
    #[serde(rename = "@URL", alias = "URL")]
    /// Result URL constrained to [`MAX_PHONE_SERVICE_RESPONSE_URL_CHARS`] characters.
    pub url: String,
}

/// Extensible structured-error number that preserves unknown numeric values.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct PhoneServiceErrorCode(pub u32);

impl PhoneServiceErrorCode {
    pub const PARSING: Self = Self(1);
    pub const FRAMING: Self = Self(2);
    pub const INTERNAL_FILE: Self = Self(3);
    pub const AUTHENTICATION: Self = Self(4);

    pub const fn get(self) -> u32 {
        self.0
    }
}

/// Structured application error suitable for XML serialization.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename = "CiscoIPPhoneError")]
pub struct CiscoIpPhoneError {
    #[serde(rename = "@Number", alias = "Number")]
    pub number: PhoneServiceErrorCode,
    #[serde(rename = "$text", default, skip_serializing_if = "String::is_empty")]
    pub message: String,
}

impl CiscoIpPhoneError {
    /// Enforces the error-message character bound before serialization.
    pub fn validate(&self) -> Result<(), PhoneXmlError> {
        validate_response_text(
            "phone-service error message",
            &self.message,
            MAX_PHONE_SERVICE_ERROR_MESSAGE_CHARS,
        )
    }
}

/// Percent-decoded interactive callback submitted by a phone application.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PhoneServiceSubmission {
    /// Percent-decoded route components in their original order.
    pub route: Vec<String>,
    /// Ordered form values. Duplicate names remain distinct.
    pub values: Vec<PhoneServiceSubmittedValue>,
}

impl PhoneServiceSubmission {
    /// Iterates matching values in submission order, including duplicates.
    pub fn values_named<'a>(&'a self, name: &'a str) -> impl Iterator<Item = &'a str> + 'a {
        self.values
            .iter()
            .filter(move |value| value.name == name)
            .map(|value| value.value.as_str())
    }
}

/// One submitted name/value pair whose value is redacted from diagnostics.
#[derive(Clone, Eq, PartialEq)]
pub struct PhoneServiceSubmittedValue {
    pub name: String,
    /// Percent-decoded value redacted from [`Debug`](std::fmt::Debug) output.
    pub value: String,
}

impl fmt::Debug for PhoneServiceSubmittedValue {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PhoneServiceSubmittedValue")
            .field("name", &self.name)
            .field("value", &"<redacted>")
            .finish()
    }
}

/// Failures while validating or decoding an application payload.
#[derive(Debug, Error)]
pub enum PhoneServiceError {
    /// A structured XML payload was malformed or violated its schema bounds.
    #[error(transparent)]
    Xml(#[from] PhoneXmlError),
    #[error("phone-service submission is not valid UTF-8")]
    InvalidUtf8,
    #[error("phone-service submission contains invalid percent encoding")]
    InvalidPercentEncoding,
    #[error("phone-service submission contains a forbidden control character")]
    ControlCharacter,
    /// The route exceeded [`MAX_PHONE_SERVICE_ROUTE_SEGMENTS`].
    #[error("phone-service submission route has {actual} components; maximum is {maximum}")]
    TooManyRouteSegments { actual: usize, maximum: usize },
    /// The query exceeded [`MAX_PHONE_SERVICE_SUBMITTED_VALUES`].
    #[error("phone-service submission has {actual} values; maximum is {maximum}")]
    TooManyValues { actual: usize, maximum: usize },
    #[error("phone-service submission has an empty parameter name")]
    EmptyParameterName,
    #[error("phone-service submission has an empty route component")]
    EmptyRouteComponent,
    /// A decoded route, name, or value exceeded its corresponding byte limit.
    #[error("phone-service submission {kind} has {actual} bytes; maximum is {maximum}")]
    ComponentTooLong {
        kind: &'static str,
        actual: usize,
        maximum: usize,
    },
}

#[derive(Deserialize)]
enum PhoneServiceXmlSchema {
    #[serde(rename = "CiscoIPPhoneResponse")]
    Response {
        #[serde(rename = "ResponseItem", default)]
        items: Vec<CiscoIpPhoneResponseItem>,
    },
    #[serde(rename = "CiscoIPPhoneError")]
    Error {
        #[serde(rename = "@Number", alias = "Number")]
        number: PhoneServiceErrorCode,
        #[serde(rename = "$text", default)]
        message: String,
    },
    #[serde(other)]
    Unknown,
}

/// Parse application data according to its message kind. Response envelopes
/// accept the two documented XML schemas; data envelopes additionally accept
/// interactive route/query submissions.
pub fn parse_phone_service_payload(
    data: &[u8],
    kind: PhoneServiceMessageKind,
) -> Result<PhoneServicePayload, PhoneServiceError> {
    if data.len() > MAX_PHONE_SERVICE_DATA_BYTES {
        return Err(PhoneXmlError::LimitExceeded {
            kind: "phone-service data",
            actual: data.len(),
            maximum: MAX_PHONE_SERVICE_DATA_BYTES,
        }
        .into());
    }
    let trimmed = trim_protocol_padding(data);
    if trimmed.is_empty() {
        return Ok(PhoneServicePayload::Opaque(data.to_vec()));
    }

    if trimmed.first() == Some(&b'<') {
        return match phone_xml::from_bytes::<PhoneServiceXmlSchema>(
            trimmed,
            MAX_PHONE_SERVICE_DATA_BYTES,
        )? {
            PhoneServiceXmlSchema::Response { items } => {
                let response = CiscoIpPhoneResponse { items };
                response.validate()?;
                Ok(PhoneServicePayload::ExecuteResponse(response))
            }
            PhoneServiceXmlSchema::Error { number, message } => {
                let error = CiscoIpPhoneError { number, message };
                error.validate()?;
                Ok(PhoneServicePayload::Error(error))
            }
            PhoneServiceXmlSchema::Unknown => Ok(PhoneServicePayload::Opaque(data.to_vec())),
        };
    }

    if kind == PhoneServiceMessageKind::Response {
        return Ok(PhoneServicePayload::Opaque(data.to_vec()));
    }
    let Ok(text) = std::str::from_utf8(trimmed) else {
        return Ok(PhoneServicePayload::Opaque(data.to_vec()));
    };
    parse_submission(text).map(PhoneServicePayload::Submission)
}

fn validate_response_text(
    field: &'static str,
    value: &str,
    maximum: usize,
) -> Result<(), PhoneXmlError> {
    if value.chars().count() > maximum {
        Err(PhoneXmlError::InvalidField {
            field,
            expected: "within the documented phone-service text bound",
        })
    } else {
        Ok(())
    }
}

fn parse_submission(text: &str) -> Result<PhoneServiceSubmission, PhoneServiceError> {
    if text.chars().any(char::is_control) || text.contains('#') {
        return Err(PhoneServiceError::ControlCharacter);
    }
    let (route, query) = text
        .split_once('?')
        .map_or((text, None), |(route, query)| (route, Some(query)));
    let route = if route.is_empty() {
        Vec::new()
    } else {
        route
            .split('/')
            .map(|component| {
                decode_component(
                    component,
                    "route component",
                    MAX_PHONE_SERVICE_ROUTE_COMPONENT_BYTES,
                )
            })
            .collect::<Result<Vec<_>, _>>()?
    };
    if route.len() > MAX_PHONE_SERVICE_ROUTE_SEGMENTS {
        return Err(PhoneServiceError::TooManyRouteSegments {
            actual: route.len(),
            maximum: MAX_PHONE_SERVICE_ROUTE_SEGMENTS,
        });
    }
    let values = query.map_or_else(|| Ok(Vec::new()), parse_form_values)?;
    Ok(PhoneServiceSubmission { route, values })
}

fn parse_form_values(query: &str) -> Result<Vec<PhoneServiceSubmittedValue>, PhoneServiceError> {
    if query.is_empty() {
        return Ok(Vec::new());
    }
    for field in query.split('&') {
        if field.is_empty() {
            return Err(PhoneServiceError::EmptyParameterName);
        }
        let (name, value) = field.split_once('=').unwrap_or((field, ""));
        validate_encoded_component(name)?;
        validate_encoded_component(value)?;
        percent_decode_str(name)
            .decode_utf8()
            .map_err(|_| PhoneServiceError::InvalidUtf8)?;
        percent_decode_str(value)
            .decode_utf8()
            .map_err(|_| PhoneServiceError::InvalidUtf8)?;
    }

    let values = form_urlencoded::parse(query.as_bytes())
        .map(|(name, value)| {
            if name.is_empty() {
                return Err(PhoneServiceError::EmptyParameterName);
            }
            validate_component_length(
                "parameter name",
                &name,
                MAX_PHONE_SERVICE_PARAMETER_NAME_BYTES,
            )?;
            validate_component_length(
                "parameter value",
                &value,
                MAX_PHONE_SERVICE_PARAMETER_VALUE_BYTES,
            )?;
            reject_decoded_controls(&name)?;
            reject_decoded_controls(&value)?;
            Ok(PhoneServiceSubmittedValue {
                name: name.into_owned(),
                value: value.into_owned(),
            })
        })
        .collect::<Result<Vec<_>, _>>()?;
    if values.len() > MAX_PHONE_SERVICE_SUBMITTED_VALUES {
        return Err(PhoneServiceError::TooManyValues {
            actual: values.len(),
            maximum: MAX_PHONE_SERVICE_SUBMITTED_VALUES,
        });
    }
    Ok(values)
}

fn decode_component(
    encoded: &str,
    kind: &'static str,
    maximum: usize,
) -> Result<String, PhoneServiceError> {
    if encoded.is_empty() {
        return Err(PhoneServiceError::EmptyRouteComponent);
    }
    validate_encoded_component(encoded)?;
    let decoded = percent_decode_str(encoded)
        .decode_utf8()
        .map_err(|_| PhoneServiceError::InvalidUtf8)?;
    validate_component_length(kind, &decoded, maximum)?;
    reject_decoded_controls(&decoded)?;
    Ok(decoded.into_owned())
}

fn validate_encoded_component(encoded: &str) -> Result<(), PhoneServiceError> {
    let bytes = encoded.as_bytes();
    let mut index = 0;
    while index < bytes.len() {
        if bytes[index] == b'%' {
            let Some(pair) = bytes.get(index + 1..index + 3) else {
                return Err(PhoneServiceError::InvalidPercentEncoding);
            };
            if !pair.iter().all(u8::is_ascii_hexdigit) {
                return Err(PhoneServiceError::InvalidPercentEncoding);
            }
            index += 3;
        } else {
            index += 1;
        }
    }
    Ok(())
}

fn validate_component_length(
    kind: &'static str,
    component: &str,
    maximum: usize,
) -> Result<(), PhoneServiceError> {
    if component.len() > maximum {
        return Err(PhoneServiceError::ComponentTooLong {
            kind,
            actual: component.len(),
            maximum,
        });
    }
    Ok(())
}

fn reject_decoded_controls(component: &str) -> Result<(), PhoneServiceError> {
    if component.chars().any(char::is_control) {
        Err(PhoneServiceError::ControlCharacter)
    } else {
        Ok(())
    }
}

fn trim_protocol_padding(mut data: &[u8]) -> &[u8] {
    while data.first().is_some_and(|byte| byte.is_ascii_whitespace()) {
        data = &data[1..];
    }
    while data
        .last()
        .is_some_and(|byte| *byte == 0 || byte.is_ascii_whitespace())
    {
        data = &data[..data.len() - 1];
    }
    data
}

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

    #[test]
    fn typed_execute_responses_and_errors_use_the_xml_boundary() {
        let payload = parse_phone_service_payload(
            br#"<CiscoIPPhoneResponse><ResponseItem Status="0" Data="Taylor &amp; Co" URL="Play:chime.raw"/><ResponseItem Status="6" Data="No Active Call" URL="SendDigits:12"/></CiscoIPPhoneResponse>"#,
            PhoneServiceMessageKind::Response,
        )
        .unwrap();
        let PhoneServicePayload::ExecuteResponse(response) = payload else {
            panic!("expected typed execute response");
        };
        assert_eq!(response.items.len(), 2);
        assert_eq!(response.items[0].status, PhoneExecuteStatus::OK);
        assert_eq!(response.items[0].data, "Taylor & Co");
        assert_eq!(response.items[1].status, PhoneExecuteStatus::NO_ACTIVE_CALL);

        assert_eq!(
            parse_phone_service_payload(
                br#"<CiscoIPPhoneError Number="4">Authentication failed</CiscoIPPhoneError>"#,
                PhoneServiceMessageKind::Response,
            )
            .unwrap(),
            PhoneServicePayload::Error(CiscoIpPhoneError {
                number: PhoneServiceErrorCode::AUTHENTICATION,
                message: "Authentication failed".into(),
            })
        );
    }

    #[test]
    fn submitted_route_and_form_values_are_decoded_in_order() {
        let payload = parse_phone_service_payload(
            b"invite/desk%20one?NUMBER=555%2A12&NAME=Fran%C3%A7ois&NOTE=a+b&NOTE=second",
            PhoneServiceMessageKind::Data,
        )
        .unwrap();
        let PhoneServicePayload::Submission(submission) = payload else {
            panic!("expected typed submission");
        };
        assert_eq!(submission.route, ["invite", "desk one"]);
        assert_eq!(
            submission
                .values
                .iter()
                .map(|value| (value.name.as_str(), value.value.as_str()))
                .collect::<Vec<_>>(),
            [
                ("NUMBER", "555*12"),
                ("NAME", "François"),
                ("NOTE", "a b"),
                ("NOTE", "second"),
            ]
        );
        assert_eq!(
            submission.values_named("NOTE").collect::<Vec<_>>(),
            ["a b", "second"]
        );
        let debug = format!("{:?}", submission.values[0]);
        assert!(debug.contains("<redacted>"));
        assert!(!debug.contains("555"));
    }

    #[test]
    fn opaque_payloads_are_reserved_for_unknown_schemas() {
        let unknown = b"<VendorPhoneResult><Value>one</Value></VendorPhoneResult>";
        assert_eq!(
            parse_phone_service_payload(unknown, PhoneServiceMessageKind::Response).unwrap(),
            PhoneServicePayload::Opaque(unknown.to_vec())
        );
        assert_eq!(
            parse_phone_service_payload(b"Success", PhoneServiceMessageKind::Response).unwrap(),
            PhoneServicePayload::Opaque(b"Success".to_vec())
        );
        assert_eq!(
            parse_phone_service_payload(&[0xff, 0x00], PhoneServiceMessageKind::Data).unwrap(),
            PhoneServicePayload::Opaque(vec![0xff, 0x00])
        );
    }

    #[test]
    fn malformed_xml_encoding_and_bounds_fail_closed_without_values_in_errors() {
        for malformed in [
            b"<CiscoIPPhoneResponse>".as_slice(),
            b"<!DOCTYPE x><CiscoIPPhoneResponse/>".as_slice(),
            b"<VendorPhoneResult>".as_slice(),
        ] {
            assert!(
                parse_phone_service_payload(malformed, PhoneServiceMessageKind::Response).is_err()
            );
        }

        for malformed in [
            "invite?PIN=secret%",
            "invite?PIN=secret%GG",
            "invite?PIN=%FF",
            "invite?PIN=secret%0Avalue",
            "invite?=secret",
            "invite//desk?PIN=secret",
            "invite?PIN=secret#fragment",
        ] {
            let error =
                parse_phone_service_payload(malformed.as_bytes(), PhoneServiceMessageKind::Data)
                    .unwrap_err()
                    .to_string();
            assert!(!error.contains("secret"), "{error}");
        }

        assert!(
            parse_phone_service_payload(
                &vec![b'x'; MAX_PHONE_SERVICE_DATA_BYTES + 1],
                PhoneServiceMessageKind::Data,
            )
            .is_err()
        );
        let too_many = format!(
            "route?{}",
            (0..=MAX_PHONE_SERVICE_SUBMITTED_VALUES)
                .map(|index| format!("p{index}=x"))
                .collect::<Vec<_>>()
                .join("&")
        );
        assert!(
            parse_phone_service_payload(too_many.as_bytes(), PhoneServiceMessageKind::Data,)
                .is_err()
        );
        let too_many_items = format!(
            "<CiscoIPPhoneResponse>{}</CiscoIPPhoneResponse>",
            r#"<ResponseItem Status="0" Data="ok" URL="Init:Services"/>"#
                .repeat(MAX_PHONE_SERVICE_RESPONSE_ITEMS + 1)
        );
        assert!(
            parse_phone_service_payload(
                too_many_items.as_bytes(),
                PhoneServiceMessageKind::Response,
            )
            .is_err()
        );
    }

    #[test]
    fn every_submission_collection_and_component_bound_is_enforced() {
        let too_many_route_segments =
            std::iter::repeat_n("x", MAX_PHONE_SERVICE_ROUTE_SEGMENTS + 1)
                .collect::<Vec<_>>()
                .join("/");
        assert!(matches!(
            parse_phone_service_payload(
                too_many_route_segments.as_bytes(),
                PhoneServiceMessageKind::Data,
            ),
            Err(PhoneServiceError::TooManyRouteSegments { .. })
        ));

        let long_route = "x".repeat(MAX_PHONE_SERVICE_ROUTE_COMPONENT_BYTES + 1);
        assert!(matches!(
            parse_phone_service_payload(long_route.as_bytes(), PhoneServiceMessageKind::Data),
            Err(PhoneServiceError::ComponentTooLong {
                kind: "route component",
                ..
            })
        ));

        let long_name = format!(
            "route?{}=value",
            "n".repeat(MAX_PHONE_SERVICE_PARAMETER_NAME_BYTES + 1)
        );
        assert!(matches!(
            parse_phone_service_payload(long_name.as_bytes(), PhoneServiceMessageKind::Data),
            Err(PhoneServiceError::ComponentTooLong {
                kind: "parameter name",
                ..
            })
        ));

        let long_value = format!(
            "route?name={}",
            "v".repeat(MAX_PHONE_SERVICE_PARAMETER_VALUE_BYTES + 1)
        );
        assert!(matches!(
            parse_phone_service_payload(long_value.as_bytes(), PhoneServiceMessageKind::Data),
            Err(PhoneServiceError::ComponentTooLong {
                kind: "parameter value",
                ..
            })
        ));
    }
}