Skip to content

Commit da8d7a9

Browse files
committed
Tests: Add test for long read
1 parent 1ad4e6e commit da8d7a9

File tree

8 files changed

+246
-27
lines changed

8 files changed

+246
-27
lines changed

matter/src/data_model/cluster_basic_information.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use num_derive::FromPrimitive;
2222
pub const ID: u32 = 0x0028;
2323

2424
#[derive(FromPrimitive)]
25-
enum Attributes {
25+
pub enum Attributes {
2626
DMRevision = 0,
2727
VendorId = 2,
2828
ProductId = 4,

matter/src/data_model/objects/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub enum EncodeValue<'a> {
4242
Tlv(TLVElement<'a>),
4343
/// This indicates a static value. This variant is typically used in the transmit/
4444
/// to-tlv path
45-
Value(&'a (dyn ToTLV)),
45+
Value(&'a dyn ToTLV),
4646
}
4747

4848
impl<'a> EncodeValue<'a> {

matter/src/data_model/sdm/noc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub enum Commands {
8181
}
8282

8383
#[derive(FromPrimitive)]
84-
enum Attributes {
84+
pub enum Attributes {
8585
NOCs = 0,
8686
Fabrics = 1,
8787
SupportedFabrics = 2,

matter/src/data_model/system_model/descriptor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub const ID: u32 = 0x001D;
2828

2929
#[derive(FromPrimitive)]
3030
#[allow(clippy::enum_variant_names)]
31-
enum Attributes {
31+
pub enum Attributes {
3232
DeviceTypeList = 0,
3333
ServerList = 1,
3434
ClientList = 2,

matter/tests/common/attributes.rs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@
1515
* limitations under the License.
1616
*/
1717

18-
use matter::interaction_model::{messages::ib::AttrResp, messages::msg::ReportDataMsg};
18+
use matter::{
19+
interaction_model::{messages::ib::AttrResp, messages::msg::ReportDataMsg},
20+
tlv::{TLVElement, TLVList, TLVWriter, TagType, ToTLV},
21+
utils::writebuf::WriteBuf,
22+
};
1923

2024
/// Assert that the data received in the outbuf matches our expectations
21-
pub fn assert_attr_report(received: &ReportDataMsg, expected: &[AttrResp]) {
25+
pub fn __assert_attr_report(received: &ReportDataMsg, expected: &[AttrResp], skip_data: bool) {
2226
let mut index = 0;
2327

2428
// We can't use assert_eq because it will also try to match data-version
@@ -29,7 +33,9 @@ pub fn assert_attr_report(received: &ReportDataMsg, expected: &[AttrResp]) {
2933
AttrResp::Data(d) => {
3034
// We don't match the data-version
3135
assert_eq!(e_d.path, d.path);
32-
assert_eq!(e_d.data, d.data);
36+
if !skip_data {
37+
assert_eq!(e_d.data, d.data);
38+
}
3339
}
3440
_ => {
3541
panic!("Invalid response, expected AttrRespIn::Data");
@@ -43,6 +49,14 @@ pub fn assert_attr_report(received: &ReportDataMsg, expected: &[AttrResp]) {
4349
assert_eq!(index, expected.len());
4450
}
4551

52+
pub fn assert_attr_report(received: &ReportDataMsg, expected: &[AttrResp]) {
53+
__assert_attr_report(received, expected, false)
54+
}
55+
56+
pub fn assert_attr_report_skip_data(received: &ReportDataMsg, expected: &[AttrResp]) {
57+
__assert_attr_report(received, expected, true)
58+
}
59+
4660
// We have to hard-code this here, and it should match the tag
4761
// of the 'data' part in AttrData
4862
pub const ATTR_DATA_TAG_DATA: u8 = 2;
@@ -85,3 +99,37 @@ macro_rules! attr_status {
8599
AttrResp::Status(AttrStatus::new($path, $status, 0))
86100
};
87101
}
102+
103+
pub struct TLVHolder {
104+
buf: [u8; 100],
105+
used_len: usize,
106+
}
107+
108+
impl TLVHolder {
109+
pub fn new_array<'a, T, I>(ctx_tag: u8, data: I) -> Self
110+
where
111+
T: ToTLV + 'a,
112+
I: IntoIterator<Item = &'a T>,
113+
{
114+
let mut s = Self {
115+
buf: [0; 100],
116+
used_len: 0,
117+
};
118+
let buf_len = s.buf.len();
119+
let mut wb = WriteBuf::new(&mut s.buf, buf_len);
120+
let mut tw = TLVWriter::new(&mut wb);
121+
let _ = tw.start_array(TagType::Context(ctx_tag));
122+
for e in data {
123+
let _ = e.to_tlv(&mut tw, TagType::Anonymous);
124+
}
125+
let _ = tw.end_container();
126+
127+
s.used_len = wb.as_slice().len();
128+
s
129+
}
130+
131+
pub fn to_tlv(&self) -> TLVElement {
132+
let s = &self.buf[..self.used_len];
133+
TLVList::new(s).iter().next().unwrap()
134+
}
135+
}

matter/tests/data_model/attributes.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ use matter::{
2929
msg::{ReadReq, ReportDataMsg, WriteReq, WriteResp},
3030
},
3131
},
32-
tlv::{self, ElementType, FromTLV, TLVElement, TLVList, TLVWriter, TagType},
33-
utils::writebuf::WriteBuf,
32+
tlv::{self, ElementType, FromTLV, TLVElement, TLVWriter, TagType},
3433
};
3534

3635
use crate::{
@@ -208,19 +207,6 @@ fn test_read_wc_endpoint_only_1_has_cluster() {
208207
handle_read_reqs(input, expected);
209208
}
210209

211-
fn get_tlvs<'a>(buf: &'a mut [u8], data: &[u16]) -> TLVElement<'a> {
212-
let buf_len = buf.len();
213-
let mut wb = WriteBuf::new(buf, buf_len);
214-
let mut tw = TLVWriter::new(&mut wb);
215-
let _ = tw.start_array(TagType::Context(2));
216-
for e in data {
217-
let _ = tw.u16(TagType::Anonymous, *e);
218-
}
219-
let _ = tw.end_container();
220-
let tlv_array = TLVList::new(wb.as_slice()).iter().next().unwrap();
221-
tlv_array
222-
}
223-
224210
#[test]
225211
fn test_read_wc_endpoint_wc_attribute() {
226212
// 1 Attr Read Request
@@ -230,9 +216,8 @@ fn test_read_wc_endpoint_wc_attribute() {
230216
let wc_ep_wc_attr = GenericPath::new(None, Some(echo_cluster::ID), None);
231217
let input = &[AttrPath::new(&wc_ep_wc_attr)];
232218

233-
let mut buf = [0u8; 100];
234-
let attr_list_tlvs = get_tlvs(
235-
&mut buf,
219+
let attr_list = TLVHolder::new_array(
220+
2,
236221
&[
237222
GlobalElements::FeatureMap as u16,
238223
GlobalElements::AttributeList as u16,
@@ -242,6 +227,7 @@ fn test_read_wc_endpoint_wc_attribute() {
242227
echo_cluster::Attributes::AttCustom as u16,
243228
],
244229
);
230+
let attr_list_tlv = attr_list.to_tlv();
245231

246232
let expected = &[
247233
attr_data_path!(
@@ -258,7 +244,7 @@ fn test_read_wc_endpoint_wc_attribute() {
258244
Some(echo_cluster::ID),
259245
Some(GlobalElements::AttributeList as u32),
260246
),
261-
attr_list_tlvs.get_element_type()
247+
attr_list_tlv.get_element_type()
262248
),
263249
attr_data_path!(
264250
GenericPath::new(
@@ -298,7 +284,7 @@ fn test_read_wc_endpoint_wc_attribute() {
298284
Some(echo_cluster::ID),
299285
Some(GlobalElements::AttributeList as u32),
300286
),
301-
attr_list_tlvs.get_element_type()
287+
attr_list_tlv.get_element_type()
302288
),
303289
attr_data_path!(
304290
GenericPath::new(
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
*
3+
* Copyright (c) 2020-2022 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::{
20+
cluster_basic_information as basic_info, cluster_on_off as onoff,
21+
objects::{EncodeValue, GlobalElements},
22+
sdm::{admin_commissioning as adm_comm, general_commissioning as gen_comm, noc},
23+
system_model::{access_control as acl, descriptor},
24+
},
25+
interaction_model::{
26+
core::{IMStatusCode, OpCode},
27+
messages::GenericPath,
28+
messages::{
29+
ib::{AttrData, AttrPath, AttrResp},
30+
msg::{ReadReq, ReportDataMsg, StatusResp},
31+
},
32+
},
33+
tlv::{self, ElementType, FromTLV, TLVElement, TagType, ToTLV},
34+
transport::{
35+
exchange::{self, Exchange},
36+
udp::MAX_RX_BUF_SIZE,
37+
},
38+
};
39+
40+
use crate::{
41+
attr_data,
42+
common::{
43+
attributes::*,
44+
echo_cluster as echo,
45+
im_engine::{ImEngine, ImInput},
46+
},
47+
};
48+
49+
pub struct LongRead {
50+
im_engine: ImEngine,
51+
}
52+
53+
impl LongRead {
54+
pub fn new() -> Self {
55+
let mut im_engine = ImEngine::new();
56+
// Use the same exchange for all parts of the transaction
57+
im_engine.exch = Some(Exchange::new(1, 0, exchange::Role::Responder));
58+
Self { im_engine }
59+
}
60+
61+
pub fn process<'a>(
62+
&mut self,
63+
action: OpCode,
64+
data: &dyn ToTLV,
65+
data_out: &'a mut [u8],
66+
) -> (u8, &'a mut [u8]) {
67+
let input = ImInput::new(action, data);
68+
let (response, output) = self.im_engine.process(&input, data_out);
69+
(response, output)
70+
}
71+
}
72+
73+
fn wildcard_read_resp(part: u8) -> Vec<AttrResp<'static>> {
74+
// For brevity, we only check the AttrPath, not the actual 'data'
75+
let dont_care = ElementType::U8(0);
76+
let part1 = vec![
77+
attr_data!(0, 29, GlobalElements::FeatureMap, dont_care),
78+
attr_data!(0, 29, GlobalElements::AttributeList, dont_care),
79+
attr_data!(0, 29, descriptor::Attributes::DeviceTypeList, dont_care),
80+
attr_data!(0, 29, descriptor::Attributes::ServerList, dont_care),
81+
attr_data!(0, 29, descriptor::Attributes::PartsList, dont_care),
82+
attr_data!(0, 29, descriptor::Attributes::ClientList, dont_care),
83+
attr_data!(0, 40, GlobalElements::FeatureMap, dont_care),
84+
attr_data!(0, 40, GlobalElements::AttributeList, dont_care),
85+
attr_data!(0, 40, basic_info::Attributes::DMRevision, dont_care),
86+
attr_data!(0, 40, basic_info::Attributes::VendorId, dont_care),
87+
attr_data!(0, 40, basic_info::Attributes::ProductId, dont_care),
88+
attr_data!(0, 40, basic_info::Attributes::HwVer, dont_care),
89+
attr_data!(0, 40, basic_info::Attributes::SwVer, dont_care),
90+
attr_data!(0, 40, basic_info::Attributes::SwVerString, dont_care),
91+
attr_data!(0, 40, basic_info::Attributes::SerialNo, dont_care),
92+
attr_data!(0, 48, GlobalElements::FeatureMap, dont_care),
93+
attr_data!(0, 48, GlobalElements::AttributeList, dont_care),
94+
attr_data!(0, 48, gen_comm::Attributes::BreadCrumb, dont_care),
95+
attr_data!(0, 48, gen_comm::Attributes::RegConfig, dont_care),
96+
attr_data!(0, 48, gen_comm::Attributes::LocationCapability, dont_care),
97+
attr_data!(
98+
0,
99+
48,
100+
gen_comm::Attributes::BasicCommissioningInfo,
101+
dont_care
102+
),
103+
attr_data!(0, 49, GlobalElements::FeatureMap, dont_care),
104+
attr_data!(0, 49, GlobalElements::AttributeList, dont_care),
105+
attr_data!(0, 60, GlobalElements::FeatureMap, dont_care),
106+
attr_data!(0, 60, GlobalElements::AttributeList, dont_care),
107+
attr_data!(0, 60, adm_comm::Attributes::WindowStatus, dont_care),
108+
attr_data!(0, 60, adm_comm::Attributes::AdminFabricIndex, dont_care),
109+
attr_data!(0, 60, adm_comm::Attributes::AdminVendorId, dont_care),
110+
attr_data!(0, 62, GlobalElements::FeatureMap, dont_care),
111+
attr_data!(0, 62, GlobalElements::AttributeList, dont_care),
112+
attr_data!(0, 62, noc::Attributes::CurrentFabricIndex, dont_care),
113+
attr_data!(0, 62, noc::Attributes::Fabrics, dont_care),
114+
attr_data!(0, 62, noc::Attributes::SupportedFabrics, dont_care),
115+
attr_data!(0, 62, noc::Attributes::CommissionedFabrics, dont_care),
116+
attr_data!(0, 31, GlobalElements::FeatureMap, dont_care),
117+
attr_data!(0, 31, GlobalElements::AttributeList, dont_care),
118+
attr_data!(0, 31, acl::Attributes::Acl, dont_care),
119+
attr_data!(0, 31, acl::Attributes::Extension, dont_care),
120+
attr_data!(0, 31, acl::Attributes::SubjectsPerEntry, dont_care),
121+
attr_data!(0, 31, acl::Attributes::TargetsPerEntry, dont_care),
122+
attr_data!(0, 31, acl::Attributes::EntriesPerFabric, dont_care),
123+
attr_data!(0, echo::ID, GlobalElements::FeatureMap, dont_care),
124+
attr_data!(0, echo::ID, GlobalElements::AttributeList, dont_care),
125+
attr_data!(0, echo::ID, echo::Attributes::Att1, dont_care),
126+
attr_data!(0, echo::ID, echo::Attributes::Att2, dont_care),
127+
attr_data!(0, echo::ID, echo::Attributes::AttCustom, dont_care),
128+
attr_data!(1, 29, GlobalElements::FeatureMap, dont_care),
129+
attr_data!(1, 29, GlobalElements::AttributeList, dont_care),
130+
attr_data!(1, 29, descriptor::Attributes::DeviceTypeList, dont_care),
131+
];
132+
133+
let part2 = vec![
134+
attr_data!(1, 29, descriptor::Attributes::ServerList, dont_care),
135+
attr_data!(1, 29, descriptor::Attributes::PartsList, dont_care),
136+
attr_data!(1, 29, descriptor::Attributes::ClientList, dont_care),
137+
attr_data!(1, 6, GlobalElements::FeatureMap, dont_care),
138+
attr_data!(1, 6, GlobalElements::AttributeList, dont_care),
139+
attr_data!(1, 6, onoff::Attributes::OnOff, dont_care),
140+
attr_data!(1, echo::ID, GlobalElements::FeatureMap, dont_care),
141+
attr_data!(1, echo::ID, GlobalElements::AttributeList, dont_care),
142+
attr_data!(1, echo::ID, echo::Attributes::Att1, dont_care),
143+
attr_data!(1, echo::ID, echo::Attributes::Att2, dont_care),
144+
attr_data!(1, echo::ID, echo::Attributes::AttCustom, dont_care),
145+
];
146+
147+
if part == 1 {
148+
part1
149+
} else {
150+
part2
151+
}
152+
}
153+
154+
#[test]
155+
fn test_long_read_success() {
156+
// Read the entire attribute database, which requires 2 reads to complete
157+
let _ = env_logger::try_init();
158+
let mut lr = LongRead::new();
159+
let mut output = [0_u8; MAX_RX_BUF_SIZE + 100];
160+
161+
let wc_path = GenericPath::new(None, None, None);
162+
163+
let read_all = [AttrPath::new(&wc_path)];
164+
let read_req = ReadReq::new(true).set_attr_requests(&read_all);
165+
let expected_part1 = wildcard_read_resp(1);
166+
let (out_code, out_data) = lr.process(OpCode::ReadRequest, &read_req, &mut output);
167+
let root = tlv::get_root_node_struct(out_data).unwrap();
168+
let report_data = ReportDataMsg::from_tlv(&root).unwrap();
169+
assert_attr_report_skip_data(&report_data, &expected_part1);
170+
assert_eq!(report_data.more_chunks, Some(true));
171+
assert_eq!(out_code, OpCode::ReportData as u8);
172+
173+
// Ask for the next read by sending a status report
174+
let status_report = StatusResp {
175+
status: IMStatusCode::Success,
176+
};
177+
let expected_part2 = wildcard_read_resp(2);
178+
let (out_code, out_data) = lr.process(OpCode::StatusResponse, &status_report, &mut output);
179+
let root = tlv::get_root_node_struct(out_data).unwrap();
180+
let report_data = ReportDataMsg::from_tlv(&root).unwrap();
181+
assert_attr_report_skip_data(&report_data, &expected_part2);
182+
assert_eq!(report_data.more_chunks, None);
183+
assert_eq!(out_code, OpCode::ReportData as u8);
184+
}

matter/tests/data_model_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ mod data_model {
2222
mod attribute_lists;
2323
mod attributes;
2424
mod commands;
25+
mod long_reads;
2526
mod timed_requests;
2627
}

0 commit comments

Comments
 (0)