blob: a596a67403f6d6cdd3d3fc533d452c0faecf45bf [file] [log] [blame]
Felipe Leme343175a2016-08-02 18:57:37 -07001/*
2 * Copyright (C) 2016 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 "../dumpsys.h"
18
19#include <vector>
20
21#include <gmock/gmock.h>
22#include <gtest/gtest.h>
23
24#include <android-base/file.h>
25#include <utils/String16.h>
26#include <utils/Vector.h>
27
28using namespace android;
29
30using ::testing::_;
31using ::testing::Action;
32using ::testing::ActionInterface;
33using ::testing::DoAll;
34using ::testing::Eq;
35using ::testing::HasSubstr;
36using ::testing::MakeAction;
37using ::testing::Not;
38using ::testing::Return;
39using ::testing::StrEq;
40using ::testing::Test;
41using ::testing::WithArg;
42using ::testing::internal::CaptureStderr;
43using ::testing::internal::CaptureStdout;
44using ::testing::internal::GetCapturedStderr;
45using ::testing::internal::GetCapturedStdout;
46
Steven Moreland6270dd12017-01-20 15:24:51 -080047using android::hardware::hidl_vec;
48using android::hardware::hidl_string;
49using android::hardware::Void;
50using HServiceManager = android::hidl::manager::V1_0::IServiceManager;
51using IServiceNotification = android::hidl::manager::V1_0::IServiceNotification;
52
Felipe Leme343175a2016-08-02 18:57:37 -070053class ServiceManagerMock : public IServiceManager {
54 public:
55 MOCK_CONST_METHOD1(getService, sp<IBinder>(const String16&));
56 MOCK_CONST_METHOD1(checkService, sp<IBinder>(const String16&));
57 MOCK_METHOD3(addService, status_t(const String16&, const sp<IBinder>&, bool));
58 MOCK_METHOD0(listServices, Vector<String16>());
59
60 protected:
61 MOCK_METHOD0(onAsBinder, IBinder*());
62};
63
Steven Moreland6270dd12017-01-20 15:24:51 -080064class HardwareServiceManagerMock : public HServiceManager {
65 public:
66 template<typename T>
67 using R = android::hardware::Return<T>; // conflicts with ::testing::Return
68
69 MOCK_METHOD2(get, R<sp<IBase>>(const hidl_string&, const hidl_string&));
70 MOCK_METHOD3(add,
71 R<bool>(const hidl_vec<hidl_string>&,
72 const hidl_string&,
73 const sp<IBase>&));
74 MOCK_METHOD1(list, R<void>(list_cb));
75 MOCK_METHOD2(listByInterface,
76 R<void>(const hidl_string&, listByInterface_cb));
77 MOCK_METHOD3(registerForNotifications,
78 R<bool>(const hidl_string&,
79 const hidl_string&,
80 const sp<IServiceNotification>&));
Yifan Hong5ccdab92017-01-25 22:53:37 -080081 MOCK_METHOD1(debugDump, R<void>(debugDump_cb));
Yifan Hongb7ddc9e2017-02-03 15:23:47 -080082 MOCK_METHOD3(registerPassthroughClient, R<void>(
83 const hidl_string&, const hidl_string&, int32_t));
Steven Moreland6270dd12017-01-20 15:24:51 -080084
85};
86
Felipe Leme343175a2016-08-02 18:57:37 -070087class BinderMock : public BBinder {
88 public:
89 BinderMock() {
90 }
91
92 MOCK_METHOD2(dump, status_t(int, const Vector<String16>&));
93};
94
95// gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher
96typedef void WriteOnFdFunction(int);
97
98class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
99 public:
100 explicit WriteOnFdAction(const std::string& output) : output_(output) {
101 }
102 virtual Result Perform(const ArgumentTuple& args) {
103 int fd = ::std::tr1::get<0>(args);
104 android::base::WriteStringToFd(output_, fd);
105 }
106
107 private:
108 std::string output_;
109};
110
111// Matcher used to emulate dump() by writing on its file descriptor.
112Action<WriteOnFdFunction> WriteOnFd(const std::string& output) {
113 return MakeAction(new WriteOnFdAction(output));
114}
115
Steven Moreland6270dd12017-01-20 15:24:51 -0800116// gmock black magic to provide a WithArg<0>(List(services)) matcher
117typedef void HardwareListFunction(HServiceManager::list_cb);
118
119class HardwareListAction : public ActionInterface<HardwareListFunction> {
120 public:
121 explicit HardwareListAction(const hidl_vec<hidl_string> &services) : services_(services) {
122 }
123 virtual Result Perform(const ArgumentTuple& args) {
124 auto cb = ::std::tr1::get<0>(args);
125 cb(services_);
126 }
127
128 private:
129 hidl_vec<hidl_string> services_;
130};
131
132Action<HardwareListFunction> HardwareList(const hidl_vec<hidl_string> &services) {
133 return MakeAction(new HardwareListAction(services));
134}
135
Felipe Leme343175a2016-08-02 18:57:37 -0700136// Matcher for args using Android's Vector<String16> format
137// TODO: move it to some common testing library
138MATCHER_P(AndroidElementsAre, expected, "") {
139 std::ostringstream errors;
140 if (arg.size() != expected.size()) {
141 errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size()
142 << ")\n";
143 }
144 int i = 0;
145 std::ostringstream actual_stream, expected_stream;
146 for (String16 actual : arg) {
147 std::string actual_str = String16::std_string(actual);
148 std::string expected_str = expected[i];
149 actual_stream << "'" << actual_str << "' ";
150 expected_stream << "'" << expected_str << "' ";
151 if (actual_str != expected_str) {
152 errors << " element mismatch at index " << i << "\n";
153 }
154 i++;
155 }
156
157 if (!errors.str().empty()) {
158 errors << "\nExpected args: " << expected_stream.str()
159 << "\nActual args: " << actual_stream.str();
160 *result_listener << errors.str();
161 return false;
162 }
163 return true;
164}
165
166// Custom action to sleep for timeout seconds
167ACTION_P(Sleep, timeout) {
168 sleep(timeout);
169}
170
171class DumpsysTest : public Test {
172 public:
Steven Moreland6270dd12017-01-20 15:24:51 -0800173 DumpsysTest() : sm_(), hm_(), dump_(&sm_, &hm_), stdout_(), stderr_() {
Felipe Leme343175a2016-08-02 18:57:37 -0700174 }
175
176 void ExpectListServices(std::vector<std::string> services) {
177 Vector<String16> services16;
178 for (auto& service : services) {
179 services16.add(String16(service.c_str()));
180 }
Steven Moreland6270dd12017-01-20 15:24:51 -0800181
Felipe Leme343175a2016-08-02 18:57:37 -0700182 EXPECT_CALL(sm_, listServices()).WillRepeatedly(Return(services16));
183 }
184
Steven Moreland6270dd12017-01-20 15:24:51 -0800185 void ExpectListHardwareServices(std::vector<std::string> services) {
186 hidl_vec<hidl_string> hidl_services;
187 hidl_services.resize(services.size());
188 for (size_t i = 0; i < services.size(); i++) {
189 hidl_services[i] = services[i];
190 }
191
192 EXPECT_CALL(hm_, list(_)).WillRepeatedly(DoAll(
193 WithArg<0>(HardwareList(hidl_services)),
194 Return(Void())));
195 }
196
Felipe Leme343175a2016-08-02 18:57:37 -0700197 sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
198 sp<BinderMock> binder_mock;
199 if (running) {
200 binder_mock = new BinderMock;
201 }
202 EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
203 return binder_mock;
204 }
205
206 void ExpectDump(const char* name, const std::string& output) {
207 sp<BinderMock> binder_mock = ExpectCheckService(name);
208 EXPECT_CALL(*binder_mock, dump(_, _))
209 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
210 }
211
212 void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
213 const std::string& output) {
214 sp<BinderMock> binder_mock = ExpectCheckService(name);
215 EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
216 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
217 }
218
219 void ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
220 sp<BinderMock> binder_mock = ExpectCheckService(name);
221 EXPECT_CALL(*binder_mock, dump(_, _))
222 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
223 }
224
225 void CallMain(const std::vector<std::string>& args) {
226 const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
227 int argc = (int)args.size() + 1;
228 int i = 1;
229 for (const std::string& arg : args) {
230 argv[i++] = arg.c_str();
231 }
232 CaptureStdout();
233 CaptureStderr();
234 int status = dump_.main(argc, const_cast<char**>(argv));
235 stdout_ = GetCapturedStdout();
236 stderr_ = GetCapturedStderr();
237 EXPECT_THAT(status, Eq(0));
238 }
239
Steven Moreland6270dd12017-01-20 15:24:51 -0800240 void AssertRunningServices(const std::vector<std::string>& services,
241 const std::string &message = "Currently running services:") {
242 std::string expected(message);
243 expected.append("\n");
Felipe Leme343175a2016-08-02 18:57:37 -0700244 for (const std::string& service : services) {
245 expected.append(" ").append(service).append("\n");
246 }
247 EXPECT_THAT(stdout_, HasSubstr(expected));
248 }
249
250 void AssertOutput(const std::string& expected) {
251 EXPECT_THAT(stdout_, StrEq(expected));
252 }
253
254 void AssertOutputContains(const std::string& expected) {
255 EXPECT_THAT(stdout_, HasSubstr(expected));
256 }
257
258 void AssertDumped(const std::string& service, const std::string& dump) {
259 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
260 }
261
262 void AssertNotDumped(const std::string& dump) {
263 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
264 }
265
266 void AssertStopped(const std::string& service) {
267 EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
268 }
269
270 ServiceManagerMock sm_;
Steven Moreland6270dd12017-01-20 15:24:51 -0800271 HardwareServiceManagerMock hm_;
Felipe Leme343175a2016-08-02 18:57:37 -0700272 Dumpsys dump_;
273
274 private:
275 std::string stdout_, stderr_;
276};
277
Steven Moreland6270dd12017-01-20 15:24:51 -0800278TEST_F(DumpsysTest, ListHwServices) {
279 ExpectListHardwareServices({"Locksmith", "Valet"});
280
281 CallMain({"--hw"});
282
283 AssertRunningServices({"Locksmith", "Valet"}, "Currently running hardware services:");
284}
285
Felipe Leme343175a2016-08-02 18:57:37 -0700286// Tests 'dumpsys -l' when all services are running
287TEST_F(DumpsysTest, ListAllServices) {
288 ExpectListServices({"Locksmith", "Valet"});
289 ExpectCheckService("Locksmith");
290 ExpectCheckService("Valet");
291
292 CallMain({"-l"});
293
294 AssertRunningServices({"Locksmith", "Valet"});
295}
296
297// Tests 'dumpsys -l' when a service is not running
298TEST_F(DumpsysTest, ListRunningServices) {
299 ExpectListServices({"Locksmith", "Valet"});
300 ExpectCheckService("Locksmith");
301 ExpectCheckService("Valet", false);
302
303 CallMain({"-l"});
304
305 AssertRunningServices({"Locksmith"});
306 AssertNotDumped({"Valet"});
307}
308
309// Tests 'dumpsys service_name' on a service is running
310TEST_F(DumpsysTest, DumpRunningService) {
311 ExpectDump("Valet", "Here's your car");
312
313 CallMain({"Valet"});
314
315 AssertOutput("Here's your car");
316}
317
318// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
319TEST_F(DumpsysTest, DumpRunningServiceTimeout) {
320 ExpectDumpAndHang("Valet", 2, "Here's your car");
321
322 CallMain({"-t", "1", "Valet"});
323
324 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1s) EXPIRED");
325 AssertNotDumped("Here's your car");
326
327 // Must wait so binder mock is deleted, otherwise test will fail with a leaked object
328 sleep(1);
329}
330
331// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
332TEST_F(DumpsysTest, DumpWithArgsRunningService) {
333 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
334
335 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
336
337 AssertOutput("I DO!");
338}
339
340// Tests 'dumpsys' with no arguments
341TEST_F(DumpsysTest, DumpMultipleServices) {
342 ExpectListServices({"running1", "stopped2", "running3"});
343 ExpectDump("running1", "dump1");
344 ExpectCheckService("stopped2", false);
345 ExpectDump("running3", "dump3");
346
347 CallMain({});
348
349 AssertRunningServices({"running1", "running3"});
350 AssertDumped("running1", "dump1");
351 AssertStopped("stopped2");
352 AssertDumped("running3", "dump3");
353}
354
355// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
356TEST_F(DumpsysTest, DumpWithSkip) {
357 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
358 ExpectDump("running1", "dump1");
359 ExpectCheckService("stopped2", false);
360 ExpectDump("skipped3", "dump3");
361 ExpectDump("running4", "dump4");
362 ExpectDump("skipped5", "dump5");
363
364 CallMain({"--skip", "skipped3", "skipped5"});
365
366 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
367 AssertDumped("running1", "dump1");
368 AssertDumped("running4", "dump4");
369 AssertStopped("stopped2");
370 AssertNotDumped("dump3");
371 AssertNotDumped("dump5");
372}