blob: b9395ba4e2f01b69faa9d1230dad42efb7f09334 [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&));
Steven Morelandb82b8f82019-10-28 10:52:34 -070057 MOCK_METHOD1(isDeclared, bool(const String16&));
Felipe Leme343175a2016-08-02 18:57:37 -070058 protected:
59 MOCK_METHOD0(onAsBinder, IBinder*());
60};
61
62class BinderMock : public BBinder {
63 public:
64 BinderMock() {
65 }
66
67 MOCK_METHOD2(dump, status_t(int, const Vector<String16>&));
68};
69
70// gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher
71typedef void WriteOnFdFunction(int);
72
73class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
74 public:
75 explicit WriteOnFdAction(const std::string& output) : output_(output) {
76 }
77 virtual Result Perform(const ArgumentTuple& args) {
Haibo Huang21f36552018-07-10 19:48:18 -070078 int fd = ::testing::get<0>(args);
Felipe Leme343175a2016-08-02 18:57:37 -070079 android::base::WriteStringToFd(output_, fd);
80 }
81
82 private:
83 std::string output_;
84};
85
86// Matcher used to emulate dump() by writing on its file descriptor.
87Action<WriteOnFdFunction> WriteOnFd(const std::string& output) {
88 return MakeAction(new WriteOnFdAction(output));
89}
90
91// Matcher for args using Android's Vector<String16> format
92// TODO: move it to some common testing library
93MATCHER_P(AndroidElementsAre, expected, "") {
94 std::ostringstream errors;
95 if (arg.size() != expected.size()) {
96 errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size()
97 << ")\n";
98 }
99 int i = 0;
100 std::ostringstream actual_stream, expected_stream;
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700101 for (const String16& actual : arg) {
Steven Morelanda0f7f2d2017-03-09 22:59:32 -0800102 std::string actual_str = String8(actual).c_str();
Felipe Leme343175a2016-08-02 18:57:37 -0700103 std::string expected_str = expected[i];
104 actual_stream << "'" << actual_str << "' ";
105 expected_stream << "'" << expected_str << "' ";
106 if (actual_str != expected_str) {
107 errors << " element mismatch at index " << i << "\n";
108 }
109 i++;
110 }
111
112 if (!errors.str().empty()) {
113 errors << "\nExpected args: " << expected_stream.str()
114 << "\nActual args: " << actual_stream.str();
115 *result_listener << errors.str();
116 return false;
117 }
118 return true;
119}
120
121// Custom action to sleep for timeout seconds
122ACTION_P(Sleep, timeout) {
123 sleep(timeout);
124}
125
126class DumpsysTest : public Test {
127 public:
Steven Moreland2c3cd832017-02-13 23:44:17 +0000128 DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() {
Felipe Leme343175a2016-08-02 18:57:37 -0700129 }
130
131 void ExpectListServices(std::vector<std::string> services) {
132 Vector<String16> services16;
133 for (auto& service : services) {
134 services16.add(String16(service.c_str()));
135 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700136 EXPECT_CALL(sm_, listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL))
Vishnu Nairf56042d2017-09-19 15:25:10 -0700137 .WillRepeatedly(Return(services16));
138 }
139
Vishnu Nair6a408532017-10-24 09:11:27 -0700140 void ExpectListServicesWithPriority(std::vector<std::string> services, int dumpFlags) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700141 Vector<String16> services16;
142 for (auto& service : services) {
143 services16.add(String16(service.c_str()));
144 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700145 EXPECT_CALL(sm_, listServices(dumpFlags)).WillRepeatedly(Return(services16));
Felipe Leme343175a2016-08-02 18:57:37 -0700146 }
147
148 sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
149 sp<BinderMock> binder_mock;
150 if (running) {
151 binder_mock = new BinderMock;
152 }
153 EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
154 return binder_mock;
155 }
156
157 void ExpectDump(const char* name, const std::string& output) {
158 sp<BinderMock> binder_mock = ExpectCheckService(name);
159 EXPECT_CALL(*binder_mock, dump(_, _))
160 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
161 }
162
163 void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
164 const std::string& output) {
165 sp<BinderMock> binder_mock = ExpectCheckService(name);
166 EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
167 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
168 }
169
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700170 sp<BinderMock> ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
Felipe Leme343175a2016-08-02 18:57:37 -0700171 sp<BinderMock> binder_mock = ExpectCheckService(name);
172 EXPECT_CALL(*binder_mock, dump(_, _))
173 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700174 return binder_mock;
Felipe Leme343175a2016-08-02 18:57:37 -0700175 }
176
177 void CallMain(const std::vector<std::string>& args) {
178 const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
179 int argc = (int)args.size() + 1;
180 int i = 1;
181 for (const std::string& arg : args) {
182 argv[i++] = arg.c_str();
183 }
184 CaptureStdout();
185 CaptureStderr();
186 int status = dump_.main(argc, const_cast<char**>(argv));
187 stdout_ = GetCapturedStdout();
188 stderr_ = GetCapturedStderr();
189 EXPECT_THAT(status, Eq(0));
190 }
191
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000192 void CallSingleService(const String16& serviceName, Vector<String16>& args, int priorityFlags,
193 bool supportsProto, std::chrono::duration<double>& elapsedDuration,
194 size_t& bytesWritten) {
195 CaptureStdout();
196 CaptureStderr();
197 dump_.setServiceArgs(args, supportsProto, priorityFlags);
Steven Moreland5a30d342019-10-08 13:53:28 -0700198 status_t status = dump_.startDumpThread(Dumpsys::Type::DUMP, serviceName, args);
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000199 EXPECT_THAT(status, Eq(0));
200 status = dump_.writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(500), false,
201 elapsedDuration, bytesWritten);
202 EXPECT_THAT(status, Eq(0));
203 dump_.stopDumpThread(/* dumpCompleted = */ true);
204 stdout_ = GetCapturedStdout();
205 stderr_ = GetCapturedStderr();
206 }
207
Steven Moreland2c3cd832017-02-13 23:44:17 +0000208 void AssertRunningServices(const std::vector<std::string>& services) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700209 std::string expected;
210 if (services.size() > 1) {
211 expected.append("Currently running services:\n");
212 }
Felipe Leme343175a2016-08-02 18:57:37 -0700213 for (const std::string& service : services) {
214 expected.append(" ").append(service).append("\n");
215 }
216 EXPECT_THAT(stdout_, HasSubstr(expected));
217 }
218
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000219 void AssertOutput(const std::string& expected) {
220 EXPECT_THAT(stdout_, StrEq(expected));
221 }
222
Felipe Leme343175a2016-08-02 18:57:37 -0700223 void AssertOutputContains(const std::string& expected) {
224 EXPECT_THAT(stdout_, HasSubstr(expected));
225 }
226
227 void AssertDumped(const std::string& service, const std::string& dump) {
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000228 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800229 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Felipe Leme343175a2016-08-02 18:57:37 -0700230 }
231
Vishnu Nair6a408532017-10-24 09:11:27 -0700232 void AssertDumpedWithPriority(const std::string& service, const std::string& dump,
233 const char16_t* priorityType) {
234 std::string priority = String8(priorityType).c_str();
235 EXPECT_THAT(stdout_,
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000236 HasSubstr("DUMP OF SERVICE " + priority + " " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800237 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Vishnu Nair6a408532017-10-24 09:11:27 -0700238 }
239
Felipe Leme343175a2016-08-02 18:57:37 -0700240 void AssertNotDumped(const std::string& dump) {
241 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
242 }
243
244 void AssertStopped(const std::string& service) {
245 EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
246 }
247
248 ServiceManagerMock sm_;
249 Dumpsys dump_;
250
251 private:
252 std::string stdout_, stderr_;
253};
254
Felipe Leme343175a2016-08-02 18:57:37 -0700255// Tests 'dumpsys -l' when all services are running
256TEST_F(DumpsysTest, ListAllServices) {
257 ExpectListServices({"Locksmith", "Valet"});
258 ExpectCheckService("Locksmith");
259 ExpectCheckService("Valet");
260
261 CallMain({"-l"});
262
263 AssertRunningServices({"Locksmith", "Valet"});
264}
265
266// Tests 'dumpsys -l' when a service is not running
267TEST_F(DumpsysTest, ListRunningServices) {
268 ExpectListServices({"Locksmith", "Valet"});
269 ExpectCheckService("Locksmith");
270 ExpectCheckService("Valet", false);
271
272 CallMain({"-l"});
273
274 AssertRunningServices({"Locksmith"});
275 AssertNotDumped({"Valet"});
276}
277
Vishnu Nairf56042d2017-09-19 15:25:10 -0700278// Tests 'dumpsys -l --priority HIGH'
279TEST_F(DumpsysTest, ListAllServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700280 ExpectListServicesWithPriority({"Locksmith", "Valet"}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700281 ExpectCheckService("Locksmith");
282 ExpectCheckService("Valet");
283
284 CallMain({"-l", "--priority", "HIGH"});
285
286 AssertRunningServices({"Locksmith", "Valet"});
287}
288
289// Tests 'dumpsys -l --priority HIGH' with and empty list
290TEST_F(DumpsysTest, ListEmptyServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700291 ExpectListServicesWithPriority({}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700292
293 CallMain({"-l", "--priority", "HIGH"});
294
295 AssertRunningServices({});
296}
297
Vishnu Nair6a408532017-10-24 09:11:27 -0700298// Tests 'dumpsys -l --proto'
299TEST_F(DumpsysTest, ListAllServicesWithProto) {
300 ExpectListServicesWithPriority({"Locksmith", "Valet", "Car"},
301 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
302 ExpectListServicesWithPriority({"Valet", "Car"}, IServiceManager::DUMP_FLAG_PROTO);
303 ExpectCheckService("Car");
304 ExpectCheckService("Valet");
305
306 CallMain({"-l", "--proto"});
307
308 AssertRunningServices({"Car", "Valet"});
309}
310
Felipe Leme343175a2016-08-02 18:57:37 -0700311// Tests 'dumpsys service_name' on a service is running
312TEST_F(DumpsysTest, DumpRunningService) {
313 ExpectDump("Valet", "Here's your car");
314
315 CallMain({"Valet"});
316
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000317 AssertOutput("Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700318}
319
320// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
Vishnu Nair6921f802017-11-22 09:17:23 -0800321TEST_F(DumpsysTest, DumpRunningServiceTimeoutInSec) {
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700322 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700323
324 CallMain({"-t", "1", "Valet"});
325
Vishnu Nair6921f802017-11-22 09:17:23 -0800326 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1000ms) EXPIRED");
327 AssertNotDumped("Here's your car");
328
329 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
330 Mock::AllowLeak(binder_mock.get());
331}
332
333// Tests 'dumpsys -T 500 service_name' on a service that times out after 2s
334TEST_F(DumpsysTest, DumpRunningServiceTimeoutInMs) {
335 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
336
337 CallMain({"-T", "500", "Valet"});
338
339 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (500ms) EXPIRED");
Felipe Leme343175a2016-08-02 18:57:37 -0700340 AssertNotDumped("Here's your car");
341
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700342 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
343 Mock::AllowLeak(binder_mock.get());
Felipe Leme343175a2016-08-02 18:57:37 -0700344}
345
346// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
347TEST_F(DumpsysTest, DumpWithArgsRunningService) {
348 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
349
350 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
351
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000352 AssertOutput("I DO!");
Felipe Leme343175a2016-08-02 18:57:37 -0700353}
354
Vishnu Nair64afc022018-02-01 15:29:34 -0800355// Tests dumpsys passes the -a flag when called on all services
356TEST_F(DumpsysTest, PassAllFlagsToServices) {
357 ExpectListServices({"Locksmith", "Valet"});
358 ExpectCheckService("Locksmith");
359 ExpectCheckService("Valet");
360 ExpectDumpWithArgs("Locksmith", {"-a"}, "dumped1");
361 ExpectDumpWithArgs("Valet", {"-a"}, "dumped2");
362
363 CallMain({"-T", "500"});
364
365 AssertDumped("Locksmith", "dumped1");
366 AssertDumped("Valet", "dumped2");
367}
368
369// Tests dumpsys passes the -a flag when called on NORMAL priority services
370TEST_F(DumpsysTest, PassAllFlagsToNormalServices) {
371 ExpectListServicesWithPriority({"Locksmith", "Valet"},
372 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
373 ExpectCheckService("Locksmith");
374 ExpectCheckService("Valet");
Vishnu Nair3919e1c2018-05-01 17:01:00 -0700375 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "NORMAL", "-a"}, "dump1");
376 ExpectDumpWithArgs("Valet", {"--dump-priority", "NORMAL", "-a"}, "dump2");
Vishnu Nair64afc022018-02-01 15:29:34 -0800377
378 CallMain({"--priority", "NORMAL"});
379
380 AssertDumped("Locksmith", "dump1");
381 AssertDumped("Valet", "dump2");
382}
383
384// Tests dumpsys passes only priority flags when called on CRITICAL priority services
385TEST_F(DumpsysTest, PassPriorityFlagsToCriticalServices) {
386 ExpectListServicesWithPriority({"Locksmith", "Valet"},
387 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
388 ExpectCheckService("Locksmith");
389 ExpectCheckService("Valet");
390 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "CRITICAL"}, "dump1");
391 ExpectDumpWithArgs("Valet", {"--dump-priority", "CRITICAL"}, "dump2");
392
393 CallMain({"--priority", "CRITICAL"});
394
395 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
396 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
397}
398
399// Tests dumpsys passes only priority flags when called on HIGH priority services
400TEST_F(DumpsysTest, PassPriorityFlagsToHighServices) {
401 ExpectListServicesWithPriority({"Locksmith", "Valet"},
402 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
403 ExpectCheckService("Locksmith");
404 ExpectCheckService("Valet");
405 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "HIGH"}, "dump1");
406 ExpectDumpWithArgs("Valet", {"--dump-priority", "HIGH"}, "dump2");
407
408 CallMain({"--priority", "HIGH"});
409
410 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
411 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
412}
413
Felipe Leme343175a2016-08-02 18:57:37 -0700414// Tests 'dumpsys' with no arguments
415TEST_F(DumpsysTest, DumpMultipleServices) {
416 ExpectListServices({"running1", "stopped2", "running3"});
417 ExpectDump("running1", "dump1");
418 ExpectCheckService("stopped2", false);
419 ExpectDump("running3", "dump3");
420
421 CallMain({});
422
423 AssertRunningServices({"running1", "running3"});
424 AssertDumped("running1", "dump1");
425 AssertStopped("stopped2");
426 AssertDumped("running3", "dump3");
427}
428
429// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
430TEST_F(DumpsysTest, DumpWithSkip) {
431 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
432 ExpectDump("running1", "dump1");
433 ExpectCheckService("stopped2", false);
434 ExpectDump("skipped3", "dump3");
435 ExpectDump("running4", "dump4");
436 ExpectDump("skipped5", "dump5");
437
438 CallMain({"--skip", "skipped3", "skipped5"});
439
440 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
441 AssertDumped("running1", "dump1");
442 AssertDumped("running4", "dump4");
443 AssertStopped("stopped2");
444 AssertNotDumped("dump3");
445 AssertNotDumped("dump5");
446}
Vishnu Nairf56042d2017-09-19 15:25:10 -0700447
448// Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services
449TEST_F(DumpsysTest, DumpWithSkipAndPriority) {
450 ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700451 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700452 ExpectDump("running1", "dump1");
453 ExpectCheckService("stopped2", false);
454 ExpectDump("skipped3", "dump3");
455 ExpectDump("running4", "dump4");
456 ExpectDump("skipped5", "dump5");
457
458 CallMain({"--priority", "CRITICAL", "--skip", "skipped3", "skipped5"});
459
460 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700461 AssertDumpedWithPriority("running1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
462 AssertDumpedWithPriority("running4", "dump4", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700463 AssertStopped("stopped2");
464 AssertNotDumped("dump3");
465 AssertNotDumped("dump5");
466}
467
468// Tests 'dumpsys --priority CRITICAL'
469TEST_F(DumpsysTest, DumpWithPriorityCritical) {
470 ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700471 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700472 ExpectDump("runningcritical1", "dump1");
473 ExpectDump("runningcritical2", "dump2");
474
475 CallMain({"--priority", "CRITICAL"});
476
477 AssertRunningServices({"runningcritical1", "runningcritical2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700478 AssertDumpedWithPriority("runningcritical1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
479 AssertDumpedWithPriority("runningcritical2", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700480}
481
482// Tests 'dumpsys --priority HIGH'
483TEST_F(DumpsysTest, DumpWithPriorityHigh) {
484 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700485 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700486 ExpectDump("runninghigh1", "dump1");
487 ExpectDump("runninghigh2", "dump2");
488
489 CallMain({"--priority", "HIGH"});
490
491 AssertRunningServices({"runninghigh1", "runninghigh2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700492 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
493 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700494}
495
496// Tests 'dumpsys --priority NORMAL'
497TEST_F(DumpsysTest, DumpWithPriorityNormal) {
498 ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700499 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700500 ExpectDump("runningnormal1", "dump1");
501 ExpectDump("runningnormal2", "dump2");
502
503 CallMain({"--priority", "NORMAL"});
504
505 AssertRunningServices({"runningnormal1", "runningnormal2"});
Vishnu Naire4f61742017-12-21 08:30:28 -0800506 AssertDumped("runningnormal1", "dump1");
507 AssertDumped("runningnormal2", "dump2");
Vishnu Nair6a408532017-10-24 09:11:27 -0700508}
509
510// Tests 'dumpsys --proto'
511TEST_F(DumpsysTest, DumpWithProto) {
512 ExpectListServicesWithPriority({"run8", "run1", "run2", "run5"},
513 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
514 ExpectListServicesWithPriority({"run3", "run2", "run4", "run8"},
515 IServiceManager::DUMP_FLAG_PROTO);
516 ExpectDump("run2", "dump1");
517 ExpectDump("run8", "dump2");
518
519 CallMain({"--proto"});
520
521 AssertRunningServices({"run2", "run8"});
522 AssertDumped("run2", "dump1");
523 AssertDumped("run8", "dump2");
524}
525
526// Tests 'dumpsys --priority HIGH --proto'
527TEST_F(DumpsysTest, DumpWithPriorityHighAndProto) {
528 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
529 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
530 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2", "runninghigh3"},
531 IServiceManager::DUMP_FLAG_PROTO);
532
533 ExpectDump("runninghigh1", "dump1");
534 ExpectDump("runninghigh2", "dump2");
535
536 CallMain({"--priority", "HIGH", "--proto"});
537
538 AssertRunningServices({"runninghigh1", "runninghigh2"});
539 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
540 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700541}
Vishnu Naire4f61742017-12-21 08:30:28 -0800542
Steven Moreland5a30d342019-10-08 13:53:28 -0700543// Tests 'dumpsys --pid'
544TEST_F(DumpsysTest, ListAllServicesWithPid) {
545 ExpectListServices({"Locksmith", "Valet"});
546 ExpectCheckService("Locksmith");
547 ExpectCheckService("Valet");
548
549 CallMain({"--pid"});
550
551 AssertRunningServices({"Locksmith", "Valet"});
552 AssertOutputContains(std::to_string(getpid()));
553}
554
555// Tests 'dumpsys --pid service_name'
556TEST_F(DumpsysTest, ListServiceWithPid) {
557 ExpectCheckService("Locksmith");
558
559 CallMain({"--pid", "Locksmith"});
560
561 AssertOutput(std::to_string(getpid()) + "\n");
562}
563
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000564TEST_F(DumpsysTest, GetBytesWritten) {
565 const char* serviceName = "service2";
566 const char* dumpContents = "dump1";
567 ExpectDump(serviceName, dumpContents);
568
569 String16 service(serviceName);
570 Vector<String16> args;
571 std::chrono::duration<double> elapsedDuration;
572 size_t bytesWritten;
573
574 CallSingleService(service, args, IServiceManager::DUMP_FLAG_PRIORITY_ALL,
575 /* as_proto = */ false, elapsedDuration, bytesWritten);
576
577 AssertOutput(dumpContents);
578 EXPECT_THAT(bytesWritten, Eq(strlen(dumpContents)));
579}
580
Vishnu Naire4f61742017-12-21 08:30:28 -0800581TEST_F(DumpsysTest, WriteDumpWithoutThreadStart) {
582 std::chrono::duration<double> elapsedDuration;
583 size_t bytesWritten;
584 status_t status =
585 dump_.writeDump(STDOUT_FILENO, String16("service"), std::chrono::milliseconds(500),
586 /* as_proto = */ false, elapsedDuration, bytesWritten);
587 EXPECT_THAT(status, Eq(INVALID_OPERATION));
Steven Moreland5a30d342019-10-08 13:53:28 -0700588}