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 | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 487 | fn trivial_client() { |
| 488 | let service_name = "trivial_client_test"; |
| 489 | let _process = ScopedServiceProcess::new(service_name); |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 490 | let test_client: Strong<dyn ITest> = |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 491 | binder::get_interface(service_name).expect("Did not get manager binder service"); |
| 492 | assert_eq!(test_client.test().unwrap(), "trivial_client_test"); |
| 493 | } |
| 494 | |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 495 | #[tokio::test] |
| 496 | async fn trivial_client_async() { |
| 497 | let service_name = "trivial_client_test"; |
| 498 | let _process = ScopedServiceProcess::new(service_name); |
| 499 | let test_client: Strong<dyn IATest<Tokio>> = |
Alice Ryhl | 929804f | 2021-11-02 12:04:39 +0000 | [diff] [blame] | 500 | 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] | 501 | assert_eq!(test_client.test().await.unwrap(), "trivial_client_test"); |
| 502 | } |
| 503 | |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 504 | #[test] |
Andrew Walbran | c3ce5c3 | 2021-06-03 16:15:56 +0000 | [diff] [blame] | 505 | fn wait_for_trivial_client() { |
| 506 | let service_name = "wait_for_trivial_client_test"; |
| 507 | let _process = ScopedServiceProcess::new(service_name); |
| 508 | let test_client: Strong<dyn ITest> = |
| 509 | binder::wait_for_interface(service_name).expect("Did not get manager binder service"); |
| 510 | assert_eq!(test_client.test().unwrap(), "wait_for_trivial_client_test"); |
| 511 | } |
| 512 | |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 513 | #[tokio::test] |
| 514 | async fn wait_for_trivial_client_async() { |
| 515 | let service_name = "wait_for_trivial_client_test"; |
| 516 | let _process = ScopedServiceProcess::new(service_name); |
| 517 | let test_client: Strong<dyn IATest<Tokio>> = |
Alice Ryhl | 929804f | 2021-11-02 12:04:39 +0000 | [diff] [blame] | 518 | 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] | 519 | assert_eq!(test_client.test().await.unwrap(), "wait_for_trivial_client_test"); |
| 520 | } |
| 521 | |
| 522 | fn get_expected_selinux_context() -> &'static str { |
| 523 | unsafe { |
| 524 | let mut out_ptr = ptr::null_mut(); |
| 525 | assert_eq!(selinux_sys::getcon(&mut out_ptr), 0); |
| 526 | assert!(!out_ptr.is_null()); |
| 527 | CStr::from_ptr(out_ptr) |
| 528 | .to_str() |
| 529 | .expect("context was invalid UTF-8") |
| 530 | } |
| 531 | } |
| 532 | |
Andrew Walbran | c3ce5c3 | 2021-06-03 16:15:56 +0000 | [diff] [blame] | 533 | #[test] |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 534 | fn get_selinux_context() { |
| 535 | let service_name = "get_selinux_context"; |
| 536 | let _process = ScopedServiceProcess::new(service_name); |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 537 | let test_client: Strong<dyn ITest> = |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 538 | binder::get_interface(service_name).expect("Did not get manager binder service"); |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 539 | assert_eq!( |
| 540 | test_client.get_selinux_context().unwrap(), |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 541 | get_expected_selinux_context() |
| 542 | ); |
| 543 | } |
| 544 | |
| 545 | #[tokio::test] |
| 546 | async fn get_selinux_context_async() { |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 547 | let service_name = "get_selinux_context_async"; |
Alice Ryhl | 05f5a2c | 2021-09-15 12:56:10 +0000 | [diff] [blame] | 548 | let _process = ScopedServiceProcess::new(service_name); |
| 549 | let test_client: Strong<dyn IATest<Tokio>> = |
Alice Ryhl | 929804f | 2021-11-02 12:04:39 +0000 | [diff] [blame] | 550 | 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] | 551 | assert_eq!( |
| 552 | test_client.get_selinux_context().await.unwrap(), |
| 553 | get_expected_selinux_context() |
Janis Danisevskis | 798a09a | 2020-08-18 08:35:38 -0700 | [diff] [blame] | 554 | ); |
| 555 | } |
| 556 | |
Alice Ryhl | c173684 | 2021-11-23 12:38:51 +0000 | [diff] [blame] | 557 | #[tokio::test] |
| 558 | async fn get_selinux_context_sync_to_async() { |
| 559 | let service_name = "get_selinux_context"; |
| 560 | let _process = ScopedServiceProcess::new(service_name); |
| 561 | let test_client: Strong<dyn ITest> = |
| 562 | binder::get_interface(service_name).expect("Did not get manager binder service"); |
| 563 | let test_client = test_client.into_async::<Tokio>(); |
| 564 | assert_eq!( |
| 565 | test_client.get_selinux_context().await.unwrap(), |
| 566 | get_expected_selinux_context() |
| 567 | ); |
| 568 | } |
| 569 | |
| 570 | #[tokio::test] |
| 571 | async fn get_selinux_context_async_to_sync() { |
| 572 | let service_name = "get_selinux_context"; |
| 573 | let _process = ScopedServiceProcess::new(service_name); |
| 574 | let test_client: Strong<dyn IATest<Tokio>> = |
| 575 | binder_tokio::get_interface(service_name).await.expect("Did not get manager binder service"); |
| 576 | let test_client = test_client.into_sync(); |
| 577 | assert_eq!( |
| 578 | test_client.get_selinux_context().unwrap(), |
| 579 | get_expected_selinux_context() |
| 580 | ); |
| 581 | } |
| 582 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 583 | struct Bools { |
| 584 | binder_died: Arc<AtomicBool>, |
| 585 | binder_dealloc: Arc<AtomicBool>, |
| 586 | } |
| 587 | |
| 588 | impl Bools { |
| 589 | fn is_dead(&self) -> bool { |
| 590 | self.binder_died.load(Ordering::Relaxed) |
| 591 | } |
| 592 | fn assert_died(&self) { |
| 593 | assert!( |
| 594 | self.is_dead(), |
| 595 | "Did not receive death notification" |
| 596 | ); |
| 597 | } |
| 598 | fn assert_dropped(&self) { |
| 599 | assert!( |
| 600 | self.binder_dealloc.load(Ordering::Relaxed), |
| 601 | "Did not dealloc death notification" |
| 602 | ); |
| 603 | } |
| 604 | fn assert_not_dropped(&self) { |
| 605 | assert!( |
| 606 | !self.binder_dealloc.load(Ordering::Relaxed), |
| 607 | "Dealloc death notification too early" |
| 608 | ); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | fn register_death_notification(binder: &mut SpIBinder) -> (Bools, DeathRecipient) { |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 613 | let binder_died = Arc::new(AtomicBool::new(false)); |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 614 | let binder_dealloc = Arc::new(AtomicBool::new(false)); |
| 615 | |
| 616 | struct SetOnDrop { |
| 617 | binder_dealloc: Arc<AtomicBool>, |
| 618 | } |
| 619 | impl Drop for SetOnDrop { |
| 620 | fn drop(&mut self) { |
| 621 | self.binder_dealloc.store(true, Ordering::Relaxed); |
| 622 | } |
| 623 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 624 | |
| 625 | let mut death_recipient = { |
| 626 | let flag = binder_died.clone(); |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 627 | let set_on_drop = SetOnDrop { |
| 628 | binder_dealloc: binder_dealloc.clone(), |
| 629 | }; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 630 | DeathRecipient::new(move || { |
| 631 | flag.store(true, Ordering::Relaxed); |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 632 | // Force the closure to take ownership of set_on_drop. When the closure is |
| 633 | // dropped, the destructor of `set_on_drop` will run. |
| 634 | let _ = &set_on_drop; |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 635 | }) |
| 636 | }; |
| 637 | |
| 638 | binder |
| 639 | .link_to_death(&mut death_recipient) |
| 640 | .expect("link_to_death failed"); |
| 641 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 642 | let bools = Bools { |
| 643 | binder_died, |
| 644 | binder_dealloc, |
| 645 | }; |
| 646 | |
| 647 | (bools, death_recipient) |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /// Killing a remote service should unregister the service and trigger |
| 651 | /// death notifications. |
| 652 | #[test] |
| 653 | fn test_death_notifications() { |
| 654 | binder::ProcessState::start_thread_pool(); |
| 655 | |
| 656 | let service_name = "test_death_notifications"; |
| 657 | let service_process = ScopedServiceProcess::new(service_name); |
| 658 | let mut remote = binder::get_service(service_name).expect("Could not retrieve service"); |
| 659 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 660 | let (bools, recipient) = register_death_notification(&mut remote); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 661 | |
| 662 | drop(service_process); |
| 663 | remote |
| 664 | .ping_binder() |
| 665 | .expect_err("Service should have died already"); |
| 666 | |
| 667 | // Pause to ensure any death notifications get delivered |
| 668 | thread::sleep(Duration::from_secs(1)); |
| 669 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 670 | bools.assert_died(); |
| 671 | bools.assert_not_dropped(); |
| 672 | |
| 673 | drop(recipient); |
| 674 | |
| 675 | bools.assert_dropped(); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | /// Test unregistering death notifications. |
| 679 | #[test] |
| 680 | fn test_unregister_death_notifications() { |
| 681 | binder::ProcessState::start_thread_pool(); |
| 682 | |
| 683 | let service_name = "test_unregister_death_notifications"; |
| 684 | let service_process = ScopedServiceProcess::new(service_name); |
| 685 | let mut remote = binder::get_service(service_name).expect("Could not retrieve service"); |
| 686 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 687 | let (bools, mut recipient) = register_death_notification(&mut remote); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 688 | |
| 689 | remote |
| 690 | .unlink_to_death(&mut recipient) |
| 691 | .expect("Could not unlink death notifications"); |
| 692 | |
| 693 | drop(service_process); |
| 694 | remote |
| 695 | .ping_binder() |
| 696 | .expect_err("Service should have died already"); |
| 697 | |
| 698 | // Pause to ensure any death notifications get delivered |
| 699 | thread::sleep(Duration::from_secs(1)); |
| 700 | |
| 701 | assert!( |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 702 | !bools.is_dead(), |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 703 | "Received unexpected death notification after unlinking", |
| 704 | ); |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 705 | |
| 706 | bools.assert_not_dropped(); |
| 707 | drop(recipient); |
| 708 | bools.assert_dropped(); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /// Dropping a remote handle should unregister any death notifications. |
| 712 | #[test] |
| 713 | fn test_death_notification_registration_lifetime() { |
| 714 | binder::ProcessState::start_thread_pool(); |
| 715 | |
| 716 | let service_name = "test_death_notification_registration_lifetime"; |
| 717 | let service_process = ScopedServiceProcess::new(service_name); |
| 718 | let mut remote = binder::get_service(service_name).expect("Could not retrieve service"); |
| 719 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 720 | let (bools, recipient) = register_death_notification(&mut remote); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 721 | |
| 722 | // This should automatically unregister our death notification. |
| 723 | drop(remote); |
| 724 | |
| 725 | drop(service_process); |
| 726 | |
| 727 | // Pause to ensure any death notifications get delivered |
| 728 | thread::sleep(Duration::from_secs(1)); |
| 729 | |
| 730 | // We dropped the remote handle, so we should not receive the death |
| 731 | // notification when the remote process dies here. |
| 732 | assert!( |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 733 | !bools.is_dead(), |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 734 | "Received unexpected death notification after dropping remote handle" |
| 735 | ); |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 736 | |
| 737 | bools.assert_not_dropped(); |
| 738 | drop(recipient); |
| 739 | bools.assert_dropped(); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | /// Test IBinder interface methods not exercised elsewhere. |
| 743 | #[test] |
| 744 | fn test_misc_ibinder() { |
| 745 | let service_name = "rust_test_ibinder"; |
| 746 | |
| 747 | { |
| 748 | let _process = ScopedServiceProcess::new(service_name); |
| 749 | |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 750 | let test_client: Strong<dyn ITest> = |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 751 | binder::get_interface(service_name).expect("Did not get test binder service"); |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 752 | let mut remote = test_client.as_binder(); |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 753 | assert!(remote.is_binder_alive()); |
| 754 | remote.ping_binder().expect("Could not ping remote service"); |
| 755 | |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 756 | let dump_args = ["dump", "args", "for", "testing"]; |
| 757 | |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 758 | let null_out = File::open("/dev/null").expect("Could not open /dev/null"); |
| 759 | remote |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 760 | .dump(&null_out, &dump_args) |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 761 | .expect("Could not dump remote service"); |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 762 | |
| 763 | let remote_args = test_client.get_dump_args().expect("Could not fetched dumped args"); |
| 764 | 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] | 765 | } |
| 766 | |
| 767 | // get/set_extensions is tested in test_extensions() |
| 768 | |
| 769 | // transact is tested everywhere else, and we can't make raw |
| 770 | // transactions outside the [FIRST_CALL_TRANSACTION, |
| 771 | // LAST_CALL_TRANSACTION] range from the NDK anyway. |
| 772 | |
| 773 | // link_to_death is tested in test_*_death_notification* tests. |
| 774 | } |
| 775 | |
| 776 | #[test] |
| 777 | fn test_extensions() { |
| 778 | let service_name = "rust_test_extensions"; |
| 779 | let extension_name = "rust_test_extensions_ext"; |
| 780 | |
| 781 | { |
| 782 | let _process = ScopedServiceProcess::new(service_name); |
| 783 | |
| 784 | let mut remote = binder::get_service(service_name); |
| 785 | assert!(remote.is_binder_alive()); |
| 786 | |
| 787 | let extension = remote |
| 788 | .get_extension() |
| 789 | .expect("Could not check for an extension"); |
| 790 | assert!(extension.is_none()); |
| 791 | } |
| 792 | |
| 793 | { |
| 794 | let _process = ScopedServiceProcess::new_with_extension(service_name, extension_name); |
| 795 | |
| 796 | let mut remote = binder::get_service(service_name); |
| 797 | assert!(remote.is_binder_alive()); |
| 798 | |
| 799 | let maybe_extension = remote |
| 800 | .get_extension() |
| 801 | .expect("Could not check for an extension"); |
| 802 | |
| 803 | let extension = maybe_extension.expect("Remote binder did not have an extension"); |
| 804 | |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 805 | let extension: Strong<dyn ITest> = FromIBinder::try_from(extension) |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 806 | .expect("Extension could not be converted to the expected interface"); |
| 807 | |
| 808 | assert_eq!(extension.test().unwrap(), extension_name); |
| 809 | } |
| 810 | } |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 811 | |
| 812 | /// Test re-associating a local binder object with a different class. |
| 813 | /// |
| 814 | /// This is needed because different binder service (e.g. NDK vs Rust) |
| 815 | /// implementations are incompatible and must not be interchanged. A local |
| 816 | /// service with the same descriptor string but a different class pointer |
| 817 | /// may have been created by an NDK service and is therefore incompatible |
| 818 | /// with the Rust service implementation. It must be treated as remote and |
| 819 | /// all API calls parceled and sent through transactions. |
| 820 | /// |
| 821 | /// Further tests of this behavior with the C NDK and Rust API are in |
| 822 | /// rust_ndk_interop.rs |
| 823 | #[test] |
| 824 | fn associate_existing_class() { |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 825 | let service = Binder::new(BnTest(Box::new(TestService::new("testing_service")))); |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 826 | |
| 827 | // This should succeed although we will have to treat the service as |
| 828 | // remote. |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 829 | let _interface: Strong<dyn ITestSameDescriptor> = |
| 830 | FromIBinder::try_from(service.as_binder()) |
| 831 | .expect("Could not re-interpret service as the ITestSameDescriptor interface"); |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /// Test that we can round-trip a rust service through a generic IBinder |
| 835 | #[test] |
| 836 | fn reassociate_rust_binder() { |
| 837 | let service_name = "testing_service"; |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 838 | let service_ibinder = BnTest::new_binder( |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 839 | TestService::new(service_name), |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 840 | BinderFeatures::default(), |
| 841 | ) |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 842 | .as_binder(); |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 843 | |
Andrew Walbran | 12400d8 | 2021-03-04 17:04:34 +0000 | [diff] [blame] | 844 | let service: Strong<dyn ITest> = service_ibinder |
| 845 | .into_interface() |
Stephen Crane | 669deb6 | 2020-09-10 17:31:39 -0700 | [diff] [blame] | 846 | .expect("Could not reassociate the generic ibinder"); |
| 847 | |
| 848 | assert_eq!(service.test().unwrap(), service_name); |
| 849 | } |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 850 | |
| 851 | #[test] |
| 852 | fn weak_binder_upgrade() { |
| 853 | let service_name = "testing_service"; |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 854 | let service = BnTest::new_binder( |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 855 | TestService::new(service_name), |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 856 | BinderFeatures::default(), |
| 857 | ); |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 858 | |
| 859 | let weak = Strong::downgrade(&service); |
| 860 | |
| 861 | let upgraded = weak.upgrade().expect("Could not upgrade weak binder"); |
| 862 | |
| 863 | assert_eq!(service, upgraded); |
| 864 | } |
| 865 | |
| 866 | #[test] |
| 867 | fn weak_binder_upgrade_dead() { |
| 868 | let service_name = "testing_service"; |
| 869 | let weak = { |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 870 | let service = BnTest::new_binder( |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 871 | TestService::new(service_name), |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 872 | BinderFeatures::default(), |
| 873 | ); |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 874 | |
| 875 | Strong::downgrade(&service) |
| 876 | }; |
| 877 | |
| 878 | assert_eq!(weak.upgrade(), Err(StatusCode::DEAD_OBJECT)); |
| 879 | } |
| 880 | |
| 881 | #[test] |
| 882 | fn weak_binder_clone() { |
| 883 | let service_name = "testing_service"; |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 884 | let service = BnTest::new_binder( |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 885 | TestService::new(service_name), |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 886 | BinderFeatures::default(), |
| 887 | ); |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 888 | |
| 889 | let weak = Strong::downgrade(&service); |
| 890 | let cloned = weak.clone(); |
| 891 | assert_eq!(weak, cloned); |
| 892 | |
| 893 | let upgraded = weak.upgrade().expect("Could not upgrade weak binder"); |
| 894 | let clone_upgraded = cloned.upgrade().expect("Could not upgrade weak binder"); |
| 895 | |
| 896 | assert_eq!(service, upgraded); |
| 897 | assert_eq!(service, clone_upgraded); |
| 898 | } |
| 899 | |
| 900 | #[test] |
| 901 | #[allow(clippy::eq_op)] |
| 902 | fn binder_ord() { |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 903 | let service1 = BnTest::new_binder( |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 904 | TestService::new("testing_service1"), |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 905 | BinderFeatures::default(), |
| 906 | ); |
| 907 | let service2 = BnTest::new_binder( |
Stephen Crane | 2a3297f | 2021-06-11 16:48:10 -0700 | [diff] [blame] | 908 | TestService::new("testing_service2"), |
Andrew Walbran | 88eca4f | 2021-04-13 14:26:01 +0000 | [diff] [blame] | 909 | BinderFeatures::default(), |
| 910 | ); |
Stephen Crane | ddb3e6d | 2020-12-18 13:27:22 -0800 | [diff] [blame] | 911 | |
| 912 | assert!(!(service1 < service1)); |
| 913 | assert!(!(service1 > service1)); |
| 914 | assert_eq!(service1 < service2, !(service2 < service1)); |
| 915 | } |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 916 | |
| 917 | #[test] |
| 918 | fn binder_parcel_mixup() { |
| 919 | let service1 = BnTest::new_binder( |
| 920 | TestService::new("testing_service1"), |
| 921 | BinderFeatures::default(), |
| 922 | ); |
| 923 | let service2 = BnTest::new_binder( |
| 924 | TestService::new("testing_service2"), |
| 925 | BinderFeatures::default(), |
| 926 | ); |
| 927 | |
| 928 | let service1 = service1.as_binder(); |
| 929 | let service2 = service2.as_binder(); |
| 930 | |
| 931 | let parcel = service1.prepare_transact().unwrap(); |
Stephen Crane | f2735b4 | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 932 | let res = service2.submit_transact(super::TestTransactionCode::Test as TransactionCode, parcel, 0); |
Alice Ryhl | feba6ca | 2021-08-19 10:47:04 +0000 | [diff] [blame] | 933 | |
| 934 | match res { |
| 935 | Ok(_) => panic!("submit_transact should fail"), |
| 936 | Err(err) => assert_eq!(err, binder::StatusCode::BAD_VALUE), |
| 937 | } |
| 938 | } |
Alice Ryhl | 29a5026 | 2021-12-10 11:14:32 +0000 | [diff] [blame] | 939 | |
| 940 | #[test] |
| 941 | fn get_is_handling_transaction() { |
| 942 | let service_name = "get_is_handling_transaction"; |
| 943 | let _process = ScopedServiceProcess::new(service_name); |
| 944 | let test_client: Strong<dyn ITest> = |
| 945 | binder::get_interface(service_name).expect("Did not get manager binder service"); |
| 946 | // Should be true externally. |
| 947 | assert!(test_client.get_is_handling_transaction().unwrap()); |
| 948 | |
| 949 | // Should be false locally. |
| 950 | assert!(!binder::is_handling_transaction()); |
| 951 | |
| 952 | // Should also be false in spawned thread. |
| 953 | std::thread::spawn(|| { |
| 954 | assert!(!binder::is_handling_transaction()); |
| 955 | }).join().unwrap(); |
| 956 | } |
| 957 | |
| 958 | #[tokio::test] |
| 959 | async fn get_is_handling_transaction_async() { |
| 960 | let service_name = "get_is_handling_transaction_async"; |
| 961 | let _process = ScopedServiceProcess::new(service_name); |
| 962 | let test_client: Strong<dyn IATest<Tokio>> = |
| 963 | binder_tokio::get_interface(service_name).await.expect("Did not get manager binder service"); |
| 964 | // Should be true externally. |
| 965 | assert!(test_client.get_is_handling_transaction().await.unwrap()); |
| 966 | |
| 967 | // Should be false locally. |
| 968 | assert!(!binder::is_handling_transaction()); |
| 969 | |
| 970 | // Should also be false in spawned task. |
| 971 | tokio::spawn(async { |
| 972 | assert!(!binder::is_handling_transaction()); |
| 973 | }).await.unwrap(); |
| 974 | |
| 975 | // And in spawn_blocking task. |
| 976 | tokio::task::spawn_blocking(|| { |
| 977 | assert!(!binder::is_handling_transaction()); |
| 978 | }).await.unwrap(); |
| 979 | } |
Stephen Crane | 2a3c250 | 2020-06-16 17:48:35 -0700 | [diff] [blame] | 980 | } |