1 /*
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 <serviceutils/PriorityDumper.h>
26 #include <utils/String16.h>
27 #include <utils/String8.h>
28 #include <utils/Vector.h>
29
30 using namespace android;
31
32 using ::testing::_;
33 using ::testing::Action;
34 using ::testing::ActionInterface;
35 using ::testing::DoAll;
36 using ::testing::Eq;
37 using ::testing::HasSubstr;
38 using ::testing::MakeAction;
39 using ::testing::Mock;
40 using ::testing::Not;
41 using ::testing::Return;
42 using ::testing::StrEq;
43 using ::testing::Test;
44 using ::testing::WithArg;
45 using ::testing::internal::CaptureStderr;
46 using ::testing::internal::CaptureStdout;
47 using ::testing::internal::GetCapturedStderr;
48 using ::testing::internal::GetCapturedStdout;
49
50 class ServiceManagerMock : public IServiceManager {
51 public:
52 MOCK_CONST_METHOD1(getService, sp<IBinder>(const String16&));
53 MOCK_CONST_METHOD1(checkService, sp<IBinder>(const String16&));
54 MOCK_METHOD4(addService, status_t(const String16&, const sp<IBinder>&, bool, int));
55 MOCK_METHOD1(listServices, Vector<String16>(int));
56 MOCK_METHOD1(waitForService, sp<IBinder>(const String16&));
57 MOCK_METHOD1(isDeclared, bool(const String16&));
58 protected:
59 MOCK_METHOD0(onAsBinder, IBinder*());
60 };
61
62 class BinderMock : public BBinder {
63 public:
BinderMock()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
71 typedef void WriteOnFdFunction(int);
72
73 class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
74 public:
WriteOnFdAction(const std::string & output)75 explicit WriteOnFdAction(const std::string& output) : output_(output) {
76 }
Perform(const ArgumentTuple & args)77 virtual Result Perform(const ArgumentTuple& args) {
78 int fd = ::testing::get<0>(args);
79 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.
WriteOnFd(const std::string & output)87 Action<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
93 MATCHER_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;
101 for (const String16& actual : arg) {
102 std::string actual_str = String8(actual).c_str();
103 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
ACTION_P(Sleep,timeout)122 ACTION_P(Sleep, timeout) {
123 sleep(timeout);
124 }
125
126 class DumpsysTest : public Test {
127 public:
DumpsysTest()128 DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() {
129 }
130
ExpectListServices(std::vector<std::string> services)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 }
136 EXPECT_CALL(sm_, listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL))
137 .WillRepeatedly(Return(services16));
138 }
139
ExpectListServicesWithPriority(std::vector<std::string> services,int dumpFlags)140 void ExpectListServicesWithPriority(std::vector<std::string> services, int dumpFlags) {
141 Vector<String16> services16;
142 for (auto& service : services) {
143 services16.add(String16(service.c_str()));
144 }
145 EXPECT_CALL(sm_, listServices(dumpFlags)).WillRepeatedly(Return(services16));
146 }
147
ExpectCheckService(const char * name,bool running=true)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
ExpectDump(const char * name,const std::string & output)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
ExpectDumpWithArgs(const char * name,std::vector<std::string> args,const std::string & output)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
ExpectDumpAndHang(const char * name,int timeout_s,const std::string & output)170 sp<BinderMock> ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
171 sp<BinderMock> binder_mock = ExpectCheckService(name);
172 EXPECT_CALL(*binder_mock, dump(_, _))
173 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
174 return binder_mock;
175 }
176
CallMain(const std::vector<std::string> & args)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
CallSingleService(const String16 & serviceName,Vector<String16> & args,int priorityFlags,bool supportsProto,std::chrono::duration<double> & elapsedDuration,size_t & bytesWritten)192 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);
198 status_t status = dump_.startDumpThread(Dumpsys::Type::DUMP, serviceName, args);
199 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
AssertRunningServices(const std::vector<std::string> & services)208 void AssertRunningServices(const std::vector<std::string>& services) {
209 std::string expected;
210 if (services.size() > 1) {
211 expected.append("Currently running services:\n");
212 }
213 for (const std::string& service : services) {
214 expected.append(" ").append(service).append("\n");
215 }
216 EXPECT_THAT(stdout_, HasSubstr(expected));
217 }
218
AssertOutput(const std::string & expected)219 void AssertOutput(const std::string& expected) {
220 EXPECT_THAT(stdout_, StrEq(expected));
221 }
222
AssertOutputContains(const std::string & expected)223 void AssertOutputContains(const std::string& expected) {
224 EXPECT_THAT(stdout_, HasSubstr(expected));
225 }
226
AssertDumped(const std::string & service,const std::string & dump)227 void AssertDumped(const std::string& service, const std::string& dump) {
228 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
229 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
230 }
231
AssertDumpedWithPriority(const std::string & service,const std::string & dump,const char16_t * priorityType)232 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_,
236 HasSubstr("DUMP OF SERVICE " + priority + " " + service + ":\n" + dump));
237 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
238 }
239
AssertNotDumped(const std::string & dump)240 void AssertNotDumped(const std::string& dump) {
241 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
242 }
243
AssertStopped(const std::string & service)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
255 // Tests 'dumpsys -l' when all services are running
TEST_F(DumpsysTest,ListAllServices)256 TEST_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
TEST_F(DumpsysTest,ListRunningServices)267 TEST_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
278 // Tests 'dumpsys -l --priority HIGH'
TEST_F(DumpsysTest,ListAllServicesWithPriority)279 TEST_F(DumpsysTest, ListAllServicesWithPriority) {
280 ExpectListServicesWithPriority({"Locksmith", "Valet"}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
281 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
TEST_F(DumpsysTest,ListEmptyServicesWithPriority)290 TEST_F(DumpsysTest, ListEmptyServicesWithPriority) {
291 ExpectListServicesWithPriority({}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
292
293 CallMain({"-l", "--priority", "HIGH"});
294
295 AssertRunningServices({});
296 }
297
298 // Tests 'dumpsys -l --proto'
TEST_F(DumpsysTest,ListAllServicesWithProto)299 TEST_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
311 // Tests 'dumpsys service_name' on a service is running
TEST_F(DumpsysTest,DumpRunningService)312 TEST_F(DumpsysTest, DumpRunningService) {
313 ExpectDump("Valet", "Here's your car");
314
315 CallMain({"Valet"});
316
317 AssertOutput("Here's your car");
318 }
319
320 // Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
TEST_F(DumpsysTest,DumpRunningServiceTimeoutInSec)321 TEST_F(DumpsysTest, DumpRunningServiceTimeoutInSec) {
322 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
323
324 CallMain({"-t", "1", "Valet"});
325
326 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
TEST_F(DumpsysTest,DumpRunningServiceTimeoutInMs)334 TEST_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");
340 AssertNotDumped("Here's your car");
341
342 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
343 Mock::AllowLeak(binder_mock.get());
344 }
345
346 // Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
TEST_F(DumpsysTest,DumpWithArgsRunningService)347 TEST_F(DumpsysTest, DumpWithArgsRunningService) {
348 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
349
350 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
351
352 AssertOutput("I DO!");
353 }
354
355 // Tests dumpsys passes the -a flag when called on all services
TEST_F(DumpsysTest,PassAllFlagsToServices)356 TEST_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
TEST_F(DumpsysTest,PassAllFlagsToNormalServices)370 TEST_F(DumpsysTest, PassAllFlagsToNormalServices) {
371 ExpectListServicesWithPriority({"Locksmith", "Valet"},
372 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
373 ExpectCheckService("Locksmith");
374 ExpectCheckService("Valet");
375 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "NORMAL", "-a"}, "dump1");
376 ExpectDumpWithArgs("Valet", {"--dump-priority", "NORMAL", "-a"}, "dump2");
377
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
TEST_F(DumpsysTest,PassPriorityFlagsToCriticalServices)385 TEST_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
TEST_F(DumpsysTest,PassPriorityFlagsToHighServices)400 TEST_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
414 // Tests 'dumpsys' with no arguments
TEST_F(DumpsysTest,DumpMultipleServices)415 TEST_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
TEST_F(DumpsysTest,DumpWithSkip)430 TEST_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 }
447
448 // Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services
TEST_F(DumpsysTest,DumpWithSkipAndPriority)449 TEST_F(DumpsysTest, DumpWithSkipAndPriority) {
450 ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"},
451 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
452 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)"});
461 AssertDumpedWithPriority("running1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
462 AssertDumpedWithPriority("running4", "dump4", PriorityDumper::PRIORITY_ARG_CRITICAL);
463 AssertStopped("stopped2");
464 AssertNotDumped("dump3");
465 AssertNotDumped("dump5");
466 }
467
468 // Tests 'dumpsys --priority CRITICAL'
TEST_F(DumpsysTest,DumpWithPriorityCritical)469 TEST_F(DumpsysTest, DumpWithPriorityCritical) {
470 ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"},
471 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
472 ExpectDump("runningcritical1", "dump1");
473 ExpectDump("runningcritical2", "dump2");
474
475 CallMain({"--priority", "CRITICAL"});
476
477 AssertRunningServices({"runningcritical1", "runningcritical2"});
478 AssertDumpedWithPriority("runningcritical1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
479 AssertDumpedWithPriority("runningcritical2", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
480 }
481
482 // Tests 'dumpsys --priority HIGH'
TEST_F(DumpsysTest,DumpWithPriorityHigh)483 TEST_F(DumpsysTest, DumpWithPriorityHigh) {
484 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
485 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
486 ExpectDump("runninghigh1", "dump1");
487 ExpectDump("runninghigh2", "dump2");
488
489 CallMain({"--priority", "HIGH"});
490
491 AssertRunningServices({"runninghigh1", "runninghigh2"});
492 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
493 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
494 }
495
496 // Tests 'dumpsys --priority NORMAL'
TEST_F(DumpsysTest,DumpWithPriorityNormal)497 TEST_F(DumpsysTest, DumpWithPriorityNormal) {
498 ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"},
499 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
500 ExpectDump("runningnormal1", "dump1");
501 ExpectDump("runningnormal2", "dump2");
502
503 CallMain({"--priority", "NORMAL"});
504
505 AssertRunningServices({"runningnormal1", "runningnormal2"});
506 AssertDumped("runningnormal1", "dump1");
507 AssertDumped("runningnormal2", "dump2");
508 }
509
510 // Tests 'dumpsys --proto'
TEST_F(DumpsysTest,DumpWithProto)511 TEST_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'
TEST_F(DumpsysTest,DumpWithPriorityHighAndProto)527 TEST_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);
541 }
542
543 // Tests 'dumpsys --pid'
TEST_F(DumpsysTest,ListAllServicesWithPid)544 TEST_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'
TEST_F(DumpsysTest,ListServiceWithPid)556 TEST_F(DumpsysTest, ListServiceWithPid) {
557 ExpectCheckService("Locksmith");
558
559 CallMain({"--pid", "Locksmith"});
560
561 AssertOutput(std::to_string(getpid()) + "\n");
562 }
563
TEST_F(DumpsysTest,GetBytesWritten)564 TEST_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
TEST_F(DumpsysTest,WriteDumpWithoutThreadStart)581 TEST_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));
588 }
589