Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [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 <gtest/gtest.h> |
| 18 | #include <gmock/gmock.h> |
| 19 | |
| 20 | #include <binder/Binder.h> |
| 21 | #include <binder/ProcessState.h> |
| 22 | #include <binder/IServiceManager.h> |
| 23 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 24 | #include "fakeservicemanager/FakeServiceManager.h" |
Pawan Wagh | 2811ae2 | 2023-08-22 00:07:03 +0000 | [diff] [blame] | 25 | #include "rust/wrappers/FakeServiceManagerWrapper.hpp" |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 26 | |
| 27 | using android::sp; |
| 28 | using android::BBinder; |
| 29 | using android::IBinder; |
| 30 | using android::OK; |
| 31 | using android::status_t; |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 32 | using android::FakeServiceManager; |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 33 | using android::String16; |
| 34 | using android::IServiceManager; |
Pawan Wagh | 2811ae2 | 2023-08-22 00:07:03 +0000 | [diff] [blame] | 35 | using android::defaultServiceManager; |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 36 | using testing::ElementsAre; |
| 37 | |
| 38 | static sp<IBinder> getBinder() { |
| 39 | class LinkableBinder : public BBinder { |
| 40 | status_t linkToDeath(const sp<DeathRecipient>&, void*, uint32_t) override { |
| 41 | // let SM linkToDeath |
| 42 | return OK; |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | return new LinkableBinder; |
| 47 | } |
| 48 | |
| 49 | TEST(AddService, HappyHappy) { |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 50 | auto sm = new FakeServiceManager(); |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 51 | EXPECT_EQ(sm->addService(String16("foo"), getBinder(), false /*allowIsolated*/, |
| 52 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK); |
| 53 | } |
| 54 | |
Devin Moore | 5f6ded8 | 2022-11-18 18:36:30 +0000 | [diff] [blame] | 55 | TEST(AddService, SadNullBinder) { |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 56 | auto sm = new FakeServiceManager(); |
Devin Moore | 5f6ded8 | 2022-11-18 18:36:30 +0000 | [diff] [blame] | 57 | EXPECT_EQ(sm->addService(String16("foo"), nullptr, false /*allowIsolated*/, |
| 58 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), android::UNEXPECTED_NULL); |
| 59 | } |
| 60 | |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 61 | TEST(AddService, HappyOverExistingService) { |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 62 | auto sm = new FakeServiceManager(); |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 63 | EXPECT_EQ(sm->addService(String16("foo"), getBinder(), false /*allowIsolated*/, |
| 64 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK); |
| 65 | EXPECT_EQ(sm->addService(String16("foo"), getBinder(), false /*allowIsolated*/, |
| 66 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK); |
| 67 | } |
| 68 | |
Devin Moore | 8cc776a | 2022-11-18 18:43:53 +0000 | [diff] [blame] | 69 | TEST(AddService, HappyClearAddedService) { |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 70 | auto sm = new FakeServiceManager(); |
Devin Moore | 8cc776a | 2022-11-18 18:43:53 +0000 | [diff] [blame] | 71 | EXPECT_EQ(sm->addService(String16("foo"), getBinder(), false /*allowIsolated*/, |
| 72 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK); |
| 73 | EXPECT_NE(sm->getService(String16("foo")), nullptr); |
| 74 | sm->clear(); |
| 75 | EXPECT_EQ(sm->getService(String16("foo")), nullptr); |
| 76 | } |
| 77 | |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 78 | TEST(GetService, HappyHappy) { |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 79 | auto sm = new FakeServiceManager(); |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 80 | sp<IBinder> service = getBinder(); |
| 81 | |
| 82 | EXPECT_EQ(sm->addService(String16("foo"), service, false /*allowIsolated*/, |
| 83 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK); |
| 84 | |
| 85 | EXPECT_EQ(sm->getService(String16("foo")), service); |
| 86 | } |
| 87 | |
Pawan Wagh | 2811ae2 | 2023-08-22 00:07:03 +0000 | [diff] [blame] | 88 | TEST(GetService, NonExistent) { |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 89 | auto sm = new FakeServiceManager(); |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 90 | |
| 91 | EXPECT_EQ(sm->getService(String16("foo")), nullptr); |
| 92 | } |
| 93 | |
| 94 | TEST(ListServices, AllServices) { |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 95 | auto sm = new FakeServiceManager(); |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 96 | |
| 97 | EXPECT_EQ(sm->addService(String16("sd"), getBinder(), false /*allowIsolated*/, |
| 98 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK); |
| 99 | EXPECT_EQ(sm->addService(String16("sc"), getBinder(), false /*allowIsolated*/, |
| 100 | IServiceManager::DUMP_FLAG_PRIORITY_NORMAL), OK); |
| 101 | EXPECT_EQ(sm->addService(String16("sb"), getBinder(), false /*allowIsolated*/, |
| 102 | IServiceManager::DUMP_FLAG_PRIORITY_HIGH), OK); |
| 103 | EXPECT_EQ(sm->addService(String16("sa"), getBinder(), false /*allowIsolated*/, |
| 104 | IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL), OK); |
| 105 | |
| 106 | android::Vector<String16> out = sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL); |
| 107 | |
| 108 | // all there and in the right order |
| 109 | EXPECT_THAT(out, ElementsAre(String16("sa"), String16("sb"), String16("sc"), |
| 110 | String16("sd"))); |
| 111 | } |
| 112 | |
Pawan Wagh | 2811ae2 | 2023-08-22 00:07:03 +0000 | [diff] [blame] | 113 | TEST(WaitForService, NonExistent) { |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 114 | auto sm = new FakeServiceManager(); |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 115 | |
| 116 | EXPECT_EQ(sm->waitForService(String16("foo")), nullptr); |
| 117 | } |
| 118 | |
| 119 | TEST(WaitForService, HappyHappy) { |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 120 | auto sm = new FakeServiceManager(); |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 121 | sp<IBinder> service = getBinder(); |
| 122 | |
| 123 | EXPECT_EQ(sm->addService(String16("foo"), service, false /*allowIsolated*/, |
| 124 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK); |
| 125 | |
| 126 | EXPECT_EQ(sm->waitForService(String16("foo")), service); |
| 127 | } |
| 128 | |
Pawan Wagh | 2811ae2 | 2023-08-22 00:07:03 +0000 | [diff] [blame] | 129 | TEST(IsDeclared, NonExistent) { |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 130 | auto sm = new FakeServiceManager(); |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 131 | |
| 132 | EXPECT_FALSE(sm->isDeclared(String16("foo"))); |
| 133 | } |
| 134 | |
| 135 | TEST(IsDeclared, HappyHappy) { |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 136 | auto sm = new FakeServiceManager(); |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 137 | sp<IBinder> service = getBinder(); |
| 138 | |
| 139 | EXPECT_EQ(sm->addService(String16("foo"), service, false /*allowIsolated*/, |
| 140 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK); |
| 141 | |
| 142 | EXPECT_TRUE(sm->isDeclared(String16("foo"))); |
| 143 | } |
Pawan Wagh | 2811ae2 | 2023-08-22 00:07:03 +0000 | [diff] [blame] | 144 | |
| 145 | TEST(SetupFakeServiceManager, NonExistent) { |
| 146 | setupFakeServiceManager(); |
| 147 | |
| 148 | EXPECT_EQ(defaultServiceManager()->getService(String16("foo")), nullptr); |
| 149 | } |
| 150 | |
| 151 | TEST(SetupFakeServiceManager, GetExistingService) { |
| 152 | setupFakeServiceManager(); |
| 153 | sp<IBinder> service = getBinder(); |
| 154 | |
| 155 | EXPECT_EQ(defaultServiceManager()->addService(String16("foo"), service, false /*allowIsolated*/, |
| 156 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK); |
| 157 | |
| 158 | EXPECT_EQ(defaultServiceManager()->getService(String16("foo")), service); |
| 159 | clearFakeServiceManager(); |
| 160 | } |
| 161 | |
| 162 | TEST(ClearFakeServiceManager, GetServiceAfterClear) { |
| 163 | setupFakeServiceManager(); |
| 164 | |
| 165 | sp<IBinder> service = getBinder(); |
| 166 | EXPECT_EQ(defaultServiceManager()->addService(String16("foo"), service, false /*allowIsolated*/, |
| 167 | IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT), OK); |
| 168 | |
| 169 | clearFakeServiceManager(); |
| 170 | EXPECT_EQ(defaultServiceManager()->getService(String16("foo")), nullptr); |
| 171 | } |