blob: 67a77f601522ccffc7bbcf9e8d1b3ac0b3a449ae [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&));
Steven Moreland2e293aa2020-09-23 00:25:16 +000058 MOCK_METHOD1(getDeclaredInstances, Vector<String16>(const String16&));
Felipe Leme343175a2016-08-02 18:57:37 -070059 protected:
60 MOCK_METHOD0(onAsBinder, IBinder*());
61};
62
63class BinderMock : public BBinder {
64 public:
65 BinderMock() {
66 }
67
68 MOCK_METHOD2(dump, status_t(int, const Vector<String16>&));
69};
70
71// gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher
72typedef void WriteOnFdFunction(int);
73
74class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
75 public:
76 explicit WriteOnFdAction(const std::string& output) : output_(output) {
77 }
78 virtual Result Perform(const ArgumentTuple& args) {
Haibo Huang21f36552018-07-10 19:48:18 -070079 int fd = ::testing::get<0>(args);
Felipe Leme343175a2016-08-02 18:57:37 -070080 android::base::WriteStringToFd(output_, fd);
81 }
82
83 private:
84 std::string output_;
85};
86
87// Matcher used to emulate dump() by writing on its file descriptor.
88Action<WriteOnFdFunction> WriteOnFd(const std::string& output) {
89 return MakeAction(new WriteOnFdAction(output));
90}
91
92// Matcher for args using Android's Vector<String16> format
93// TODO: move it to some common testing library
94MATCHER_P(AndroidElementsAre, expected, "") {
95 std::ostringstream errors;
96 if (arg.size() != expected.size()) {
97 errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size()
98 << ")\n";
99 }
100 int i = 0;
101 std::ostringstream actual_stream, expected_stream;
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700102 for (const String16& actual : arg) {
Steven Morelanda0f7f2d2017-03-09 22:59:32 -0800103 std::string actual_str = String8(actual).c_str();
Felipe Leme343175a2016-08-02 18:57:37 -0700104 std::string expected_str = expected[i];
105 actual_stream << "'" << actual_str << "' ";
106 expected_stream << "'" << expected_str << "' ";
107 if (actual_str != expected_str) {
108 errors << " element mismatch at index " << i << "\n";
109 }
110 i++;
111 }
112
113 if (!errors.str().empty()) {
114 errors << "\nExpected args: " << expected_stream.str()
115 << "\nActual args: " << actual_stream.str();
116 *result_listener << errors.str();
117 return false;
118 }
119 return true;
120}
121
122// Custom action to sleep for timeout seconds
123ACTION_P(Sleep, timeout) {
124 sleep(timeout);
125}
126
127class DumpsysTest : public Test {
128 public:
Steven Moreland2c3cd832017-02-13 23:44:17 +0000129 DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() {
Felipe Leme343175a2016-08-02 18:57:37 -0700130 }
131
132 void ExpectListServices(std::vector<std::string> services) {
133 Vector<String16> services16;
134 for (auto& service : services) {
135 services16.add(String16(service.c_str()));
136 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700137 EXPECT_CALL(sm_, listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL))
Vishnu Nairf56042d2017-09-19 15:25:10 -0700138 .WillRepeatedly(Return(services16));
139 }
140
Vishnu Nair6a408532017-10-24 09:11:27 -0700141 void ExpectListServicesWithPriority(std::vector<std::string> services, int dumpFlags) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700142 Vector<String16> services16;
143 for (auto& service : services) {
144 services16.add(String16(service.c_str()));
145 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700146 EXPECT_CALL(sm_, listServices(dumpFlags)).WillRepeatedly(Return(services16));
Felipe Leme343175a2016-08-02 18:57:37 -0700147 }
148
149 sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
150 sp<BinderMock> binder_mock;
151 if (running) {
152 binder_mock = new BinderMock;
153 }
154 EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
155 return binder_mock;
156 }
157
158 void ExpectDump(const char* name, const std::string& output) {
159 sp<BinderMock> binder_mock = ExpectCheckService(name);
160 EXPECT_CALL(*binder_mock, dump(_, _))
161 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
162 }
163
164 void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
165 const std::string& output) {
166 sp<BinderMock> binder_mock = ExpectCheckService(name);
167 EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
168 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
169 }
170
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700171 sp<BinderMock> ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
Felipe Leme343175a2016-08-02 18:57:37 -0700172 sp<BinderMock> binder_mock = ExpectCheckService(name);
173 EXPECT_CALL(*binder_mock, dump(_, _))
174 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700175 return binder_mock;
Felipe Leme343175a2016-08-02 18:57:37 -0700176 }
177
178 void CallMain(const std::vector<std::string>& args) {
179 const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
180 int argc = (int)args.size() + 1;
181 int i = 1;
182 for (const std::string& arg : args) {
183 argv[i++] = arg.c_str();
184 }
185 CaptureStdout();
186 CaptureStderr();
187 int status = dump_.main(argc, const_cast<char**>(argv));
188 stdout_ = GetCapturedStdout();
189 stderr_ = GetCapturedStderr();
190 EXPECT_THAT(status, Eq(0));
191 }
192
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000193 void CallSingleService(const String16& serviceName, Vector<String16>& args, int priorityFlags,
194 bool supportsProto, std::chrono::duration<double>& elapsedDuration,
195 size_t& bytesWritten) {
196 CaptureStdout();
197 CaptureStderr();
198 dump_.setServiceArgs(args, supportsProto, priorityFlags);
Steven Moreland5a30d342019-10-08 13:53:28 -0700199 status_t status = dump_.startDumpThread(Dumpsys::Type::DUMP, serviceName, args);
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000200 EXPECT_THAT(status, Eq(0));
201 status = dump_.writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(500), false,
202 elapsedDuration, bytesWritten);
203 EXPECT_THAT(status, Eq(0));
204 dump_.stopDumpThread(/* dumpCompleted = */ true);
205 stdout_ = GetCapturedStdout();
206 stderr_ = GetCapturedStderr();
207 }
208
Steven Moreland2c3cd832017-02-13 23:44:17 +0000209 void AssertRunningServices(const std::vector<std::string>& services) {
Steven Moreland31dac352020-03-05 09:46:45 -0800210 std::string expected = "Currently running services:\n";
Felipe Leme343175a2016-08-02 18:57:37 -0700211 for (const std::string& service : services) {
212 expected.append(" ").append(service).append("\n");
213 }
214 EXPECT_THAT(stdout_, HasSubstr(expected));
215 }
216
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000217 void AssertOutput(const std::string& expected) {
218 EXPECT_THAT(stdout_, StrEq(expected));
219 }
220
Felipe Leme343175a2016-08-02 18:57:37 -0700221 void AssertOutputContains(const std::string& expected) {
222 EXPECT_THAT(stdout_, HasSubstr(expected));
223 }
224
225 void AssertDumped(const std::string& service, const std::string& dump) {
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000226 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800227 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Felipe Leme343175a2016-08-02 18:57:37 -0700228 }
229
Vishnu Nair6a408532017-10-24 09:11:27 -0700230 void AssertDumpedWithPriority(const std::string& service, const std::string& dump,
231 const char16_t* priorityType) {
232 std::string priority = String8(priorityType).c_str();
233 EXPECT_THAT(stdout_,
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000234 HasSubstr("DUMP OF SERVICE " + priority + " " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800235 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Vishnu Nair6a408532017-10-24 09:11:27 -0700236 }
237
Felipe Leme343175a2016-08-02 18:57:37 -0700238 void AssertNotDumped(const std::string& dump) {
239 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
240 }
241
242 void AssertStopped(const std::string& service) {
243 EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
244 }
245
246 ServiceManagerMock sm_;
247 Dumpsys dump_;
248
249 private:
250 std::string stdout_, stderr_;
251};
252
Felipe Leme343175a2016-08-02 18:57:37 -0700253// Tests 'dumpsys -l' when all services are running
254TEST_F(DumpsysTest, ListAllServices) {
255 ExpectListServices({"Locksmith", "Valet"});
256 ExpectCheckService("Locksmith");
257 ExpectCheckService("Valet");
258
259 CallMain({"-l"});
260
261 AssertRunningServices({"Locksmith", "Valet"});
262}
263
Steven Moreland31dac352020-03-05 09:46:45 -0800264TEST_F(DumpsysTest, ListServicesOneRegistered) {
265 ExpectListServices({"Locksmith"});
266 ExpectCheckService("Locksmith");
267
268 CallMain({"-l"});
269
270 AssertRunningServices({"Locksmith"});
271}
272
273TEST_F(DumpsysTest, ListServicesEmpty) {
274 CallMain({"-l"});
275
276 AssertRunningServices({});
277}
278
Felipe Leme343175a2016-08-02 18:57:37 -0700279// Tests 'dumpsys -l' when a service is not running
280TEST_F(DumpsysTest, ListRunningServices) {
281 ExpectListServices({"Locksmith", "Valet"});
282 ExpectCheckService("Locksmith");
283 ExpectCheckService("Valet", false);
284
285 CallMain({"-l"});
286
287 AssertRunningServices({"Locksmith"});
288 AssertNotDumped({"Valet"});
289}
290
Vishnu Nairf56042d2017-09-19 15:25:10 -0700291// Tests 'dumpsys -l --priority HIGH'
292TEST_F(DumpsysTest, ListAllServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700293 ExpectListServicesWithPriority({"Locksmith", "Valet"}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700294 ExpectCheckService("Locksmith");
295 ExpectCheckService("Valet");
296
297 CallMain({"-l", "--priority", "HIGH"});
298
299 AssertRunningServices({"Locksmith", "Valet"});
300}
301
302// Tests 'dumpsys -l --priority HIGH' with and empty list
303TEST_F(DumpsysTest, ListEmptyServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700304 ExpectListServicesWithPriority({}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700305
306 CallMain({"-l", "--priority", "HIGH"});
307
308 AssertRunningServices({});
309}
310
Vishnu Nair6a408532017-10-24 09:11:27 -0700311// Tests 'dumpsys -l --proto'
312TEST_F(DumpsysTest, ListAllServicesWithProto) {
313 ExpectListServicesWithPriority({"Locksmith", "Valet", "Car"},
314 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
315 ExpectListServicesWithPriority({"Valet", "Car"}, IServiceManager::DUMP_FLAG_PROTO);
316 ExpectCheckService("Car");
317 ExpectCheckService("Valet");
318
319 CallMain({"-l", "--proto"});
320
321 AssertRunningServices({"Car", "Valet"});
322}
323
Felipe Leme343175a2016-08-02 18:57:37 -0700324// Tests 'dumpsys service_name' on a service is running
325TEST_F(DumpsysTest, DumpRunningService) {
326 ExpectDump("Valet", "Here's your car");
327
328 CallMain({"Valet"});
329
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000330 AssertOutput("Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700331}
332
333// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
Vishnu Nair6921f802017-11-22 09:17:23 -0800334TEST_F(DumpsysTest, DumpRunningServiceTimeoutInSec) {
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700335 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700336
337 CallMain({"-t", "1", "Valet"});
338
Vishnu Nair6921f802017-11-22 09:17:23 -0800339 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1000ms) 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 -T 500 service_name' on a service that times out after 2s
347TEST_F(DumpsysTest, DumpRunningServiceTimeoutInMs) {
348 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
349
350 CallMain({"-T", "500", "Valet"});
351
352 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (500ms) EXPIRED");
Felipe Leme343175a2016-08-02 18:57:37 -0700353 AssertNotDumped("Here's your car");
354
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700355 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
356 Mock::AllowLeak(binder_mock.get());
Felipe Leme343175a2016-08-02 18:57:37 -0700357}
358
359// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
360TEST_F(DumpsysTest, DumpWithArgsRunningService) {
361 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
362
363 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
364
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000365 AssertOutput("I DO!");
Felipe Leme343175a2016-08-02 18:57:37 -0700366}
367
Vishnu Nair64afc022018-02-01 15:29:34 -0800368// Tests dumpsys passes the -a flag when called on all services
369TEST_F(DumpsysTest, PassAllFlagsToServices) {
370 ExpectListServices({"Locksmith", "Valet"});
371 ExpectCheckService("Locksmith");
372 ExpectCheckService("Valet");
373 ExpectDumpWithArgs("Locksmith", {"-a"}, "dumped1");
374 ExpectDumpWithArgs("Valet", {"-a"}, "dumped2");
375
376 CallMain({"-T", "500"});
377
378 AssertDumped("Locksmith", "dumped1");
379 AssertDumped("Valet", "dumped2");
380}
381
382// Tests dumpsys passes the -a flag when called on NORMAL priority services
383TEST_F(DumpsysTest, PassAllFlagsToNormalServices) {
384 ExpectListServicesWithPriority({"Locksmith", "Valet"},
385 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
386 ExpectCheckService("Locksmith");
387 ExpectCheckService("Valet");
Vishnu Nair3919e1c2018-05-01 17:01:00 -0700388 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "NORMAL", "-a"}, "dump1");
389 ExpectDumpWithArgs("Valet", {"--dump-priority", "NORMAL", "-a"}, "dump2");
Vishnu Nair64afc022018-02-01 15:29:34 -0800390
391 CallMain({"--priority", "NORMAL"});
392
393 AssertDumped("Locksmith", "dump1");
394 AssertDumped("Valet", "dump2");
395}
396
397// Tests dumpsys passes only priority flags when called on CRITICAL priority services
398TEST_F(DumpsysTest, PassPriorityFlagsToCriticalServices) {
399 ExpectListServicesWithPriority({"Locksmith", "Valet"},
400 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
401 ExpectCheckService("Locksmith");
402 ExpectCheckService("Valet");
403 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "CRITICAL"}, "dump1");
404 ExpectDumpWithArgs("Valet", {"--dump-priority", "CRITICAL"}, "dump2");
405
406 CallMain({"--priority", "CRITICAL"});
407
408 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
409 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
410}
411
412// Tests dumpsys passes only priority flags when called on HIGH priority services
413TEST_F(DumpsysTest, PassPriorityFlagsToHighServices) {
414 ExpectListServicesWithPriority({"Locksmith", "Valet"},
415 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
416 ExpectCheckService("Locksmith");
417 ExpectCheckService("Valet");
418 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "HIGH"}, "dump1");
419 ExpectDumpWithArgs("Valet", {"--dump-priority", "HIGH"}, "dump2");
420
421 CallMain({"--priority", "HIGH"});
422
423 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
424 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
425}
426
Felipe Leme343175a2016-08-02 18:57:37 -0700427// Tests 'dumpsys' with no arguments
428TEST_F(DumpsysTest, DumpMultipleServices) {
429 ExpectListServices({"running1", "stopped2", "running3"});
430 ExpectDump("running1", "dump1");
431 ExpectCheckService("stopped2", false);
432 ExpectDump("running3", "dump3");
433
434 CallMain({});
435
436 AssertRunningServices({"running1", "running3"});
437 AssertDumped("running1", "dump1");
438 AssertStopped("stopped2");
439 AssertDumped("running3", "dump3");
440}
441
442// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
443TEST_F(DumpsysTest, DumpWithSkip) {
444 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
445 ExpectDump("running1", "dump1");
446 ExpectCheckService("stopped2", false);
447 ExpectDump("skipped3", "dump3");
448 ExpectDump("running4", "dump4");
449 ExpectDump("skipped5", "dump5");
450
451 CallMain({"--skip", "skipped3", "skipped5"});
452
453 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
454 AssertDumped("running1", "dump1");
455 AssertDumped("running4", "dump4");
456 AssertStopped("stopped2");
457 AssertNotDumped("dump3");
458 AssertNotDumped("dump5");
459}
Vishnu Nairf56042d2017-09-19 15:25:10 -0700460
461// Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services
462TEST_F(DumpsysTest, DumpWithSkipAndPriority) {
463 ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700464 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700465 ExpectDump("running1", "dump1");
466 ExpectCheckService("stopped2", false);
467 ExpectDump("skipped3", "dump3");
468 ExpectDump("running4", "dump4");
469 ExpectDump("skipped5", "dump5");
470
471 CallMain({"--priority", "CRITICAL", "--skip", "skipped3", "skipped5"});
472
473 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700474 AssertDumpedWithPriority("running1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
475 AssertDumpedWithPriority("running4", "dump4", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700476 AssertStopped("stopped2");
477 AssertNotDumped("dump3");
478 AssertNotDumped("dump5");
479}
480
481// Tests 'dumpsys --priority CRITICAL'
482TEST_F(DumpsysTest, DumpWithPriorityCritical) {
483 ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700484 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700485 ExpectDump("runningcritical1", "dump1");
486 ExpectDump("runningcritical2", "dump2");
487
488 CallMain({"--priority", "CRITICAL"});
489
490 AssertRunningServices({"runningcritical1", "runningcritical2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700491 AssertDumpedWithPriority("runningcritical1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
492 AssertDumpedWithPriority("runningcritical2", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700493}
494
495// Tests 'dumpsys --priority HIGH'
496TEST_F(DumpsysTest, DumpWithPriorityHigh) {
497 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700498 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700499 ExpectDump("runninghigh1", "dump1");
500 ExpectDump("runninghigh2", "dump2");
501
502 CallMain({"--priority", "HIGH"});
503
504 AssertRunningServices({"runninghigh1", "runninghigh2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700505 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
506 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700507}
508
509// Tests 'dumpsys --priority NORMAL'
510TEST_F(DumpsysTest, DumpWithPriorityNormal) {
511 ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700512 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700513 ExpectDump("runningnormal1", "dump1");
514 ExpectDump("runningnormal2", "dump2");
515
516 CallMain({"--priority", "NORMAL"});
517
518 AssertRunningServices({"runningnormal1", "runningnormal2"});
Vishnu Naire4f61742017-12-21 08:30:28 -0800519 AssertDumped("runningnormal1", "dump1");
520 AssertDumped("runningnormal2", "dump2");
Vishnu Nair6a408532017-10-24 09:11:27 -0700521}
522
523// Tests 'dumpsys --proto'
524TEST_F(DumpsysTest, DumpWithProto) {
525 ExpectListServicesWithPriority({"run8", "run1", "run2", "run5"},
526 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
527 ExpectListServicesWithPriority({"run3", "run2", "run4", "run8"},
528 IServiceManager::DUMP_FLAG_PROTO);
529 ExpectDump("run2", "dump1");
530 ExpectDump("run8", "dump2");
531
532 CallMain({"--proto"});
533
534 AssertRunningServices({"run2", "run8"});
535 AssertDumped("run2", "dump1");
536 AssertDumped("run8", "dump2");
537}
538
539// Tests 'dumpsys --priority HIGH --proto'
540TEST_F(DumpsysTest, DumpWithPriorityHighAndProto) {
541 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
542 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
543 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2", "runninghigh3"},
544 IServiceManager::DUMP_FLAG_PROTO);
545
546 ExpectDump("runninghigh1", "dump1");
547 ExpectDump("runninghigh2", "dump2");
548
549 CallMain({"--priority", "HIGH", "--proto"});
550
551 AssertRunningServices({"runninghigh1", "runninghigh2"});
552 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
553 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700554}
Vishnu Naire4f61742017-12-21 08:30:28 -0800555
Steven Moreland5a30d342019-10-08 13:53:28 -0700556// Tests 'dumpsys --pid'
557TEST_F(DumpsysTest, ListAllServicesWithPid) {
558 ExpectListServices({"Locksmith", "Valet"});
559 ExpectCheckService("Locksmith");
560 ExpectCheckService("Valet");
561
562 CallMain({"--pid"});
563
564 AssertRunningServices({"Locksmith", "Valet"});
565 AssertOutputContains(std::to_string(getpid()));
566}
567
568// Tests 'dumpsys --pid service_name'
569TEST_F(DumpsysTest, ListServiceWithPid) {
570 ExpectCheckService("Locksmith");
571
572 CallMain({"--pid", "Locksmith"});
573
574 AssertOutput(std::to_string(getpid()) + "\n");
575}
576
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000577TEST_F(DumpsysTest, GetBytesWritten) {
578 const char* serviceName = "service2";
579 const char* dumpContents = "dump1";
580 ExpectDump(serviceName, dumpContents);
581
582 String16 service(serviceName);
583 Vector<String16> args;
584 std::chrono::duration<double> elapsedDuration;
585 size_t bytesWritten;
586
587 CallSingleService(service, args, IServiceManager::DUMP_FLAG_PRIORITY_ALL,
588 /* as_proto = */ false, elapsedDuration, bytesWritten);
589
590 AssertOutput(dumpContents);
591 EXPECT_THAT(bytesWritten, Eq(strlen(dumpContents)));
592}
593
Vishnu Naire4f61742017-12-21 08:30:28 -0800594TEST_F(DumpsysTest, WriteDumpWithoutThreadStart) {
595 std::chrono::duration<double> elapsedDuration;
596 size_t bytesWritten;
597 status_t status =
598 dump_.writeDump(STDOUT_FILENO, String16("service"), std::chrono::milliseconds(500),
599 /* as_proto = */ false, elapsedDuration, bytesWritten);
600 EXPECT_THAT(status, Eq(INVALID_OPERATION));
Steven Moreland5a30d342019-10-08 13:53:28 -0700601}