tests: use 'dyn ToTLV' to avoid duplication
This commit is contained in:
parent
ebe2e979f5
commit
4a041e1f8c
6 changed files with 39 additions and 95 deletions
|
@ -29,6 +29,7 @@ use matter::{
|
|||
error::Error,
|
||||
fabric::FabricMgr,
|
||||
interaction_model::{core::OpCode, InteractionModel},
|
||||
tlv::{TLVWriter, TagType, ToTLV},
|
||||
transport::packet::Packet,
|
||||
transport::proto_demux::HandleProto,
|
||||
transport::{
|
||||
|
@ -38,6 +39,7 @@ use matter::{
|
|||
proto_demux::ProtoCtx,
|
||||
session::{CloneData, SessionMgr, SessionMode},
|
||||
},
|
||||
utils::writebuf::WriteBuf,
|
||||
};
|
||||
use std::{
|
||||
net::{Ipv4Addr, SocketAddr},
|
||||
|
@ -64,16 +66,16 @@ pub struct ImEngine {
|
|||
|
||||
pub struct ImInput<'a> {
|
||||
action: OpCode,
|
||||
data_in: &'a [u8],
|
||||
data: &'a dyn ToTLV,
|
||||
peer_id: u64,
|
||||
}
|
||||
|
||||
pub const IM_ENGINE_PEER_ID: u64 = 445566;
|
||||
impl<'a> ImInput<'a> {
|
||||
pub fn new(action: OpCode, data_in: &'a [u8]) -> Self {
|
||||
pub fn new(action: OpCode, data: &'a dyn ToTLV) -> Self {
|
||||
Self {
|
||||
action,
|
||||
data_in,
|
||||
data,
|
||||
peer_id: IM_ENGINE_PEER_ID,
|
||||
}
|
||||
}
|
||||
|
@ -152,10 +154,21 @@ impl ImEngine {
|
|||
rx.set_proto_id(0x01);
|
||||
rx.set_proto_opcode(input.action as u8);
|
||||
rx.peer = Address::default();
|
||||
let in_data_len = input.data_in.len();
|
||||
let rx_buf = rx.as_borrow_slice();
|
||||
rx_buf[..in_data_len].copy_from_slice(input.data_in);
|
||||
rx.get_parsebuf().unwrap().set_len(in_data_len);
|
||||
|
||||
{
|
||||
let mut buf = [0u8; 400];
|
||||
let buf_len = buf.len();
|
||||
let mut wb = WriteBuf::new(&mut buf, buf_len);
|
||||
let mut tw = TLVWriter::new(&mut wb);
|
||||
|
||||
input.data.to_tlv(&mut tw, TagType::Anonymous).unwrap();
|
||||
|
||||
let input_data = wb.as_borrow_slice();
|
||||
let in_data_len = input_data.len();
|
||||
let rx_buf = rx.as_borrow_slice();
|
||||
rx_buf[..in_data_len].copy_from_slice(input_data);
|
||||
rx.get_parsebuf().unwrap().set_len(in_data_len);
|
||||
}
|
||||
|
||||
let mut ctx = ProtoCtx::new(exch_ctx, rx, tx);
|
||||
self.im.handle_proto_id(&mut ctx).unwrap();
|
||||
|
@ -169,11 +182,11 @@ impl ImEngine {
|
|||
// Create an Interaction Model, Data Model and run a rx/tx transaction through it
|
||||
pub fn im_engine<'a>(
|
||||
action: OpCode,
|
||||
data_in: &[u8],
|
||||
data: &dyn ToTLV,
|
||||
data_out: &'a mut [u8],
|
||||
) -> (DataModel, u8, &'a mut [u8]) {
|
||||
let mut engine = ImEngine::new();
|
||||
let input = ImInput::new(action, data_in);
|
||||
let input = ImInput::new(action, data);
|
||||
let (response, output) = engine.process(&input, data_out);
|
||||
(engine.dm, response, output)
|
||||
}
|
||||
|
|
|
@ -29,8 +29,7 @@ use matter::{
|
|||
},
|
||||
messages::{msg, GenericPath},
|
||||
},
|
||||
tlv::{self, ElementType, FromTLV, TLVArray, TLVElement, TLVWriter, TagType, ToTLV},
|
||||
utils::writebuf::WriteBuf,
|
||||
tlv::{self, ElementType, FromTLV, TLVArray, TLVElement, TLVWriter, TagType},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
|
@ -61,16 +60,10 @@ fn gen_read_reqs_output<'a>(
|
|||
dataver_filters: Option<TLVArray<'a, DataVersionFilter>>,
|
||||
out_buf: &'a mut [u8],
|
||||
) -> ReportDataMsg<'a> {
|
||||
let mut buf = [0u8; 400];
|
||||
let buf_len = buf.len();
|
||||
let mut wb = WriteBuf::new(&mut buf, buf_len);
|
||||
let mut tw = TLVWriter::new(&mut wb);
|
||||
|
||||
let mut read_req = ReadReq::new(true).set_attr_requests(input);
|
||||
read_req.dataver_filters = dataver_filters;
|
||||
read_req.to_tlv(&mut tw, TagType::Anonymous).unwrap();
|
||||
|
||||
let mut input = ImInput::new(OpCode::ReadRequest, wb.as_borrow_slice());
|
||||
let mut input = ImInput::new(OpCode::ReadRequest, &read_req);
|
||||
input.set_peer_node_id(peer_node_id);
|
||||
|
||||
let (_, out_buf) = im.process(&input, out_buf);
|
||||
|
@ -87,17 +80,10 @@ fn handle_write_reqs(
|
|||
input: &[AttrData],
|
||||
expected: &[AttrStatus],
|
||||
) {
|
||||
let mut buf = [0u8; 400];
|
||||
let mut out_buf = [0u8; 400];
|
||||
|
||||
let buf_len = buf.len();
|
||||
let mut wb = WriteBuf::new(&mut buf, buf_len);
|
||||
let mut tw = TLVWriter::new(&mut wb);
|
||||
|
||||
let write_req = WriteReq::new(false, input);
|
||||
write_req.to_tlv(&mut tw, TagType::Anonymous).unwrap();
|
||||
|
||||
let mut input = ImInput::new(OpCode::WriteRequest, wb.as_borrow_slice());
|
||||
let mut input = ImInput::new(OpCode::WriteRequest, &write_req);
|
||||
input.set_peer_node_id(peer_node_id);
|
||||
let (_, out_buf) = im.process(&input, &mut out_buf);
|
||||
|
||||
|
|
|
@ -19,14 +19,13 @@ use matter::{
|
|||
data_model::{core::DataModel, objects::EncodeValue},
|
||||
interaction_model::{
|
||||
core::{IMStatusCode, OpCode},
|
||||
messages::GenericPath,
|
||||
messages::{
|
||||
ib::{AttrData, AttrPath, AttrStatus},
|
||||
msg::WriteReq,
|
||||
msg::{WriteReq, WriteResp},
|
||||
},
|
||||
messages::{msg, GenericPath},
|
||||
},
|
||||
tlv::{self, FromTLV, Nullable, TLVWriter, TagType, ToTLV},
|
||||
utils::writebuf::WriteBuf,
|
||||
tlv::{self, FromTLV, Nullable},
|
||||
};
|
||||
|
||||
use crate::common::{
|
||||
|
@ -36,36 +35,14 @@ use crate::common::{
|
|||
|
||||
// Helper for handling Write Attribute sequences
|
||||
fn handle_write_reqs(input: &[AttrData], expected: &[AttrStatus]) -> DataModel {
|
||||
let mut buf = [0u8; 400];
|
||||
let mut out_buf = [0u8; 400];
|
||||
|
||||
let buf_len = buf.len();
|
||||
let mut wb = WriteBuf::new(&mut buf, buf_len);
|
||||
let mut tw = TLVWriter::new(&mut wb);
|
||||
|
||||
let write_req = WriteReq::new(false, input);
|
||||
write_req.to_tlv(&mut tw, TagType::Anonymous).unwrap();
|
||||
|
||||
let (dm, _, out_buf) = im_engine(OpCode::WriteRequest, wb.as_borrow_slice(), &mut out_buf);
|
||||
let (dm, _, out_buf) = im_engine(OpCode::WriteRequest, &write_req, &mut out_buf);
|
||||
tlv::print_tlv_list(out_buf);
|
||||
let root = tlv::get_root_node_struct(out_buf).unwrap();
|
||||
|
||||
let mut index = 0;
|
||||
let response_iter = root
|
||||
.find_tag(msg::WriteRespTag::WriteResponses as u32)
|
||||
.unwrap()
|
||||
.confirm_array()
|
||||
.unwrap()
|
||||
.enter()
|
||||
.unwrap();
|
||||
for response in response_iter {
|
||||
println!("Validating index {}", index);
|
||||
let status = AttrStatus::from_tlv(&response).unwrap();
|
||||
assert_eq!(expected[index], status);
|
||||
println!("Index {} success", index);
|
||||
index += 1;
|
||||
}
|
||||
assert_eq!(index, expected.len());
|
||||
let resp = WriteResp::from_tlv(&root).unwrap();
|
||||
assert_eq!(resp.write_responses, expected);
|
||||
dm
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ use matter::{
|
|||
msg::{ReadReq, ReportDataMsg, WriteReq, WriteResp},
|
||||
},
|
||||
},
|
||||
tlv::{self, ElementType, FromTLV, TLVElement, TLVList, TLVWriter, TagType, ToTLV},
|
||||
tlv::{self, ElementType, FromTLV, TLVElement, TLVList, TLVWriter, TagType},
|
||||
utils::writebuf::WriteBuf,
|
||||
};
|
||||
|
||||
|
@ -46,15 +46,8 @@ fn handle_read_reqs(input: &[AttrPath], expected: &[AttrResp]) {
|
|||
|
||||
// Helper for handling Read Req sequences
|
||||
fn gen_read_reqs_output<'a>(input: &[AttrPath], out_buf: &'a mut [u8]) -> ReportDataMsg<'a> {
|
||||
let mut buf = [0u8; 400];
|
||||
let buf_len = buf.len();
|
||||
let mut wb = WriteBuf::new(&mut buf, buf_len);
|
||||
let mut tw = TLVWriter::new(&mut wb);
|
||||
|
||||
let read_req = ReadReq::new(true).set_attr_requests(input);
|
||||
read_req.to_tlv(&mut tw, TagType::Anonymous).unwrap();
|
||||
|
||||
let (_, _, out_buf) = im_engine(OpCode::ReadRequest, wb.as_borrow_slice(), out_buf);
|
||||
let (_, _, out_buf) = im_engine(OpCode::ReadRequest, &read_req, out_buf);
|
||||
tlv::print_tlv_list(out_buf);
|
||||
let root = tlv::get_root_node_struct(out_buf).unwrap();
|
||||
ReportDataMsg::from_tlv(&root).unwrap()
|
||||
|
@ -62,17 +55,10 @@ fn gen_read_reqs_output<'a>(input: &[AttrPath], out_buf: &'a mut [u8]) -> Report
|
|||
|
||||
// Helper for handling Write Attribute sequences
|
||||
fn handle_write_reqs(input: &[AttrData], expected: &[AttrStatus]) -> DataModel {
|
||||
let mut buf = [0u8; 400];
|
||||
let mut out_buf = [0u8; 400];
|
||||
|
||||
let buf_len = buf.len();
|
||||
let mut wb = WriteBuf::new(&mut buf, buf_len);
|
||||
let mut tw = TLVWriter::new(&mut wb);
|
||||
|
||||
let write_req = WriteReq::new(false, input);
|
||||
write_req.to_tlv(&mut tw, TagType::Anonymous).unwrap();
|
||||
|
||||
let (dm, _, out_buf) = im_engine(OpCode::WriteRequest, wb.as_borrow_slice(), &mut out_buf);
|
||||
let (dm, _, out_buf) = im_engine(OpCode::WriteRequest, &write_req, &mut out_buf);
|
||||
let root = tlv::get_root_node_struct(out_buf).unwrap();
|
||||
let response = WriteResp::from_tlv(&root).unwrap();
|
||||
assert_eq!(response.write_responses, expected);
|
||||
|
|
|
@ -25,8 +25,7 @@ use matter::{
|
|||
msg::InvReq,
|
||||
},
|
||||
},
|
||||
tlv::{self, FromTLV, TLVArray, TLVWriter, TagType, ToTLV},
|
||||
utils::writebuf::WriteBuf,
|
||||
tlv::{self, FromTLV, TLVArray},
|
||||
};
|
||||
|
||||
use crate::common::{echo_cluster, im_engine::im_engine};
|
||||
|
@ -38,21 +37,14 @@ enum ExpectedInvResp {
|
|||
|
||||
// Helper for handling Invoke Command sequences
|
||||
fn handle_commands(input: &[CmdData], expected: &[ExpectedInvResp]) {
|
||||
let mut buf = [0u8; 400];
|
||||
let mut out_buf = [0u8; 400];
|
||||
|
||||
let buf_len = buf.len();
|
||||
let mut wb = WriteBuf::new(&mut buf, buf_len);
|
||||
let mut tw = TLVWriter::new(&mut wb);
|
||||
|
||||
let req = InvReq {
|
||||
suppress_response: Some(false),
|
||||
timed_request: Some(false),
|
||||
inv_requests: Some(TLVArray::Slice(input)),
|
||||
};
|
||||
req.to_tlv(&mut tw, TagType::Anonymous).unwrap();
|
||||
|
||||
let (_, _, out_buf) = im_engine(OpCode::InvokeRequest, wb.as_borrow_slice(), &mut out_buf);
|
||||
let (_, _, out_buf) = im_engine(OpCode::InvokeRequest, &req, &mut out_buf);
|
||||
tlv::print_tlv_list(out_buf);
|
||||
let root = tlv::get_root_node_struct(out_buf).unwrap();
|
||||
|
||||
|
|
|
@ -31,9 +31,8 @@ use matter::{
|
|||
msg::{StatusResp, TimedReq, WriteReq, WriteResp},
|
||||
},
|
||||
},
|
||||
tlv::{self, FromTLV, TLVWriter, TagType, ToTLV},
|
||||
tlv::{self, FromTLV, TLVWriter},
|
||||
transport::exchange::{self, Exchange},
|
||||
utils::writebuf::WriteBuf,
|
||||
};
|
||||
|
||||
use crate::common::{
|
||||
|
@ -53,20 +52,14 @@ fn handle_timed_write_reqs(
|
|||
timeout: u16,
|
||||
delay: u16,
|
||||
) -> DataModel {
|
||||
let mut buf = [0u8; 400];
|
||||
let buf_len = buf.len();
|
||||
|
||||
let mut im_engine = ImEngine::new();
|
||||
// Use the same exchange for all parts of the transaction
|
||||
im_engine.exch = Some(Exchange::new(1, 0, exchange::Role::Responder));
|
||||
|
||||
// Send Timed Req
|
||||
let mut out_buf = [0u8; 400];
|
||||
let mut wb = WriteBuf::new(&mut buf, buf_len);
|
||||
let mut tw = TLVWriter::new(&mut wb);
|
||||
let timed_req = TimedReq { timeout };
|
||||
timed_req.to_tlv(&mut tw, TagType::Anonymous).unwrap();
|
||||
let im_input = ImInput::new(OpCode::TimedRequest, wb.as_borrow_slice());
|
||||
let im_input = ImInput::new(OpCode::TimedRequest, &timed_req);
|
||||
let (_, out_buf) = im_engine.process(&im_input, &mut out_buf);
|
||||
tlv::print_tlv_list(out_buf);
|
||||
|
||||
|
@ -76,11 +69,8 @@ fn handle_timed_write_reqs(
|
|||
|
||||
// Send Write Req
|
||||
let mut out_buf = [0u8; 400];
|
||||
let mut wb = WriteBuf::new(&mut buf, buf_len);
|
||||
let mut tw = TLVWriter::new(&mut wb);
|
||||
let write_req = WriteReq::new(false, input);
|
||||
write_req.to_tlv(&mut tw, TagType::Anonymous).unwrap();
|
||||
let input = ImInput::new(OpCode::WriteRequest, wb.as_borrow_slice());
|
||||
let input = ImInput::new(OpCode::WriteRequest, &write_req);
|
||||
let (resp_opcode, out_buf) = im_engine.process(&input, &mut out_buf);
|
||||
|
||||
tlv::print_tlv_list(out_buf);
|
||||
|
|
Loading…
Add table
Reference in a new issue