xref: /aosp_15_r20/frameworks/native/libs/binder/ndk/tests/binderVendorDoubleLoadTest.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2019 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 
17 #include <BnBinderVendorDoubleLoadTest.h>
18 #include <aidl/BnBinderVendorDoubleLoadTest.h>
19 #include <aidl/android/os/IServiceManager.h>
20 #include <android-base/logging.h>
21 #include <android/binder_ibinder.h>
22 #include <android/binder_manager.h>
23 #include <android/binder_process.h>
24 #include <android/binder_stability.h>
25 #include <binder/IPCThreadState.h>
26 #include <binder/IServiceManager.h>
27 #include <binder/ProcessState.h>
28 #include <binder/Stability.h>
29 #include <binder/Status.h>
30 #include <gtest/gtest.h>
31 #include <sys/prctl.h>
32 
33 using namespace android;
34 using ::android::binder::Status;
35 using ::android::internal::Stability;
36 using ::ndk::ScopedAStatus;
37 using ::ndk::SharedRefBase;
38 using ::ndk::SpAIBinder;
39 
40 static const std::string kLocalNdkServerName = "NdkServer-local-IBinderVendorDoubleLoadTest";
41 static const std::string kRemoteNdkServerName = "NdkServer-remote-IBinderVendorDoubleLoadTest";
42 
43 class NdkServer : public aidl::BnBinderVendorDoubleLoadTest {
RepeatString(const std::string & in,std::string * out)44     ScopedAStatus RepeatString(const std::string& in, std::string* out) override {
45         *out = in;
46         return ScopedAStatus::ok();
47     }
48 };
49 class CppServer : public BnBinderVendorDoubleLoadTest {
RepeatString(const std::string & in,std::string * out)50     Status RepeatString(const std::string& in, std::string* out) override {
51         *out = in;
52         return Status::ok();
53     }
54 };
55 
TEST(DoubleBinder,VendorCppCantCallIntoSystem)56 TEST(DoubleBinder, VendorCppCantCallIntoSystem) {
57     Vector<String16> services = defaultServiceManager()->listServices();
58     EXPECT_TRUE(services.empty());
59 }
60 
TEST(DoubleBinder,VendorCppCantRegisterService)61 TEST(DoubleBinder, VendorCppCantRegisterService) {
62     sp<CppServer> cppServer = new CppServer;
63     status_t status = defaultServiceManager()->addService(String16("anything"), cppServer);
64     EXPECT_EQ(EX_TRANSACTION_FAILED, status);
65 }
66 
TEST(DoubleBinder,CppVendorCantManuallyMarkVintfStability)67 TEST(DoubleBinder, CppVendorCantManuallyMarkVintfStability) {
68     // this test also implies that stability logic is turned on in vendor
69     ASSERT_DEATH(
70             {
71                 sp<IBinder> binder = new CppServer();
72                 Stability::markVintf(binder.get());
73             },
74             "Should only mark known object.");
75 }
76 
TEST(DoubleBinder,NdkVendorCantManuallyMarkVintfStability)77 TEST(DoubleBinder, NdkVendorCantManuallyMarkVintfStability) {
78     // this test also implies that stability logic is turned on in vendor
79     ASSERT_DEATH(
80             {
81                 std::shared_ptr<NdkServer> ndkServer = SharedRefBase::make<NdkServer>();
82                 AIBinder_markVintfStability(ndkServer->asBinder().get());
83             },
84             "Should only mark known object.");
85 }
86 
TEST(DoubleBinder,CallIntoNdk)87 TEST(DoubleBinder, CallIntoNdk) {
88     for (const std::string& serviceName : {kLocalNdkServerName, kRemoteNdkServerName}) {
89         SpAIBinder binder = SpAIBinder(AServiceManager_checkService(serviceName.c_str()));
90         ASSERT_NE(nullptr, binder.get()) << serviceName;
91         EXPECT_EQ(STATUS_OK, AIBinder_ping(binder.get())) << serviceName;
92 
93         std::shared_ptr<aidl::IBinderVendorDoubleLoadTest> server =
94                 aidl::IBinderVendorDoubleLoadTest::fromBinder(binder);
95 
96         ASSERT_NE(nullptr, server.get()) << serviceName;
97 
98         EXPECT_EQ(STATUS_OK, AIBinder_ping(server->asBinder().get()));
99 
100         std::string outString;
101         ScopedAStatus status = server->RepeatString("foo", &outString);
102         EXPECT_EQ(STATUS_OK, AStatus_getExceptionCode(status.get()))
103                 << serviceName << " " << status;
104         EXPECT_EQ("foo", outString) << serviceName;
105     }
106 }
107 
TEST(DoubleBinder,CallIntoSystemStabilityNdk)108 TEST(DoubleBinder, CallIntoSystemStabilityNdk) {
109     // picking an arbitrary system service
110     SpAIBinder binder = SpAIBinder(AServiceManager_checkService("manager"));
111     ASSERT_NE(nullptr, binder.get());
112 
113     // can make stable transaction to system server
114     EXPECT_EQ(STATUS_OK, AIBinder_ping(binder.get()));
115 
116     using aidl::android::os::IServiceManager;
117     std::shared_ptr<IServiceManager> manager = IServiceManager::fromBinder(binder);
118     ASSERT_NE(nullptr, manager.get());
119 
120     std::vector<std::string> services;
121     ASSERT_EQ(
122             STATUS_BAD_TYPE,
123             manager->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL, &services).getStatus());
124 }
125 
initDrivers()126 void initDrivers() {
127     // Explicitly instantiated with the same driver that system would use.
128     // __ANDROID_VNDK__ right now uses /dev/vndbinder by default.
129     ProcessState::initWithDriver("/dev/binder");
130     ProcessState::self()->startThreadPool();
131     ABinderProcess_startThreadPool();
132 }
133 
main(int argc,char ** argv)134 int main(int argc, char** argv) {
135     ::testing::InitGoogleTest(&argc, argv);
136 
137     if (fork() == 0) {
138         // child process
139 
140         prctl(PR_SET_PDEATHSIG, SIGHUP);
141 
142         initDrivers();
143 
144         // REMOTE SERVERS
145         std::shared_ptr<NdkServer> ndkServer = SharedRefBase::make<NdkServer>();
146         CHECK(STATUS_OK == AServiceManager_addService(ndkServer->asBinder().get(),
147                                                       kRemoteNdkServerName.c_str()));
148 
149         // OR sleep forever or whatever, it doesn't matter
150         IPCThreadState::self()->joinThreadPool(true);
151         exit(1);  // should not reach
152     }
153 
154     sleep(1);
155 
156     initDrivers();
157 
158     // LOCAL SERVERS
159     std::shared_ptr<NdkServer> ndkServer = SharedRefBase::make<NdkServer>();
160     CHECK(STATUS_OK ==
161           AServiceManager_addService(ndkServer->asBinder().get(), kLocalNdkServerName.c_str()));
162 
163     return RUN_ALL_TESTS();
164 }
165