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 | |
| 90 | void checkNoStabilityServer(const sp<IBinderStabilityTest>& unkemptServer) { |
| 91 | EXPECT_TRUE(unkemptServer->sendBinder(new BBinder()).isOk()); |
| 92 | EXPECT_TRUE(unkemptServer->sendBinder(getCompilationUnitStability()).isOk()); |
| 93 | EXPECT_TRUE(unkemptServer->sendBinder(getVintfStability()).isOk()); |
| 94 | |
| 95 | sp<IBinder> out; |
| 96 | EXPECT_TRUE(unkemptServer->returnNoStabilityBinder(&out).isOk()); |
| 97 | EXPECT_NE(nullptr, out.get()); |
| 98 | |
| 99 | EXPECT_TRUE(unkemptServer->returnLocalStabilityBinder(&out).isOk()); |
| 100 | EXPECT_NE(nullptr, out.get()); |
| 101 | |
| 102 | EXPECT_TRUE(unkemptServer->returnVintfStabilityBinder(&out).isOk()); |
| 103 | EXPECT_NE(nullptr, out.get()); |
| 104 | } |
| 105 | |
| 106 | void checkLowStabilityServer(const sp<IBinderStabilityTest>& complServer) { |
| 107 | EXPECT_FALSE(complServer->sendBinder(new BBinder()).isOk()); |
| 108 | EXPECT_TRUE(complServer->sendBinder(getCompilationUnitStability()).isOk()); |
| 109 | EXPECT_TRUE(complServer->sendBinder(getVintfStability()).isOk()); |
| 110 | |
| 111 | sp<IBinder> out; |
| 112 | EXPECT_FALSE(complServer->returnNoStabilityBinder(&out).isOk()); |
| 113 | EXPECT_EQ(nullptr, out.get()); |
| 114 | |
| 115 | EXPECT_TRUE(complServer->returnLocalStabilityBinder(&out).isOk()); |
| 116 | EXPECT_NE(nullptr, out.get()); |
| 117 | |
| 118 | EXPECT_TRUE(complServer->returnVintfStabilityBinder(&out).isOk()); |
| 119 | EXPECT_NE(nullptr, out.get()); |
| 120 | } |
| 121 | |
| 122 | void checkHighStabilityServer(const sp<IBinderStabilityTest>& highStability) { |
| 123 | EXPECT_FALSE(highStability->sendBinder(new BBinder()).isOk()); |
| 124 | EXPECT_FALSE(highStability->sendBinder(getCompilationUnitStability()).isOk()); |
| 125 | EXPECT_TRUE(highStability->sendBinder(getVintfStability()).isOk()); |
| 126 | |
| 127 | sp<IBinder> out; |
| 128 | EXPECT_FALSE(highStability->returnNoStabilityBinder(&out).isOk()); |
| 129 | EXPECT_EQ(nullptr, out.get()); |
| 130 | |
| 131 | EXPECT_FALSE(highStability->returnLocalStabilityBinder(&out).isOk()); |
| 132 | EXPECT_EQ(nullptr, out.get()); |
| 133 | |
| 134 | EXPECT_TRUE(highStability->returnVintfStabilityBinder(&out).isOk()); |
| 135 | EXPECT_NE(nullptr, out.get()); |
| 136 | } |
| 137 | |
| 138 | TEST(BinderStability, LocalNoStabilityServer) { |
| 139 | // in practice, a low stability server is probably one that hasn't been rebuilt |
| 140 | // or was written by hand. |
| 141 | auto server = BadStabilityTester::getNoStabilityServer(); |
| 142 | ASSERT_NE(nullptr, IInterface::asBinder(server)->localBinder()); |
| 143 | checkNoStabilityServer(server); |
| 144 | } |
| 145 | |
| 146 | TEST(BinderStability, LocalLowStabilityServer) { |
| 147 | auto server = BadStabilityTester::getCompilationUnitStabilityServer(); |
| 148 | ASSERT_NE(nullptr, IInterface::asBinder(server)->localBinder()); |
| 149 | checkLowStabilityServer(server); |
| 150 | } |
| 151 | |
| 152 | TEST(BinderStability, LocalHighStabilityServer) { |
| 153 | auto server = BadStabilityTester::getVintfStabilityServer(); |
| 154 | ASSERT_NE(nullptr, IInterface::asBinder(server)->localBinder()); |
| 155 | checkHighStabilityServer(server); |
| 156 | } |
| 157 | |
| 158 | TEST(BinderStability, RemoteNoStabilityServer) { |
| 159 | sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kNoStabilityServer); |
| 160 | auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder); |
| 161 | |
| 162 | ASSERT_NE(nullptr, remoteServer.get()); |
| 163 | ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder()); |
| 164 | |
| 165 | checkNoStabilityServer(remoteServer); |
| 166 | } |
| 167 | |
| 168 | TEST(BinderStability, RemoteLowStabilityServer) { |
| 169 | sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kCompilationUnitServer); |
| 170 | auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder); |
| 171 | |
| 172 | ASSERT_NE(nullptr, remoteServer.get()); |
| 173 | ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder()); |
| 174 | |
| 175 | checkLowStabilityServer(remoteServer); |
| 176 | } |
| 177 | |
| 178 | TEST(BinderStability, RemoteVintfServer) { |
| 179 | sp<IBinder> remoteBinder = android::defaultServiceManager()->getService(kVintfServer); |
| 180 | auto remoteServer = interface_cast<IBinderStabilityTest>(remoteBinder); |
| 181 | |
| 182 | ASSERT_NE(nullptr, remoteServer.get()); |
| 183 | ASSERT_NE(nullptr, IInterface::asBinder(remoteServer)->remoteBinder()); |
| 184 | |
| 185 | checkHighStabilityServer(remoteServer); |
| 186 | } |
| 187 | |
| 188 | class MarksStabilityInConstructor : public BBinder { |
| 189 | public: |
| 190 | static bool gDestructed; |
| 191 | |
| 192 | MarksStabilityInConstructor() { |
| 193 | internal::Stability::markCompilationUnit(this); |
| 194 | } |
| 195 | ~MarksStabilityInConstructor() { |
| 196 | gDestructed = true; |
| 197 | } |
| 198 | }; |
| 199 | bool MarksStabilityInConstructor::gDestructed = false; |
| 200 | |
| 201 | TEST(BinderStability, MarkingObjectNoDestructTest) { |
| 202 | ASSERT_FALSE(MarksStabilityInConstructor::gDestructed); |
| 203 | |
| 204 | // best practice is to put this directly in an sp, but for this test, we |
| 205 | // want to explicitly check what happens before that happens |
| 206 | MarksStabilityInConstructor* binder = new MarksStabilityInConstructor(); |
| 207 | ASSERT_FALSE(MarksStabilityInConstructor::gDestructed); |
| 208 | |
| 209 | sp<MarksStabilityInConstructor> binderSp = binder; |
| 210 | ASSERT_FALSE(MarksStabilityInConstructor::gDestructed); |
| 211 | |
| 212 | binderSp = nullptr; |
| 213 | ASSERT_TRUE(MarksStabilityInConstructor::gDestructed); |
| 214 | } |
| 215 | |
| 216 | int main(int argc, char** argv) { |
| 217 | ::testing::InitGoogleTest(&argc, argv); |
| 218 | |
| 219 | if (fork() == 0) { |
| 220 | // child process |
| 221 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
| 222 | |
| 223 | sp<IBinder> noStability = new BadStabilityTester; |
| 224 | android::defaultServiceManager()->addService(kNoStabilityServer, noStability); |
| 225 | |
| 226 | sp<IBinder> compil = new BadStabilityTester; |
| 227 | internal::Stability::markCompilationUnit(compil.get()); |
| 228 | android::defaultServiceManager()->addService(kCompilationUnitServer, compil); |
| 229 | |
| 230 | sp<IBinder> vintf = new BadStabilityTester; |
| 231 | internal::Stability::markVintf(vintf.get()); |
| 232 | android::defaultServiceManager()->addService(kVintfServer, vintf); |
| 233 | |
| 234 | IPCThreadState::self()->joinThreadPool(true); |
| 235 | exit(1); // should not reach |
| 236 | } |
| 237 | |
| 238 | // This is not racey. Just giving these services some time to register before we call |
| 239 | // getService which sleeps for much longer... |
| 240 | usleep(10000); |
| 241 | |
| 242 | return RUN_ALL_TESTS(); |
| 243 | } |