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