1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 //! MockEventManager 18 19 use crate::event_manager::EventManager; 20 use jni::errors::{Error, JniError, Result}; 21 use log::warn; 22 use std::collections::VecDeque; 23 use std::sync::Mutex; 24 use uwb_uci_packets::{ 25 DeviceStatusNtfPacket, ExtendedMacTwoWayRangeDataNtfPacket, GenericErrorPacket, 26 SessionStatusNtfPacket, SessionUpdateControllerMulticastListNtfPacket, 27 ShortMacTwoWayRangeDataNtfPacket, UciNotificationPacket, 28 }; 29 30 #[cfg(any(test, fuzzing))] 31 enum ExpectedCall { 32 DeviceStatus { out: Result<()> }, 33 CoreGenericError { out: Result<()> }, 34 SessionStatus { out: Result<()> }, 35 ShortRangeData { out: Result<()> }, 36 ExtendedRangeData { out: Result<()> }, 37 SessionUpdateControllerMulticastList { out: Result<()> }, 38 VendorUci { out: Result<()> }, 39 } 40 41 #[cfg(any(test, fuzzing))] 42 #[derive(Default)] 43 pub struct MockEventManager { 44 expected_calls: Mutex<VecDeque<ExpectedCall>>, 45 } 46 47 #[cfg(any(test, fuzzing))] 48 impl MockEventManager { new() -> Self49 pub fn new() -> Self { 50 Default::default() 51 } 52 expect_device_status_notification_received(&mut self, out: Result<()>)53 pub fn expect_device_status_notification_received(&mut self, out: Result<()>) { 54 self.add_expected_call(ExpectedCall::DeviceStatus { out }); 55 } 56 expect_core_generic_error_notification_received(&mut self, out: Result<()>)57 pub fn expect_core_generic_error_notification_received(&mut self, out: Result<()>) { 58 self.add_expected_call(ExpectedCall::CoreGenericError { out }); 59 } 60 expect_session_status_notification_received(&mut self, out: Result<()>)61 pub fn expect_session_status_notification_received(&mut self, out: Result<()>) { 62 self.add_expected_call(ExpectedCall::SessionStatus { out }); 63 } 64 expect_short_range_data_notification_received(&mut self, out: Result<()>)65 pub fn expect_short_range_data_notification_received(&mut self, out: Result<()>) { 66 self.add_expected_call(ExpectedCall::ShortRangeData { out }); 67 } 68 expect_extended_range_data_notification_received(&mut self, out: Result<()>)69 pub fn expect_extended_range_data_notification_received(&mut self, out: Result<()>) { 70 self.add_expected_call(ExpectedCall::ExtendedRangeData { out }); 71 } 72 expect_session_update_controller_multicast_list_notification_received( &mut self, out: Result<()>, )73 pub fn expect_session_update_controller_multicast_list_notification_received( 74 &mut self, 75 out: Result<()>, 76 ) { 77 self.add_expected_call(ExpectedCall::SessionUpdateControllerMulticastList { out }); 78 } 79 expect_vendor_uci_notification_received(&mut self, out: Result<()>)80 pub fn expect_vendor_uci_notification_received(&mut self, out: Result<()>) { 81 self.add_expected_call(ExpectedCall::VendorUci { out }); 82 } 83 add_expected_call(&mut self, call: ExpectedCall)84 fn add_expected_call(&mut self, call: ExpectedCall) { 85 self.expected_calls.lock().unwrap().push_back(call); 86 } 87 unwrap_out(&self, out: Option<Result<()>>, method_name: &str) -> Result<()>88 fn unwrap_out(&self, out: Option<Result<()>>, method_name: &str) -> Result<()> { 89 out.unwrap_or_else(move || { 90 warn!("unpected {:?}() called", method_name); 91 Err(Error::JniCall(JniError::Unknown)) 92 }) 93 } 94 clear_expected_calls(&self)95 pub fn clear_expected_calls(&self) { 96 self.expected_calls.lock().unwrap().clear(); 97 } 98 } 99 100 #[cfg(any(test, fuzzing))] 101 impl Drop for MockEventManager { drop(&mut self)102 fn drop(&mut self) { 103 assert!(self.expected_calls.lock().unwrap().is_empty()); 104 } 105 } 106 #[cfg(any(test, fuzzing))] 107 impl EventManager for MockEventManager { device_status_notification_received(&self, _data: DeviceStatusNtfPacket) -> Result<()>108 fn device_status_notification_received(&self, _data: DeviceStatusNtfPacket) -> Result<()> { 109 let out = { 110 let mut expected_calls = self.expected_calls.lock().unwrap(); 111 match expected_calls.pop_front() { 112 Some(ExpectedCall::DeviceStatus { out }) => Some(out), 113 Some(call) => { 114 expected_calls.push_front(call); 115 None 116 } 117 None => None, 118 } 119 }; 120 121 self.unwrap_out(out, "device_status_notification_received") 122 } 123 core_generic_error_notification_received(&self, _data: GenericErrorPacket) -> Result<()>124 fn core_generic_error_notification_received(&self, _data: GenericErrorPacket) -> Result<()> { 125 let out = { 126 let mut expected_calls = self.expected_calls.lock().unwrap(); 127 match expected_calls.pop_front() { 128 Some(ExpectedCall::CoreGenericError { out }) => Some(out), 129 Some(call) => { 130 expected_calls.push_front(call); 131 None 132 } 133 None => None, 134 } 135 }; 136 137 self.unwrap_out(out, "core_generic_error_notification_received") 138 } 139 session_status_notification_received(&self, _data: SessionStatusNtfPacket) -> Result<()>140 fn session_status_notification_received(&self, _data: SessionStatusNtfPacket) -> Result<()> { 141 let out = { 142 let mut expected_calls = self.expected_calls.lock().unwrap(); 143 match expected_calls.pop_front() { 144 Some(ExpectedCall::SessionStatus { out }) => Some(out), 145 Some(call) => { 146 expected_calls.push_front(call); 147 None 148 } 149 None => None, 150 } 151 }; 152 153 self.unwrap_out(out, "session_status_notification_received") 154 } 155 short_range_data_notification_received( &self, _data: ShortMacTwoWayRangeDataNtfPacket, ) -> Result<()>156 fn short_range_data_notification_received( 157 &self, 158 _data: ShortMacTwoWayRangeDataNtfPacket, 159 ) -> Result<()> { 160 let out = { 161 let mut expected_calls = self.expected_calls.lock().unwrap(); 162 match expected_calls.pop_front() { 163 Some(ExpectedCall::ShortRangeData { out }) => Some(out), 164 Some(call) => { 165 expected_calls.push_front(call); 166 None 167 } 168 None => None, 169 } 170 }; 171 172 self.unwrap_out(out, "short_range_data_notification_received") 173 } extended_range_data_notification_received( &self, _data: ExtendedMacTwoWayRangeDataNtfPacket, ) -> Result<()>174 fn extended_range_data_notification_received( 175 &self, 176 _data: ExtendedMacTwoWayRangeDataNtfPacket, 177 ) -> Result<()> { 178 let out = { 179 let mut expected_calls = self.expected_calls.lock().unwrap(); 180 match expected_calls.pop_front() { 181 Some(ExpectedCall::ExtendedRangeData { out }) => Some(out), 182 Some(call) => { 183 expected_calls.push_front(call); 184 None 185 } 186 None => None, 187 } 188 }; 189 190 self.unwrap_out(out, "extended_range_data_notification_received") 191 } 192 session_update_controller_multicast_list_notification_received( &self, _data: SessionUpdateControllerMulticastListNtfPacket, ) -> Result<()>193 fn session_update_controller_multicast_list_notification_received( 194 &self, 195 _data: SessionUpdateControllerMulticastListNtfPacket, 196 ) -> Result<()> { 197 let out = { 198 let mut expected_calls = self.expected_calls.lock().unwrap(); 199 match expected_calls.pop_front() { 200 Some(ExpectedCall::SessionUpdateControllerMulticastList { out }) => Some(out), 201 Some(call) => { 202 expected_calls.push_front(call); 203 None 204 } 205 None => None, 206 } 207 }; 208 209 self.unwrap_out(out, "session_update_controller_multicast_list_notification_received") 210 } 211 vendor_uci_notification_received(&self, _data: UciNotificationPacket) -> Result<()>212 fn vendor_uci_notification_received(&self, _data: UciNotificationPacket) -> Result<()> { 213 let out = { 214 let mut expected_calls = self.expected_calls.lock().unwrap(); 215 match expected_calls.pop_front() { 216 Some(ExpectedCall::VendorUci { out }) => Some(out), 217 Some(call) => { 218 expected_calls.push_front(call); 219 None 220 } 221 None => None, 222 } 223 }; 224 225 self.unwrap_out(out, "vendor_uci_notification_received") 226 } 227 } 228