blob: 49c131894534d5e9b5556a8def1bd646a90e5ff6 [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
Devin Moorecfeeda42020-12-08 12:50:58 -080019#include <regex>
Felipe Leme343175a2016-08-02 18:57:37 -070020#include <vector>
21
22#include <gmock/gmock.h>
23#include <gtest/gtest.h>
24
25#include <android-base/file.h>
Devin Moorecfeeda42020-12-08 12:50:58 -080026#include <binder/Binder.h>
27#include <binder/ProcessState.h>
Vishnu Nair6a408532017-10-24 09:11:27 -070028#include <serviceutils/PriorityDumper.h>
Felipe Leme343175a2016-08-02 18:57:37 -070029#include <utils/String16.h>
Steven Morelanda0f7f2d2017-03-09 22:59:32 -080030#include <utils/String8.h>
Felipe Leme343175a2016-08-02 18:57:37 -070031#include <utils/Vector.h>
32
33using namespace android;
34
35using ::testing::_;
36using ::testing::Action;
37using ::testing::ActionInterface;
38using ::testing::DoAll;
39using ::testing::Eq;
40using ::testing::HasSubstr;
41using ::testing::MakeAction;
Felipe Leme5c8a98f2017-08-25 13:39:04 -070042using ::testing::Mock;
Felipe Leme343175a2016-08-02 18:57:37 -070043using ::testing::Not;
44using ::testing::Return;
45using ::testing::StrEq;
46using ::testing::Test;
47using ::testing::WithArg;
48using ::testing::internal::CaptureStderr;
49using ::testing::internal::CaptureStdout;
50using ::testing::internal::GetCapturedStderr;
51using ::testing::internal::GetCapturedStdout;
52
53class ServiceManagerMock : public IServiceManager {
54 public:
55 MOCK_CONST_METHOD1(getService, sp<IBinder>(const String16&));
56 MOCK_CONST_METHOD1(checkService, sp<IBinder>(const String16&));
Vishnu Nairf56042d2017-09-19 15:25:10 -070057 MOCK_METHOD4(addService, status_t(const String16&, const sp<IBinder>&, bool, int));
58 MOCK_METHOD1(listServices, Vector<String16>(int));
Steven Moreland1c47b582019-08-27 18:05:27 -070059 MOCK_METHOD1(waitForService, sp<IBinder>(const String16&));
Steven Morelandb82b8f82019-10-28 10:52:34 -070060 MOCK_METHOD1(isDeclared, bool(const String16&));
Steven Moreland2e293aa2020-09-23 00:25:16 +000061 MOCK_METHOD1(getDeclaredInstances, Vector<String16>(const String16&));
Steven Morelandedd4e072021-04-21 00:27:29 +000062 MOCK_METHOD1(updatableViaApex, std::optional<String16>(const String16&));
Devin Moore5e4c2f12021-09-09 22:36:33 +000063 MOCK_METHOD1(getConnectionInfo, std::optional<ConnectionInfo>(const String16&));
Jayant Chowdhary30700942022-01-31 14:12:40 -080064 MOCK_METHOD2(registerForNotifications, status_t(const String16&,
65 const sp<LocalRegistrationCallback>&));
66 MOCK_METHOD2(unregisterForNotifications, status_t(const String16&,
67 const sp<LocalRegistrationCallback>&));
Felipe Leme343175a2016-08-02 18:57:37 -070068 protected:
69 MOCK_METHOD0(onAsBinder, IBinder*());
70};
71
72class BinderMock : public BBinder {
73 public:
74 BinderMock() {
75 }
76
77 MOCK_METHOD2(dump, status_t(int, const Vector<String16>&));
78};
79
80// gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher
81typedef void WriteOnFdFunction(int);
82
83class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
84 public:
85 explicit WriteOnFdAction(const std::string& output) : output_(output) {
86 }
87 virtual Result Perform(const ArgumentTuple& args) {
Haibo Huang21f36552018-07-10 19:48:18 -070088 int fd = ::testing::get<0>(args);
Felipe Leme343175a2016-08-02 18:57:37 -070089 android::base::WriteStringToFd(output_, fd);
90 }
91
92 private:
93 std::string output_;
94};
95
96// Matcher used to emulate dump() by writing on its file descriptor.
97Action<WriteOnFdFunction> WriteOnFd(const std::string& output) {
98 return MakeAction(new WriteOnFdAction(output));
99}
100
101// Matcher for args using Android's Vector<String16> format
102// TODO: move it to some common testing library
103MATCHER_P(AndroidElementsAre, expected, "") {
104 std::ostringstream errors;
105 if (arg.size() != expected.size()) {
106 errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size()
107 << ")\n";
108 }
109 int i = 0;
110 std::ostringstream actual_stream, expected_stream;
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700111 for (const String16& actual : arg) {
Steven Morelanda0f7f2d2017-03-09 22:59:32 -0800112 std::string actual_str = String8(actual).c_str();
Felipe Leme343175a2016-08-02 18:57:37 -0700113 std::string expected_str = expected[i];
114 actual_stream << "'" << actual_str << "' ";
115 expected_stream << "'" << expected_str << "' ";
116 if (actual_str != expected_str) {
117 errors << " element mismatch at index " << i << "\n";
118 }
119 i++;
120 }
121
122 if (!errors.str().empty()) {
123 errors << "\nExpected args: " << expected_stream.str()
124 << "\nActual args: " << actual_stream.str();
125 *result_listener << errors.str();
126 return false;
127 }
128 return true;
129}
130
131// Custom action to sleep for timeout seconds
132ACTION_P(Sleep, timeout) {
133 sleep(timeout);
134}
135
136class DumpsysTest : public Test {
137 public:
Steven Moreland2c3cd832017-02-13 23:44:17 +0000138 DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() {
Felipe Leme343175a2016-08-02 18:57:37 -0700139 }
140
141 void ExpectListServices(std::vector<std::string> services) {
142 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(IServiceManager::DUMP_FLAG_PRIORITY_ALL))
Vishnu Nairf56042d2017-09-19 15:25:10 -0700147 .WillRepeatedly(Return(services16));
148 }
149
Vishnu Nair6a408532017-10-24 09:11:27 -0700150 void ExpectListServicesWithPriority(std::vector<std::string> services, int dumpFlags) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700151 Vector<String16> services16;
152 for (auto& service : services) {
153 services16.add(String16(service.c_str()));
154 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700155 EXPECT_CALL(sm_, listServices(dumpFlags)).WillRepeatedly(Return(services16));
Felipe Leme343175a2016-08-02 18:57:37 -0700156 }
157
158 sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
159 sp<BinderMock> binder_mock;
160 if (running) {
161 binder_mock = new BinderMock;
162 }
163 EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
164 return binder_mock;
165 }
166
167 void ExpectDump(const char* name, const std::string& output) {
168 sp<BinderMock> binder_mock = ExpectCheckService(name);
169 EXPECT_CALL(*binder_mock, dump(_, _))
170 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
171 }
172
173 void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
174 const std::string& output) {
175 sp<BinderMock> binder_mock = ExpectCheckService(name);
176 EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
177 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
178 }
179
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700180 sp<BinderMock> ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
Felipe Leme343175a2016-08-02 18:57:37 -0700181 sp<BinderMock> binder_mock = ExpectCheckService(name);
182 EXPECT_CALL(*binder_mock, dump(_, _))
183 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700184 return binder_mock;
Felipe Leme343175a2016-08-02 18:57:37 -0700185 }
186
187 void CallMain(const std::vector<std::string>& args) {
188 const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
189 int argc = (int)args.size() + 1;
190 int i = 1;
191 for (const std::string& arg : args) {
192 argv[i++] = arg.c_str();
193 }
194 CaptureStdout();
195 CaptureStderr();
196 int status = dump_.main(argc, const_cast<char**>(argv));
197 stdout_ = GetCapturedStdout();
198 stderr_ = GetCapturedStderr();
199 EXPECT_THAT(status, Eq(0));
200 }
201
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000202 void CallSingleService(const String16& serviceName, Vector<String16>& args, int priorityFlags,
203 bool supportsProto, std::chrono::duration<double>& elapsedDuration,
204 size_t& bytesWritten) {
205 CaptureStdout();
206 CaptureStderr();
207 dump_.setServiceArgs(args, supportsProto, priorityFlags);
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000208 status_t status = dump_.startDumpThread(Dumpsys::TYPE_DUMP, serviceName, args);
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000209 EXPECT_THAT(status, Eq(0));
210 status = dump_.writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(500), false,
211 elapsedDuration, bytesWritten);
212 EXPECT_THAT(status, Eq(0));
213 dump_.stopDumpThread(/* dumpCompleted = */ true);
214 stdout_ = GetCapturedStdout();
215 stderr_ = GetCapturedStderr();
216 }
217
Steven Moreland2c3cd832017-02-13 23:44:17 +0000218 void AssertRunningServices(const std::vector<std::string>& services) {
Steven Moreland31dac352020-03-05 09:46:45 -0800219 std::string expected = "Currently running services:\n";
Felipe Leme343175a2016-08-02 18:57:37 -0700220 for (const std::string& service : services) {
221 expected.append(" ").append(service).append("\n");
222 }
223 EXPECT_THAT(stdout_, HasSubstr(expected));
224 }
225
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000226 void AssertOutput(const std::string& expected) {
227 EXPECT_THAT(stdout_, StrEq(expected));
228 }
229
Felipe Leme343175a2016-08-02 18:57:37 -0700230 void AssertOutputContains(const std::string& expected) {
231 EXPECT_THAT(stdout_, HasSubstr(expected));
232 }
233
Devin Moorecfeeda42020-12-08 12:50:58 -0800234 void AssertOutputFormat(const std::string format) {
235 EXPECT_THAT(stdout_, testing::MatchesRegex(format));
236 }
237
Felipe Leme343175a2016-08-02 18:57:37 -0700238 void AssertDumped(const std::string& service, const std::string& dump) {
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000239 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800240 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Felipe Leme343175a2016-08-02 18:57:37 -0700241 }
242
Vishnu Nair6a408532017-10-24 09:11:27 -0700243 void AssertDumpedWithPriority(const std::string& service, const std::string& dump,
244 const char16_t* priorityType) {
245 std::string priority = String8(priorityType).c_str();
246 EXPECT_THAT(stdout_,
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000247 HasSubstr("DUMP OF SERVICE " + priority + " " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800248 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Vishnu Nair6a408532017-10-24 09:11:27 -0700249 }
250
Felipe Leme343175a2016-08-02 18:57:37 -0700251 void AssertNotDumped(const std::string& dump) {
252 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
253 }
254
255 void AssertStopped(const std::string& service) {
256 EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
257 }
258
259 ServiceManagerMock sm_;
260 Dumpsys dump_;
261
262 private:
263 std::string stdout_, stderr_;
264};
265
Felipe Leme343175a2016-08-02 18:57:37 -0700266// Tests 'dumpsys -l' when all services are running
267TEST_F(DumpsysTest, ListAllServices) {
268 ExpectListServices({"Locksmith", "Valet"});
269 ExpectCheckService("Locksmith");
270 ExpectCheckService("Valet");
271
272 CallMain({"-l"});
273
274 AssertRunningServices({"Locksmith", "Valet"});
275}
276
Steven Moreland31dac352020-03-05 09:46:45 -0800277TEST_F(DumpsysTest, ListServicesOneRegistered) {
278 ExpectListServices({"Locksmith"});
279 ExpectCheckService("Locksmith");
280
281 CallMain({"-l"});
282
283 AssertRunningServices({"Locksmith"});
284}
285
286TEST_F(DumpsysTest, ListServicesEmpty) {
287 CallMain({"-l"});
288
289 AssertRunningServices({});
290}
291
Felipe Leme343175a2016-08-02 18:57:37 -0700292// Tests 'dumpsys -l' when a service is not running
293TEST_F(DumpsysTest, ListRunningServices) {
294 ExpectListServices({"Locksmith", "Valet"});
295 ExpectCheckService("Locksmith");
296 ExpectCheckService("Valet", false);
297
298 CallMain({"-l"});
299
300 AssertRunningServices({"Locksmith"});
301 AssertNotDumped({"Valet"});
302}
303
Vishnu Nairf56042d2017-09-19 15:25:10 -0700304// Tests 'dumpsys -l --priority HIGH'
305TEST_F(DumpsysTest, ListAllServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700306 ExpectListServicesWithPriority({"Locksmith", "Valet"}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700307 ExpectCheckService("Locksmith");
308 ExpectCheckService("Valet");
309
310 CallMain({"-l", "--priority", "HIGH"});
311
312 AssertRunningServices({"Locksmith", "Valet"});
313}
314
315// Tests 'dumpsys -l --priority HIGH' with and empty list
316TEST_F(DumpsysTest, ListEmptyServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700317 ExpectListServicesWithPriority({}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700318
319 CallMain({"-l", "--priority", "HIGH"});
320
321 AssertRunningServices({});
322}
323
Vishnu Nair6a408532017-10-24 09:11:27 -0700324// Tests 'dumpsys -l --proto'
325TEST_F(DumpsysTest, ListAllServicesWithProto) {
326 ExpectListServicesWithPriority({"Locksmith", "Valet", "Car"},
327 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
328 ExpectListServicesWithPriority({"Valet", "Car"}, IServiceManager::DUMP_FLAG_PROTO);
329 ExpectCheckService("Car");
330 ExpectCheckService("Valet");
331
332 CallMain({"-l", "--proto"});
333
334 AssertRunningServices({"Car", "Valet"});
335}
336
Felipe Leme343175a2016-08-02 18:57:37 -0700337// Tests 'dumpsys service_name' on a service is running
338TEST_F(DumpsysTest, DumpRunningService) {
339 ExpectDump("Valet", "Here's your car");
340
341 CallMain({"Valet"});
342
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000343 AssertOutput("Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700344}
345
346// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
Vishnu Nair6921f802017-11-22 09:17:23 -0800347TEST_F(DumpsysTest, DumpRunningServiceTimeoutInSec) {
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700348 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700349
350 CallMain({"-t", "1", "Valet"});
351
Vishnu Nair6921f802017-11-22 09:17:23 -0800352 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1000ms) EXPIRED");
353 AssertNotDumped("Here's your car");
354
355 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
356 Mock::AllowLeak(binder_mock.get());
357}
358
359// Tests 'dumpsys -T 500 service_name' on a service that times out after 2s
360TEST_F(DumpsysTest, DumpRunningServiceTimeoutInMs) {
361 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
362
363 CallMain({"-T", "500", "Valet"});
364
365 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (500ms) EXPIRED");
Felipe Leme343175a2016-08-02 18:57:37 -0700366 AssertNotDumped("Here's your car");
367
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700368 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
369 Mock::AllowLeak(binder_mock.get());
Felipe Leme343175a2016-08-02 18:57:37 -0700370}
371
372// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
373TEST_F(DumpsysTest, DumpWithArgsRunningService) {
374 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
375
376 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
377
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000378 AssertOutput("I DO!");
Felipe Leme343175a2016-08-02 18:57:37 -0700379}
380
Vishnu Nair64afc022018-02-01 15:29:34 -0800381// Tests dumpsys passes the -a flag when called on all services
382TEST_F(DumpsysTest, PassAllFlagsToServices) {
383 ExpectListServices({"Locksmith", "Valet"});
384 ExpectCheckService("Locksmith");
385 ExpectCheckService("Valet");
386 ExpectDumpWithArgs("Locksmith", {"-a"}, "dumped1");
387 ExpectDumpWithArgs("Valet", {"-a"}, "dumped2");
388
389 CallMain({"-T", "500"});
390
391 AssertDumped("Locksmith", "dumped1");
392 AssertDumped("Valet", "dumped2");
393}
394
395// Tests dumpsys passes the -a flag when called on NORMAL priority services
396TEST_F(DumpsysTest, PassAllFlagsToNormalServices) {
397 ExpectListServicesWithPriority({"Locksmith", "Valet"},
398 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
399 ExpectCheckService("Locksmith");
400 ExpectCheckService("Valet");
Vishnu Nair3919e1c2018-05-01 17:01:00 -0700401 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "NORMAL", "-a"}, "dump1");
402 ExpectDumpWithArgs("Valet", {"--dump-priority", "NORMAL", "-a"}, "dump2");
Vishnu Nair64afc022018-02-01 15:29:34 -0800403
404 CallMain({"--priority", "NORMAL"});
405
406 AssertDumped("Locksmith", "dump1");
407 AssertDumped("Valet", "dump2");
408}
409
410// Tests dumpsys passes only priority flags when called on CRITICAL priority services
411TEST_F(DumpsysTest, PassPriorityFlagsToCriticalServices) {
412 ExpectListServicesWithPriority({"Locksmith", "Valet"},
413 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
414 ExpectCheckService("Locksmith");
415 ExpectCheckService("Valet");
416 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "CRITICAL"}, "dump1");
417 ExpectDumpWithArgs("Valet", {"--dump-priority", "CRITICAL"}, "dump2");
418
419 CallMain({"--priority", "CRITICAL"});
420
421 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
422 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
423}
424
425// Tests dumpsys passes only priority flags when called on HIGH priority services
426TEST_F(DumpsysTest, PassPriorityFlagsToHighServices) {
427 ExpectListServicesWithPriority({"Locksmith", "Valet"},
428 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
429 ExpectCheckService("Locksmith");
430 ExpectCheckService("Valet");
431 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "HIGH"}, "dump1");
432 ExpectDumpWithArgs("Valet", {"--dump-priority", "HIGH"}, "dump2");
433
434 CallMain({"--priority", "HIGH"});
435
436 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
437 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
438}
439
Felipe Leme343175a2016-08-02 18:57:37 -0700440// Tests 'dumpsys' with no arguments
441TEST_F(DumpsysTest, DumpMultipleServices) {
442 ExpectListServices({"running1", "stopped2", "running3"});
443 ExpectDump("running1", "dump1");
444 ExpectCheckService("stopped2", false);
445 ExpectDump("running3", "dump3");
446
447 CallMain({});
448
449 AssertRunningServices({"running1", "running3"});
450 AssertDumped("running1", "dump1");
451 AssertStopped("stopped2");
452 AssertDumped("running3", "dump3");
453}
454
455// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
456TEST_F(DumpsysTest, DumpWithSkip) {
457 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
458 ExpectDump("running1", "dump1");
459 ExpectCheckService("stopped2", false);
460 ExpectDump("skipped3", "dump3");
461 ExpectDump("running4", "dump4");
462 ExpectDump("skipped5", "dump5");
463
464 CallMain({"--skip", "skipped3", "skipped5"});
465
466 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
467 AssertDumped("running1", "dump1");
468 AssertDumped("running4", "dump4");
469 AssertStopped("stopped2");
470 AssertNotDumped("dump3");
471 AssertNotDumped("dump5");
472}
Vishnu Nairf56042d2017-09-19 15:25:10 -0700473
474// Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services
475TEST_F(DumpsysTest, DumpWithSkipAndPriority) {
476 ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700477 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700478 ExpectDump("running1", "dump1");
479 ExpectCheckService("stopped2", false);
480 ExpectDump("skipped3", "dump3");
481 ExpectDump("running4", "dump4");
482 ExpectDump("skipped5", "dump5");
483
484 CallMain({"--priority", "CRITICAL", "--skip", "skipped3", "skipped5"});
485
486 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700487 AssertDumpedWithPriority("running1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
488 AssertDumpedWithPriority("running4", "dump4", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700489 AssertStopped("stopped2");
490 AssertNotDumped("dump3");
491 AssertNotDumped("dump5");
492}
493
494// Tests 'dumpsys --priority CRITICAL'
495TEST_F(DumpsysTest, DumpWithPriorityCritical) {
496 ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700497 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700498 ExpectDump("runningcritical1", "dump1");
499 ExpectDump("runningcritical2", "dump2");
500
501 CallMain({"--priority", "CRITICAL"});
502
503 AssertRunningServices({"runningcritical1", "runningcritical2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700504 AssertDumpedWithPriority("runningcritical1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
505 AssertDumpedWithPriority("runningcritical2", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700506}
507
508// Tests 'dumpsys --priority HIGH'
509TEST_F(DumpsysTest, DumpWithPriorityHigh) {
510 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700511 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700512 ExpectDump("runninghigh1", "dump1");
513 ExpectDump("runninghigh2", "dump2");
514
515 CallMain({"--priority", "HIGH"});
516
517 AssertRunningServices({"runninghigh1", "runninghigh2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700518 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
519 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700520}
521
522// Tests 'dumpsys --priority NORMAL'
523TEST_F(DumpsysTest, DumpWithPriorityNormal) {
524 ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700525 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700526 ExpectDump("runningnormal1", "dump1");
527 ExpectDump("runningnormal2", "dump2");
528
529 CallMain({"--priority", "NORMAL"});
530
531 AssertRunningServices({"runningnormal1", "runningnormal2"});
Vishnu Naire4f61742017-12-21 08:30:28 -0800532 AssertDumped("runningnormal1", "dump1");
533 AssertDumped("runningnormal2", "dump2");
Vishnu Nair6a408532017-10-24 09:11:27 -0700534}
535
536// Tests 'dumpsys --proto'
537TEST_F(DumpsysTest, DumpWithProto) {
538 ExpectListServicesWithPriority({"run8", "run1", "run2", "run5"},
539 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
540 ExpectListServicesWithPriority({"run3", "run2", "run4", "run8"},
541 IServiceManager::DUMP_FLAG_PROTO);
542 ExpectDump("run2", "dump1");
543 ExpectDump("run8", "dump2");
544
545 CallMain({"--proto"});
546
547 AssertRunningServices({"run2", "run8"});
548 AssertDumped("run2", "dump1");
549 AssertDumped("run8", "dump2");
550}
551
552// Tests 'dumpsys --priority HIGH --proto'
553TEST_F(DumpsysTest, DumpWithPriorityHighAndProto) {
554 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
555 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
556 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2", "runninghigh3"},
557 IServiceManager::DUMP_FLAG_PROTO);
558
559 ExpectDump("runninghigh1", "dump1");
560 ExpectDump("runninghigh2", "dump2");
561
562 CallMain({"--priority", "HIGH", "--proto"});
563
564 AssertRunningServices({"runninghigh1", "runninghigh2"});
565 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
566 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700567}
Vishnu Naire4f61742017-12-21 08:30:28 -0800568
Steven Moreland5a30d342019-10-08 13:53:28 -0700569// Tests 'dumpsys --pid'
570TEST_F(DumpsysTest, ListAllServicesWithPid) {
571 ExpectListServices({"Locksmith", "Valet"});
572 ExpectCheckService("Locksmith");
573 ExpectCheckService("Valet");
574
575 CallMain({"--pid"});
576
577 AssertRunningServices({"Locksmith", "Valet"});
578 AssertOutputContains(std::to_string(getpid()));
579}
580
581// Tests 'dumpsys --pid service_name'
582TEST_F(DumpsysTest, ListServiceWithPid) {
583 ExpectCheckService("Locksmith");
584
585 CallMain({"--pid", "Locksmith"});
586
587 AssertOutput(std::to_string(getpid()) + "\n");
588}
589
Steven Morelande5a6a872021-05-19 22:11:37 +0000590// Tests 'dumpsys --stability'
591TEST_F(DumpsysTest, ListAllServicesWithStability) {
592 ExpectListServices({"Locksmith", "Valet"});
593 ExpectCheckService("Locksmith");
594 ExpectCheckService("Valet");
595
596 CallMain({"--stability"});
597
598 AssertRunningServices({"Locksmith", "Valet"});
599 AssertOutputContains("stability");
600}
601
602// Tests 'dumpsys --stability service_name'
603TEST_F(DumpsysTest, ListServiceWithStability) {
604 ExpectCheckService("Locksmith");
605
606 CallMain({"--stability", "Locksmith"});
607
608 AssertOutputContains("stability");
609}
610
Devin Moorecfeeda42020-12-08 12:50:58 -0800611// Tests 'dumpsys --thread'
612TEST_F(DumpsysTest, ListAllServicesWithThread) {
613 ExpectListServices({"Locksmith", "Valet"});
614 ExpectCheckService("Locksmith");
615 ExpectCheckService("Valet");
616
617 CallMain({"--thread"});
618
619 AssertRunningServices({"Locksmith", "Valet"});
620
621 const std::string format("(.|\n)*((Threads in use: [0-9]+/[0-9]+)?\n-(.|\n)*){2}");
622 AssertOutputFormat(format);
623}
624
625// Tests 'dumpsys --thread service_name'
626TEST_F(DumpsysTest, ListServiceWithThread) {
627 ExpectCheckService("Locksmith");
628
629 CallMain({"--thread", "Locksmith"});
630 // returns an empty string without root enabled
631 const std::string format("(^$|Threads in use: [0-9]/[0-9]+\n)");
632 AssertOutputFormat(format);
633}
634
Devin Mooredef9fae2021-08-05 19:05:45 +0000635// Tests 'dumpsys --clients'
636TEST_F(DumpsysTest, ListAllServicesWithClients) {
637 ExpectListServices({"Locksmith", "Valet"});
638 ExpectCheckService("Locksmith");
639 ExpectCheckService("Valet");
640
641 CallMain({"--clients"});
642
643 AssertRunningServices({"Locksmith", "Valet"});
644
645 const std::string format("(.|\n)*((Client PIDs are not available for local binders.)(.|\n)*){2}");
646 AssertOutputFormat(format);
647}
648
649// Tests 'dumpsys --clients service_name'
650TEST_F(DumpsysTest, ListServiceWithClients) {
651 ExpectCheckService("Locksmith");
652
653 CallMain({"--clients", "Locksmith"});
654
655 const std::string format("Client PIDs are not available for local binders.\n");
656 AssertOutputFormat(format);
657}
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000658// Tests 'dumpsys --thread --stability'
659TEST_F(DumpsysTest, ListAllServicesWithMultipleOptions) {
660 ExpectListServices({"Locksmith", "Valet"});
661 ExpectCheckService("Locksmith");
662 ExpectCheckService("Valet");
663
664 CallMain({"--pid", "--stability"});
665 AssertRunningServices({"Locksmith", "Valet"});
666
667 AssertOutputContains(std::to_string(getpid()));
668 AssertOutputContains("stability");
669}
670
671// Tests 'dumpsys --pid --stability service_name'
672TEST_F(DumpsysTest, ListServiceWithMultipleOptions) {
673 ExpectCheckService("Locksmith");
674 CallMain({"--pid", "--stability", "Locksmith"});
675
676 AssertOutputContains(std::to_string(getpid()));
677 AssertOutputContains("stability");
678}
679
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000680TEST_F(DumpsysTest, GetBytesWritten) {
681 const char* serviceName = "service2";
682 const char* dumpContents = "dump1";
683 ExpectDump(serviceName, dumpContents);
684
685 String16 service(serviceName);
686 Vector<String16> args;
687 std::chrono::duration<double> elapsedDuration;
688 size_t bytesWritten;
689
690 CallSingleService(service, args, IServiceManager::DUMP_FLAG_PRIORITY_ALL,
691 /* as_proto = */ false, elapsedDuration, bytesWritten);
692
693 AssertOutput(dumpContents);
694 EXPECT_THAT(bytesWritten, Eq(strlen(dumpContents)));
695}
696
Vishnu Naire4f61742017-12-21 08:30:28 -0800697TEST_F(DumpsysTest, WriteDumpWithoutThreadStart) {
698 std::chrono::duration<double> elapsedDuration;
699 size_t bytesWritten;
700 status_t status =
701 dump_.writeDump(STDOUT_FILENO, String16("service"), std::chrono::milliseconds(500),
702 /* as_proto = */ false, elapsedDuration, bytesWritten);
703 EXPECT_THAT(status, Eq(INVALID_OPERATION));
Steven Moreland5a30d342019-10-08 13:53:28 -0700704}
Devin Moorecfeeda42020-12-08 12:50:58 -0800705
706int main(int argc, char** argv) {
707 ::testing::InitGoogleTest(&argc, argv);
708
709 // start a binder thread pool for testing --thread option
710 android::ProcessState::self()->setThreadPoolMaxThreadCount(8);
711 ProcessState::self()->startThreadPool();
712
713 return RUN_ALL_TESTS();
714}