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