blob: d0b167e6b0b6d7bc5e7df2f0686523e119fe1108 [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));
Steven Moreland1c47b582019-08-27 18:05:27 -070056 MOCK_METHOD1(waitForService, sp<IBinder>(const String16&));
Felipe Leme343175a2016-08-02 18:57:37 -070057 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) {
Haibo Huang21f36552018-07-10 19:48:18 -070077 int fd = ::testing::get<0>(args);
Felipe Leme343175a2016-08-02 18:57:37 -070078 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
Felipe Leme343175a2016-08-02 18:57:37 -0700202 void AssertOutputContains(const std::string& expected) {
203 EXPECT_THAT(stdout_, HasSubstr(expected));
204 }
205
206 void AssertDumped(const std::string& service, const std::string& dump) {
Steven Morelandf5ea44c2019-09-23 15:42:01 -0700207 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n"));
208 EXPECT_THAT(stdout_, HasSubstr(dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800209 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Felipe Leme343175a2016-08-02 18:57:37 -0700210 }
211
Vishnu Nair6a408532017-10-24 09:11:27 -0700212 void AssertDumpedWithPriority(const std::string& service, const std::string& dump,
213 const char16_t* priorityType) {
214 std::string priority = String8(priorityType).c_str();
215 EXPECT_THAT(stdout_,
Steven Morelandf5ea44c2019-09-23 15:42:01 -0700216 HasSubstr("DUMP OF SERVICE " + priority + " " + service + ":\n"));
217 EXPECT_THAT(stdout_, HasSubstr(dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800218 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Vishnu Nair6a408532017-10-24 09:11:27 -0700219 }
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
Steven Morelandf5ea44c2019-09-23 15:42:01 -0700298 AssertOutputContains("Pid: " + std::to_string(getpid()));
299 AssertOutputContains("Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700300}
301
302// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
Vishnu Nair6921f802017-11-22 09:17:23 -0800303TEST_F(DumpsysTest, DumpRunningServiceTimeoutInSec) {
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700304 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700305
306 CallMain({"-t", "1", "Valet"});
307
Vishnu Nair6921f802017-11-22 09:17:23 -0800308 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1000ms) EXPIRED");
309 AssertNotDumped("Here's your car");
310
311 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
312 Mock::AllowLeak(binder_mock.get());
313}
314
315// Tests 'dumpsys -T 500 service_name' on a service that times out after 2s
316TEST_F(DumpsysTest, DumpRunningServiceTimeoutInMs) {
317 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
318
319 CallMain({"-T", "500", "Valet"});
320
321 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (500ms) EXPIRED");
Felipe Leme343175a2016-08-02 18:57:37 -0700322 AssertNotDumped("Here's your car");
323
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700324 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
325 Mock::AllowLeak(binder_mock.get());
Felipe Leme343175a2016-08-02 18:57:37 -0700326}
327
328// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
329TEST_F(DumpsysTest, DumpWithArgsRunningService) {
330 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
331
332 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
333
Steven Morelandf5ea44c2019-09-23 15:42:01 -0700334 AssertOutputContains("I DO!");
Felipe Leme343175a2016-08-02 18:57:37 -0700335}
336
Vishnu Nair64afc022018-02-01 15:29:34 -0800337// Tests dumpsys passes the -a flag when called on all services
338TEST_F(DumpsysTest, PassAllFlagsToServices) {
339 ExpectListServices({"Locksmith", "Valet"});
340 ExpectCheckService("Locksmith");
341 ExpectCheckService("Valet");
342 ExpectDumpWithArgs("Locksmith", {"-a"}, "dumped1");
343 ExpectDumpWithArgs("Valet", {"-a"}, "dumped2");
344
345 CallMain({"-T", "500"});
346
347 AssertDumped("Locksmith", "dumped1");
348 AssertDumped("Valet", "dumped2");
349}
350
351// Tests dumpsys passes the -a flag when called on NORMAL priority services
352TEST_F(DumpsysTest, PassAllFlagsToNormalServices) {
353 ExpectListServicesWithPriority({"Locksmith", "Valet"},
354 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
355 ExpectCheckService("Locksmith");
356 ExpectCheckService("Valet");
Vishnu Nair3919e1c2018-05-01 17:01:00 -0700357 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "NORMAL", "-a"}, "dump1");
358 ExpectDumpWithArgs("Valet", {"--dump-priority", "NORMAL", "-a"}, "dump2");
Vishnu Nair64afc022018-02-01 15:29:34 -0800359
360 CallMain({"--priority", "NORMAL"});
361
362 AssertDumped("Locksmith", "dump1");
363 AssertDumped("Valet", "dump2");
364}
365
366// Tests dumpsys passes only priority flags when called on CRITICAL priority services
367TEST_F(DumpsysTest, PassPriorityFlagsToCriticalServices) {
368 ExpectListServicesWithPriority({"Locksmith", "Valet"},
369 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
370 ExpectCheckService("Locksmith");
371 ExpectCheckService("Valet");
372 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "CRITICAL"}, "dump1");
373 ExpectDumpWithArgs("Valet", {"--dump-priority", "CRITICAL"}, "dump2");
374
375 CallMain({"--priority", "CRITICAL"});
376
377 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
378 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
379}
380
381// Tests dumpsys passes only priority flags when called on HIGH priority services
382TEST_F(DumpsysTest, PassPriorityFlagsToHighServices) {
383 ExpectListServicesWithPriority({"Locksmith", "Valet"},
384 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
385 ExpectCheckService("Locksmith");
386 ExpectCheckService("Valet");
387 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "HIGH"}, "dump1");
388 ExpectDumpWithArgs("Valet", {"--dump-priority", "HIGH"}, "dump2");
389
390 CallMain({"--priority", "HIGH"});
391
392 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
393 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
394}
395
Felipe Leme343175a2016-08-02 18:57:37 -0700396// Tests 'dumpsys' with no arguments
397TEST_F(DumpsysTest, DumpMultipleServices) {
398 ExpectListServices({"running1", "stopped2", "running3"});
399 ExpectDump("running1", "dump1");
400 ExpectCheckService("stopped2", false);
401 ExpectDump("running3", "dump3");
402
403 CallMain({});
404
405 AssertRunningServices({"running1", "running3"});
406 AssertDumped("running1", "dump1");
407 AssertStopped("stopped2");
408 AssertDumped("running3", "dump3");
409}
410
411// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
412TEST_F(DumpsysTest, DumpWithSkip) {
413 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
414 ExpectDump("running1", "dump1");
415 ExpectCheckService("stopped2", false);
416 ExpectDump("skipped3", "dump3");
417 ExpectDump("running4", "dump4");
418 ExpectDump("skipped5", "dump5");
419
420 CallMain({"--skip", "skipped3", "skipped5"});
421
422 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
423 AssertDumped("running1", "dump1");
424 AssertDumped("running4", "dump4");
425 AssertStopped("stopped2");
426 AssertNotDumped("dump3");
427 AssertNotDumped("dump5");
428}
Vishnu Nairf56042d2017-09-19 15:25:10 -0700429
430// Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services
431TEST_F(DumpsysTest, DumpWithSkipAndPriority) {
432 ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700433 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700434 ExpectDump("running1", "dump1");
435 ExpectCheckService("stopped2", false);
436 ExpectDump("skipped3", "dump3");
437 ExpectDump("running4", "dump4");
438 ExpectDump("skipped5", "dump5");
439
440 CallMain({"--priority", "CRITICAL", "--skip", "skipped3", "skipped5"});
441
442 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700443 AssertDumpedWithPriority("running1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
444 AssertDumpedWithPriority("running4", "dump4", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700445 AssertStopped("stopped2");
446 AssertNotDumped("dump3");
447 AssertNotDumped("dump5");
448}
449
450// Tests 'dumpsys --priority CRITICAL'
451TEST_F(DumpsysTest, DumpWithPriorityCritical) {
452 ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700453 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700454 ExpectDump("runningcritical1", "dump1");
455 ExpectDump("runningcritical2", "dump2");
456
457 CallMain({"--priority", "CRITICAL"});
458
459 AssertRunningServices({"runningcritical1", "runningcritical2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700460 AssertDumpedWithPriority("runningcritical1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
461 AssertDumpedWithPriority("runningcritical2", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700462}
463
464// Tests 'dumpsys --priority HIGH'
465TEST_F(DumpsysTest, DumpWithPriorityHigh) {
466 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700467 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700468 ExpectDump("runninghigh1", "dump1");
469 ExpectDump("runninghigh2", "dump2");
470
471 CallMain({"--priority", "HIGH"});
472
473 AssertRunningServices({"runninghigh1", "runninghigh2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700474 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
475 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700476}
477
478// Tests 'dumpsys --priority NORMAL'
479TEST_F(DumpsysTest, DumpWithPriorityNormal) {
480 ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700481 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700482 ExpectDump("runningnormal1", "dump1");
483 ExpectDump("runningnormal2", "dump2");
484
485 CallMain({"--priority", "NORMAL"});
486
487 AssertRunningServices({"runningnormal1", "runningnormal2"});
Vishnu Naire4f61742017-12-21 08:30:28 -0800488 AssertDumped("runningnormal1", "dump1");
489 AssertDumped("runningnormal2", "dump2");
Vishnu Nair6a408532017-10-24 09:11:27 -0700490}
491
492// Tests 'dumpsys --proto'
493TEST_F(DumpsysTest, DumpWithProto) {
494 ExpectListServicesWithPriority({"run8", "run1", "run2", "run5"},
495 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
496 ExpectListServicesWithPriority({"run3", "run2", "run4", "run8"},
497 IServiceManager::DUMP_FLAG_PROTO);
498 ExpectDump("run2", "dump1");
499 ExpectDump("run8", "dump2");
500
501 CallMain({"--proto"});
502
503 AssertRunningServices({"run2", "run8"});
504 AssertDumped("run2", "dump1");
505 AssertDumped("run8", "dump2");
506}
507
508// Tests 'dumpsys --priority HIGH --proto'
509TEST_F(DumpsysTest, DumpWithPriorityHighAndProto) {
510 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
511 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
512 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2", "runninghigh3"},
513 IServiceManager::DUMP_FLAG_PROTO);
514
515 ExpectDump("runninghigh1", "dump1");
516 ExpectDump("runninghigh2", "dump2");
517
518 CallMain({"--priority", "HIGH", "--proto"});
519
520 AssertRunningServices({"runninghigh1", "runninghigh2"});
521 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
522 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700523}
Vishnu Naire4f61742017-12-21 08:30:28 -0800524
Vishnu Naire4f61742017-12-21 08:30:28 -0800525TEST_F(DumpsysTest, WriteDumpWithoutThreadStart) {
526 std::chrono::duration<double> elapsedDuration;
527 size_t bytesWritten;
528 status_t status =
529 dump_.writeDump(STDOUT_FILENO, String16("service"), std::chrono::milliseconds(500),
530 /* as_proto = */ false, elapsedDuration, bytesWritten);
531 EXPECT_THAT(status, Eq(INVALID_OPERATION));
Steven Morelandf5ea44c2019-09-23 15:42:01 -0700532}