rs-matter/matter/tests/data_model/timed_requests.rs

147 lines
3.8 KiB
Rust
Raw Normal View History

/*
*
* Copyright (c) 2023 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use matter::{
data_model::objects::EncodeValue,
interaction_model::{
core::IMStatusCode,
messages::ib::{AttrData, AttrPath, AttrStatus},
messages::{ib::CmdData, ib::CmdPath, GenericPath},
},
tlv::TLVWriter,
};
2023-01-07 13:31:28 +05:30
use crate::{
common::{
commands::*,
echo_cluster,
handlers::{TimedInvResponse, WriteResponse},
2023-06-10 14:01:35 +00:00
im_engine::ImEngine,
2023-05-04 05:26:13 +00:00
init_env_logger,
2023-01-07 13:31:28 +05:30
},
echo_req, echo_resp,
};
#[test]
fn test_timed_write_fail_and_success() {
// - 1 Timed Attr Write Transaction should fail due to timeout
// - 1 Timed Attr Write Transaction should succeed
let val0 = 10;
2023-05-04 05:26:13 +00:00
init_env_logger();
let attr_data0 = |tag, t: &mut TLVWriter| {
let _ = t.u16(tag, val0);
};
let ep_att = GenericPath::new(
None,
Some(echo_cluster::ID),
Some(echo_cluster::AttributesDiscriminants::AttWrite as u32),
);
let input = &[AttrData::new(
None,
AttrPath::new(&ep_att),
EncodeValue::Closure(&attr_data0),
)];
let ep0_att = GenericPath::new(
Some(0),
Some(echo_cluster::ID),
Some(echo_cluster::AttributesDiscriminants::AttWrite as u32),
);
let ep1_att = GenericPath::new(
Some(1),
Some(echo_cluster::ID),
Some(echo_cluster::AttributesDiscriminants::AttWrite as u32),
);
let expected = &[
2023-02-27 16:55:38 +01:00
AttrStatus::new(&ep0_att, IMStatusCode::Success, 0),
AttrStatus::new(&ep1_att, IMStatusCode::Success, 0),
];
// Test with incorrect handling
2023-06-10 14:01:35 +00:00
ImEngine::timed_write_reqs(input, &WriteResponse::TransactionError, 100, 500);
// Test with correct handling
2023-06-10 14:01:35 +00:00
let im = ImEngine::new_default();
let handler = im.handler();
im.add_default_acl();
im.handle_timed_write_reqs(
&handler,
input,
&WriteResponse::TransactionSuccess(expected),
400,
0,
);
2023-06-10 14:01:35 +00:00
assert_eq!(val0, handler.echo_cluster(0).att_write.get());
2023-01-07 13:31:28 +05:30
}
#[test]
fn test_timed_cmd_success() {
// A timed request that works
2023-05-04 05:26:13 +00:00
init_env_logger();
2023-01-07 13:31:28 +05:30
let input = &[echo_req!(0, 5), echo_req!(1, 10)];
let expected = &[echo_resp!(0, 10), echo_resp!(1, 30)];
2023-06-10 14:01:35 +00:00
ImEngine::timed_commands(
2023-01-07 13:31:28 +05:30
input,
2023-01-10 08:53:04 +01:00
&TimedInvResponse::TransactionSuccess(expected),
2023-01-07 13:31:28 +05:30
400,
0,
true,
);
}
#[test]
fn test_timed_cmd_timeout() {
// A timed request that is executed after t imeout
2023-05-04 05:26:13 +00:00
init_env_logger();
2023-01-07 13:31:28 +05:30
let input = &[echo_req!(0, 5), echo_req!(1, 10)];
2023-06-10 14:01:35 +00:00
ImEngine::timed_commands(
2023-01-07 13:31:28 +05:30
input,
2023-01-10 08:53:04 +01:00
&TimedInvResponse::TransactionError(IMStatusCode::Timeout),
2023-06-10 14:01:35 +00:00
100,
2023-01-07 13:31:28 +05:30
500,
true,
);
}
#[test]
fn test_timed_cmd_timedout_mismatch() {
// A timed request with timeout mismatch
2023-05-04 05:26:13 +00:00
init_env_logger();
2023-01-07 13:31:28 +05:30
let input = &[echo_req!(0, 5), echo_req!(1, 10)];
2023-06-10 14:01:35 +00:00
ImEngine::timed_commands(
2023-01-07 13:31:28 +05:30
input,
2023-01-10 08:53:04 +01:00
&TimedInvResponse::TransactionError(IMStatusCode::TimedRequestMisMatch),
2023-01-07 13:31:28 +05:30
400,
0,
false,
);
let input = &[echo_req!(0, 5), echo_req!(1, 10)];
2023-06-10 14:01:35 +00:00
ImEngine::timed_commands(
2023-01-07 13:31:28 +05:30
input,
2023-01-10 08:53:04 +01:00
&TimedInvResponse::TransactionError(IMStatusCode::TimedRequestMisMatch),
2023-01-07 13:31:28 +05:30
0,
0,
true,
);
}