blob: 50daf1c0ccac550a5da0ba7b42a97b9c1fdffc36 [file] [log] [blame]
Stephen Crane2a3c2502020-06-16 17:48:35 -07001/*
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 Crane7bca1052021-10-25 17:52:51 -070019use binder::{declare_binder_enum, declare_binder_interface};
Stephen Cranef2735b42022-01-19 17:49:46 +000020use binder::{BinderFeatures, Interface, StatusCode, ThreadState};
21// Import from internal API for testing only, do not use this module in
22// production.
23use binder::binder_impl::{
24 Binder, BorrowedParcel, IBinderInternal, TransactionCode, FIRST_CALL_TRANSACTION,
Andrew Walbran12400d82021-03-04 17:04:34 +000025};
Stephen Cranef2735b42022-01-19 17:49:46 +000026
Janis Danisevskis798a09a2020-08-18 08:35:38 -070027use std::convert::{TryFrom, TryInto};
Stephen Crane2a3297f2021-06-11 16:48:10 -070028use std::ffi::CStr;
29use std::fs::File;
30use std::sync::Mutex;
Stephen Crane2a3c2502020-06-16 17:48:35 -070031
32/// Name of service runner.
33///
34/// Must match the binary name in Android.bp
35const 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.
41fn 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 Crane2a3297f2021-06-11 16:48:10 -070058 let mut service = Binder::new(BnTest(Box::new(TestService::new(&service_name))));
Janis Danisevskis798a09a2020-08-18 08:35:38 -070059 service.set_requesting_sid(true);
Stephen Crane2a3c2502020-06-16 17:48:35 -070060 if let Some(extension_name) = extension_name {
Andrew Walbran88eca4f2021-04-13 14:26:01 +000061 let extension =
Stephen Crane2a3297f2021-06-11 16:48:10 -070062 BnTest::new_binder(TestService::new(&extension_name), BinderFeatures::default());
Stephen Crane2a3c2502020-06-16 17:48:35 -070063 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
75fn 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 Crane2a3c2502020-06-16 17:48:35 -070086struct TestService {
87 s: String,
Stephen Crane2a3297f2021-06-11 16:48:10 -070088 dump_args: Mutex<Vec<String>>,
89}
90
91impl TestService {
92 fn new(s: &str) -> Self {
93 Self {
94 s: s.to_string(),
95 dump_args: Mutex::new(Vec::new()),
96 }
97 }
Stephen Crane2a3c2502020-06-16 17:48:35 -070098}
99
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700100#[repr(u32)]
101enum TestTransactionCode {
Andrew Walbran12400d82021-03-04 17:04:34 +0000102 Test = FIRST_CALL_TRANSACTION,
Stephen Crane2a3297f2021-06-11 16:48:10 -0700103 GetDumpArgs,
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700104 GetSelinuxContext,
Alice Ryhl29a50262021-12-10 11:14:32 +0000105 GetIsHandlingTransaction,
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700106}
107
108impl 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 Crane2a3297f2021-06-11 16:48:10 -0700114 _ if c == TestTransactionCode::GetDumpArgs as u32 => Ok(TestTransactionCode::GetDumpArgs),
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700115 _ if c == TestTransactionCode::GetSelinuxContext as u32 => {
116 Ok(TestTransactionCode::GetSelinuxContext)
117 }
Alice Ryhl29a50262021-12-10 11:14:32 +0000118 _ if c == TestTransactionCode::GetIsHandlingTransaction as u32 => Ok(TestTransactionCode::GetIsHandlingTransaction),
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700119 _ => Err(StatusCode::UNKNOWN_TRANSACTION),
120 }
121 }
122}
123
Stephen Crane2a3297f2021-06-11 16:48:10 -0700124impl Interface for TestService {
Stephen Cranef2735b42022-01-19 17:49:46 +0000125 fn dump(&self, _file: &File, args: &[&CStr]) -> Result<(), StatusCode> {
Stephen Crane2a3297f2021-06-11 16:48:10 -0700126 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 Crane2a3c2502020-06-16 17:48:35 -0700131
132impl ITest for TestService {
Stephen Cranef2735b42022-01-19 17:49:46 +0000133 fn test(&self) -> Result<String, StatusCode> {
Stephen Crane2a3c2502020-06-16 17:48:35 -0700134 Ok(self.s.clone())
135 }
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700136
Stephen Cranef2735b42022-01-19 17:49:46 +0000137 fn get_dump_args(&self) -> Result<Vec<String>, StatusCode> {
Stephen Crane2a3297f2021-06-11 16:48:10 -0700138 let args = self.dump_args.lock().unwrap().clone();
139 Ok(args)
140 }
141
Stephen Cranef2735b42022-01-19 17:49:46 +0000142 fn get_selinux_context(&self) -> Result<String, StatusCode> {
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700143 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 Ryhl29a50262021-12-10 11:14:32 +0000147
Stephen Cranef2735b42022-01-19 17:49:46 +0000148 fn get_is_handling_transaction(&self) -> Result<bool, StatusCode> {
Alice Ryhl29a50262021-12-10 11:14:32 +0000149 Ok(binder::is_handling_transaction())
150 }
Stephen Crane2a3c2502020-06-16 17:48:35 -0700151}
152
153/// Trivial testing binder interface
154pub trait ITest: Interface {
155 /// Returns a test string
Stephen Cranef2735b42022-01-19 17:49:46 +0000156 fn test(&self) -> Result<String, StatusCode>;
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700157
Stephen Crane2a3297f2021-06-11 16:48:10 -0700158 /// Return the arguments sent via dump
Stephen Cranef2735b42022-01-19 17:49:46 +0000159 fn get_dump_args(&self) -> Result<Vec<String>, StatusCode>;
Stephen Crane2a3297f2021-06-11 16:48:10 -0700160
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700161 /// Returns the caller's SELinux context
Stephen Cranef2735b42022-01-19 17:49:46 +0000162 fn get_selinux_context(&self) -> Result<String, StatusCode>;
Alice Ryhl29a50262021-12-10 11:14:32 +0000163
164 /// Returns the value of calling `is_handling_transaction`.
Stephen Cranef2735b42022-01-19 17:49:46 +0000165 fn get_is_handling_transaction(&self) -> Result<bool, StatusCode>;
Stephen Crane2a3c2502020-06-16 17:48:35 -0700166}
167
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000168/// Async trivial testing binder interface
169pub trait IATest<P>: Interface {
170 /// Returns a test string
Stephen Cranef2735b42022-01-19 17:49:46 +0000171 fn test(&self) -> binder::BoxFuture<'static, Result<String, StatusCode>>;
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000172
173 /// Return the arguments sent via dump
Stephen Cranef2735b42022-01-19 17:49:46 +0000174 fn get_dump_args(&self) -> binder::BoxFuture<'static, Result<Vec<String>, StatusCode>>;
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000175
176 /// Returns the caller's SELinux context
Stephen Cranef2735b42022-01-19 17:49:46 +0000177 fn get_selinux_context(&self) -> binder::BoxFuture<'static, Result<String, StatusCode>>;
Alice Ryhl29a50262021-12-10 11:14:32 +0000178
179 /// Returns the value of calling `is_handling_transaction`.
Stephen Cranef2735b42022-01-19 17:49:46 +0000180 fn get_is_handling_transaction(&self) -> binder::BoxFuture<'static, Result<bool, StatusCode>>;
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000181}
182
Stephen Crane2a3c2502020-06-16 17:48:35 -0700183declare_binder_interface! {
184 ITest["android.os.ITest"] {
185 native: BnTest(on_transact),
186 proxy: BpTest {
187 x: i32 = 100
188 },
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000189 async: IATest,
Stephen Crane2a3c2502020-06-16 17:48:35 -0700190 }
191}
192
193fn on_transact(
194 service: &dyn ITest,
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700195 code: TransactionCode,
Alice Ryhl8618c482021-11-09 15:35:35 +0000196 _data: &BorrowedParcel<'_>,
197 reply: &mut BorrowedParcel<'_>,
Stephen Cranef2735b42022-01-19 17:49:46 +0000198) -> Result<(), StatusCode> {
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700199 match code.try_into()? {
200 TestTransactionCode::Test => reply.write(&service.test()?),
Stephen Crane2a3297f2021-06-11 16:48:10 -0700201 TestTransactionCode::GetDumpArgs => reply.write(&service.get_dump_args()?),
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700202 TestTransactionCode::GetSelinuxContext => reply.write(&service.get_selinux_context()?),
Alice Ryhl29a50262021-12-10 11:14:32 +0000203 TestTransactionCode::GetIsHandlingTransaction => reply.write(&service.get_is_handling_transaction()?),
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700204 }
Stephen Crane2a3c2502020-06-16 17:48:35 -0700205}
206
207impl ITest for BpTest {
Stephen Cranef2735b42022-01-19 17:49:46 +0000208 fn test(&self) -> Result<String, StatusCode> {
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700209 let reply =
210 self.binder
211 .transact(TestTransactionCode::Test as TransactionCode, 0, |_| Ok(()))?;
212 reply.read()
213 }
214
Stephen Cranef2735b42022-01-19 17:49:46 +0000215 fn get_dump_args(&self) -> Result<Vec<String>, StatusCode> {
Stephen Crane2a3297f2021-06-11 16:48:10 -0700216 let reply =
217 self.binder
218 .transact(TestTransactionCode::GetDumpArgs as TransactionCode, 0, |_| Ok(()))?;
219 reply.read()
220 }
221
Stephen Cranef2735b42022-01-19 17:49:46 +0000222 fn get_selinux_context(&self) -> Result<String, StatusCode> {
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700223 let reply = self.binder.transact(
224 TestTransactionCode::GetSelinuxContext as TransactionCode,
225 0,
226 |_| Ok(()),
227 )?;
Stephen Crane2a3c2502020-06-16 17:48:35 -0700228 reply.read()
229 }
Alice Ryhl29a50262021-12-10 11:14:32 +0000230
Stephen Cranef2735b42022-01-19 17:49:46 +0000231 fn get_is_handling_transaction(&self) -> Result<bool, StatusCode> {
Alice Ryhl29a50262021-12-10 11:14:32 +0000232 let reply = self.binder.transact(
233 TestTransactionCode::GetIsHandlingTransaction as TransactionCode,
234 0,
235 |_| Ok(()),
236 )?;
237 reply.read()
238 }
Stephen Crane2a3c2502020-06-16 17:48:35 -0700239}
240
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000241impl<P: binder::BinderAsyncPool> IATest<P> for BpTest {
Stephen Cranef2735b42022-01-19 17:49:46 +0000242 fn test(&self) -> binder::BoxFuture<'static, Result<String, StatusCode>> {
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000243 let binder = self.binder.clone();
244 P::spawn(
Alice Ryhl8618c482021-11-09 15:35:35 +0000245 move || binder.transact(TestTransactionCode::Test as TransactionCode, 0, |_| Ok(())),
246 |reply| async move { reply?.read() }
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000247 )
248 }
249
Stephen Cranef2735b42022-01-19 17:49:46 +0000250 fn get_dump_args(&self) -> binder::BoxFuture<'static, Result<Vec<String>, StatusCode>> {
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000251 let binder = self.binder.clone();
252 P::spawn(
Alice Ryhl8618c482021-11-09 15:35:35 +0000253 move || binder.transact(TestTransactionCode::GetDumpArgs as TransactionCode, 0, |_| Ok(())),
254 |reply| async move { reply?.read() }
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000255 )
256 }
257
Stephen Cranef2735b42022-01-19 17:49:46 +0000258 fn get_selinux_context(&self) -> binder::BoxFuture<'static, Result<String, StatusCode>> {
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000259 let binder = self.binder.clone();
260 P::spawn(
Alice Ryhl8618c482021-11-09 15:35:35 +0000261 move || binder.transact(TestTransactionCode::GetSelinuxContext as TransactionCode, 0, |_| Ok(())),
262 |reply| async move { reply?.read() }
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000263 )
264 }
Alice Ryhl29a50262021-12-10 11:14:32 +0000265
Stephen Cranef2735b42022-01-19 17:49:46 +0000266 fn get_is_handling_transaction(&self) -> binder::BoxFuture<'static, Result<bool, StatusCode>> {
Alice Ryhl29a50262021-12-10 11:14:32 +0000267 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 Ryhl05f5a2c2021-09-15 12:56:10 +0000273}
274
Stephen Crane2a3c2502020-06-16 17:48:35 -0700275impl ITest for Binder<BnTest> {
Stephen Cranef2735b42022-01-19 17:49:46 +0000276 fn test(&self) -> Result<String, StatusCode> {
Stephen Crane2a3c2502020-06-16 17:48:35 -0700277 self.0.test()
278 }
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700279
Stephen Cranef2735b42022-01-19 17:49:46 +0000280 fn get_dump_args(&self) -> Result<Vec<String>, StatusCode> {
Stephen Crane2a3297f2021-06-11 16:48:10 -0700281 self.0.get_dump_args()
282 }
283
Stephen Cranef2735b42022-01-19 17:49:46 +0000284 fn get_selinux_context(&self) -> Result<String, StatusCode> {
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700285 self.0.get_selinux_context()
286 }
Alice Ryhl29a50262021-12-10 11:14:32 +0000287
Stephen Cranef2735b42022-01-19 17:49:46 +0000288 fn get_is_handling_transaction(&self) -> Result<bool, StatusCode> {
Alice Ryhl29a50262021-12-10 11:14:32 +0000289 self.0.get_is_handling_transaction()
290 }
Stephen Crane2a3c2502020-06-16 17:48:35 -0700291}
292
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000293impl<P: binder::BinderAsyncPool> IATest<P> for Binder<BnTest> {
Stephen Cranef2735b42022-01-19 17:49:46 +0000294 fn test(&self) -> binder::BoxFuture<'static, Result<String, StatusCode>> {
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000295 let res = self.0.test();
296 Box::pin(async move { res })
297 }
298
Stephen Cranef2735b42022-01-19 17:49:46 +0000299 fn get_dump_args(&self) -> binder::BoxFuture<'static, Result<Vec<String>, StatusCode>> {
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000300 let res = self.0.get_dump_args();
301 Box::pin(async move { res })
302 }
303
Stephen Cranef2735b42022-01-19 17:49:46 +0000304 fn get_selinux_context(&self) -> binder::BoxFuture<'static, Result<String, StatusCode>> {
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000305 let res = self.0.get_selinux_context();
306 Box::pin(async move { res })
307 }
Alice Ryhl29a50262021-12-10 11:14:32 +0000308
Stephen Cranef2735b42022-01-19 17:49:46 +0000309 fn get_is_handling_transaction(&self) -> binder::BoxFuture<'static, Result<bool, StatusCode>> {
Alice Ryhl29a50262021-12-10 11:14:32 +0000310 let res = self.0.get_is_handling_transaction();
311 Box::pin(async move { res })
312 }
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000313}
314
Stephen Crane669deb62020-09-10 17:31:39 -0700315/// Trivial testing binder interface
316pub trait ITestSameDescriptor: Interface {}
317
318declare_binder_interface! {
319 ITestSameDescriptor["android.os.ITest"] {
320 native: BnTestSameDescriptor(on_transact_same_descriptor),
321 proxy: BpTestSameDescriptor,
322 }
323}
324
325fn on_transact_same_descriptor(
326 _service: &dyn ITestSameDescriptor,
327 _code: TransactionCode,
Alice Ryhl8618c482021-11-09 15:35:35 +0000328 _data: &BorrowedParcel<'_>,
329 _reply: &mut BorrowedParcel<'_>,
Stephen Cranef2735b42022-01-19 17:49:46 +0000330) -> Result<(), StatusCode> {
Stephen Crane669deb62020-09-10 17:31:39 -0700331 Ok(())
332}
333
334impl ITestSameDescriptor for BpTestSameDescriptor {}
335
336impl ITestSameDescriptor for Binder<BnTestSameDescriptor> {}
337
Stephen Crane7bca1052021-10-25 17:52:51 -0700338declare_binder_enum! {
339 TestEnum : [i32; 3] {
340 FOO = 1,
341 BAR = 2,
342 BAZ = 3,
343 }
344}
345
346declare_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 Crane2a3c2502020-06-16 17:48:35 -0700355#[cfg(test)]
356mod tests {
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700357 use selinux_bindgen as selinux_sys;
358 use std::ffi::CStr;
Stephen Crane2a3c2502020-06-16 17:48:35 -0700359 use std::fs::File;
360 use std::process::{Child, Command};
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700361 use std::ptr;
Stephen Crane2a3c2502020-06-16 17:48:35 -0700362 use std::sync::atomic::{AtomicBool, Ordering};
363 use std::sync::Arc;
364 use std::thread;
365 use std::time::Duration;
366
Andrew Walbran12400d82021-03-04 17:04:34 +0000367 use binder::{
Stephen Cranef2735b42022-01-19 17:49:46 +0000368 BinderFeatures, DeathRecipient, FromIBinder, IBinder, Interface, SpIBinder, StatusCode,
369 Strong,
Andrew Walbran12400d82021-03-04 17:04:34 +0000370 };
Stephen Cranef2735b42022-01-19 17:49:46 +0000371 // 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 Crane2a3c2502020-06-16 17:48:35 -0700374
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000375 use binder_tokio::Tokio;
376
377 use super::{BnTest, ITest, IATest, ITestSameDescriptor, TestService, RUST_SERVICE_BINARY};
Stephen Crane2a3c2502020-06-16 17:48:35 -0700378
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 Ryhl05f5a2c2021-09-15 12:56:10 +0000425 assert_eq!(
426 binder::get_interface::<dyn IATest<Tokio>>("this_service_does_not_exist").err(),
427 Some(StatusCode::NAME_NOT_FOUND)
428 );
Stephen Crane2a3c2502020-06-16 17:48:35 -0700429
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 Ryhl05f5a2c2021-09-15 12:56:10 +0000435 assert_eq!(
436 binder::get_interface::<dyn IATest<Tokio>>("manager").err(),
437 Some(StatusCode::BAD_TYPE)
438 );
Stephen Crane2a3c2502020-06-16 17:48:35 -0700439 }
440
Alice Ryhl929804f2021-11-02 12:04:39 +0000441 #[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 Crane2a3c2502020-06-16 17:48:35 -0700468 #[test]
Andrew Walbranc3ce5c32021-06-03 16:15:56 +0000469 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 Ryhl05f5a2c2021-09-15 12:56:10 +0000480 assert_eq!(
481 binder::wait_for_interface::<dyn IATest<Tokio>>("manager").err(),
482 Some(StatusCode::BAD_TYPE)
483 );
Andrew Walbranc3ce5c32021-06-03 16:15:56 +0000484 }
485
486 #[test]
Stephen Crane2a3c2502020-06-16 17:48:35 -0700487 fn trivial_client() {
488 let service_name = "trivial_client_test";
489 let _process = ScopedServiceProcess::new(service_name);
Stephen Craneddb3e6d2020-12-18 13:27:22 -0800490 let test_client: Strong<dyn ITest> =
Stephen Crane2a3c2502020-06-16 17:48:35 -0700491 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 Ryhl05f5a2c2021-09-15 12:56:10 +0000495 #[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 Ryhl929804f2021-11-02 12:04:39 +0000500 binder_tokio::get_interface(service_name).await.expect("Did not get manager binder service");
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000501 assert_eq!(test_client.test().await.unwrap(), "trivial_client_test");
502 }
503
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700504 #[test]
Andrew Walbranc3ce5c32021-06-03 16:15:56 +0000505 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 Ryhl05f5a2c2021-09-15 12:56:10 +0000513 #[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 Ryhl929804f2021-11-02 12:04:39 +0000518 binder_tokio::wait_for_interface(service_name).await.expect("Did not get manager binder service");
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000519 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 Walbranc3ce5c32021-06-03 16:15:56 +0000533 #[test]
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700534 fn get_selinux_context() {
535 let service_name = "get_selinux_context";
536 let _process = ScopedServiceProcess::new(service_name);
Stephen Craneddb3e6d2020-12-18 13:27:22 -0800537 let test_client: Strong<dyn ITest> =
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700538 binder::get_interface(service_name).expect("Did not get manager binder service");
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700539 assert_eq!(
540 test_client.get_selinux_context().unwrap(),
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000541 get_expected_selinux_context()
542 );
543 }
544
545 #[tokio::test]
546 async fn get_selinux_context_async() {
Alice Ryhl29a50262021-12-10 11:14:32 +0000547 let service_name = "get_selinux_context_async";
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000548 let _process = ScopedServiceProcess::new(service_name);
549 let test_client: Strong<dyn IATest<Tokio>> =
Alice Ryhl929804f2021-11-02 12:04:39 +0000550 binder_tokio::get_interface(service_name).await.expect("Did not get manager binder service");
Alice Ryhl05f5a2c2021-09-15 12:56:10 +0000551 assert_eq!(
552 test_client.get_selinux_context().await.unwrap(),
553 get_expected_selinux_context()
Janis Danisevskis798a09a2020-08-18 08:35:38 -0700554 );
555 }
556
Alice Ryhlc1736842021-11-23 12:38:51 +0000557 #[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 Ryhlea9d9d22021-08-27 07:51:30 +0000583 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 Crane2a3c2502020-06-16 17:48:35 -0700613 let binder_died = Arc::new(AtomicBool::new(false));
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000614 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 Crane2a3c2502020-06-16 17:48:35 -0700624
625 let mut death_recipient = {
626 let flag = binder_died.clone();
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000627 let set_on_drop = SetOnDrop {
628 binder_dealloc: binder_dealloc.clone(),
629 };
Stephen Crane2a3c2502020-06-16 17:48:35 -0700630 DeathRecipient::new(move || {
631 flag.store(true, Ordering::Relaxed);
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000632 // 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 Crane2a3c2502020-06-16 17:48:35 -0700635 })
636 };
637
638 binder
639 .link_to_death(&mut death_recipient)
640 .expect("link_to_death failed");
641
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000642 let bools = Bools {
643 binder_died,
644 binder_dealloc,
645 };
646
647 (bools, death_recipient)
Stephen Crane2a3c2502020-06-16 17:48:35 -0700648 }
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 Ryhlea9d9d22021-08-27 07:51:30 +0000660 let (bools, recipient) = register_death_notification(&mut remote);
Stephen Crane2a3c2502020-06-16 17:48:35 -0700661
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 Ryhlea9d9d22021-08-27 07:51:30 +0000670 bools.assert_died();
671 bools.assert_not_dropped();
672
673 drop(recipient);
674
675 bools.assert_dropped();
Stephen Crane2a3c2502020-06-16 17:48:35 -0700676 }
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 Ryhlea9d9d22021-08-27 07:51:30 +0000687 let (bools, mut recipient) = register_death_notification(&mut remote);
Stephen Crane2a3c2502020-06-16 17:48:35 -0700688
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 Ryhlea9d9d22021-08-27 07:51:30 +0000702 !bools.is_dead(),
Stephen Crane2a3c2502020-06-16 17:48:35 -0700703 "Received unexpected death notification after unlinking",
704 );
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000705
706 bools.assert_not_dropped();
707 drop(recipient);
708 bools.assert_dropped();
Stephen Crane2a3c2502020-06-16 17:48:35 -0700709 }
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 Ryhlea9d9d22021-08-27 07:51:30 +0000720 let (bools, recipient) = register_death_notification(&mut remote);
Stephen Crane2a3c2502020-06-16 17:48:35 -0700721
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 Ryhlea9d9d22021-08-27 07:51:30 +0000733 !bools.is_dead(),
Stephen Crane2a3c2502020-06-16 17:48:35 -0700734 "Received unexpected death notification after dropping remote handle"
735 );
Alice Ryhlea9d9d22021-08-27 07:51:30 +0000736
737 bools.assert_not_dropped();
738 drop(recipient);
739 bools.assert_dropped();
Stephen Crane2a3c2502020-06-16 17:48:35 -0700740 }
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 Crane2a3297f2021-06-11 16:48:10 -0700750 let test_client: Strong<dyn ITest> =
Stephen Cranef2735b42022-01-19 17:49:46 +0000751 binder::get_interface(service_name).expect("Did not get test binder service");
Stephen Crane2a3297f2021-06-11 16:48:10 -0700752 let mut remote = test_client.as_binder();
Stephen Crane2a3c2502020-06-16 17:48:35 -0700753 assert!(remote.is_binder_alive());
754 remote.ping_binder().expect("Could not ping remote service");
755
Stephen Crane2a3297f2021-06-11 16:48:10 -0700756 let dump_args = ["dump", "args", "for", "testing"];
757
Stephen Crane2a3c2502020-06-16 17:48:35 -0700758 let null_out = File::open("/dev/null").expect("Could not open /dev/null");
759 remote
Stephen Crane2a3297f2021-06-11 16:48:10 -0700760 .dump(&null_out, &dump_args)
Stephen Crane2a3c2502020-06-16 17:48:35 -0700761 .expect("Could not dump remote service");
Stephen Crane2a3297f2021-06-11 16:48:10 -0700762
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 Crane2a3c2502020-06-16 17:48:35 -0700765 }
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 Craneddb3e6d2020-12-18 13:27:22 -0800805 let extension: Strong<dyn ITest> = FromIBinder::try_from(extension)
Stephen Crane2a3c2502020-06-16 17:48:35 -0700806 .expect("Extension could not be converted to the expected interface");
807
808 assert_eq!(extension.test().unwrap(), extension_name);
809 }
810 }
Stephen Crane669deb62020-09-10 17:31:39 -0700811
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 Crane2a3297f2021-06-11 16:48:10 -0700825 let service = Binder::new(BnTest(Box::new(TestService::new("testing_service"))));
Stephen Crane669deb62020-09-10 17:31:39 -0700826
827 // This should succeed although we will have to treat the service as
828 // remote.
Andrew Walbran12400d82021-03-04 17:04:34 +0000829 let _interface: Strong<dyn ITestSameDescriptor> =
830 FromIBinder::try_from(service.as_binder())
831 .expect("Could not re-interpret service as the ITestSameDescriptor interface");
Stephen Crane669deb62020-09-10 17:31:39 -0700832 }
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 Walbran88eca4f2021-04-13 14:26:01 +0000838 let service_ibinder = BnTest::new_binder(
Stephen Crane2a3297f2021-06-11 16:48:10 -0700839 TestService::new(service_name),
Andrew Walbran88eca4f2021-04-13 14:26:01 +0000840 BinderFeatures::default(),
841 )
Andrew Walbran12400d82021-03-04 17:04:34 +0000842 .as_binder();
Stephen Crane669deb62020-09-10 17:31:39 -0700843
Andrew Walbran12400d82021-03-04 17:04:34 +0000844 let service: Strong<dyn ITest> = service_ibinder
845 .into_interface()
Stephen Crane669deb62020-09-10 17:31:39 -0700846 .expect("Could not reassociate the generic ibinder");
847
848 assert_eq!(service.test().unwrap(), service_name);
849 }
Stephen Craneddb3e6d2020-12-18 13:27:22 -0800850
851 #[test]
852 fn weak_binder_upgrade() {
853 let service_name = "testing_service";
Andrew Walbran88eca4f2021-04-13 14:26:01 +0000854 let service = BnTest::new_binder(
Stephen Crane2a3297f2021-06-11 16:48:10 -0700855 TestService::new(service_name),
Andrew Walbran88eca4f2021-04-13 14:26:01 +0000856 BinderFeatures::default(),
857 );
Stephen Craneddb3e6d2020-12-18 13:27:22 -0800858
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 Walbran88eca4f2021-04-13 14:26:01 +0000870 let service = BnTest::new_binder(
Stephen Crane2a3297f2021-06-11 16:48:10 -0700871 TestService::new(service_name),
Andrew Walbran88eca4f2021-04-13 14:26:01 +0000872 BinderFeatures::default(),
873 );
Stephen Craneddb3e6d2020-12-18 13:27:22 -0800874
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 Walbran88eca4f2021-04-13 14:26:01 +0000884 let service = BnTest::new_binder(
Stephen Crane2a3297f2021-06-11 16:48:10 -0700885 TestService::new(service_name),
Andrew Walbran88eca4f2021-04-13 14:26:01 +0000886 BinderFeatures::default(),
887 );
Stephen Craneddb3e6d2020-12-18 13:27:22 -0800888
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 Walbran88eca4f2021-04-13 14:26:01 +0000903 let service1 = BnTest::new_binder(
Stephen Crane2a3297f2021-06-11 16:48:10 -0700904 TestService::new("testing_service1"),
Andrew Walbran88eca4f2021-04-13 14:26:01 +0000905 BinderFeatures::default(),
906 );
907 let service2 = BnTest::new_binder(
Stephen Crane2a3297f2021-06-11 16:48:10 -0700908 TestService::new("testing_service2"),
Andrew Walbran88eca4f2021-04-13 14:26:01 +0000909 BinderFeatures::default(),
910 );
Stephen Craneddb3e6d2020-12-18 13:27:22 -0800911
912 assert!(!(service1 < service1));
913 assert!(!(service1 > service1));
914 assert_eq!(service1 < service2, !(service2 < service1));
915 }
Alice Ryhlfeba6ca2021-08-19 10:47:04 +0000916
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 Cranef2735b42022-01-19 17:49:46 +0000932 let res = service2.submit_transact(super::TestTransactionCode::Test as TransactionCode, parcel, 0);
Alice Ryhlfeba6ca2021-08-19 10:47:04 +0000933
934 match res {
935 Ok(_) => panic!("submit_transact should fail"),
936 Err(err) => assert_eq!(err, binder::StatusCode::BAD_VALUE),
937 }
938 }
Alice Ryhl29a50262021-12-10 11:14:32 +0000939
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 Crane2a3c2502020-06-16 17:48:35 -0700980}