blob: 0218b9495093ad5f301c7d073449a1f3d9841222 [file] [log] [blame]
Steven Morelanda86a3562019-08-01 23:28:34 +00001/*
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 <android/os/IServiceManager.h>
18#include <binder/Binder.h>
19#include <binder/IBinder.h>
20#include <binder/IPCThreadState.h>
21#include <binder/IServiceManager.h>
22#include <binder/Parcel.h>
23#include <binder/Stability.h>
24#include <gtest/gtest.h>
25
26#include <sys/prctl.h>
27
28#include "BnBinderStabilityTest.h"
29#include "BpBinderStabilityTest.h"
30
31using namespace android;
32using android::binder::Status;
33using android::os::IServiceManager;
34
35const String16 kNoStabilityServer = String16("binder_stability_test_service_low");
36const String16 kCompilationUnitServer = String16("binder_stability_test_service_compl");
37const String16 kVintfServer = String16("binder_stability_test_service_vintf");
38
39sp<IBinder> getCompilationUnitStability() {
40 sp<IBinder> binder = new BBinder();
41 // NO! NO! NO! NO! DO NOT EVERY DO SOMETHING LIKE THIS?
42 // WHAT ARE YOU CRAZY? IT'S VERY DANGEROUS
43 internal::Stability::markCompilationUnit(binder.get()); // <- BAD, NO! DO NOT COPY
44 return binder;
45}
46
47sp<IBinder> getVintfStability() {
48 sp<IBinder> binder = new BBinder();
49 // NO! NO! NO! NO! DO NOT EVERY DO SOMETHING LIKE THIS?
50 // WHAT ARE YOU CRAZY? IT'S VERY DANGEROUS
51 internal::Stability::markVintf(binder.get()); // <- BAD, NO! DO NOT COPY
52 return binder;
53}
54
55// NO! NO! NO! Do not even think of doing something like this!
56// This is for testing! If a class like this was actually used in production,
57// it would ruin everything!
58class BadStabilityTester : public BnBinderStabilityTest {
59public:
60 Status sendBinder(const sp<IBinder>& /*binder*/) override {
61 return Status::ok();
62 }
63 Status returnNoStabilityBinder(sp<IBinder>* _aidl_return) override {
64 *_aidl_return = new BBinder();
65 return Status::ok();
66 }
67 Status returnLocalStabilityBinder(sp<IBinder>* _aidl_return) override {
68 *_aidl_return = getCompilationUnitStability();
69 return Status::ok();
70 }
71 Status returnVintfStabilityBinder(sp<IBinder>* _aidl_return) override {
72 *_aidl_return = getVintfStability();
73 return Status::ok();
74 }
75
76 static sp<IBinderStabilityTest> getNoStabilityServer() {
77 sp<IBinder> remote = new BadStabilityTester;
78 return new BpBinderStabilityTest(remote);
79 }
80 static sp<IBinderStabilityTest> getCompilationUnitStabilityServer() {
81 sp<IBinder> remote = new BadStabilityTester;
82 internal::Stability::markCompilationUnit(remote.get());
83 return new BpBinderStabilityTest(remote);
84 }
85 static sp<IBinderStabilityTest> getVintfStabilityServer() {
86 sp<IBinder> remote = new BadStabilityTester;
87 internal::Stability::markVintf(remote.get()); // <- BAD, NO! DO NOT COPY
88 return new BpBinderStabilityTest(remote);
89 }
90};
91
Steven Moreland05929552019-07-31 17:51:25 -070092void checkLocalStabilityBinder(const sp<IBinderStabilityTest>& complServer) {
93 // this binder should automatically be set to local stability
94 EXPECT_TRUE(complServer->sendBinder(new BBinder()).isOk());
Steven Morelanda86a3562019-08-01 23:28:34 +000095 EXPECT_TRUE(complServer->sendBinder(getCompilationUnitStability()).isOk());
96 EXPECT_TRUE(complServer->sendBinder(getVintfStability()).isOk());
97
98 sp<IBinder> out;
Steven Moreland05929552019-07-31 17:51:25 -070099 // should automatically be set to local stability
100 EXPECT_TRUE(complServer->returnNoStabilityBinder(&out).isOk());
101 EXPECT_NE(nullptr, out.get());
Steven Morelanda86a3562019-08-01 23:28:34 +0000102
103 EXPECT_TRUE(complServer->returnLocalStabilityBinder(&out).isOk());
104 EXPECT_NE(nullptr, out.get());
105
106 EXPECT_TRUE(complServer->returnVintfStabilityBinder(&out).isOk());
107 EXPECT_NE(nullptr, out.get());
108}
109
110void checkHighStabilityServer(const sp<IBinderStabilityTest>& highStability) {
111 EXPECT_FALSE(highStability->sendBinder(new BBinder()).isOk());
112 EXPECT_FALSE(highStability->sendBinder(getCompilationUnitStability()).isOk());
113 EXPECT_TRUE(highStability->sendBinder(getVintfStability()).isOk());
114
115 sp<IBinder> out;
116 EXPECT_FALSE(highStability->returnNoStabilityBinder(&out).isOk());
117 EXPECT_EQ(nullptr, out.get());
118
119 EXPECT_FALSE(highStability->returnLocalStabilityBinder(&out).isOk());
120 EXPECT_EQ(nullptr, out.get());
121
122 EXPECT_TRUE(highStability->returnVintfStabilityBinder(&out).isOk());
123 EXPECT_NE(nullptr, out.get());
124}
125
126TEST(BinderStability, LocalNoStabilityServer) {
127 // in practice, a low stability server is probably one that hasn't been rebuilt
128 // or was written by hand.
129 auto server = BadStabilityTester::getNoStabilityServer();
130 ASSERT_NE(nullptr, IInterface::asBinder(server)->localBinder());
Steven Moreland05929552019-07-31 17:51:25 -0700131
132 // it should be considered to have local stability
133 checkLocalStabilityBinder(server);
Steven Morelanda86a3562019-08-01 23:28:34 +0000134}
135
136TEST(BinderStability, LocalLowStabilityServer) {
137 auto server = BadStabilityTester::getCompilationUnitStabilityServer();
138 ASSERT_NE(nullptr, IInterface::asBinder(server)->localBinder());
Steven Moreland05929552019-07-31 17:51:25 -0700139 checkLocalStabilityBinder(server);
Steven Morelanda86a3562019-08-01 23:28:34 +0000140}
141
142TEST(BinderStability, LocalHighStabilityServer) {
143 auto server = BadStabilityTester::getVintfStabilityServer();
144 ASSERT_NE(nullptr, IInterface::asBinder(server)->localBinder());
145 checkHighStabilityServer(server);
146}
147
148TEST(BinderStability, RemoteNoStabilityServer) {
149 sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kNoStabilityServer);
150 auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder);
151
152 ASSERT_NE(nullptr, remoteServer.get());
153 ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());
154
Steven Moreland05929552019-07-31 17:51:25 -0700155 // it should be considered to have local stability
156 checkLocalStabilityBinder(remoteServer);
Steven Morelanda86a3562019-08-01 23:28:34 +0000157}
158
159TEST(BinderStability, RemoteLowStabilityServer) {
160 sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kCompilationUnitServer);
161 auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder);
162
163 ASSERT_NE(nullptr, remoteServer.get());
164 ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());
165
Steven Moreland05929552019-07-31 17:51:25 -0700166 checkLocalStabilityBinder(remoteServer);
Steven Morelanda86a3562019-08-01 23:28:34 +0000167}
168
169TEST(BinderStability, RemoteVintfServer) {
170 sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kVintfServer);
171 auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder);
172
173 ASSERT_NE(nullptr, remoteServer.get());
174 ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());
175
176 checkHighStabilityServer(remoteServer);
177}
178
179class MarksStabilityInConstructor : public BBinder {
180public:
181 static bool gDestructed;
182
183 MarksStabilityInConstructor() {
184 internal::Stability::markCompilationUnit(this);
185 }
186 ~MarksStabilityInConstructor() {
187 gDestructed = true;
188 }
189};
190bool MarksStabilityInConstructor::gDestructed = false;
191
192TEST(BinderStability, MarkingObjectNoDestructTest) {
193 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
194
195 // best practice is to put this directly in an sp, but for this test, we
196 // want to explicitly check what happens before that happens
197 MarksStabilityInConstructor* binder = new MarksStabilityInConstructor();
198 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
199
200 sp<MarksStabilityInConstructor> binderSp = binder;
201 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
202
203 binderSp = nullptr;
204 ASSERT_TRUE(MarksStabilityInConstructor::gDestructed);
205}
206
207int main(int argc, char** argv) {
208 ::testing::InitGoogleTest(&argc, argv);
209
210 if (fork() == 0) {
211 // child process
212 prctl(PR_SET_PDEATHSIG, SIGHUP);
213
214 sp<IBinder> noStability = new BadStabilityTester;
215 android::defaultServiceManager()->addService(kNoStabilityServer, noStability);
216
217 sp<IBinder> compil = new BadStabilityTester;
218 internal::Stability::markCompilationUnit(compil.get());
219 android::defaultServiceManager()->addService(kCompilationUnitServer, compil);
220
221 sp<IBinder> vintf = new BadStabilityTester;
222 internal::Stability::markVintf(vintf.get());
223 android::defaultServiceManager()->addService(kVintfServer, vintf);
224
225 IPCThreadState::self()->joinThreadPool(true);
226 exit(1); // should not reach
227 }
228
229 // This is not racey. Just giving these services some time to register before we call
230 // getService which sleeps for much longer...
231 usleep(10000);
232
233 return RUN_ALL_TESTS();
234}