blob: b8e5ce1a6322772572cebd8dcf230c341959f77b [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&));
Jooyung Han76944fe2022-10-25 17:02:45 +090063 MOCK_METHOD1(getUpdatableNames, Vector<String16>(const String16&));
Devin Moore5e4c2f12021-09-09 22:36:33 +000064 MOCK_METHOD1(getConnectionInfo, std::optional<ConnectionInfo>(const String16&));
Jayant Chowdhary30700942022-01-31 14:12:40 -080065 MOCK_METHOD2(registerForNotifications, status_t(const String16&,
66 const sp<LocalRegistrationCallback>&));
67 MOCK_METHOD2(unregisterForNotifications, status_t(const String16&,
68 const sp<LocalRegistrationCallback>&));
Jayant Chowdharya0a8eb22022-05-20 03:30:09 +000069 MOCK_METHOD0(getServiceDebugInfo, std::vector<ServiceDebugInfo>());
Felipe Leme343175a2016-08-02 18:57:37 -070070 protected:
71 MOCK_METHOD0(onAsBinder, IBinder*());
72};
73
74class BinderMock : public BBinder {
75 public:
76 BinderMock() {
77 }
78
79 MOCK_METHOD2(dump, status_t(int, const Vector<String16>&));
80};
81
82// gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher
83typedef void WriteOnFdFunction(int);
84
85class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
86 public:
87 explicit WriteOnFdAction(const std::string& output) : output_(output) {
88 }
89 virtual Result Perform(const ArgumentTuple& args) {
Haibo Huang21f36552018-07-10 19:48:18 -070090 int fd = ::testing::get<0>(args);
Felipe Leme343175a2016-08-02 18:57:37 -070091 android::base::WriteStringToFd(output_, fd);
92 }
93
94 private:
95 std::string output_;
96};
97
98// Matcher used to emulate dump() by writing on its file descriptor.
99Action<WriteOnFdFunction> WriteOnFd(const std::string& output) {
100 return MakeAction(new WriteOnFdAction(output));
101}
102
103// Matcher for args using Android's Vector<String16> format
104// TODO: move it to some common testing library
105MATCHER_P(AndroidElementsAre, expected, "") {
106 std::ostringstream errors;
107 if (arg.size() != expected.size()) {
108 errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size()
109 << ")\n";
110 }
111 int i = 0;
112 std::ostringstream actual_stream, expected_stream;
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700113 for (const String16& actual : arg) {
Steven Morelanda0f7f2d2017-03-09 22:59:32 -0800114 std::string actual_str = String8(actual).c_str();
Felipe Leme343175a2016-08-02 18:57:37 -0700115 std::string expected_str = expected[i];
116 actual_stream << "'" << actual_str << "' ";
117 expected_stream << "'" << expected_str << "' ";
118 if (actual_str != expected_str) {
119 errors << " element mismatch at index " << i << "\n";
120 }
121 i++;
122 }
123
124 if (!errors.str().empty()) {
125 errors << "\nExpected args: " << expected_stream.str()
126 << "\nActual args: " << actual_stream.str();
127 *result_listener << errors.str();
128 return false;
129 }
130 return true;
131}
132
133// Custom action to sleep for timeout seconds
134ACTION_P(Sleep, timeout) {
135 sleep(timeout);
136}
137
138class DumpsysTest : public Test {
139 public:
Steven Moreland2c3cd832017-02-13 23:44:17 +0000140 DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() {
Felipe Leme343175a2016-08-02 18:57:37 -0700141 }
142
143 void ExpectListServices(std::vector<std::string> services) {
144 Vector<String16> services16;
145 for (auto& service : services) {
146 services16.add(String16(service.c_str()));
147 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700148 EXPECT_CALL(sm_, listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL))
Vishnu Nairf56042d2017-09-19 15:25:10 -0700149 .WillRepeatedly(Return(services16));
150 }
151
Vishnu Nair6a408532017-10-24 09:11:27 -0700152 void ExpectListServicesWithPriority(std::vector<std::string> services, int dumpFlags) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700153 Vector<String16> services16;
154 for (auto& service : services) {
155 services16.add(String16(service.c_str()));
156 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700157 EXPECT_CALL(sm_, listServices(dumpFlags)).WillRepeatedly(Return(services16));
Felipe Leme343175a2016-08-02 18:57:37 -0700158 }
159
160 sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
161 sp<BinderMock> binder_mock;
162 if (running) {
163 binder_mock = new BinderMock;
164 }
165 EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
166 return binder_mock;
167 }
168
169 void ExpectDump(const char* name, const std::string& output) {
170 sp<BinderMock> binder_mock = ExpectCheckService(name);
171 EXPECT_CALL(*binder_mock, dump(_, _))
172 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
173 }
174
175 void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
176 const std::string& output) {
177 sp<BinderMock> binder_mock = ExpectCheckService(name);
178 EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
179 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
180 }
181
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700182 sp<BinderMock> ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
Felipe Leme343175a2016-08-02 18:57:37 -0700183 sp<BinderMock> binder_mock = ExpectCheckService(name);
184 EXPECT_CALL(*binder_mock, dump(_, _))
185 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700186 return binder_mock;
Felipe Leme343175a2016-08-02 18:57:37 -0700187 }
188
189 void CallMain(const std::vector<std::string>& args) {
190 const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
191 int argc = (int)args.size() + 1;
192 int i = 1;
193 for (const std::string& arg : args) {
194 argv[i++] = arg.c_str();
195 }
196 CaptureStdout();
197 CaptureStderr();
198 int status = dump_.main(argc, const_cast<char**>(argv));
199 stdout_ = GetCapturedStdout();
200 stderr_ = GetCapturedStderr();
201 EXPECT_THAT(status, Eq(0));
202 }
203
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000204 void CallSingleService(const String16& serviceName, Vector<String16>& args, int priorityFlags,
205 bool supportsProto, std::chrono::duration<double>& elapsedDuration,
206 size_t& bytesWritten) {
207 CaptureStdout();
208 CaptureStderr();
209 dump_.setServiceArgs(args, supportsProto, priorityFlags);
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000210 status_t status = dump_.startDumpThread(Dumpsys::TYPE_DUMP, serviceName, args);
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000211 EXPECT_THAT(status, Eq(0));
212 status = dump_.writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(500), false,
213 elapsedDuration, bytesWritten);
214 EXPECT_THAT(status, Eq(0));
215 dump_.stopDumpThread(/* dumpCompleted = */ true);
216 stdout_ = GetCapturedStdout();
217 stderr_ = GetCapturedStderr();
218 }
219
Steven Moreland2c3cd832017-02-13 23:44:17 +0000220 void AssertRunningServices(const std::vector<std::string>& services) {
Steven Moreland31dac352020-03-05 09:46:45 -0800221 std::string expected = "Currently running services:\n";
Felipe Leme343175a2016-08-02 18:57:37 -0700222 for (const std::string& service : services) {
223 expected.append(" ").append(service).append("\n");
224 }
225 EXPECT_THAT(stdout_, HasSubstr(expected));
226 }
227
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000228 void AssertOutput(const std::string& expected) {
229 EXPECT_THAT(stdout_, StrEq(expected));
230 }
231
Felipe Leme343175a2016-08-02 18:57:37 -0700232 void AssertOutputContains(const std::string& expected) {
233 EXPECT_THAT(stdout_, HasSubstr(expected));
234 }
235
Devin Moorecfeeda42020-12-08 12:50:58 -0800236 void AssertOutputFormat(const std::string format) {
237 EXPECT_THAT(stdout_, testing::MatchesRegex(format));
238 }
239
Felipe Leme343175a2016-08-02 18:57:37 -0700240 void AssertDumped(const std::string& service, const std::string& dump) {
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000241 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800242 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Felipe Leme343175a2016-08-02 18:57:37 -0700243 }
244
Vishnu Nair6a408532017-10-24 09:11:27 -0700245 void AssertDumpedWithPriority(const std::string& service, const std::string& dump,
246 const char16_t* priorityType) {
247 std::string priority = String8(priorityType).c_str();
248 EXPECT_THAT(stdout_,
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000249 HasSubstr("DUMP OF SERVICE " + priority + " " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800250 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Vishnu Nair6a408532017-10-24 09:11:27 -0700251 }
252
Felipe Leme343175a2016-08-02 18:57:37 -0700253 void AssertNotDumped(const std::string& dump) {
254 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
255 }
256
257 void AssertStopped(const std::string& service) {
258 EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
259 }
260
261 ServiceManagerMock sm_;
262 Dumpsys dump_;
263
264 private:
265 std::string stdout_, stderr_;
266};
267
Felipe Leme343175a2016-08-02 18:57:37 -0700268// Tests 'dumpsys -l' when all services are running
269TEST_F(DumpsysTest, ListAllServices) {
270 ExpectListServices({"Locksmith", "Valet"});
271 ExpectCheckService("Locksmith");
272 ExpectCheckService("Valet");
273
274 CallMain({"-l"});
275
276 AssertRunningServices({"Locksmith", "Valet"});
277}
278
Steven Moreland31dac352020-03-05 09:46:45 -0800279TEST_F(DumpsysTest, ListServicesOneRegistered) {
280 ExpectListServices({"Locksmith"});
281 ExpectCheckService("Locksmith");
282
283 CallMain({"-l"});
284
285 AssertRunningServices({"Locksmith"});
286}
287
288TEST_F(DumpsysTest, ListServicesEmpty) {
289 CallMain({"-l"});
290
291 AssertRunningServices({});
292}
293
Felipe Leme343175a2016-08-02 18:57:37 -0700294// Tests 'dumpsys -l' when a service is not running
295TEST_F(DumpsysTest, ListRunningServices) {
296 ExpectListServices({"Locksmith", "Valet"});
297 ExpectCheckService("Locksmith");
298 ExpectCheckService("Valet", false);
299
300 CallMain({"-l"});
301
302 AssertRunningServices({"Locksmith"});
303 AssertNotDumped({"Valet"});
304}
305
Vishnu Nairf56042d2017-09-19 15:25:10 -0700306// Tests 'dumpsys -l --priority HIGH'
307TEST_F(DumpsysTest, ListAllServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700308 ExpectListServicesWithPriority({"Locksmith", "Valet"}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700309 ExpectCheckService("Locksmith");
310 ExpectCheckService("Valet");
311
312 CallMain({"-l", "--priority", "HIGH"});
313
314 AssertRunningServices({"Locksmith", "Valet"});
315}
316
317// Tests 'dumpsys -l --priority HIGH' with and empty list
318TEST_F(DumpsysTest, ListEmptyServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700319 ExpectListServicesWithPriority({}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700320
321 CallMain({"-l", "--priority", "HIGH"});
322
323 AssertRunningServices({});
324}
325
Vishnu Nair6a408532017-10-24 09:11:27 -0700326// Tests 'dumpsys -l --proto'
327TEST_F(DumpsysTest, ListAllServicesWithProto) {
328 ExpectListServicesWithPriority({"Locksmith", "Valet", "Car"},
329 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
330 ExpectListServicesWithPriority({"Valet", "Car"}, IServiceManager::DUMP_FLAG_PROTO);
331 ExpectCheckService("Car");
332 ExpectCheckService("Valet");
333
334 CallMain({"-l", "--proto"});
335
336 AssertRunningServices({"Car", "Valet"});
337}
338
Felipe Leme343175a2016-08-02 18:57:37 -0700339// Tests 'dumpsys service_name' on a service is running
340TEST_F(DumpsysTest, DumpRunningService) {
341 ExpectDump("Valet", "Here's your car");
342
343 CallMain({"Valet"});
344
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000345 AssertOutput("Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700346}
347
348// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
Vishnu Nair6921f802017-11-22 09:17:23 -0800349TEST_F(DumpsysTest, DumpRunningServiceTimeoutInSec) {
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700350 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700351
352 CallMain({"-t", "1", "Valet"});
353
Vishnu Nair6921f802017-11-22 09:17:23 -0800354 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1000ms) EXPIRED");
355 AssertNotDumped("Here's your car");
356
357 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
358 Mock::AllowLeak(binder_mock.get());
359}
360
361// Tests 'dumpsys -T 500 service_name' on a service that times out after 2s
362TEST_F(DumpsysTest, DumpRunningServiceTimeoutInMs) {
363 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
364
365 CallMain({"-T", "500", "Valet"});
366
367 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (500ms) EXPIRED");
Felipe Leme343175a2016-08-02 18:57:37 -0700368 AssertNotDumped("Here's your car");
369
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700370 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
371 Mock::AllowLeak(binder_mock.get());
Felipe Leme343175a2016-08-02 18:57:37 -0700372}
373
374// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
375TEST_F(DumpsysTest, DumpWithArgsRunningService) {
376 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
377
378 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
379
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000380 AssertOutput("I DO!");
Felipe Leme343175a2016-08-02 18:57:37 -0700381}
382
Vishnu Nair64afc022018-02-01 15:29:34 -0800383// Tests dumpsys passes the -a flag when called on all services
384TEST_F(DumpsysTest, PassAllFlagsToServices) {
385 ExpectListServices({"Locksmith", "Valet"});
386 ExpectCheckService("Locksmith");
387 ExpectCheckService("Valet");
388 ExpectDumpWithArgs("Locksmith", {"-a"}, "dumped1");
389 ExpectDumpWithArgs("Valet", {"-a"}, "dumped2");
390
391 CallMain({"-T", "500"});
392
393 AssertDumped("Locksmith", "dumped1");
394 AssertDumped("Valet", "dumped2");
395}
396
397// Tests dumpsys passes the -a flag when called on NORMAL priority services
398TEST_F(DumpsysTest, PassAllFlagsToNormalServices) {
399 ExpectListServicesWithPriority({"Locksmith", "Valet"},
400 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
401 ExpectCheckService("Locksmith");
402 ExpectCheckService("Valet");
Vishnu Nair3919e1c2018-05-01 17:01:00 -0700403 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "NORMAL", "-a"}, "dump1");
404 ExpectDumpWithArgs("Valet", {"--dump-priority", "NORMAL", "-a"}, "dump2");
Vishnu Nair64afc022018-02-01 15:29:34 -0800405
406 CallMain({"--priority", "NORMAL"});
407
408 AssertDumped("Locksmith", "dump1");
409 AssertDumped("Valet", "dump2");
410}
411
412// Tests dumpsys passes only priority flags when called on CRITICAL priority services
413TEST_F(DumpsysTest, PassPriorityFlagsToCriticalServices) {
414 ExpectListServicesWithPriority({"Locksmith", "Valet"},
415 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
416 ExpectCheckService("Locksmith");
417 ExpectCheckService("Valet");
418 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "CRITICAL"}, "dump1");
419 ExpectDumpWithArgs("Valet", {"--dump-priority", "CRITICAL"}, "dump2");
420
421 CallMain({"--priority", "CRITICAL"});
422
423 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
424 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
425}
426
427// Tests dumpsys passes only priority flags when called on HIGH priority services
428TEST_F(DumpsysTest, PassPriorityFlagsToHighServices) {
429 ExpectListServicesWithPriority({"Locksmith", "Valet"},
430 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
431 ExpectCheckService("Locksmith");
432 ExpectCheckService("Valet");
433 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "HIGH"}, "dump1");
434 ExpectDumpWithArgs("Valet", {"--dump-priority", "HIGH"}, "dump2");
435
436 CallMain({"--priority", "HIGH"});
437
438 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
439 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
440}
441
Felipe Leme343175a2016-08-02 18:57:37 -0700442// Tests 'dumpsys' with no arguments
443TEST_F(DumpsysTest, DumpMultipleServices) {
444 ExpectListServices({"running1", "stopped2", "running3"});
445 ExpectDump("running1", "dump1");
446 ExpectCheckService("stopped2", false);
447 ExpectDump("running3", "dump3");
448
449 CallMain({});
450
451 AssertRunningServices({"running1", "running3"});
452 AssertDumped("running1", "dump1");
453 AssertStopped("stopped2");
454 AssertDumped("running3", "dump3");
455}
456
457// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
458TEST_F(DumpsysTest, DumpWithSkip) {
459 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
460 ExpectDump("running1", "dump1");
461 ExpectCheckService("stopped2", false);
462 ExpectDump("skipped3", "dump3");
463 ExpectDump("running4", "dump4");
464 ExpectDump("skipped5", "dump5");
465
466 CallMain({"--skip", "skipped3", "skipped5"});
467
468 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
469 AssertDumped("running1", "dump1");
470 AssertDumped("running4", "dump4");
471 AssertStopped("stopped2");
472 AssertNotDumped("dump3");
473 AssertNotDumped("dump5");
474}
Vishnu Nairf56042d2017-09-19 15:25:10 -0700475
476// Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services
477TEST_F(DumpsysTest, DumpWithSkipAndPriority) {
478 ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700479 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700480 ExpectDump("running1", "dump1");
481 ExpectCheckService("stopped2", false);
482 ExpectDump("skipped3", "dump3");
483 ExpectDump("running4", "dump4");
484 ExpectDump("skipped5", "dump5");
485
486 CallMain({"--priority", "CRITICAL", "--skip", "skipped3", "skipped5"});
487
488 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700489 AssertDumpedWithPriority("running1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
490 AssertDumpedWithPriority("running4", "dump4", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700491 AssertStopped("stopped2");
492 AssertNotDumped("dump3");
493 AssertNotDumped("dump5");
494}
495
496// Tests 'dumpsys --priority CRITICAL'
497TEST_F(DumpsysTest, DumpWithPriorityCritical) {
498 ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700499 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700500 ExpectDump("runningcritical1", "dump1");
501 ExpectDump("runningcritical2", "dump2");
502
503 CallMain({"--priority", "CRITICAL"});
504
505 AssertRunningServices({"runningcritical1", "runningcritical2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700506 AssertDumpedWithPriority("runningcritical1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
507 AssertDumpedWithPriority("runningcritical2", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700508}
509
510// Tests 'dumpsys --priority HIGH'
511TEST_F(DumpsysTest, DumpWithPriorityHigh) {
512 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700513 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700514 ExpectDump("runninghigh1", "dump1");
515 ExpectDump("runninghigh2", "dump2");
516
517 CallMain({"--priority", "HIGH"});
518
519 AssertRunningServices({"runninghigh1", "runninghigh2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700520 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
521 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700522}
523
524// Tests 'dumpsys --priority NORMAL'
525TEST_F(DumpsysTest, DumpWithPriorityNormal) {
526 ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700527 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700528 ExpectDump("runningnormal1", "dump1");
529 ExpectDump("runningnormal2", "dump2");
530
531 CallMain({"--priority", "NORMAL"});
532
533 AssertRunningServices({"runningnormal1", "runningnormal2"});
Vishnu Naire4f61742017-12-21 08:30:28 -0800534 AssertDumped("runningnormal1", "dump1");
535 AssertDumped("runningnormal2", "dump2");
Vishnu Nair6a408532017-10-24 09:11:27 -0700536}
537
538// Tests 'dumpsys --proto'
539TEST_F(DumpsysTest, DumpWithProto) {
540 ExpectListServicesWithPriority({"run8", "run1", "run2", "run5"},
541 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
542 ExpectListServicesWithPriority({"run3", "run2", "run4", "run8"},
543 IServiceManager::DUMP_FLAG_PROTO);
544 ExpectDump("run2", "dump1");
545 ExpectDump("run8", "dump2");
546
547 CallMain({"--proto"});
548
549 AssertRunningServices({"run2", "run8"});
550 AssertDumped("run2", "dump1");
551 AssertDumped("run8", "dump2");
552}
553
554// Tests 'dumpsys --priority HIGH --proto'
555TEST_F(DumpsysTest, DumpWithPriorityHighAndProto) {
556 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
557 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
558 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2", "runninghigh3"},
559 IServiceManager::DUMP_FLAG_PROTO);
560
561 ExpectDump("runninghigh1", "dump1");
562 ExpectDump("runninghigh2", "dump2");
563
564 CallMain({"--priority", "HIGH", "--proto"});
565
566 AssertRunningServices({"runninghigh1", "runninghigh2"});
567 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
568 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700569}
Vishnu Naire4f61742017-12-21 08:30:28 -0800570
Steven Moreland5a30d342019-10-08 13:53:28 -0700571// Tests 'dumpsys --pid'
572TEST_F(DumpsysTest, ListAllServicesWithPid) {
573 ExpectListServices({"Locksmith", "Valet"});
574 ExpectCheckService("Locksmith");
575 ExpectCheckService("Valet");
576
577 CallMain({"--pid"});
578
579 AssertRunningServices({"Locksmith", "Valet"});
580 AssertOutputContains(std::to_string(getpid()));
581}
582
583// Tests 'dumpsys --pid service_name'
584TEST_F(DumpsysTest, ListServiceWithPid) {
585 ExpectCheckService("Locksmith");
586
587 CallMain({"--pid", "Locksmith"});
588
589 AssertOutput(std::to_string(getpid()) + "\n");
590}
591
Steven Morelande5a6a872021-05-19 22:11:37 +0000592// Tests 'dumpsys --stability'
593TEST_F(DumpsysTest, ListAllServicesWithStability) {
594 ExpectListServices({"Locksmith", "Valet"});
595 ExpectCheckService("Locksmith");
596 ExpectCheckService("Valet");
597
598 CallMain({"--stability"});
599
600 AssertRunningServices({"Locksmith", "Valet"});
601 AssertOutputContains("stability");
602}
603
604// Tests 'dumpsys --stability service_name'
605TEST_F(DumpsysTest, ListServiceWithStability) {
606 ExpectCheckService("Locksmith");
607
608 CallMain({"--stability", "Locksmith"});
609
610 AssertOutputContains("stability");
611}
612
Devin Moorecfeeda42020-12-08 12:50:58 -0800613// Tests 'dumpsys --thread'
614TEST_F(DumpsysTest, ListAllServicesWithThread) {
615 ExpectListServices({"Locksmith", "Valet"});
616 ExpectCheckService("Locksmith");
617 ExpectCheckService("Valet");
618
619 CallMain({"--thread"});
620
621 AssertRunningServices({"Locksmith", "Valet"});
622
623 const std::string format("(.|\n)*((Threads in use: [0-9]+/[0-9]+)?\n-(.|\n)*){2}");
624 AssertOutputFormat(format);
625}
626
627// Tests 'dumpsys --thread service_name'
628TEST_F(DumpsysTest, ListServiceWithThread) {
629 ExpectCheckService("Locksmith");
630
631 CallMain({"--thread", "Locksmith"});
632 // returns an empty string without root enabled
633 const std::string format("(^$|Threads in use: [0-9]/[0-9]+\n)");
634 AssertOutputFormat(format);
635}
636
Devin Mooredef9fae2021-08-05 19:05:45 +0000637// Tests 'dumpsys --clients'
638TEST_F(DumpsysTest, ListAllServicesWithClients) {
639 ExpectListServices({"Locksmith", "Valet"});
640 ExpectCheckService("Locksmith");
641 ExpectCheckService("Valet");
642
643 CallMain({"--clients"});
644
645 AssertRunningServices({"Locksmith", "Valet"});
646
647 const std::string format("(.|\n)*((Client PIDs are not available for local binders.)(.|\n)*){2}");
648 AssertOutputFormat(format);
649}
650
651// Tests 'dumpsys --clients service_name'
652TEST_F(DumpsysTest, ListServiceWithClients) {
653 ExpectCheckService("Locksmith");
654
655 CallMain({"--clients", "Locksmith"});
656
657 const std::string format("Client PIDs are not available for local binders.\n");
658 AssertOutputFormat(format);
659}
Steven Morelandcbd69fc2021-07-20 20:45:43 +0000660// Tests 'dumpsys --thread --stability'
661TEST_F(DumpsysTest, ListAllServicesWithMultipleOptions) {
662 ExpectListServices({"Locksmith", "Valet"});
663 ExpectCheckService("Locksmith");
664 ExpectCheckService("Valet");
665
666 CallMain({"--pid", "--stability"});
667 AssertRunningServices({"Locksmith", "Valet"});
668
669 AssertOutputContains(std::to_string(getpid()));
670 AssertOutputContains("stability");
671}
672
673// Tests 'dumpsys --pid --stability service_name'
674TEST_F(DumpsysTest, ListServiceWithMultipleOptions) {
675 ExpectCheckService("Locksmith");
676 CallMain({"--pid", "--stability", "Locksmith"});
677
678 AssertOutputContains(std::to_string(getpid()));
679 AssertOutputContains("stability");
680}
681
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000682TEST_F(DumpsysTest, GetBytesWritten) {
683 const char* serviceName = "service2";
684 const char* dumpContents = "dump1";
685 ExpectDump(serviceName, dumpContents);
686
687 String16 service(serviceName);
688 Vector<String16> args;
689 std::chrono::duration<double> elapsedDuration;
690 size_t bytesWritten;
691
692 CallSingleService(service, args, IServiceManager::DUMP_FLAG_PRIORITY_ALL,
693 /* as_proto = */ false, elapsedDuration, bytesWritten);
694
695 AssertOutput(dumpContents);
696 EXPECT_THAT(bytesWritten, Eq(strlen(dumpContents)));
697}
698
Vishnu Naire4f61742017-12-21 08:30:28 -0800699TEST_F(DumpsysTest, WriteDumpWithoutThreadStart) {
700 std::chrono::duration<double> elapsedDuration;
701 size_t bytesWritten;
702 status_t status =
703 dump_.writeDump(STDOUT_FILENO, String16("service"), std::chrono::milliseconds(500),
704 /* as_proto = */ false, elapsedDuration, bytesWritten);
705 EXPECT_THAT(status, Eq(INVALID_OPERATION));
Steven Moreland5a30d342019-10-08 13:53:28 -0700706}
Devin Moorecfeeda42020-12-08 12:50:58 -0800707
708int main(int argc, char** argv) {
709 ::testing::InitGoogleTest(&argc, argv);
710
711 // start a binder thread pool for testing --thread option
712 android::ProcessState::self()->setThreadPoolMaxThreadCount(8);
713 ProcessState::self()->startThreadPool();
714
715 return RUN_ALL_TESTS();
716}