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 //! Test Rust client for the AIDL compiler.
18
19 use ::binder::{binder_impl::Parcel, Parcelable};
20 use aidl_test_fixedsizearray::aidl::android::aidl::fixedsizearray::FixedSizeArrayExample::{
21 FixedSizeArrayExample,
22 IRepeatFixedSizeArray::{BpRepeatFixedSizeArray, IRepeatFixedSizeArray},
23 IntParcelable::IntParcelable,
24 };
25 use aidl_test_interface::aidl::android::aidl::tests::nested::{
26 INestedService, ParcelableWithNested,
27 };
28 use aidl_test_interface::aidl::android::aidl::tests::unions::EnumUnion::EnumUnion;
29 use aidl_test_interface::aidl::android::aidl::tests::INewName::{self, BpNewName};
30 use aidl_test_interface::aidl::android::aidl::tests::IOldName::{self, BpOldName};
31 use aidl_test_interface::aidl::android::aidl::tests::ITestService::{
32 self, BpTestService, Empty::Empty, ITestServiceDefault, ITestServiceDefaultRef,
33 };
34 use aidl_test_interface::aidl::android::aidl::tests::{
35 extension::ExtendableParcelable::ExtendableParcelable, extension::MyExt::MyExt,
36 extension::MyExt2::MyExt2, extension::MyExtLike::MyExtLike, BackendType::BackendType,
37 ByteEnum::ByteEnum, IntEnum::IntEnum, LongEnum::LongEnum, RecursiveList::RecursiveList,
38 StructuredParcelable, Union,
39 };
40 use aidl_test_interface::binder::{self, BinderFeatures, IBinder, Interface};
41 use aidl_test_nonvintf_parcelable::aidl::android::aidl::tests::nonvintf::{
42 NonVintfExtendableParcelable::NonVintfExtendableParcelable,
43 NonVintfParcelable::NonVintfParcelable,
44 };
45 use aidl_test_unstable_parcelable::aidl::android::aidl::tests::unstable::{
46 UnstableExtendableParcelable::UnstableExtendableParcelable,
47 UnstableParcelable::UnstableParcelable,
48 };
49 use aidl_test_versioned_interface::aidl::android::aidl::versioned::tests::{
50 BazUnion::BazUnion, Foo::Foo, IFooInterface, IFooInterface::BpFooInterface,
51 };
52 use aidl_test_vintf_parcelable::aidl::android::aidl::tests::vintf::{
53 VintfExtendableParcelable::VintfExtendableParcelable, VintfParcelable::VintfParcelable,
54 };
55 use android_aidl_test_trunk::aidl::android::aidl::test::trunk::{
56 ITrunkStableTest::BpTrunkStableTest, ITrunkStableTest::IMyCallback,
57 ITrunkStableTest::ITrunkStableTest, ITrunkStableTest::MyEnum::MyEnum,
58 ITrunkStableTest::MyOtherParcelable::MyOtherParcelable,
59 ITrunkStableTest::MyParcelable::MyParcelable, ITrunkStableTest::MyUnion::MyUnion,
60 };
61 use simple_parcelable::SimpleParcelable;
62
63 use std::fs::File;
64 use std::io::{Read, Write};
65 use std::os::fd::{FromRawFd, OwnedFd};
66 use std::sync::{Arc, Mutex};
67
get_test_service() -> binder::Strong<dyn ITestService::ITestService>68 fn get_test_service() -> binder::Strong<dyn ITestService::ITestService> {
69 binder::wait_for_interface(<BpTestService as ITestService::ITestService>::get_descriptor())
70 .expect("did not get binder service")
71 }
72
get_test_trunk_stable_service() -> binder::Strong<dyn ITrunkStableTest>73 fn get_test_trunk_stable_service() -> binder::Strong<dyn ITrunkStableTest> {
74 binder::wait_for_interface(<BpTrunkStableTest as ITrunkStableTest>::get_descriptor())
75 .expect("did not get binder service")
76 }
77
78 #[test]
test_constants()79 fn test_constants() {
80 assert_eq!(ITestService::A1, 1);
81 assert_eq!(ITestService::A2, 1);
82 assert_eq!(ITestService::A3, 1);
83 assert_eq!(ITestService::A4, 1);
84 assert_eq!(ITestService::A5, 1);
85 assert_eq!(ITestService::A6, 1);
86 assert_eq!(ITestService::A7, 1);
87 assert_eq!(ITestService::A8, 1);
88 assert_eq!(ITestService::A9, 1);
89 assert_eq!(ITestService::A10, 1);
90 assert_eq!(ITestService::A11, 1);
91 assert_eq!(ITestService::A12, 1);
92 assert_eq!(ITestService::A13, 1);
93 assert_eq!(ITestService::A14, 1);
94 assert_eq!(ITestService::A15, 1);
95 assert_eq!(ITestService::A16, 1);
96 assert_eq!(ITestService::A17, 1);
97 assert_eq!(ITestService::A18, 1);
98 assert_eq!(ITestService::A19, 1);
99 assert_eq!(ITestService::A20, 1);
100 assert_eq!(ITestService::A21, 1);
101 assert_eq!(ITestService::A22, 1);
102 assert_eq!(ITestService::A23, 1);
103 assert_eq!(ITestService::A24, 1);
104 assert_eq!(ITestService::A25, 1);
105 assert_eq!(ITestService::A26, 1);
106 assert_eq!(ITestService::A27, 1);
107 assert_eq!(ITestService::A28, 1);
108 assert_eq!(ITestService::A29, 1);
109 assert_eq!(ITestService::A30, 1);
110 assert_eq!(ITestService::A31, 1);
111 assert_eq!(ITestService::A32, 1);
112 assert_eq!(ITestService::A33, 1);
113 assert_eq!(ITestService::A34, 1);
114 assert_eq!(ITestService::A35, 1);
115 assert_eq!(ITestService::A36, 1);
116 assert_eq!(ITestService::A37, 1);
117 assert_eq!(ITestService::A38, 1);
118 assert_eq!(ITestService::A39, 1);
119 assert_eq!(ITestService::A40, 1);
120 assert_eq!(ITestService::A41, 1);
121 assert_eq!(ITestService::A42, 1);
122 assert_eq!(ITestService::A43, 1);
123 assert_eq!(ITestService::A44, 1);
124 assert_eq!(ITestService::A45, 1);
125 assert_eq!(ITestService::A46, 1);
126 assert_eq!(ITestService::A47, 1);
127 assert_eq!(ITestService::A48, 1);
128 assert_eq!(ITestService::A49, 1);
129 assert_eq!(ITestService::A50, 1);
130 assert_eq!(ITestService::A51, 1);
131 assert_eq!(ITestService::A52, 1);
132 assert_eq!(ITestService::A53, 1);
133 assert_eq!(ITestService::A54, 1);
134 assert_eq!(ITestService::A55, 1);
135 assert_eq!(ITestService::A56, 1);
136 assert_eq!(ITestService::A57, 1);
137 assert_eq!(ITestService::FLOAT_CONSTANT4, 2.2_f32);
138 assert_eq!(ITestService::FLOAT_CONSTANT5, -2.2_f32);
139 assert_eq!(ITestService::DOUBLE_CONSTANT4, 2.2_f64);
140 assert_eq!(ITestService::DOUBLE_CONSTANT5, -2.2_f64);
141 }
142
143 #[test]
test_oneway()144 fn test_oneway() {
145 let result = get_test_service().TestOneway();
146 assert_eq!(result, Ok(()));
147 }
148
149 macro_rules! test_primitive {
150 ($test:ident, $func:ident, $value:expr) => {
151 #[test]
152 fn $test() {
153 let value = $value;
154 let result = get_test_service().$func(value);
155 assert_eq!(result, Ok(value));
156 }
157 };
158 }
159
160 test_primitive! {test_primitive_bool_false, RepeatBoolean, false}
161 test_primitive! {test_primitive_bool_true, RepeatBoolean, true}
162 test_primitive! {test_primitive_byte, RepeatByte, -128i8}
163 test_primitive! {test_primitive_char, RepeatChar, 'A' as u16}
164 test_primitive! {test_primitive_int, RepeatInt, 1i32 << 30}
165 test_primitive! {test_primitive_long, RepeatLong, 1i64 << 60}
166 test_primitive! {test_primitive_float, RepeatFloat, 1.0f32 / 3.0f32}
167 test_primitive! {test_primitive_double, RepeatDouble, 1.0f64 / 3.0f64}
168 test_primitive! {test_primitive_byte_constant, RepeatByte, ITestService::BYTE_CONSTANT}
169 test_primitive! {test_primitive_constant1, RepeatInt, ITestService::CONSTANT}
170 test_primitive! {test_primitive_constant2, RepeatInt, ITestService::CONSTANT2}
171 test_primitive! {test_primitive_constant3, RepeatInt, ITestService::CONSTANT3}
172 test_primitive! {test_primitive_constant4, RepeatInt, ITestService::CONSTANT4}
173 test_primitive! {test_primitive_constant5, RepeatInt, ITestService::CONSTANT5}
174 test_primitive! {test_primitive_constant6, RepeatInt, ITestService::CONSTANT6}
175 test_primitive! {test_primitive_constant7, RepeatInt, ITestService::CONSTANT7}
176 test_primitive! {test_primitive_constant8, RepeatInt, ITestService::CONSTANT8}
177 test_primitive! {test_primitive_constant9, RepeatInt, ITestService::CONSTANT9}
178 test_primitive! {test_primitive_constant10, RepeatInt, ITestService::CONSTANT10}
179 test_primitive! {test_primitive_constant11, RepeatInt, ITestService::CONSTANT11}
180 test_primitive! {test_primitive_constant12, RepeatInt, ITestService::CONSTANT12}
181 test_primitive! {test_primitive_long_constant, RepeatLong, ITestService::LONG_CONSTANT}
182 test_primitive! {test_primitive_byte_enum, RepeatByteEnum, ByteEnum::FOO}
183 test_primitive! {test_primitive_int_enum, RepeatIntEnum, IntEnum::BAR}
184 test_primitive! {test_primitive_long_enum, RepeatLongEnum, LongEnum::FOO}
185 test_primitive! {test_primitive_float_constant, RepeatFloat, ITestService::FLOAT_CONSTANT}
186 test_primitive! {test_primitive_float_constant2, RepeatFloat, ITestService::FLOAT_CONSTANT2}
187 test_primitive! {test_primitive_float_constant3, RepeatFloat, ITestService::FLOAT_CONSTANT3}
188 test_primitive! {test_primitive_float_constant4, RepeatFloat, ITestService::FLOAT_CONSTANT4}
189 test_primitive! {test_primitive_float_constant5, RepeatFloat, ITestService::FLOAT_CONSTANT5}
190 test_primitive! {test_primitive_float_constant6, RepeatFloat, ITestService::FLOAT_CONSTANT6}
191 test_primitive! {test_primitive_float_constant7, RepeatFloat, ITestService::FLOAT_CONSTANT7}
192 test_primitive! {test_primitive_double_constant, RepeatDouble, ITestService::DOUBLE_CONSTANT}
193 test_primitive! {test_primitive_double_constant2, RepeatDouble, ITestService::DOUBLE_CONSTANT2}
194 test_primitive! {test_primitive_double_constant3, RepeatDouble, ITestService::DOUBLE_CONSTANT3}
195 test_primitive! {test_primitive_double_constant4, RepeatDouble, ITestService::DOUBLE_CONSTANT4}
196 test_primitive! {test_primitive_double_constant5, RepeatDouble, ITestService::DOUBLE_CONSTANT5}
197 test_primitive! {test_primitive_double_constant6, RepeatDouble, ITestService::DOUBLE_CONSTANT6}
198 test_primitive! {test_primitive_double_constant7, RepeatDouble, ITestService::DOUBLE_CONSTANT7}
199
200 #[test]
test_repeat_string()201 fn test_repeat_string() {
202 let service = get_test_service();
203 let inputs = [
204 "typical string".into(),
205 String::new(),
206 "\0\0".into(),
207 // This is actually two unicode code points:
208 // U+10437: The 'small letter yee' character in the deseret alphabet
209 // U+20AC: A euro sign
210 String::from_utf16(&[0xD801, 0xDC37, 0x20AC]).expect("error converting string"),
211 ITestService::STRING_CONSTANT.into(),
212 ITestService::STRING_CONSTANT2.into(),
213 ];
214 for input in &inputs {
215 let result = service.RepeatString(input);
216 assert_eq!(result.as_ref(), Ok(input));
217 }
218 }
219
220 #[test]
test_repeat_parcelable()221 fn test_repeat_parcelable() {
222 let service = get_test_service();
223 let input = SimpleParcelable { name: "foo".to_string(), number: 42 };
224 let mut out_param = SimpleParcelable::default();
225 let returned = service.RepeatSimpleParcelable(&input, &mut out_param);
226 assert_eq!(returned, Ok(input.clone()));
227 assert_eq!(out_param, input);
228 }
229
230 macro_rules! test_reverse_array {
231 ($test:ident, $func:ident, $array:expr) => {
232 #[test]
233 fn $test() {
234 let mut array = $array.to_vec();
235
236 // Java needs initial values here (can't resize arrays)
237 let mut repeated = vec![Default::default(); array.len()];
238
239 let result = get_test_service().$func(&array, &mut repeated);
240 assert_eq!(repeated, array);
241 array.reverse();
242 assert_eq!(result, Ok(array));
243 }
244 };
245 }
246
247 test_reverse_array! {test_array_boolean, ReverseBoolean, [true, false, false]}
248 test_reverse_array! {test_array_byte, ReverseByte, [255u8, 0u8, 127u8]}
249 test_reverse_array! {
250 service,
251 ReverseChar,
252 ['A' as u16, 'B' as u16, 'C' as u16]
253 }
254 test_reverse_array! {test_array_int, ReverseInt, [1, 2, 3]}
255 test_reverse_array! {test_array_long, ReverseLong, [-1i64, 0i64, 1i64 << 60]}
256 test_reverse_array! {test_array_float, ReverseFloat, [-0.3f32, -0.7f32, 8.0f32]}
257 test_reverse_array! {
258 test_array_double,
259 ReverseDouble,
260 [1.0f64 / 3.0f64, 1.0f64 / 7.0f64, 42.0f64]
261 }
262 test_reverse_array! {
263 test_array_string,
264 ReverseString,
265 ["f".into(), "a".into(), "b".into()]
266 }
267 test_reverse_array! {
268 test_array_byte_enum,
269 ReverseByteEnum,
270 [ByteEnum::FOO, ByteEnum::BAR, ByteEnum::BAR]
271 }
272 test_reverse_array! {
273 test_array_byte_enum_values,
274 ReverseByteEnum,
275 ByteEnum::enum_values()
276 }
277 test_reverse_array! {
278 test_array_byte_enum_v2,
279 ReverseByteEnum,
280 [ByteEnum::FOO, ByteEnum::BAR, ByteEnum::BAZ]
281 }
282 test_reverse_array! {
283 test_array_int_enum,
284 ReverseIntEnum,
285 [IntEnum::FOO, IntEnum::BAR, IntEnum::BAR]
286 }
287 test_reverse_array! {
288 test_array_long_enum,
289 ReverseLongEnum,
290 [LongEnum::FOO, LongEnum::BAR, LongEnum::BAR]
291 }
292 test_reverse_array! {
293 test_array_string_list,
294 ReverseStringList,
295 ["f".into(), "a".into(), "b".into()]
296 }
297 test_reverse_array! {
298 test_array_utf8_string,
299 ReverseUtf8CppString,
300 [
301 "a".into(),
302 String::new(),
303 std::str::from_utf8(&[0xC3, 0xB8])
304 .expect("error converting string")
305 .into(),
306 ]
307 }
308 test_reverse_array! {
309 test_reverse_parcelable,
310 ReverseSimpleParcelables,
311 [
312 SimpleParcelable {name: "a".to_string(), number: 1 },
313 SimpleParcelable {name: "b".to_string(), number: 2 },
314 SimpleParcelable {name: "c".to_string(), number: 3 },
315 ]
316 }
317
318 #[test]
test_binder_exchange()319 fn test_binder_exchange() {
320 const NAME: &str = "Smythe";
321 let service = get_test_service();
322 let got = service.GetOtherTestService(NAME).expect("error calling GetOtherTestService");
323 assert_eq!(got.GetName().as_ref().map(String::as_ref), Ok(NAME));
324 assert_eq!(service.VerifyName(&got, NAME), Ok(true));
325 }
326
327 #[test]
test_binder_array_exchange()328 fn test_binder_array_exchange() {
329 let names = vec!["Fizz".into(), "Buzz".into()];
330 let service = get_test_service();
331 let got = service.GetInterfaceArray(&names).expect("error calling GetInterfaceArray");
332 assert_eq!(got.iter().map(|s| s.GetName()).collect::<Result<Vec<_>, _>>(), Ok(names.clone()));
333 assert_eq!(service.VerifyNamesWithInterfaceArray(&got, &names), Ok(true));
334 }
335
336 #[test]
test_binder_nullable_array_exchange()337 fn test_binder_nullable_array_exchange() {
338 let names = vec![Some("Fizz".into()), None, Some("Buzz".into())];
339 let service = get_test_service();
340 let got = service
341 .GetNullableInterfaceArray(Some(&names))
342 .expect("error calling GetNullableInterfaceArray");
343 assert_eq!(
344 got.as_ref().map(|arr| arr
345 .iter()
346 .map(|opt_s| opt_s.as_ref().map(|s| s.GetName().expect("error calling GetName")))
347 .collect::<Vec<_>>()),
348 Some(names.clone())
349 );
350 assert_eq!(
351 service.VerifyNamesWithNullableInterfaceArray(got.as_ref().map(|v| &v[..]), Some(&names)),
352 Ok(true)
353 );
354 }
355
356 #[test]
test_interface_list_exchange()357 fn test_interface_list_exchange() {
358 let names = vec![Some("Fizz".into()), None, Some("Buzz".into())];
359 let service = get_test_service();
360 let got = service.GetInterfaceList(Some(&names)).expect("error calling GetInterfaceList");
361 assert_eq!(
362 got.as_ref().map(|arr| arr
363 .iter()
364 .map(|opt_s| opt_s.as_ref().map(|s| s.GetName().expect("error calling GetName")))
365 .collect::<Vec<_>>()),
366 Some(names.clone())
367 );
368 assert_eq!(
369 service.VerifyNamesWithInterfaceList(got.as_ref().map(|v| &v[..]), Some(&names)),
370 Ok(true)
371 );
372 }
373
build_pipe() -> (OwnedFd, OwnedFd)374 fn build_pipe() -> (OwnedFd, OwnedFd) {
375 // Safety: we get two file descriptors from pipe()
376 // and pass them after checking if the function returned
377 // without an error, so the descriptors should be valid
378 // by that point
379 unsafe {
380 let mut fds = [0, 0];
381 if libc::pipe(fds.as_mut_ptr()) != 0 {
382 panic!("pipe() error");
383 }
384 (OwnedFd::from_raw_fd(fds[0]), OwnedFd::from_raw_fd(fds[1]))
385 }
386 }
387
388 /// Helper function that constructs a `File` from a `ParcelFileDescriptor`.
389 ///
390 /// This is needed because `File` is currently the way to read and write
391 /// to pipes using the `Read` and `Write` traits.
file_from_pfd(fd: &binder::ParcelFileDescriptor) -> File392 fn file_from_pfd(fd: &binder::ParcelFileDescriptor) -> File {
393 fd.as_ref().try_clone().expect("failed to clone file descriptor").into()
394 }
395
396 #[test]
test_parcel_file_descriptor()397 fn test_parcel_file_descriptor() {
398 let service = get_test_service();
399 let (read_fd, write_fd) = build_pipe();
400 let mut read_file = File::from(read_fd);
401
402 let write_pfd = binder::ParcelFileDescriptor::new(write_fd);
403 let result_pfd = service
404 .RepeatParcelFileDescriptor(&write_pfd)
405 .expect("error calling RepeatParcelFileDescriptor");
406
407 const TEST_DATA: &[u8] = b"FrazzleSnazzleFlimFlamFlibbityGumboChops";
408 file_from_pfd(&result_pfd).write_all(TEST_DATA).expect("error writing to pipe");
409
410 let mut buf = [0u8; TEST_DATA.len()];
411 read_file.read_exact(&mut buf).expect("error reading from pipe");
412 assert_eq!(&buf[..], TEST_DATA);
413 }
414
415 #[test]
test_parcel_file_descriptor_array()416 fn test_parcel_file_descriptor_array() {
417 let service = get_test_service();
418
419 let (read_fd, write_fd) = build_pipe();
420 let input =
421 [binder::ParcelFileDescriptor::new(read_fd), binder::ParcelFileDescriptor::new(write_fd)];
422
423 let mut repeated = vec![];
424
425 let backend = service.getBackendType().expect("error getting backend type");
426 if backend == BackendType::JAVA {
427 // Java needs initial values here (can't resize arrays)
428 // Other backends can't accept 'None', but we can use it in Java for convenience, rather
429 // than creating file descriptors.
430 repeated = vec![None, None];
431 }
432
433 let result = service
434 .ReverseParcelFileDescriptorArray(&input[..], &mut repeated)
435 .expect("error calling ReverseParcelFileDescriptorArray");
436
437 file_from_pfd(&input[1]).write_all(b"First").expect("error writing to pipe");
438 file_from_pfd(repeated[1].as_ref().expect("received None for ParcelFileDescriptor"))
439 .write_all(b"Second")
440 .expect("error writing to pipe");
441 file_from_pfd(&result[0]).write_all(b"Third").expect("error writing to pipe");
442
443 const TEST_DATA: &[u8] = b"FirstSecondThird";
444 let mut buf = [0u8; TEST_DATA.len()];
445 file_from_pfd(&input[0]).read_exact(&mut buf).expect("error reading from pipe");
446 assert_eq!(&buf[..], TEST_DATA);
447 }
448
449 #[test]
test_service_specific_exception()450 fn test_service_specific_exception() {
451 let service = get_test_service();
452
453 for i in -1..2 {
454 let result = service.ThrowServiceException(i);
455 assert!(result.is_err());
456
457 let status = result.unwrap_err();
458 assert_eq!(status.exception_code(), binder::ExceptionCode::SERVICE_SPECIFIC);
459 assert_eq!(status.service_specific_error(), i);
460 }
461 }
462
463 macro_rules! test_nullable {
464 ($test:ident, $func:ident, $value:expr) => {
465 #[test]
466 fn $test() {
467 let service = get_test_service();
468 let value = Some($value);
469 let result = service.$func(value.as_deref());
470 assert_eq!(result, Ok(value));
471
472 let result = service.$func(None);
473 assert_eq!(result, Ok(None));
474 }
475 };
476 }
477
478 test_nullable! {test_nullable_array_int, RepeatNullableIntArray, vec![1, 2, 3]}
479 test_nullable! {
480 test_nullable_array_byte_enum,
481 RepeatNullableByteEnumArray,
482 vec![ByteEnum::FOO, ByteEnum::BAR]
483 }
484 test_nullable! {
485 test_nullable_array_int_enum,
486 RepeatNullableIntEnumArray,
487 vec![IntEnum::FOO, IntEnum::BAR]
488 }
489 test_nullable! {
490 test_nullable_array_long_enum,
491 RepeatNullableLongEnumArray,
492 vec![LongEnum::FOO, LongEnum::BAR]
493 }
494 test_nullable! {test_nullable_string, RepeatNullableString, "Blooob".into()}
495 test_nullable! {
496 test_nullable_string_list,
497 RepeatNullableStringList,
498 vec![
499 Some("Wat".into()),
500 Some("Blooob".into()),
501 Some("Wat".into()),
502 None,
503 Some("YEAH".into()),
504 Some("OKAAAAY".into()),
505 ]
506 }
507
508 #[test]
test_nullable_parcelable()509 fn test_nullable_parcelable() {
510 let value = Empty {};
511
512 let service = get_test_service();
513 let value = Some(value);
514 let result = service.RepeatNullableParcelable(value.as_ref());
515 assert_eq!(result, Ok(value));
516
517 let result = service.RepeatNullableParcelable(None);
518 assert_eq!(result, Ok(None));
519 }
520
521 test_nullable! {
522 test_nullable_parcelable_array,
523 RepeatNullableParcelableArray,
524 vec![
525 Some(Empty {}),
526 None,
527 ]
528 }
529
530 test_nullable! {
531 test_nullable_parcelable_list,
532 RepeatNullableParcelableList,
533 vec![
534 Some(Empty {}),
535 None,
536 ]
537 }
538
539 #[test]
test_binder()540 fn test_binder() {
541 let service = get_test_service();
542 assert!(service.GetCallback(true).expect("error calling GetCallback").is_none());
543 let callback = service
544 .GetCallback(false)
545 .expect("error calling GetCallback")
546 .expect("expected Some from GetCallback");
547
548 // We don't have any place to get a fresh `SpIBinder`, so we
549 // reuse the interface for the binder tests
550 let binder = callback.as_binder();
551 assert_eq!(service.TakesAnIBinder(&binder), Ok(()));
552 assert_eq!(service.TakesANullableIBinder(None), Ok(()));
553 assert_eq!(service.TakesANullableIBinder(Some(&binder)), Ok(()));
554 }
555
556 macro_rules! test_reverse_null_array {
557 ($service:expr, $func:ident, $expect_repeated:expr) => {{
558 let mut repeated = None;
559 let result = $service.$func(None, &mut repeated);
560 assert_eq!(repeated, $expect_repeated);
561 assert_eq!(result, Ok(None));
562 }};
563 }
564
565 macro_rules! test_reverse_nullable_array {
566 ($service:expr, $func:ident, $array:expr) => {{
567 let mut array = $array;
568 // Java needs initial values here (can't resize arrays)
569 let mut repeated = Some(vec![Default::default(); array.len()]);
570 let result = $service.$func(Some(&array[..]), &mut repeated);
571 assert_eq!(repeated.as_ref(), Some(&array));
572 array.reverse();
573 assert_eq!(result, Ok(Some(array)));
574 }};
575 }
576
577 #[test]
test_utf8_string()578 fn test_utf8_string() {
579 let service = get_test_service();
580 let inputs = [
581 "typical string",
582 "",
583 "\0\0",
584 std::str::from_utf8(&[0xF0, 0x90, 0x90, 0xB7, 0xE2, 0x82, 0xAC])
585 .expect("error converting string"),
586 ITestService::STRING_CONSTANT_UTF8,
587 ];
588 for input in &inputs {
589 let result = service.RepeatUtf8CppString(input);
590 assert_eq!(result.as_ref().map(String::as_str), Ok(*input));
591
592 let result = service.RepeatNullableUtf8CppString(Some(input));
593 assert_eq!(result.as_ref().map(Option::as_deref), Ok(Some(*input)));
594 }
595
596 let result = service.RepeatNullableUtf8CppString(None);
597 assert_eq!(result, Ok(None));
598
599 let inputs = vec![
600 Some("typical string".into()),
601 Some(String::new()),
602 None,
603 Some(
604 std::str::from_utf8(&[0xF0, 0x90, 0x90, 0xB7, 0xE2, 0x82, 0xAC])
605 .expect("error converting string")
606 .into(),
607 ),
608 Some(ITestService::STRING_CONSTANT_UTF8.into()),
609 ];
610
611 // Java can't return a null list as a parameter
612 let backend = service.getBackendType().expect("error getting backend type");
613 let null_output = if backend == BackendType::JAVA { Some(vec![]) } else { None };
614 test_reverse_null_array!(service, ReverseUtf8CppStringList, null_output);
615
616 test_reverse_null_array!(service, ReverseNullableUtf8CppString, None);
617
618 test_reverse_nullable_array!(service, ReverseUtf8CppStringList, inputs.clone());
619 test_reverse_nullable_array!(service, ReverseNullableUtf8CppString, inputs);
620 }
621
622 #[allow(clippy::approx_constant)]
623 #[allow(clippy::float_cmp)]
624 #[test]
test_parcelable()625 fn test_parcelable() {
626 let service = get_test_service();
627 let mut parcelable = StructuredParcelable::StructuredParcelable::default();
628
629 const DESIRED_VALUE: i32 = 23;
630 parcelable.f = DESIRED_VALUE;
631
632 assert_eq!(parcelable.stringDefaultsToFoo, "foo");
633 assert_eq!(parcelable.byteDefaultsToFour, 4);
634 assert_eq!(parcelable.intDefaultsToFive, 5);
635 assert_eq!(parcelable.longDefaultsToNegativeSeven, -7);
636 assert!(parcelable.booleanDefaultsToTrue);
637 assert_eq!(parcelable.charDefaultsToC, 'C' as u16);
638 assert_eq!(parcelable.floatDefaultsToPi, 3.14f32);
639 assert_eq!(parcelable.doubleWithDefault, -3.14e17f64);
640 assert!(!parcelable.boolDefault);
641 assert_eq!(parcelable.byteDefault, 0);
642 assert_eq!(parcelable.intDefault, 0);
643 assert_eq!(parcelable.longDefault, 0);
644 assert_eq!(parcelable.floatDefault, 0.0f32);
645 assert_eq!(parcelable.doubleDefault, 0.0f64);
646 assert_eq!(parcelable.arrayDefaultsTo123, &[1, 2, 3]);
647 assert!(parcelable.arrayDefaultsToEmpty.is_empty());
648
649 let result = service.FillOutStructuredParcelable(&mut parcelable);
650 assert_eq!(result, Ok(()));
651
652 assert_eq!(parcelable.shouldContainThreeFs, [DESIRED_VALUE, DESIRED_VALUE, DESIRED_VALUE]);
653 assert_eq!(parcelable.shouldBeJerry, "Jerry");
654 assert_eq!(parcelable.int32_min, i32::MIN);
655 assert_eq!(parcelable.int32_max, i32::MAX);
656 assert_eq!(parcelable.int64_max, i64::MAX);
657 assert_eq!(parcelable.hexInt32_neg_1, -1);
658 for i in parcelable.int8_1 {
659 assert_eq!(i, 1);
660 }
661 for i in parcelable.int32_1 {
662 assert_eq!(i, 1);
663 }
664 for i in parcelable.int64_1 {
665 assert_eq!(i, 1);
666 }
667 assert_eq!(parcelable.hexInt32_pos_1, 1);
668 assert_eq!(parcelable.hexInt64_pos_1, 1);
669 assert_eq!(parcelable.const_exprs_1.0, 1);
670 assert_eq!(parcelable.const_exprs_2.0, 1);
671 assert_eq!(parcelable.const_exprs_3.0, 1);
672 assert_eq!(parcelable.const_exprs_4.0, 1);
673 assert_eq!(parcelable.const_exprs_5.0, 1);
674 assert_eq!(parcelable.const_exprs_6.0, 1);
675 assert_eq!(parcelable.const_exprs_7.0, 1);
676 assert_eq!(parcelable.const_exprs_8.0, 1);
677 assert_eq!(parcelable.const_exprs_9.0, 1);
678 assert_eq!(parcelable.const_exprs_10.0, 1);
679 assert_eq!(parcelable.addString1, "hello world!");
680 assert_eq!(parcelable.addString2, "The quick brown fox jumps over the lazy dog.");
681
682 assert_eq!(
683 parcelable.shouldSetBit0AndBit2,
684 StructuredParcelable::BIT0 | StructuredParcelable::BIT2
685 );
686
687 assert_eq!(parcelable.u, Some(Union::Union::Ns(vec![1, 2, 3])));
688 assert_eq!(parcelable.shouldBeConstS1, Some(Union::Union::S(Union::S1.to_string())))
689 }
690
691 #[test]
test_repeat_extendable_parcelable()692 fn test_repeat_extendable_parcelable() {
693 let service = get_test_service();
694
695 let ext = Arc::new(MyExt { a: 42, b: "EXT".into() });
696 let mut ep = ExtendableParcelable { a: 1, b: "a".into(), c: 42, ..Default::default() };
697 ep.ext.set_parcelable(Arc::clone(&ext)).expect("error setting parcelable");
698
699 let mut ep2 = ExtendableParcelable::default();
700 let result = service.RepeatExtendableParcelable(&ep, &mut ep2);
701 assert_eq!(result, Ok(()));
702 assert_eq!(ep2.a, ep.a);
703 assert_eq!(ep2.b, ep.b);
704
705 let ret_ext = ep2.ext.get_parcelable::<MyExt>().expect("error getting parcelable");
706 assert!(ret_ext.is_some());
707
708 let ret_ext = ret_ext.unwrap();
709 assert_eq!(ret_ext.a, ext.a);
710 assert_eq!(ret_ext.b, ext.b);
711 }
712
713 macro_rules! test_parcelable_holder_stability {
714 ($test:ident, $holder:path, $parcelable:path) => {
715 #[test]
716 fn $test() {
717 let mut holder = <$holder>::default();
718 let parcelable = Arc::new(<$parcelable>::default());
719 let result = holder.ext.set_parcelable(Arc::clone(&parcelable));
720 assert_eq!(result, Ok(()));
721
722 let parcelable2 = holder.ext.get_parcelable::<$parcelable>().unwrap().unwrap();
723 assert!(Arc::ptr_eq(&parcelable, &parcelable2));
724 }
725 };
726 }
727
728 test_parcelable_holder_stability! {
729 test_vintf_parcelable_holder_can_contain_vintf_parcelable,
730 VintfExtendableParcelable,
731 VintfParcelable
732 }
733 test_parcelable_holder_stability! {
734 test_stable_parcelable_holder_can_contain_vintf_parcelable,
735 NonVintfExtendableParcelable,
736 VintfParcelable
737 }
738 test_parcelable_holder_stability! {
739 test_stable_parcelable_holder_can_contain_non_vintf_parcelable,
740 NonVintfExtendableParcelable,
741 NonVintfParcelable
742 }
743 test_parcelable_holder_stability! {
744 test_stable_parcelable_holder_can_contain_unstable_parcelable,
745 NonVintfExtendableParcelable,
746 UnstableParcelable
747 }
748 test_parcelable_holder_stability! {
749 test_unstable_parcelable_holder_can_contain_vintf_parcelable,
750 UnstableExtendableParcelable,
751 VintfParcelable
752 }
753 test_parcelable_holder_stability! {
754 test_unstable_parcelable_holder_can_contain_non_vintf_parcelable,
755 UnstableExtendableParcelable,
756 NonVintfParcelable
757 }
758 test_parcelable_holder_stability! {
759 test_unstable_parcelable_holder_can_contain_unstable_parcelable,
760 UnstableExtendableParcelable,
761 UnstableParcelable
762 }
763
764 #[test]
test_vintf_parcelable_holder_cannot_contain_not_vintf_parcelable()765 fn test_vintf_parcelable_holder_cannot_contain_not_vintf_parcelable() {
766 let mut holder = VintfExtendableParcelable::default();
767 let parcelable = Arc::new(NonVintfParcelable::default());
768 let result = holder.ext.set_parcelable(Arc::clone(&parcelable));
769 assert_eq!(result, Err(binder::StatusCode::BAD_VALUE));
770
771 let parcelable2 = holder.ext.get_parcelable::<NonVintfParcelable>();
772 assert!(parcelable2.unwrap().is_none());
773 }
774
775 #[test]
test_vintf_parcelable_holder_cannot_contain_unstable_parcelable()776 fn test_vintf_parcelable_holder_cannot_contain_unstable_parcelable() {
777 let mut holder = VintfExtendableParcelable::default();
778 let parcelable = Arc::new(UnstableParcelable::default());
779 let result = holder.ext.set_parcelable(Arc::clone(&parcelable));
780 assert_eq!(result, Err(binder::StatusCode::BAD_VALUE));
781
782 let parcelable2 = holder.ext.get_parcelable::<UnstableParcelable>();
783 assert!(parcelable2.unwrap().is_none());
784 }
785
786 #[test]
test_read_write_extension()787 fn test_read_write_extension() {
788 let ext = Arc::new(MyExt { a: 42, b: "EXT".into() });
789 let ext2 = Arc::new(MyExt2 { a: 42, b: MyExt { a: 24, b: "INEXT".into() }, c: "EXT2".into() });
790
791 let mut ep = ExtendableParcelable { a: 1, b: "a".into(), c: 42, ..Default::default() };
792
793 ep.ext.set_parcelable(Arc::clone(&ext)).unwrap();
794 ep.ext2.set_parcelable(Arc::clone(&ext2)).unwrap();
795
796 let ext_like = ep.ext.get_parcelable::<MyExtLike>();
797 assert_eq!(ext_like.unwrap_err(), binder::StatusCode::BAD_VALUE);
798
799 let actual_ext = ep.ext.get_parcelable::<MyExt>();
800 assert!(actual_ext.unwrap().is_some());
801 let actual_ext2 = ep.ext2.get_parcelable::<MyExt2>();
802 assert!(actual_ext2.unwrap().is_some());
803
804 check_extension_content(&ep, &ext, &ext2);
805
806 let mut parcel = Parcel::new();
807 ep.write_to_parcel(&mut parcel.borrowed()).unwrap();
808
809 // SAFETY: 0 is less than the current size of the parcel data buffer, because the parcel is not
810 // empty.
811 unsafe {
812 parcel.set_data_position(0).unwrap();
813 }
814 let mut ep1 = ExtendableParcelable::default();
815 ep1.read_from_parcel(parcel.borrowed_ref()).unwrap();
816
817 // SAFETY: 0 is less than the current size of the parcel data buffer, because the parcel is not
818 // empty.
819 unsafe {
820 parcel.set_data_position(0).unwrap();
821 }
822 ep1.write_to_parcel(&mut parcel.borrowed()).unwrap();
823
824 // SAFETY: 0 is less than the current size of the parcel data buffer, because the parcel is not
825 // empty.
826 unsafe {
827 parcel.set_data_position(0).unwrap();
828 }
829 let mut ep2 = ExtendableParcelable::default();
830 ep2.read_from_parcel(parcel.borrowed_ref()).unwrap();
831
832 let ext_like = ep2.ext.get_parcelable::<MyExtLike>();
833 assert!(ext_like.unwrap().is_none());
834
835 let actual_ext = ep2.ext.get_parcelable::<MyExt>();
836 assert!(actual_ext.unwrap().is_some());
837
838 let new_ext2 =
839 Arc::new(MyExt2 { a: 79, b: MyExt { a: 42, b: "INNEWEXT".into() }, c: "NEWEXT2".into() });
840 ep2.ext2.set_parcelable(Arc::clone(&new_ext2)).unwrap();
841
842 check_extension_content(&ep1, &ext, &ext2);
843 check_extension_content(&ep2, &ext, &new_ext2);
844 }
845
check_extension_content(ep: &ExtendableParcelable, ext: &MyExt, ext2: &MyExt2)846 fn check_extension_content(ep: &ExtendableParcelable, ext: &MyExt, ext2: &MyExt2) {
847 assert_eq!(ep.a, 1);
848 assert_eq!(ep.b, "a");
849 assert_eq!(ep.c, 42);
850
851 let actual_ext = ep.ext.get_parcelable::<MyExt>().unwrap().unwrap();
852 assert_eq!(ext.a, actual_ext.a);
853 assert_eq!(ext.b, actual_ext.b);
854
855 let actual_ext2 = ep.ext2.get_parcelable::<MyExt2>().unwrap().unwrap();
856 assert_eq!(ext2.a, actual_ext2.a);
857 assert_eq!(ext2.b.a, actual_ext2.b.a);
858 assert_eq!(ext2.b.b, actual_ext2.b.b);
859 assert_eq!(ext2.c, actual_ext2.c);
860 }
861
862 #[test]
test_reverse_recursive_list()863 fn test_reverse_recursive_list() {
864 let service = get_test_service();
865
866 let mut head = None;
867 for n in 0..10 {
868 let node = RecursiveList { value: n, next: head };
869 head = Some(Box::new(node));
870 }
871 // head = [9, 8, .., 0]
872 let result = service.ReverseList(head.as_ref().unwrap());
873 assert!(result.is_ok());
874
875 // reversed should be [0, 1, ... 9]
876 let mut reversed: Option<&RecursiveList> = result.as_ref().ok();
877 for n in 0..10 {
878 assert_eq!(reversed.map(|inner| inner.value), Some(n));
879 reversed = reversed.unwrap().next.as_ref().map(|n| n.as_ref());
880 }
881 assert!(reversed.is_none())
882 }
883
884 #[test]
test_get_union_tags()885 fn test_get_union_tags() {
886 let service = get_test_service();
887 let result = service.GetUnionTags(&[]);
888 assert_eq!(result, Ok(vec![]));
889 let result = service.GetUnionTags(&[Union::Union::N(0), Union::Union::Ns(vec![])]);
890 assert_eq!(result, Ok(vec![Union::Tag::Tag::n, Union::Tag::Tag::ns]));
891 }
892
893 #[test]
test_unions()894 fn test_unions() {
895 assert_eq!(Union::Union::default(), Union::Union::Ns(vec![]));
896 assert_eq!(EnumUnion::default(), EnumUnion::IntEnum(IntEnum::FOO));
897 }
898
899 const EXPECTED_ARG_VALUE: i32 = 100;
900 const EXPECTED_RETURN_VALUE: i32 = 200;
901
902 struct TestDefaultImpl;
903
904 impl binder::Interface for TestDefaultImpl {}
905
906 impl ITestServiceDefault for TestDefaultImpl {
UnimplementedMethod(&self, arg: i32) -> binder::Result<i32>907 fn UnimplementedMethod(&self, arg: i32) -> binder::Result<i32> {
908 assert_eq!(arg, EXPECTED_ARG_VALUE);
909 Ok(EXPECTED_RETURN_VALUE)
910 }
911 }
912
913 #[test]
test_default_impl()914 fn test_default_impl() {
915 let service = get_test_service();
916 let di: ITestServiceDefaultRef = Some(Arc::new(TestDefaultImpl));
917 <BpTestService as ITestService::ITestService>::setDefaultImpl(di);
918
919 let result = service.UnimplementedMethod(EXPECTED_ARG_VALUE);
920 assert_eq!(result, Ok(EXPECTED_RETURN_VALUE));
921 }
922
923 #[test]
test_versioned_interface_version()924 fn test_versioned_interface_version() {
925 let service: binder::Strong<dyn IFooInterface::IFooInterface> = binder::wait_for_interface(
926 <BpFooInterface as IFooInterface::IFooInterface>::get_descriptor(),
927 )
928 .expect("did not get binder service");
929
930 let version = service.getInterfaceVersion();
931 assert_eq!(version, Ok(1));
932 }
933
934 #[test]
test_versioned_interface_hash()935 fn test_versioned_interface_hash() {
936 let service: binder::Strong<dyn IFooInterface::IFooInterface> = binder::wait_for_interface(
937 <BpFooInterface as IFooInterface::IFooInterface>::get_descriptor(),
938 )
939 .expect("did not get binder service");
940
941 let hash = service.getInterfaceHash();
942 assert_eq!(hash.as_ref().map(String::as_str), Ok("9e7be1859820c59d9d55dd133e71a3687b5d2e5b"));
943 }
944
945 #[test]
test_versioned_known_union_field_is_ok()946 fn test_versioned_known_union_field_is_ok() {
947 let service: binder::Strong<dyn IFooInterface::IFooInterface> = binder::wait_for_interface(
948 <BpFooInterface as IFooInterface::IFooInterface>::get_descriptor(),
949 )
950 .expect("did not get binder service");
951
952 assert_eq!(service.acceptUnionAndReturnString(&BazUnion::IntNum(42)), Ok(String::from("42")));
953 }
954
955 #[test]
test_versioned_unknown_union_field_triggers_error()956 fn test_versioned_unknown_union_field_triggers_error() {
957 let service: binder::Strong<dyn IFooInterface::IFooInterface> = binder::wait_for_interface(
958 <BpFooInterface as IFooInterface::IFooInterface>::get_descriptor(),
959 )
960 .expect("did not get binder service");
961
962 let ret = service.acceptUnionAndReturnString(&BazUnion::LongNum(42));
963 assert!(ret.is_err());
964
965 let main_service = get_test_service();
966 let backend = main_service.getBackendType().expect("error getting backend type");
967
968 // b/173458620 - for investigation of fixing difference
969 if backend == BackendType::JAVA {
970 assert_eq!(ret.unwrap_err().exception_code(), binder::ExceptionCode::ILLEGAL_ARGUMENT);
971 } else {
972 assert_eq!(ret.unwrap_err().transaction_error(), binder::StatusCode::BAD_VALUE);
973 }
974 }
975
976 #[test]
test_array_of_parcelable_with_new_field()977 fn test_array_of_parcelable_with_new_field() {
978 let service: binder::Strong<dyn IFooInterface::IFooInterface> = binder::wait_for_interface(
979 <BpFooInterface as IFooInterface::IFooInterface>::get_descriptor(),
980 )
981 .expect("did not get binder service");
982
983 let foos = [Default::default(), Default::default(), Default::default()];
984 let ret = service.returnsLengthOfFooArray(&foos);
985 assert_eq!(ret, Ok(foos.len() as i32));
986 }
987
988 #[test]
test_read_data_correctly_after_parcelable_with_new_field()989 fn test_read_data_correctly_after_parcelable_with_new_field() {
990 let service: binder::Strong<dyn IFooInterface::IFooInterface> = binder::wait_for_interface(
991 <BpFooInterface as IFooInterface::IFooInterface>::get_descriptor(),
992 )
993 .expect("did not get binder service");
994
995 let in_foo = Default::default();
996 let mut inout_foo = Foo { intDefault42: 0 };
997 let mut out_foo = Foo { intDefault42: 0 };
998 let ret = service.ignoreParcelablesAndRepeatInt(&in_foo, &mut inout_foo, &mut out_foo, 43);
999 assert_eq!(ret, Ok(43));
1000 assert_eq!(inout_foo.intDefault42, 0);
1001 assert_eq!(out_foo.intDefault42, 0);
1002 }
1003
1004 #[test]
test_calling_v2_api_triggers_error()1005 fn test_calling_v2_api_triggers_error() {
1006 let service: binder::Strong<dyn IFooInterface::IFooInterface> = binder::wait_for_interface(
1007 <BpFooInterface as IFooInterface::IFooInterface>::get_descriptor(),
1008 )
1009 .expect("did not get binder service");
1010
1011 let ret = service.newApi();
1012
1013 assert_eq!(ret.unwrap_err().transaction_error(), binder::StatusCode::UNKNOWN_TRANSACTION);
1014 }
1015
test_renamed_interface<F>(f: F) where F: FnOnce(binder::Strong<dyn IOldName::IOldName>, binder::Strong<dyn INewName::INewName>),1016 fn test_renamed_interface<F>(f: F)
1017 where
1018 F: FnOnce(binder::Strong<dyn IOldName::IOldName>, binder::Strong<dyn INewName::INewName>),
1019 {
1020 let service = get_test_service();
1021 let old_name = service.GetOldNameInterface();
1022 assert!(old_name.is_ok());
1023
1024 let new_name = service.GetNewNameInterface();
1025 assert!(new_name.is_ok());
1026
1027 f(old_name.unwrap(), new_name.unwrap());
1028 }
1029
1030 #[test]
test_renamed_interface_old_as_old()1031 fn test_renamed_interface_old_as_old() {
1032 test_renamed_interface(|old_name, _| {
1033 assert_eq!(
1034 <BpOldName as IOldName::IOldName>::get_descriptor(),
1035 "android.aidl.tests.IOldName"
1036 );
1037
1038 let real_name = old_name.RealName();
1039 assert_eq!(real_name.as_ref().map(String::as_str), Ok("OldName"));
1040 });
1041 }
1042
1043 #[test]
test_renamed_interface_new_as_new()1044 fn test_renamed_interface_new_as_new() {
1045 test_renamed_interface(|_, new_name| {
1046 assert_eq!(
1047 <BpNewName as INewName::INewName>::get_descriptor(),
1048 "android.aidl.tests.IOldName"
1049 );
1050
1051 let real_name = new_name.RealName();
1052 assert_eq!(real_name.as_ref().map(String::as_str), Ok("NewName"));
1053 });
1054 }
1055
1056 #[test]
test_renamed_interface_old_as_new()1057 fn test_renamed_interface_old_as_new() {
1058 test_renamed_interface(|old_name, _| {
1059 let new_name = old_name.as_binder().into_interface::<dyn INewName::INewName>();
1060 assert!(new_name.is_ok());
1061
1062 let real_name = new_name.unwrap().RealName();
1063 assert_eq!(real_name.as_ref().map(String::as_str), Ok("OldName"));
1064 });
1065 }
1066
1067 #[test]
test_renamed_interface_new_as_old()1068 fn test_renamed_interface_new_as_old() {
1069 test_renamed_interface(|_, new_name| {
1070 let old_name = new_name.as_binder().into_interface::<dyn IOldName::IOldName>();
1071 assert!(old_name.is_ok());
1072
1073 let real_name = old_name.unwrap().RealName();
1074 assert_eq!(real_name.as_ref().map(String::as_str), Ok("NewName"));
1075 });
1076 }
1077
1078 #[derive(Debug, Default)]
1079 struct Callback {
1080 received: Arc<Mutex<Option<ParcelableWithNested::Status::Status>>>,
1081 }
1082
1083 impl Interface for Callback {}
1084
1085 impl INestedService::ICallback::ICallback for Callback {
done(&self, st: ParcelableWithNested::Status::Status) -> binder::Result<()>1086 fn done(&self, st: ParcelableWithNested::Status::Status) -> binder::Result<()> {
1087 *self.received.lock().unwrap() = Some(st);
1088 Ok(())
1089 }
1090 }
1091
1092 #[test]
test_nested_type()1093 fn test_nested_type() {
1094 let service: binder::Strong<dyn INestedService::INestedService> = binder::wait_for_interface(
1095 <INestedService::BpNestedService as INestedService::INestedService>::get_descriptor(),
1096 )
1097 .expect("did not get binder service");
1098
1099 let p = ParcelableWithNested::ParcelableWithNested {
1100 status: ParcelableWithNested::Status::Status::OK,
1101 };
1102 // OK -> NOT_OK
1103 let ret = service.flipStatus(&p);
1104 assert_eq!(
1105 ret,
1106 Ok(INestedService::Result::Result { status: ParcelableWithNested::Status::Status::NOT_OK })
1107 );
1108 let received = Arc::new(Mutex::new(None));
1109 // NOT_OK -> OK with nested callback interface
1110 let cb = INestedService::ICallback::BnCallback::new_binder(
1111 Callback { received: Arc::clone(&received) },
1112 BinderFeatures::default(),
1113 );
1114 let ret = service.flipStatusWithCallback(ParcelableWithNested::Status::Status::NOT_OK, &cb);
1115 assert_eq!(ret, Ok(()));
1116 let received = received.lock().unwrap();
1117 assert_eq!(*received, Some(ParcelableWithNested::Status::Status::OK))
1118 }
1119
1120 #[test]
test_nonnull_binder()1121 fn test_nonnull_binder() {
1122 let service = get_test_service();
1123 let result = service.TakesAnIBinder(&service.as_binder());
1124 assert!(result.is_ok());
1125 }
1126
1127 #[test]
test_binder_list_without_null()1128 fn test_binder_list_without_null() {
1129 let service = get_test_service();
1130 let result = service.TakesAnIBinderList(&[service.as_binder()]);
1131 assert!(result.is_ok());
1132 }
1133
1134 #[test]
test_null_binder_to_annotated_method()1135 fn test_null_binder_to_annotated_method() {
1136 let service = get_test_service();
1137 let result = service.TakesANullableIBinder(None);
1138 assert!(result.is_ok());
1139 }
1140
1141 #[test]
test_binder_list_with_null_to_annotated_method()1142 fn test_binder_list_with_null_to_annotated_method() {
1143 let service = get_test_service();
1144 let result = service.TakesANullableIBinderList(Some(&[Some(service.as_binder()), None]));
1145 assert!(result.is_ok());
1146 }
1147
1148 #[test]
test_binder_array()1149 fn test_binder_array() {
1150 let service = get_test_service();
1151 let callback = service
1152 .GetCallback(false)
1153 .expect("error calling GetCallback")
1154 .expect("expected Some from GetCallback");
1155
1156 let mut array = vec![service.as_binder(), callback.as_binder()];
1157
1158 // Java needs initial values here (can't resize arrays)
1159 let mut repeated = vec![Default::default(); array.len()];
1160
1161 let result = service.ReverseIBinderArray(&array, &mut repeated);
1162 assert_eq!(repeated.into_iter().collect::<Option<Vec<_>>>().as_ref(), Some(&array));
1163 array.reverse();
1164 assert_eq!(result, Ok(array));
1165 }
1166
1167 #[test]
test_nullable_binder_array()1168 fn test_nullable_binder_array() {
1169 let service = get_test_service();
1170 let mut array = vec![Some(service.as_binder()), None];
1171
1172 // Java needs initial values here (can't resize arrays)
1173 let mut repeated = Some(vec![Default::default(); array.len()]);
1174
1175 let result = service.ReverseNullableIBinderArray(Some(&array[..]), &mut repeated);
1176 assert_eq!(repeated.as_ref(), Some(&array));
1177 array.reverse();
1178 assert_eq!(result, Ok(Some(array)));
1179 }
1180
1181 #[test]
test_read_write_fixed_size_array()1182 fn test_read_write_fixed_size_array() {
1183 let mut parcel = Parcel::new();
1184 let mut p: FixedSizeArrayExample = Default::default();
1185 p.byteMatrix[0][0] = 0;
1186 p.byteMatrix[0][1] = 1;
1187 p.byteMatrix[1][0] = 2;
1188 p.byteMatrix[1][1] = 3;
1189
1190 p.floatMatrix[0][0] = 0.0;
1191 p.floatMatrix[0][1] = 1.0;
1192 p.floatMatrix[1][0] = 2.0;
1193 p.floatMatrix[1][1] = 3.0;
1194
1195 p.boolNullableArray = Some([true, false]);
1196 p.byteNullableArray = Some([42, 0]);
1197 p.stringNullableArray = Some([Some("hello".into()), Some("world".into())]);
1198
1199 p.boolNullableMatrix = Some([[true, false], Default::default()]);
1200 p.byteNullableMatrix = Some([[42, 0], Default::default()]);
1201 p.stringNullableMatrix =
1202 Some([[Some("hello".into()), Some("world".into())], Default::default()]);
1203
1204 assert_eq!(parcel.write(&p), Ok(()));
1205 // SAFETY: 0 is less than the current size of the parcel data buffer, because the parcel is not
1206 // empty.
1207 unsafe {
1208 parcel.set_data_position(0).unwrap();
1209 }
1210 assert_eq!(p, parcel.read::<FixedSizeArrayExample>().unwrap());
1211 }
1212
1213 #[test]
test_fixed_size_array_uses_array_optimization()1214 fn test_fixed_size_array_uses_array_optimization() {
1215 let mut parcel = Parcel::new();
1216 let byte_array = [[1u8, 2u8, 3u8], [4u8, 5u8, 6u8]];
1217 assert_eq!(parcel.write(&byte_array), Ok(()));
1218 // SAFETY: 0 is less than the current size of the parcel data buffer, because the parcel is not
1219 // empty.
1220 unsafe {
1221 parcel.set_data_position(0).unwrap();
1222 }
1223 assert_eq!(parcel.read::<i32>(), Ok(2i32));
1224 assert_eq!(parcel.read::<Vec<u8>>(), Ok(vec![1u8, 2u8, 3u8]));
1225 assert_eq!(parcel.read::<Vec<u8>>(), Ok(vec![4u8, 5u8, 6u8]));
1226 }
1227
1228 macro_rules! test_repeat_fixed_size_array {
1229 ($service:ident, $func:ident, $value:expr) => {
1230 let array = $value;
1231 let mut repeated = Default::default();
1232 let result = $service.$func(&array, &mut repeated).unwrap();
1233 assert_eq!(repeated, array);
1234 assert_eq!(result, array);
1235 };
1236 }
1237
1238 macro_rules! test_repeat_fixed_size_array_1d_binder {
1239 ($service:ident, $func:ident, $value:expr) => {
1240 let array = $value;
1241 let mut repeated = Default::default();
1242 let result = $service.$func(&array, &mut repeated).unwrap();
1243 assert_eq!(result, array.clone());
1244 assert_eq!(repeated, array.map(Some));
1245 };
1246 }
1247
1248 macro_rules! test_repeat_fixed_size_array_2d_binder {
1249 ($service:ident, $func:ident, $value:expr) => {
1250 let array = $value;
1251 let mut repeated = Default::default();
1252 let result = $service.$func(&array, &mut repeated).unwrap();
1253 assert_eq!(result, array.clone());
1254 assert_eq!(repeated, array.map(|row| row.map(Some)));
1255 };
1256 }
1257
1258 #[test]
test_fixed_size_array_over_binder()1259 fn test_fixed_size_array_over_binder() {
1260 let test_service = get_test_service();
1261 let service: binder::Strong<dyn IRepeatFixedSizeArray> = binder::wait_for_interface(
1262 <BpRepeatFixedSizeArray as IRepeatFixedSizeArray>::get_descriptor(),
1263 )
1264 .expect("did not get binder service");
1265
1266 test_repeat_fixed_size_array!(service, RepeatBytes, [1u8, 2u8, 3u8]);
1267 test_repeat_fixed_size_array!(service, RepeatInts, [1i32, 2i32, 3i32]);
1268
1269 let binder1 = test_service.as_binder();
1270 let binder2 = test_service
1271 .GetCallback(false)
1272 .expect("error calling GetCallback")
1273 .expect("expected Some from GetCallback")
1274 .as_binder();
1275 let binder3 = service.as_binder();
1276 test_repeat_fixed_size_array_1d_binder!(
1277 service,
1278 RepeatBinders,
1279 [binder1.clone(), binder2.clone(), binder3.clone()]
1280 );
1281
1282 let p1 = IntParcelable { value: 1 };
1283 let p2 = IntParcelable { value: 2 };
1284 let p3 = IntParcelable { value: 3 };
1285 test_repeat_fixed_size_array!(service, RepeatParcelables, [p1, p2, p3]);
1286
1287 test_repeat_fixed_size_array!(service, Repeat2dBytes, [[1u8, 2u8, 3u8], [1u8, 2u8, 3u8]]);
1288 test_repeat_fixed_size_array!(service, Repeat2dInts, [[1i32, 2i32, 3i32], [1i32, 2i32, 3i32]]);
1289
1290 test_repeat_fixed_size_array_2d_binder!(
1291 service,
1292 Repeat2dBinders,
1293 [[binder1.clone(), binder2.clone(), binder3.clone()], [binder1, binder2, binder3]]
1294 );
1295
1296 test_repeat_fixed_size_array!(service, Repeat2dParcelables, [[p1, p2, p3], [p1, p2, p3]]);
1297 }
1298
1299 #[test]
test_ping()1300 fn test_ping() {
1301 let test_service = get_test_service();
1302 assert_eq!(test_service.as_binder().ping_binder(), Ok(()));
1303 }
1304
1305 #[test]
test_trunk_stable_parcelable()1306 fn test_trunk_stable_parcelable() {
1307 let service = get_test_trunk_stable_service();
1308 let res = service.getInterfaceVersion();
1309 assert!(res.is_ok());
1310 let version = res.unwrap();
1311
1312 let p1 = MyParcelable { a: 12, b: 13, c: 14 };
1313 let result = service.repeatParcelable(&p1);
1314 assert!(result.is_ok());
1315 let p2 = result.unwrap();
1316 assert_eq!(p1.a, p2.a);
1317 assert_eq!(p1.b, p2.b);
1318 if version == 1 {
1319 assert_ne!(p1.c, p2.c);
1320 assert_eq!(0, p2.c);
1321 } else {
1322 assert_eq!(p1.c, p2.c);
1323 }
1324 }
1325
1326 #[test]
test_trunk_stable_enum()1327 fn test_trunk_stable_enum() {
1328 let service = get_test_trunk_stable_service();
1329 let e1 = MyEnum::THREE;
1330 let result = service.repeatEnum(e1);
1331 assert!(result.is_ok());
1332 assert_eq!(result.unwrap(), e1);
1333 }
1334 #[test]
test_trunk_stable_union()1335 fn test_trunk_stable_union() {
1336 let service = get_test_trunk_stable_service();
1337 let res = service.getInterfaceVersion();
1338 assert!(res.is_ok());
1339 let version = res.unwrap();
1340
1341 let u1 = MyUnion::C(14);
1342 let result = service.repeatUnion(&u1);
1343 if version == 1 {
1344 assert!(result.is_err());
1345 } else {
1346 assert!(result.is_ok());
1347 }
1348 }
1349 #[test]
test_trunk_stable_unimplemented()1350 fn test_trunk_stable_unimplemented() {
1351 let service = get_test_trunk_stable_service();
1352 let res = service.getInterfaceVersion();
1353 assert!(res.is_ok());
1354 let version = res.unwrap();
1355
1356 let p1 = MyOtherParcelable { a: 12, b: 13 };
1357 let result = service.repeatOtherParcelable(&p1);
1358 if version == 1 {
1359 assert!(result.is_err());
1360 } else {
1361 assert!(result.is_ok());
1362 }
1363 }
1364
1365 #[test]
test_trunk_stable_hash()1366 fn test_trunk_stable_hash() {
1367 let service = get_test_trunk_stable_service();
1368 let res = service.getInterfaceVersion();
1369 assert!(res.is_ok());
1370 let version = res.unwrap();
1371
1372 let hash = service.getInterfaceHash();
1373 if version == 1 {
1374 assert_eq!(
1375 hash.as_ref().map(String::as_str),
1376 Ok("88311b9118fb6fe9eff4a2ca19121de0587f6d5f")
1377 );
1378 // Check local values of version and hash
1379 assert_eq!(
1380 android_aidl_test_trunk::aidl::android::aidl::test::trunk::ITrunkStableTest::VERSION,
1381 1
1382 );
1383 assert_eq!(
1384 android_aidl_test_trunk::aidl::android::aidl::test::trunk::ITrunkStableTest::HASH,
1385 "88311b9118fb6fe9eff4a2ca19121de0587f6d5f"
1386 );
1387 } else {
1388 assert_eq!(hash.as_ref().map(String::as_str), Ok("notfrozen"));
1389 // Check local values of version and hash
1390 assert_eq!(
1391 android_aidl_test_trunk::aidl::android::aidl::test::trunk::ITrunkStableTest::VERSION,
1392 2
1393 );
1394 assert_eq!(
1395 android_aidl_test_trunk::aidl::android::aidl::test::trunk::ITrunkStableTest::HASH,
1396 "notfrozen"
1397 );
1398 }
1399 }
1400
1401 #[derive(Debug, Default)]
1402 pub struct MyCallback {
1403 pub repeat_parcelable_called: Arc<Mutex<bool>>,
1404 pub repeat_enum_called: Arc<Mutex<bool>>,
1405 pub repeat_union_called: Arc<Mutex<bool>>,
1406 pub repeat_other_parcelable_called: Arc<Mutex<bool>>,
1407 }
1408
1409 impl Interface for MyCallback {}
1410
1411 impl IMyCallback::IMyCallback for MyCallback {
repeatParcelable(&self, in_parcel: &MyParcelable) -> binder::Result<MyParcelable>1412 fn repeatParcelable(&self, in_parcel: &MyParcelable) -> binder::Result<MyParcelable> {
1413 *self.repeat_parcelable_called.lock().unwrap() = true;
1414 let tmp: MyParcelable = MyParcelable { a: in_parcel.a, b: in_parcel.b, c: in_parcel.c };
1415 Ok(tmp)
1416 }
repeatEnum(&self, in_enum: MyEnum) -> binder::Result<MyEnum>1417 fn repeatEnum(&self, in_enum: MyEnum) -> binder::Result<MyEnum> {
1418 *self.repeat_enum_called.lock().unwrap() = true;
1419 Ok(in_enum)
1420 }
repeatUnion(&self, in_union: &MyUnion) -> binder::Result<MyUnion>1421 fn repeatUnion(&self, in_union: &MyUnion) -> binder::Result<MyUnion> {
1422 *self.repeat_union_called.lock().unwrap() = true;
1423 match in_union {
1424 MyUnion::A(n) => Ok(MyUnion::A(*n)),
1425 MyUnion::B(n) => Ok(MyUnion::B(*n)),
1426 MyUnion::C(n) => Ok(MyUnion::C(*n)),
1427 }
1428 }
repeatOtherParcelable( &self, in_parcel: &MyOtherParcelable, ) -> binder::Result<MyOtherParcelable>1429 fn repeatOtherParcelable(
1430 &self,
1431 in_parcel: &MyOtherParcelable,
1432 ) -> binder::Result<MyOtherParcelable> {
1433 *self.repeat_other_parcelable_called.lock().unwrap() = true;
1434 let tmp: MyOtherParcelable = MyOtherParcelable { a: in_parcel.a, b: in_parcel.b };
1435 Ok(tmp)
1436 }
1437 }
1438
1439 #[test]
test_trunk_stable_callback()1440 fn test_trunk_stable_callback() {
1441 let service = get_test_trunk_stable_service();
1442 let res = service.getInterfaceVersion();
1443 assert!(res.is_ok());
1444 let version = res.unwrap();
1445
1446 let repeat_parcelable_called = Arc::new(Mutex::new(false));
1447 let repeat_enum_called = Arc::new(Mutex::new(false));
1448 let repeat_union_called = Arc::new(Mutex::new(false));
1449 let repeat_other_parcelable_called = Arc::new(Mutex::new(false));
1450 let cb = IMyCallback::BnMyCallback::new_binder(
1451 MyCallback {
1452 repeat_parcelable_called: Arc::clone(&repeat_parcelable_called),
1453 repeat_enum_called: Arc::clone(&repeat_enum_called),
1454 repeat_union_called: Arc::clone(&repeat_union_called),
1455 repeat_other_parcelable_called: Arc::clone(&repeat_other_parcelable_called),
1456 },
1457 BinderFeatures::default(),
1458 );
1459 let result = service.callMyCallback(&cb);
1460 assert!(result.is_ok());
1461 assert!(*repeat_parcelable_called.lock().unwrap());
1462 assert!(*repeat_enum_called.lock().unwrap());
1463 assert!(*repeat_union_called.lock().unwrap());
1464 if version == 1 {
1465 assert!(!*repeat_other_parcelable_called.lock().unwrap());
1466 } else {
1467 assert!(*repeat_other_parcelable_called.lock().unwrap());
1468 }
1469 }
1470