diff options
author | Chih-Yu Huang <akahuang@google.com> | 2022-04-16 01:23:28 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-04-16 01:23:28 +0000 |
commit | 2e6a801a12e248f24e12e28da5acc151a12e1f83 (patch) | |
tree | aaf976e3ee61ebe9d933e20ee69bbb9e8a3397f4 | |
parent | 2cad18350265e51a82dac486574976744f34ff9a (diff) | |
parent | d21957a715591a1687f0aa45eb3c242af8b0bd2b (diff) | |
download | uwb-2e6a801a12e248f24e12e28da5acc151a12e1f83.tar.gz |
uwb_core: verify the equality of TLV list by HashMap am: 5f9ff7f9dc am: d21957a715
Original change: https://googleplex-android-review.googlesource.com/c/platform/external/uwb/+/17767895
Change-Id: I83d8ee0210993b3a947dfba02519d004bb347f64
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | src/rust/uwb_core/src/uci/mock_uci_manager.rs | 8 | ||||
-rw-r--r-- | src/rust/uwb_core/src/uci/params.rs | 23 | ||||
-rw-r--r-- | src/rust/uwb_core/src/uci/uci_manager.rs | 14 |
3 files changed, 29 insertions, 16 deletions
diff --git a/src/rust/uwb_core/src/uci/mock_uci_manager.rs b/src/rust/uwb_core/src/uci/mock_uci_manager.rs index ef1d8b2..05af30c 100644 --- a/src/rust/uwb_core/src/uci/mock_uci_manager.rs +++ b/src/rust/uwb_core/src/uci/mock_uci_manager.rs @@ -24,7 +24,7 @@ use tokio::time::timeout; use crate::uci::error::{Error, Result}; use crate::uci::notification::UciNotification; use crate::uci::params::{ - app_config_tlv_eq, device_config_tlv_eq, AppConfigTlv, AppConfigTlvType, CapTlv, Controlee, + app_config_tlvs_eq, device_config_tlvs_eq, AppConfigTlv, AppConfigTlvType, CapTlv, Controlee, CoreSetConfigResponse, CountryCode, DeviceConfigId, DeviceConfigTlv, GetDeviceInfoResponse, PowerStats, RawVendorMessage, ResetConfig, SessionId, SessionState, SessionType, SetAppConfigResponse, UpdateMulticastListAction, @@ -325,8 +325,7 @@ impl UciManager for MockUciManager { let mut expected_calls = self.expected_calls.lock().unwrap(); match expected_calls.pop_front() { Some(ExpectedCall::CoreSetConfig { expected_config_tlvs, out }) - if zip(&expected_config_tlvs, &config_tlvs) - .all(|(a, b)| device_config_tlv_eq(a, b)) => + if device_config_tlvs_eq(&expected_config_tlvs, &config_tlvs) => { self.expect_call_consumed.notify_one(); out @@ -409,8 +408,7 @@ impl UciManager for MockUciManager { expected_config_tlvs, out, }) if expected_session_id == session_id - && zip(&expected_config_tlvs, &config_tlvs) - .all(|(a, b)| app_config_tlv_eq(a, b)) => + && app_config_tlvs_eq(&expected_config_tlvs, &config_tlvs) => { self.expect_call_consumed.notify_one(); out diff --git a/src/rust/uwb_core/src/uci/params.rs b/src/rust/uwb_core/src/uci/params.rs index 71cf7b7..72c39b8 100644 --- a/src/rust/uwb_core/src/uci/params.rs +++ b/src/rust/uwb_core/src/uci/params.rs @@ -17,7 +17,8 @@ #![allow(clippy::eq_op)] -use std::iter::zip; +use std::collections::{hash_map::RandomState, HashMap}; +use std::iter::{zip, FromIterator}; use crate::uci::error::StatusCode; @@ -54,12 +55,24 @@ pub fn cap_tlv_eq(a: &CapTlv, b: &CapTlv) -> bool { a.t == b.t && a.v == b.v } -pub fn app_config_tlv_eq(a: &AppConfigTlv, b: &AppConfigTlv) -> bool { - a.cfg_id == b.cfg_id && a.v == b.v +pub fn app_config_tlvs_eq(a: &[AppConfigTlv], b: &[AppConfigTlv]) -> bool { + app_config_tlvs_to_map(a) == app_config_tlvs_to_map(b) } -pub fn device_config_tlv_eq(a: &DeviceConfigTlv, b: &DeviceConfigTlv) -> bool { - a.cfg_id == b.cfg_id && a.v == b.v +fn app_config_tlvs_to_map( + tlvs: &[AppConfigTlv], +) -> HashMap<AppConfigTlvType, &Vec<u8>, RandomState> { + HashMap::from_iter(tlvs.iter().map(|config| (config.cfg_id, &config.v))) +} + +pub fn device_config_tlvs_eq(a: &[DeviceConfigTlv], b: &[DeviceConfigTlv]) -> bool { + device_config_tlvs_to_map(a) == device_config_tlvs_to_map(b) +} + +fn device_config_tlvs_to_map( + tlvs: &[DeviceConfigTlv], +) -> HashMap<DeviceConfigId, &Vec<u8>, RandomState> { + HashMap::from_iter(tlvs.iter().map(|config| (config.cfg_id, &config.v))) } #[derive(Debug, Clone)] diff --git a/src/rust/uwb_core/src/uci/uci_manager.rs b/src/rust/uwb_core/src/uci/uci_manager.rs index d625fa5..1f72954 100644 --- a/src/rust/uwb_core/src/uci/uci_manager.rs +++ b/src/rust/uwb_core/src/uci/uci_manager.rs @@ -649,7 +649,7 @@ mod tests { use crate::uci::error::StatusCode; use crate::uci::mock_uci_hal::MockUciHal; use crate::uci::params::{ - app_config_tlv_eq, cap_tlv_eq, device_config_tlv_eq, power_stats_eq, CapTlvType, + app_config_tlvs_eq, cap_tlv_eq, device_config_tlvs_eq, power_stats_eq, CapTlvType, }; use crate::utils::init_test_logging; @@ -835,11 +835,13 @@ mod tests { .await; let config_id = DeviceConfigId::DeviceState; - let expected_result = - DeviceConfigTlv { cfg_id: DeviceConfigId::DeviceState, v: vec![0x12, 0x34, 0x56] }; + let expected_result = vec![DeviceConfigTlv { + cfg_id: DeviceConfigId::DeviceState, + v: vec![0x12, 0x34, 0x56], + }]; CoreSetConfigResponse { status: StatusCode::UciStatusOk, config_status: vec![] }; let result = uci_manager.core_get_config(vec![config_id]).await.unwrap(); - assert!(device_config_tlv_eq(&result[0], &expected_result)); + assert!(device_config_tlvs_eq(&result, &expected_result)); assert!(mock_hal.wait_expected_calls_done().await); } @@ -949,9 +951,9 @@ mod tests { let session_id = 0x123; let config_id = AppConfigTlvType::DeviceType; let expected_result = - AppConfigTlv { cfg_id: AppConfigTlvType::DeviceType, v: vec![0x12, 0x34, 0x56] }; + vec![AppConfigTlv { cfg_id: AppConfigTlvType::DeviceType, v: vec![0x12, 0x34, 0x56] }]; let result = uci_manager.session_get_app_config(session_id, vec![config_id]).await.unwrap(); - assert!(app_config_tlv_eq(&result[0], &expected_result)); + assert!(app_config_tlvs_eq(&result, &expected_result)); assert!(mock_hal.wait_expected_calls_done().await); } |