• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #define LOG_TAG "Cts-NdkBinderTest"
17 
18 #include <aidl/test_package/BnEmpty.h>
19 #include <aidl/test_package/BnSkippedIds.h>
20 #include <aidl/test_package/BpCompatTest.h>
21 #include <aidl/test_package/BpTest.h>
22 #include <aidl/test_package/ByteEnum.h>
23 #include <aidl/test_package/ExtendableParcelable.h>
24 #include <aidl/test_package/FixedSize.h>
25 #include <aidl/test_package/FixedSizeUnion.h>
26 #include <aidl/test_package/Foo.h>
27 #include <aidl/test_package/IntEnum.h>
28 #include <aidl/test_package/LongEnum.h>
29 #include <aidl/test_package/RegularPolygon.h>
30 #include <android/binder_ibinder_jni.h>
31 #include <android/log.h>
32 #include <android/persistable_bundle_aidl.h>
33 #include <gtest/gtest.h>
34 #include <stdio.h>
35 #include <sys/socket.h>
36 #include <sys/types.h>
37 
38 #include <type_traits>
39 
40 #include "itest_impl.h"
41 #include "utilities.h"
42 
43 using ::aidl::android::os::PersistableBundle;
44 using ::aidl::test_package::Bar;
45 using ::aidl::test_package::BpTest;
46 using ::aidl::test_package::ByteEnum;
47 using ::aidl::test_package::ExtendableParcelable;
48 using ::aidl::test_package::FixedSize;
49 using ::aidl::test_package::FixedSizeUnion;
50 using ::aidl::test_package::Foo;
51 using ::aidl::test_package::GenericBar;
52 using ::aidl::test_package::ICompatTest;
53 using ::aidl::test_package::IntEnum;
54 using ::aidl::test_package::ISkippedIds;
55 using ::aidl::test_package::ITest;
56 using ::aidl::test_package::LongEnum;
57 using ::aidl::test_package::MyExt;
58 using ::aidl::test_package::RegularPolygon;
59 using ::ndk::ScopedAStatus;
60 using ::ndk::ScopedFileDescriptor;
61 using ::ndk::SharedRefBase;
62 using ::ndk::SpAIBinder;
63 
64 // This client is built for 32 and 64-bit targets. The size of FixedSize must remain the same.
65 static_assert(sizeof(FixedSize) == 16);
66 static_assert(offsetof(FixedSize, a) == 0);
67 static_assert(offsetof(FixedSize, b) == 8);
68 
69 static_assert(sizeof(FixedSizeUnion) == 16);  // tag(uint8_t), value(union of {int32_t, long64_t})
70 static_assert(alignof(FixedSizeUnion) == 8);
71 
72 static_assert(FixedSizeUnion::fixed_size::value);
73 
74 class MyEmpty : public ::aidl::test_package::BnEmpty {};
75 class YourEmpty : public ::aidl::test_package::BnEmpty {};
76 
77 // AIDL tests which are independent of the service
78 class NdkBinderTest_AidlLocal : public NdkBinderTest {};
79 
TEST_F(NdkBinderTest_AidlLocal,FromBinder)80 TEST_F(NdkBinderTest_AidlLocal, FromBinder) {
81   std::shared_ptr<MyTest> test = SharedRefBase::make<MyTest>();
82   SpAIBinder binder = test->asBinder();
83   EXPECT_EQ(test, ITest::fromBinder(binder));
84 
85   EXPECT_FALSE(test->isRemote());
86 }
87 
TEST_F(NdkBinderTest_AidlLocal,ConfirmFixedSizeTrue)88 TEST_F(NdkBinderTest_AidlLocal, ConfirmFixedSizeTrue) {
89   bool res = std::is_same<FixedSize::fixed_size, std::true_type>::value;
90   EXPECT_EQ(res, true);
91 }
92 
TEST_F(NdkBinderTest_AidlLocal,ConfirmFixedSizeFalse)93 TEST_F(NdkBinderTest_AidlLocal, ConfirmFixedSizeFalse) {
94   bool res = std::is_same<RegularPolygon::fixed_size, std::true_type>::value;
95   EXPECT_EQ(res, false);
96 }
97 
98 struct Params {
99   std::shared_ptr<ITest> iface;
100   bool shouldBeRemote;
101   bool shouldBeWrapped;
102   std::string expectedName;
103   bool shouldBeOld;
104 };
105 
106 #define iface GetParam().iface
107 #define shouldBeRemote GetParam().shouldBeRemote
108 #define shouldBeWrapped GetParam().shouldBeWrapped
109 
110 // AIDL tests which run on each type of service (local C++, local Java, remote C++, remote Java,
111 // etc..)
112 class NdkBinderTest_Aidl : public NdkBinderTest,
113                            public ::testing::WithParamInterface<Params> {};
114 
TEST_P(NdkBinderTest_Aidl,GotTest)115 TEST_P(NdkBinderTest_Aidl, GotTest) { ASSERT_NE(nullptr, iface); }
116 
TEST_P(NdkBinderTest_Aidl,SanityCheckSource)117 TEST_P(NdkBinderTest_Aidl, SanityCheckSource) {
118   std::string name;
119   ASSERT_OK(iface->GetName(&name));
120   EXPECT_EQ(GetParam().expectedName, name);
121 }
122 
TEST_P(NdkBinderTest_Aidl,Remoteness)123 TEST_P(NdkBinderTest_Aidl, Remoteness) {
124   ASSERT_EQ(shouldBeRemote, iface->isRemote());
125 }
126 
TEST_P(NdkBinderTest_Aidl,UseBinder)127 TEST_P(NdkBinderTest_Aidl, UseBinder) {
128   ASSERT_EQ(STATUS_OK, AIBinder_ping(iface->asBinder().get()));
129 }
130 
TEST_P(NdkBinderTest_Aidl,GetExtension)131 TEST_P(NdkBinderTest_Aidl, GetExtension) {
132   SpAIBinder ext;
133   ASSERT_EQ(STATUS_OK, AIBinder_getExtension(iface->asBinder().get(), ext.getR()));
134 
135   // TODO(b/139325468): add support in Java as well
136   if (GetParam().expectedName == "CPP") {
137     EXPECT_EQ(STATUS_OK, AIBinder_ping(ext.get()));
138   } else {
139     ASSERT_EQ(nullptr, ext.get());
140   }
141 }
142 
ReadFdToString(int fd,std::string * content)143 bool ReadFdToString(int fd, std::string* content) {
144   char buf[64];
145   ssize_t n;
146   while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) {
147     content->append(buf, n);
148   }
149   return (n == 0) ? true : false;
150 }
151 
dumpToString(std::shared_ptr<ITest> itest,std::vector<const char * > args)152 std::string dumpToString(std::shared_ptr<ITest> itest, std::vector<const char*> args) {
153   int fd[2] = {-1, -1};
154   EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fd));
155 
156   EXPECT_OK(itest->dump(fd[0], args.data(), args.size()));
157   close(fd[0]);
158 
159   std::string ret;
160   EXPECT_TRUE(ReadFdToString(fd[1], &ret));
161 
162   close(fd[1]);
163   return ret;
164 }
165 
getCompatTest(std::shared_ptr<ITest> itest)166 auto getCompatTest(std::shared_ptr<ITest> itest) {
167   SpAIBinder binder;
168   itest->getICompatTest(&binder);
169   return ICompatTest::fromBinder(binder);
170 }
171 
TEST_P(NdkBinderTest_Aidl,UseDump)172 TEST_P(NdkBinderTest_Aidl, UseDump) {
173   std::string name;
174   EXPECT_OK(iface->GetName(&name));
175   if (name == "JAVA" && !iface->isRemote()) {
176     // TODO(b/127361166): GTEST_SKIP is considered a failure, would prefer to use that here
177     // TODO(b/127339049): JavaBBinder doesn't implement dump
178     return;
179   }
180 
181   EXPECT_EQ("", dumpToString(iface, {}));
182   EXPECT_EQ("", dumpToString(iface, {"", ""}));
183   EXPECT_EQ("Hello World!", dumpToString(iface, {"Hello ", "World!"}));
184   EXPECT_EQ("ABC", dumpToString(iface, {"A", "B", "C"}));
185 }
186 
TEST_P(NdkBinderTest_Aidl,Trivial)187 TEST_P(NdkBinderTest_Aidl, Trivial) {
188   ASSERT_OK(iface->TestVoidReturn());
189 
190   if (shouldBeWrapped) {
191     ASSERT_OK(iface->TestOneway());
192   } else {
193     ASSERT_EQ(STATUS_UNKNOWN_ERROR, AStatus_getStatus(iface->TestOneway().get()));
194   }
195 }
196 
TEST_P(NdkBinderTest_Aidl,CallingInfo)197 TEST_P(NdkBinderTest_Aidl, CallingInfo) {
198   EXPECT_OK(iface->CacheCallingInfoFromOneway());
199   int32_t res;
200 
201   EXPECT_OK(iface->GiveMeMyCallingPid(&res));
202   EXPECT_EQ(getpid(), res);
203 
204   EXPECT_OK(iface->GiveMeMyCallingUid(&res));
205   EXPECT_EQ(getuid(), res);
206 
207   EXPECT_OK(iface->GiveMeMyCallingPidFromOneway(&res));
208   if (shouldBeRemote) {
209     // PID is hidden from oneway calls
210     EXPECT_EQ(0, res);
211   } else {
212     EXPECT_EQ(getpid(), res);
213   }
214 
215   EXPECT_OK(iface->GiveMeMyCallingUidFromOneway(&res));
216   EXPECT_EQ(getuid(), res);
217 }
218 
TEST_P(NdkBinderTest_Aidl,ConstantsInInterface)219 TEST_P(NdkBinderTest_Aidl, ConstantsInInterface) {
220   ASSERT_EQ(0, ITest::kZero);
221   ASSERT_EQ(1, ITest::kOne);
222   ASSERT_EQ(0xffffffff, ITest::kOnes);
223   ASSERT_EQ(1, ITest::kByteOne);
224   ASSERT_EQ(0xffffffffffffffff, ITest::kLongOnes);
225   ASSERT_EQ(std::string(""), ITest::kEmpty);
226   ASSERT_EQ(std::string("foo"), ITest::kFoo);
227 }
228 
TEST_P(NdkBinderTest_Aidl,ConstantsInParcelable)229 TEST_P(NdkBinderTest_Aidl, ConstantsInParcelable) {
230   ASSERT_EQ(0, Foo::kZero);
231   ASSERT_EQ(1, Foo::kOne);
232   ASSERT_EQ(0xffffffff, Foo::kOnes);
233   ASSERT_EQ(1, Foo::kByteOne);
234   ASSERT_EQ(0xffffffffffffffff, Foo::kLongOnes);
235   ASSERT_EQ(std::string(""), Foo::kEmpty);
236   ASSERT_EQ(std::string("foo"), Foo::kFoo);
237 }
238 
TEST_P(NdkBinderTest_Aidl,ConstantsInUnion)239 TEST_P(NdkBinderTest_Aidl, ConstantsInUnion) {
240   ASSERT_EQ(0, SimpleUnion::kZero);
241   ASSERT_EQ(1, SimpleUnion::kOne);
242   ASSERT_EQ(0xffffffff, SimpleUnion::kOnes);
243   ASSERT_EQ(1, SimpleUnion::kByteOne);
244   ASSERT_EQ(0xffffffffffffffff, SimpleUnion::kLongOnes);
245   ASSERT_EQ(std::string(""), SimpleUnion::kEmpty);
246   ASSERT_EQ(std::string("foo"), SimpleUnion::kFoo);
247 }
248 
TEST_P(NdkBinderTest_Aidl,RepeatPrimitiveInt)249 TEST_P(NdkBinderTest_Aidl, RepeatPrimitiveInt) {
250   int32_t out;
251   ASSERT_OK(iface->RepeatInt(3, &out));
252   EXPECT_EQ(3, out);
253 }
254 
TEST_P(NdkBinderTest_Aidl,RepeatPrimitiveLong)255 TEST_P(NdkBinderTest_Aidl, RepeatPrimitiveLong) {
256   int64_t out;
257   ASSERT_OK(iface->RepeatLong(3, &out));
258   EXPECT_EQ(3, out);
259 }
260 
TEST_P(NdkBinderTest_Aidl,RepeatPrimitiveFloat)261 TEST_P(NdkBinderTest_Aidl, RepeatPrimitiveFloat) {
262   float out;
263   ASSERT_OK(iface->RepeatFloat(2.0f, &out));
264   EXPECT_EQ(2.0f, out);
265 }
266 
TEST_P(NdkBinderTest_Aidl,RepeatPrimitiveDouble)267 TEST_P(NdkBinderTest_Aidl, RepeatPrimitiveDouble) {
268   double out;
269   ASSERT_OK(iface->RepeatDouble(3.0, &out));
270   EXPECT_EQ(3.0, out);
271 }
272 
TEST_P(NdkBinderTest_Aidl,RepeatPrimitiveBoolean)273 TEST_P(NdkBinderTest_Aidl, RepeatPrimitiveBoolean) {
274   bool out;
275   ASSERT_OK(iface->RepeatBoolean(true, &out));
276   EXPECT_EQ(true, out);
277 }
278 
TEST_P(NdkBinderTest_Aidl,RepeatPrimitiveChar)279 TEST_P(NdkBinderTest_Aidl, RepeatPrimitiveChar) {
280   char16_t out;
281   ASSERT_OK(iface->RepeatChar(L'@', &out));
282   EXPECT_EQ(L'@', out);
283 }
284 
TEST_P(NdkBinderTest_Aidl,RepeatPrimitiveByte)285 TEST_P(NdkBinderTest_Aidl, RepeatPrimitiveByte) {
286   int8_t out;
287   ASSERT_OK(iface->RepeatByte(3, &out));
288   EXPECT_EQ(3, out);
289 }
290 
TEST_P(NdkBinderTest_Aidl,RepeatPrimitiveByteEnum)291 TEST_P(NdkBinderTest_Aidl, RepeatPrimitiveByteEnum) {
292   ByteEnum out;
293   ASSERT_OK(iface->RepeatByteEnum(ByteEnum::FOO, &out));
294   EXPECT_EQ(ByteEnum::FOO, out);
295 }
296 
TEST_P(NdkBinderTest_Aidl,RepeatPrimitiveIntEnum)297 TEST_P(NdkBinderTest_Aidl, RepeatPrimitiveIntEnum) {
298   IntEnum out;
299   ASSERT_OK(iface->RepeatIntEnum(IntEnum::FOO, &out));
300   EXPECT_EQ(IntEnum::FOO, out);
301 }
302 
TEST_P(NdkBinderTest_Aidl,RepeatPrimitiveIntEnumUndefined)303 TEST_P(NdkBinderTest_Aidl, RepeatPrimitiveIntEnumUndefined) {
304   IntEnum out;
305   ASSERT_OK(iface->RepeatIntEnum(static_cast<IntEnum>(12), &out));
306   EXPECT_EQ(static_cast<IntEnum>(12), out);
307 }
308 
TEST_P(NdkBinderTest_Aidl,RepeatPrimitiveIntEnumIncorrectBitwiseOp)309 TEST_P(NdkBinderTest_Aidl, RepeatPrimitiveIntEnumIncorrectBitwiseOp) {
310   IntEnum out;
311   IntEnum in =
312       static_cast<IntEnum>(static_cast<int32_t>(IntEnum::FOO) | static_cast<int32_t>(IntEnum::BAR));
313   ASSERT_OK(iface->RepeatIntEnum(in, &out));
314   EXPECT_EQ(in, out);
315 }
316 
TEST_P(NdkBinderTest_Aidl,RepeatPrimitiveLongEnum)317 TEST_P(NdkBinderTest_Aidl, RepeatPrimitiveLongEnum) {
318   LongEnum out;
319   ASSERT_OK(iface->RepeatLongEnum(LongEnum::FOO, &out));
320   EXPECT_EQ(LongEnum::FOO, out);
321 }
322 
TEST_P(NdkBinderTest_Aidl,EnumToString)323 TEST_P(NdkBinderTest_Aidl, EnumToString) {
324   EXPECT_EQ(toString(ByteEnum::FOO), "FOO");
325   EXPECT_EQ(toString(IntEnum::BAR), "BAR");
326   EXPECT_EQ(toString(LongEnum::FOO), "FOO");
327 
328   EXPECT_EQ(toString(static_cast<IntEnum>(-1)), "-1");
329 }
330 
TEST_P(NdkBinderTest_Aidl,EnumValues)331 TEST_P(NdkBinderTest_Aidl, EnumValues) {
332   auto range = ::ndk::enum_range<ByteEnum>();
333   auto iter = range.begin();
334   EXPECT_EQ(ByteEnum::FOO, *iter++);
335   EXPECT_EQ(ByteEnum::BAR, *iter++);
336   EXPECT_EQ(range.end(), iter);
337 }
338 
TEST_P(NdkBinderTest_Aidl,RepeatBinder)339 TEST_P(NdkBinderTest_Aidl, RepeatBinder) {
340   SpAIBinder binder = iface->asBinder();
341   SpAIBinder ret;
342 
343   ASSERT_OK(iface->RepeatBinder(binder, &ret));
344   EXPECT_EQ(binder.get(), ret.get());
345 
346   if (shouldBeWrapped) {
347     ndk::ScopedAStatus status = iface->RepeatBinder(nullptr, &ret);
348     ASSERT_EQ(STATUS_UNEXPECTED_NULL, AStatus_getStatus(status.get()));
349   } else {
350     ASSERT_OK(iface->RepeatBinder(nullptr, &ret));
351     EXPECT_EQ(nullptr, ret.get());
352   }
353 
354   ASSERT_OK(iface->RepeatNullableBinder(binder, &ret));
355   EXPECT_EQ(binder.get(), ret.get());
356 
357   ASSERT_OK(iface->RepeatNullableBinder(nullptr, &ret));
358   EXPECT_EQ(nullptr, ret.get());
359 }
360 
TEST_P(NdkBinderTest_Aidl,RepeatInterface)361 TEST_P(NdkBinderTest_Aidl, RepeatInterface) {
362   std::shared_ptr<IEmpty> empty = SharedRefBase::make<MyEmpty>();
363 
364   std::shared_ptr<IEmpty> ret;
365   ASSERT_OK(iface->RepeatInterface(empty, &ret));
366   EXPECT_EQ(empty.get(), ret.get());
367 
368   // b/210547999
369   // interface writes are always nullable in AIDL C++ (but reads are not
370   // nullable by default). However, the NDK backend follows the Java behavior
371   // and always allows interfaces to be nullable (for reads and writes).
372   ASSERT_OK(iface->RepeatInterface(nullptr, &ret));
373   EXPECT_EQ(nullptr, ret.get());
374 
375   ASSERT_OK(iface->RepeatNullableInterface(empty, &ret));
376   EXPECT_EQ(empty.get(), ret.get());
377 
378   ASSERT_OK(iface->RepeatNullableInterface(nullptr, &ret));
379   EXPECT_EQ(nullptr, ret.get());
380 }
381 
checkInOut(const ScopedFileDescriptor & inFd,const ScopedFileDescriptor & outFd)382 static void checkInOut(const ScopedFileDescriptor& inFd,
383                        const ScopedFileDescriptor& outFd) {
384   static const std::string kContent = "asdf";
385 
386   ASSERT_EQ(static_cast<int>(kContent.size()),
387             write(inFd.get(), kContent.data(), kContent.size()));
388 
389   std::string out;
390   out.resize(kContent.size());
391   ASSERT_EQ(static_cast<int>(kContent.size()),
392             read(outFd.get(), &out[0], kContent.size()));
393 
394   EXPECT_EQ(kContent, out);
395 }
396 
checkFdRepeat(const std::shared_ptr<ITest> & test,ScopedAStatus (ITest::* repeatFd)(const ScopedFileDescriptor &,ScopedFileDescriptor *))397 static void checkFdRepeat(
398     const std::shared_ptr<ITest>& test,
399     ScopedAStatus (ITest::*repeatFd)(const ScopedFileDescriptor&,
400                                      ScopedFileDescriptor*)) {
401   int fds[2];
402 
403   while (pipe(fds) == -1 && errno == EAGAIN)
404     ;
405 
406   ScopedFileDescriptor readFd(fds[0]);
407   ScopedFileDescriptor writeFd(fds[1]);
408 
409   ScopedFileDescriptor readOutFd;
410   ASSERT_OK((test.get()->*repeatFd)(readFd, &readOutFd));
411 
412   checkInOut(writeFd, readOutFd);
413 }
414 
TEST_P(NdkBinderTest_Aidl,RepeatFdArray)415 TEST_P(NdkBinderTest_Aidl, RepeatFdArray) {
416   int fds[2];
417 
418   while (pipe(fds) == -1 && errno == EAGAIN)
419     ;
420   std::vector<ScopedFileDescriptor> sfds;
421   sfds.emplace_back(fds[0]);
422   sfds.emplace_back(fds[1]);
423 
424   std::vector<ScopedFileDescriptor> sfds_out1;
425   sfds_out1.resize(sfds.size());
426   std::vector<ScopedFileDescriptor> sfds_out2;
427 
428   ASSERT_OK((iface->RepeatFdArray(sfds, &sfds_out1, &sfds_out2)));
429 
430   // sfds <-> sfds_out1
431   checkInOut(sfds[1], sfds_out1[0]);
432   checkInOut(sfds_out1[1], sfds[0]);
433 
434   // sfds_out1 <-> sfds_out2
435   checkInOut(sfds_out1[1], sfds_out2[0]);
436   checkInOut(sfds_out2[1], sfds_out1[0]);
437 
438   // sfds <-> sfds_out2
439   checkInOut(sfds[1], sfds_out2[0]);
440   checkInOut(sfds_out2[1], sfds[0]);
441 }
442 
TEST_P(NdkBinderTest_Aidl,RepeatFd)443 TEST_P(NdkBinderTest_Aidl, RepeatFd) { checkFdRepeat(iface, &ITest::RepeatFd); }
444 
TEST_P(NdkBinderTest_Aidl,RepeatFdNull)445 TEST_P(NdkBinderTest_Aidl, RepeatFdNull) {
446   ScopedFileDescriptor fd;
447   // FD is different from most types because the standard type used to represent
448   // it can also contain a null value (this is why many other types don't have
449   // 'null' tests for the non-@nullable Repeat* functions).
450   //
451   // Even worse, these are default initialized to this value, so it's a pretty
452   // common error:
453   EXPECT_EQ(fd.get(), -1);
454   ScopedFileDescriptor out;
455 
456   if (shouldBeWrapped) {
457     ASSERT_EQ(STATUS_UNEXPECTED_NULL, AStatus_getStatus(iface->RepeatFd(fd, &out).get()));
458   } else {
459     // another in/out-process difference
460     ASSERT_OK(iface->RepeatFd(fd, &out));
461   }
462 }
463 
TEST_P(NdkBinderTest_Aidl,RepeatNullableFd)464 TEST_P(NdkBinderTest_Aidl, RepeatNullableFd) {
465   checkFdRepeat(iface, &ITest::RepeatNullableFd);
466 
467   ScopedFileDescriptor in;
468   EXPECT_EQ(-1, in.get());
469 
470   ScopedFileDescriptor out;
471   ASSERT_OK(iface->RepeatNullableFd(in, &out));
472 
473   EXPECT_EQ(-1, out.get());
474 }
475 
TEST_P(NdkBinderTest_Aidl,RepeatString)476 TEST_P(NdkBinderTest_Aidl, RepeatString) {
477   std::string res;
478 
479   EXPECT_OK(iface->RepeatString("", &res));
480   EXPECT_EQ("", res);
481 
482   EXPECT_OK(iface->RepeatString("a", &res));
483   EXPECT_EQ("a", res);
484 
485   EXPECT_OK(iface->RepeatString("say what?", &res));
486   EXPECT_EQ("say what?", res);
487 
488   std::string stringWithNulls = "asdf";
489   stringWithNulls[1] = '\0';
490 
491   EXPECT_OK(iface->RepeatString(stringWithNulls, &res));
492   EXPECT_EQ(stringWithNulls, res);
493 }
494 
TEST_P(NdkBinderTest_Aidl,RepeatNullableString)495 TEST_P(NdkBinderTest_Aidl, RepeatNullableString) {
496   std::optional<std::string> res;
497 
498   EXPECT_OK(iface->RepeatNullableString(std::nullopt, &res));
499   EXPECT_EQ(std::nullopt, res);
500 
501   EXPECT_OK(iface->RepeatNullableString("", &res));
502   EXPECT_EQ("", *res);
503 
504   EXPECT_OK(iface->RepeatNullableString("a", &res));
505   EXPECT_EQ("a", *res);
506 
507   EXPECT_OK(iface->RepeatNullableString("say what?", &res));
508   EXPECT_EQ("say what?", *res);
509 }
510 
TEST_P(NdkBinderTest_Aidl,ParcelableOrder)511 TEST_P(NdkBinderTest_Aidl, ParcelableOrder) {
512   RegularPolygon p1 = {"A", 1, 1.0f};
513 
514   // tests on self
515   EXPECT_EQ(p1, p1);
516   EXPECT_LE(p1, p1);
517   EXPECT_GE(p1, p1);
518   EXPECT_FALSE(p1 < p1);
519   EXPECT_FALSE(p1 > p1);
520 
521   RegularPolygon p2 = {"A", 2, 1.0f};
522   RegularPolygon p3 = {"B", 1, 1.0f};
523   for (const auto& bigger : {p2, p3}) {
524     EXPECT_FALSE(p1 == bigger);
525     EXPECT_LE(p1, bigger);
526     EXPECT_GE(bigger, p1);
527     EXPECT_LT(p1, bigger);
528     EXPECT_GT(bigger, p1);
529   }
530 }
531 
TEST_P(NdkBinderTest_Aidl,ParcelableDefaults)532 TEST_P(NdkBinderTest_Aidl, ParcelableDefaults) {
533   RegularPolygon polygon;
534 
535   EXPECT_EQ("square", polygon.name);
536   EXPECT_EQ(4, polygon.numSides);
537   EXPECT_EQ(1.0f, polygon.sideLength);
538 }
539 
TEST_P(NdkBinderTest_Aidl,RepeatPolygon)540 TEST_P(NdkBinderTest_Aidl, RepeatPolygon) {
541   RegularPolygon defaultPolygon = {"hexagon", 6, 2.0f};
542   RegularPolygon outputPolygon;
543   ASSERT_OK(iface->RepeatPolygon(defaultPolygon, &outputPolygon));
544   EXPECT_EQ(defaultPolygon, outputPolygon);
545 }
546 
TEST_P(NdkBinderTest_Aidl,RepeatNullNullablePolygon)547 TEST_P(NdkBinderTest_Aidl, RepeatNullNullablePolygon) {
548   std::optional<RegularPolygon> defaultPolygon;
549   std::optional<RegularPolygon> outputPolygon;
550   ASSERT_OK(iface->RepeatNullablePolygon(defaultPolygon, &outputPolygon));
551   EXPECT_EQ(defaultPolygon, outputPolygon);
552 }
553 
TEST_P(NdkBinderTest_Aidl,RepeatPresentNullablePolygon)554 TEST_P(NdkBinderTest_Aidl, RepeatPresentNullablePolygon) {
555   std::optional<RegularPolygon> defaultPolygon =
556       std::optional<RegularPolygon>({"septagon", 7, 3.0f});
557   std::optional<RegularPolygon> outputPolygon;
558   ASSERT_OK(iface->RepeatNullablePolygon(defaultPolygon, &outputPolygon));
559   EXPECT_EQ(defaultPolygon, outputPolygon);
560 }
561 
TEST_P(NdkBinderTest_Aidl,RepeatDefaultPersistableBundle)562 TEST_P(NdkBinderTest_Aidl, RepeatDefaultPersistableBundle) {
563   PersistableBundle defaultPBundle;
564   PersistableBundle outPBundle;
565   ASSERT_OK(iface->RepeatPersistableBundle(defaultPBundle, &outPBundle));
566   // The == operator checks the underlying pointers which should be different
567   EXPECT_NE(defaultPBundle, outPBundle);
568   // The deepEquals function checks the contents of the bundle, which should be
569   // the same
570   EXPECT_TRUE(defaultPBundle.deepEquals(outPBundle));
571 }
572 
573 const bool kBoolVal = true;
574 const int32_t kIntVal = 11111;
575 const int64_t kLongVal = 12345;
576 const double kDoubleVal = 54321;
577 const std::string kStringVal = "cool";
578 const std::vector<bool> kBoolVVal = {true, false, true};
579 const std::vector<int32_t> kIntVVal = {1111, -2222, 3333};
580 const std::vector<int64_t> kLongVVal = {11111, -22222, 33333};
581 const std::vector<double> kDoubleVVal = {111111, -222222, 333333};
582 const std::vector<std::string> kStringVVal = {"hello", "monkey", "!"};
583 
TEST_P(NdkBinderTest_Aidl,RepeatTypesPersistableBundle)584 TEST_P(NdkBinderTest_Aidl, RepeatTypesPersistableBundle) {
585   PersistableBundle inPBundle;
586   PersistableBundle outPBundle;
587   // put all supported types && verify
588   inPBundle.putBoolean("bool", kBoolVal);
589   inPBundle.putInt("int", kIntVal);
590   inPBundle.putLong("long", kLongVal);
591   inPBundle.putDouble("double", kDoubleVal);
592   inPBundle.putString("string", kStringVal);
593   inPBundle.putBooleanVector("boolv", kBoolVVal);
594   inPBundle.putIntVector("intv", kIntVVal);
595   inPBundle.putLongVector("longv", kLongVVal);
596   inPBundle.putDoubleVector("doublev", kDoubleVVal);
597   inPBundle.putStringVector("stringv", kStringVVal);
598   PersistableBundle innerBundle;
599   innerBundle.putBoolean("bool", kBoolVal);
600   innerBundle.putInt("int", kIntVal);
601   inPBundle.putPersistableBundle("pbundle", innerBundle);
602   bool outBool = false;
603   int32_t outInt = 0;
604   int64_t outLong = 0;
605   double outDouble = 0;
606   std::string outString = std::string();
607   std::vector<bool> outBoolV = std::vector<bool>();
608   std::vector<int32_t> outIntV = std::vector<int32_t>();
609   std::vector<int64_t> outLongV = std::vector<int64_t>();
610   std::vector<double> outDoubleV = std::vector<double>();
611   std::vector<std::string> outStringV = std::vector<std::string>();
612   PersistableBundle outInnerBundle;
613   EXPECT_TRUE(inPBundle.getBoolean("bool", &outBool));
614   EXPECT_EQ(outBool, kBoolVal);
615   EXPECT_TRUE(inPBundle.getInt("int", &outInt));
616   EXPECT_EQ(outInt, kIntVal);
617   EXPECT_TRUE(inPBundle.getLong("long", &outLong));
618   EXPECT_EQ(outLong, kLongVal);
619   EXPECT_TRUE(inPBundle.getDouble("double", &outDouble));
620   EXPECT_EQ(outDouble, kDoubleVal);
621   EXPECT_TRUE(inPBundle.getString("string", &outString));
622   EXPECT_EQ(outString, kStringVal);
623   EXPECT_TRUE(inPBundle.getBooleanVector("boolv", &outBoolV));
624   EXPECT_EQ(outBoolV, kBoolVVal);
625   EXPECT_TRUE(inPBundle.getIntVector("intv", &outIntV));
626   EXPECT_EQ(outIntV, kIntVVal);
627   EXPECT_TRUE(inPBundle.getLongVector("longv", &outLongV));
628   EXPECT_EQ(outLongV, kLongVVal);
629   EXPECT_TRUE(inPBundle.getDoubleVector("doublev", &outDoubleV));
630   EXPECT_EQ(outDoubleV, kDoubleVVal);
631   EXPECT_TRUE(inPBundle.getStringVector("stringv", &outStringV));
632   EXPECT_EQ(outStringV, kStringVVal);
633   EXPECT_TRUE(inPBundle.getPersistableBundle("pbundle", &outInnerBundle));
634   EXPECT_TRUE(innerBundle.deepEquals(outInnerBundle));
635 
636   ASSERT_OK(iface->RepeatPersistableBundle(inPBundle, &outPBundle));
637 
638   // verify all supported types make it to/from the service
639   outBool = false;
640   outInt = 0;
641   outLong = 0;
642   outDouble = 0;
643   outString = std::string();
644   outBoolV.clear();
645   outIntV.clear();
646   outLongV.clear();
647   outDoubleV.clear();
648   outInnerBundle = PersistableBundle();
649   // The == operator checks the underlying pointers which should be different
650   EXPECT_NE(inPBundle, outPBundle);
651   // The deepEquals function checks the contents of the bundle, which should be
652   // the same
653   EXPECT_TRUE(inPBundle.deepEquals(outPBundle));
654   EXPECT_EQ(inPBundle.size(), outPBundle.size());
655 
656   EXPECT_TRUE(outPBundle.getBoolean("bool", &outBool));
657   EXPECT_EQ(outBool, kBoolVal);
658   EXPECT_TRUE(outPBundle.getInt("int", &outInt));
659   EXPECT_EQ(outInt, kIntVal);
660   EXPECT_TRUE(outPBundle.getLong("long", &outLong));
661   EXPECT_EQ(outLong, kLongVal);
662   EXPECT_TRUE(outPBundle.getDouble("double", &outDouble));
663   EXPECT_EQ(outDouble, kDoubleVal);
664   EXPECT_TRUE(outPBundle.getString("string", &outString));
665   EXPECT_EQ(outString, kStringVal);
666   EXPECT_TRUE(outPBundle.getBooleanVector("boolv", &outBoolV));
667   EXPECT_EQ(outBoolV, kBoolVVal);
668   EXPECT_TRUE(outPBundle.getIntVector("intv", &outIntV));
669   EXPECT_EQ(outIntV, kIntVVal);
670   EXPECT_TRUE(outPBundle.getLongVector("longv", &outLongV));
671   EXPECT_EQ(outLongV, kLongVVal);
672   EXPECT_TRUE(outPBundle.getDoubleVector("doublev", &outDoubleV));
673   EXPECT_EQ(outDoubleV, kDoubleVVal);
674   EXPECT_TRUE(outPBundle.getPersistableBundle("pbundle", &outInnerBundle));
675   EXPECT_TRUE(innerBundle.deepEquals(outInnerBundle));
676 }
677 
TEST_P(NdkBinderTest_Aidl,EraseAllPersistableBundle)678 TEST_P(NdkBinderTest_Aidl, EraseAllPersistableBundle) {
679   PersistableBundle pBundle;
680   // fill it up, empty it out and verify sizes along the way
681   EXPECT_EQ(0, pBundle.size());
682   pBundle.putBoolean("bool", kBoolVal);
683   pBundle.putInt("int", kIntVal);
684   pBundle.putLong("long", kLongVal);
685   pBundle.putDouble("double", kDoubleVal);
686   pBundle.putString("string", kStringVal);
687   EXPECT_GT(pBundle.size(), 0);
688   EXPECT_GT(pBundle.erase("bool"), 0);
689   EXPECT_GT(pBundle.erase("int"), 0);
690   EXPECT_GT(pBundle.erase("long"), 0);
691   EXPECT_GT(pBundle.erase("double"), 0);
692   EXPECT_GT(pBundle.erase("string"), 0);
693   EXPECT_EQ(0, pBundle.size());
694 }
695 
TEST_P(NdkBinderTest_Aidl,GetBoolKeysPersistableBundle)696 TEST_P(NdkBinderTest_Aidl, GetBoolKeysPersistableBundle) {
697   PersistableBundle pBundle;
698   pBundle.putBoolean("first", kBoolVal);
699   pBundle.putBoolean("second", kBoolVal);
700   pBundle.putBoolean("third", kBoolVal);
701   EXPECT_EQ(3, pBundle.size());
702   std::set<std::string> ret = pBundle.getBooleanKeys();
703   EXPECT_EQ(3, ret.size());
704   EXPECT_EQ(ret, std::set<std::string>({"first", "second", "third"}));
705 }
706 
TEST_P(NdkBinderTest_Aidl,GetIntKeysPersistableBundle)707 TEST_P(NdkBinderTest_Aidl, GetIntKeysPersistableBundle) {
708   PersistableBundle pBundle;
709   pBundle.putInt("first", kIntVal);
710   pBundle.putInt("second", kIntVal);
711   pBundle.putInt("third", kIntVal);
712   EXPECT_EQ(3, pBundle.size());
713   std::set<std::string> ret = pBundle.getIntKeys();
714   EXPECT_EQ(3, ret.size());
715   EXPECT_EQ(ret, std::set<std::string>({"first", "second", "third"}));
716 }
717 
TEST_P(NdkBinderTest_Aidl,GetLongKeysPersistableBundle)718 TEST_P(NdkBinderTest_Aidl, GetLongKeysPersistableBundle) {
719   PersistableBundle pBundle;
720   pBundle.putLong("first", kLongVal);
721   pBundle.putLong("second", kLongVal);
722   pBundle.putLong("third", kLongVal);
723   EXPECT_EQ(3, pBundle.size());
724   std::set<std::string> ret = pBundle.getLongKeys();
725   EXPECT_EQ(3, ret.size());
726   EXPECT_EQ(ret, std::set<std::string>({"first", "second", "third"}));
727 }
728 
TEST_P(NdkBinderTest_Aidl,GetDoubleKeysPersistableBundle)729 TEST_P(NdkBinderTest_Aidl, GetDoubleKeysPersistableBundle) {
730   PersistableBundle pBundle;
731   pBundle.putDouble("first", kDoubleVal);
732   pBundle.putDouble("second", kDoubleVal);
733   pBundle.putDouble("third", kDoubleVal);
734   EXPECT_EQ(3, pBundle.size());
735   std::set<std::string> ret = pBundle.getDoubleKeys();
736   EXPECT_EQ(3, ret.size());
737   EXPECT_EQ(ret, std::set<std::string>({"first", "second", "third"}));
738 }
739 
TEST_P(NdkBinderTest_Aidl,GetStringKeysPersistableBundle)740 TEST_P(NdkBinderTest_Aidl, GetStringKeysPersistableBundle) {
741   PersistableBundle pBundle;
742   pBundle.putString("first", kStringVal);
743   pBundle.putString("second", kStringVal);
744   pBundle.putString("third", kStringVal);
745   EXPECT_EQ(3, pBundle.size());
746   std::set<std::string> ret = pBundle.getStringKeys();
747   EXPECT_EQ(3, ret.size());
748   EXPECT_EQ(ret, std::set<std::string>({"first", "second", "third"}));
749 }
750 
TEST_P(NdkBinderTest_Aidl,GetBooleanVectorKeysPersistableBundle)751 TEST_P(NdkBinderTest_Aidl, GetBooleanVectorKeysPersistableBundle) {
752   PersistableBundle pBundle;
753   pBundle.putBooleanVector("first", kBoolVVal);
754   pBundle.putBooleanVector("second", kBoolVVal);
755   pBundle.putBooleanVector("third", kBoolVVal);
756   EXPECT_EQ(3, pBundle.size());
757   std::set<std::string> ret = pBundle.getBooleanVectorKeys();
758   EXPECT_EQ(3, ret.size());
759   EXPECT_EQ(ret, std::set<std::string>({"first", "second", "third"}));
760 }
761 
TEST_P(NdkBinderTest_Aidl,GetIntVectorKeysPersistableBundle)762 TEST_P(NdkBinderTest_Aidl, GetIntVectorKeysPersistableBundle) {
763   PersistableBundle pBundle;
764   pBundle.putIntVector("first", kIntVVal);
765   pBundle.putIntVector("second", kIntVVal);
766   pBundle.putIntVector("third", kIntVVal);
767   EXPECT_EQ(3, pBundle.size());
768   std::set<std::string> ret = pBundle.getIntVectorKeys();
769   EXPECT_EQ(3, ret.size());
770   EXPECT_EQ(ret, std::set<std::string>({"first", "second", "third"}));
771 }
772 
TEST_P(NdkBinderTest_Aidl,GetLongVectorKeysPersistableBundle)773 TEST_P(NdkBinderTest_Aidl, GetLongVectorKeysPersistableBundle) {
774   PersistableBundle pBundle;
775   pBundle.putLongVector("first", kLongVVal);
776   pBundle.putLongVector("second", kLongVVal);
777   pBundle.putLongVector("third", kLongVVal);
778   EXPECT_EQ(3, pBundle.size());
779   std::set<std::string> ret = pBundle.getLongVectorKeys();
780   EXPECT_EQ(3, ret.size());
781   EXPECT_EQ(ret, std::set<std::string>({"first", "second", "third"}));
782 }
783 
TEST_P(NdkBinderTest_Aidl,GetDoubleVectorKeysPersistableBundle)784 TEST_P(NdkBinderTest_Aidl, GetDoubleVectorKeysPersistableBundle) {
785   PersistableBundle pBundle;
786   pBundle.putDoubleVector("first", kDoubleVVal);
787   pBundle.putDoubleVector("second", kDoubleVVal);
788   pBundle.putDoubleVector("third", kDoubleVVal);
789   EXPECT_EQ(3, pBundle.size());
790   std::set<std::string> ret = pBundle.getDoubleVectorKeys();
791   EXPECT_EQ(3, ret.size());
792   EXPECT_EQ(ret, std::set<std::string>({"first", "second", "third"}));
793 }
794 
TEST_P(NdkBinderTest_Aidl,GetStringVectorKeysPersistableBundle)795 TEST_P(NdkBinderTest_Aidl, GetStringVectorKeysPersistableBundle) {
796   PersistableBundle pBundle;
797   pBundle.putStringVector("first", kStringVVal);
798   pBundle.putStringVector("second", kStringVVal);
799   pBundle.putStringVector("third", kStringVVal);
800   EXPECT_EQ(3, pBundle.size());
801   std::set<std::string> ret = pBundle.getStringVectorKeys();
802   EXPECT_EQ(3, ret.size());
803   EXPECT_EQ(ret, std::set<std::string>({"first", "second", "third"}));
804 }
805 
TEST_P(NdkBinderTest_Aidl,GetPersistableBundleKeysPersistableBundle)806 TEST_P(NdkBinderTest_Aidl, GetPersistableBundleKeysPersistableBundle) {
807   PersistableBundle pBundle;
808   PersistableBundle innerBundle;
809   pBundle.putPersistableBundle("first", innerBundle);
810   pBundle.putPersistableBundle("second", innerBundle);
811   pBundle.putPersistableBundle("third", innerBundle);
812   EXPECT_EQ(3, pBundle.size());
813   std::set<std::string> ret = pBundle.getPersistableBundleKeys();
814   EXPECT_EQ(3, ret.size());
815   EXPECT_EQ(ret, std::set<std::string>({"first", "second", "third"}));
816 }
817 
TEST_P(NdkBinderTest_Aidl,InsAndOuts)818 TEST_P(NdkBinderTest_Aidl, InsAndOuts) {
819   RegularPolygon defaultPolygon;
820   ASSERT_OK(iface->RenamePolygon(&defaultPolygon, "Jerry"));
821   EXPECT_EQ("Jerry", defaultPolygon.name);
822 }
823 
TEST_P(NdkBinderTest_Aidl,NewField)824 TEST_P(NdkBinderTest_Aidl, NewField) {
825   Baz baz;
826   baz.d = {"a", "b", "c"};
827 
828   Baz outbaz;
829 
830   ASSERT_OK(getCompatTest(iface)->repeatBaz(baz, &outbaz));
831 
832   if (GetParam().shouldBeOld) {
833     EXPECT_EQ(std::nullopt, outbaz.d);
834   } else {
835     EXPECT_EQ(baz.d, outbaz.d);
836   }
837 }
838 
TEST_P(NdkBinderTest_Aidl,RenameFoo)839 TEST_P(NdkBinderTest_Aidl, RenameFoo) {
840   Foo foo;
841   Foo outputFoo;
842   ASSERT_OK(iface->renameFoo(&foo, "MYFOO"));
843 
844   EXPECT_EQ("MYFOO", foo.a);
845 }
846 
TEST_P(NdkBinderTest_Aidl,RenameBar)847 TEST_P(NdkBinderTest_Aidl, RenameBar) {
848   Foo foo;
849   Foo outputFoo;
850   ASSERT_OK(iface->renameBar(&foo, "MYBAR"));
851 
852   EXPECT_EQ("MYBAR", foo.d.a);
853 }
854 
TEST_P(NdkBinderTest_Aidl,GetLastItem)855 TEST_P(NdkBinderTest_Aidl, GetLastItem) {
856   Foo foo;
857   foo.f = 15;
858   int retF;
859   ASSERT_OK(iface->getF(foo, &retF));
860   EXPECT_EQ(15, retF);
861 }
862 
TEST_P(NdkBinderTest_Aidl,RepeatFoo)863 TEST_P(NdkBinderTest_Aidl, RepeatFoo) {
864   Foo foo;
865   foo.a = "NEW FOO";
866   foo.b = 57;
867   foo.d.b = "a";
868   foo.e.d = 99;
869   foo.shouldBeByteBar = ByteEnum::BAR;
870   foo.shouldBeIntBar = IntEnum::BAR;
871   foo.shouldBeLongBar = LongEnum::BAR;
872   foo.shouldContainTwoByteFoos = {ByteEnum::FOO, ByteEnum::FOO};
873   foo.shouldContainTwoIntFoos = {IntEnum::FOO, IntEnum::FOO};
874   foo.shouldContainTwoLongFoos = {LongEnum::FOO, LongEnum::FOO};
875   foo.u = SimpleUnion::make<SimpleUnion::c>("hello");
876   foo.shouldSetBit0AndBit2 = Foo::BIT0 | Foo::BIT2;
877   foo.shouldBeConstS1 = SimpleUnion::S1;
878 
879   Foo retFoo;
880 
881   ASSERT_OK(iface->repeatFoo(foo, &retFoo));
882 
883   EXPECT_EQ(foo.a, retFoo.a);
884   EXPECT_EQ(foo.b, retFoo.b);
885   EXPECT_EQ(foo.d.b, retFoo.d.b);
886   EXPECT_EQ(foo.e.d, retFoo.e.d);
887   EXPECT_EQ(foo.shouldBeByteBar, retFoo.shouldBeByteBar);
888   EXPECT_EQ(foo.shouldBeIntBar, retFoo.shouldBeIntBar);
889   EXPECT_EQ(foo.shouldBeLongBar, retFoo.shouldBeLongBar);
890   EXPECT_EQ(foo.shouldContainTwoByteFoos, retFoo.shouldContainTwoByteFoos);
891   EXPECT_EQ(foo.shouldContainTwoIntFoos, retFoo.shouldContainTwoIntFoos);
892   EXPECT_EQ(foo.shouldContainTwoLongFoos, retFoo.shouldContainTwoLongFoos);
893   EXPECT_EQ(foo.u, retFoo.u);
894   EXPECT_EQ(foo.shouldSetBit0AndBit2, retFoo.shouldSetBit0AndBit2);
895   EXPECT_EQ(foo.shouldBeConstS1, retFoo.shouldBeConstS1);
896 }
897 
TEST_P(NdkBinderTest_Aidl,RepeatGenericBar)898 TEST_P(NdkBinderTest_Aidl, RepeatGenericBar) {
899   GenericBar<int32_t> bar;
900   bar.a = 40;
901   bar.shouldBeGenericFoo.a = 41;
902   bar.shouldBeGenericFoo.b = 42;
903 
904   GenericBar<int32_t> retBar;
905 
906   ASSERT_OK(iface->repeatGenericBar(bar, &retBar));
907 
908   EXPECT_EQ(bar.a, retBar.a);
909   EXPECT_EQ(bar.shouldBeGenericFoo.a, retBar.shouldBeGenericFoo.a);
910   EXPECT_EQ(bar.shouldBeGenericFoo.b, retBar.shouldBeGenericFoo.b);
911 }
912 
913 template <typename T>
914 using RepeatMethod = ScopedAStatus (ITest::*)(const std::vector<T>&,
915                                               std::vector<T>*, std::vector<T>*);
916 
917 template <typename T>
testRepeat(const std::shared_ptr<ITest> & i,RepeatMethod<T> repeatMethod,std::vector<std::vector<T>> tests)918 void testRepeat(const std::shared_ptr<ITest>& i, RepeatMethod<T> repeatMethod,
919                 std::vector<std::vector<T>> tests) {
920   for (const auto& input : tests) {
921     std::vector<T> out1;
922     out1.resize(input.size());
923     std::vector<T> out2;
924 
925     ASSERT_OK((i.get()->*repeatMethod)(input, &out1, &out2)) << input.size();
926     EXPECT_EQ(input, out1);
927     EXPECT_EQ(input, out2);
928   }
929 }
930 
931 template <typename T>
testRepeat2List(const std::shared_ptr<ITest> & i,RepeatMethod<T> repeatMethod,std::vector<std::vector<T>> tests)932 void testRepeat2List(const std::shared_ptr<ITest>& i, RepeatMethod<T> repeatMethod,
933                      std::vector<std::vector<T>> tests) {
934   for (const auto& input : tests) {
935     std::vector<T> out1;
936     std::vector<T> out2;
937     std::vector<T> expected;
938 
939     expected.insert(expected.end(), input.begin(), input.end());
940     expected.insert(expected.end(), input.begin(), input.end());
941 
942     ASSERT_OK((i.get()->*repeatMethod)(input, &out1, &out2)) << expected.size();
943     EXPECT_EQ(expected, out1);
944     EXPECT_EQ(expected, out2);
945   }
946 }
947 
TEST_P(NdkBinderTest_Aidl,Arrays)948 TEST_P(NdkBinderTest_Aidl, Arrays) {
949   testRepeat<bool>(iface, &ITest::RepeatBooleanArray,
950                    {
951                        {},
952                        {true},
953                        {false, true, false},
954                    });
955   testRepeat<uint8_t>(iface, &ITest::RepeatByteArray,
956                       {
957                           {},
958                           {1},
959                           {1, 2, 3},
960                       });
961   testRepeat<char16_t>(iface, &ITest::RepeatCharArray,
962                        {
963                            {},
964                            {L'@'},
965                            {L'@', L'!', L'A'},
966                        });
967   testRepeat<int32_t>(iface, &ITest::RepeatIntArray,
968                       {
969                           {},
970                           {1},
971                           {1, 2, 3},
972                       });
973   testRepeat<int64_t>(iface, &ITest::RepeatLongArray,
974                       {
975                           {},
976                           {1},
977                           {1, 2, 3},
978                       });
979   testRepeat<float>(iface, &ITest::RepeatFloatArray,
980                     {
981                         {},
982                         {1.0f},
983                         {1.0f, 2.0f, 3.0f},
984                     });
985   testRepeat<double>(iface, &ITest::RepeatDoubleArray,
986                      {
987                          {},
988                          {1.0},
989                          {1.0, 2.0, 3.0},
990                      });
991   testRepeat<ByteEnum>(iface, &ITest::RepeatByteEnumArray,
992                        {
993                            {},
994                            {ByteEnum::FOO},
995                            {ByteEnum::FOO, ByteEnum::BAR},
996                        });
997   testRepeat<IntEnum>(iface, &ITest::RepeatIntEnumArray,
998                       {
999                           {},
1000                           {IntEnum::FOO},
1001                           {IntEnum::FOO, IntEnum::BAR},
1002                       });
1003   testRepeat<LongEnum>(iface, &ITest::RepeatLongEnumArray,
1004                        {
1005                            {},
1006                            {LongEnum::FOO},
1007                            {LongEnum::FOO, LongEnum::BAR},
1008                        });
1009   testRepeat<std::string>(iface, &ITest::RepeatStringArray,
1010                           {
1011                               {},
1012                               {"asdf"},
1013                               {"", "aoeu", "lol", "brb"},
1014                           });
1015   testRepeat<RegularPolygon>(iface, &ITest::RepeatRegularPolygonArray,
1016                              {
1017                                  {},
1018                                  {{"hexagon", 6, 2.0f}},
1019                                  {{"hexagon", 6, 2.0f}, {"square", 4, 7.0f}, {"pentagon", 5, 4.2f}},
1020                              });
1021   std::shared_ptr<IEmpty> my_empty = SharedRefBase::make<MyEmpty>();
1022   testRepeat<SpAIBinder>(iface, &ITest::RepeatBinderArray,
1023                          {
1024                              {},
1025                              {iface->asBinder()},
1026                              {iface->asBinder(), my_empty->asBinder()},
1027                          });
1028 
1029   std::shared_ptr<IEmpty> your_empty = SharedRefBase::make<YourEmpty>();
1030   testRepeat<std::shared_ptr<IEmpty>>(iface, &ITest::RepeatInterfaceArray,
1031                                       {
1032                                           {},
1033                                           {my_empty},
1034                                           {my_empty, your_empty},
1035                                           // Legacy behavior: allow null for non-nullable interface
1036                                           {my_empty, your_empty, nullptr},
1037                                       });
1038 }
1039 
TEST_P(NdkBinderTest_Aidl,Lists)1040 TEST_P(NdkBinderTest_Aidl, Lists) {
1041   testRepeat2List<std::string>(iface, &ITest::Repeat2StringList,
1042                                {
1043                                    {},
1044                                    {"asdf"},
1045                                    {"", "aoeu", "lol", "brb"},
1046                                });
1047   testRepeat2List<RegularPolygon>(
1048       iface, &ITest::Repeat2RegularPolygonList,
1049       {
1050           {},
1051           {{"hexagon", 6, 2.0f}},
1052           {{"hexagon", 6, 2.0f}, {"square", 4, 7.0f}, {"pentagon", 5, 4.2f}},
1053       });
1054 }
1055 
1056 template <typename T>
1057 using RepeatNullableMethod = ScopedAStatus (ITest::*)(
1058     const std::optional<std::vector<std::optional<T>>>&,
1059     std::optional<std::vector<std::optional<T>>>*,
1060     std::optional<std::vector<std::optional<T>>>*);
1061 
1062 template <typename T>
testRepeat(const std::shared_ptr<ITest> & i,RepeatNullableMethod<T> repeatMethod,std::vector<std::optional<std::vector<std::optional<T>>>> tests)1063 void testRepeat(
1064     const std::shared_ptr<ITest>& i, RepeatNullableMethod<T> repeatMethod,
1065     std::vector<std::optional<std::vector<std::optional<T>>>> tests) {
1066   for (const auto& input : tests) {
1067     std::optional<std::vector<std::optional<T>>> out1;
1068     if (input) {
1069       out1 = std::vector<std::optional<T>>{};
1070       out1->resize(input->size());
1071     }
1072     std::optional<std::vector<std::optional<T>>> out2;
1073 
1074     ASSERT_OK((i.get()->*repeatMethod)(input, &out1, &out2))
1075         << (input ? input->size() : -1);
1076     EXPECT_EQ(input, out1);
1077     EXPECT_EQ(input, out2);
1078   }
1079 }
1080 
1081 template <typename T>
1082 using SingleRepeatNullableMethod = ScopedAStatus (ITest::*)(
1083     const std::optional<std::vector<T>>&, std::optional<std::vector<T>>*);
1084 
1085 template <typename T>
testRepeat(const std::shared_ptr<ITest> & i,SingleRepeatNullableMethod<T> repeatMethod,std::vector<std::optional<std::vector<T>>> tests)1086 void testRepeat(const std::shared_ptr<ITest>& i,
1087                 SingleRepeatNullableMethod<T> repeatMethod,
1088                 std::vector<std::optional<std::vector<T>>> tests) {
1089   for (const auto& input : tests) {
1090     std::optional<std::vector<T>> ret;
1091     ASSERT_OK((i.get()->*repeatMethod)(input, &ret))
1092         << (input ? input->size() : -1);
1093     EXPECT_EQ(input, ret);
1094   }
1095 }
1096 
TEST_P(NdkBinderTest_Aidl,NullableArrays)1097 TEST_P(NdkBinderTest_Aidl, NullableArrays) {
1098   testRepeat<bool>(iface, &ITest::RepeatNullableBooleanArray,
1099                    {
1100                        std::nullopt,
1101                        {{}},
1102                        {{true}},
1103                        {{false, true, false}},
1104                    });
1105   testRepeat<uint8_t>(iface, &ITest::RepeatNullableByteArray,
1106                       {
1107                           std::nullopt,
1108                           {{}},
1109                           {{1}},
1110                           {{1, 2, 3}},
1111                       });
1112   testRepeat<char16_t>(iface, &ITest::RepeatNullableCharArray,
1113                        {
1114                            std::nullopt,
1115                            {{}},
1116                            {{L'@'}},
1117                            {{L'@', L'!', L'A'}},
1118                        });
1119   testRepeat<int32_t>(iface, &ITest::RepeatNullableIntArray,
1120                       {
1121                           std::nullopt,
1122                           {{}},
1123                           {{1}},
1124                           {{1, 2, 3}},
1125                       });
1126   testRepeat<int64_t>(iface, &ITest::RepeatNullableLongArray,
1127                       {
1128                           std::nullopt,
1129                           {{}},
1130                           {{1}},
1131                           {{1, 2, 3}},
1132                       });
1133   testRepeat<float>(iface, &ITest::RepeatNullableFloatArray,
1134                     {
1135                         std::nullopt,
1136                         {{}},
1137                         {{1.0f}},
1138                         {{1.0f, 2.0f, 3.0f}},
1139                     });
1140   testRepeat<double>(iface, &ITest::RepeatNullableDoubleArray,
1141                      {
1142                          std::nullopt,
1143                          {{}},
1144                          {{1.0}},
1145                          {{1.0, 2.0, 3.0}},
1146                      });
1147   testRepeat<ByteEnum>(iface, &ITest::RepeatNullableByteEnumArray,
1148                        {
1149                            std::nullopt,
1150                            {{}},
1151                            {{ByteEnum::FOO}},
1152                            {{ByteEnum::FOO, ByteEnum::BAR}},
1153                        });
1154   testRepeat<IntEnum>(iface, &ITest::RepeatNullableIntEnumArray,
1155                       {
1156                           std::nullopt,
1157                           {{}},
1158                           {{IntEnum::FOO}},
1159                           {{IntEnum::FOO, IntEnum::BAR}},
1160                       });
1161   testRepeat<LongEnum>(iface, &ITest::RepeatNullableLongEnumArray,
1162                        {
1163                            std::nullopt,
1164                            {{}},
1165                            {{LongEnum::FOO}},
1166                            {{LongEnum::FOO, LongEnum::BAR}},
1167                        });
1168   testRepeat<std::optional<std::string>>(
1169       iface, &ITest::RepeatNullableStringArray,
1170       {
1171           std::nullopt,
1172           {{}},
1173           {{"asdf"}},
1174           {{std::nullopt}},
1175           {{"aoeu", "lol", "brb"}},
1176           {{"", "aoeu", std::nullopt, "brb"}},
1177       });
1178   testRepeat<std::string>(iface, &ITest::DoubleRepeatNullableStringArray,
1179                           {
1180                               {{}},
1181                               {{"asdf"}},
1182                               {{std::nullopt}},
1183                               {{"aoeu", "lol", "brb"}},
1184                               {{"", "aoeu", std::nullopt, "brb"}},
1185                           });
1186   std::shared_ptr<IEmpty> my_empty = SharedRefBase::make<MyEmpty>();
1187   testRepeat<SpAIBinder>(iface, &ITest::RepeatNullableBinderArray,
1188                          {
1189                              std::nullopt,
1190                              {{}},
1191                              {{iface->asBinder()}},
1192                              {{nullptr}},
1193                              {{iface->asBinder(), my_empty->asBinder()}},
1194                              {{iface->asBinder(), nullptr, my_empty->asBinder()}},
1195                          });
1196   std::shared_ptr<IEmpty> your_empty = SharedRefBase::make<YourEmpty>();
1197   testRepeat<std::shared_ptr<IEmpty>>(iface, &ITest::RepeatNullableInterfaceArray,
1198                                       {
1199                                           std::nullopt,
1200                                           {{}},
1201                                           {{my_empty}},
1202                                           {{nullptr}},
1203                                           {{my_empty, your_empty}},
1204                                           {{my_empty, nullptr, your_empty}},
1205                                       });
1206 }
1207 
1208 class DefaultImpl : public ::aidl::test_package::ICompatTestDefault {
1209  public:
NewMethodThatReturns10(int32_t * _aidl_return)1210   ::ndk::ScopedAStatus NewMethodThatReturns10(int32_t* _aidl_return) override {
1211     *_aidl_return = 100;  // default impl returns different value
1212     return ::ndk::ScopedAStatus(AStatus_newOk());
1213   }
1214 };
1215 
TEST_P(NdkBinderTest_Aidl,NewMethod)1216 TEST_P(NdkBinderTest_Aidl, NewMethod) {
1217   std::shared_ptr<ICompatTest> default_impl = SharedRefBase::make<DefaultImpl>();
1218   ::aidl::test_package::ICompatTest::setDefaultImpl(default_impl);
1219 
1220   auto compat_test = getCompatTest(iface);
1221   int32_t res;
1222   EXPECT_OK(compat_test->NewMethodThatReturns10(&res));
1223   if (GetParam().shouldBeOld) {
1224     // Remote was built with version 1 interface which does not have
1225     // "NewMethodThatReturns10". In this case the default method
1226     // which returns 100 is called.
1227     EXPECT_EQ(100, res);
1228   } else {
1229     // Remote is built with the current version of the interface.
1230     // The method returns 10.
1231     EXPECT_EQ(10, res);
1232   }
1233 }
1234 
TEST_P(NdkBinderTest_Aidl,RepeatStringNullableLater)1235 TEST_P(NdkBinderTest_Aidl, RepeatStringNullableLater) {
1236   std::optional<std::string> res;
1237 
1238   std::string name;
1239   EXPECT_OK(iface->GetName(&name));
1240 
1241   // Java considers every type to be nullable, but this is okay, since it will
1242   // pass back NullPointerException to the client if it does not handle a null
1243   // type, similar to how a C++ server would refuse to unparcel a null
1244   // non-nullable type. Of course, this is not ideal, but the problem runs very
1245   // deep.
1246   const bool supports_nullable = !GetParam().shouldBeOld || name == "Java";
1247   auto compat_test = getCompatTest(iface);
1248   if (supports_nullable) {
1249     EXPECT_OK(compat_test->RepeatStringNullableLater(std::nullopt, &res));
1250     EXPECT_EQ(std::nullopt, res);
1251   } else {
1252     ndk::ScopedAStatus status = compat_test->RepeatStringNullableLater(std::nullopt, &res);
1253     ASSERT_EQ(STATUS_UNEXPECTED_NULL, AStatus_getStatus(status.get()));
1254   }
1255 
1256   EXPECT_OK(compat_test->RepeatStringNullableLater("", &res));
1257   EXPECT_EQ("", res);
1258 
1259   EXPECT_OK(compat_test->RepeatStringNullableLater("a", &res));
1260   EXPECT_EQ("a", res);
1261 
1262   EXPECT_OK(compat_test->RepeatStringNullableLater("say what?", &res));
1263   EXPECT_EQ("say what?", res);
1264 }
1265 
TEST_P(NdkBinderTest_Aidl,GetInterfaceVersion)1266 TEST_P(NdkBinderTest_Aidl, GetInterfaceVersion) {
1267   int32_t res;
1268   auto compat_test = getCompatTest(iface);
1269   EXPECT_OK(compat_test->getInterfaceVersion(&res));
1270   if (GetParam().shouldBeOld) {
1271     EXPECT_EQ(1, res);
1272   } else {
1273     // 3 is the not-yet-frozen version. It may be V2 or V3 depending
1274     // on the release config. 'next' will be V2.
1275     EXPECT_TRUE(res > 1);
1276   }
1277 }
1278 
TEST_P(NdkBinderTest_Aidl,GetInterfaceHash)1279 TEST_P(NdkBinderTest_Aidl, GetInterfaceHash) {
1280   std::string res;
1281   auto compat_test = getCompatTest(iface);
1282   EXPECT_OK(compat_test->getInterfaceHash(&res));
1283   if (GetParam().shouldBeOld) {
1284     // aidl_api/libbinder_ndk_test_interface/1/.hash
1285     EXPECT_EQ("b663b681b3e0d66f9b5428c2f23365031b7d4ba0", res);
1286   } else {
1287     int32_t version = 0;
1288     EXPECT_OK(compat_test->getInterfaceVersion(&version));
1289     if (version == 2) {
1290       // aidl_api/libbinder_ndk_test_interface/2/.hash
1291       EXPECT_EQ("2740afaf3b5a0e739c44165c49633a0af87369f2", res);
1292     } else {
1293       EXPECT_EQ("notfrozen", res);
1294     }
1295   }
1296 }
1297 
TEST_P(NdkBinderTest_Aidl,LegacyBinder)1298 TEST_P(NdkBinderTest_Aidl, LegacyBinder) {
1299   SpAIBinder binder;
1300   iface->getLegacyBinderTest(&binder);
1301   ASSERT_NE(nullptr, binder.get());
1302 
1303   ASSERT_TRUE(AIBinder_associateClass(binder.get(), kLegacyBinderClass));
1304 
1305   constexpr int32_t kVal = 42;
1306 
1307   ::ndk::ScopedAParcel in;
1308   ::ndk::ScopedAParcel out;
1309   ASSERT_EQ(STATUS_OK, AIBinder_prepareTransaction(binder.get(), in.getR()));
1310   ASSERT_EQ(STATUS_OK, AParcel_writeInt32(in.get(), kVal));
1311   ASSERT_EQ(STATUS_OK,
1312             AIBinder_transact(binder.get(), FIRST_CALL_TRANSACTION, in.getR(), out.getR(), 0));
1313 
1314   int32_t output;
1315   ASSERT_EQ(STATUS_OK, AParcel_readInt32(out.get(), &output));
1316   EXPECT_EQ(kVal, output);
1317 }
1318 
TEST_P(NdkBinderTest_Aidl,ParcelableHolderTest)1319 TEST_P(NdkBinderTest_Aidl, ParcelableHolderTest) {
1320   ExtendableParcelable ep;
1321   MyExt myext1;
1322   myext1.a = 42;
1323   myext1.b = "mystr";
1324   ep.ext.setParcelable(myext1);
1325   std::optional<MyExt> myext2;
1326   ep.ext.getParcelable(&myext2);
1327   EXPECT_TRUE(myext2);
1328   EXPECT_EQ(42, myext2->a);
1329   EXPECT_EQ("mystr", myext2->b);
1330 
1331   AParcel* parcel = AParcel_create();
1332   ep.writeToParcel(parcel);
1333   AParcel_setDataPosition(parcel, 0);
1334   ExtendableParcelable ep2;
1335   ep2.readFromParcel(parcel);
1336   std::optional<MyExt> myext3;
1337   ep2.ext.getParcelable(&myext3);
1338   EXPECT_TRUE(myext3);
1339   EXPECT_EQ(42, myext3->a);
1340   EXPECT_EQ("mystr", myext3->b);
1341   AParcel_delete(parcel);
1342 }
1343 
TEST_P(NdkBinderTest_Aidl,ParcelableHolderCopyTest)1344 TEST_P(NdkBinderTest_Aidl, ParcelableHolderCopyTest) {
1345   ndk::AParcelableHolder ph1{ndk::STABILITY_LOCAL};
1346   MyExt myext1;
1347   myext1.a = 42;
1348   myext1.b = "mystr";
1349   ph1.setParcelable(myext1);
1350 
1351   ndk::AParcelableHolder ph2{ph1};
1352   std::optional<MyExt> myext2;
1353   ph2.getParcelable(&myext2);
1354   EXPECT_TRUE(myext2);
1355   EXPECT_EQ(42, myext2->a);
1356   EXPECT_EQ("mystr", myext2->b);
1357 
1358   std::optional<MyExt> myext3;
1359   ph1.getParcelable(&myext3);
1360   EXPECT_TRUE(myext3);
1361   EXPECT_EQ(42, myext3->a);
1362   EXPECT_EQ("mystr", myext3->b);
1363 }
1364 
TEST_P(NdkBinderTest_Aidl,ParcelableHolderAssignmentWithLocalStabilityTest)1365 TEST_P(NdkBinderTest_Aidl, ParcelableHolderAssignmentWithLocalStabilityTest) {
1366   ndk::AParcelableHolder ph1{ndk::STABILITY_LOCAL};
1367   MyExt myext1;
1368   myext1.a = 42;
1369   myext1.b = "mystr";
1370   EXPECT_EQ(STATUS_OK, ph1.setParcelable(myext1));
1371 
1372   ndk::AParcelableHolder ph2{ndk::STABILITY_LOCAL};
1373   MyExt myext2;
1374   myext2.a = 0xdb;
1375   myext2.b = "magic";
1376   EXPECT_EQ(STATUS_OK, ph2.setParcelable(myext2));
1377 
1378   ph2 = ph1;
1379   std::optional<MyExt> myext3;
1380   EXPECT_EQ(STATUS_OK, ph2.getParcelable(&myext3));
1381   EXPECT_NE(std::nullopt, myext3);
1382   EXPECT_TRUE(myext3 != myext2);
1383   EXPECT_TRUE(myext3 == myext1);
1384 }
1385 
TEST_P(NdkBinderTest_Aidl,ParcelableHolderAssignmentWithVintfStabilityTest)1386 TEST_P(NdkBinderTest_Aidl, ParcelableHolderAssignmentWithVintfStabilityTest) {
1387   ndk::AParcelableHolder ph1{ndk::STABILITY_VINTF};
1388   MyExt myext1;
1389   myext1.a = 42;
1390   myext1.b = "mystr";
1391   // STABILITY_VINTF Pracelable can't set with STABILITY_LOCAL.
1392   EXPECT_EQ(STATUS_BAD_VALUE, ph1.setParcelable(myext1));
1393 
1394   ndk::AParcelableHolder ph2{ndk::STABILITY_VINTF};
1395   MyExt myext2;
1396   myext2.a = 0xbd;
1397   myext2.b = "cigam";
1398   EXPECT_EQ(STATUS_BAD_VALUE, ph2.setParcelable(myext2));
1399 
1400   ph2 = ph1;
1401   std::optional<MyExt> myext3;
1402   EXPECT_EQ(STATUS_OK, ph2.getParcelable(&myext3));
1403   EXPECT_EQ(std::nullopt, myext3);
1404 }
1405 
TEST_P(NdkBinderTest_Aidl,ParcelableHolderCommunicationTest)1406 TEST_P(NdkBinderTest_Aidl, ParcelableHolderCommunicationTest) {
1407   ExtendableParcelable ep;
1408   ep.c = 42L;
1409   MyExt myext1;
1410   myext1.a = 42;
1411   myext1.b = "mystr";
1412   ep.ext.setParcelable(myext1);
1413 
1414   ExtendableParcelable ep2;
1415   EXPECT_OK(iface->RepeatExtendableParcelable(ep, &ep2));
1416   std::optional<MyExt> myext2;
1417   ep2.ext.getParcelable(&myext2);
1418   EXPECT_EQ(42L, ep2.c);
1419   EXPECT_TRUE(myext2);
1420   EXPECT_EQ(42, myext2->a);
1421   EXPECT_EQ("mystr", myext2->b);
1422 }
1423 
TEST_P(NdkBinderTest_Aidl,EmptyParcelableHolderCommunicationTest)1424 TEST_P(NdkBinderTest_Aidl, EmptyParcelableHolderCommunicationTest) {
1425   ExtendableParcelable ep;
1426   ExtendableParcelable ep2;
1427   ep.c = 42L;
1428   EXPECT_OK(iface->RepeatExtendableParcelableWithoutExtension(ep, &ep2));
1429 
1430   EXPECT_EQ(42L, ep2.c);
1431 }
1432 
getProxyLocalService()1433 std::shared_ptr<ITest> getProxyLocalService() {
1434   std::shared_ptr<MyTest> test = SharedRefBase::make<MyTest>();
1435   SpAIBinder binder = test->asBinder();
1436 
1437   // adding an arbitrary class as the extension
1438   std::shared_ptr<MyTest> ext = SharedRefBase::make<MyTest>();
1439   SpAIBinder extBinder = ext->asBinder();
1440 
1441   binder_status_t ret = AIBinder_setExtension(binder.get(), extBinder.get());
1442   if (ret != STATUS_OK) {
1443     __android_log_write(ANDROID_LOG_ERROR, LOG_TAG, "Could not set local extension");
1444   }
1445 
1446   // BpTest -> AIBinder -> test
1447   //
1448   // Warning: for testing purposes only. This parcels things within the same process for testing
1449   // purposes. In normal usage, this should just return SharedRefBase::make<MyTest> directly.
1450   return SharedRefBase::make<BpTest>(binder);
1451 }
1452 
getNdkBinderTestJavaService(const std::string & method)1453 std::shared_ptr<ITest> getNdkBinderTestJavaService(const std::string& method) {
1454   JNIEnv* env = GetEnv();
1455   if (env == nullptr) {
1456     __android_log_write(ANDROID_LOG_ERROR, LOG_TAG, "No environment");
1457     return nullptr;
1458   }
1459 
1460   jobject object = callStaticJavaMethodForObject(env, "android/binder/cts/NdkBinderTest", method,
1461                                                  "()Landroid/os/IBinder;");
1462 
1463   SpAIBinder binder = SpAIBinder(AIBinder_fromJavaBinder(env, object));
1464 
1465   return ITest::fromBinder(binder);
1466 }
1467 
1468 INSTANTIATE_TEST_CASE_P(LocalProxyToNative, NdkBinderTest_Aidl,
1469                         ::testing::Values(Params{getProxyLocalService(), false /*shouldBeRemote*/,
1470                                                  true /*shouldBeWrapped*/, "CPP",
1471                                                  false /*shouldBeOld*/}));
1472 INSTANTIATE_TEST_CASE_P(LocalNativeFromJava, NdkBinderTest_Aidl,
1473                         ::testing::Values(Params{
1474                             getNdkBinderTestJavaService("getLocalNativeService"),
1475                             false /*shouldBeRemote*/, false /*shouldBeWrapped*/, "CPP",
1476                             false /*shouldBeOld*/}));
1477 INSTANTIATE_TEST_CASE_P(LocalJava, NdkBinderTest_Aidl,
1478                         ::testing::Values(Params{getNdkBinderTestJavaService("getLocalJavaService"),
1479                                                  false /*shouldBeRemote*/, true /*shouldBeWrapped*/,
1480                                                  "JAVA", false /*shouldBeOld*/}));
1481 INSTANTIATE_TEST_CASE_P(RemoteNative, NdkBinderTest_Aidl,
1482                         ::testing::Values(Params{
1483                             getNdkBinderTestJavaService("getRemoteNativeService"),
1484                             true /*shouldBeRemote*/, true /*shouldBeWrapped*/, "CPP",
1485                             false /*shouldBeOld*/}));
1486 INSTANTIATE_TEST_CASE_P(RemoteJava, NdkBinderTest_Aidl,
1487                         ::testing::Values(Params{
1488                             getNdkBinderTestJavaService("getRemoteJavaService"),
1489                             true /*shouldBeRemote*/, true /*shouldBeWrapped*/, "JAVA",
1490                             false /*shouldBeOld*/}));
1491 
1492 INSTANTIATE_TEST_CASE_P(RemoteNativeOld, NdkBinderTest_Aidl,
1493                         ::testing::Values(Params{
1494                             getNdkBinderTestJavaService("getRemoteOldNativeService"),
1495                             true /*shouldBeRemote*/, true /*shouldBeWrapped*/, "CPP",
1496                             true /*shouldBeOld*/}));
1497 
1498 class SkippedIdsService : public ::aidl::test_package::BnSkippedIds {
1499  public:
SkippedIdsService()1500   SkippedIdsService() {}
1501   virtual ~SkippedIdsService() = default;
1502 
getBoolean(bool * _aidl_return)1503   ScopedAStatus getBoolean(bool* _aidl_return) override {
1504     *_aidl_return = true;
1505     return ScopedAStatus::ok();
1506   }
1507 
getInt(int32_t * _aidl_return)1508   ScopedAStatus getInt(int32_t* _aidl_return) override {
1509     *_aidl_return = 12;
1510     return ScopedAStatus::ok();
1511   }
1512 
getFloat(float * _aidl_return)1513   ScopedAStatus getFloat(float* _aidl_return) override {
1514     *_aidl_return = 1.1f;
1515     return ScopedAStatus::ok();
1516   }
1517 
getDouble(double * _aidl_return)1518   ScopedAStatus getDouble(double* _aidl_return) override {
1519     *_aidl_return = 1.0003;
1520     return ScopedAStatus::ok();
1521   }
1522 
getChar(char16_t * _aidl_return)1523   ScopedAStatus getChar(char16_t* _aidl_return) override {
1524     *_aidl_return = 't';
1525     return ScopedAStatus::ok();
1526   }
1527 };
1528 
TEST_P(NdkBinderTest_Aidl,ServiceWithSkippedIds_traceByName)1529 TEST_P(NdkBinderTest_Aidl, ServiceWithSkippedIds_traceByName) {
1530   std::shared_ptr<SkippedIdsService> service = SharedRefBase::make<SkippedIdsService>();
1531   ASSERT_NE(nullptr, service);
1532 
1533   bool boolValue;
1534   auto status = service->getBoolean(&boolValue);
1535   ASSERT_TRUE(status.isOk()) << status;
1536   EXPECT_EQ(true, boolValue);
1537 
1538   int intValue;
1539   status = service->getInt(&intValue);
1540   ASSERT_TRUE(status.isOk()) << status;
1541   EXPECT_EQ(12, intValue);
1542 
1543   float floatValue;
1544   status = service->getFloat(&floatValue);
1545   ASSERT_TRUE(status.isOk()) << status;
1546   EXPECT_EQ(1.1f, floatValue);
1547 }
1548 
TEST_F(NdkBinderTest_Aidl,ServiceWithSkippedIds_traceMethodsWithCodes)1549 TEST_F(NdkBinderTest_Aidl, ServiceWithSkippedIds_traceMethodsWithCodes) {
1550   std::shared_ptr<SkippedIdsService> service = SharedRefBase::make<SkippedIdsService>();
1551   ASSERT_NE(nullptr, service);
1552   double doubleValue;
1553   auto status = service->getDouble(&doubleValue);
1554   ASSERT_TRUE(status.isOk()) << status;
1555   EXPECT_EQ(1.0003, doubleValue);
1556 }
1557