blob: fa63db553338e0215c3cf45dba35a00701cce9f1 [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&));
Felipe Leme343175a2016-08-02 18:57:37 -070063 protected:
64 MOCK_METHOD0(onAsBinder, IBinder*());
65};
66
67class BinderMock : public BBinder {
68 public:
69 BinderMock() {
70 }
71
72 MOCK_METHOD2(dump, status_t(int, const Vector<String16>&));
73};
74
75// gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher
76typedef void WriteOnFdFunction(int);
77
78class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
79 public:
80 explicit WriteOnFdAction(const std::string& output) : output_(output) {
81 }
82 virtual Result Perform(const ArgumentTuple& args) {
Haibo Huang21f36552018-07-10 19:48:18 -070083 int fd = ::testing::get<0>(args);
Felipe Leme343175a2016-08-02 18:57:37 -070084 android::base::WriteStringToFd(output_, fd);
85 }
86
87 private:
88 std::string output_;
89};
90
91// Matcher used to emulate dump() by writing on its file descriptor.
92Action<WriteOnFdFunction> WriteOnFd(const std::string& output) {
93 return MakeAction(new WriteOnFdAction(output));
94}
95
96// Matcher for args using Android's Vector<String16> format
97// TODO: move it to some common testing library
98MATCHER_P(AndroidElementsAre, expected, "") {
99 std::ostringstream errors;
100 if (arg.size() != expected.size()) {
101 errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size()
102 << ")\n";
103 }
104 int i = 0;
105 std::ostringstream actual_stream, expected_stream;
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700106 for (const String16& actual : arg) {
Steven Morelanda0f7f2d2017-03-09 22:59:32 -0800107 std::string actual_str = String8(actual).c_str();
Felipe Leme343175a2016-08-02 18:57:37 -0700108 std::string expected_str = expected[i];
109 actual_stream << "'" << actual_str << "' ";
110 expected_stream << "'" << expected_str << "' ";
111 if (actual_str != expected_str) {
112 errors << " element mismatch at index " << i << "\n";
113 }
114 i++;
115 }
116
117 if (!errors.str().empty()) {
118 errors << "\nExpected args: " << expected_stream.str()
119 << "\nActual args: " << actual_stream.str();
120 *result_listener << errors.str();
121 return false;
122 }
123 return true;
124}
125
126// Custom action to sleep for timeout seconds
127ACTION_P(Sleep, timeout) {
128 sleep(timeout);
129}
130
131class DumpsysTest : public Test {
132 public:
Steven Moreland2c3cd832017-02-13 23:44:17 +0000133 DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() {
Felipe Leme343175a2016-08-02 18:57:37 -0700134 }
135
136 void ExpectListServices(std::vector<std::string> services) {
137 Vector<String16> services16;
138 for (auto& service : services) {
139 services16.add(String16(service.c_str()));
140 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700141 EXPECT_CALL(sm_, listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL))
Vishnu Nairf56042d2017-09-19 15:25:10 -0700142 .WillRepeatedly(Return(services16));
143 }
144
Vishnu Nair6a408532017-10-24 09:11:27 -0700145 void ExpectListServicesWithPriority(std::vector<std::string> services, int dumpFlags) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700146 Vector<String16> services16;
147 for (auto& service : services) {
148 services16.add(String16(service.c_str()));
149 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700150 EXPECT_CALL(sm_, listServices(dumpFlags)).WillRepeatedly(Return(services16));
Felipe Leme343175a2016-08-02 18:57:37 -0700151 }
152
153 sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
154 sp<BinderMock> binder_mock;
155 if (running) {
156 binder_mock = new BinderMock;
157 }
158 EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
159 return binder_mock;
160 }
161
162 void ExpectDump(const char* name, const std::string& output) {
163 sp<BinderMock> binder_mock = ExpectCheckService(name);
164 EXPECT_CALL(*binder_mock, dump(_, _))
165 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
166 }
167
168 void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
169 const std::string& output) {
170 sp<BinderMock> binder_mock = ExpectCheckService(name);
171 EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
172 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
173 }
174
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700175 sp<BinderMock> ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
Felipe Leme343175a2016-08-02 18:57:37 -0700176 sp<BinderMock> binder_mock = ExpectCheckService(name);
177 EXPECT_CALL(*binder_mock, dump(_, _))
178 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700179 return binder_mock;
Felipe Leme343175a2016-08-02 18:57:37 -0700180 }
181
182 void CallMain(const std::vector<std::string>& args) {
183 const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
184 int argc = (int)args.size() + 1;
185 int i = 1;
186 for (const std::string& arg : args) {
187 argv[i++] = arg.c_str();
188 }
189 CaptureStdout();
190 CaptureStderr();
191 int status = dump_.main(argc, const_cast<char**>(argv));
192 stdout_ = GetCapturedStdout();
193 stderr_ = GetCapturedStderr();
194 EXPECT_THAT(status, Eq(0));
195 }
196
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000197 void CallSingleService(const String16& serviceName, Vector<String16>& args, int priorityFlags,
198 bool supportsProto, std::chrono::duration<double>& elapsedDuration,
199 size_t& bytesWritten) {
200 CaptureStdout();
201 CaptureStderr();
202 dump_.setServiceArgs(args, supportsProto, priorityFlags);
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000203 status_t status = dump_.startDumpThread(Dumpsys::TYPE_DUMP, serviceName, args);
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000204 EXPECT_THAT(status, Eq(0));
205 status = dump_.writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(500), false,
206 elapsedDuration, bytesWritten);
207 EXPECT_THAT(status, Eq(0));
208 dump_.stopDumpThread(/* dumpCompleted = */ true);
209 stdout_ = GetCapturedStdout();
210 stderr_ = GetCapturedStderr();
211 }
212
Steven Moreland2c3cd832017-02-13 23:44:17 +0000213 void AssertRunningServices(const std::vector<std::string>& services) {
Steven Moreland31dac352020-03-05 09:46:45 -0800214 std::string expected = "Currently running services:\n";
Felipe Leme343175a2016-08-02 18:57:37 -0700215 for (const std::string& service : services) {
216 expected.append(" ").append(service).append("\n");
217 }
218 EXPECT_THAT(stdout_, HasSubstr(expected));
219 }
220
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000221 void AssertOutput(const std::string& expected) {
222 EXPECT_THAT(stdout_, StrEq(expected));
223 }
224
Felipe Leme343175a2016-08-02 18:57:37 -0700225 void AssertOutputContains(const std::string& expected) {
226 EXPECT_THAT(stdout_, HasSubstr(expected));
227 }
228
Devin Moorecfeeda42020-12-08 12:50:58 -0800229 void AssertOutputFormat(const std::string format) {
230 EXPECT_THAT(stdout_, testing::MatchesRegex(format));
231 }
232
Felipe Leme343175a2016-08-02 18:57:37 -0700233 void AssertDumped(const std::string& service, const std::string& dump) {
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000234 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800235 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Felipe Leme343175a2016-08-02 18:57:37 -0700236 }
237
Vishnu Nair6a408532017-10-24 09:11:27 -0700238 void AssertDumpedWithPriority(const std::string& service, const std::string& dump,
239 const char16_t* priorityType) {
240 std::string priority = String8(priorityType).c_str();
241 EXPECT_THAT(stdout_,
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000242 HasSubstr("DUMP OF SERVICE " + priority + " " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800243 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Vishnu Nair6a408532017-10-24 09:11:27 -0700244 }
245
Felipe Leme343175a2016-08-02 18:57:37 -0700246 void AssertNotDumped(const std::string& dump) {
247 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
248 }
249
250 void AssertStopped(const std::string& service) {
251 EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
252 }
253
254 ServiceManagerMock sm_;
255 Dumpsys dump_;
256
257 private:
258 std::string stdout_, stderr_;
259};
260
Felipe Leme343175a2016-08-02 18:57:37 -0700261// Tests 'dumpsys -l' when all services are running
262TEST_F(DumpsysTest, ListAllServices) {
263 ExpectListServices({"Locksmith", "Valet"});
264 ExpectCheckService("Locksmith");
265 ExpectCheckService("Valet");
266
267 CallMain({"-l"});
268
269 AssertRunningServices({"Locksmith", "Valet"});
270}
271
Steven Moreland31dac352020-03-05 09:46:45 -0800272TEST_F(DumpsysTest, ListServicesOneRegistered) {
273 ExpectListServices({"Locksmith"});
274 ExpectCheckService("Locksmith");
275
276 CallMain({"-l"});
277
278 AssertRunningServices({"Locksmith"});
279}
280
281TEST_F(DumpsysTest, ListServicesEmpty) {
282 CallMain({"-l"});
283
284 AssertRunningServices({});
285}
286
Felipe Leme343175a2016-08-02 18:57:37 -0700287// Tests 'dumpsys -l' when a service is not running
288TEST_F(DumpsysTest, ListRunningServices) {
289 ExpectListServices({"Locksmith", "Valet"});
290 ExpectCheckService("Locksmith");
291 ExpectCheckService("Valet", false);
292
293 CallMain({"-l"});
294
295 AssertRunningServices({"Locksmith"});
296 AssertNotDumped({"Valet"});
297}
298
Vishnu Nairf56042d2017-09-19 15:25:10 -0700299// Tests 'dumpsys -l --priority HIGH'
300TEST_F(DumpsysTest, ListAllServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700301 ExpectListServicesWithPriority({"Locksmith", "Valet"}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700302 ExpectCheckService("Locksmith");
303 ExpectCheckService("Valet");
304
305 CallMain({"-l", "--priority", "HIGH"});
306
307 AssertRunningServices({"Locksmith", "Valet"});
308}
309
310// Tests 'dumpsys -l --priority HIGH' with and empty list
311TEST_F(DumpsysTest, ListEmptyServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700312 ExpectListServicesWithPriority({}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700313
314 CallMain({"-l", "--priority", "HIGH"});
315
316 AssertRunningServices({});
317}
318
Vishnu Nair6a408532017-10-24 09:11:27 -0700319// Tests 'dumpsys -l --proto'
320TEST_F(DumpsysTest, ListAllServicesWithProto) {
321 ExpectListServicesWithPriority({"Locksmith", "Valet", "Car"},
322 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
323 ExpectListServicesWithPriority({"Valet", "Car"}, IServiceManager::DUMP_FLAG_PROTO);
324 ExpectCheckService("Car");
325 ExpectCheckService("Valet");
326
327 CallMain({"-l", "--proto"});
328
329 AssertRunningServices({"Car", "Valet"});
330}
331
Felipe Leme343175a2016-08-02 18:57:37 -0700332// Tests 'dumpsys service_name' on a service is running
333TEST_F(DumpsysTest, DumpRunningService) {
334 ExpectDump("Valet", "Here's your car");
335
336 CallMain({"Valet"});
337
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000338 AssertOutput("Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700339}
340
341// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
Vishnu Nair6921f802017-11-22 09:17:23 -0800342TEST_F(DumpsysTest, DumpRunningServiceTimeoutInSec) {
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700343 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700344
345 CallMain({"-t", "1", "Valet"});
346
Vishnu Nair6921f802017-11-22 09:17:23 -0800347 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1000ms) EXPIRED");
348 AssertNotDumped("Here's your car");
349
350 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
351 Mock::AllowLeak(binder_mock.get());
352}
353
354// Tests 'dumpsys -T 500 service_name' on a service that times out after 2s
355TEST_F(DumpsysTest, DumpRunningServiceTimeoutInMs) {
356 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
357
358 CallMain({"-T", "500", "Valet"});
359
360 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (500ms) EXPIRED");
Felipe Leme343175a2016-08-02 18:57:37 -0700361 AssertNotDumped("Here's your car");
362
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700363 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
364 Mock::AllowLeak(binder_mock.get());
Felipe Leme343175a2016-08-02 18:57:37 -0700365}
366
367// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
368TEST_F(DumpsysTest, DumpWithArgsRunningService) {
369 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
370
371 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
372
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000373 AssertOutput("I DO!");
Felipe Leme343175a2016-08-02 18:57:37 -0700374}
375
Vishnu Nair64afc022018-02-01 15:29:34 -0800376// Tests dumpsys passes the -a flag when called on all services
377TEST_F(DumpsysTest, PassAllFlagsToServices) {
378 ExpectListServices({"Locksmith", "Valet"});
379 ExpectCheckService("Locksmith");
380 ExpectCheckService("Valet");
381 ExpectDumpWithArgs("Locksmith", {"-a"}, "dumped1");
382 ExpectDumpWithArgs("Valet", {"-a"}, "dumped2");
383
384 CallMain({"-T", "500"});
385
386 AssertDumped("Locksmith", "dumped1");
387 AssertDumped("Valet", "dumped2");
388}
389
390// Tests dumpsys passes the -a flag when called on NORMAL priority services
391TEST_F(DumpsysTest, PassAllFlagsToNormalServices) {
392 ExpectListServicesWithPriority({"Locksmith", "Valet"},
393 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
394 ExpectCheckService("Locksmith");
395 ExpectCheckService("Valet");
Vishnu Nair3919e1c2018-05-01 17:01:00 -0700396 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "NORMAL", "-a"}, "dump1");
397 ExpectDumpWithArgs("Valet", {"--dump-priority", "NORMAL", "-a"}, "dump2");
Vishnu Nair64afc022018-02-01 15:29:34 -0800398
399 CallMain({"--priority", "NORMAL"});
400
401 AssertDumped("Locksmith", "dump1");
402 AssertDumped("Valet", "dump2");
403}
404
405// Tests dumpsys passes only priority flags when called on CRITICAL priority services
406TEST_F(DumpsysTest, PassPriorityFlagsToCriticalServices) {
407 ExpectListServicesWithPriority({"Locksmith", "Valet"},
408 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
409 ExpectCheckService("Locksmith");
410 ExpectCheckService("Valet");
411 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "CRITICAL"}, "dump1");
412 ExpectDumpWithArgs("Valet", {"--dump-priority", "CRITICAL"}, "dump2");
413
414 CallMain({"--priority", "CRITICAL"});
415
416 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
417 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
418}
419
420// Tests dumpsys passes only priority flags when called on HIGH priority services
421TEST_F(DumpsysTest, PassPriorityFlagsToHighServices) {
422 ExpectListServicesWithPriority({"Locksmith", "Valet"},
423 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
424 ExpectCheckService("Locksmith");
425 ExpectCheckService("Valet");
426 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "HIGH"}, "dump1");
427 ExpectDumpWithArgs("Valet", {"--dump-priority", "HIGH"}, "dump2");
428
429 CallMain({"--priority", "HIGH"});
430
431 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
432 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
433}
434
Felipe Leme343175a2016-08-02 18:57:37 -0700435// Tests 'dumpsys' with no arguments
436TEST_F(DumpsysTest, DumpMultipleServices) {
437 ExpectListServices({"running1", "stopped2", "running3"});
438 ExpectDump("running1", "dump1");
439 ExpectCheckService("stopped2", false);
440 ExpectDump("running3", "dump3");
441
442 CallMain({});
443
444 AssertRunningServices({"running1", "running3"});
445 AssertDumped("running1", "dump1");
446 AssertStopped("stopped2");
447 AssertDumped("running3", "dump3");
448}
449
450// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
451TEST_F(DumpsysTest, DumpWithSkip) {
452 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
453 ExpectDump("running1", "dump1");
454 ExpectCheckService("stopped2", false);
455 ExpectDump("skipped3", "dump3");
456 ExpectDump("running4", "dump4");
457 ExpectDump("skipped5", "dump5");
458
459 CallMain({"--skip", "skipped3", "skipped5"});
460
461 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
462 AssertDumped("running1", "dump1");
463 AssertDumped("running4", "dump4");
464 AssertStopped("stopped2");
465 AssertNotDumped("dump3");
466 AssertNotDumped("dump5");
467}
Vishnu Nairf56042d2017-09-19 15:25:10 -0700468
469// Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services
470TEST_F(DumpsysTest, DumpWithSkipAndPriority) {
471 ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700472 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700473 ExpectDump("running1", "dump1");
474 ExpectCheckService("stopped2", false);
475 ExpectDump("skipped3", "dump3");
476 ExpectDump("running4", "dump4");
477 ExpectDump("skipped5", "dump5");
478
479 CallMain({"--priority", "CRITICAL", "--skip", "skipped3", "skipped5"});
480
481 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700482 AssertDumpedWithPriority("running1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
483 AssertDumpedWithPriority("running4", "dump4", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700484 AssertStopped("stopped2");
485 AssertNotDumped("dump3");
486 AssertNotDumped("dump5");
487}
488
489// Tests 'dumpsys --priority CRITICAL'
490TEST_F(DumpsysTest, DumpWithPriorityCritical) {
491 ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700492 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700493 ExpectDump("runningcritical1", "dump1");
494 ExpectDump("runningcritical2", "dump2");
495
496 CallMain({"--priority", "CRITICAL"});
497
498 AssertRunningServices({"runningcritical1", "runningcritical2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700499 AssertDumpedWithPriority("runningcritical1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
500 AssertDumpedWithPriority("runningcritical2", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700501}
502
503// Tests 'dumpsys --priority HIGH'
504TEST_F(DumpsysTest, DumpWithPriorityHigh) {
505 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700506 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700507 ExpectDump("runninghigh1", "dump1");
508 ExpectDump("runninghigh2", "dump2");
509
510 CallMain({"--priority", "HIGH"});
511
512 AssertRunningServices({"runninghigh1", "runninghigh2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700513 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
514 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700515}
516
517// Tests 'dumpsys --priority NORMAL'
518TEST_F(DumpsysTest, DumpWithPriorityNormal) {
519 ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700520 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700521 ExpectDump("runningnormal1", "dump1");
522 ExpectDump("runningnormal2", "dump2");
523
524 CallMain({"--priority", "NORMAL"});
525
526 AssertRunningServices({"runningnormal1", "runningnormal2"});
Vishnu Naire4f61742017-12-21 08:30:28 -0800527 AssertDumped("runningnormal1", "dump1");
528 AssertDumped("runningnormal2", "dump2");
Vishnu Nair6a408532017-10-24 09:11:27 -0700529}
530
531// Tests 'dumpsys --proto'
532TEST_F(DumpsysTest, DumpWithProto) {
533 ExpectListServicesWithPriority({"run8", "run1", "run2", "run5"},
534 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
535 ExpectListServicesWithPriority({"run3", "run2", "run4", "run8"},
536 IServiceManager::DUMP_FLAG_PROTO);
537 ExpectDump("run2", "dump1");
538 ExpectDump("run8", "dump2");
539
540 CallMain({"--proto"});
541
542 AssertRunningServices({"run2", "run8"});
543 AssertDumped("run2", "dump1");
544 AssertDumped("run8", "dump2");
545}
546
547// Tests 'dumpsys --priority HIGH --proto'
548TEST_F(DumpsysTest, DumpWithPriorityHighAndProto) {
549 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
550 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
551 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2", "runninghigh3"},
552 IServiceManager::DUMP_FLAG_PROTO);
553
554 ExpectDump("runninghigh1", "dump1");
555 ExpectDump("runninghigh2", "dump2");
556
557 CallMain({"--priority", "HIGH", "--proto"});
558
559 AssertRunningServices({"runninghigh1", "runninghigh2"});
560 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
561 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700562}
Vishnu Naire4f61742017-12-21 08:30:28 -0800563
Steven Moreland5a30d342019-10-08 13:53:28 -0700564// Tests 'dumpsys --pid'
565TEST_F(DumpsysTest, ListAllServicesWithPid) {
566 ExpectListServices({"Locksmith", "Valet"});
567 ExpectCheckService("Locksmith");
568 ExpectCheckService("Valet");
569
570 CallMain({"--pid"});
571
572 AssertRunningServices({"Locksmith", "Valet"});
573 AssertOutputContains(std::to_string(getpid()));
574}
575
576// Tests 'dumpsys --pid service_name'
577TEST_F(DumpsysTest, ListServiceWithPid) {
578 ExpectCheckService("Locksmith");
579
580 CallMain({"--pid", "Locksmith"});
581
582 AssertOutput(std::to_string(getpid()) + "\n");
583}
584
Steven Morelande5a6a872021-05-19 22:11:37 +0000585// Tests 'dumpsys --stability'
586TEST_F(DumpsysTest, ListAllServicesWithStability) {
587 ExpectListServices({"Locksmith", "Valet"});
588 ExpectCheckService("Locksmith");
589 ExpectCheckService("Valet");
590
591 CallMain({"--stability"});
592
593 AssertRunningServices({"Locksmith", "Valet"});
594 AssertOutputContains("stability");
595}
596
597// Tests 'dumpsys --stability service_name'
598TEST_F(DumpsysTest, ListServiceWithStability) {
599 ExpectCheckService("Locksmith");
600
601 CallMain({"--stability", "Locksmith"});
602
603 AssertOutputContains("stability");
604}
605
Devin Moorecfeeda42020-12-08 12:50:58 -0800606// Tests 'dumpsys --thread'
607TEST_F(DumpsysTest, ListAllServicesWithThread) {
608 ExpectListServices({"Locksmith", "Valet"});
609 ExpectCheckService("Locksmith");
610 ExpectCheckService("Valet");
611
612 CallMain({"--thread"});
613
614 AssertRunningServices({"Locksmith", "Valet"});
615
616 const std::string format("(.|\n)*((Threads in use: [0-9]+/[0-9]+)?\n-(.|\n)*){2}");
617 AssertOutputFormat(format);
618}
619
620// Tests 'dumpsys --thread service_name'
621TEST_F(DumpsysTest, ListServiceWithThread) {
622 ExpectCheckService("Locksmith");
623
624 CallMain({"--thread", "Locksmith"});
625 // returns an empty string without root enabled
626 const std::string format("(^$|Threads in use: [0-9]/[0-9]+\n)");
627 AssertOutputFormat(format);
628}
629
Devin Mooredef9fae2021-08-05 19:05:45 +0000630// Tests 'dumpsys --clients'
631TEST_F(DumpsysTest, ListAllServicesWithClients) {
632 ExpectListServices({"Locksmith", "Valet"});
633 ExpectCheckService("Locksmith");
634 ExpectCheckService("Valet");
635
636 CallMain({"--clients"});
637
638 AssertRunningServices({"Locksmith", "Valet"});
639
640 const std::string format("(.|\n)*((Client PIDs are not available for local binders.)(.|\n)*){2}");
641 AssertOutputFormat(format);
642}
643
644// Tests 'dumpsys --clients service_name'
645TEST_F(DumpsysTest, ListServiceWithClients) {
646 ExpectCheckService("Locksmith");
647
648 CallMain({"--clients", "Locksmith"});
649
650 const std::string format("Client PIDs are not available for local binders.\n");
651 AssertOutputFormat(format);
652}
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000653// Tests 'dumpsys --thread --stability'
654TEST_F(DumpsysTest, ListAllServicesWithMultipleOptions) {
655 ExpectListServices({"Locksmith", "Valet"});
656 ExpectCheckService("Locksmith");
657 ExpectCheckService("Valet");
658
659 CallMain({"--pid", "--stability"});
660 AssertRunningServices({"Locksmith", "Valet"});
661
662 AssertOutputContains(std::to_string(getpid()));
663 AssertOutputContains("stability");
664}
665
666// Tests 'dumpsys --pid --stability service_name'
667TEST_F(DumpsysTest, ListServiceWithMultipleOptions) {
668 ExpectCheckService("Locksmith");
669 CallMain({"--pid", "--stability", "Locksmith"});
670
671 AssertOutputContains(std::to_string(getpid()));
672 AssertOutputContains("stability");
673}
674
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000675TEST_F(DumpsysTest, GetBytesWritten) {
676 const char* serviceName = "service2";
677 const char* dumpContents = "dump1";
678 ExpectDump(serviceName, dumpContents);
679
680 String16 service(serviceName);
681 Vector<String16> args;
682 std::chrono::duration<double> elapsedDuration;
683 size_t bytesWritten;
684
685 CallSingleService(service, args, IServiceManager::DUMP_FLAG_PRIORITY_ALL,
686 /* as_proto = */ false, elapsedDuration, bytesWritten);
687
688 AssertOutput(dumpContents);
689 EXPECT_THAT(bytesWritten, Eq(strlen(dumpContents)));
690}
691
Vishnu Naire4f61742017-12-21 08:30:28 -0800692TEST_F(DumpsysTest, WriteDumpWithoutThreadStart) {
693 std::chrono::duration<double> elapsedDuration;
694 size_t bytesWritten;
695 status_t status =
696 dump_.writeDump(STDOUT_FILENO, String16("service"), std::chrono::milliseconds(500),
697 /* as_proto = */ false, elapsedDuration, bytesWritten);
698 EXPECT_THAT(status, Eq(INVALID_OPERATION));
Steven Moreland5a30d342019-10-08 13:53:28 -0700699}
Devin Moorecfeeda42020-12-08 12:50:58 -0800700
701int main(int argc, char** argv) {
702 ::testing::InitGoogleTest(&argc, argv);
703
704 // start a binder thread pool for testing --thread option
705 android::ProcessState::self()->setThreadPoolMaxThreadCount(8);
706 ProcessState::self()->startThreadPool();
707
708 return RUN_ALL_TESTS();
709}