diff --git a/fastpair/rust/bluetooth/src/common/address.rs b/fastpair/rust/bluetooth/src/common/address.rs index db0481f6..bff124d7 100644 --- a/fastpair/rust/bluetooth/src/common/address.rs +++ b/fastpair/rust/bluetooth/src/common/address.rs @@ -93,3 +93,87 @@ impl From for u64 { u64::from_le_bytes(bytes) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ble_address_new() { + let addr = BleAddress::new(0x112233445566, BleAddressKind::Public); + assert_eq!(addr.val, [0x66, 0x55, 0x44, 0x33, 0x22, 0x11]); + assert_eq!(addr.kind, BleAddressKind::Public); + } + + #[test] + fn ble_address_get_kind() { + let addr_public = + BleAddress::new(0x112233445566, BleAddressKind::Public); + assert_eq!(addr_public.get_kind(), BleAddressKind::Public); + + let addr_random = + BleAddress::new(0xAABBCCDDEEFF, BleAddressKind::Random); + assert_eq!(addr_random.get_kind(), BleAddressKind::Random); + } + + #[test] + fn ble_address_into_u64() { + let ble_addr = BleAddress::new(0x112233445566, BleAddressKind::Public); + let u64_addr: u64 = ble_addr.into(); + assert_eq!(u64_addr, 0x112233445566); + } + + #[test] + fn classic_address_from_u64() { + let u64_addr = 0x112233445566; + let classic_addr: ClassicAddress = u64_addr.into(); + assert_eq!(classic_addr.0, [0x66, 0x55, 0x44, 0x33, 0x22, 0x11]); + } + + #[test] + fn try_from_ble_address_to_classic() { + let ble_addr = BleAddress::new(0x112233445566, BleAddressKind::Public); + let result: Result = + TryFrom::try_from(ble_addr); + assert!(result.is_ok()); + assert_eq!(result.unwrap().0, [0x66, 0x55, 0x44, 0x33, 0x22, 0x11]); + + let ble_addr_random = + BleAddress::new(0xAABBCCDDEEFF, BleAddressKind::Random); + let result: Result = + TryFrom::try_from(ble_addr_random); + assert!(result.is_err()); + assert!(matches!( + result.unwrap_err(), + BluetoothError::BadTypeConversion(_), + )); + } + + #[test] + fn test_u64_to_6lsb() { + // Test a case where the input number is smaller than 6 bytes + let num = 0x123456; + let expected_result = [0x56, 0x34, 0x12, 0, 0, 0]; + let result = u64_to_6lsb(num); + assert_eq!(result, expected_result); + + // Test a case where the input number is exactly 6 bytes + let num = 0xAABBCCDDEEFF; + let expected_result = [0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA]; + let result = u64_to_6lsb(num); + assert_eq!(result, expected_result); + + // Test a case where the number is too large so the two most significant + // bytes get dropped. + let num = 0x1122334455667788; + let expected_result = [0x88, 0x77, 0x66, 0x55, 0x44, 0x33]; + let result = u64_to_6lsb(num); + assert_eq!(result, expected_result); + + // Test a case where the input number is 0 + let num = 0; + let expected_result = [0, 0, 0, 0, 0, 0]; + let result = u64_to_6lsb(num); + assert_eq!(result, expected_result); + } +} diff --git a/fastpair/rust/bluetooth/src/common/advertisement.rs b/fastpair/rust/bluetooth/src/common/advertisement.rs index 9fb7f999..93413079 100644 --- a/fastpair/rust/bluetooth/src/common/advertisement.rs +++ b/fastpair/rust/bluetooth/src/common/advertisement.rs @@ -96,7 +96,7 @@ pub enum BleDataTypeId { /// Struct representing the Bluetooth Service Data common data type. `U` should /// be one of the valid uuid sizes, specified in: /// Bluetooth Supplement to the Core Specification, Part A, Section 1.11. -#[derive(Clone)] +#[derive(Clone, PartialEq, Eq, Debug)] pub struct ServiceData { uuid: U, data: Vec, @@ -115,3 +115,58 @@ impl ServiceData { &self.data } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::common::BleAddressKind; + + #[test] + fn ble_advertisement_new() { + let address = BleAddress::new(0x112233445566, BleAddressKind::Public); + let ad = BleAdvertisement::new(address, Some(-60), Some(10)); + assert_eq!(ad.address(), address); + assert_eq!(ad.rssi(), Some(-60)); + assert_eq!(ad.tx_power(), Some(10)); + assert!(ad.service_data_16bit_uuid.is_none()); + } + + #[test] + fn ble_advertisement_set_and_get_service_data() { + let address = BleAddress::new(0x112233445566, BleAddressKind::Public); + let mut ad = BleAdvertisement::new(address, Some(-60), Some(10)); + + let service_data = vec![ + ServiceData::new(0x1234, vec![0x01, 0x02, 0x03]), + ServiceData::new(0x5678, vec![0x04, 0x05]), + ]; + + ad.set_service_data_16bit_uuid(service_data.clone()); + + let retrieved_service_data = ad.service_data_16bit_uuid().unwrap(); + assert_eq!(*retrieved_service_data, service_data); + } + + #[test] + fn ble_advertisement_missing_service_data() { + let address = BleAddress::new(0x112233445566, BleAddressKind::Public); + let ad = BleAdvertisement::new(address, Some(-60), Some(10)); + + let result = ad.service_data_16bit_uuid(); + assert!(result.is_err()); + assert!(matches!( + result.unwrap_err(), + BluetoothError::FailedPrecondition(_) + )); + } + + #[test] + fn service_data_new() { + let uuid = 0x1234; + let data = vec![0x01, 0x02, 0x03]; + + let service_data = ServiceData::new(uuid, data.clone()); + assert_eq!(service_data.uuid(), uuid); + assert_eq!(*service_data.data(), data); + } +} diff --git a/fastpair/rust/bluetooth/src/common/error.rs b/fastpair/rust/bluetooth/src/common/error.rs index 8b20b36e..d0d43208 100644 --- a/fastpair/rust/bluetooth/src/common/error.rs +++ b/fastpair/rust/bluetooth/src/common/error.rs @@ -16,7 +16,7 @@ use thiserror::Error; /// Library error type. #[non_exhaustive] -#[derive(Error, Debug)] +#[derive(Error, Debug, PartialEq)] pub enum BluetoothError { /// Reported when the user attempts a bad type conversion, e.g. converting /// a BLE random address to a BT Classic address.