Skip to content

Commit 91b451d

Browse files
committed
tests: Add tests for timed invoke
1 parent 4a041e1 commit 91b451d

File tree

4 files changed

+257
-98
lines changed

4 files changed

+257
-98
lines changed

matter/tests/common/commands.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
*
3+
* Copyright (c) 2023 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
use matter::{
19+
data_model::objects::EncodeValue,
20+
interaction_model::{
21+
messages::ib::{CmdPath, CmdStatus, InvResp},
22+
messages::msg,
23+
},
24+
};
25+
26+
pub enum ExpectedInvResp {
27+
Cmd(CmdPath, u8),
28+
Status(CmdStatus),
29+
}
30+
31+
pub fn assert_inv_response(resp: &msg::InvResp, expected: &[ExpectedInvResp]) {
32+
let mut index = 0;
33+
for inv_response in resp.inv_responses.unwrap().iter() {
34+
println!("Validating index {}", index);
35+
match expected[index] {
36+
ExpectedInvResp::Cmd(e_c, e_d) => match inv_response {
37+
InvResp::Cmd(c) => {
38+
assert_eq!(e_c, c.path);
39+
match c.data {
40+
EncodeValue::Tlv(t) => {
41+
assert_eq!(e_d, t.find_tag(0).unwrap().u8().unwrap())
42+
}
43+
_ => panic!("Incorrect CmdDataType"),
44+
}
45+
}
46+
_ => {
47+
panic!("Invalid response, expected InvResponse::Cmd");
48+
}
49+
},
50+
ExpectedInvResp::Status(e_status) => match inv_response {
51+
InvResp::Status(status) => {
52+
assert_eq!(e_status, status);
53+
}
54+
_ => {
55+
panic!("Invalid response, expected InvResponse::Status");
56+
}
57+
},
58+
}
59+
println!("Index {} success", index);
60+
index += 1;
61+
}
62+
assert_eq!(index, expected.len());
63+
}
64+
65+
#[macro_export]
66+
macro_rules! cmd_data {
67+
($path:ident, $data:literal) => {
68+
CmdData::new($path, EncodeValue::Value(&($data as u32)))
69+
};
70+
}
71+
72+
#[macro_export]
73+
macro_rules! echo_req {
74+
($endpoint:literal, $data:literal) => {
75+
CmdData::new(
76+
CmdPath::new(
77+
Some($endpoint),
78+
Some(echo_cluster::ID),
79+
Some(echo_cluster::Commands::EchoReq as u16),
80+
),
81+
EncodeValue::Value(&($data as u32)),
82+
)
83+
};
84+
}
85+
86+
#[macro_export]
87+
macro_rules! echo_resp {
88+
($endpoint:literal, $data:literal) => {
89+
ExpectedInvResp::Cmd(
90+
CmdPath::new(
91+
Some($endpoint),
92+
Some(echo_cluster::ID),
93+
Some(echo_cluster::Commands::EchoResp as u16),
94+
),
95+
$data,
96+
)
97+
};
98+
}

matter/tests/common/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
*/
1717

1818
pub mod attributes;
19+
pub mod commands;
1920
pub mod echo_cluster;
2021
pub mod im_engine;

matter/tests/data_model/commands.rs

Lines changed: 9 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,25 @@
1515
* limitations under the License.
1616
*/
1717

18+
use crate::{
19+
cmd_data,
20+
common::{commands::*, echo_cluster, im_engine::im_engine},
21+
echo_req, echo_resp,
22+
};
23+
1824
use matter::{
1925
data_model::{cluster_on_off, objects::EncodeValue},
2026
interaction_model::{
2127
core::{IMStatusCode, OpCode},
22-
messages::msg,
2328
messages::{
24-
ib::{CmdData, CmdPath, CmdStatus, InvResp},
29+
ib::{CmdData, CmdPath, CmdStatus},
30+
msg,
2531
msg::InvReq,
2632
},
2733
},
2834
tlv::{self, FromTLV, TLVArray},
2935
};
3036

31-
use crate::common::{echo_cluster, im_engine::im_engine};
32-
33-
enum ExpectedInvResp {
34-
Cmd(CmdPath, u8),
35-
Status(CmdStatus),
36-
}
37-
3837
// Helper for handling Invoke Command sequences
3938
fn handle_commands(input: &[CmdData], expected: &[ExpectedInvResp]) {
4039
let mut out_buf = [0u8; 400];
@@ -47,71 +46,8 @@ fn handle_commands(input: &[CmdData], expected: &[ExpectedInvResp]) {
4746
let (_, _, out_buf) = im_engine(OpCode::InvokeRequest, &req, &mut out_buf);
4847
tlv::print_tlv_list(out_buf);
4948
let root = tlv::get_root_node_struct(out_buf).unwrap();
50-
5149
let resp = msg::InvResp::from_tlv(&root).unwrap();
52-
let mut index = 0;
53-
for inv_response in resp.inv_responses.unwrap().iter() {
54-
println!("Validating index {}", index);
55-
match expected[index] {
56-
ExpectedInvResp::Cmd(e_c, e_d) => match inv_response {
57-
InvResp::Cmd(c) => {
58-
assert_eq!(e_c, c.path);
59-
match c.data {
60-
EncodeValue::Tlv(t) => {
61-
assert_eq!(e_d, t.find_tag(0).unwrap().u8().unwrap())
62-
}
63-
_ => panic!("Incorrect CmdDataType"),
64-
}
65-
}
66-
_ => {
67-
panic!("Invalid response, expected InvResponse::Cmd");
68-
}
69-
},
70-
ExpectedInvResp::Status(e_status) => match inv_response {
71-
InvResp::Status(status) => {
72-
assert_eq!(e_status, status);
73-
}
74-
_ => {
75-
panic!("Invalid response, expected InvResponse::Status");
76-
}
77-
},
78-
}
79-
println!("Index {} success", index);
80-
index += 1;
81-
}
82-
assert_eq!(index, expected.len());
83-
}
84-
85-
macro_rules! cmd_data {
86-
($path:ident, $data:literal) => {
87-
CmdData::new($path, EncodeValue::Value(&($data as u32)))
88-
};
89-
}
90-
91-
macro_rules! echo_req {
92-
($endpoint:literal, $data:literal) => {
93-
CmdData::new(
94-
CmdPath::new(
95-
Some($endpoint),
96-
Some(echo_cluster::ID),
97-
Some(echo_cluster::Commands::EchoReq as u16),
98-
),
99-
EncodeValue::Value(&($data as u32)),
100-
)
101-
};
102-
}
103-
104-
macro_rules! echo_resp {
105-
($endpoint:literal, $data:literal) => {
106-
ExpectedInvResp::Cmd(
107-
CmdPath::new(
108-
Some($endpoint),
109-
Some(echo_cluster::ID),
110-
Some(echo_cluster::Commands::EchoResp as u16),
111-
),
112-
$data,
113-
)
114-
};
50+
assert_inv_response(&resp, expected)
11551
}
11652

11753
#[test]

0 commit comments

Comments
 (0)