blob: 01a2fa3b22b45a548ed1895a537263f5a97ae19f [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>
26#include <utils/Vector.h>
27
28using namespace android;
29
30using ::testing::_;
31using ::testing::Action;
32using ::testing::ActionInterface;
33using ::testing::DoAll;
34using ::testing::Eq;
35using ::testing::HasSubstr;
36using ::testing::MakeAction;
37using ::testing::Not;
38using ::testing::Return;
39using ::testing::StrEq;
40using ::testing::Test;
41using ::testing::WithArg;
42using ::testing::internal::CaptureStderr;
43using ::testing::internal::CaptureStdout;
44using ::testing::internal::GetCapturedStderr;
45using ::testing::internal::GetCapturedStdout;
46
Steven Moreland6270dd12017-01-20 15:24:51 -080047using android::hardware::hidl_vec;
48using android::hardware::hidl_string;
49using android::hardware::Void;
50using HServiceManager = android::hidl::manager::V1_0::IServiceManager;
51using IServiceNotification = android::hidl::manager::V1_0::IServiceNotification;
52
Felipe Leme343175a2016-08-02 18:57:37 -070053class ServiceManagerMock : public IServiceManager {
54 public:
55 MOCK_CONST_METHOD1(getService, sp<IBinder>(const String16&));
56 MOCK_CONST_METHOD1(checkService, sp<IBinder>(const String16&));
57 MOCK_METHOD3(addService, status_t(const String16&, const sp<IBinder>&, bool));
58 MOCK_METHOD0(listServices, Vector<String16>());
59
60 protected:
61 MOCK_METHOD0(onAsBinder, IBinder*());
62};
63
Steven Moreland6270dd12017-01-20 15:24:51 -080064class HardwareServiceManagerMock : public HServiceManager {
65 public:
66 template<typename T>
67 using R = android::hardware::Return<T>; // conflicts with ::testing::Return
68
69 MOCK_METHOD2(get, R<sp<IBase>>(const hidl_string&, const hidl_string&));
70 MOCK_METHOD3(add,
71 R<bool>(const hidl_vec<hidl_string>&,
72 const hidl_string&,
73 const sp<IBase>&));
74 MOCK_METHOD1(list, R<void>(list_cb));
75 MOCK_METHOD2(listByInterface,
76 R<void>(const hidl_string&, listByInterface_cb));
77 MOCK_METHOD3(registerForNotifications,
78 R<bool>(const hidl_string&,
79 const hidl_string&,
80 const sp<IServiceNotification>&));
Yifan Hong5ccdab92017-01-25 22:53:37 -080081 MOCK_METHOD1(debugDump, R<void>(debugDump_cb));
Steven Moreland6270dd12017-01-20 15:24:51 -080082
83};
84
Felipe Leme343175a2016-08-02 18:57:37 -070085class BinderMock : public BBinder {
86 public:
87 BinderMock() {
88 }
89
90 MOCK_METHOD2(dump, status_t(int, const Vector<String16>&));
91};
92
93// gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher
94typedef void WriteOnFdFunction(int);
95
96class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
97 public:
98 explicit WriteOnFdAction(const std::string& output) : output_(output) {
99 }
100 virtual Result Perform(const ArgumentTuple& args) {
101 int fd = ::std::tr1::get<0>(args);
102 android::base::WriteStringToFd(output_, fd);
103 }
104
105 private:
106 std::string output_;
107};
108
109// Matcher used to emulate dump() by writing on its file descriptor.
110Action<WriteOnFdFunction> WriteOnFd(const std::string& output) {
111 return MakeAction(new WriteOnFdAction(output));
112}
113
Steven Moreland6270dd12017-01-20 15:24:51 -0800114// gmock black magic to provide a WithArg<0>(List(services)) matcher
115typedef void HardwareListFunction(HServiceManager::list_cb);
116
117class HardwareListAction : public ActionInterface<HardwareListFunction> {
118 public:
119 explicit HardwareListAction(const hidl_vec<hidl_string> &services) : services_(services) {
120 }
121 virtual Result Perform(const ArgumentTuple& args) {
122 auto cb = ::std::tr1::get<0>(args);
123 cb(services_);
124 }
125
126 private:
127 hidl_vec<hidl_string> services_;
128};
129
130Action<HardwareListFunction> HardwareList(const hidl_vec<hidl_string> &services) {
131 return MakeAction(new HardwareListAction(services));
132}
133
Felipe Leme343175a2016-08-02 18:57:37 -0700134// Matcher for args using Android's Vector<String16> format
135// TODO: move it to some common testing library
136MATCHER_P(AndroidElementsAre, expected, "") {
137 std::ostringstream errors;
138 if (arg.size() != expected.size()) {
139 errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size()
140 << ")\n";
141 }
142 int i = 0;
143 std::ostringstream actual_stream, expected_stream;
144 for (String16 actual : arg) {
145 std::string actual_str = String16::std_string(actual);
146 std::string expected_str = expected[i];
147 actual_stream << "'" << actual_str << "' ";
148 expected_stream << "'" << expected_str << "' ";
149 if (actual_str != expected_str) {
150 errors << " element mismatch at index " << i << "\n";
151 }
152 i++;
153 }
154
155 if (!errors.str().empty()) {
156 errors << "\nExpected args: " << expected_stream.str()
157 << "\nActual args: " << actual_stream.str();
158 *result_listener << errors.str();
159 return false;
160 }
161 return true;
162}
163
164// Custom action to sleep for timeout seconds
165ACTION_P(Sleep, timeout) {
166 sleep(timeout);
167}
168
169class DumpsysTest : public Test {
170 public:
Steven Moreland6270dd12017-01-20 15:24:51 -0800171 DumpsysTest() : sm_(), hm_(), dump_(&sm_, &hm_), stdout_(), stderr_() {
Felipe Leme343175a2016-08-02 18:57:37 -0700172 }
173
174 void ExpectListServices(std::vector<std::string> services) {
175 Vector<String16> services16;
176 for (auto& service : services) {
177 services16.add(String16(service.c_str()));
178 }
Steven Moreland6270dd12017-01-20 15:24:51 -0800179
Felipe Leme343175a2016-08-02 18:57:37 -0700180 EXPECT_CALL(sm_, listServices()).WillRepeatedly(Return(services16));
181 }
182
Steven Moreland6270dd12017-01-20 15:24:51 -0800183 void ExpectListHardwareServices(std::vector<std::string> services) {
184 hidl_vec<hidl_string> hidl_services;
185 hidl_services.resize(services.size());
186 for (size_t i = 0; i < services.size(); i++) {
187 hidl_services[i] = services[i];
188 }
189
190 EXPECT_CALL(hm_, list(_)).WillRepeatedly(DoAll(
191 WithArg<0>(HardwareList(hidl_services)),
192 Return(Void())));
193 }
194
Felipe Leme343175a2016-08-02 18:57:37 -0700195 sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
196 sp<BinderMock> binder_mock;
197 if (running) {
198 binder_mock = new BinderMock;
199 }
200 EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
201 return binder_mock;
202 }
203
204 void ExpectDump(const char* name, const std::string& output) {
205 sp<BinderMock> binder_mock = ExpectCheckService(name);
206 EXPECT_CALL(*binder_mock, dump(_, _))
207 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
208 }
209
210 void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
211 const std::string& output) {
212 sp<BinderMock> binder_mock = ExpectCheckService(name);
213 EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
214 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
215 }
216
217 void ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
218 sp<BinderMock> binder_mock = ExpectCheckService(name);
219 EXPECT_CALL(*binder_mock, dump(_, _))
220 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
221 }
222
223 void CallMain(const std::vector<std::string>& args) {
224 const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
225 int argc = (int)args.size() + 1;
226 int i = 1;
227 for (const std::string& arg : args) {
228 argv[i++] = arg.c_str();
229 }
230 CaptureStdout();
231 CaptureStderr();
232 int status = dump_.main(argc, const_cast<char**>(argv));
233 stdout_ = GetCapturedStdout();
234 stderr_ = GetCapturedStderr();
235 EXPECT_THAT(status, Eq(0));
236 }
237
Steven Moreland6270dd12017-01-20 15:24:51 -0800238 void AssertRunningServices(const std::vector<std::string>& services,
239 const std::string &message = "Currently running services:") {
240 std::string expected(message);
241 expected.append("\n");
Felipe Leme343175a2016-08-02 18:57:37 -0700242 for (const std::string& service : services) {
243 expected.append(" ").append(service).append("\n");
244 }
245 EXPECT_THAT(stdout_, HasSubstr(expected));
246 }
247
248 void AssertOutput(const std::string& expected) {
249 EXPECT_THAT(stdout_, StrEq(expected));
250 }
251
252 void AssertOutputContains(const std::string& expected) {
253 EXPECT_THAT(stdout_, HasSubstr(expected));
254 }
255
256 void AssertDumped(const std::string& service, const std::string& dump) {
257 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
258 }
259
260 void AssertNotDumped(const std::string& dump) {
261 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
262 }
263
264 void AssertStopped(const std::string& service) {
265 EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
266 }
267
268 ServiceManagerMock sm_;
Steven Moreland6270dd12017-01-20 15:24:51 -0800269 HardwareServiceManagerMock hm_;
Felipe Leme343175a2016-08-02 18:57:37 -0700270 Dumpsys dump_;
271
272 private:
273 std::string stdout_, stderr_;
274};
275
Steven Moreland6270dd12017-01-20 15:24:51 -0800276TEST_F(DumpsysTest, ListHwServices) {
277 ExpectListHardwareServices({"Locksmith", "Valet"});
278
279 CallMain({"--hw"});
280
281 AssertRunningServices({"Locksmith", "Valet"}, "Currently running hardware services:");
282}
283
Felipe Leme343175a2016-08-02 18:57:37 -0700284// Tests 'dumpsys -l' when all services are running
285TEST_F(DumpsysTest, ListAllServices) {
286 ExpectListServices({"Locksmith", "Valet"});
287 ExpectCheckService("Locksmith");
288 ExpectCheckService("Valet");
289
290 CallMain({"-l"});
291
292 AssertRunningServices({"Locksmith", "Valet"});
293}
294
295// Tests 'dumpsys -l' when a service is not running
296TEST_F(DumpsysTest, ListRunningServices) {
297 ExpectListServices({"Locksmith", "Valet"});
298 ExpectCheckService("Locksmith");
299 ExpectCheckService("Valet", false);
300
301 CallMain({"-l"});
302
303 AssertRunningServices({"Locksmith"});
304 AssertNotDumped({"Valet"});
305}
306
307// Tests 'dumpsys service_name' on a service is running
308TEST_F(DumpsysTest, DumpRunningService) {
309 ExpectDump("Valet", "Here's your car");
310
311 CallMain({"Valet"});
312
313 AssertOutput("Here's your car");
314}
315
316// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
317TEST_F(DumpsysTest, DumpRunningServiceTimeout) {
318 ExpectDumpAndHang("Valet", 2, "Here's your car");
319
320 CallMain({"-t", "1", "Valet"});
321
322 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1s) EXPIRED");
323 AssertNotDumped("Here's your car");
324
325 // Must wait so binder mock is deleted, otherwise test will fail with a leaked object
326 sleep(1);
327}
328
329// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
330TEST_F(DumpsysTest, DumpWithArgsRunningService) {
331 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
332
333 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
334
335 AssertOutput("I DO!");
336}
337
338// Tests 'dumpsys' with no arguments
339TEST_F(DumpsysTest, DumpMultipleServices) {
340 ExpectListServices({"running1", "stopped2", "running3"});
341 ExpectDump("running1", "dump1");
342 ExpectCheckService("stopped2", false);
343 ExpectDump("running3", "dump3");
344
345 CallMain({});
346
347 AssertRunningServices({"running1", "running3"});
348 AssertDumped("running1", "dump1");
349 AssertStopped("stopped2");
350 AssertDumped("running3", "dump3");
351}
352
353// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
354TEST_F(DumpsysTest, DumpWithSkip) {
355 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
356 ExpectDump("running1", "dump1");
357 ExpectCheckService("stopped2", false);
358 ExpectDump("skipped3", "dump3");
359 ExpectDump("running4", "dump4");
360 ExpectDump("skipped5", "dump5");
361
362 CallMain({"--skip", "skipped3", "skipped5"});
363
364 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
365 AssertDumped("running1", "dump1");
366 AssertDumped("running4", "dump4");
367 AssertStopped("stopped2");
368 AssertNotDumped("dump3");
369 AssertNotDumped("dump5");
370}