blob: 52595e0940a5e61457944d63940b091748c8d583 [file] [log] [blame]
Steven Morelanddea3cf92019-07-16 18:06:55 -07001/*
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 <binder/Binder.h>
18#include <binder/IBinder.h>
19#include <binder/IPCThreadState.h>
20#include <binder/IServiceManager.h>
21#include <binder/Parcel.h>
22#include <binder/Stability.h>
23#include <gtest/gtest.h>
24
25#include <sys/prctl.h>
26
Steven Morelandc709dd82019-08-05 20:30:14 -070027#include "BnBinderStabilityTestSub.h"
Steven Morelanddea3cf92019-07-16 18:06:55 -070028#include "BnBinderStabilityTest.h"
29#include "BpBinderStabilityTest.h"
30
31using namespace android;
32using android::binder::Status;
33
34const String16 kNoStabilityServer = String16("binder_stability_test_service_low");
35const String16 kCompilationUnitServer = String16("binder_stability_test_service_compl");
36const String16 kVintfServer = String16("binder_stability_test_service_vintf");
37
Steven Morelandc709dd82019-08-05 20:30:14 -070038class BadStabilityTestSub : public BnBinderStabilityTestSub {
39 Status userDefinedTransaction() {
40 return Status::ok();
41 }
42};
43
44sp<IBinderStabilityTestSub> getCompilationUnitStability() {
45 sp<BnBinderStabilityTestSub> iface = new BadStabilityTestSub();
Steven Morelanddea3cf92019-07-16 18:06:55 -070046 // NO! NO! NO! NO! DO NOT EVERY DO SOMETHING LIKE THIS?
47 // WHAT ARE YOU CRAZY? IT'S VERY DANGEROUS
Steven Morelandc709dd82019-08-05 20:30:14 -070048 internal::Stability::markCompilationUnit(iface.get()); // <- BAD, NO! DO NOT COPY
49 return iface;
Steven Morelanddea3cf92019-07-16 18:06:55 -070050}
51
Steven Morelandc709dd82019-08-05 20:30:14 -070052sp<IBinderStabilityTestSub> getVintfStability() {
53 sp<BnBinderStabilityTestSub> iface = new BadStabilityTestSub();
Steven Morelanddea3cf92019-07-16 18:06:55 -070054 // NO! NO! NO! NO! DO NOT EVERY DO SOMETHING LIKE THIS?
55 // WHAT ARE YOU CRAZY? IT'S VERY DANGEROUS
Steven Morelandc709dd82019-08-05 20:30:14 -070056 internal::Stability::markVintf(iface.get()); // <- BAD, NO! DO NOT COPY
57 return iface;
58}
59
60sp<IBinderStabilityTestSub> getVendorStability() {
61 sp<BnBinderStabilityTestSub> iface = new BadStabilityTestSub();
62 // NO! NO! NO! NO! DO NOT EVERY DO SOMETHING LIKE THIS?
63 // WHAT ARE YOU CRAZY? IT'S VERY DANGEROUS
64 internal::Stability::markVndk(iface.get()); // <- BAD, NO! DO NOT COPY
65 return iface;
Steven Morelanddea3cf92019-07-16 18:06:55 -070066}
67
68// NO! NO! NO! Do not even think of doing something like this!
69// This is for testing! If a class like this was actually used in production,
70// it would ruin everything!
71class BadStabilityTester : public BnBinderStabilityTest {
72public:
Steven Morelandc709dd82019-08-05 20:30:14 -070073 Status sendBinder(const sp<IBinderStabilityTestSub>& /*binder*/) override {
Steven Morelanddea3cf92019-07-16 18:06:55 -070074 return Status::ok();
75 }
Steven Morelandc709dd82019-08-05 20:30:14 -070076 Status sendAndCallBinder(const sp<IBinderStabilityTestSub>& binder) override {
77 return binder->userDefinedTransaction();
78 }
79 Status returnNoStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override {
80 *_aidl_return = new BadStabilityTestSub();
Steven Morelanddea3cf92019-07-16 18:06:55 -070081 return Status::ok();
82 }
Steven Morelandc709dd82019-08-05 20:30:14 -070083 Status returnLocalStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override {
Steven Morelanddea3cf92019-07-16 18:06:55 -070084 *_aidl_return = getCompilationUnitStability();
85 return Status::ok();
86 }
Steven Morelandc709dd82019-08-05 20:30:14 -070087 Status returnVintfStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override {
Steven Morelanddea3cf92019-07-16 18:06:55 -070088 *_aidl_return = getVintfStability();
89 return Status::ok();
90 }
Steven Morelandc709dd82019-08-05 20:30:14 -070091 Status returnVendorStabilityBinder(sp<IBinderStabilityTestSub>* _aidl_return) override {
92 *_aidl_return = getVendorStability();
93 return Status::ok();
Steven Morelanddea3cf92019-07-16 18:06:55 -070094 }
95};
96
Steven Morelandc709dd82019-08-05 20:30:14 -070097void checkSystemStabilityBinder(const sp<IBinderStabilityTest>& complServer) {
98 EXPECT_TRUE(complServer->sendBinder(new BadStabilityTestSub()).isOk());
Steven Morelanddea3cf92019-07-16 18:06:55 -070099 EXPECT_TRUE(complServer->sendBinder(getCompilationUnitStability()).isOk());
100 EXPECT_TRUE(complServer->sendBinder(getVintfStability()).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700101 EXPECT_TRUE(complServer->sendBinder(getVendorStability()).isOk());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700102
Steven Morelandc709dd82019-08-05 20:30:14 -0700103 EXPECT_TRUE(complServer->sendAndCallBinder(new BadStabilityTestSub()).isOk());
104 EXPECT_TRUE(complServer->sendAndCallBinder(getCompilationUnitStability()).isOk());
105 EXPECT_TRUE(complServer->sendAndCallBinder(getVintfStability()).isOk());
106
107 // !!! user-defined transaction may not be stable for remote server !!!
108 EXPECT_FALSE(complServer->sendAndCallBinder(getVendorStability()).isOk());
109
110 sp<IBinderStabilityTestSub> out;
Steven Moreland2a9f32f2019-07-31 17:51:25 -0700111 EXPECT_TRUE(complServer->returnNoStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700112 ASSERT_NE(nullptr, out.get());
113 EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder());
114 EXPECT_TRUE(out->userDefinedTransaction().isOk());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700115
116 EXPECT_TRUE(complServer->returnLocalStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700117 ASSERT_NE(nullptr, out.get());
118 EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder());
119 EXPECT_TRUE(out->userDefinedTransaction().isOk());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700120
121 EXPECT_TRUE(complServer->returnVintfStabilityBinder(&out).isOk());
Steven Morelandc709dd82019-08-05 20:30:14 -0700122 ASSERT_NE(nullptr, out.get());
123 EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder());
124 EXPECT_TRUE(out->userDefinedTransaction().isOk());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700125
Steven Morelandc709dd82019-08-05 20:30:14 -0700126 EXPECT_TRUE(complServer->returnVendorStabilityBinder(&out).isOk());
127 ASSERT_NE(nullptr, out.get());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700128
Steven Morelandc709dd82019-08-05 20:30:14 -0700129 // !!! libbinder-defined transaction works !!!
130 EXPECT_EQ(OK, IInterface::asBinder(out)->pingBinder());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700131
Steven Morelandc709dd82019-08-05 20:30:14 -0700132 // !!! user-defined transaction may not be stable !!!
133 EXPECT_FALSE(out->userDefinedTransaction().isOk());
Steven Morelanddea3cf92019-07-16 18:06:55 -0700134}
135
136TEST(BinderStability, RemoteNoStabilityServer) {
137 sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kNoStabilityServer);
138 auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder);
139
140 ASSERT_NE(nullptr, remoteServer.get());
141 ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());
142
Steven Morelandc709dd82019-08-05 20:30:14 -0700143 checkSystemStabilityBinder(remoteServer);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700144}
145
146TEST(BinderStability, RemoteLowStabilityServer) {
147 sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kCompilationUnitServer);
148 auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder);
149
150 ASSERT_NE(nullptr, remoteServer.get());
151 ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());
152
Steven Morelandc709dd82019-08-05 20:30:14 -0700153 checkSystemStabilityBinder(remoteServer);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700154}
155
156TEST(BinderStability, RemoteVintfServer) {
157 sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kVintfServer);
158 auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder);
159
160 ASSERT_NE(nullptr, remoteServer.get());
161 ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder());
162
Steven Morelandc709dd82019-08-05 20:30:14 -0700163 checkSystemStabilityBinder(remoteServer);
Steven Morelanddea3cf92019-07-16 18:06:55 -0700164}
165
166class MarksStabilityInConstructor : public BBinder {
167public:
168 static bool gDestructed;
169
170 MarksStabilityInConstructor() {
171 internal::Stability::markCompilationUnit(this);
172 }
173 ~MarksStabilityInConstructor() {
174 gDestructed = true;
175 }
176};
177bool MarksStabilityInConstructor::gDestructed = false;
178
179TEST(BinderStability, MarkingObjectNoDestructTest) {
180 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
181
182 // best practice is to put this directly in an sp, but for this test, we
183 // want to explicitly check what happens before that happens
184 MarksStabilityInConstructor* binder = new MarksStabilityInConstructor();
185 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
186
187 sp<MarksStabilityInConstructor> binderSp = binder;
188 ASSERT_FALSE(MarksStabilityInConstructor::gDestructed);
189
190 binderSp = nullptr;
191 ASSERT_TRUE(MarksStabilityInConstructor::gDestructed);
192}
193
194int main(int argc, char** argv) {
195 ::testing::InitGoogleTest(&argc, argv);
196
197 if (fork() == 0) {
198 // child process
199 prctl(PR_SET_PDEATHSIG, SIGHUP);
200
201 sp<IBinder> noStability = new BadStabilityTester;
202 android::defaultServiceManager()->addService(kNoStabilityServer, noStability);
203
204 sp<IBinder> compil = new BadStabilityTester;
205 internal::Stability::markCompilationUnit(compil.get());
206 android::defaultServiceManager()->addService(kCompilationUnitServer, compil);
207
208 sp<IBinder> vintf = new BadStabilityTester;
209 internal::Stability::markVintf(vintf.get());
210 android::defaultServiceManager()->addService(kVintfServer, vintf);
211
212 IPCThreadState::self()->joinThreadPool(true);
213 exit(1); // should not reach
214 }
215
216 // This is not racey. Just giving these services some time to register before we call
217 // getService which sleeps for much longer...
218 usleep(10000);
219
220 return RUN_ALL_TESTS();
221}