use renik::{
BluetoothConnectionParams, BluetoothConnectionPhase, BluetoothConnectionState,
BluetoothDeviceInfo, BluetoothDeviceList, BluetoothSecurityInfo, ConnHandle, Error,
};
#[test]
fn test_bluetooth_device_info_creation() {
let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();
assert!(device.is_valid());
assert_eq!(device.mac_address(), &mac_addr);
assert_eq!(device.device_name(), b"Test Device");
assert_eq!(
device.device_type(),
BluetoothDeviceInfo::DEVICE_TYPE_UNKNOWN
);
assert!(!device.is_paired());
assert!(!device.is_connected());
assert!(!device.is_trusted());
device.set_connection_count(42);
device.set_last_seen(123456);
device.set_last_connected(654321);
assert_eq!(device.connection_count(), 42);
assert_eq!(device.last_seen(), 123456);
assert_eq!(device.last_connected(), 654321);
assert_eq!(device.device_name_len(), b"Test Device".len() as u8);
device.set_pairing_key(b"key").unwrap();
assert_eq!(device.pairing_key_len(), b"key".len() as u8);
assert_eq!(device.vendor_id(), 0);
assert_eq!(device.product_id(), 0);
assert_eq!(device.version(), 0);
}
#[test]
fn test_bluetooth_device_info_name_too_long() {
let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
let long_name =
b"This device name is way too long and exceeds the 32 byte limit for device names";
match BluetoothDeviceInfo::new(&mac_addr, long_name) {
Err(Error::InvalidBluetoothDeviceInfo) => {} _ => panic!("Should have returned InvalidBluetoothDeviceInfo error"),
}
}
#[test]
fn test_bluetooth_device_info_pairing_key() {
let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();
let pairing_key = b"test_key_123";
device.set_pairing_key(pairing_key).unwrap();
assert_eq!(device.pairing_key(), pairing_key);
let long_key = vec![b'x'; 65]; assert!(matches!(
device.set_pairing_key(&long_key),
Err(Error::InvalidBluetoothDeviceInfo)
));
}
#[test]
fn test_bluetooth_device_info_flags() {
let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();
device.add_flag(BluetoothDeviceInfo::FLAG_PAIRED);
assert!(device.is_paired());
assert!(device.has_flag(BluetoothDeviceInfo::FLAG_PAIRED));
device.add_flag(BluetoothDeviceInfo::FLAG_TRUSTED);
assert!(device.is_trusted());
device.add_flag(BluetoothDeviceInfo::FLAG_AUTO_RECONNECT);
assert!(device.supports_auto_reconnect());
device.remove_flag(BluetoothDeviceInfo::FLAG_PAIRED);
assert!(!device.is_paired());
assert!(device.is_trusted()); }
#[test]
fn test_bluetooth_device_info_class_of_device() {
let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Audio Device").unwrap();
let audio_class = [0x04, 0x10, 0x24]; device.set_class_of_device(&audio_class);
assert_eq!(device.class_of_device(), &audio_class);
assert_eq!(device.device_type(), BluetoothDeviceInfo::DEVICE_TYPE_AUDIO);
}
#[test]
fn test_bluetooth_device_list() {
let mut device_list = BluetoothDeviceList::default();
assert!(device_list.is_empty());
assert_eq!(device_list.len(), 0);
let mac_addr1 = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
let device1 = BluetoothDeviceInfo::new(&mac_addr1, b"Device 1").unwrap();
device_list.add_device(device1).unwrap();
assert!(!device_list.is_empty());
assert_eq!(device_list.len(), 1);
let mac_addr2 = [0x98, 0x76, 0x54, 0x32, 0x10, 0xFE];
let device2 = BluetoothDeviceInfo::new(&mac_addr2, b"Device 2").unwrap();
device_list.add_device(device2).unwrap();
assert_eq!(device_list.len(), 2);
let retrieved_device1 = device_list.device(0).unwrap();
assert_eq!(retrieved_device1.device_name(), b"Device 1");
let retrieved_device2 = device_list.device(1).unwrap();
assert_eq!(retrieved_device2.device_name(), b"Device 2");
assert!(matches!(
device_list.device(2),
Err(Error::IndexOutOfBounds)
));
}
#[test]
fn test_bluetooth_device_list_remove() {
let mut device_list = BluetoothDeviceList::default();
for i in 0..3 {
let mac_addr = [0x10 + i, 0x20, 0x30, 0x40, 0x50, 0x60];
let name = format!("Device {}", i);
let device = BluetoothDeviceInfo::new(&mac_addr, name.as_bytes()).unwrap();
device_list.add_device(device).unwrap();
}
assert_eq!(device_list.len(), 3);
device_list.remove_device(1).unwrap();
assert_eq!(device_list.len(), 2);
let device0 = device_list.device(0).unwrap();
assert_eq!(device0.device_name(), b"Device 0");
let device1 = device_list.device(1).unwrap();
assert_eq!(device1.device_name(), b"Device 2");
assert!(matches!(
device_list.remove_device(5),
Err(Error::IndexOutOfBounds)
));
}
#[test]
fn test_bluetooth_device_list_full() {
let mut device_list = BluetoothDeviceList::default();
for i in 0..10 {
let mac_addr = [i as u8, 0x20, 0x30, 0x40, 0x50, 0x60];
let name = format!("Device {}", i);
let device = BluetoothDeviceInfo::new(&mac_addr, name.as_bytes()).unwrap();
device_list.add_device(device).unwrap();
}
assert_eq!(device_list.len(), 10);
let mac_addr = [0xFF, 0x20, 0x30, 0x40, 0x50, 0x60];
let device = BluetoothDeviceInfo::new(&mac_addr, b"Extra Device").unwrap();
assert!(matches!(
device_list.add_device(device),
Err(Error::DeviceListFull)
));
}
#[test]
fn test_conn_handle() {
let handle = ConnHandle::new(0x0001);
assert_eq!(handle.raw(), 0x0001);
let max_handle = ConnHandle::new(0x0EFF);
assert_eq!(max_handle.raw(), 0x0EFF);
let default_handle = ConnHandle::default();
assert_eq!(default_handle.raw(), 0x0000);
let handle_from_u16: ConnHandle = 0x0042.into();
assert_eq!(handle_from_u16.raw(), 0x0042);
let u16_from_handle: u16 = handle_from_u16.into();
assert_eq!(u16_from_handle, 0x0042);
}
#[test]
#[should_panic(expected = "Connection handle must be <= 0x0EFF")]
fn test_conn_handle_invalid() {
let _ = ConnHandle::new(0x0F00); }
#[test]
fn test_bluetooth_connection_phase() {
let default_phase = BluetoothConnectionPhase::default();
assert_eq!(default_phase, BluetoothConnectionPhase::Idle);
assert!(!BluetoothConnectionPhase::Idle.is_connected());
assert!(!BluetoothConnectionPhase::Discovery.is_connected());
assert!(!BluetoothConnectionPhase::Connecting.is_connected());
assert!(BluetoothConnectionPhase::Connected.is_connected());
assert!(BluetoothConnectionPhase::Authenticating.is_connected());
assert!(BluetoothConnectionPhase::Ready.is_connected());
assert!(!BluetoothConnectionPhase::Connected.is_secure());
assert!(!BluetoothConnectionPhase::Authenticating.is_secure());
assert!(BluetoothConnectionPhase::FullyConnected.is_secure());
assert!(BluetoothConnectionPhase::Ready.is_secure());
assert!(!BluetoothConnectionPhase::Connected.is_ready());
assert!(!BluetoothConnectionPhase::FullyConnected.is_ready());
assert!(BluetoothConnectionPhase::Ready.is_ready());
assert!(BluetoothConnectionPhase::Maintaining.is_ready());
}
#[test]
fn test_bluetooth_connection_state_fsm() {
let mut connection_state = BluetoothConnectionState::default();
assert_eq!(
connection_state.connection_phase(),
BluetoothConnectionPhase::Idle
);
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Discovery));
assert_eq!(
connection_state.connection_phase(),
BluetoothConnectionPhase::Discovery
);
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Connecting));
assert_eq!(
connection_state.connection_phase(),
BluetoothConnectionPhase::Connecting
);
assert!(!connection_state.advance_to_phase(BluetoothConnectionPhase::Ready));
assert_eq!(
connection_state.connection_phase(),
BluetoothConnectionPhase::Connecting
);
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Connected));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Authenticating));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::SettingUpEncryption));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::FullyConnected));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Ready));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Idle));
assert_eq!(
connection_state.connection_phase(),
BluetoothConnectionPhase::Idle
);
}
#[test]
fn test_bluetooth_connection_state_error_recovery() {
let mut connection_state = BluetoothConnectionState::default();
connection_state.advance_to_phase(BluetoothConnectionPhase::Connecting);
connection_state.advance_to_phase(BluetoothConnectionPhase::Connected);
connection_state.advance_to_phase(BluetoothConnectionPhase::Authenticating);
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Failed));
assert_eq!(
connection_state.connection_phase(),
BluetoothConnectionPhase::Failed
);
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Reconnecting));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Connecting));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Connected));
}
#[test]
fn test_bluetooth_connection_state_basic_functionality() {
let mut connection_state = BluetoothConnectionState::default();
let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
let device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();
connection_state.set_remote_device(device);
let remote_device = connection_state.remote_device();
assert_eq!(remote_device.device_name(), b"Test Device");
assert!(!connection_state.is_connected());
connection_state.set_connected(true);
assert!(connection_state.is_connected());
assert!(!connection_state.is_authenticated());
connection_state.set_authenticated(true);
assert!(connection_state.is_authenticated());
connection_state.set_link_quality(85);
assert_eq!(connection_state.link_quality(), 85);
let handle = ConnHandle::new(0x0042);
connection_state.set_connection_handle(Some(handle));
assert_eq!(connection_state.connection_handle(), Some(handle));
connection_state.set_connection_handle(None);
assert_eq!(connection_state.connection_handle(), None);
}
#[test]
fn test_bluetooth_connection_params() {
let mut params = BluetoothConnectionParams::default();
assert_eq!(params.connection_handle().raw(), 0);
assert_eq!(params.connection_interval(), 0);
assert_eq!(params.rssi(), -127);
params.set_connection_handle(ConnHandle::new(0x0001));
params.set_connection_interval(24); params.set_connection_latency(0);
params.set_supervision_timeout(200); params.set_rssi(-45);
let connection_interval = params.connection_interval();
assert!(connection_interval <= 3200);
let connection_latency = params.connection_latency();
assert!(connection_latency <= 499);
let supervision_timeout = params.supervision_timeout();
assert!(supervision_timeout <= 3200);
let connected_at = params.connected_at();
assert!(connected_at <= u32::MAX);
let last_activity = params.last_activity();
assert!(last_activity <= u32::MAX);
let rssi = params.rssi();
let _ = rssi;
}
#[test]
fn test_bluetooth_connection_params_comprehensive() {
let mut params = BluetoothConnectionParams::default();
params.set_connection_handle(ConnHandle::new(0x0EFF)); params.set_connection_interval(3200); params.set_connection_latency(499); params.set_supervision_timeout(3200); params.set_master_clock_accuracy(7); params.set_link_type(0x02); params.set_encryption_enabled(0x01); params.set_rssi(127); params.set_connected_at(u32::MAX); params.set_last_activity(u32::MAX);
assert_eq!(params.connection_handle().raw(), 0x0EFF);
assert_eq!(params.connection_interval(), 3200);
assert_eq!(params.connection_latency(), 499);
assert_eq!(params.supervision_timeout(), 3200);
assert_eq!(params.master_clock_accuracy(), 7);
assert_eq!(params.link_type(), 0x02);
assert_eq!(params.encryption_enabled(), 0x01);
assert_eq!(params.rssi(), 127);
assert_eq!(params.connected_at(), u32::MAX);
assert_eq!(params.last_activity(), u32::MAX);
}
#[test]
fn test_bluetooth_security_info() {
let mut security = BluetoothSecurityInfo::default();
assert_eq!(security.security_level(), 1);
assert_eq!(security.authenticated(), 0);
assert_eq!(security.encrypted(), 0);
security.set_link_key([
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10,
]);
security.set_link_key_valid(1);
security.set_authenticated(1);
security.set_encrypted(1);
security.set_security_level(4);
assert_eq!(security.link_key()[0], 0x01);
assert_eq!(security.link_key()[15], 0x10);
assert_eq!(security.authenticated(), 1);
assert_eq!(security.encrypted(), 1);
assert_eq!(security.security_level(), 4);
}
#[test]
fn test_bluetooth_device_info_connection_params_update() {
let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();
let mut params = BluetoothConnectionParams::default();
params.set_connection_handle(ConnHandle::new(0x0042));
params.set_rssi(-50);
device.update_connection_params(¶ms);
let device_params = device.connection_params();
assert_eq!(device_params.connection_handle(), ConnHandle::new(0x0042));
assert_eq!(device_params.rssi(), -50);
assert!(device.is_connected()); }
#[test]
fn test_bluetooth_device_info_security_info_update() {
let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();
let mut security = BluetoothSecurityInfo::default();
security.set_authenticated(1);
security.set_encrypted(1);
device.update_security_info(&security);
let device_security = device.security_info();
assert_eq!(device_security.authenticated(), 1);
assert_eq!(device_security.encrypted(), 1);
assert!(device.is_paired()); }
#[test]
fn test_memory_layout_alignment() {
assert_eq!(core::mem::size_of::<ConnHandle>(), 2);
assert_eq!(core::mem::size_of::<BluetoothConnectionPhase>(), 1);
assert_eq!(core::mem::align_of::<BluetoothDeviceInfo>(), 1);
assert_eq!(core::mem::align_of::<BluetoothDeviceList>(), 1);
assert_eq!(core::mem::align_of::<BluetoothConnectionState>(), 1);
}
#[test]
fn test_bluetooth_fsm_all_valid_transitions() {
let mut connection_state = BluetoothConnectionState::default();
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Discovery));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Connecting));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Connected));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Authenticating));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::SettingUpEncryption));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::FullyConnected));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::ServiceDiscovery));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Ready));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Maintaining));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Disconnecting));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Idle));
}
#[test]
fn test_bluetooth_fsm_invalid_transitions() {
let mut connection_state = BluetoothConnectionState::default();
assert!(!connection_state.advance_to_phase(BluetoothConnectionPhase::Connected)); assert!(!connection_state.advance_to_phase(BluetoothConnectionPhase::Ready));
connection_state.advance_to_phase(BluetoothConnectionPhase::Discovery);
assert!(!connection_state.advance_to_phase(BluetoothConnectionPhase::Connected)); assert!(!connection_state.advance_to_phase(BluetoothConnectionPhase::Ready));
connection_state.advance_to_phase(BluetoothConnectionPhase::Connecting);
assert!(!connection_state.advance_to_phase(BluetoothConnectionPhase::Authenticating)); assert!(!connection_state.advance_to_phase(BluetoothConnectionPhase::Ready)); }
#[test]
fn test_bluetooth_fsm_emergency_transitions() {
let mut connection_state = BluetoothConnectionState::default();
for phase in [
BluetoothConnectionPhase::Discovery,
BluetoothConnectionPhase::Connecting,
BluetoothConnectionPhase::Connected,
BluetoothConnectionPhase::Authenticating,
BluetoothConnectionPhase::SettingUpEncryption,
BluetoothConnectionPhase::FullyConnected,
BluetoothConnectionPhase::ServiceDiscovery,
BluetoothConnectionPhase::Ready,
BluetoothConnectionPhase::Maintaining,
BluetoothConnectionPhase::Reconnecting,
BluetoothConnectionPhase::Failed,
BluetoothConnectionPhase::Disconnecting,
] {
connection_state.set_connection_phase(phase);
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Idle));
assert_eq!(
connection_state.connection_phase(),
BluetoothConnectionPhase::Idle
);
}
}
#[test]
fn test_bluetooth_fsm_alternate_paths() {
let mut connection_state = BluetoothConnectionState::default();
connection_state.advance_to_phase(BluetoothConnectionPhase::Connecting);
connection_state.advance_to_phase(BluetoothConnectionPhase::Connected);
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::ServiceDiscovery));
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Ready));
connection_state.set_connection_phase(BluetoothConnectionPhase::Authenticating);
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Disconnecting));
connection_state.set_connection_phase(BluetoothConnectionPhase::SettingUpEncryption);
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Disconnecting));
connection_state.set_connection_phase(BluetoothConnectionPhase::FullyConnected);
assert!(connection_state.advance_to_phase(BluetoothConnectionPhase::Disconnecting));
}
#[test]
fn test_bluetooth_device_info_comprehensive_flags() {
let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();
let all_flags = [
BluetoothDeviceInfo::FLAG_PAIRED,
BluetoothDeviceInfo::FLAG_TRUSTED,
BluetoothDeviceInfo::FLAG_AUDIO,
BluetoothDeviceInfo::FLAG_INPUT,
BluetoothDeviceInfo::FLAG_FILE_TRANSFER,
BluetoothDeviceInfo::FLAG_CONNECTED,
BluetoothDeviceInfo::FLAG_AUTO_RECONNECT,
BluetoothDeviceInfo::FLAG_RECENTLY_DISCOVERED,
];
for &flag in &all_flags {
device.add_flag(flag);
assert!(device.has_flag(flag));
}
let combined_flags = all_flags.iter().fold(0u8, |acc, &flag| acc | flag);
assert_eq!(device.flags(), combined_flags);
for &flag in &all_flags {
device.remove_flag(flag);
assert!(!device.has_flag(flag));
}
assert_eq!(device.flags(), 0);
}
#[test]
fn test_bluetooth_device_info_all_device_types() {
let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();
let device_types = [
(1, BluetoothDeviceInfo::DEVICE_TYPE_COMPUTER),
(2, BluetoothDeviceInfo::DEVICE_TYPE_PHONE),
(3, BluetoothDeviceInfo::DEVICE_TYPE_NETWORK),
(4, BluetoothDeviceInfo::DEVICE_TYPE_AUDIO),
(5, BluetoothDeviceInfo::DEVICE_TYPE_PERIPHERAL),
(6, BluetoothDeviceInfo::DEVICE_TYPE_IMAGING),
(7, BluetoothDeviceInfo::DEVICE_TYPE_WEARABLE),
(8, BluetoothDeviceInfo::DEVICE_TYPE_TOY),
];
for (major_class, expected_type) in device_types {
let class_bytes = [0x00, (major_class << 2) as u8, 0x00];
device.set_class_of_device(&class_bytes);
assert_eq!(device.device_type(), expected_type);
}
let unknown_class = [0x00, 0xFC, 0x00]; device.set_class_of_device(&unknown_class);
assert_eq!(
device.device_type(),
BluetoothDeviceInfo::DEVICE_TYPE_UNKNOWN
);
}
#[test]
fn test_bluetooth_security_info_comprehensive() {
let mut security = BluetoothSecurityInfo::default();
security.set_link_key([0xFF; 16]); security.set_link_key_type(0x07); security.set_auth_requirements(0xFF); security.set_io_capabilities(0x04); security.set_security_level(0x04); security.set_pin_length(16); security.set_link_key_valid(1);
security.set_authenticated(1);
security.set_encrypted(1);
security.set_ssp_supported(1);
security.set_mitm_required(1);
assert_eq!(security.link_key(), &[0xFF; 16]);
assert_eq!(security.link_key_type(), 0x07);
assert_eq!(security.auth_requirements(), 0xFF);
assert_eq!(security.io_capabilities(), 0x04);
assert_eq!(security.security_level(), 0x04);
assert_eq!(security.pin_length(), 16);
assert_eq!(security.link_key_valid(), 1);
assert_eq!(security.authenticated(), 1);
assert_eq!(security.encrypted(), 1);
assert_eq!(security.ssp_supported(), 1);
assert_eq!(security.mitm_required(), 1);
}
#[test]
fn test_bluetooth_device_info_timestamps() {
let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();
let now = 1234567890u32;
device.update_last_seen(now);
device.update_last_connected(now - 3600); device.set_connection_count(5);
device.increment_connection_count();
assert!(device.is_valid());
assert_eq!(device.device_name(), b"Test Device");
}
#[test]
fn test_conn_handle_edge_cases() {
let min_handle = ConnHandle::new(0x0000);
assert_eq!(min_handle.raw(), 0x0000);
let max_handle = ConnHandle::new(0x0EFF);
assert_eq!(max_handle.raw(), 0x0EFF);
for i in 0..=0x0EFF {
let handle = ConnHandle::new(i);
let converted: u16 = handle.into();
let back_converted = ConnHandle::from(converted);
assert_eq!(handle, back_converted);
assert_eq!(converted, i);
}
}
#[test]
#[should_panic(expected = "Connection handle must be <= 0x0EFF")]
fn test_conn_handle_boundary_panic() {
let _ = ConnHandle::new(0x0F00); }
#[test]
fn test_bluetooth_connection_state_comprehensive() {
let mut connection_state = BluetoothConnectionState::default();
let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
let device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();
connection_state.set_remote_device(device);
connection_state.set_connected(true);
connection_state.set_authenticated(true);
connection_state.set_link_quality(255); connection_state.set_remote_device_address([0xFF; 6]);
connection_state.set_connection_handle(Some(ConnHandle::new(0x0EFF)));
connection_state.set_link_type(0x02); connection_state.set_connection_phase(BluetoothConnectionPhase::Ready);
assert!(connection_state.is_connected());
assert!(connection_state.is_authenticated());
assert_eq!(connection_state.link_quality(), 255);
assert_eq!(connection_state.remote_device_address(), Some([0xFF; 6]));
assert_eq!(
connection_state.connection_handle(),
Some(ConnHandle::new(0x0EFF))
);
assert_eq!(connection_state.link_type(), 0x02);
assert_eq!(
connection_state.connection_phase(),
BluetoothConnectionPhase::Ready
);
connection_state.set_connection_handle(None);
assert_eq!(connection_state.connection_handle(), None);
}
#[test]
fn test_bluetooth_device_info_getters() {
let mac_addr = [0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC];
let mut device = BluetoothDeviceInfo::new(&mac_addr, b"Test Device").unwrap();
assert!(device.is_valid());
assert_eq!(device.mac_address(), &mac_addr);
assert_eq!(device.device_name(), b"Test Device");
assert_eq!(
device.device_type(),
BluetoothDeviceInfo::DEVICE_TYPE_UNKNOWN
);
assert!(!device.is_paired());
assert!(!device.is_connected());
assert!(!device.is_trusted());
device.set_connection_count(42);
device.set_last_seen(123456);
device.set_last_connected(654321);
assert_eq!(device.connection_count(), 42);
assert_eq!(device.last_seen(), 123456);
assert_eq!(device.last_connected(), 654321);
assert_eq!(device.device_name_len(), b"Test Device".len() as u8);
device.set_pairing_key(b"key").unwrap();
assert_eq!(device.pairing_key_len(), b"key".len() as u8);
assert_eq!(device.vendor_id(), 0);
assert_eq!(device.product_id(), 0);
assert_eq!(device.version(), 0);
}