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