blob: 18a4da9739564af3d1268bd1655d46eb69efb954 [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
302TEST_F(DumpsysTest, DumpRunningServiceTimeout) {
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
307 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1s) EXPIRED");
308 AssertNotDumped("Here's your car");
309
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700310 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
311 Mock::AllowLeak(binder_mock.get());
Felipe Leme343175a2016-08-02 18:57:37 -0700312}
313
314// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
315TEST_F(DumpsysTest, DumpWithArgsRunningService) {
316 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
317
318 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
319
320 AssertOutput("I DO!");
321}
322
323// Tests 'dumpsys' with no arguments
324TEST_F(DumpsysTest, DumpMultipleServices) {
325 ExpectListServices({"running1", "stopped2", "running3"});
326 ExpectDump("running1", "dump1");
327 ExpectCheckService("stopped2", false);
328 ExpectDump("running3", "dump3");
329
330 CallMain({});
331
332 AssertRunningServices({"running1", "running3"});
333 AssertDumped("running1", "dump1");
334 AssertStopped("stopped2");
335 AssertDumped("running3", "dump3");
336}
337
338// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
339TEST_F(DumpsysTest, DumpWithSkip) {
340 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
341 ExpectDump("running1", "dump1");
342 ExpectCheckService("stopped2", false);
343 ExpectDump("skipped3", "dump3");
344 ExpectDump("running4", "dump4");
345 ExpectDump("skipped5", "dump5");
346
347 CallMain({"--skip", "skipped3", "skipped5"});
348
349 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
350 AssertDumped("running1", "dump1");
351 AssertDumped("running4", "dump4");
352 AssertStopped("stopped2");
353 AssertNotDumped("dump3");
354 AssertNotDumped("dump5");
355}
Vishnu Nairf56042d2017-09-19 15:25:10 -0700356
357// Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services
358TEST_F(DumpsysTest, DumpWithSkipAndPriority) {
359 ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700360 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700361 ExpectDump("running1", "dump1");
362 ExpectCheckService("stopped2", false);
363 ExpectDump("skipped3", "dump3");
364 ExpectDump("running4", "dump4");
365 ExpectDump("skipped5", "dump5");
366
367 CallMain({"--priority", "CRITICAL", "--skip", "skipped3", "skipped5"});
368
369 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700370 AssertDumpedWithPriority("running1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
371 AssertDumpedWithPriority("running4", "dump4", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700372 AssertStopped("stopped2");
373 AssertNotDumped("dump3");
374 AssertNotDumped("dump5");
375}
376
377// Tests 'dumpsys --priority CRITICAL'
378TEST_F(DumpsysTest, DumpWithPriorityCritical) {
379 ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700380 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700381 ExpectDump("runningcritical1", "dump1");
382 ExpectDump("runningcritical2", "dump2");
383
384 CallMain({"--priority", "CRITICAL"});
385
386 AssertRunningServices({"runningcritical1", "runningcritical2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700387 AssertDumpedWithPriority("runningcritical1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
388 AssertDumpedWithPriority("runningcritical2", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700389}
390
391// Tests 'dumpsys --priority HIGH'
392TEST_F(DumpsysTest, DumpWithPriorityHigh) {
393 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700394 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700395 ExpectDump("runninghigh1", "dump1");
396 ExpectDump("runninghigh2", "dump2");
397
398 CallMain({"--priority", "HIGH"});
399
400 AssertRunningServices({"runninghigh1", "runninghigh2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700401 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
402 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700403}
404
405// Tests 'dumpsys --priority NORMAL'
406TEST_F(DumpsysTest, DumpWithPriorityNormal) {
407 ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700408 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700409 ExpectDump("runningnormal1", "dump1");
410 ExpectDump("runningnormal2", "dump2");
411
412 CallMain({"--priority", "NORMAL"});
413
414 AssertRunningServices({"runningnormal1", "runningnormal2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700415 AssertDumpedWithPriority("runningnormal1", "dump1", PriorityDumper::PRIORITY_ARG_NORMAL);
416 AssertDumpedWithPriority("runningnormal2", "dump2", PriorityDumper::PRIORITY_ARG_NORMAL);
417}
418
419// Tests 'dumpsys --proto'
420TEST_F(DumpsysTest, DumpWithProto) {
421 ExpectListServicesWithPriority({"run8", "run1", "run2", "run5"},
422 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
423 ExpectListServicesWithPriority({"run3", "run2", "run4", "run8"},
424 IServiceManager::DUMP_FLAG_PROTO);
425 ExpectDump("run2", "dump1");
426 ExpectDump("run8", "dump2");
427
428 CallMain({"--proto"});
429
430 AssertRunningServices({"run2", "run8"});
431 AssertDumped("run2", "dump1");
432 AssertDumped("run8", "dump2");
433}
434
435// Tests 'dumpsys --priority HIGH --proto'
436TEST_F(DumpsysTest, DumpWithPriorityHighAndProto) {
437 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
438 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
439 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2", "runninghigh3"},
440 IServiceManager::DUMP_FLAG_PROTO);
441
442 ExpectDump("runninghigh1", "dump1");
443 ExpectDump("runninghigh2", "dump2");
444
445 CallMain({"--priority", "HIGH", "--proto"});
446
447 AssertRunningServices({"runninghigh1", "runninghigh2"});
448 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
449 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700450}