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