blob: 0ad256471f1336ecb18f4461bc3f82436416ff9e [file] [log] [blame]
Felipe Leme343175a2016-08-02 18:57:37 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "../dumpsys.h"
18
19#include <vector>
20
21#include <gmock/gmock.h>
22#include <gtest/gtest.h>
23
24#include <android-base/file.h>
Vishnu Nair6a408532017-10-24 09:11:27 -070025#include <serviceutils/PriorityDumper.h>
Felipe Leme343175a2016-08-02 18:57:37 -070026#include <utils/String16.h>
Steven Morelanda0f7f2d2017-03-09 22:59:32 -080027#include <utils/String8.h>
Felipe Leme343175a2016-08-02 18:57:37 -070028#include <utils/Vector.h>
29
30using namespace android;
31
32using ::testing::_;
33using ::testing::Action;
34using ::testing::ActionInterface;
35using ::testing::DoAll;
36using ::testing::Eq;
37using ::testing::HasSubstr;
38using ::testing::MakeAction;
Felipe Leme5c8a98f2017-08-25 13:39:04 -070039using ::testing::Mock;
Felipe Leme343175a2016-08-02 18:57:37 -070040using ::testing::Not;
41using ::testing::Return;
42using ::testing::StrEq;
43using ::testing::Test;
44using ::testing::WithArg;
45using ::testing::internal::CaptureStderr;
46using ::testing::internal::CaptureStdout;
47using ::testing::internal::GetCapturedStderr;
48using ::testing::internal::GetCapturedStdout;
49
50class ServiceManagerMock : public IServiceManager {
51 public:
52 MOCK_CONST_METHOD1(getService, sp<IBinder>(const String16&));
53 MOCK_CONST_METHOD1(checkService, sp<IBinder>(const String16&));
Vishnu Nairf56042d2017-09-19 15:25:10 -070054 MOCK_METHOD4(addService, status_t(const String16&, const sp<IBinder>&, bool, int));
55 MOCK_METHOD1(listServices, Vector<String16>(int));
Steven Moreland1c47b582019-08-27 18:05:27 -070056 MOCK_METHOD1(waitForService, sp<IBinder>(const String16&));
Steven Morelandb82b8f82019-10-28 10:52:34 -070057 MOCK_METHOD1(isDeclared, bool(const String16&));
Steven Moreland2e293aa2020-09-23 00:25:16 +000058 MOCK_METHOD1(getDeclaredInstances, Vector<String16>(const String16&));
Steven Morelandedd4e072021-04-21 00:27:29 +000059 MOCK_METHOD1(updatableViaApex, std::optional<String16>(const String16&));
Felipe Leme343175a2016-08-02 18:57:37 -070060 protected:
61 MOCK_METHOD0(onAsBinder, IBinder*());
62};
63
64class BinderMock : public BBinder {
65 public:
66 BinderMock() {
67 }
68
69 MOCK_METHOD2(dump, status_t(int, const Vector<String16>&));
70};
71
72// gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher
73typedef void WriteOnFdFunction(int);
74
75class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
76 public:
77 explicit WriteOnFdAction(const std::string& output) : output_(output) {
78 }
79 virtual Result Perform(const ArgumentTuple& args) {
Haibo Huang21f36552018-07-10 19:48:18 -070080 int fd = ::testing::get<0>(args);
Felipe Leme343175a2016-08-02 18:57:37 -070081 android::base::WriteStringToFd(output_, fd);
82 }
83
84 private:
85 std::string output_;
86};
87
88// Matcher used to emulate dump() by writing on its file descriptor.
89Action<WriteOnFdFunction> WriteOnFd(const std::string& output) {
90 return MakeAction(new WriteOnFdAction(output));
91}
92
93// Matcher for args using Android's Vector<String16> format
94// TODO: move it to some common testing library
95MATCHER_P(AndroidElementsAre, expected, "") {
96 std::ostringstream errors;
97 if (arg.size() != expected.size()) {
98 errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size()
99 << ")\n";
100 }
101 int i = 0;
102 std::ostringstream actual_stream, expected_stream;
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -0700103 for (const String16& actual : arg) {
Steven Morelanda0f7f2d2017-03-09 22:59:32 -0800104 std::string actual_str = String8(actual).c_str();
Felipe Leme343175a2016-08-02 18:57:37 -0700105 std::string expected_str = expected[i];
106 actual_stream << "'" << actual_str << "' ";
107 expected_stream << "'" << expected_str << "' ";
108 if (actual_str != expected_str) {
109 errors << " element mismatch at index " << i << "\n";
110 }
111 i++;
112 }
113
114 if (!errors.str().empty()) {
115 errors << "\nExpected args: " << expected_stream.str()
116 << "\nActual args: " << actual_stream.str();
117 *result_listener << errors.str();
118 return false;
119 }
120 return true;
121}
122
123// Custom action to sleep for timeout seconds
124ACTION_P(Sleep, timeout) {
125 sleep(timeout);
126}
127
128class DumpsysTest : public Test {
129 public:
Steven Moreland2c3cd832017-02-13 23:44:17 +0000130 DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() {
Felipe Leme343175a2016-08-02 18:57:37 -0700131 }
132
133 void ExpectListServices(std::vector<std::string> services) {
134 Vector<String16> services16;
135 for (auto& service : services) {
136 services16.add(String16(service.c_str()));
137 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700138 EXPECT_CALL(sm_, listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL))
Vishnu Nairf56042d2017-09-19 15:25:10 -0700139 .WillRepeatedly(Return(services16));
140 }
141
Vishnu Nair6a408532017-10-24 09:11:27 -0700142 void ExpectListServicesWithPriority(std::vector<std::string> services, int dumpFlags) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700143 Vector<String16> services16;
144 for (auto& service : services) {
145 services16.add(String16(service.c_str()));
146 }
Vishnu Nair6a408532017-10-24 09:11:27 -0700147 EXPECT_CALL(sm_, listServices(dumpFlags)).WillRepeatedly(Return(services16));
Felipe Leme343175a2016-08-02 18:57:37 -0700148 }
149
150 sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
151 sp<BinderMock> binder_mock;
152 if (running) {
153 binder_mock = new BinderMock;
154 }
155 EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
156 return binder_mock;
157 }
158
159 void ExpectDump(const char* name, const std::string& output) {
160 sp<BinderMock> binder_mock = ExpectCheckService(name);
161 EXPECT_CALL(*binder_mock, dump(_, _))
162 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
163 }
164
165 void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
166 const std::string& output) {
167 sp<BinderMock> binder_mock = ExpectCheckService(name);
168 EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
169 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
170 }
171
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700172 sp<BinderMock> ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
Felipe Leme343175a2016-08-02 18:57:37 -0700173 sp<BinderMock> binder_mock = ExpectCheckService(name);
174 EXPECT_CALL(*binder_mock, dump(_, _))
175 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700176 return binder_mock;
Felipe Leme343175a2016-08-02 18:57:37 -0700177 }
178
179 void CallMain(const std::vector<std::string>& args) {
180 const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
181 int argc = (int)args.size() + 1;
182 int i = 1;
183 for (const std::string& arg : args) {
184 argv[i++] = arg.c_str();
185 }
186 CaptureStdout();
187 CaptureStderr();
188 int status = dump_.main(argc, const_cast<char**>(argv));
189 stdout_ = GetCapturedStdout();
190 stderr_ = GetCapturedStderr();
191 EXPECT_THAT(status, Eq(0));
192 }
193
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000194 void CallSingleService(const String16& serviceName, Vector<String16>& args, int priorityFlags,
195 bool supportsProto, std::chrono::duration<double>& elapsedDuration,
196 size_t& bytesWritten) {
197 CaptureStdout();
198 CaptureStderr();
199 dump_.setServiceArgs(args, supportsProto, priorityFlags);
Steven Moreland5a30d342019-10-08 13:53:28 -0700200 status_t status = dump_.startDumpThread(Dumpsys::Type::DUMP, serviceName, args);
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000201 EXPECT_THAT(status, Eq(0));
202 status = dump_.writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(500), false,
203 elapsedDuration, bytesWritten);
204 EXPECT_THAT(status, Eq(0));
205 dump_.stopDumpThread(/* dumpCompleted = */ true);
206 stdout_ = GetCapturedStdout();
207 stderr_ = GetCapturedStderr();
208 }
209
Steven Moreland2c3cd832017-02-13 23:44:17 +0000210 void AssertRunningServices(const std::vector<std::string>& services) {
Steven Moreland31dac352020-03-05 09:46:45 -0800211 std::string expected = "Currently running services:\n";
Felipe Leme343175a2016-08-02 18:57:37 -0700212 for (const std::string& service : services) {
213 expected.append(" ").append(service).append("\n");
214 }
215 EXPECT_THAT(stdout_, HasSubstr(expected));
216 }
217
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000218 void AssertOutput(const std::string& expected) {
219 EXPECT_THAT(stdout_, StrEq(expected));
220 }
221
Felipe Leme343175a2016-08-02 18:57:37 -0700222 void AssertOutputContains(const std::string& expected) {
223 EXPECT_THAT(stdout_, HasSubstr(expected));
224 }
225
226 void AssertDumped(const std::string& service, const std::string& dump) {
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000227 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800228 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Felipe Leme343175a2016-08-02 18:57:37 -0700229 }
230
Vishnu Nair6a408532017-10-24 09:11:27 -0700231 void AssertDumpedWithPriority(const std::string& service, const std::string& dump,
232 const char16_t* priorityType) {
233 std::string priority = String8(priorityType).c_str();
234 EXPECT_THAT(stdout_,
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000235 HasSubstr("DUMP OF SERVICE " + priority + " " + service + ":\n" + dump));
Vishnu Naire4f61742017-12-21 08:30:28 -0800236 EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
Vishnu Nair6a408532017-10-24 09:11:27 -0700237 }
238
Felipe Leme343175a2016-08-02 18:57:37 -0700239 void AssertNotDumped(const std::string& dump) {
240 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
241 }
242
243 void AssertStopped(const std::string& service) {
244 EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
245 }
246
247 ServiceManagerMock sm_;
248 Dumpsys dump_;
249
250 private:
251 std::string stdout_, stderr_;
252};
253
Felipe Leme343175a2016-08-02 18:57:37 -0700254// Tests 'dumpsys -l' when all services are running
255TEST_F(DumpsysTest, ListAllServices) {
256 ExpectListServices({"Locksmith", "Valet"});
257 ExpectCheckService("Locksmith");
258 ExpectCheckService("Valet");
259
260 CallMain({"-l"});
261
262 AssertRunningServices({"Locksmith", "Valet"});
263}
264
Steven Moreland31dac352020-03-05 09:46:45 -0800265TEST_F(DumpsysTest, ListServicesOneRegistered) {
266 ExpectListServices({"Locksmith"});
267 ExpectCheckService("Locksmith");
268
269 CallMain({"-l"});
270
271 AssertRunningServices({"Locksmith"});
272}
273
274TEST_F(DumpsysTest, ListServicesEmpty) {
275 CallMain({"-l"});
276
277 AssertRunningServices({});
278}
279
Felipe Leme343175a2016-08-02 18:57:37 -0700280// Tests 'dumpsys -l' when a service is not running
281TEST_F(DumpsysTest, ListRunningServices) {
282 ExpectListServices({"Locksmith", "Valet"});
283 ExpectCheckService("Locksmith");
284 ExpectCheckService("Valet", false);
285
286 CallMain({"-l"});
287
288 AssertRunningServices({"Locksmith"});
289 AssertNotDumped({"Valet"});
290}
291
Vishnu Nairf56042d2017-09-19 15:25:10 -0700292// Tests 'dumpsys -l --priority HIGH'
293TEST_F(DumpsysTest, ListAllServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700294 ExpectListServicesWithPriority({"Locksmith", "Valet"}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700295 ExpectCheckService("Locksmith");
296 ExpectCheckService("Valet");
297
298 CallMain({"-l", "--priority", "HIGH"});
299
300 AssertRunningServices({"Locksmith", "Valet"});
301}
302
303// Tests 'dumpsys -l --priority HIGH' with and empty list
304TEST_F(DumpsysTest, ListEmptyServicesWithPriority) {
Vishnu Nair6a408532017-10-24 09:11:27 -0700305 ExpectListServicesWithPriority({}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700306
307 CallMain({"-l", "--priority", "HIGH"});
308
309 AssertRunningServices({});
310}
311
Vishnu Nair6a408532017-10-24 09:11:27 -0700312// Tests 'dumpsys -l --proto'
313TEST_F(DumpsysTest, ListAllServicesWithProto) {
314 ExpectListServicesWithPriority({"Locksmith", "Valet", "Car"},
315 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
316 ExpectListServicesWithPriority({"Valet", "Car"}, IServiceManager::DUMP_FLAG_PROTO);
317 ExpectCheckService("Car");
318 ExpectCheckService("Valet");
319
320 CallMain({"-l", "--proto"});
321
322 AssertRunningServices({"Car", "Valet"});
323}
324
Felipe Leme343175a2016-08-02 18:57:37 -0700325// Tests 'dumpsys service_name' on a service is running
326TEST_F(DumpsysTest, DumpRunningService) {
327 ExpectDump("Valet", "Here's your car");
328
329 CallMain({"Valet"});
330
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000331 AssertOutput("Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700332}
333
334// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
Vishnu Nair6921f802017-11-22 09:17:23 -0800335TEST_F(DumpsysTest, DumpRunningServiceTimeoutInSec) {
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700336 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700337
338 CallMain({"-t", "1", "Valet"});
339
Vishnu Nair6921f802017-11-22 09:17:23 -0800340 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1000ms) EXPIRED");
341 AssertNotDumped("Here's your car");
342
343 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
344 Mock::AllowLeak(binder_mock.get());
345}
346
347// Tests 'dumpsys -T 500 service_name' on a service that times out after 2s
348TEST_F(DumpsysTest, DumpRunningServiceTimeoutInMs) {
349 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
350
351 CallMain({"-T", "500", "Valet"});
352
353 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (500ms) EXPIRED");
Felipe Leme343175a2016-08-02 18:57:37 -0700354 AssertNotDumped("Here's your car");
355
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700356 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
357 Mock::AllowLeak(binder_mock.get());
Felipe Leme343175a2016-08-02 18:57:37 -0700358}
359
360// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
361TEST_F(DumpsysTest, DumpWithArgsRunningService) {
362 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
363
364 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
365
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000366 AssertOutput("I DO!");
Felipe Leme343175a2016-08-02 18:57:37 -0700367}
368
Vishnu Nair64afc022018-02-01 15:29:34 -0800369// Tests dumpsys passes the -a flag when called on all services
370TEST_F(DumpsysTest, PassAllFlagsToServices) {
371 ExpectListServices({"Locksmith", "Valet"});
372 ExpectCheckService("Locksmith");
373 ExpectCheckService("Valet");
374 ExpectDumpWithArgs("Locksmith", {"-a"}, "dumped1");
375 ExpectDumpWithArgs("Valet", {"-a"}, "dumped2");
376
377 CallMain({"-T", "500"});
378
379 AssertDumped("Locksmith", "dumped1");
380 AssertDumped("Valet", "dumped2");
381}
382
383// Tests dumpsys passes the -a flag when called on NORMAL priority services
384TEST_F(DumpsysTest, PassAllFlagsToNormalServices) {
385 ExpectListServicesWithPriority({"Locksmith", "Valet"},
386 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
387 ExpectCheckService("Locksmith");
388 ExpectCheckService("Valet");
Vishnu Nair3919e1c2018-05-01 17:01:00 -0700389 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "NORMAL", "-a"}, "dump1");
390 ExpectDumpWithArgs("Valet", {"--dump-priority", "NORMAL", "-a"}, "dump2");
Vishnu Nair64afc022018-02-01 15:29:34 -0800391
392 CallMain({"--priority", "NORMAL"});
393
394 AssertDumped("Locksmith", "dump1");
395 AssertDumped("Valet", "dump2");
396}
397
398// Tests dumpsys passes only priority flags when called on CRITICAL priority services
399TEST_F(DumpsysTest, PassPriorityFlagsToCriticalServices) {
400 ExpectListServicesWithPriority({"Locksmith", "Valet"},
401 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
402 ExpectCheckService("Locksmith");
403 ExpectCheckService("Valet");
404 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "CRITICAL"}, "dump1");
405 ExpectDumpWithArgs("Valet", {"--dump-priority", "CRITICAL"}, "dump2");
406
407 CallMain({"--priority", "CRITICAL"});
408
409 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
410 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
411}
412
413// Tests dumpsys passes only priority flags when called on HIGH priority services
414TEST_F(DumpsysTest, PassPriorityFlagsToHighServices) {
415 ExpectListServicesWithPriority({"Locksmith", "Valet"},
416 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
417 ExpectCheckService("Locksmith");
418 ExpectCheckService("Valet");
419 ExpectDumpWithArgs("Locksmith", {"--dump-priority", "HIGH"}, "dump1");
420 ExpectDumpWithArgs("Valet", {"--dump-priority", "HIGH"}, "dump2");
421
422 CallMain({"--priority", "HIGH"});
423
424 AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
425 AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
426}
427
Felipe Leme343175a2016-08-02 18:57:37 -0700428// Tests 'dumpsys' with no arguments
429TEST_F(DumpsysTest, DumpMultipleServices) {
430 ExpectListServices({"running1", "stopped2", "running3"});
431 ExpectDump("running1", "dump1");
432 ExpectCheckService("stopped2", false);
433 ExpectDump("running3", "dump3");
434
435 CallMain({});
436
437 AssertRunningServices({"running1", "running3"});
438 AssertDumped("running1", "dump1");
439 AssertStopped("stopped2");
440 AssertDumped("running3", "dump3");
441}
442
443// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
444TEST_F(DumpsysTest, DumpWithSkip) {
445 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
446 ExpectDump("running1", "dump1");
447 ExpectCheckService("stopped2", false);
448 ExpectDump("skipped3", "dump3");
449 ExpectDump("running4", "dump4");
450 ExpectDump("skipped5", "dump5");
451
452 CallMain({"--skip", "skipped3", "skipped5"});
453
454 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
455 AssertDumped("running1", "dump1");
456 AssertDumped("running4", "dump4");
457 AssertStopped("stopped2");
458 AssertNotDumped("dump3");
459 AssertNotDumped("dump5");
460}
Vishnu Nairf56042d2017-09-19 15:25:10 -0700461
462// Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services
463TEST_F(DumpsysTest, DumpWithSkipAndPriority) {
464 ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700465 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700466 ExpectDump("running1", "dump1");
467 ExpectCheckService("stopped2", false);
468 ExpectDump("skipped3", "dump3");
469 ExpectDump("running4", "dump4");
470 ExpectDump("skipped5", "dump5");
471
472 CallMain({"--priority", "CRITICAL", "--skip", "skipped3", "skipped5"});
473
474 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700475 AssertDumpedWithPriority("running1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
476 AssertDumpedWithPriority("running4", "dump4", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700477 AssertStopped("stopped2");
478 AssertNotDumped("dump3");
479 AssertNotDumped("dump5");
480}
481
482// Tests 'dumpsys --priority CRITICAL'
483TEST_F(DumpsysTest, DumpWithPriorityCritical) {
484 ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700485 IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700486 ExpectDump("runningcritical1", "dump1");
487 ExpectDump("runningcritical2", "dump2");
488
489 CallMain({"--priority", "CRITICAL"});
490
491 AssertRunningServices({"runningcritical1", "runningcritical2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700492 AssertDumpedWithPriority("runningcritical1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
493 AssertDumpedWithPriority("runningcritical2", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700494}
495
496// Tests 'dumpsys --priority HIGH'
497TEST_F(DumpsysTest, DumpWithPriorityHigh) {
498 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700499 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700500 ExpectDump("runninghigh1", "dump1");
501 ExpectDump("runninghigh2", "dump2");
502
503 CallMain({"--priority", "HIGH"});
504
505 AssertRunningServices({"runninghigh1", "runninghigh2"});
Vishnu Nair6a408532017-10-24 09:11:27 -0700506 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
507 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700508}
509
510// Tests 'dumpsys --priority NORMAL'
511TEST_F(DumpsysTest, DumpWithPriorityNormal) {
512 ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"},
Vishnu Nair6a408532017-10-24 09:11:27 -0700513 IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700514 ExpectDump("runningnormal1", "dump1");
515 ExpectDump("runningnormal2", "dump2");
516
517 CallMain({"--priority", "NORMAL"});
518
519 AssertRunningServices({"runningnormal1", "runningnormal2"});
Vishnu Naire4f61742017-12-21 08:30:28 -0800520 AssertDumped("runningnormal1", "dump1");
521 AssertDumped("runningnormal2", "dump2");
Vishnu Nair6a408532017-10-24 09:11:27 -0700522}
523
524// Tests 'dumpsys --proto'
525TEST_F(DumpsysTest, DumpWithProto) {
526 ExpectListServicesWithPriority({"run8", "run1", "run2", "run5"},
527 IServiceManager::DUMP_FLAG_PRIORITY_ALL);
528 ExpectListServicesWithPriority({"run3", "run2", "run4", "run8"},
529 IServiceManager::DUMP_FLAG_PROTO);
530 ExpectDump("run2", "dump1");
531 ExpectDump("run8", "dump2");
532
533 CallMain({"--proto"});
534
535 AssertRunningServices({"run2", "run8"});
536 AssertDumped("run2", "dump1");
537 AssertDumped("run8", "dump2");
538}
539
540// Tests 'dumpsys --priority HIGH --proto'
541TEST_F(DumpsysTest, DumpWithPriorityHighAndProto) {
542 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
543 IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
544 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2", "runninghigh3"},
545 IServiceManager::DUMP_FLAG_PROTO);
546
547 ExpectDump("runninghigh1", "dump1");
548 ExpectDump("runninghigh2", "dump2");
549
550 CallMain({"--priority", "HIGH", "--proto"});
551
552 AssertRunningServices({"runninghigh1", "runninghigh2"});
553 AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
554 AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700555}
Vishnu Naire4f61742017-12-21 08:30:28 -0800556
Steven Moreland5a30d342019-10-08 13:53:28 -0700557// Tests 'dumpsys --pid'
558TEST_F(DumpsysTest, ListAllServicesWithPid) {
559 ExpectListServices({"Locksmith", "Valet"});
560 ExpectCheckService("Locksmith");
561 ExpectCheckService("Valet");
562
563 CallMain({"--pid"});
564
565 AssertRunningServices({"Locksmith", "Valet"});
566 AssertOutputContains(std::to_string(getpid()));
567}
568
569// Tests 'dumpsys --pid service_name'
570TEST_F(DumpsysTest, ListServiceWithPid) {
571 ExpectCheckService("Locksmith");
572
573 CallMain({"--pid", "Locksmith"});
574
575 AssertOutput(std::to_string(getpid()) + "\n");
576}
577
Steven Morelanda6ddb9a2019-09-27 16:41:02 +0000578TEST_F(DumpsysTest, GetBytesWritten) {
579 const char* serviceName = "service2";
580 const char* dumpContents = "dump1";
581 ExpectDump(serviceName, dumpContents);
582
583 String16 service(serviceName);
584 Vector<String16> args;
585 std::chrono::duration<double> elapsedDuration;
586 size_t bytesWritten;
587
588 CallSingleService(service, args, IServiceManager::DUMP_FLAG_PRIORITY_ALL,
589 /* as_proto = */ false, elapsedDuration, bytesWritten);
590
591 AssertOutput(dumpContents);
592 EXPECT_THAT(bytesWritten, Eq(strlen(dumpContents)));
593}
594
Vishnu Naire4f61742017-12-21 08:30:28 -0800595TEST_F(DumpsysTest, WriteDumpWithoutThreadStart) {
596 std::chrono::duration<double> elapsedDuration;
597 size_t bytesWritten;
598 status_t status =
599 dump_.writeDump(STDOUT_FILENO, String16("service"), std::chrono::milliseconds(500),
600 /* as_proto = */ false, elapsedDuration, bytesWritten);
601 EXPECT_THAT(status, Eq(INVALID_OPERATION));
Steven Moreland5a30d342019-10-08 13:53:28 -0700602}