blob: 39af7dfae00ca4d5c1fd2c781d8829e11d1312ac [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&));
Felipe Leme343175a2016-08-02 18:57:37 -070062 protected:
63 MOCK_METHOD0(onAsBinder, IBinder*());
64};
65
66class BinderMock : public BBinder {
67 public:
68 BinderMock() {
69 }
70
71 MOCK_METHOD2(dump, status_t(int, const Vector<String16>&));
72};
73
74// gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher
75typedef void WriteOnFdFunction(int);
76
77class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
78 public:
79 explicit WriteOnFdAction(const std::string& output) : output_(output) {
80 }
81 virtual Result Perform(const ArgumentTuple& args) {
Haibo Huang21f36552018-07-10 19:48:18 -070082 int fd = ::testing::get<0>(args);
Felipe Leme343175a2016-08-02 18:57:37 -070083 android::base::WriteStringToFd(output_, fd);
84 }
85
86 private:
87 std::string output_;
88};
89
90// Matcher used to emulate dump() by writing on its file descriptor.
91Action<WriteOnFdFunction> WriteOnFd(const std::string& output) {
92 return MakeAction(new WriteOnFdAction(output));
93}
94
95// Matcher for args using Android's Vector<String16> format
96// TODO: move it to some common testing library
97MATCHER_P(AndroidElementsAre, expected, "") {
98 std::ostringstream errors;
99 if (arg.size() != expected.size()) {
100 errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size()
101 << ")\n";
102 }
103 int i = 0;
104 std::ostringstream actual_stream, expected_stream;
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700105 for (const String16& actual : arg) {
Steven Morelanda0f7f2d2017-03-09 22:59:32 -0800106 std::string actual_str = String8(actual).c_str();
Felipe Leme343175a2016-08-02 18:57:37 -0700107 std::string expected_str = expected[i];
108 actual_stream << "'" << actual_str << "' ";
109 expected_stream << "'" << expected_str << "' ";
110 if (actual_str != expected_str) {
111 errors << " element mismatch at index " << i << "\n";
112 }
113 i++;
114 }
115
116 if (!errors.str().empty()) {
117 errors << "\nExpected args: " << expected_stream.str()
118 << "\nActual args: " << actual_stream.str();
119 *result_listener << errors.str();
120 return false;
121 }
122 return true;
123}
124
125// Custom action to sleep for timeout seconds
126ACTION_P(Sleep, timeout) {
127 sleep(timeout);
128}
129
130class DumpsysTest : public Test {
131 public:
Steven Moreland2c3cd832017-02-13 23:44:17 +0000132 DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() {
Felipe Leme343175a2016-08-02 18:57:37 -0700133 }
134
135 void ExpectListServices(std::vector<std::string> services) {
136 Vector<String16> services16;
137 for (auto& service : services) {
138 services16.add(String16(service.c_str()));
139 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700140 EXPECT_CALL(sm_, listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL))
Vishnu Nairf56042d2017-09-19 15:25:10 -0700141 .WillRepeatedly(Return(services16));
142 }
143
Vishnu Nair6a408532017-10-24 09:11:27 -0700144 void ExpectListServicesWithPriority(std::vector<std::string> services, int dumpFlags) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700145 Vector<String16> services16;
146 for (auto& service : services) {
147 services16.add(String16(service.c_str()));
148 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700149 EXPECT_CALL(sm_, listServices(dumpFlags)).WillRepeatedly(Return(services16));
Felipe Leme343175a2016-08-02 18:57:37 -0700150 }
151
152 sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
153 sp<BinderMock> binder_mock;
154 if (running) {
155 binder_mock = new BinderMock;
156 }
157 EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
158 return binder_mock;
159 }
160
161 void ExpectDump(const char* name, const std::string& output) {
162 sp<BinderMock> binder_mock = ExpectCheckService(name);
163 EXPECT_CALL(*binder_mock, dump(_, _))
164 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
165 }
166
167 void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
168 const std::string& output) {
169 sp<BinderMock> binder_mock = ExpectCheckService(name);
170 EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
171 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
172 }
173
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700174 sp<BinderMock> ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
Felipe Leme343175a2016-08-02 18:57:37 -0700175 sp<BinderMock> binder_mock = ExpectCheckService(name);
176 EXPECT_CALL(*binder_mock, dump(_, _))
177 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700178 return binder_mock;
Felipe Leme343175a2016-08-02 18:57:37 -0700179 }
180
181 void CallMain(const std::vector<std::string>& args) {
182 const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
183 int argc = (int)args.size() + 1;
184 int i = 1;
185 for (const std::string& arg : args) {
186 argv[i++] = arg.c_str();
187 }
188 CaptureStdout();
189 CaptureStderr();
190 int status = dump_.main(argc, const_cast<char**>(argv));
191 stdout_ = GetCapturedStdout();
192 stderr_ = GetCapturedStderr();
193 EXPECT_THAT(status, Eq(0));
194 }
195
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000196 void CallSingleService(const String16& serviceName, Vector<String16>& args, int priorityFlags,
197 bool supportsProto, std::chrono::duration<double>& elapsedDuration,
198 size_t& bytesWritten) {
199 CaptureStdout();
200 CaptureStderr();
201 dump_.setServiceArgs(args, supportsProto, priorityFlags);
Steven Moreland5a30d342019-10-08 13:53:28 -0700202 status_t status = dump_.startDumpThread(Dumpsys::Type::DUMP, serviceName, args);
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000203 EXPECT_THAT(status, Eq(0));
204 status = dump_.writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(500), false,
205 elapsedDuration, bytesWritten);
206 EXPECT_THAT(status, Eq(0));
207 dump_.stopDumpThread(/* dumpCompleted = */ true);
208 stdout_ = GetCapturedStdout();
209 stderr_ = GetCapturedStderr();
210 }
211
Steven Moreland2c3cd832017-02-13 23:44:17 +0000212 void AssertRunningServices(const std::vector<std::string>& services) {
Steven Moreland31dac352020-03-05 09:46:45 -0800213 std::string expected = "Currently running services:\n";
Felipe Leme343175a2016-08-02 18:57:37 -0700214 for (const std::string& service : services) {
215 expected.append(" ").append(service).append("\n");
216 }
217 EXPECT_THAT(stdout_, HasSubstr(expected));
218 }
219
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000220 void AssertOutput(const std::string& expected) {
221 EXPECT_THAT(stdout_, StrEq(expected));
222 }
223
Felipe Leme343175a2016-08-02 18:57:37 -0700224 void AssertOutputContains(const std::string& expected) {
225 EXPECT_THAT(stdout_, HasSubstr(expected));
226 }
227
Devin Moorecfeeda42020-12-08 12:50:58 -0800228 void AssertOutputFormat(const std::string format) {
229 EXPECT_THAT(stdout_, testing::MatchesRegex(format));
230 }
231
Felipe Leme343175a2016-08-02 18:57:37 -0700232 void AssertDumped(const std::string& service, const std::string& dump) {
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000233 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800234 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Felipe Leme343175a2016-08-02 18:57:37 -0700235 }
236
Vishnu Nair6a408532017-10-24 09:11:27 -0700237 void AssertDumpedWithPriority(const std::string& service, const std::string& dump,
238 const char16_t* priorityType) {
239 std::string priority = String8(priorityType).c_str();
240 EXPECT_THAT(stdout_,
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000241 HasSubstr("DUMP OF SERVICE " + priority + " " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800242 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Vishnu Nair6a408532017-10-24 09:11:27 -0700243 }
244
Felipe Leme343175a2016-08-02 18:57:37 -0700245 void AssertNotDumped(const std::string& dump) {
246 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
247 }
248
249 void AssertStopped(const std::string& service) {
250 EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
251 }
252
253 ServiceManagerMock sm_;
254 Dumpsys dump_;
255
256 private:
257 std::string stdout_, stderr_;
258};
259
Felipe Leme343175a2016-08-02 18:57:37 -0700260// Tests 'dumpsys -l' when all services are running
261TEST_F(DumpsysTest, ListAllServices) {
262 ExpectListServices({"Locksmith", "Valet"});
263 ExpectCheckService("Locksmith");
264 ExpectCheckService("Valet");
265
266 CallMain({"-l"});
267
268 AssertRunningServices({"Locksmith", "Valet"});
269}
270
Steven Moreland31dac352020-03-05 09:46:45 -0800271TEST_F(DumpsysTest, ListServicesOneRegistered) {
272 ExpectListServices({"Locksmith"});
273 ExpectCheckService("Locksmith");
274
275 CallMain({"-l"});
276
277 AssertRunningServices({"Locksmith"});
278}
279
280TEST_F(DumpsysTest, ListServicesEmpty) {
281 CallMain({"-l"});
282
283 AssertRunningServices({});
284}
285
Felipe Leme343175a2016-08-02 18:57:37 -0700286// Tests 'dumpsys -l' when a service is not running
287TEST_F(DumpsysTest, ListRunningServices) {
288 ExpectListServices({"Locksmith", "Valet"});
289 ExpectCheckService("Locksmith");
290 ExpectCheckService("Valet", false);
291
292 CallMain({"-l"});
293
294 AssertRunningServices({"Locksmith"});
295 AssertNotDumped({"Valet"});
296}
297
Vishnu Nairf56042d2017-09-19 15:25:10 -0700298// Tests 'dumpsys -l --priority HIGH'
299TEST_F(DumpsysTest, ListAllServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700300 ExpectListServicesWithPriority({"Locksmith", "Valet"}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700301 ExpectCheckService("Locksmith");
302 ExpectCheckService("Valet");
303
304 CallMain({"-l", "--priority", "HIGH"});
305
306 AssertRunningServices({"Locksmith", "Valet"});
307}
308
309// Tests 'dumpsys -l --priority HIGH' with and empty list
310TEST_F(DumpsysTest, ListEmptyServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700311 ExpectListServicesWithPriority({}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700312
313 CallMain({"-l", "--priority", "HIGH"});
314
315 AssertRunningServices({});
316}
317
Vishnu Nair6a408532017-10-24 09:11:27 -0700318// Tests 'dumpsys -l --proto'
319TEST_F(DumpsysTest, ListAllServicesWithProto) {
320 ExpectListServicesWithPriority({"Locksmith", "Valet", "Car"},
321 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
322 ExpectListServicesWithPriority({"Valet", "Car"}, IServiceManager::DUMP_FLAG_PROTO);
323 ExpectCheckService("Car");
324 ExpectCheckService("Valet");
325
326 CallMain({"-l", "--proto"});
327
328 AssertRunningServices({"Car", "Valet"});
329}
330
Felipe Leme343175a2016-08-02 18:57:37 -0700331// Tests 'dumpsys service_name' on a service is running
332TEST_F(DumpsysTest, DumpRunningService) {
333 ExpectDump("Valet", "Here's your car");
334
335 CallMain({"Valet"});
336
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000337 AssertOutput("Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700338}
339
340// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
Vishnu Nair6921f802017-11-22 09:17:23 -0800341TEST_F(DumpsysTest, DumpRunningServiceTimeoutInSec) {
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700342 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700343
344 CallMain({"-t", "1", "Valet"});
345
Vishnu Nair6921f802017-11-22 09:17:23 -0800346 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1000ms) EXPIRED");
347 AssertNotDumped("Here's your car");
348
349 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
350 Mock::AllowLeak(binder_mock.get());
351}
352
353// Tests 'dumpsys -T 500 service_name' on a service that times out after 2s
354TEST_F(DumpsysTest, DumpRunningServiceTimeoutInMs) {
355 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
356
357 CallMain({"-T", "500", "Valet"});
358
359 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (500ms) EXPIRED");
Felipe Leme343175a2016-08-02 18:57:37 -0700360 AssertNotDumped("Here's your car");
361
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700362 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
363 Mock::AllowLeak(binder_mock.get());
Felipe Leme343175a2016-08-02 18:57:37 -0700364}
365
366// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
367TEST_F(DumpsysTest, DumpWithArgsRunningService) {
368 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
369
370 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
371
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000372 AssertOutput("I DO!");
Felipe Leme343175a2016-08-02 18:57:37 -0700373}
374
Vishnu Nair64afc022018-02-01 15:29:34 -0800375// Tests dumpsys passes the -a flag when called on all services
376TEST_F(DumpsysTest, PassAllFlagsToServices) {
377 ExpectListServices({"Locksmith", "Valet"});
378 ExpectCheckService("Locksmith");
379 ExpectCheckService("Valet");
380 ExpectDumpWithArgs("Locksmith", {"-a"}, "dumped1");
381 ExpectDumpWithArgs("Valet", {"-a"}, "dumped2");
382
383 CallMain({"-T", "500"});
384
385 AssertDumped("Locksmith", "dumped1");
386 AssertDumped("Valet", "dumped2");
387}
388
389// Tests dumpsys passes the -a flag when called on NORMAL priority services
390TEST_F(DumpsysTest, PassAllFlagsToNormalServices) {
391 ExpectListServicesWithPriority({"Locksmith", "Valet"},
392 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
393 ExpectCheckService("Locksmith");
394 ExpectCheckService("Valet");
Vishnu Nair3919e1c2018-05-01 17:01:00 -0700395 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "NORMAL", "-a"}, "dump1");
396 ExpectDumpWithArgs("Valet", {"--dump-priority", "NORMAL", "-a"}, "dump2");
Vishnu Nair64afc022018-02-01 15:29:34 -0800397
398 CallMain({"--priority", "NORMAL"});
399
400 AssertDumped("Locksmith", "dump1");
401 AssertDumped("Valet", "dump2");
402}
403
404// Tests dumpsys passes only priority flags when called on CRITICAL priority services
405TEST_F(DumpsysTest, PassPriorityFlagsToCriticalServices) {
406 ExpectListServicesWithPriority({"Locksmith", "Valet"},
407 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
408 ExpectCheckService("Locksmith");
409 ExpectCheckService("Valet");
410 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "CRITICAL"}, "dump1");
411 ExpectDumpWithArgs("Valet", {"--dump-priority", "CRITICAL"}, "dump2");
412
413 CallMain({"--priority", "CRITICAL"});
414
415 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
416 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
417}
418
419// Tests dumpsys passes only priority flags when called on HIGH priority services
420TEST_F(DumpsysTest, PassPriorityFlagsToHighServices) {
421 ExpectListServicesWithPriority({"Locksmith", "Valet"},
422 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
423 ExpectCheckService("Locksmith");
424 ExpectCheckService("Valet");
425 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "HIGH"}, "dump1");
426 ExpectDumpWithArgs("Valet", {"--dump-priority", "HIGH"}, "dump2");
427
428 CallMain({"--priority", "HIGH"});
429
430 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
431 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
432}
433
Felipe Leme343175a2016-08-02 18:57:37 -0700434// Tests 'dumpsys' with no arguments
435TEST_F(DumpsysTest, DumpMultipleServices) {
436 ExpectListServices({"running1", "stopped2", "running3"});
437 ExpectDump("running1", "dump1");
438 ExpectCheckService("stopped2", false);
439 ExpectDump("running3", "dump3");
440
441 CallMain({});
442
443 AssertRunningServices({"running1", "running3"});
444 AssertDumped("running1", "dump1");
445 AssertStopped("stopped2");
446 AssertDumped("running3", "dump3");
447}
448
449// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
450TEST_F(DumpsysTest, DumpWithSkip) {
451 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
452 ExpectDump("running1", "dump1");
453 ExpectCheckService("stopped2", false);
454 ExpectDump("skipped3", "dump3");
455 ExpectDump("running4", "dump4");
456 ExpectDump("skipped5", "dump5");
457
458 CallMain({"--skip", "skipped3", "skipped5"});
459
460 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
461 AssertDumped("running1", "dump1");
462 AssertDumped("running4", "dump4");
463 AssertStopped("stopped2");
464 AssertNotDumped("dump3");
465 AssertNotDumped("dump5");
466}
Vishnu Nairf56042d2017-09-19 15:25:10 -0700467
468// Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services
469TEST_F(DumpsysTest, DumpWithSkipAndPriority) {
470 ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700471 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700472 ExpectDump("running1", "dump1");
473 ExpectCheckService("stopped2", false);
474 ExpectDump("skipped3", "dump3");
475 ExpectDump("running4", "dump4");
476 ExpectDump("skipped5", "dump5");
477
478 CallMain({"--priority", "CRITICAL", "--skip", "skipped3", "skipped5"});
479
480 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700481 AssertDumpedWithPriority("running1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
482 AssertDumpedWithPriority("running4", "dump4", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700483 AssertStopped("stopped2");
484 AssertNotDumped("dump3");
485 AssertNotDumped("dump5");
486}
487
488// Tests 'dumpsys --priority CRITICAL'
489TEST_F(DumpsysTest, DumpWithPriorityCritical) {
490 ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700491 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700492 ExpectDump("runningcritical1", "dump1");
493 ExpectDump("runningcritical2", "dump2");
494
495 CallMain({"--priority", "CRITICAL"});
496
497 AssertRunningServices({"runningcritical1", "runningcritical2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700498 AssertDumpedWithPriority("runningcritical1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
499 AssertDumpedWithPriority("runningcritical2", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700500}
501
502// Tests 'dumpsys --priority HIGH'
503TEST_F(DumpsysTest, DumpWithPriorityHigh) {
504 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700505 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700506 ExpectDump("runninghigh1", "dump1");
507 ExpectDump("runninghigh2", "dump2");
508
509 CallMain({"--priority", "HIGH"});
510
511 AssertRunningServices({"runninghigh1", "runninghigh2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700512 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
513 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700514}
515
516// Tests 'dumpsys --priority NORMAL'
517TEST_F(DumpsysTest, DumpWithPriorityNormal) {
518 ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700519 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700520 ExpectDump("runningnormal1", "dump1");
521 ExpectDump("runningnormal2", "dump2");
522
523 CallMain({"--priority", "NORMAL"});
524
525 AssertRunningServices({"runningnormal1", "runningnormal2"});
Vishnu Naire4f61742017-12-21 08:30:28 -0800526 AssertDumped("runningnormal1", "dump1");
527 AssertDumped("runningnormal2", "dump2");
Vishnu Nair6a408532017-10-24 09:11:27 -0700528}
529
530// Tests 'dumpsys --proto'
531TEST_F(DumpsysTest, DumpWithProto) {
532 ExpectListServicesWithPriority({"run8", "run1", "run2", "run5"},
533 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
534 ExpectListServicesWithPriority({"run3", "run2", "run4", "run8"},
535 IServiceManager::DUMP_FLAG_PROTO);
536 ExpectDump("run2", "dump1");
537 ExpectDump("run8", "dump2");
538
539 CallMain({"--proto"});
540
541 AssertRunningServices({"run2", "run8"});
542 AssertDumped("run2", "dump1");
543 AssertDumped("run8", "dump2");
544}
545
546// Tests 'dumpsys --priority HIGH --proto'
547TEST_F(DumpsysTest, DumpWithPriorityHighAndProto) {
548 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
549 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
550 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2", "runninghigh3"},
551 IServiceManager::DUMP_FLAG_PROTO);
552
553 ExpectDump("runninghigh1", "dump1");
554 ExpectDump("runninghigh2", "dump2");
555
556 CallMain({"--priority", "HIGH", "--proto"});
557
558 AssertRunningServices({"runninghigh1", "runninghigh2"});
559 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
560 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700561}
Vishnu Naire4f61742017-12-21 08:30:28 -0800562
Steven Moreland5a30d342019-10-08 13:53:28 -0700563// Tests 'dumpsys --pid'
564TEST_F(DumpsysTest, ListAllServicesWithPid) {
565 ExpectListServices({"Locksmith", "Valet"});
566 ExpectCheckService("Locksmith");
567 ExpectCheckService("Valet");
568
569 CallMain({"--pid"});
570
571 AssertRunningServices({"Locksmith", "Valet"});
572 AssertOutputContains(std::to_string(getpid()));
573}
574
575// Tests 'dumpsys --pid service_name'
576TEST_F(DumpsysTest, ListServiceWithPid) {
577 ExpectCheckService("Locksmith");
578
579 CallMain({"--pid", "Locksmith"});
580
581 AssertOutput(std::to_string(getpid()) + "\n");
582}
583
Devin Moorecfeeda42020-12-08 12:50:58 -0800584// Tests 'dumpsys --thread'
585TEST_F(DumpsysTest, ListAllServicesWithThread) {
586 ExpectListServices({"Locksmith", "Valet"});
587 ExpectCheckService("Locksmith");
588 ExpectCheckService("Valet");
589
590 CallMain({"--thread"});
591
592 AssertRunningServices({"Locksmith", "Valet"});
593
594 const std::string format("(.|\n)*((Threads in use: [0-9]+/[0-9]+)?\n-(.|\n)*){2}");
595 AssertOutputFormat(format);
596}
597
598// Tests 'dumpsys --thread service_name'
599TEST_F(DumpsysTest, ListServiceWithThread) {
600 ExpectCheckService("Locksmith");
601
602 CallMain({"--thread", "Locksmith"});
603 // returns an empty string without root enabled
604 const std::string format("(^$|Threads in use: [0-9]/[0-9]+\n)");
605 AssertOutputFormat(format);
606}
607
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000608TEST_F(DumpsysTest, GetBytesWritten) {
609 const char* serviceName = "service2";
610 const char* dumpContents = "dump1";
611 ExpectDump(serviceName, dumpContents);
612
613 String16 service(serviceName);
614 Vector<String16> args;
615 std::chrono::duration<double> elapsedDuration;
616 size_t bytesWritten;
617
618 CallSingleService(service, args, IServiceManager::DUMP_FLAG_PRIORITY_ALL,
619 /* as_proto = */ false, elapsedDuration, bytesWritten);
620
621 AssertOutput(dumpContents);
622 EXPECT_THAT(bytesWritten, Eq(strlen(dumpContents)));
623}
624
Vishnu Naire4f61742017-12-21 08:30:28 -0800625TEST_F(DumpsysTest, WriteDumpWithoutThreadStart) {
626 std::chrono::duration<double> elapsedDuration;
627 size_t bytesWritten;
628 status_t status =
629 dump_.writeDump(STDOUT_FILENO, String16("service"), std::chrono::milliseconds(500),
630 /* as_proto = */ false, elapsedDuration, bytesWritten);
631 EXPECT_THAT(status, Eq(INVALID_OPERATION));
Steven Moreland5a30d342019-10-08 13:53:28 -0700632}
Devin Moorecfeeda42020-12-08 12:50:58 -0800633
634int main(int argc, char** argv) {
635 ::testing::InitGoogleTest(&argc, argv);
636
637 // start a binder thread pool for testing --thread option
638 android::ProcessState::self()->setThreadPoolMaxThreadCount(8);
639 ProcessState::self()->startThreadPool();
640
641 return RUN_ALL_TESTS();
642}