#![cfg(all(feature = "std", feature = "rustcrypto"))]
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::indexing_slicing,
clippy::panic,
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
clippy::cast_sign_loss
)]
use matter_kit::msg::SessionId;
use matter_kit::sc::{
PASSCODE_ID_COMMISSIONING, PbkdfParamRequest, PbkdfParamResponse, RANDOM_LEN, SessionParams,
};
use matter_kit::tlv::{Tag, TlvReader};
fn refuse_empty_optionals(what: &str, encoded: &[u8]) {
match TlvReader::first_empty_optional(encoded).expect("the encoder produced well-formed TLV") {
None => {}
Some(Tag::Context(n)) => panic!(
"{what} wrote an empty optional structure under context tag {n}. Appendix A permits \
it, this crate's decoder accepts it, and every released CHIP SDK refuses it — so \
the field is omitted instead, or the exception is documented where §4.14.1.2's is."
),
Some(other) => panic!("{what} wrote an empty container under {other:?}"),
}
}
fn params() -> SessionParams {
SessionParams::default()
}
#[test]
fn a_pbkdf_param_request_never_carries_an_empty_optional() {
let mut buf = [0u8; 512];
for (what, session_params) in [
("PBKDFParamRequest with session parameters", Some(params())),
("PBKDFParamRequest with none", None),
] {
let req = PbkdfParamRequest {
initiator_random: [0x11; RANDOM_LEN],
initiator_session_id: SessionId(0x1234),
passcode_id: PASSCODE_ID_COMMISSIONING,
has_pbkdf_parameters: false,
session_params,
};
let len = req.encode(&mut buf).expect("encode");
refuse_empty_optionals(what, &buf[..len]);
}
}
#[test]
fn the_pbkdf_parameters_exception_is_still_an_exception() {
let mut buf = [0u8; 512];
let response = PbkdfParamResponse {
initiator_random: [0x11; RANDOM_LEN],
responder_random: [0x22; RANDOM_LEN],
responder_session_id: SessionId(0x5678),
pbkdf_parameters: None,
session_params: Some(params()),
};
let len = response.encode(&mut buf).expect("encode");
assert_eq!(
TlvReader::first_empty_optional(&buf[..len]).expect("well-formed"),
Some(Tag::Context(4)),
"§4.14.1.2's empty parameter set is the exception, and it has to still be there"
);
}
#[test]
fn no_interaction_model_message_carries_an_empty_optional() {
use matter_kit::im::{
AttributePath, AttributeStatus, CommandData, encode_invoke_request, encode_read_request,
encode_subscribe_request, encode_write_response,
};
let mut buf = [0u8; 1024];
let encoded = encode_read_request(&mut buf, [AttributePath::wildcard()], [], false)
.expect("read request");
refuse_empty_optionals("ReadRequest over a wildcard", encoded);
let encoded = encode_subscribe_request(
&mut buf,
[AttributePath::wildcard()],
[],
0,
60,
false,
false,
)
.expect("subscribe request");
refuse_empty_optionals("SubscribeRequest", encoded);
let encoded = encode_write_response(&mut buf, core::iter::empty::<AttributeStatus>())
.expect("empty write response");
refuse_empty_optionals("WriteResponse with no statuses", encoded);
let encoded = encode_invoke_request(
&mut buf,
core::iter::empty::<CommandData<'_>>(),
false,
false,
)
.expect("invoke with no commands");
refuse_empty_optionals("InvokeRequest with no commands", encoded);
}
#[test]
fn the_check_finds_a_deliberate_offender() {
use matter_kit::tlv::{ContainerKind, TlvWriter};
let mut buf = [0u8; 64];
let mut w = TlvWriter::new(&mut buf);
w.start(Tag::Anonymous, ContainerKind::Structure).unwrap();
w.unsigned(Tag::Context(0), 1).unwrap();
w.start(Tag::Context(5), ContainerKind::Structure).unwrap();
w.end_container().unwrap();
w.end_container().unwrap();
let encoded = w.finish().unwrap();
assert_eq!(
TlvReader::first_empty_optional(encoded).unwrap(),
Some(Tag::Context(5))
);
let mut buf = [0u8; 64];
let mut w = TlvWriter::new(&mut buf);
w.start(Tag::Anonymous, ContainerKind::Array).unwrap();
w.end_container().unwrap();
let encoded = w.finish().unwrap();
assert_eq!(TlvReader::first_empty_optional(encoded).unwrap(), None);
let mut buf = [0u8; 64];
let mut w = TlvWriter::new(&mut buf);
w.start(Tag::Anonymous, ContainerKind::Structure).unwrap();
w.start(Tag::Context(3), ContainerKind::Structure).unwrap();
w.unsigned(Tag::Context(0), 7).unwrap();
w.end_container().unwrap();
w.end_container().unwrap();
let encoded = w.finish().unwrap();
assert_eq!(TlvReader::first_empty_optional(encoded).unwrap(), None);
let mut buf = [0u8; 64];
let mut w = TlvWriter::new(&mut buf);
w.start(Tag::Anonymous, ContainerKind::Structure).unwrap();
w.start(Tag::Context(0), ContainerKind::Array).unwrap();
w.end_container().unwrap();
w.end_container().unwrap();
let encoded = w.finish().unwrap();
assert_eq!(TlvReader::first_empty_optional(encoded).unwrap(), None);
}