blob: 812d5cacd54361940efd3ee06a5a42e7b2bfe672 [file] [log] [blame]
Steven Moreland80e1e6d2019-06-21 12:35:59 -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/ProcessState.h>
18#include <cutils/android_filesystem_config.h>
19#include <gtest/gtest.h>
20#include <gmock/gmock.h>
21
22#include "Access.h"
23#include "ServiceManager.h"
24
25using android::sp;
26using android::Access;
27using android::IBinder;
28using android::ServiceManager;
29using android::os::IServiceManager;
30using testing::_;
31using testing::ElementsAre;
32using testing::NiceMock;
33using testing::Return;
34
35static sp<IBinder> getBinder() {
36 // It doesn't matter what remote binder it is, we just need one so that linkToDeath will work.
37 // The context manager (servicemanager) is easy to get and is in another process.
38 return android::ProcessState::self()->getContextObject(nullptr);
39}
40
41class MockAccess : public Access {
42public:
43 MOCK_METHOD1(getCallingContext, CallingContext(const std::string& name));
44 MOCK_METHOD1(canAdd, bool(const CallingContext&));
45 MOCK_METHOD1(canFind, bool(const CallingContext&));
46 MOCK_METHOD1(canList, bool(const CallingContext&));
47};
48
49static sp<ServiceManager> getPermissiveServiceManager() {
50 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
51
52 ON_CALL(*access, getCallingContext(_)).WillByDefault(Return(Access::CallingContext{}));
53 ON_CALL(*access, canAdd(_)).WillByDefault(Return(true));
54 ON_CALL(*access, canFind(_)).WillByDefault(Return(true));
55 ON_CALL(*access, canList(_)).WillByDefault(Return(true));
56
57 sp<ServiceManager> sm = new ServiceManager(std::move(access));
58 return sm;
59}
60
61TEST(AddService, HappyHappy) {
62 auto sm = getPermissiveServiceManager();
63 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
64 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
65}
66
67TEST(AddService, EmptyNameDisallowed) {
68 auto sm = getPermissiveServiceManager();
69 EXPECT_FALSE(sm->addService("", getBinder(), false /*allowIsolated*/,
70 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
71}
72
73TEST(AddService, JustShortEnoughServiceNameHappy) {
74 auto sm = getPermissiveServiceManager();
75 EXPECT_TRUE(sm->addService(std::string(127, 'a'), getBinder(), false /*allowIsolated*/,
76 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
77}
78
79TEST(AddService, TooLongNameDisallowed) {
80 auto sm = getPermissiveServiceManager();
81 EXPECT_FALSE(sm->addService(std::string(128, 'a'), getBinder(), false /*allowIsolated*/,
82 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
83}
84
85TEST(AddService, AddNullServiceDisallowed) {
86 auto sm = getPermissiveServiceManager();
87 EXPECT_FALSE(sm->addService("foo", nullptr, false /*allowIsolated*/,
88 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
89}
90
91TEST(AddService, AddDisallowedFromApp) {
92 for (uid_t uid : { AID_APP_START, AID_APP_START + 1, AID_APP_END }) {
93 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
94 EXPECT_CALL(*access, getCallingContext(_)).WillOnce(Return(Access::CallingContext{
95 .debugPid = 1337,
96 .uid = uid,
97 }));
98 EXPECT_CALL(*access, canAdd(_)).Times(0);
99 sp<ServiceManager> sm = new ServiceManager(std::move(access));
100
101 EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
102 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
103 }
104
105}
106
107TEST(AddService, HappyOverExistingService) {
108 auto sm = getPermissiveServiceManager();
109 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
110 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
111 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
112 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
113}
114
115TEST(AddService, NoPermissions) {
116 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
117
118 EXPECT_CALL(*access, getCallingContext(_)).WillOnce(Return(Access::CallingContext{}));
119 EXPECT_CALL(*access, canAdd(_)).WillOnce(Return(false));
120
121 sp<ServiceManager> sm = new ServiceManager(std::move(access));
122
123 EXPECT_FALSE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
124 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
125}
126
127TEST(GetService, HappyHappy) {
128 auto sm = getPermissiveServiceManager();
129 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
130 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
131
132 sp<IBinder> out;
133 EXPECT_TRUE(sm->getService("foo", &out).isOk());
134 EXPECT_EQ(getBinder(), out);
135}
136
137TEST(GetService, NonExistant) {
138 auto sm = getPermissiveServiceManager();
139
140 sp<IBinder> out;
141 EXPECT_TRUE(sm->getService("foo", &out).isOk());
142 EXPECT_EQ(nullptr, out.get());
143}
144
145TEST(GetService, NoPermissionsForGettingService) {
146 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
147
148 EXPECT_CALL(*access, getCallingContext(_)).WillRepeatedly(Return(Access::CallingContext{}));
149 EXPECT_CALL(*access, canAdd(_)).WillOnce(Return(true));
150 EXPECT_CALL(*access, canFind(_)).WillOnce(Return(false));
151
152 sp<ServiceManager> sm = new ServiceManager(std::move(access));
153
154 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
155 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
156
157 sp<IBinder> out;
158 // returns nullptr but has OK status for legacy compatibility
159 EXPECT_TRUE(sm->getService("foo", &out).isOk());
160 EXPECT_EQ(nullptr, out.get());
161}
162
163TEST(GetService, AllowedFromIsolated) {
164 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
165
166 EXPECT_CALL(*access, getCallingContext(_))
167 // something adds it
168 .WillOnce(Return(Access::CallingContext{}))
169 // next call is from isolated app
170 .WillOnce(Return(Access::CallingContext{
171 .uid = AID_ISOLATED_START,
172 }));
173 EXPECT_CALL(*access, canAdd(_)).WillOnce(Return(true));
174 EXPECT_CALL(*access, canFind(_)).WillOnce(Return(true));
175
176 sp<ServiceManager> sm = new ServiceManager(std::move(access));
177
178 EXPECT_TRUE(sm->addService("foo", getBinder(), true /*allowIsolated*/,
179 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
180
181 sp<IBinder> out;
182 EXPECT_TRUE(sm->getService("foo", &out).isOk());
183 EXPECT_EQ(getBinder(), out.get());
184}
185
186TEST(GetService, NotAllowedFromIsolated) {
187 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
188
189 EXPECT_CALL(*access, getCallingContext(_))
190 // something adds it
191 .WillOnce(Return(Access::CallingContext{}))
192 // next call is from isolated app
193 .WillOnce(Return(Access::CallingContext{
194 .uid = AID_ISOLATED_START,
195 }));
196 EXPECT_CALL(*access, canAdd(_)).WillOnce(Return(true));
197
198 // TODO(b/136023468): when security check is first, this should be called first
199 // EXPECT_CALL(*access, canFind(_)).WillOnce(Return(true));
200
201 sp<ServiceManager> sm = new ServiceManager(std::move(access));
202
203 EXPECT_TRUE(sm->addService("foo", getBinder(), false /*allowIsolated*/,
204 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
205
206 sp<IBinder> out;
207 // returns nullptr but has OK status for legacy compatibility
208 EXPECT_TRUE(sm->getService("foo", &out).isOk());
209 EXPECT_EQ(nullptr, out.get());
210}
211
212TEST(ListServices, NoPermissions) {
213 std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>();
214
215 EXPECT_CALL(*access, getCallingContext(_)).WillOnce(Return(Access::CallingContext{}));
216 EXPECT_CALL(*access, canList(_)).WillOnce(Return(false));
217
218 sp<ServiceManager> sm = new ServiceManager(std::move(access));
219
220 std::vector<std::string> out;
221 EXPECT_FALSE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL, &out).isOk());
222 EXPECT_TRUE(out.empty());
223}
224
225TEST(ListServices, AllServices) {
226 auto sm = getPermissiveServiceManager();
227
228 EXPECT_TRUE(sm->addService("sd", getBinder(), false /*allowIsolated*/,
229 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
230 EXPECT_TRUE(sm->addService("sc", getBinder(), false /*allowIsolated*/,
231 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL).isOk());
232 EXPECT_TRUE(sm->addService("sb", getBinder(), false /*allowIsolated*/,
233 IServiceManager::DUMP_FLAG_PRIORITY_HIGH).isOk());
234 EXPECT_TRUE(sm->addService("sa", getBinder(), false /*allowIsolated*/,
235 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL).isOk());
236
237 std::vector<std::string> out;
238 EXPECT_TRUE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL, &out).isOk());
239
240 // all there and in the right order
241 EXPECT_THAT(out, ElementsAre("sa", "sb", "sc", "sd"));
242}
243
244TEST(ListServices, CriticalServices) {
245 auto sm = getPermissiveServiceManager();
246
247 EXPECT_TRUE(sm->addService("sd", getBinder(), false /*allowIsolated*/,
248 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk());
249 EXPECT_TRUE(sm->addService("sc", getBinder(), false /*allowIsolated*/,
250 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL).isOk());
251 EXPECT_TRUE(sm->addService("sb", getBinder(), false /*allowIsolated*/,
252 IServiceManager::DUMP_FLAG_PRIORITY_HIGH).isOk());
253 EXPECT_TRUE(sm->addService("sa", getBinder(), false /*allowIsolated*/,
254 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL).isOk());
255
256 std::vector<std::string> out;
257 EXPECT_TRUE(sm->listServices(IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL, &out).isOk());
258
259 // all there and in the right order
260 EXPECT_THAT(out, ElementsAre("sa"));
261}