Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | //! Rust Binder crate integration tests |
| 18 | |
Stephen Crane | 7bca105 | 2021-10-25 17:52:51 -0700 | [diff] [blame] | 19 | use binder::{declare_binder_enum, declare_binder_interface}; |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 20 | use binder::{BinderFeatures, Interface, StatusCode, ThreadState}; |
| 21 | // Import from internal API for testing only, do not use this module in |
| 22 | // production. |
| 23 | use binder::binder_impl::{ |
| 24 | Binder, BorrowedParcel, IBinderInternal, TransactionCode, FIRST_CALL_TRANSACTION, |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 25 | }; |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 26 | |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 27 | use std::convert::{TryFrom, TryInto}; |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 28 | use std::ffi::CStr; |
Andrei Homescu | d23c049 | 2023-11-09 01:51:59 +0000 | [diff] [blame] | 29 | use std::io::Write; |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 30 | use std::sync::Mutex; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 31 | |
| 32 | /// Name of service runner. |
| 33 | /// |
| 34 | /// Must match the binary name in Android.bp |
| 35 | const RUST_SERVICE_BINARY: &str = "rustBinderTestService"; |
| 36 | |
| 37 | /// Binary to run a test service. |
| 38 | /// |
| 39 | /// This needs to be in a separate process from the tests, so we spawn this |
| 40 | /// binary as a child, providing the service name as an argument. |
| 41 | fn main() -> Result<(), &'static str> { |
| 42 | // Ensure that we can handle all transactions on the main thread. |
| 43 | binder::ProcessState::set_thread_pool_max_thread_count(0); |
| 44 | binder::ProcessState::start_thread_pool(); |
| 45 | |
| 46 | let mut args = std::env::args().skip(1); |
| 47 | if args.len() < 1 || args.len() > 2 { |
| 48 | print_usage(); |
| 49 | return Err(""); |
| 50 | } |
| 51 | let service_name = args.next().ok_or_else(|| { |
| 52 | print_usage(); |
| 53 | "Missing SERVICE_NAME argument" |
| 54 | })?; |
| 55 | let extension_name = args.next(); |
| 56 | |
| 57 | { |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 58 | let mut service = Binder::new(BnTest(Box::new(TestService::new(&service_name)))); |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 59 | service.set_requesting_sid(true); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 60 | if let Some(extension_name) = extension_name { |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 61 | let extension = |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 62 | BnTest::new_binder(TestService::new(&extension_name), BinderFeatures::default()); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 63 | service.set_extension(&mut extension.as_binder()).expect("Could not add extension"); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 64 | } |
| 65 | binder::add_service(&service_name, service.as_binder()) |
| 66 | .expect("Could not register service"); |
| 67 | } |
| 68 | |
| 69 | binder::ProcessState::join_thread_pool(); |
| 70 | Err("Unexpected exit after join_thread_pool") |
| 71 | } |
| 72 | |
| 73 | fn print_usage() { |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 74 | eprintln!("Usage: {} SERVICE_NAME [EXTENSION_NAME]", RUST_SERVICE_BINARY); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 75 | eprintln!(concat!( |
| 76 | "Spawn a Binder test service identified by SERVICE_NAME,", |
| 77 | " optionally with an extesion named EXTENSION_NAME", |
| 78 | )); |
| 79 | } |
| 80 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 81 | struct TestService { |
| 82 | s: String, |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 83 | dump_args: Mutex<Vec<String>>, |
| 84 | } |
| 85 | |
| 86 | impl TestService { |
| 87 | fn new(s: &str) -> Self { |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 88 | Self { s: s.to_string(), dump_args: Mutex::new(Vec::new()) } |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 89 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 92 | #[repr(u32)] |
| 93 | enum TestTransactionCode { |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 94 | Test = FIRST_CALL_TRANSACTION, |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 95 | GetDumpArgs, |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 96 | GetSelinuxContext, |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 97 | GetIsHandlingTransaction, |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | impl TryFrom<u32> for TestTransactionCode { |
| 101 | type Error = StatusCode; |
| 102 | |
| 103 | fn try_from(c: u32) -> Result<Self, Self::Error> { |
| 104 | match c { |
| 105 | _ if c == TestTransactionCode::Test as u32 => Ok(TestTransactionCode::Test), |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 106 | _ if c == TestTransactionCode::GetDumpArgs as u32 => { |
| 107 | Ok(TestTransactionCode::GetDumpArgs) |
| 108 | } |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 109 | _ if c == TestTransactionCode::GetSelinuxContext as u32 => { |
| 110 | Ok(TestTransactionCode::GetSelinuxContext) |
| 111 | } |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 112 | _ if c == TestTransactionCode::GetIsHandlingTransaction as u32 => { |
| 113 | Ok(TestTransactionCode::GetIsHandlingTransaction) |
| 114 | } |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 115 | _ => Err(StatusCode::UNKNOWN_TRANSACTION), |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 120 | impl Interface for TestService { |
Andrei Homescu | d23c049 | 2023-11-09 01:51:59 +0000 | [diff] [blame] | 121 | fn dump(&self, _writer: &mut dyn Write, args: &[&CStr]) -> Result<(), StatusCode> { |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 122 | let mut dump_args = self.dump_args.lock().unwrap(); |
| 123 | dump_args.extend(args.iter().map(|s| s.to_str().unwrap().to_owned())); |
| 124 | Ok(()) |
| 125 | } |
| 126 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 127 | |
| 128 | impl ITest for TestService { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 129 | fn test(&self) -> Result<String, StatusCode> { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 130 | Ok(self.s.clone()) |
| 131 | } |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 132 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 133 | fn get_dump_args(&self) -> Result<Vec<String>, StatusCode> { |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 134 | let args = self.dump_args.lock().unwrap().clone(); |
| 135 | Ok(args) |
| 136 | } |
| 137 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 138 | fn get_selinux_context(&self) -> Result<String, StatusCode> { |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 139 | let sid = |
| 140 | ThreadState::with_calling_sid(|sid| sid.map(|s| s.to_string_lossy().into_owned())); |
| 141 | sid.ok_or(StatusCode::UNEXPECTED_NULL) |
| 142 | } |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 143 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 144 | fn get_is_handling_transaction(&self) -> Result<bool, StatusCode> { |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 145 | Ok(binder::is_handling_transaction()) |
| 146 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /// Trivial testing binder interface |
| 150 | pub trait ITest: Interface { |
| 151 | /// Returns a test string |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 152 | fn test(&self) -> Result<String, StatusCode>; |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 153 | |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 154 | /// Return the arguments sent via dump |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 155 | fn get_dump_args(&self) -> Result<Vec<String>, StatusCode>; |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 156 | |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 157 | /// Returns the caller's SELinux context |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 158 | fn get_selinux_context(&self) -> Result<String, StatusCode>; |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 159 | |
| 160 | /// Returns the value of calling `is_handling_transaction`. |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 161 | fn get_is_handling_transaction(&self) -> Result<bool, StatusCode>; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 164 | /// Async trivial testing binder interface |
| 165 | pub trait IATest<P>: Interface { |
| 166 | /// Returns a test string |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 167 | fn test(&self) -> binder::BoxFuture<'static, Result<String, StatusCode>>; |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 168 | |
| 169 | /// Return the arguments sent via dump |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 170 | fn get_dump_args(&self) -> binder::BoxFuture<'static, Result<Vec<String>, StatusCode>>; |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 171 | |
| 172 | /// Returns the caller's SELinux context |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 173 | fn get_selinux_context(&self) -> binder::BoxFuture<'static, Result<String, StatusCode>>; |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 174 | |
| 175 | /// Returns the value of calling `is_handling_transaction`. |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 176 | fn get_is_handling_transaction(&self) -> binder::BoxFuture<'static, Result<bool, StatusCode>>; |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 179 | declare_binder_interface! { |
| 180 | ITest["android.os.ITest"] { |
| 181 | native: BnTest(on_transact), |
| 182 | proxy: BpTest { |
| 183 | x: i32 = 100 |
| 184 | }, |
Alice Ryhl | 0ae829e | 2024-05-21 14:21:22 +0000 | [diff] [blame] | 185 | async: IATest(try_into_local_async), |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | fn on_transact( |
| 190 | service: &dyn ITest, |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 191 | code: TransactionCode, |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 192 | _data: &BorrowedParcel<'_>, |
| 193 | reply: &mut BorrowedParcel<'_>, |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 194 | ) -> Result<(), StatusCode> { |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 195 | match code.try_into()? { |
| 196 | TestTransactionCode::Test => reply.write(&service.test()?), |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 197 | TestTransactionCode::GetDumpArgs => reply.write(&service.get_dump_args()?), |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 198 | TestTransactionCode::GetSelinuxContext => reply.write(&service.get_selinux_context()?), |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 199 | TestTransactionCode::GetIsHandlingTransaction => { |
| 200 | reply.write(&service.get_is_handling_transaction()?) |
| 201 | } |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 202 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | impl ITest for BpTest { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 206 | fn test(&self) -> Result<String, StatusCode> { |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 207 | let reply = |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 208 | self.binder.transact(TestTransactionCode::Test as TransactionCode, 0, |_| Ok(()))?; |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 209 | reply.read() |
| 210 | } |
| 211 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 212 | fn get_dump_args(&self) -> Result<Vec<String>, StatusCode> { |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 213 | let reply = |
| 214 | self.binder |
| 215 | .transact(TestTransactionCode::GetDumpArgs as TransactionCode, 0, |_| Ok(()))?; |
| 216 | reply.read() |
| 217 | } |
| 218 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 219 | fn get_selinux_context(&self) -> Result<String, StatusCode> { |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 220 | let reply = self.binder.transact( |
| 221 | TestTransactionCode::GetSelinuxContext as TransactionCode, |
| 222 | 0, |
| 223 | |_| Ok(()), |
| 224 | )?; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 225 | reply.read() |
| 226 | } |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 227 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 228 | fn get_is_handling_transaction(&self) -> Result<bool, StatusCode> { |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 229 | let reply = self.binder.transact( |
| 230 | TestTransactionCode::GetIsHandlingTransaction as TransactionCode, |
| 231 | 0, |
| 232 | |_| Ok(()), |
| 233 | )?; |
| 234 | reply.read() |
| 235 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 238 | impl<P: binder::BinderAsyncPool> IATest<P> for BpTest { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 239 | fn test(&self) -> binder::BoxFuture<'static, Result<String, StatusCode>> { |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 240 | let binder = self.binder.clone(); |
| 241 | P::spawn( |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 242 | move || binder.transact(TestTransactionCode::Test as TransactionCode, 0, |_| Ok(())), |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 243 | |reply| async move { reply?.read() }, |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 244 | ) |
| 245 | } |
| 246 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 247 | fn get_dump_args(&self) -> binder::BoxFuture<'static, Result<Vec<String>, StatusCode>> { |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 248 | let binder = self.binder.clone(); |
| 249 | P::spawn( |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 250 | move || { |
| 251 | binder.transact(TestTransactionCode::GetDumpArgs as TransactionCode, 0, |_| Ok(())) |
| 252 | }, |
| 253 | |reply| async move { reply?.read() }, |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 254 | ) |
| 255 | } |
| 256 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 257 | fn get_selinux_context(&self) -> binder::BoxFuture<'static, Result<String, StatusCode>> { |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 258 | let binder = self.binder.clone(); |
| 259 | P::spawn( |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 260 | move || { |
| 261 | binder.transact( |
| 262 | TestTransactionCode::GetSelinuxContext as TransactionCode, |
| 263 | 0, |
| 264 | |_| Ok(()), |
| 265 | ) |
| 266 | }, |
| 267 | |reply| async move { reply?.read() }, |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 268 | ) |
| 269 | } |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 270 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 271 | fn get_is_handling_transaction(&self) -> binder::BoxFuture<'static, Result<bool, StatusCode>> { |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 272 | let binder = self.binder.clone(); |
| 273 | P::spawn( |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 274 | move || { |
| 275 | binder.transact( |
| 276 | TestTransactionCode::GetIsHandlingTransaction as TransactionCode, |
| 277 | 0, |
| 278 | |_| Ok(()), |
| 279 | ) |
| 280 | }, |
| 281 | |reply| async move { reply?.read() }, |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 282 | ) |
| 283 | } |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 286 | impl ITest for Binder<BnTest> { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 287 | fn test(&self) -> Result<String, StatusCode> { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 288 | self.0.test() |
| 289 | } |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 290 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 291 | fn get_dump_args(&self) -> Result<Vec<String>, StatusCode> { |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 292 | self.0.get_dump_args() |
| 293 | } |
| 294 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 295 | fn get_selinux_context(&self) -> Result<String, StatusCode> { |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 296 | self.0.get_selinux_context() |
| 297 | } |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 298 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 299 | fn get_is_handling_transaction(&self) -> Result<bool, StatusCode> { |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 300 | self.0.get_is_handling_transaction() |
| 301 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 304 | impl<P: binder::BinderAsyncPool> IATest<P> for Binder<BnTest> { |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 305 | fn test(&self) -> binder::BoxFuture<'static, Result<String, StatusCode>> { |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 306 | let res = self.0.test(); |
| 307 | Box::pin(async move { res }) |
| 308 | } |
| 309 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 310 | fn get_dump_args(&self) -> binder::BoxFuture<'static, Result<Vec<String>, StatusCode>> { |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 311 | let res = self.0.get_dump_args(); |
| 312 | Box::pin(async move { res }) |
| 313 | } |
| 314 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 315 | fn get_selinux_context(&self) -> binder::BoxFuture<'static, Result<String, StatusCode>> { |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 316 | let res = self.0.get_selinux_context(); |
| 317 | Box::pin(async move { res }) |
| 318 | } |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 319 | |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 320 | fn get_is_handling_transaction(&self) -> binder::BoxFuture<'static, Result<bool, StatusCode>> { |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 321 | let res = self.0.get_is_handling_transaction(); |
| 322 | Box::pin(async move { res }) |
| 323 | } |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 324 | } |
| 325 | |
Alice Ryhl | 0ae829e | 2024-05-21 14:21:22 +0000 | [diff] [blame] | 326 | impl BnTest { |
| 327 | fn try_into_local_async<P: binder::BinderAsyncPool + 'static>( |
| 328 | me: Binder<BnTest>, |
| 329 | ) -> Option<binder::Strong<dyn IATest<P>>> { |
| 330 | Some(binder::Strong::new(Box::new(me) as _)) |
| 331 | } |
| 332 | } |
| 333 | |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 334 | /// Trivial testing binder interface |
| 335 | pub trait ITestSameDescriptor: Interface {} |
| 336 | |
| 337 | declare_binder_interface! { |
| 338 | ITestSameDescriptor["android.os.ITest"] { |
| 339 | native: BnTestSameDescriptor(on_transact_same_descriptor), |
| 340 | proxy: BpTestSameDescriptor, |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | fn on_transact_same_descriptor( |
| 345 | _service: &dyn ITestSameDescriptor, |
| 346 | _code: TransactionCode, |
Alice Ryhl | 8618c48 | 2021-11-09 15:35:35 +0000 | [diff] [blame] | 347 | _data: &BorrowedParcel<'_>, |
| 348 | _reply: &mut BorrowedParcel<'_>, |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 349 | ) -> Result<(), StatusCode> { |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 350 | Ok(()) |
| 351 | } |
| 352 | |
| 353 | impl ITestSameDescriptor for BpTestSameDescriptor {} |
| 354 | |
| 355 | impl ITestSameDescriptor for Binder<BnTestSameDescriptor> {} |
| 356 | |
Stephen Crane | 7bca105 | 2021-10-25 17:52:51 -0700 | [diff] [blame] | 357 | declare_binder_enum! { |
| 358 | TestEnum : [i32; 3] { |
| 359 | FOO = 1, |
| 360 | BAR = 2, |
| 361 | BAZ = 3, |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | declare_binder_enum! { |
| 366 | #[deprecated(since = "1.0.0")] |
| 367 | TestDeprecatedEnum : [i32; 3] { |
| 368 | FOO = 1, |
| 369 | BAR = 2, |
| 370 | BAZ = 3, |
| 371 | } |
| 372 | } |
| 373 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 374 | #[cfg(test)] |
| 375 | mod tests { |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 376 | use selinux_bindgen as selinux_sys; |
| 377 | use std::ffi::CStr; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 378 | use std::fs::File; |
| 379 | use std::process::{Child, Command}; |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 380 | use std::ptr; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 381 | use std::sync::atomic::{AtomicBool, Ordering}; |
| 382 | use std::sync::Arc; |
| 383 | use std::thread; |
| 384 | use std::time::Duration; |
| 385 | |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 386 | use binder::{ |
Devin Moore | 7860d40 | 2024-11-15 23:25:59 +0000 | [diff] [blame] | 387 | Accessor, AccessorProvider, BinderFeatures, DeathRecipient, FromIBinder, IBinder, |
| 388 | Interface, SpIBinder, StatusCode, Strong, |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 389 | }; |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 390 | // Import from impl API for testing only, should not be necessary as long as |
| 391 | // you are using AIDL. |
| 392 | use binder::binder_impl::{Binder, IBinderInternal, TransactionCode}; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 393 | |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 394 | use binder_tokio::Tokio; |
| 395 | |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 396 | use super::{BnTest, IATest, ITest, ITestSameDescriptor, TestService, RUST_SERVICE_BINARY}; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 397 | |
| 398 | pub struct ScopedServiceProcess(Child); |
| 399 | |
| 400 | impl ScopedServiceProcess { |
| 401 | pub fn new(identifier: &str) -> Self { |
| 402 | Self::new_internal(identifier, None) |
| 403 | } |
| 404 | |
| 405 | pub fn new_with_extension(identifier: &str, extension: &str) -> Self { |
| 406 | Self::new_internal(identifier, Some(extension)) |
| 407 | } |
| 408 | |
| 409 | fn new_internal(identifier: &str, extension: Option<&str>) -> Self { |
| 410 | let mut binary_path = |
| 411 | std::env::current_exe().expect("Could not retrieve current executable path"); |
| 412 | binary_path.pop(); |
| 413 | binary_path.push(RUST_SERVICE_BINARY); |
| 414 | let mut command = Command::new(&binary_path); |
| 415 | command.arg(identifier); |
| 416 | if let Some(ext) = extension { |
| 417 | command.arg(ext); |
| 418 | } |
| 419 | let child = command.spawn().expect("Could not start service"); |
| 420 | Self(child) |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | impl Drop for ScopedServiceProcess { |
| 425 | fn drop(&mut self) { |
| 426 | self.0.kill().expect("Could not kill child process"); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 427 | self.0.wait().expect("Could not wait for child process to die"); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | #[test] |
Frederick Mayle | 68f34bf | 2024-05-08 11:04:10 -0700 | [diff] [blame] | 432 | fn check_get_service() { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 433 | let mut sm = binder::get_service("manager").expect("Did not get manager binder service"); |
| 434 | assert!(sm.is_binder_alive()); |
| 435 | assert!(sm.ping_binder().is_ok()); |
| 436 | |
| 437 | assert!(binder::get_service("this_service_does_not_exist").is_none()); |
| 438 | assert_eq!( |
| 439 | binder::get_interface::<dyn ITest>("this_service_does_not_exist").err(), |
| 440 | Some(StatusCode::NAME_NOT_FOUND) |
| 441 | ); |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 442 | assert_eq!( |
| 443 | binder::get_interface::<dyn IATest<Tokio>>("this_service_does_not_exist").err(), |
| 444 | Some(StatusCode::NAME_NOT_FOUND) |
| 445 | ); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 446 | |
| 447 | // The service manager service isn't an ITest, so this must fail. |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 448 | assert_eq!(binder::get_interface::<dyn ITest>("manager").err(), Some(StatusCode::BAD_TYPE)); |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 449 | assert_eq!( |
| 450 | binder::get_interface::<dyn IATest<Tokio>>("manager").err(), |
| 451 | Some(StatusCode::BAD_TYPE) |
| 452 | ); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Alice Ryhl | 929804f | 2021-11-02 12:04:39 +0000 | [diff] [blame] | 455 | #[tokio::test] |
Frederick Mayle | 68f34bf | 2024-05-08 11:04:10 -0700 | [diff] [blame] | 456 | async fn check_get_service_async() { |
Alice Ryhl | 929804f | 2021-11-02 12:04:39 +0000 | [diff] [blame] | 457 | let mut sm = binder::get_service("manager").expect("Did not get manager binder service"); |
| 458 | assert!(sm.is_binder_alive()); |
| 459 | assert!(sm.ping_binder().is_ok()); |
| 460 | |
| 461 | assert!(binder::get_service("this_service_does_not_exist").is_none()); |
| 462 | assert_eq!( |
| 463 | binder_tokio::get_interface::<dyn ITest>("this_service_does_not_exist").await.err(), |
| 464 | Some(StatusCode::NAME_NOT_FOUND) |
| 465 | ); |
| 466 | assert_eq!( |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 467 | binder_tokio::get_interface::<dyn IATest<Tokio>>("this_service_does_not_exist") |
| 468 | .await |
| 469 | .err(), |
Alice Ryhl | 929804f | 2021-11-02 12:04:39 +0000 | [diff] [blame] | 470 | Some(StatusCode::NAME_NOT_FOUND) |
| 471 | ); |
| 472 | |
| 473 | // The service manager service isn't an ITest, so this must fail. |
| 474 | assert_eq!( |
| 475 | binder_tokio::get_interface::<dyn ITest>("manager").await.err(), |
| 476 | Some(StatusCode::BAD_TYPE) |
| 477 | ); |
| 478 | assert_eq!( |
| 479 | binder_tokio::get_interface::<dyn IATest<Tokio>>("manager").await.err(), |
| 480 | Some(StatusCode::BAD_TYPE) |
| 481 | ); |
| 482 | } |
| 483 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 484 | #[test] |
Frederick Mayle | 68f34bf | 2024-05-08 11:04:10 -0700 | [diff] [blame] | 485 | fn check_check_service() { |
| 486 | let mut sm = binder::check_service("manager").expect("Did not find manager binder service"); |
| 487 | assert!(sm.is_binder_alive()); |
| 488 | assert!(sm.ping_binder().is_ok()); |
| 489 | |
| 490 | assert!(binder::check_service("this_service_does_not_exist").is_none()); |
| 491 | assert_eq!( |
| 492 | binder::check_interface::<dyn ITest>("this_service_does_not_exist").err(), |
| 493 | Some(StatusCode::NAME_NOT_FOUND) |
| 494 | ); |
| 495 | assert_eq!( |
| 496 | binder::check_interface::<dyn IATest<Tokio>>("this_service_does_not_exist").err(), |
| 497 | Some(StatusCode::NAME_NOT_FOUND) |
| 498 | ); |
| 499 | |
| 500 | // The service manager service isn't an ITest, so this must fail. |
| 501 | assert_eq!( |
| 502 | binder::check_interface::<dyn ITest>("manager").err(), |
| 503 | Some(StatusCode::BAD_TYPE) |
| 504 | ); |
| 505 | assert_eq!( |
| 506 | binder::check_interface::<dyn IATest<Tokio>>("manager").err(), |
| 507 | Some(StatusCode::BAD_TYPE) |
| 508 | ); |
| 509 | } |
| 510 | |
| 511 | #[tokio::test] |
| 512 | async fn check_check_service_async() { |
| 513 | let mut sm = binder::check_service("manager").expect("Did not find manager binder service"); |
| 514 | assert!(sm.is_binder_alive()); |
| 515 | assert!(sm.ping_binder().is_ok()); |
| 516 | |
| 517 | assert!(binder::check_service("this_service_does_not_exist").is_none()); |
| 518 | assert_eq!( |
| 519 | binder_tokio::check_interface::<dyn ITest>("this_service_does_not_exist").await.err(), |
| 520 | Some(StatusCode::NAME_NOT_FOUND) |
| 521 | ); |
| 522 | assert_eq!( |
| 523 | binder_tokio::check_interface::<dyn IATest<Tokio>>("this_service_does_not_exist") |
| 524 | .await |
| 525 | .err(), |
| 526 | Some(StatusCode::NAME_NOT_FOUND) |
| 527 | ); |
| 528 | |
| 529 | // The service manager service isn't an ITest, so this must fail. |
| 530 | assert_eq!( |
| 531 | binder_tokio::check_interface::<dyn ITest>("manager").await.err(), |
| 532 | Some(StatusCode::BAD_TYPE) |
| 533 | ); |
| 534 | assert_eq!( |
| 535 | binder_tokio::check_interface::<dyn IATest<Tokio>>("manager").await.err(), |
| 536 | Some(StatusCode::BAD_TYPE) |
| 537 | ); |
| 538 | } |
| 539 | |
| 540 | #[test] |
Andrew Walbran | c3ce5c3 | 2021-06-03 16:15:56 +0000 | [diff] [blame] | 541 | fn check_wait_for_service() { |
| 542 | let mut sm = |
| 543 | binder::wait_for_service("manager").expect("Did not get manager binder service"); |
| 544 | assert!(sm.is_binder_alive()); |
| 545 | assert!(sm.ping_binder().is_ok()); |
| 546 | |
| 547 | // The service manager service isn't an ITest, so this must fail. |
| 548 | assert_eq!( |
| 549 | binder::wait_for_interface::<dyn ITest>("manager").err(), |
| 550 | Some(StatusCode::BAD_TYPE) |
| 551 | ); |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 552 | assert_eq!( |
| 553 | binder::wait_for_interface::<dyn IATest<Tokio>>("manager").err(), |
| 554 | Some(StatusCode::BAD_TYPE) |
| 555 | ); |
Andrew Walbran | c3ce5c3 | 2021-06-03 16:15:56 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | #[test] |
Stephen Crane | 098bbc9 | 2022-02-14 13:31:53 -0800 | [diff] [blame] | 559 | fn get_declared_instances() { |
| 560 | // At the time of writing this test, there is no good VINTF interface |
| 561 | // guaranteed to be on all devices. Cuttlefish has light, so this will |
| 562 | // generally test things. |
| 563 | let has_lights = binder::is_declared("android.hardware.light.ILights/default") |
| 564 | .expect("Could not check for declared interface"); |
| 565 | |
| 566 | let instances = binder::get_declared_instances("android.hardware.light.ILights") |
| 567 | .expect("Could not get declared instances"); |
| 568 | |
Chris Wailes | 4247166 | 2022-11-16 15:41:38 -0800 | [diff] [blame] | 569 | let expected_defaults = usize::from(has_lights); |
Stephen Crane | 098bbc9 | 2022-02-14 13:31:53 -0800 | [diff] [blame] | 570 | assert_eq!(expected_defaults, instances.iter().filter(|i| i.as_str() == "default").count()); |
| 571 | } |
| 572 | |
| 573 | #[test] |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 574 | fn trivial_client() { |
| 575 | let service_name = "trivial_client_test"; |
| 576 | let _process = ScopedServiceProcess::new(service_name); |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 577 | let test_client: Strong<dyn ITest> = |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 578 | binder::get_interface(service_name).expect("Did not get manager binder service"); |
| 579 | assert_eq!(test_client.test().unwrap(), "trivial_client_test"); |
| 580 | } |
| 581 | |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 582 | #[tokio::test] |
| 583 | async fn trivial_client_async() { |
| 584 | let service_name = "trivial_client_test"; |
| 585 | let _process = ScopedServiceProcess::new(service_name); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 586 | let test_client: Strong<dyn IATest<Tokio>> = binder_tokio::get_interface(service_name) |
| 587 | .await |
| 588 | .expect("Did not get manager binder service"); |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 589 | assert_eq!(test_client.test().await.unwrap(), "trivial_client_test"); |
| 590 | } |
| 591 | |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 592 | #[test] |
Andrew Walbran | c3ce5c3 | 2021-06-03 16:15:56 +0000 | [diff] [blame] | 593 | fn wait_for_trivial_client() { |
| 594 | let service_name = "wait_for_trivial_client_test"; |
| 595 | let _process = ScopedServiceProcess::new(service_name); |
| 596 | let test_client: Strong<dyn ITest> = |
| 597 | binder::wait_for_interface(service_name).expect("Did not get manager binder service"); |
| 598 | assert_eq!(test_client.test().unwrap(), "wait_for_trivial_client_test"); |
| 599 | } |
| 600 | |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 601 | #[tokio::test] |
| 602 | async fn wait_for_trivial_client_async() { |
| 603 | let service_name = "wait_for_trivial_client_test"; |
| 604 | let _process = ScopedServiceProcess::new(service_name); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 605 | let test_client: Strong<dyn IATest<Tokio>> = binder_tokio::wait_for_interface(service_name) |
| 606 | .await |
| 607 | .expect("Did not get manager binder service"); |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 608 | assert_eq!(test_client.test().await.unwrap(), "wait_for_trivial_client_test"); |
| 609 | } |
| 610 | |
| 611 | fn get_expected_selinux_context() -> &'static str { |
Andrew Walbran | 4ed9d77 | 2023-07-21 18:21:05 +0100 | [diff] [blame] | 612 | // SAFETY: The pointer we pass to `getcon` is valid because it comes from a reference, and |
| 613 | // `getcon` doesn't retain it after it returns. If `getcon` succeeds then `out_ptr` will |
| 614 | // point to a valid C string, otherwise it will remain null. We check for null, so the |
| 615 | // pointer we pass to `CStr::from_ptr` must be a valid pointer to a C string. There is a |
| 616 | // memory leak as we don't call `freecon`, but that's fine because this is just a test. |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 617 | unsafe { |
| 618 | let mut out_ptr = ptr::null_mut(); |
| 619 | assert_eq!(selinux_sys::getcon(&mut out_ptr), 0); |
| 620 | assert!(!out_ptr.is_null()); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 621 | CStr::from_ptr(out_ptr).to_str().expect("context was invalid UTF-8") |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
Andrew Walbran | c3ce5c3 | 2021-06-03 16:15:56 +0000 | [diff] [blame] | 625 | #[test] |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 626 | fn get_selinux_context() { |
| 627 | let service_name = "get_selinux_context"; |
| 628 | let _process = ScopedServiceProcess::new(service_name); |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 629 | let test_client: Strong<dyn ITest> = |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 630 | binder::get_interface(service_name).expect("Did not get manager binder service"); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 631 | assert_eq!(test_client.get_selinux_context().unwrap(), get_expected_selinux_context()); |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | #[tokio::test] |
| 635 | async fn get_selinux_context_async() { |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 636 | let service_name = "get_selinux_context_async"; |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 637 | let _process = ScopedServiceProcess::new(service_name); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 638 | let test_client: Strong<dyn IATest<Tokio>> = binder_tokio::get_interface(service_name) |
| 639 | .await |
| 640 | .expect("Did not get manager binder service"); |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 641 | assert_eq!( |
| 642 | test_client.get_selinux_context().await.unwrap(), |
| 643 | get_expected_selinux_context() |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 644 | ); |
| 645 | } |
| 646 | |
Alice Ryhl | c173684 | 2021-11-23 12:38:51 +0000 | [diff] [blame] | 647 | #[tokio::test] |
| 648 | async fn get_selinux_context_sync_to_async() { |
| 649 | let service_name = "get_selinux_context"; |
| 650 | let _process = ScopedServiceProcess::new(service_name); |
| 651 | let test_client: Strong<dyn ITest> = |
| 652 | binder::get_interface(service_name).expect("Did not get manager binder service"); |
| 653 | let test_client = test_client.into_async::<Tokio>(); |
| 654 | assert_eq!( |
| 655 | test_client.get_selinux_context().await.unwrap(), |
| 656 | get_expected_selinux_context() |
| 657 | ); |
| 658 | } |
| 659 | |
| 660 | #[tokio::test] |
| 661 | async fn get_selinux_context_async_to_sync() { |
| 662 | let service_name = "get_selinux_context"; |
| 663 | let _process = ScopedServiceProcess::new(service_name); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 664 | let test_client: Strong<dyn IATest<Tokio>> = binder_tokio::get_interface(service_name) |
| 665 | .await |
| 666 | .expect("Did not get manager binder service"); |
Alice Ryhl | c173684 | 2021-11-23 12:38:51 +0000 | [diff] [blame] | 667 | let test_client = test_client.into_sync(); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 668 | assert_eq!(test_client.get_selinux_context().unwrap(), get_expected_selinux_context()); |
Alice Ryhl | c173684 | 2021-11-23 12:38:51 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 671 | struct Bools { |
| 672 | binder_died: Arc<AtomicBool>, |
| 673 | binder_dealloc: Arc<AtomicBool>, |
| 674 | } |
| 675 | |
| 676 | impl Bools { |
| 677 | fn is_dead(&self) -> bool { |
| 678 | self.binder_died.load(Ordering::Relaxed) |
| 679 | } |
| 680 | fn assert_died(&self) { |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 681 | assert!(self.is_dead(), "Did not receive death notification"); |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 682 | } |
| 683 | fn assert_dropped(&self) { |
| 684 | assert!( |
| 685 | self.binder_dealloc.load(Ordering::Relaxed), |
| 686 | "Did not dealloc death notification" |
| 687 | ); |
| 688 | } |
| 689 | fn assert_not_dropped(&self) { |
| 690 | assert!( |
| 691 | !self.binder_dealloc.load(Ordering::Relaxed), |
| 692 | "Dealloc death notification too early" |
| 693 | ); |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | fn register_death_notification(binder: &mut SpIBinder) -> (Bools, DeathRecipient) { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 698 | let binder_died = Arc::new(AtomicBool::new(false)); |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 699 | let binder_dealloc = Arc::new(AtomicBool::new(false)); |
| 700 | |
| 701 | struct SetOnDrop { |
| 702 | binder_dealloc: Arc<AtomicBool>, |
| 703 | } |
| 704 | impl Drop for SetOnDrop { |
| 705 | fn drop(&mut self) { |
| 706 | self.binder_dealloc.store(true, Ordering::Relaxed); |
| 707 | } |
| 708 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 709 | |
| 710 | let mut death_recipient = { |
| 711 | let flag = binder_died.clone(); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 712 | let set_on_drop = SetOnDrop { binder_dealloc: binder_dealloc.clone() }; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 713 | DeathRecipient::new(move || { |
| 714 | flag.store(true, Ordering::Relaxed); |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 715 | // Force the closure to take ownership of set_on_drop. When the closure is |
| 716 | // dropped, the destructor of `set_on_drop` will run. |
| 717 | let _ = &set_on_drop; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 718 | }) |
| 719 | }; |
| 720 | |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 721 | binder.link_to_death(&mut death_recipient).expect("link_to_death failed"); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 722 | |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 723 | let bools = Bools { binder_died, binder_dealloc }; |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 724 | |
| 725 | (bools, death_recipient) |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | /// Killing a remote service should unregister the service and trigger |
| 729 | /// death notifications. |
| 730 | #[test] |
| 731 | fn test_death_notifications() { |
| 732 | binder::ProcessState::start_thread_pool(); |
| 733 | |
| 734 | let service_name = "test_death_notifications"; |
| 735 | let service_process = ScopedServiceProcess::new(service_name); |
| 736 | let mut remote = binder::get_service(service_name).expect("Could not retrieve service"); |
| 737 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 738 | let (bools, recipient) = register_death_notification(&mut remote); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 739 | |
| 740 | drop(service_process); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 741 | remote.ping_binder().expect_err("Service should have died already"); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 742 | |
| 743 | // Pause to ensure any death notifications get delivered |
| 744 | thread::sleep(Duration::from_secs(1)); |
| 745 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 746 | bools.assert_died(); |
| 747 | bools.assert_not_dropped(); |
| 748 | |
| 749 | drop(recipient); |
| 750 | |
| 751 | bools.assert_dropped(); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /// Test unregistering death notifications. |
| 755 | #[test] |
| 756 | fn test_unregister_death_notifications() { |
| 757 | binder::ProcessState::start_thread_pool(); |
| 758 | |
| 759 | let service_name = "test_unregister_death_notifications"; |
| 760 | let service_process = ScopedServiceProcess::new(service_name); |
| 761 | let mut remote = binder::get_service(service_name).expect("Could not retrieve service"); |
| 762 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 763 | let (bools, mut recipient) = register_death_notification(&mut remote); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 764 | |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 765 | remote.unlink_to_death(&mut recipient).expect("Could not unlink death notifications"); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 766 | |
| 767 | drop(service_process); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 768 | remote.ping_binder().expect_err("Service should have died already"); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 769 | |
| 770 | // Pause to ensure any death notifications get delivered |
| 771 | thread::sleep(Duration::from_secs(1)); |
| 772 | |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 773 | assert!(!bools.is_dead(), "Received unexpected death notification after unlinking",); |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 774 | |
| 775 | bools.assert_not_dropped(); |
| 776 | drop(recipient); |
| 777 | bools.assert_dropped(); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | /// Dropping a remote handle should unregister any death notifications. |
| 781 | #[test] |
| 782 | fn test_death_notification_registration_lifetime() { |
| 783 | binder::ProcessState::start_thread_pool(); |
| 784 | |
| 785 | let service_name = "test_death_notification_registration_lifetime"; |
| 786 | let service_process = ScopedServiceProcess::new(service_name); |
| 787 | let mut remote = binder::get_service(service_name).expect("Could not retrieve service"); |
| 788 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 789 | let (bools, recipient) = register_death_notification(&mut remote); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 790 | |
| 791 | // This should automatically unregister our death notification. |
| 792 | drop(remote); |
| 793 | |
| 794 | drop(service_process); |
| 795 | |
| 796 | // Pause to ensure any death notifications get delivered |
| 797 | thread::sleep(Duration::from_secs(1)); |
| 798 | |
| 799 | // We dropped the remote handle, so we should not receive the death |
| 800 | // notification when the remote process dies here. |
| 801 | assert!( |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 802 | !bools.is_dead(), |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 803 | "Received unexpected death notification after dropping remote handle" |
| 804 | ); |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 805 | |
| 806 | bools.assert_not_dropped(); |
| 807 | drop(recipient); |
| 808 | bools.assert_dropped(); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | /// Test IBinder interface methods not exercised elsewhere. |
| 812 | #[test] |
| 813 | fn test_misc_ibinder() { |
| 814 | let service_name = "rust_test_ibinder"; |
| 815 | |
| 816 | { |
| 817 | let _process = ScopedServiceProcess::new(service_name); |
| 818 | |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 819 | let test_client: Strong<dyn ITest> = |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 820 | binder::get_interface(service_name).expect("Did not get test binder service"); |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 821 | let mut remote = test_client.as_binder(); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 822 | assert!(remote.is_binder_alive()); |
| 823 | remote.ping_binder().expect("Could not ping remote service"); |
| 824 | |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 825 | let dump_args = ["dump", "args", "for", "testing"]; |
| 826 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 827 | let null_out = File::open("/dev/null").expect("Could not open /dev/null"); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 828 | remote.dump(&null_out, &dump_args).expect("Could not dump remote service"); |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 829 | |
| 830 | let remote_args = test_client.get_dump_args().expect("Could not fetched dumped args"); |
| 831 | assert_eq!(dump_args, remote_args[..], "Remote args don't match call to dump"); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | // get/set_extensions is tested in test_extensions() |
| 835 | |
| 836 | // transact is tested everywhere else, and we can't make raw |
| 837 | // transactions outside the [FIRST_CALL_TRANSACTION, |
| 838 | // LAST_CALL_TRANSACTION] range from the NDK anyway. |
| 839 | |
| 840 | // link_to_death is tested in test_*_death_notification* tests. |
| 841 | } |
| 842 | |
| 843 | #[test] |
| 844 | fn test_extensions() { |
| 845 | let service_name = "rust_test_extensions"; |
| 846 | let extension_name = "rust_test_extensions_ext"; |
| 847 | |
| 848 | { |
| 849 | let _process = ScopedServiceProcess::new(service_name); |
| 850 | |
| 851 | let mut remote = binder::get_service(service_name); |
| 852 | assert!(remote.is_binder_alive()); |
| 853 | |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 854 | let extension = remote.get_extension().expect("Could not check for an extension"); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 855 | assert!(extension.is_none()); |
| 856 | } |
| 857 | |
| 858 | { |
| 859 | let _process = ScopedServiceProcess::new_with_extension(service_name, extension_name); |
| 860 | |
| 861 | let mut remote = binder::get_service(service_name); |
| 862 | assert!(remote.is_binder_alive()); |
| 863 | |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 864 | let maybe_extension = remote.get_extension().expect("Could not check for an extension"); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 865 | |
| 866 | let extension = maybe_extension.expect("Remote binder did not have an extension"); |
| 867 | |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 868 | let extension: Strong<dyn ITest> = FromIBinder::try_from(extension) |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 869 | .expect("Extension could not be converted to the expected interface"); |
| 870 | |
| 871 | assert_eq!(extension.test().unwrap(), extension_name); |
| 872 | } |
| 873 | } |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 874 | |
| 875 | /// Test re-associating a local binder object with a different class. |
| 876 | /// |
| 877 | /// This is needed because different binder service (e.g. NDK vs Rust) |
| 878 | /// implementations are incompatible and must not be interchanged. A local |
| 879 | /// service with the same descriptor string but a different class pointer |
| 880 | /// may have been created by an NDK service and is therefore incompatible |
| 881 | /// with the Rust service implementation. It must be treated as remote and |
| 882 | /// all API calls parceled and sent through transactions. |
| 883 | /// |
| 884 | /// Further tests of this behavior with the C NDK and Rust API are in |
| 885 | /// rust_ndk_interop.rs |
| 886 | #[test] |
| 887 | fn associate_existing_class() { |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 888 | let service = Binder::new(BnTest(Box::new(TestService::new("testing_service")))); |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 889 | |
| 890 | // This should succeed although we will have to treat the service as |
| 891 | // remote. |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 892 | let _interface: Strong<dyn ITestSameDescriptor> = |
| 893 | FromIBinder::try_from(service.as_binder()) |
| 894 | .expect("Could not re-interpret service as the ITestSameDescriptor interface"); |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | /// Test that we can round-trip a rust service through a generic IBinder |
| 898 | #[test] |
| 899 | fn reassociate_rust_binder() { |
| 900 | let service_name = "testing_service"; |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 901 | let service_ibinder = |
| 902 | BnTest::new_binder(TestService::new(service_name), BinderFeatures::default()) |
| 903 | .as_binder(); |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 904 | |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 905 | let service: Strong<dyn ITest> = |
| 906 | service_ibinder.into_interface().expect("Could not reassociate the generic ibinder"); |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 907 | |
| 908 | assert_eq!(service.test().unwrap(), service_name); |
| 909 | } |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 910 | |
Devin Moore | bbd71b1 | 2024-08-09 23:20:03 +0000 | [diff] [blame] | 911 | struct ToBeDeleted { |
| 912 | deleted: Arc<AtomicBool>, |
| 913 | } |
| 914 | |
| 915 | impl Drop for ToBeDeleted { |
| 916 | fn drop(&mut self) { |
| 917 | assert!(!self.deleted.load(Ordering::Relaxed)); |
| 918 | self.deleted.store(true, Ordering::Relaxed); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | #[test] |
| 923 | fn test_accessor_callback_destruction() { |
| 924 | let deleted: Arc<AtomicBool> = Arc::new(AtomicBool::new(false)); |
| 925 | { |
| 926 | let accessor: Accessor; |
| 927 | { |
| 928 | let helper = ToBeDeleted { deleted: deleted.clone() }; |
| 929 | let get_connection_info = move |_instance: &str| { |
| 930 | // Capture this object so we can see it get destructed |
| 931 | // after the parent scope |
| 932 | let _ = &helper; |
| 933 | None |
| 934 | }; |
| 935 | accessor = Accessor::new("foo.service", get_connection_info); |
| 936 | } |
| 937 | |
| 938 | match accessor.as_binder() { |
| 939 | Some(_) => { |
| 940 | assert!(!deleted.load(Ordering::Relaxed)); |
| 941 | } |
| 942 | None => panic!("failed to get that accessor binder"), |
| 943 | } |
| 944 | } |
| 945 | assert!(deleted.load(Ordering::Relaxed)); |
| 946 | } |
| 947 | |
Devin Moore | 0555fbf | 2024-08-29 15:51:50 +0000 | [diff] [blame] | 948 | #[test] |
| 949 | fn test_accessor_delegator_new_each_time() { |
| 950 | let get_connection_info = move |_instance: &str| None; |
| 951 | let accessor = Accessor::new("foo.service", get_connection_info); |
| 952 | let delegator_binder = |
| 953 | binder::delegate_accessor("foo.service", accessor.as_binder().unwrap()); |
| 954 | let delegator_binder2 = |
| 955 | binder::delegate_accessor("foo.service", accessor.as_binder().unwrap()); |
| 956 | |
| 957 | // The delegate_accessor creates new delegators each time |
| 958 | assert!(delegator_binder != delegator_binder2); |
| 959 | } |
| 960 | |
| 961 | #[test] |
| 962 | fn test_accessor_delegate_the_delegator() { |
| 963 | let get_connection_info = move |_instance: &str| None; |
| 964 | let accessor = Accessor::new("foo.service", get_connection_info); |
| 965 | let delegator_binder = |
| 966 | binder::delegate_accessor("foo.service", accessor.as_binder().unwrap()); |
| 967 | let delegator_binder2 = |
| 968 | binder::delegate_accessor("foo.service", delegator_binder.clone().unwrap()); |
| 969 | |
| 970 | assert!(delegator_binder.clone() == delegator_binder); |
| 971 | // The delegate_accessor creates new delegators each time. Even when they are delegators |
| 972 | // of delegators. |
| 973 | assert!(delegator_binder != delegator_binder2); |
| 974 | } |
| 975 | |
| 976 | #[test] |
| 977 | fn test_accessor_delegator_wrong_name() { |
| 978 | let get_connection_info = move |_instance: &str| None; |
| 979 | let accessor = Accessor::new("foo.service", get_connection_info); |
| 980 | let delegator_binder = |
| 981 | binder::delegate_accessor("NOT.foo.service", accessor.as_binder().unwrap()); |
| 982 | assert_eq!(delegator_binder, Err(StatusCode::NAME_NOT_FOUND)); |
| 983 | } |
| 984 | |
Devin Moore | 7860d40 | 2024-11-15 23:25:59 +0000 | [diff] [blame] | 985 | #[test] |
| 986 | fn test_accessor_provider_simple() { |
| 987 | let instances: Vec<String> = vec!["foo.service".to_owned(), "foo.other_service".to_owned()]; |
| 988 | let accessor = AccessorProvider::new(&instances, move |_inst: &str| None); |
| 989 | assert!(accessor.is_some()); |
| 990 | } |
| 991 | |
| 992 | #[test] |
| 993 | fn test_accessor_provider_no_instance() { |
| 994 | let instances: Vec<String> = vec![]; |
| 995 | let accessor = AccessorProvider::new(&instances, move |_inst: &str| None); |
| 996 | assert!(accessor.is_none()); |
| 997 | } |
| 998 | |
| 999 | #[test] |
| 1000 | fn test_accessor_provider_double_register() { |
| 1001 | let instances: Vec<String> = vec!["foo.service".to_owned(), "foo.other_service".to_owned()]; |
| 1002 | let accessor = AccessorProvider::new(&instances, move |_inst: &str| None); |
| 1003 | assert!(accessor.is_some()); |
| 1004 | let accessor2 = AccessorProvider::new(&instances, move |_inst: &str| None); |
| 1005 | assert!(accessor2.is_none()); |
| 1006 | } |
| 1007 | |
| 1008 | #[test] |
| 1009 | fn test_accessor_provider_register_drop_register() { |
| 1010 | let instances: Vec<String> = vec!["foo.service".to_owned(), "foo.other_service".to_owned()]; |
| 1011 | { |
| 1012 | let accessor = AccessorProvider::new(&instances, move |_inst: &str| None); |
| 1013 | assert!(accessor.is_some()); |
| 1014 | // accessor drops and unregisters the provider |
| 1015 | } |
| 1016 | { |
| 1017 | let accessor = AccessorProvider::new(&instances, move |_inst: &str| None); |
| 1018 | assert!(accessor.is_some()); |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | #[test] |
| 1023 | fn test_accessor_provider_callback_destruction() { |
| 1024 | let deleted: Arc<AtomicBool> = Arc::new(AtomicBool::new(false)); |
| 1025 | let instances: Vec<String> = vec!["foo.service".to_owned(), "foo.other_service".to_owned()]; |
| 1026 | { |
| 1027 | let accessor: Option<AccessorProvider>; |
| 1028 | { |
| 1029 | let helper = ToBeDeleted { deleted: deleted.clone() }; |
| 1030 | accessor = AccessorProvider::new(&instances, move |_inst: &str| { |
| 1031 | let _ = &helper; |
| 1032 | None |
| 1033 | }); |
| 1034 | } |
| 1035 | assert!(accessor.is_some()); |
| 1036 | assert!(!deleted.load(Ordering::Relaxed)); |
| 1037 | } |
| 1038 | assert!(deleted.load(Ordering::Relaxed)); |
| 1039 | } |
| 1040 | |
Devin Moore | b4eafd3 | 2024-11-18 16:46:33 +0000 | [diff] [blame] | 1041 | #[test] |
| 1042 | fn test_accessor_from_accessor_binder() { |
| 1043 | let get_connection_info = move |_instance: &str| None; |
| 1044 | let accessor = Accessor::new("foo.service", get_connection_info); |
| 1045 | let accessor2 = |
| 1046 | Accessor::from_binder("foo.service", accessor.as_binder().unwrap()).unwrap(); |
| 1047 | assert_eq!(accessor.as_binder(), accessor2.as_binder()); |
| 1048 | } |
| 1049 | |
| 1050 | #[test] |
| 1051 | fn test_accessor_from_non_accessor_binder() { |
| 1052 | let service_name = "rust_test_ibinder"; |
| 1053 | let _process = ScopedServiceProcess::new(service_name); |
| 1054 | let binder = binder::get_service(service_name).unwrap(); |
| 1055 | assert!(binder.is_binder_alive()); |
| 1056 | |
| 1057 | let accessor = Accessor::from_binder("rust_test_ibinder", binder); |
| 1058 | assert!(accessor.is_none()); |
| 1059 | } |
| 1060 | |
| 1061 | #[test] |
| 1062 | fn test_accessor_from_wrong_accessor_binder() { |
| 1063 | let get_connection_info = move |_instance: &str| None; |
| 1064 | let accessor = Accessor::new("foo.service", get_connection_info); |
| 1065 | let accessor2 = Accessor::from_binder("NOT.foo.service", accessor.as_binder().unwrap()); |
| 1066 | assert!(accessor2.is_none()); |
| 1067 | } |
| 1068 | |
Alice Ryhl | 0ae829e | 2024-05-21 14:21:22 +0000 | [diff] [blame] | 1069 | #[tokio::test] |
| 1070 | async fn reassociate_rust_binder_async() { |
| 1071 | let service_name = "testing_service"; |
| 1072 | let service_ibinder = |
| 1073 | BnTest::new_binder(TestService::new(service_name), BinderFeatures::default()) |
| 1074 | .as_binder(); |
| 1075 | |
| 1076 | let service: Strong<dyn IATest<Tokio>> = |
| 1077 | service_ibinder.into_interface().expect("Could not reassociate the generic ibinder"); |
| 1078 | |
| 1079 | assert_eq!(service.test().await.unwrap(), service_name); |
| 1080 | } |
| 1081 | |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 1082 | #[test] |
| 1083 | fn weak_binder_upgrade() { |
| 1084 | let service_name = "testing_service"; |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 1085 | let service = BnTest::new_binder(TestService::new(service_name), BinderFeatures::default()); |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 1086 | |
| 1087 | let weak = Strong::downgrade(&service); |
| 1088 | |
| 1089 | let upgraded = weak.upgrade().expect("Could not upgrade weak binder"); |
| 1090 | |
| 1091 | assert_eq!(service, upgraded); |
| 1092 | } |
| 1093 | |
| 1094 | #[test] |
| 1095 | fn weak_binder_upgrade_dead() { |
| 1096 | let service_name = "testing_service"; |
| 1097 | let weak = { |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 1098 | let service = |
| 1099 | BnTest::new_binder(TestService::new(service_name), BinderFeatures::default()); |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 1100 | |
| 1101 | Strong::downgrade(&service) |
| 1102 | }; |
| 1103 | |
| 1104 | assert_eq!(weak.upgrade(), Err(StatusCode::DEAD_OBJECT)); |
| 1105 | } |
| 1106 | |
| 1107 | #[test] |
| 1108 | fn weak_binder_clone() { |
| 1109 | let service_name = "testing_service"; |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 1110 | let service = BnTest::new_binder(TestService::new(service_name), BinderFeatures::default()); |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 1111 | |
| 1112 | let weak = Strong::downgrade(&service); |
| 1113 | let cloned = weak.clone(); |
| 1114 | assert_eq!(weak, cloned); |
| 1115 | |
| 1116 | let upgraded = weak.upgrade().expect("Could not upgrade weak binder"); |
| 1117 | let clone_upgraded = cloned.upgrade().expect("Could not upgrade weak binder"); |
| 1118 | |
| 1119 | assert_eq!(service, upgraded); |
| 1120 | assert_eq!(service, clone_upgraded); |
| 1121 | } |
| 1122 | |
| 1123 | #[test] |
| 1124 | #[allow(clippy::eq_op)] |
| 1125 | fn binder_ord() { |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 1126 | let service1 = |
| 1127 | BnTest::new_binder(TestService::new("testing_service1"), BinderFeatures::default()); |
| 1128 | let service2 = |
| 1129 | BnTest::new_binder(TestService::new("testing_service2"), BinderFeatures::default()); |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 1130 | |
Charisee | 1ccdc90 | 2022-02-26 01:22:27 +0000 | [diff] [blame] | 1131 | assert!((service1 >= service1)); |
| 1132 | assert!((service1 <= service1)); |
| 1133 | assert_eq!(service1 < service2, (service2 >= service1)); |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 1134 | } |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 1135 | |
| 1136 | #[test] |
| 1137 | fn binder_parcel_mixup() { |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 1138 | let service1 = |
| 1139 | BnTest::new_binder(TestService::new("testing_service1"), BinderFeatures::default()); |
| 1140 | let service2 = |
| 1141 | BnTest::new_binder(TestService::new("testing_service2"), BinderFeatures::default()); |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 1142 | |
| 1143 | let service1 = service1.as_binder(); |
| 1144 | let service2 = service2.as_binder(); |
| 1145 | |
| 1146 | let parcel = service1.prepare_transact().unwrap(); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 1147 | let res = service2.submit_transact( |
| 1148 | super::TestTransactionCode::Test as TransactionCode, |
| 1149 | parcel, |
| 1150 | 0, |
| 1151 | ); |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 1152 | |
| 1153 | match res { |
| 1154 | Ok(_) => panic!("submit_transact should fail"), |
| 1155 | Err(err) => assert_eq!(err, binder::StatusCode::BAD_VALUE), |
| 1156 | } |
| 1157 | } |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 1158 | |
| 1159 | #[test] |
| 1160 | fn get_is_handling_transaction() { |
| 1161 | let service_name = "get_is_handling_transaction"; |
| 1162 | let _process = ScopedServiceProcess::new(service_name); |
| 1163 | let test_client: Strong<dyn ITest> = |
| 1164 | binder::get_interface(service_name).expect("Did not get manager binder service"); |
| 1165 | // Should be true externally. |
| 1166 | assert!(test_client.get_is_handling_transaction().unwrap()); |
| 1167 | |
| 1168 | // Should be false locally. |
| 1169 | assert!(!binder::is_handling_transaction()); |
| 1170 | |
| 1171 | // Should also be false in spawned thread. |
| 1172 | std::thread::spawn(|| { |
| 1173 | assert!(!binder::is_handling_transaction()); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 1174 | }) |
| 1175 | .join() |
| 1176 | .unwrap(); |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | #[tokio::test] |
| 1180 | async fn get_is_handling_transaction_async() { |
| 1181 | let service_name = "get_is_handling_transaction_async"; |
| 1182 | let _process = ScopedServiceProcess::new(service_name); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 1183 | let test_client: Strong<dyn IATest<Tokio>> = binder_tokio::get_interface(service_name) |
| 1184 | .await |
| 1185 | .expect("Did not get manager binder service"); |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 1186 | // Should be true externally. |
| 1187 | assert!(test_client.get_is_handling_transaction().await.unwrap()); |
| 1188 | |
| 1189 | // Should be false locally. |
| 1190 | assert!(!binder::is_handling_transaction()); |
| 1191 | |
| 1192 | // Should also be false in spawned task. |
| 1193 | tokio::spawn(async { |
| 1194 | assert!(!binder::is_handling_transaction()); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 1195 | }) |
| 1196 | .await |
| 1197 | .unwrap(); |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 1198 | |
| 1199 | // And in spawn_blocking task. |
| 1200 | tokio::task::spawn_blocking(|| { |
| 1201 | assert!(!binder::is_handling_transaction()); |
Matthew Maurer | e268a9f | 2022-07-26 09:31:30 -0700 | [diff] [blame] | 1202 | }) |
| 1203 | .await |
| 1204 | .unwrap(); |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 1205 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 1206 | } |