blob: ecd4f0dae9f33a077044286bb77ae74faf1adefc [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
Steven Moreland6e5a7752019-08-05 20:30:14 -070028#include "BnBinderStabilityTestSub.h"
Steven Morelanda86a3562019-08-01 23:28:34 +000029#include "BnBinderStabilityTest.h"
30#include "BpBinderStabilityTest.h"
31
32using namespace android;
33using android::binder::Status;
34using android::os::IServiceManager;
35
36const String16 kNoStabilityServer = String16("binder_stability_test_service_low");
37const String16 kCompilationUnitServer = String16("binder_stability_test_service_compl");
38const String16 kVintfServer = String16("binder_stability_test_service_vintf");
39
Steven Moreland6e5a7752019-08-05 20:30:14 -070040class BadStabilityTestSub : public BnBinderStabilityTestSub {
41 Status userDefinedTransaction() {
42 return Status::ok();
43 }
44};
45
46sp<IBinderStabilityTestSub> getCompilationUnitStability() {
47 sp<BnBinderStabilityTestSub> iface = new BadStabilityTestSub();
Steven Morelanda86a3562019-08-01 23:28:34 +000048 // NO! NO! NO! NO! DO NOT EVERY DO SOMETHING LIKE THIS?
49 // WHAT ARE YOU CRAZY? IT'S VERY DANGEROUS
Steven Moreland6e5a7752019-08-05 20:30:14 -070050 internal::Stability::markCompilationUnit(iface.get()); // <- BAD, NO! DO NOT COPY
51 return iface;
Steven Morelanda86a3562019-08-01 23:28:34 +000052}
53
Steven Moreland6e5a7752019-08-05 20:30:14 -070054sp<IBinderStabilityTestSub> getVintfStability() {
55 sp<BnBinderStabilityTestSub> iface = new BadStabilityTestSub();
Steven Morelanda86a3562019-08-01 23:28:34 +000056 // NO! NO! NO! NO! DO NOT EVERY DO SOMETHING LIKE THIS?
57 // WHAT ARE YOU CRAZY? IT'S VERY DANGEROUS
Steven Moreland6e5a7752019-08-05 20:30:14 -070058 internal::Stability::markVintf(iface.get()); // <- BAD, NO! DO NOT COPY
59 return iface;
60}
61
62sp<IBinderStabilityTestSub> getVendorStability() {
63 sp<BnBinderStabilityTestSub> iface = new BadStabilityTestSub();
64 // NO! NO! NO! NO! DO NOT EVERY DO SOMETHING LIKE THIS?
65 // WHAT ARE YOU CRAZY? IT'S VERY DANGEROUS
66 internal::Stability::markVndk(iface.get()); // <- BAD, NO! DO NOT COPY
67 return iface;
Steven Morelanda86a3562019-08-01 23:28:34 +000068}
69
70// NO! NO! NO! Do not even think of doing something like this!
71// This is for testing! If a class like this was actually used in production,
72// it would ruin everything!
73class BadStabilityTester : public BnBinderStabilityTest {
74public:
Steven Moreland6e5a7752019-08-05 20:30:14 -070075 Status sendBinder(const sp<IBinderStabilityTestSub>& /*binder*/) override {
Steven Morelanda86a3562019-08-01 23:28:34 +000076 return Status::ok();
77 }
Steven Moreland6e5a7752019-08-05 20:30:14 -070078 Status sendAndCallBinder(const sp<IBinderStabilityTestSub>& binder) override {
79 return binder->userDefinedTransaction();
80 }
81 Status returnNoStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override {
82 *_aidl_return = new BadStabilityTestSub();
Steven Morelanda86a3562019-08-01 23:28:34 +000083 return Status::ok();
84 }
Steven Moreland6e5a7752019-08-05 20:30:14 -070085 Status returnLocalStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override {
Steven Morelanda86a3562019-08-01 23:28:34 +000086 *_aidl_return = getCompilationUnitStability();
87 return Status::ok();
88 }
Steven Moreland6e5a7752019-08-05 20:30:14 -070089 Status returnVintfStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override {
Steven Morelanda86a3562019-08-01 23:28:34 +000090 *_aidl_return = getVintfStability();
91 return Status::ok();
92 }
Steven Moreland6e5a7752019-08-05 20:30:14 -070093 Status returnVendorStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override {
94 *_aidl_return = getVendorStability();
95 return Status::ok();
Steven Morelanda86a3562019-08-01 23:28:34 +000096 }
97};
98
Steven Moreland6e5a7752019-08-05 20:30:14 -070099void checkSystemStabilityBinder(const sp<IBinderStabilityTest>& complServer) {
100 EXPECT_TRUE(complServer->sendBinder(new BadStabilityTestSub()).isOk());
Steven Morelanda86a3562019-08-01 23:28:34 +0000101 EXPECT_TRUE(complServer->sendBinder(getCompilationUnitStability()).isOk());
102 EXPECT_TRUE(complServer->sendBinder(getVintfStability()).isOk());
Steven Moreland6e5a7752019-08-05 20:30:14 -0700103 EXPECT_TRUE(complServer->sendBinder(getVendorStability()).isOk());
Steven Morelanda86a3562019-08-01 23:28:34 +0000104
Steven Moreland6e5a7752019-08-05 20:30:14 -0700105 EXPECT_TRUE(complServer->sendAndCallBinder(new BadStabilityTestSub()).isOk());
106 EXPECT_TRUE(complServer->sendAndCallBinder(getCompilationUnitStability()).isOk());
107 EXPECT_TRUE(complServer->sendAndCallBinder(getVintfStability()).isOk());
108
109 // !!! user-defined transaction may not be stable for remote server !!!
110 EXPECT_FALSE(complServer->sendAndCallBinder(getVendorStability()).isOk());
111
112 sp<IBinderStabilityTestSub> out;
Steven Moreland05929552019-07-31 17:51:25 -0700113 EXPECT_TRUE(complServer->returnNoStabilityBinder(&out).isOk());
Steven Moreland6e5a7752019-08-05 20:30:14 -0700114 ASSERT_NE(nullptr, out.get());
115 EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder());
116 EXPECT_TRUE(out->userDefinedTransaction().isOk());
Steven Morelanda86a3562019-08-01 23:28:34 +0000117
118 EXPECT_TRUE(complServer->returnLocalStabilityBinder(&out).isOk());
Steven Moreland6e5a7752019-08-05 20:30:14 -0700119 ASSERT_NE(nullptr, out.get());
120 EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder());
121 EXPECT_TRUE(out->userDefinedTransaction().isOk());
Steven Morelanda86a3562019-08-01 23:28:34 +0000122
123 EXPECT_TRUE(complServer->returnVintfStabilityBinder(&out).isOk());
Steven Moreland6e5a7752019-08-05 20:30:14 -0700124 ASSERT_NE(nullptr, out.get());
125 EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder());
126 EXPECT_TRUE(out->userDefinedTransaction().isOk());
Steven Morelanda86a3562019-08-01 23:28:34 +0000127
Steven Moreland6e5a7752019-08-05 20:30:14 -0700128 EXPECT_TRUE(complServer->returnVendorStabilityBinder(&out).isOk());
129 ASSERT_NE(nullptr, out.get());
Steven Morelanda86a3562019-08-01 23:28:34 +0000130
Steven Moreland6e5a7752019-08-05 20:30:14 -0700131 // !!! libbinder-defined transaction works !!!
132 EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder());
Steven Morelanda86a3562019-08-01 23:28:34 +0000133
Steven Moreland6e5a7752019-08-05 20:30:14 -0700134 // !!! user-defined transaction may not be stable !!!
135 EXPECT_FALSE(out->userDefinedTransaction().isOk());
Steven Morelanda86a3562019-08-01 23:28:34 +0000136}
137
138TEST(BinderStability, RemoteNoStabilityServer) {
139 sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kNoStabilityServer);
140 auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder);
141
142 ASSERT_NE(nullptr, remoteServer.get());
143 ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());
144
Steven Moreland6e5a7752019-08-05 20:30:14 -0700145 checkSystemStabilityBinder(remoteServer);
Steven Morelanda86a3562019-08-01 23:28:34 +0000146}
147
148TEST(BinderStability, RemoteLowStabilityServer) {
149 sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kCompilationUnitServer);
150 auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder);
151
152 ASSERT_NE(nullptr, remoteServer.get());
153 ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());
154
Steven Moreland6e5a7752019-08-05 20:30:14 -0700155 checkSystemStabilityBinder(remoteServer);
Steven Morelanda86a3562019-08-01 23:28:34 +0000156}
157
158TEST(BinderStability, RemoteVintfServer) {
159 sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kVintfServer);
160 auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder);
161
162 ASSERT_NE(nullptr, remoteServer.get());
163 ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());
164
Steven Moreland6e5a7752019-08-05 20:30:14 -0700165 checkSystemStabilityBinder(remoteServer);
Steven Morelanda86a3562019-08-01 23:28:34 +0000166}
167
168class MarksStabilityInConstructor : public BBinder {
169public:
170 static bool gDestructed;
171
172 MarksStabilityInConstructor() {
173 internal::Stability::markCompilationUnit(this);
174 }
175 ~MarksStabilityInConstructor() {
176 gDestructed = true;
177 }
178};
179bool MarksStabilityInConstructor::gDestructed = false;
180
181TEST(BinderStability, MarkingObjectNoDestructTest) {
182 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
183
184 // best practice is to put this directly in an sp, but for this test, we
185 // want to explicitly check what happens before that happens
186 MarksStabilityInConstructor* binder = new MarksStabilityInConstructor();
187 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
188
189 sp<MarksStabilityInConstructor> binderSp = binder;
190 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
191
192 binderSp = nullptr;
193 ASSERT_TRUE(MarksStabilityInConstructor::gDestructed);
194}
195
196int main(int argc, char** argv) {
197 ::testing::InitGoogleTest(&argc, argv);
198
199 if (fork() == 0) {
200 // child process
201 prctl(PR_SET_PDEATHSIG, SIGHUP);
202
203 sp<IBinder> noStability = new BadStabilityTester;
204 android::defaultServiceManager()->addService(kNoStabilityServer, noStability);
205
206 sp<IBinder> compil = new BadStabilityTester;
207 internal::Stability::markCompilationUnit(compil.get());
208 android::defaultServiceManager()->addService(kCompilationUnitServer, compil);
209
210 sp<IBinder> vintf = new BadStabilityTester;
211 internal::Stability::markVintf(vintf.get());
212 android::defaultServiceManager()->addService(kVintfServer, vintf);
213
214 IPCThreadState::self()->joinThreadPool(true);
215 exit(1); // should not reach
216 }
217
218 // This is not racey. Just giving these services some time to register before we call
219 // getService which sleeps for much longer...
220 usleep(10000);
221
222 return RUN_ALL_TESTS();
223}