1 /*
2  * Copyright (C) 2022 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 #include <android/binder_auto_utils.h>
17 #include <android/binder_manager.h>
18 #include <binder/ProcessState.h>
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <aidl/android/aidl/tests/BnTestService.h>
23 #include <android-base/logging.h>
24 
25 using aidl::android::aidl::tests::BackendType;
26 using aidl::android::aidl::tests::ITestService;
27 using aidl::android::aidl::tests::ITestServiceDelegator;
28 
29 static constexpr int8_t kCustomByte = 8;
30 
31 static_assert(std::is_same<ITestService::DefaultDelegator, ITestServiceDelegator>::value);
32 
33 struct CustomDelegator : public ITestServiceDelegator {
34  public:
CustomDelegatorCustomDelegator35   CustomDelegator(std::shared_ptr<ITestService>& impl) : ITestServiceDelegator(impl) {}
36 
37   // Change RepeatByte to always return the same byte.
RepeatByteCustomDelegator38   ndk::ScopedAStatus RepeatByte(int8_t /* token */, int8_t* _aidl_return) override {
39     *_aidl_return = kCustomByte;
40     return ndk::ScopedAStatus::ok();
41   }
42 };
43 
44 struct AidlDelegatorTest : testing::Test {
45   template <typename T>
getServiceAidlDelegatorTest46   std::shared_ptr<T> getService() {
47     android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
48     android::ProcessState::self()->startThreadPool();
49     ndk::SpAIBinder binder = ndk::SpAIBinder(AServiceManager_waitForService(T::descriptor));
50     return T::fromBinder(binder);
51   }
SetUpAidlDelegatorTest52   void SetUp() override { service = getService<ITestService>(); }
53   std::shared_ptr<ITestService> service;
54 };
55 
TEST_F(AidlDelegatorTest,SimpleDelegator)56 TEST_F(AidlDelegatorTest, SimpleDelegator) {
57   auto delegator = ndk::SharedRefBase::make<ITestServiceDelegator>(service);
58   int8_t returned_value;
59   auto status = delegator->RepeatByte(12, &returned_value);
60   ASSERT_TRUE(status.isOk()) << status.getMessage();
61   EXPECT_EQ(12, returned_value);
62 }
63 
TEST_F(AidlDelegatorTest,CustomDelegator)64 TEST_F(AidlDelegatorTest, CustomDelegator) {
65   auto delegator = ndk::SharedRefBase::make<CustomDelegator>(service);
66   int8_t returned_value;
67   auto status = delegator->RepeatByte(12, &returned_value);
68   ASSERT_TRUE(status.isOk()) << status.getMessage();
69   EXPECT_EQ(kCustomByte, returned_value);
70 }
71 
TEST_F(AidlDelegatorTest,SendDelegator)72 TEST_F(AidlDelegatorTest, SendDelegator) {
73   auto delegator = ndk::SharedRefBase::make<ITestServiceDelegator>(service);
74   auto fromAsBinder = ITestServiceDelegator::fromBinder(delegator->asBinder());
75   // Make sure the delegator works after asBinder -> fromBinder conversions
76   int8_t returned_value = 0;
77   auto status = fromAsBinder->RepeatByte(12, &returned_value);
78   ASSERT_TRUE(status.isOk()) << status.getDescription();
79   EXPECT_EQ(12, returned_value);
80 }
81