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