blob: 9fe45729af6158a693b16445d592f6ae444fb97a [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>
25#include <utils/String16.h>
Steven Morelanda0f7f2d2017-03-09 22:59:32 -080026#include <utils/String8.h>
Felipe Leme343175a2016-08-02 18:57:37 -070027#include <utils/Vector.h>
28
29using namespace android;
30
31using ::testing::_;
32using ::testing::Action;
33using ::testing::ActionInterface;
34using ::testing::DoAll;
35using ::testing::Eq;
36using ::testing::HasSubstr;
37using ::testing::MakeAction;
Felipe Leme5c8a98f2017-08-25 13:39:04 -070038using ::testing::Mock;
Felipe Leme343175a2016-08-02 18:57:37 -070039using ::testing::Not;
40using ::testing::Return;
41using ::testing::StrEq;
42using ::testing::Test;
43using ::testing::WithArg;
44using ::testing::internal::CaptureStderr;
45using ::testing::internal::CaptureStdout;
46using ::testing::internal::GetCapturedStderr;
47using ::testing::internal::GetCapturedStdout;
48
49class ServiceManagerMock : public IServiceManager {
50 public:
51 MOCK_CONST_METHOD1(getService, sp<IBinder>(const String16&));
52 MOCK_CONST_METHOD1(checkService, sp<IBinder>(const String16&));
Vishnu Nairf56042d2017-09-19 15:25:10 -070053 MOCK_METHOD4(addService, status_t(const String16&, const sp<IBinder>&, bool, int));
54 MOCK_METHOD1(listServices, Vector<String16>(int));
Felipe Leme343175a2016-08-02 18:57:37 -070055
56 protected:
57 MOCK_METHOD0(onAsBinder, IBinder*());
58};
59
60class BinderMock : public BBinder {
61 public:
62 BinderMock() {
63 }
64
65 MOCK_METHOD2(dump, status_t(int, const Vector<String16>&));
66};
67
68// gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher
69typedef void WriteOnFdFunction(int);
70
71class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
72 public:
73 explicit WriteOnFdAction(const std::string& output) : output_(output) {
74 }
75 virtual Result Perform(const ArgumentTuple& args) {
76 int fd = ::std::tr1::get<0>(args);
77 android::base::WriteStringToFd(output_, fd);
78 }
79
80 private:
81 std::string output_;
82};
83
84// Matcher used to emulate dump() by writing on its file descriptor.
85Action<WriteOnFdFunction> WriteOnFd(const std::string& output) {
86 return MakeAction(new WriteOnFdAction(output));
87}
88
89// Matcher for args using Android's Vector<String16> format
90// TODO: move it to some common testing library
91MATCHER_P(AndroidElementsAre, expected, "") {
92 std::ostringstream errors;
93 if (arg.size() != expected.size()) {
94 errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size()
95 << ")\n";
96 }
97 int i = 0;
98 std::ostringstream actual_stream, expected_stream;
Chih-Hung Hsiehcb057c22017-08-03 15:48:25 -070099 for (const String16& actual : arg) {
Steven Morelanda0f7f2d2017-03-09 22:59:32 -0800100 std::string actual_str = String8(actual).c_str();
Felipe Leme343175a2016-08-02 18:57:37 -0700101 std::string expected_str = expected[i];
102 actual_stream << "'" << actual_str << "' ";
103 expected_stream << "'" << expected_str << "' ";
104 if (actual_str != expected_str) {
105 errors << " element mismatch at index " << i << "\n";
106 }
107 i++;
108 }
109
110 if (!errors.str().empty()) {
111 errors << "\nExpected args: " << expected_stream.str()
112 << "\nActual args: " << actual_stream.str();
113 *result_listener << errors.str();
114 return false;
115 }
116 return true;
117}
118
119// Custom action to sleep for timeout seconds
120ACTION_P(Sleep, timeout) {
121 sleep(timeout);
122}
123
124class DumpsysTest : public Test {
125 public:
Steven Moreland2c3cd832017-02-13 23:44:17 +0000126 DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() {
Felipe Leme343175a2016-08-02 18:57:37 -0700127 }
128
129 void ExpectListServices(std::vector<std::string> services) {
130 Vector<String16> services16;
131 for (auto& service : services) {
132 services16.add(String16(service.c_str()));
133 }
Vishnu Nairf56042d2017-09-19 15:25:10 -0700134 EXPECT_CALL(sm_, listServices(IServiceManager::DUMP_PRIORITY_ALL))
135 .WillRepeatedly(Return(services16));
136 }
137
138 void ExpectListServicesWithPriority(std::vector<std::string> services, int dumpPriority) {
139 Vector<String16> services16;
140 for (auto& service : services) {
141 services16.add(String16(service.c_str()));
142 }
143 EXPECT_CALL(sm_, listServices(dumpPriority)).WillRepeatedly(Return(services16));
Felipe Leme343175a2016-08-02 18:57:37 -0700144 }
145
146 sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
147 sp<BinderMock> binder_mock;
148 if (running) {
149 binder_mock = new BinderMock;
150 }
151 EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
152 return binder_mock;
153 }
154
155 void ExpectDump(const char* name, const std::string& output) {
156 sp<BinderMock> binder_mock = ExpectCheckService(name);
157 EXPECT_CALL(*binder_mock, dump(_, _))
158 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
159 }
160
161 void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
162 const std::string& output) {
163 sp<BinderMock> binder_mock = ExpectCheckService(name);
164 EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
165 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
166 }
167
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700168 sp<BinderMock> ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
Felipe Leme343175a2016-08-02 18:57:37 -0700169 sp<BinderMock> binder_mock = ExpectCheckService(name);
170 EXPECT_CALL(*binder_mock, dump(_, _))
171 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700172 return binder_mock;
Felipe Leme343175a2016-08-02 18:57:37 -0700173 }
174
175 void CallMain(const std::vector<std::string>& args) {
176 const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
177 int argc = (int)args.size() + 1;
178 int i = 1;
179 for (const std::string& arg : args) {
180 argv[i++] = arg.c_str();
181 }
182 CaptureStdout();
183 CaptureStderr();
184 int status = dump_.main(argc, const_cast<char**>(argv));
185 stdout_ = GetCapturedStdout();
186 stderr_ = GetCapturedStderr();
187 EXPECT_THAT(status, Eq(0));
188 }
189
Steven Moreland2c3cd832017-02-13 23:44:17 +0000190 void AssertRunningServices(const std::vector<std::string>& services) {
Vishnu Nairf56042d2017-09-19 15:25:10 -0700191 std::string expected;
192 if (services.size() > 1) {
193 expected.append("Currently running services:\n");
194 }
Felipe Leme343175a2016-08-02 18:57:37 -0700195 for (const std::string& service : services) {
196 expected.append(" ").append(service).append("\n");
197 }
198 EXPECT_THAT(stdout_, HasSubstr(expected));
199 }
200
201 void AssertOutput(const std::string& expected) {
202 EXPECT_THAT(stdout_, StrEq(expected));
203 }
204
205 void AssertOutputContains(const std::string& expected) {
206 EXPECT_THAT(stdout_, HasSubstr(expected));
207 }
208
209 void AssertDumped(const std::string& service, const std::string& dump) {
210 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
211 }
212
213 void AssertNotDumped(const std::string& dump) {
214 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
215 }
216
217 void AssertStopped(const std::string& service) {
218 EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
219 }
220
221 ServiceManagerMock sm_;
222 Dumpsys dump_;
223
224 private:
225 std::string stdout_, stderr_;
226};
227
Felipe Leme343175a2016-08-02 18:57:37 -0700228// Tests 'dumpsys -l' when all services are running
229TEST_F(DumpsysTest, ListAllServices) {
230 ExpectListServices({"Locksmith", "Valet"});
231 ExpectCheckService("Locksmith");
232 ExpectCheckService("Valet");
233
234 CallMain({"-l"});
235
236 AssertRunningServices({"Locksmith", "Valet"});
237}
238
239// Tests 'dumpsys -l' when a service is not running
240TEST_F(DumpsysTest, ListRunningServices) {
241 ExpectListServices({"Locksmith", "Valet"});
242 ExpectCheckService("Locksmith");
243 ExpectCheckService("Valet", false);
244
245 CallMain({"-l"});
246
247 AssertRunningServices({"Locksmith"});
248 AssertNotDumped({"Valet"});
249}
250
Vishnu Nairf56042d2017-09-19 15:25:10 -0700251// Tests 'dumpsys -l --priority HIGH'
252TEST_F(DumpsysTest, ListAllServicesWithPriority) {
253 ExpectListServicesWithPriority({"Locksmith", "Valet"}, IServiceManager::DUMP_PRIORITY_HIGH);
254 ExpectCheckService("Locksmith");
255 ExpectCheckService("Valet");
256
257 CallMain({"-l", "--priority", "HIGH"});
258
259 AssertRunningServices({"Locksmith", "Valet"});
260}
261
262// Tests 'dumpsys -l --priority HIGH' with and empty list
263TEST_F(DumpsysTest, ListEmptyServicesWithPriority) {
264 ExpectListServicesWithPriority({}, IServiceManager::DUMP_PRIORITY_HIGH);
265
266 CallMain({"-l", "--priority", "HIGH"});
267
268 AssertRunningServices({});
269}
270
Felipe Leme343175a2016-08-02 18:57:37 -0700271// Tests 'dumpsys service_name' on a service is running
272TEST_F(DumpsysTest, DumpRunningService) {
273 ExpectDump("Valet", "Here's your car");
274
275 CallMain({"Valet"});
276
277 AssertOutput("Here's your car");
278}
279
280// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
281TEST_F(DumpsysTest, DumpRunningServiceTimeout) {
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700282 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700283
284 CallMain({"-t", "1", "Valet"});
285
286 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1s) EXPIRED");
287 AssertNotDumped("Here's your car");
288
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700289 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
290 Mock::AllowLeak(binder_mock.get());
Felipe Leme343175a2016-08-02 18:57:37 -0700291}
292
293// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
294TEST_F(DumpsysTest, DumpWithArgsRunningService) {
295 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
296
297 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
298
299 AssertOutput("I DO!");
300}
301
302// Tests 'dumpsys' with no arguments
303TEST_F(DumpsysTest, DumpMultipleServices) {
304 ExpectListServices({"running1", "stopped2", "running3"});
305 ExpectDump("running1", "dump1");
306 ExpectCheckService("stopped2", false);
307 ExpectDump("running3", "dump3");
308
309 CallMain({});
310
311 AssertRunningServices({"running1", "running3"});
312 AssertDumped("running1", "dump1");
313 AssertStopped("stopped2");
314 AssertDumped("running3", "dump3");
315}
316
317// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
318TEST_F(DumpsysTest, DumpWithSkip) {
319 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
320 ExpectDump("running1", "dump1");
321 ExpectCheckService("stopped2", false);
322 ExpectDump("skipped3", "dump3");
323 ExpectDump("running4", "dump4");
324 ExpectDump("skipped5", "dump5");
325
326 CallMain({"--skip", "skipped3", "skipped5"});
327
328 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
329 AssertDumped("running1", "dump1");
330 AssertDumped("running4", "dump4");
331 AssertStopped("stopped2");
332 AssertNotDumped("dump3");
333 AssertNotDumped("dump5");
334}
Vishnu Nairf56042d2017-09-19 15:25:10 -0700335
336// Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services
337TEST_F(DumpsysTest, DumpWithSkipAndPriority) {
338 ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"},
339 IServiceManager::DUMP_PRIORITY_CRITICAL);
340 ExpectDump("running1", "dump1");
341 ExpectCheckService("stopped2", false);
342 ExpectDump("skipped3", "dump3");
343 ExpectDump("running4", "dump4");
344 ExpectDump("skipped5", "dump5");
345
346 CallMain({"--priority", "CRITICAL", "--skip", "skipped3", "skipped5"});
347
348 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
349 AssertDumped("running1", "dump1");
350 AssertDumped("running4", "dump4");
351 AssertStopped("stopped2");
352 AssertNotDumped("dump3");
353 AssertNotDumped("dump5");
354}
355
356// Tests 'dumpsys --priority CRITICAL'
357TEST_F(DumpsysTest, DumpWithPriorityCritical) {
358 ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"},
359 IServiceManager::DUMP_PRIORITY_CRITICAL);
360 ExpectDump("runningcritical1", "dump1");
361 ExpectDump("runningcritical2", "dump2");
362
363 CallMain({"--priority", "CRITICAL"});
364
365 AssertRunningServices({"runningcritical1", "runningcritical2"});
366 AssertDumped("runningcritical1", "dump1");
367 AssertDumped("runningcritical2", "dump2");
368}
369
370// Tests 'dumpsys --priority HIGH'
371TEST_F(DumpsysTest, DumpWithPriorityHigh) {
372 ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
373 IServiceManager::DUMP_PRIORITY_HIGH);
374 ExpectDump("runninghigh1", "dump1");
375 ExpectDump("runninghigh2", "dump2");
376
377 CallMain({"--priority", "HIGH"});
378
379 AssertRunningServices({"runninghigh1", "runninghigh2"});
380 AssertDumped("runninghigh1", "dump1");
381 AssertDumped("runninghigh2", "dump2");
382}
383
384// Tests 'dumpsys --priority NORMAL'
385TEST_F(DumpsysTest, DumpWithPriorityNormal) {
386 ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"},
387 IServiceManager::DUMP_PRIORITY_NORMAL);
388 ExpectDump("runningnormal1", "dump1");
389 ExpectDump("runningnormal2", "dump2");
390
391 CallMain({"--priority", "NORMAL"});
392
393 AssertRunningServices({"runningnormal1", "runningnormal2"});
394 AssertDumped("runningnormal1", "dump1");
395 AssertDumped("runningnormal2", "dump2");
396}