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