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/BpTest.h>
20 #include <aidl/test_package/Foo.h>
21 #include <aidl/test_package/RegularPolygon.h>
22 #include <android/binder_ibinder_jni.h>
23 #include <gtest/gtest.h>
24
25 #include "itest_impl.h"
26 #include "utilities.h"
27
28 #include <stdio.h>
29 #include <sys/socket.h>
30 #include <sys/types.h>
31
32 using ::aidl::test_package::Bar;
33 using ::aidl::test_package::BpTest;
34 using ::aidl::test_package::Foo;
35 using ::aidl::test_package::ITest;
36 using ::aidl::test_package::RegularPolygon;
37 using ::ndk::ScopedAStatus;
38 using ::ndk::ScopedFileDescriptor;
39 using ::ndk::SharedRefBase;
40 using ::ndk::SpAIBinder;
41
42 // AIDL tests which are independent of the service
43 class NdkBinderTest_AidlLocal : public NdkBinderTest {};
44
TEST_F(NdkBinderTest_AidlLocal,FromBinder)45 TEST_F(NdkBinderTest_AidlLocal, FromBinder) {
46 std::shared_ptr<MyTest> test = SharedRefBase::make<MyTest>();
47 SpAIBinder binder = test->asBinder();
48 EXPECT_EQ(test, ITest::fromBinder(binder));
49
50 EXPECT_FALSE(test->isRemote());
51 }
52
53 struct Params {
54 std::shared_ptr<ITest> iface;
55 bool shouldBeRemote;
56 bool shouldBeWrapped;
57 std::string expectedName;
58 bool shouldBeOld;
59 };
60
61 #define iface GetParam().iface
62 #define shouldBeRemote GetParam().shouldBeRemote
63 #define shouldBeWrapped GetParam().shouldBeWrapped
64
65 // AIDL tests which run on each type of service (local C++, local Java, remote C++, remote Java,
66 // etc..)
67 class NdkBinderTest_Aidl : public NdkBinderTest,
68 public ::testing::WithParamInterface<Params> {};
69
TEST_P(NdkBinderTest_Aidl,GotTest)70 TEST_P(NdkBinderTest_Aidl, GotTest) { ASSERT_NE(nullptr, iface); }
71
TEST_P(NdkBinderTest_Aidl,SanityCheckSource)72 TEST_P(NdkBinderTest_Aidl, SanityCheckSource) {
73 std::string name;
74 ASSERT_OK(iface->GetName(&name));
75 EXPECT_EQ(GetParam().expectedName, name);
76 }
77
TEST_P(NdkBinderTest_Aidl,Remoteness)78 TEST_P(NdkBinderTest_Aidl, Remoteness) {
79 ASSERT_EQ(shouldBeRemote, iface->isRemote());
80 }
81
TEST_P(NdkBinderTest_Aidl,UseBinder)82 TEST_P(NdkBinderTest_Aidl, UseBinder) {
83 ASSERT_EQ(STATUS_OK, AIBinder_ping(iface->asBinder().get()));
84 }
85
ReadFdToString(int fd,std::string * content)86 bool ReadFdToString(int fd, std::string* content) {
87 char buf[64];
88 ssize_t n;
89 while ((n = TEMP_FAILURE_RETRY(read(fd, &buf[0], sizeof(buf)))) > 0) {
90 content->append(buf, n);
91 }
92 return (n == 0) ? true : false;
93 }
94
dumpToString(std::shared_ptr<ITest> itest,std::vector<const char * > args)95 std::string dumpToString(std::shared_ptr<ITest> itest, std::vector<const char*> args) {
96 int fd[2] = {-1, -1};
97 EXPECT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, fd));
98
99 EXPECT_OK(itest->dump(fd[0], args.data(), args.size()));
100 close(fd[0]);
101
102 std::string ret;
103 EXPECT_TRUE(ReadFdToString(fd[1], &ret));
104
105 close(fd[1]);
106 return ret;
107 }
108
TEST_P(NdkBinderTest_Aidl,UseDump)109 TEST_P(NdkBinderTest_Aidl, UseDump) {
110 std::string name;
111 EXPECT_OK(iface->GetName(&name));
112 if (name == "JAVA" && !iface->isRemote()) {
113 // TODO(b/127361166): GTEST_SKIP is considered a failure, would prefer to use that here
114 // TODO(b/127339049): JavaBBinder doesn't implement dump
115 return;
116 }
117
118 EXPECT_EQ("", dumpToString(iface, {}));
119 EXPECT_EQ("", dumpToString(iface, {"", ""}));
120 EXPECT_EQ("Hello World!", dumpToString(iface, {"Hello ", "World!"}));
121 EXPECT_EQ("ABC", dumpToString(iface, {"A", "B", "C"}));
122 }
123
TEST_P(NdkBinderTest_Aidl,Trivial)124 TEST_P(NdkBinderTest_Aidl, Trivial) {
125 ASSERT_OK(iface->TestVoidReturn());
126
127 if (shouldBeWrapped) {
128 ASSERT_OK(iface->TestOneway());
129 } else {
130 ASSERT_EQ(STATUS_UNKNOWN_ERROR, AStatus_getStatus(iface->TestOneway().get()));
131 }
132 }
133
TEST_P(NdkBinderTest_Aidl,CallingInfo)134 TEST_P(NdkBinderTest_Aidl, CallingInfo) {
135 EXPECT_OK(iface->CacheCallingInfoFromOneway());
136 int32_t res;
137
138 EXPECT_OK(iface->GiveMeMyCallingPid(&res));
139 EXPECT_EQ(getpid(), res);
140
141 EXPECT_OK(iface->GiveMeMyCallingUid(&res));
142 EXPECT_EQ(getuid(), res);
143
144 EXPECT_OK(iface->GiveMeMyCallingPidFromOneway(&res));
145 if (shouldBeRemote) {
146 // PID is hidden from oneway calls
147 EXPECT_EQ(0, res);
148 } else {
149 EXPECT_EQ(getpid(), res);
150 }
151
152 EXPECT_OK(iface->GiveMeMyCallingUidFromOneway(&res));
153 EXPECT_EQ(getuid(), res);
154 }
155
TEST_P(NdkBinderTest_Aidl,Constants)156 TEST_P(NdkBinderTest_Aidl, Constants) {
157 ASSERT_EQ(0, ITest::kZero);
158 ASSERT_EQ(1, ITest::kOne);
159 ASSERT_EQ(0xffffffff, ITest::kOnes);
160 ASSERT_EQ(std::string(""), ITest::kEmpty);
161 ASSERT_EQ(std::string("foo"), ITest::kFoo);
162 }
163
TEST_P(NdkBinderTest_Aidl,RepeatPrimitives)164 TEST_P(NdkBinderTest_Aidl, RepeatPrimitives) {
165 {
166 int32_t out;
167 ASSERT_OK(iface->RepeatInt(3, &out));
168 EXPECT_EQ(3, out);
169 }
170
171 {
172 int64_t out;
173 ASSERT_OK(iface->RepeatLong(3, &out));
174 EXPECT_EQ(3, out);
175 }
176
177 {
178 float out;
179 ASSERT_OK(iface->RepeatFloat(2.0f, &out));
180 EXPECT_EQ(2.0f, out);
181 }
182
183 {
184 double out;
185 ASSERT_OK(iface->RepeatDouble(3.0, &out));
186 EXPECT_EQ(3.0, out);
187 }
188
189 {
190 bool out;
191 ASSERT_OK(iface->RepeatBoolean(true, &out));
192 EXPECT_EQ(true, out);
193 }
194
195 {
196 char16_t out;
197 ASSERT_OK(iface->RepeatChar(L'@', &out));
198 EXPECT_EQ(L'@', out);
199 }
200
201 {
202 int8_t out;
203 ASSERT_OK(iface->RepeatByte(3, &out));
204 EXPECT_EQ(3, out);
205 }
206 }
207
TEST_P(NdkBinderTest_Aidl,RepeatBinder)208 TEST_P(NdkBinderTest_Aidl, RepeatBinder) {
209 SpAIBinder binder = iface->asBinder();
210 SpAIBinder ret;
211
212 ASSERT_OK(iface->RepeatBinder(binder, &ret));
213 EXPECT_EQ(binder.get(), ret.get());
214
215 ASSERT_OK(iface->RepeatNullableBinder(binder, &ret));
216 EXPECT_EQ(binder.get(), ret.get());
217
218 ASSERT_OK(iface->RepeatNullableBinder(nullptr, &ret));
219 EXPECT_EQ(nullptr, ret.get());
220 }
221
TEST_P(NdkBinderTest_Aidl,RepeatInterface)222 TEST_P(NdkBinderTest_Aidl, RepeatInterface) {
223 class MyEmpty : public ::aidl::test_package::BnEmpty {};
224
225 std::shared_ptr<IEmpty> empty = SharedRefBase::make<MyEmpty>();
226
227 std::shared_ptr<IEmpty> ret;
228 ASSERT_OK(iface->RepeatInterface(empty, &ret));
229 EXPECT_EQ(empty.get(), ret.get());
230
231 ASSERT_OK(iface->RepeatNullableInterface(empty, &ret));
232 EXPECT_EQ(empty.get(), ret.get());
233
234 ASSERT_OK(iface->RepeatNullableInterface(nullptr, &ret));
235 EXPECT_EQ(nullptr, ret.get());
236 }
237
checkInOut(const ScopedFileDescriptor & inFd,const ScopedFileDescriptor & outFd)238 static void checkInOut(const ScopedFileDescriptor& inFd,
239 const ScopedFileDescriptor& outFd) {
240 static const std::string kContent = "asdf";
241
242 ASSERT_EQ(static_cast<int>(kContent.size()),
243 write(inFd.get(), kContent.data(), kContent.size()));
244
245 std::string out;
246 out.resize(kContent.size());
247 ASSERT_EQ(static_cast<int>(kContent.size()),
248 read(outFd.get(), &out[0], kContent.size()));
249
250 EXPECT_EQ(kContent, out);
251 }
252
checkFdRepeat(const std::shared_ptr<ITest> & test,ScopedAStatus (ITest::* repeatFd)(const ScopedFileDescriptor &,ScopedFileDescriptor *))253 static void checkFdRepeat(
254 const std::shared_ptr<ITest>& test,
255 ScopedAStatus (ITest::*repeatFd)(const ScopedFileDescriptor&,
256 ScopedFileDescriptor*)) {
257 int fds[2];
258
259 while (pipe(fds) == -1 && errno == EAGAIN)
260 ;
261
262 ScopedFileDescriptor readFd(fds[0]);
263 ScopedFileDescriptor writeFd(fds[1]);
264
265 ScopedFileDescriptor readOutFd;
266 ASSERT_OK((test.get()->*repeatFd)(readFd, &readOutFd));
267
268 checkInOut(writeFd, readOutFd);
269 }
270
TEST_P(NdkBinderTest_Aidl,RepeatFd)271 TEST_P(NdkBinderTest_Aidl, RepeatFd) { checkFdRepeat(iface, &ITest::RepeatFd); }
272
TEST_P(NdkBinderTest_Aidl,RepeatNullableFd)273 TEST_P(NdkBinderTest_Aidl, RepeatNullableFd) {
274 checkFdRepeat(iface, &ITest::RepeatNullableFd);
275
276 ScopedFileDescriptor in;
277 EXPECT_EQ(-1, in.get());
278
279 ScopedFileDescriptor out;
280 ASSERT_OK(iface->RepeatNullableFd(in, &out));
281
282 EXPECT_EQ(-1, out.get());
283 }
284
TEST_P(NdkBinderTest_Aidl,RepeatString)285 TEST_P(NdkBinderTest_Aidl, RepeatString) {
286 std::string res;
287
288 EXPECT_OK(iface->RepeatString("", &res));
289 EXPECT_EQ("", res);
290
291 EXPECT_OK(iface->RepeatString("a", &res));
292 EXPECT_EQ("a", res);
293
294 EXPECT_OK(iface->RepeatString("say what?", &res));
295 EXPECT_EQ("say what?", res);
296 }
297
TEST_P(NdkBinderTest_Aidl,RepeatNullableString)298 TEST_P(NdkBinderTest_Aidl, RepeatNullableString) {
299 std::optional<std::string> res;
300
301 EXPECT_OK(iface->RepeatNullableString(std::nullopt, &res));
302 EXPECT_EQ(std::nullopt, res);
303
304 EXPECT_OK(iface->RepeatNullableString("", &res));
305 EXPECT_EQ("", *res);
306
307 EXPECT_OK(iface->RepeatNullableString("a", &res));
308 EXPECT_EQ("a", *res);
309
310 EXPECT_OK(iface->RepeatNullableString("say what?", &res));
311 EXPECT_EQ("say what?", *res);
312 }
313
TEST_P(NdkBinderTest_Aidl,ParcelableDefaults)314 TEST_P(NdkBinderTest_Aidl, ParcelableDefaults) {
315 RegularPolygon polygon;
316
317 EXPECT_EQ("square", polygon.name);
318 EXPECT_EQ(4, polygon.numSides);
319 EXPECT_EQ(1.0f, polygon.sideLength);
320 }
321
TEST_P(NdkBinderTest_Aidl,RepeatPolygon)322 TEST_P(NdkBinderTest_Aidl, RepeatPolygon) {
323 RegularPolygon defaultPolygon = {"hexagon", 6, 2.0f};
324 RegularPolygon outputPolygon;
325 ASSERT_OK(iface->RepeatPolygon(defaultPolygon, &outputPolygon));
326 EXPECT_EQ("hexagon", outputPolygon.name);
327 EXPECT_EQ(defaultPolygon.numSides, outputPolygon.numSides);
328 EXPECT_EQ(defaultPolygon.sideLength, outputPolygon.sideLength);
329 }
330
TEST_P(NdkBinderTest_Aidl,InsAndOuts)331 TEST_P(NdkBinderTest_Aidl, InsAndOuts) {
332 RegularPolygon defaultPolygon;
333 ASSERT_OK(iface->RenamePolygon(&defaultPolygon, "Jerry"));
334 EXPECT_EQ("Jerry", defaultPolygon.name);
335 }
336
TEST_P(NdkBinderTest_Aidl,RenameFoo)337 TEST_P(NdkBinderTest_Aidl, RenameFoo) {
338 Foo foo;
339 Foo outputFoo;
340 ASSERT_OK(iface->renameFoo(&foo, "MYFOO"));
341
342 EXPECT_EQ("MYFOO", foo.a);
343 }
344
TEST_P(NdkBinderTest_Aidl,RenameBar)345 TEST_P(NdkBinderTest_Aidl, RenameBar) {
346 Foo foo;
347 Foo outputFoo;
348 ASSERT_OK(iface->renameBar(&foo, "MYBAR"));
349
350 EXPECT_EQ("MYBAR", foo.d.a);
351 }
352
TEST_P(NdkBinderTest_Aidl,GetLastItem)353 TEST_P(NdkBinderTest_Aidl, GetLastItem) {
354 Foo foo;
355 foo.f = 15;
356 int retF;
357 ASSERT_OK(iface->getF(foo, &retF));
358 EXPECT_EQ(15, retF);
359 }
360
TEST_P(NdkBinderTest_Aidl,RepeatFoo)361 TEST_P(NdkBinderTest_Aidl, RepeatFoo) {
362 Foo foo;
363 foo.a = "NEW FOO";
364 foo.b = 57;
365 foo.d.b = "a";
366 foo.e.d = 99;
367 Foo retFoo;
368
369 ASSERT_OK(iface->repeatFoo(foo, &retFoo));
370
371 EXPECT_EQ(foo.a, retFoo.a);
372 EXPECT_EQ(foo.b, retFoo.b);
373 EXPECT_EQ(foo.d.b, retFoo.d.b);
374 EXPECT_EQ(foo.e.d, retFoo.e.d);
375 }
376
377 template <typename T>
378 using RepeatMethod = ScopedAStatus (ITest::*)(const std::vector<T>&,
379 std::vector<T>*, std::vector<T>*);
380
381 namespace aidl {
382 namespace test_package {
operator ==(const RegularPolygon & lhs,const RegularPolygon & rhs)383 inline bool operator==(const RegularPolygon& lhs, const RegularPolygon& rhs) {
384 return lhs.name == rhs.name && lhs.numSides == rhs.numSides && lhs.sideLength == rhs.sideLength;
385 }
operator ==(const std::vector<RegularPolygon> & lhs,const std::vector<RegularPolygon> & rhs)386 inline bool operator==(const std::vector<RegularPolygon>& lhs,
387 const std::vector<RegularPolygon>& rhs) {
388 if (lhs.size() != rhs.size()) return false;
389 for (size_t i = 0; i < lhs.size(); i++) {
390 if (!(lhs[i] == rhs[i])) return false;
391 }
392 return true;
393 }
394 } // namespace test_package
395 } // namespace aidl
396
397 template <typename T>
testRepeat(const std::shared_ptr<ITest> & i,RepeatMethod<T> repeatMethod,std::vector<std::vector<T>> tests)398 void testRepeat(const std::shared_ptr<ITest>& i, RepeatMethod<T> repeatMethod,
399 std::vector<std::vector<T>> tests) {
400 for (const auto& input : tests) {
401 std::vector<T> out1;
402 out1.resize(input.size());
403 std::vector<T> out2;
404
405 ASSERT_OK((i.get()->*repeatMethod)(input, &out1, &out2)) << input.size();
406 EXPECT_EQ(input, out1);
407 EXPECT_EQ(input, out2);
408 }
409 }
410
TEST_P(NdkBinderTest_Aidl,Arrays)411 TEST_P(NdkBinderTest_Aidl, Arrays) {
412 testRepeat<bool>(iface, &ITest::RepeatBooleanArray,
413 {
414 {},
415 {true},
416 {false, true, false},
417 });
418 testRepeat<int8_t>(iface, &ITest::RepeatByteArray,
419 {
420 {},
421 {1},
422 {1, 2, 3},
423 });
424 testRepeat<char16_t>(iface, &ITest::RepeatCharArray,
425 {
426 {},
427 {L'@'},
428 {L'@', L'!', L'A'},
429 });
430 testRepeat<int32_t>(iface, &ITest::RepeatIntArray,
431 {
432 {},
433 {1},
434 {1, 2, 3},
435 });
436 testRepeat<int64_t>(iface, &ITest::RepeatLongArray,
437 {
438 {},
439 {1},
440 {1, 2, 3},
441 });
442 testRepeat<float>(iface, &ITest::RepeatFloatArray,
443 {
444 {},
445 {1.0f},
446 {1.0f, 2.0f, 3.0f},
447 });
448 testRepeat<double>(iface, &ITest::RepeatDoubleArray,
449 {
450 {},
451 {1.0},
452 {1.0, 2.0, 3.0},
453 });
454 testRepeat<std::string>(iface, &ITest::RepeatStringArray,
455 {
456 {},
457 {"asdf"},
458 {"", "aoeu", "lol", "brb"},
459 });
460 testRepeat<RegularPolygon>(iface, &ITest::RepeatRegularPolygonArray,
461 {
462 {},
463 {{"hexagon", 6, 2.0f}},
464 {{"hexagon", 6, 2.0f}, {"square", 4, 7.0f}, {"pentagon", 5, 4.2f}},
465 });
466 }
467
468 template <typename T>
469 using RepeatNullableMethod = ScopedAStatus (ITest::*)(
470 const std::optional<std::vector<std::optional<T>>>&,
471 std::optional<std::vector<std::optional<T>>>*,
472 std::optional<std::vector<std::optional<T>>>*);
473
474 template <typename T>
testRepeat(const std::shared_ptr<ITest> & i,RepeatNullableMethod<T> repeatMethod,std::vector<std::optional<std::vector<std::optional<T>>>> tests)475 void testRepeat(
476 const std::shared_ptr<ITest>& i, RepeatNullableMethod<T> repeatMethod,
477 std::vector<std::optional<std::vector<std::optional<T>>>> tests) {
478 for (const auto& input : tests) {
479 std::optional<std::vector<std::optional<T>>> out1;
480 if (input) {
481 out1 = std::vector<std::optional<T>>{};
482 out1->resize(input->size());
483 }
484 std::optional<std::vector<std::optional<T>>> out2;
485
486 ASSERT_OK((i.get()->*repeatMethod)(input, &out1, &out2))
487 << (input ? input->size() : -1);
488 EXPECT_EQ(input, out1);
489 EXPECT_EQ(input, out2);
490 }
491 }
492
493 template <typename T>
494 using SingleRepeatNullableMethod = ScopedAStatus (ITest::*)(
495 const std::optional<std::vector<T>>&, std::optional<std::vector<T>>*);
496
497 template <typename T>
testRepeat(const std::shared_ptr<ITest> & i,SingleRepeatNullableMethod<T> repeatMethod,std::vector<std::optional<std::vector<T>>> tests)498 void testRepeat(const std::shared_ptr<ITest>& i,
499 SingleRepeatNullableMethod<T> repeatMethod,
500 std::vector<std::optional<std::vector<T>>> tests) {
501 for (const auto& input : tests) {
502 std::optional<std::vector<T>> ret;
503 ASSERT_OK((i.get()->*repeatMethod)(input, &ret))
504 << (input ? input->size() : -1);
505 EXPECT_EQ(input, ret);
506 }
507 }
508
TEST_P(NdkBinderTest_Aidl,NullableArrays)509 TEST_P(NdkBinderTest_Aidl, NullableArrays) {
510 testRepeat<bool>(iface, &ITest::RepeatNullableBooleanArray,
511 {
512 std::nullopt,
513 {{}},
514 {{true}},
515 {{false, true, false}},
516 });
517 testRepeat<int8_t>(iface, &ITest::RepeatNullableByteArray,
518 {
519 std::nullopt,
520 {{}},
521 {{1}},
522 {{1, 2, 3}},
523 });
524 testRepeat<char16_t>(iface, &ITest::RepeatNullableCharArray,
525 {
526 std::nullopt,
527 {{}},
528 {{L'@'}},
529 {{L'@', L'!', L'A'}},
530 });
531 testRepeat<int32_t>(iface, &ITest::RepeatNullableIntArray,
532 {
533 std::nullopt,
534 {{}},
535 {{1}},
536 {{1, 2, 3}},
537 });
538 testRepeat<int64_t>(iface, &ITest::RepeatNullableLongArray,
539 {
540 std::nullopt,
541 {{}},
542 {{1}},
543 {{1, 2, 3}},
544 });
545 testRepeat<float>(iface, &ITest::RepeatNullableFloatArray,
546 {
547 std::nullopt,
548 {{}},
549 {{1.0f}},
550 {{1.0f, 2.0f, 3.0f}},
551 });
552 testRepeat<double>(iface, &ITest::RepeatNullableDoubleArray,
553 {
554 std::nullopt,
555 {{}},
556 {{1.0}},
557 {{1.0, 2.0, 3.0}},
558 });
559 testRepeat<std::optional<std::string>>(
560 iface, &ITest::RepeatNullableStringArray,
561 {
562 std::nullopt,
563 {{}},
564 {{"asdf"}},
565 {{std::nullopt}},
566 {{"aoeu", "lol", "brb"}},
567 {{"", "aoeu", std::nullopt, "brb"}},
568 });
569 testRepeat<std::string>(iface, &ITest::DoubleRepeatNullableStringArray,
570 {
571 {{}},
572 {{"asdf"}},
573 {{std::nullopt}},
574 {{"aoeu", "lol", "brb"}},
575 {{"", "aoeu", std::nullopt, "brb"}},
576 });
577 }
578
579 class DefaultImpl : public ::aidl::test_package::ITestDefault {
580 public:
NewMethodThatReturns10(int32_t * _aidl_return)581 ::ndk::ScopedAStatus NewMethodThatReturns10(int32_t* _aidl_return) override {
582 *_aidl_return = 100; // default impl returns different value
583 return ::ndk::ScopedAStatus(AStatus_newOk());
584 }
585 };
586
TEST_P(NdkBinderTest_Aidl,NewMethod)587 TEST_P(NdkBinderTest_Aidl, NewMethod) {
588 std::shared_ptr<ITest> default_impl = SharedRefBase::make<DefaultImpl>();
589 ::aidl::test_package::ITest::setDefaultImpl(default_impl);
590
591 int32_t res;
592 EXPECT_OK(iface->NewMethodThatReturns10(&res));
593 if (GetParam().shouldBeOld) {
594 // Remote was built with version 1 interface which does not have
595 // "NewMethodThatReturns10". In this case the default method
596 // which returns 100 is called.
597 EXPECT_EQ(100, res);
598 } else {
599 // Remote is built with the current version of the interface.
600 // The method returns 10.
601 EXPECT_EQ(10, res);
602 }
603 }
604
TEST_P(NdkBinderTest_Aidl,GetInterfaceVersion)605 TEST_P(NdkBinderTest_Aidl, GetInterfaceVersion) {
606 int32_t res;
607 EXPECT_OK(iface->getInterfaceVersion(&res));
608 if (GetParam().shouldBeOld) {
609 EXPECT_EQ(1, res);
610 } else {
611 // 10000 is the not-yet-frozen version
612 EXPECT_EQ(10000, res);
613 }
614 }
615
getProxyLocalService()616 std::shared_ptr<ITest> getProxyLocalService() {
617 // BpTest -> AIBinder -> test
618 //
619 // Warning: for testing purposes only. This parcels things within the same process for testing
620 // purposes. In normal usage, this should just return SharedRefBase::make<MyTest> directly.
621 std::shared_ptr<MyTest> test = SharedRefBase::make<MyTest>();
622 return (new BpTest(test->asBinder()))->ref<ITest>();
623 }
624
getNdkBinderTestJavaService(const std::string & method)625 std::shared_ptr<ITest> getNdkBinderTestJavaService(const std::string& method) {
626 JNIEnv* env = GetEnv();
627 if (env == nullptr) {
628 std::cout << "No environment" << std::endl;
629 return nullptr;
630 }
631
632 jclass cl = env->FindClass("android/binder/cts/NdkBinderTest");
633 if (cl == nullptr) {
634 std::cout << "No class" << std::endl;
635 return nullptr;
636 }
637
638 jmethodID mid =
639 env->GetStaticMethodID(cl, method.c_str(), "()Landroid/os/IBinder;");
640 if (mid == nullptr) {
641 std::cout << "No method id" << std::endl;
642 return nullptr;
643 }
644
645 jobject object = env->CallStaticObjectMethod(cl, mid);
646 if (object == nullptr) {
647 std::cout << "Got null service from Java" << std::endl;
648 return nullptr;
649 }
650
651 SpAIBinder binder = SpAIBinder(AIBinder_fromJavaBinder(env, object));
652
653 return ITest::fromBinder(binder);
654 }
655
656 INSTANTIATE_TEST_CASE_P(LocalProxyToNative, NdkBinderTest_Aidl,
657 ::testing::Values(Params{getProxyLocalService(), false /*shouldBeRemote*/,
658 true /*shouldBeWrapped*/, "CPP",
659 false /*shouldBeOld*/}));
660 INSTANTIATE_TEST_CASE_P(LocalNativeFromJava, NdkBinderTest_Aidl,
661 ::testing::Values(Params{
662 getNdkBinderTestJavaService("getLocalNativeService"),
663 false /*shouldBeRemote*/, false /*shouldBeWrapped*/, "CPP",
664 false /*shouldBeOld*/}));
665 INSTANTIATE_TEST_CASE_P(LocalJava, NdkBinderTest_Aidl,
666 ::testing::Values(Params{getNdkBinderTestJavaService("getLocalJavaService"),
667 false /*shouldBeRemote*/, true /*shouldBeWrapped*/,
668 "JAVA", false /*shouldBeOld*/}));
669 INSTANTIATE_TEST_CASE_P(RemoteNative, NdkBinderTest_Aidl,
670 ::testing::Values(Params{
671 getNdkBinderTestJavaService("getRemoteNativeService"),
672 true /*shouldBeRemote*/, true /*shouldBeWrapped*/, "CPP",
673 false /*shouldBeOld*/}));
674 INSTANTIATE_TEST_CASE_P(RemoteJava, NdkBinderTest_Aidl,
675 ::testing::Values(Params{
676 getNdkBinderTestJavaService("getRemoteJavaService"),
677 true /*shouldBeRemote*/, true /*shouldBeWrapped*/, "JAVA",
678 false /*shouldBeOld*/}));
679
680 INSTANTIATE_TEST_CASE_P(RemoteNativeOld, NdkBinderTest_Aidl,
681 ::testing::Values(Params{
682 getNdkBinderTestJavaService("getRemoteOldNativeService"),
683 true /*shouldBeRemote*/, true /*shouldBeWrapped*/, "CPP",
684 true /*shouldBeOld*/}));
685