blob: bdb0a9ad8fe20e49a55cf1c05d0ca18e4a06c2b6 [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>
Vishnu Nair6a408532017-10-24 09:11:27 -070025#include <serviceutils/PriorityDumper.h>
Felipe Leme343175a2016-08-02 18:57:37 -070026#include <utils/String16.h>
Steven Morelanda0f7f2d2017-03-09 22:59:32 -080027#include <utils/String8.h>
Felipe Leme343175a2016-08-02 18:57:37 -070028#include <utils/Vector.h>
29
30using namespace android;
31
32using ::testing::_;
33using ::testing::Action;
34using ::testing::ActionInterface;
35using ::testing::DoAll;
36using ::testing::Eq;
37using ::testing::HasSubstr;
38using ::testing::MakeAction;
Felipe Leme5c8a98f2017-08-25 13:39:04 -070039using ::testing::Mock;
Felipe Leme343175a2016-08-02 18:57:37 -070040using ::testing::Not;
41using ::testing::Return;
42using ::testing::StrEq;
43using ::testing::Test;
44using ::testing::WithArg;
45using ::testing::internal::CaptureStderr;
46using ::testing::internal::CaptureStdout;
47using ::testing::internal::GetCapturedStderr;
48using ::testing::internal::GetCapturedStdout;
49
50class ServiceManagerMock : public IServiceManager {
51 public:
52 MOCK_CONST_METHOD1(getService, sp<IBinder>(const String16&));
53 MOCK_CONST_METHOD1(checkService, sp<IBinder>(const String16&));
Vishnu Nairf56042d2017-09-19 15:25:10 -070054 MOCK_METHOD4(addService, status_t(const String16&, const sp<IBinder>&, bool, int));
55 MOCK_METHOD1(listServices, Vector<String16>(int));
Felipe Leme343175a2016-08-02 18:57:37 -070056
57 protected:
58 MOCK_METHOD0(onAsBinder, IBinder*());
59};
60
61class BinderMock : public BBinder {
62 public:
63 BinderMock() {
64 }
65
66 MOCK_METHOD2(dump, status_t(int, const Vector<String16>&));
67};
68
69// gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher
70typedef void WriteOnFdFunction(int);
71
72class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
73 public:
74 explicit WriteOnFdAction(const std::string& output) : output_(output) {
75 }
76 virtual Result Perform(const ArgumentTuple& args) {
77 int fd = ::std::tr1::get<0>(args);
78 android::base::WriteStringToFd(output_, fd);
79 }
80
81 private:
82 std::string output_;
83};
84
85// Matcher used to emulate dump() by writing on its file descriptor.
86Action<WriteOnFdFunction> WriteOnFd(const std::string& output) {
87 return MakeAction(new WriteOnFdAction(output));
88}
89
90// Matcher for args using Android's Vector<String16> format
91// TODO: move it to some common testing library
92MATCHER_P(AndroidElementsAre, expected, "") {
93 std::ostringstream errors;
94 if (arg.size() != expected.size()) {
95 errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size()
96 << ")\n";
97 }
98 int i = 0;
99 std::ostringstream actual_stream, expected_stream;
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700100 for (const String16& actual : arg) {
Steven Morelanda0f7f2d2017-03-09 22:59:32 -0800101 std::string actual_str = String8(actual).c_str();
Felipe Leme343175a2016-08-02 18:57:37 -0700102 std::string expected_str = expected[i];
103 actual_stream << "'" << actual_str << "' ";
104 expected_stream << "'" << expected_str << "' ";
105 if (actual_str != expected_str) {
106 errors << " element mismatch at index " << i << "\n";
107 }
108 i++;
109 }
110
111 if (!errors.str().empty()) {
112 errors << "\nExpected args: " << expected_stream.str()
113 << "\nActual args: " << actual_stream.str();
114 *result_listener << errors.str();
115 return false;
116 }
117 return true;
118}
119
120// Custom action to sleep for timeout seconds
121ACTION_P(Sleep, timeout) {
122 sleep(timeout);
123}
124
125class DumpsysTest : public Test {
126 public:
Steven Moreland2c3cd832017-02-13 23:44:17 +0000127 DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() {
Felipe Leme343175a2016-08-02 18:57:37 -0700128 }
129
130 void ExpectListServices(std::vector<std::string> services) {
131 Vector<String16> services16;
132 for (auto& service : services) {
133 services16.add(String16(service.c_str()));
134 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700135 EXPECT_CALL(sm_, listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL))
Vishnu Nairf56042d2017-09-19 15:25:10 -0700136 .WillRepeatedly(Return(services16));
137 }
138
Vishnu Nair6a408532017-10-24 09:11:27 -0700139 void ExpectListServicesWithPriority(std::vector<std::string> services, int dumpFlags) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700140 Vector<String16> services16;
141 for (auto& service : services) {
142 services16.add(String16(service.c_str()));
143 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700144 EXPECT_CALL(sm_, listServices(dumpFlags)).WillRepeatedly(Return(services16));
Felipe Leme343175a2016-08-02 18:57:37 -0700145 }
146
147 sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
148 sp<BinderMock> binder_mock;
149 if (running) {
150 binder_mock = new BinderMock;
151 }
152 EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
153 return binder_mock;
154 }
155
156 void ExpectDump(const char* name, const std::string& output) {
157 sp<BinderMock> binder_mock = ExpectCheckService(name);
158 EXPECT_CALL(*binder_mock, dump(_, _))
159 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
160 }
161
162 void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
163 const std::string& output) {
164 sp<BinderMock> binder_mock = ExpectCheckService(name);
165 EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
166 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
167 }
168
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700169 sp<BinderMock> ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
Felipe Leme343175a2016-08-02 18:57:37 -0700170 sp<BinderMock> binder_mock = ExpectCheckService(name);
171 EXPECT_CALL(*binder_mock, dump(_, _))
172 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700173 return binder_mock;
Felipe Leme343175a2016-08-02 18:57:37 -0700174 }
175
176 void CallMain(const std::vector<std::string>& args) {
177 const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
178 int argc = (int)args.size() + 1;
179 int i = 1;
180 for (const std::string& arg : args) {
181 argv[i++] = arg.c_str();
182 }
183 CaptureStdout();
184 CaptureStderr();
185 int status = dump_.main(argc, const_cast<char**>(argv));
186 stdout_ = GetCapturedStdout();
187 stderr_ = GetCapturedStderr();
188 EXPECT_THAT(status, Eq(0));
189 }
190
Steven Moreland2c3cd832017-02-13 23:44:17 +0000191 void AssertRunningServices(const std::vector<std::string>& services) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700192 std::string expected;
193 if (services.size() > 1) {
194 expected.append("Currently running services:\n");
195 }
Felipe Leme343175a2016-08-02 18:57:37 -0700196 for (const std::string& service : services) {
197 expected.append(" ").append(service).append("\n");
198 }
199 EXPECT_THAT(stdout_, HasSubstr(expected));
200 }
201
202 void AssertOutput(const std::string& expected) {
203 EXPECT_THAT(stdout_, StrEq(expected));
204 }
205
206 void AssertOutputContains(const std::string& expected) {
207 EXPECT_THAT(stdout_, HasSubstr(expected));
208 }
209
210 void AssertDumped(const std::string& service, const std::string& dump) {
211 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
212 }
213
Vishnu Nair6a408532017-10-24 09:11:27 -0700214 void AssertDumpedWithPriority(const std::string& service, const std::string& dump,
215 const char16_t* priorityType) {
216 std::string priority = String8(priorityType).c_str();
217 EXPECT_THAT(stdout_,
218 HasSubstr("DUMP OF SERVICE " + priority + " " + service + ":\n" + dump));
219 }
220
Felipe Leme343175a2016-08-02 18:57:37 -0700221 void AssertNotDumped(const std::string& dump) {
222 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
223 }
224
225 void AssertStopped(const std::string& service) {
226 EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
227 }
228
229 ServiceManagerMock sm_;
230 Dumpsys dump_;
231
232 private:
233 std::string stdout_, stderr_;
234};
235
Felipe Leme343175a2016-08-02 18:57:37 -0700236// Tests 'dumpsys -l' when all services are running
237TEST_F(DumpsysTest, ListAllServices) {
238 ExpectListServices({"Locksmith", "Valet"});
239 ExpectCheckService("Locksmith");
240 ExpectCheckService("Valet");
241
242 CallMain({"-l"});
243
244 AssertRunningServices({"Locksmith", "Valet"});
245}
246
247// Tests 'dumpsys -l' when a service is not running
248TEST_F(DumpsysTest, ListRunningServices) {
249 ExpectListServices({"Locksmith", "Valet"});
250 ExpectCheckService("Locksmith");
251 ExpectCheckService("Valet", false);
252
253 CallMain({"-l"});
254
255 AssertRunningServices({"Locksmith"});
256 AssertNotDumped({"Valet"});
257}
258
Vishnu Nairf56042d2017-09-19 15:25:10 -0700259// Tests 'dumpsys -l --priority HIGH'
260TEST_F(DumpsysTest, ListAllServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700261 ExpectListServicesWithPriority({"Locksmith", "Valet"}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700262 ExpectCheckService("Locksmith");
263 ExpectCheckService("Valet");
264
265 CallMain({"-l", "--priority", "HIGH"});
266
267 AssertRunningServices({"Locksmith", "Valet"});
268}
269
270// Tests 'dumpsys -l --priority HIGH' with and empty list
271TEST_F(DumpsysTest, ListEmptyServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700272 ExpectListServicesWithPriority({}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700273
274 CallMain({"-l", "--priority", "HIGH"});
275
276 AssertRunningServices({});
277}
278
Vishnu Nair6a408532017-10-24 09:11:27 -0700279// Tests 'dumpsys -l --proto'
280TEST_F(DumpsysTest, ListAllServicesWithProto) {
281 ExpectListServicesWithPriority({"Locksmith", "Valet", "Car"},
282 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
283 ExpectListServicesWithPriority({"Valet", "Car"}, IServiceManager::DUMP_FLAG_PROTO);
284 ExpectCheckService("Car");
285 ExpectCheckService("Valet");
286
287 CallMain({"-l", "--proto"});
288
289 AssertRunningServices({"Car", "Valet"});
290}
291
Felipe Leme343175a2016-08-02 18:57:37 -0700292// Tests 'dumpsys service_name' on a service is running
293TEST_F(DumpsysTest, DumpRunningService) {
294 ExpectDump("Valet", "Here's your car");
295
296 CallMain({"Valet"});
297
298 AssertOutput("Here's your car");
299}
300
301// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
Vishnu Nair6921f802017-11-22 09:17:23 -0800302TEST_F(DumpsysTest, DumpRunningServiceTimeoutInSec) {
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700303 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700304
305 CallMain({"-t", "1", "Valet"});
306
Vishnu Nair6921f802017-11-22 09:17:23 -0800307 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1000ms) EXPIRED");
308 AssertNotDumped("Here's your car");
309
310 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
311 Mock::AllowLeak(binder_mock.get());
312}
313
314// Tests 'dumpsys -T 500 service_name' on a service that times out after 2s
315TEST_F(DumpsysTest, DumpRunningServiceTimeoutInMs) {
316 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
317
318 CallMain({"-T", "500", "Valet"});
319
320 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (500ms) EXPIRED");
Felipe Leme343175a2016-08-02 18:57:37 -0700321 AssertNotDumped("Here's your car");
322
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700323 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
324 Mock::AllowLeak(binder_mock.get());
Felipe Leme343175a2016-08-02 18:57:37 -0700325}
326
327// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
328TEST_F(DumpsysTest, DumpWithArgsRunningService) {
329 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
330
331 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
332
333 AssertOutput("I DO!");
334}
335
336// Tests 'dumpsys' with no arguments
337TEST_F(DumpsysTest, DumpMultipleServices) {
338 ExpectListServices({"running1", "stopped2", "running3"});
339 ExpectDump("running1", "dump1");
340 ExpectCheckService("stopped2", false);
341 ExpectDump("running3", "dump3");
342
343 CallMain({});
344
345 AssertRunningServices({"running1", "running3"});
346 AssertDumped("running1", "dump1");
347 AssertStopped("stopped2");
348 AssertDumped("running3", "dump3");
349}
350
351// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
352TEST_F(DumpsysTest, DumpWithSkip) {
353 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
354 ExpectDump("running1", "dump1");
355 ExpectCheckService("stopped2", false);
356 ExpectDump("skipped3", "dump3");
357 ExpectDump("running4", "dump4");
358 ExpectDump("skipped5", "dump5");
359
360 CallMain({"--skip", "skipped3", "skipped5"});
361
362 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
363 AssertDumped("running1", "dump1");
364 AssertDumped("running4", "dump4");
365 AssertStopped("stopped2");
366 AssertNotDumped("dump3");
367 AssertNotDumped("dump5");
368}
Vishnu Nairf56042d2017-09-19 15:25:10 -0700369
370// Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services
371TEST_F(DumpsysTest, DumpWithSkipAndPriority) {
372 ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700373 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700374 ExpectDump("running1", "dump1");
375 ExpectCheckService("stopped2", false);
376 ExpectDump("skipped3", "dump3");
377 ExpectDump("running4", "dump4");
378 ExpectDump("skipped5", "dump5");
379
380 CallMain({"--priority", "CRITICAL", "--skip", "skipped3", "skipped5"});
381
382 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700383 AssertDumpedWithPriority("running1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
384 AssertDumpedWithPriority("running4", "dump4", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700385 AssertStopped("stopped2");
386 AssertNotDumped("dump3");
387 AssertNotDumped("dump5");
388}
389
390// Tests 'dumpsys --priority CRITICAL'
391TEST_F(DumpsysTest, DumpWithPriorityCritical) {
392 ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700393 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700394 ExpectDump("runningcritical1", "dump1");
395 ExpectDump("runningcritical2", "dump2");
396
397 CallMain({"--priority", "CRITICAL"});
398
399 AssertRunningServices({"runningcritical1", "runningcritical2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700400 AssertDumpedWithPriority("runningcritical1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
401 AssertDumpedWithPriority("runningcritical2", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700402}
403
404// Tests 'dumpsys --priority HIGH'
405TEST_F(DumpsysTest, DumpWithPriorityHigh) {
406 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700407 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700408 ExpectDump("runninghigh1", "dump1");
409 ExpectDump("runninghigh2", "dump2");
410
411 CallMain({"--priority", "HIGH"});
412
413 AssertRunningServices({"runninghigh1", "runninghigh2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700414 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
415 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700416}
417
418// Tests 'dumpsys --priority NORMAL'
419TEST_F(DumpsysTest, DumpWithPriorityNormal) {
420 ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700421 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700422 ExpectDump("runningnormal1", "dump1");
423 ExpectDump("runningnormal2", "dump2");
424
425 CallMain({"--priority", "NORMAL"});
426
427 AssertRunningServices({"runningnormal1", "runningnormal2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700428 AssertDumpedWithPriority("runningnormal1", "dump1", PriorityDumper::PRIORITY_ARG_NORMAL);
429 AssertDumpedWithPriority("runningnormal2", "dump2", PriorityDumper::PRIORITY_ARG_NORMAL);
430}
431
432// Tests 'dumpsys --proto'
433TEST_F(DumpsysTest, DumpWithProto) {
434 ExpectListServicesWithPriority({"run8", "run1", "run2", "run5"},
435 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
436 ExpectListServicesWithPriority({"run3", "run2", "run4", "run8"},
437 IServiceManager::DUMP_FLAG_PROTO);
438 ExpectDump("run2", "dump1");
439 ExpectDump("run8", "dump2");
440
441 CallMain({"--proto"});
442
443 AssertRunningServices({"run2", "run8"});
444 AssertDumped("run2", "dump1");
445 AssertDumped("run8", "dump2");
446}
447
448// Tests 'dumpsys --priority HIGH --proto'
449TEST_F(DumpsysTest, DumpWithPriorityHighAndProto) {
450 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
451 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
452 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2", "runninghigh3"},
453 IServiceManager::DUMP_FLAG_PROTO);
454
455 ExpectDump("runninghigh1", "dump1");
456 ExpectDump("runninghigh2", "dump2");
457
458 CallMain({"--priority", "HIGH", "--proto"});
459
460 AssertRunningServices({"runninghigh1", "runninghigh2"});
461 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
462 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700463}