blob: a66685d23dfdc18f4dc7a1952e175c04191e568f [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>&));
81
82};
83
Felipe Leme343175a2016-08-02 18:57:37 -070084class BinderMock : public BBinder {
85 public:
86 BinderMock() {
87 }
88
89 MOCK_METHOD2(dump, status_t(int, const Vector<String16>&));
90};
91
92// gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher
93typedef void WriteOnFdFunction(int);
94
95class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
96 public:
97 explicit WriteOnFdAction(const std::string& output) : output_(output) {
98 }
99 virtual Result Perform(const ArgumentTuple& args) {
100 int fd = ::std::tr1::get<0>(args);
101 android::base::WriteStringToFd(output_, fd);
102 }
103
104 private:
105 std::string output_;
106};
107
108// Matcher used to emulate dump() by writing on its file descriptor.
109Action<WriteOnFdFunction> WriteOnFd(const std::string& output) {
110 return MakeAction(new WriteOnFdAction(output));
111}
112
Steven Moreland6270dd12017-01-20 15:24:51 -0800113// gmock black magic to provide a WithArg<0>(List(services)) matcher
114typedef void HardwareListFunction(HServiceManager::list_cb);
115
116class HardwareListAction : public ActionInterface<HardwareListFunction> {
117 public:
118 explicit HardwareListAction(const hidl_vec<hidl_string> &services) : services_(services) {
119 }
120 virtual Result Perform(const ArgumentTuple& args) {
121 auto cb = ::std::tr1::get<0>(args);
122 cb(services_);
123 }
124
125 private:
126 hidl_vec<hidl_string> services_;
127};
128
129Action<HardwareListFunction> HardwareList(const hidl_vec<hidl_string> &services) {
130 return MakeAction(new HardwareListAction(services));
131}
132
Felipe Leme343175a2016-08-02 18:57:37 -0700133// Matcher for args using Android's Vector<String16> format
134// TODO: move it to some common testing library
135MATCHER_P(AndroidElementsAre, expected, "") {
136 std::ostringstream errors;
137 if (arg.size() != expected.size()) {
138 errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size()
139 << ")\n";
140 }
141 int i = 0;
142 std::ostringstream actual_stream, expected_stream;
143 for (String16 actual : arg) {
144 std::string actual_str = String16::std_string(actual);
145 std::string expected_str = expected[i];
146 actual_stream << "'" << actual_str << "' ";
147 expected_stream << "'" << expected_str << "' ";
148 if (actual_str != expected_str) {
149 errors << " element mismatch at index " << i << "\n";
150 }
151 i++;
152 }
153
154 if (!errors.str().empty()) {
155 errors << "\nExpected args: " << expected_stream.str()
156 << "\nActual args: " << actual_stream.str();
157 *result_listener << errors.str();
158 return false;
159 }
160 return true;
161}
162
163// Custom action to sleep for timeout seconds
164ACTION_P(Sleep, timeout) {
165 sleep(timeout);
166}
167
168class DumpsysTest : public Test {
169 public:
Steven Moreland6270dd12017-01-20 15:24:51 -0800170 DumpsysTest() : sm_(), hm_(), dump_(&sm_, &hm_), stdout_(), stderr_() {
Felipe Leme343175a2016-08-02 18:57:37 -0700171 }
172
173 void ExpectListServices(std::vector<std::string> services) {
174 Vector<String16> services16;
175 for (auto& service : services) {
176 services16.add(String16(service.c_str()));
177 }
Steven Moreland6270dd12017-01-20 15:24:51 -0800178
Felipe Leme343175a2016-08-02 18:57:37 -0700179 EXPECT_CALL(sm_, listServices()).WillRepeatedly(Return(services16));
180 }
181
Steven Moreland6270dd12017-01-20 15:24:51 -0800182 void ExpectListHardwareServices(std::vector<std::string> services) {
183 hidl_vec<hidl_string> hidl_services;
184 hidl_services.resize(services.size());
185 for (size_t i = 0; i < services.size(); i++) {
186 hidl_services[i] = services[i];
187 }
188
189 EXPECT_CALL(hm_, list(_)).WillRepeatedly(DoAll(
190 WithArg<0>(HardwareList(hidl_services)),
191 Return(Void())));
192 }
193
Felipe Leme343175a2016-08-02 18:57:37 -0700194 sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
195 sp<BinderMock> binder_mock;
196 if (running) {
197 binder_mock = new BinderMock;
198 }
199 EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
200 return binder_mock;
201 }
202
203 void ExpectDump(const char* name, const std::string& output) {
204 sp<BinderMock> binder_mock = ExpectCheckService(name);
205 EXPECT_CALL(*binder_mock, dump(_, _))
206 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
207 }
208
209 void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
210 const std::string& output) {
211 sp<BinderMock> binder_mock = ExpectCheckService(name);
212 EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
213 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
214 }
215
216 void ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
217 sp<BinderMock> binder_mock = ExpectCheckService(name);
218 EXPECT_CALL(*binder_mock, dump(_, _))
219 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
220 }
221
222 void CallMain(const std::vector<std::string>& args) {
223 const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
224 int argc = (int)args.size() + 1;
225 int i = 1;
226 for (const std::string& arg : args) {
227 argv[i++] = arg.c_str();
228 }
229 CaptureStdout();
230 CaptureStderr();
231 int status = dump_.main(argc, const_cast<char**>(argv));
232 stdout_ = GetCapturedStdout();
233 stderr_ = GetCapturedStderr();
234 EXPECT_THAT(status, Eq(0));
235 }
236
Steven Moreland6270dd12017-01-20 15:24:51 -0800237 void AssertRunningServices(const std::vector<std::string>& services,
238 const std::string &message = "Currently running services:") {
239 std::string expected(message);
240 expected.append("\n");
Felipe Leme343175a2016-08-02 18:57:37 -0700241 for (const std::string& service : services) {
242 expected.append(" ").append(service).append("\n");
243 }
244 EXPECT_THAT(stdout_, HasSubstr(expected));
245 }
246
247 void AssertOutput(const std::string& expected) {
248 EXPECT_THAT(stdout_, StrEq(expected));
249 }
250
251 void AssertOutputContains(const std::string& expected) {
252 EXPECT_THAT(stdout_, HasSubstr(expected));
253 }
254
255 void AssertDumped(const std::string& service, const std::string& dump) {
256 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
257 }
258
259 void AssertNotDumped(const std::string& dump) {
260 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
261 }
262
263 void AssertStopped(const std::string& service) {
264 EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
265 }
266
267 ServiceManagerMock sm_;
Steven Moreland6270dd12017-01-20 15:24:51 -0800268 HardwareServiceManagerMock hm_;
Felipe Leme343175a2016-08-02 18:57:37 -0700269 Dumpsys dump_;
270
271 private:
272 std::string stdout_, stderr_;
273};
274
Steven Moreland6270dd12017-01-20 15:24:51 -0800275TEST_F(DumpsysTest, ListHwServices) {
276 ExpectListHardwareServices({"Locksmith", "Valet"});
277
278 CallMain({"--hw"});
279
280 AssertRunningServices({"Locksmith", "Valet"}, "Currently running hardware services:");
281}
282
Felipe Leme343175a2016-08-02 18:57:37 -0700283// Tests 'dumpsys -l' when all services are running
284TEST_F(DumpsysTest, ListAllServices) {
285 ExpectListServices({"Locksmith", "Valet"});
286 ExpectCheckService("Locksmith");
287 ExpectCheckService("Valet");
288
289 CallMain({"-l"});
290
291 AssertRunningServices({"Locksmith", "Valet"});
292}
293
294// 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
306// Tests 'dumpsys service_name' on a service is running
307TEST_F(DumpsysTest, DumpRunningService) {
308 ExpectDump("Valet", "Here's your car");
309
310 CallMain({"Valet"});
311
312 AssertOutput("Here's your car");
313}
314
315// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
316TEST_F(DumpsysTest, DumpRunningServiceTimeout) {
317 ExpectDumpAndHang("Valet", 2, "Here's your car");
318
319 CallMain({"-t", "1", "Valet"});
320
321 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1s) EXPIRED");
322 AssertNotDumped("Here's your car");
323
324 // Must wait so binder mock is deleted, otherwise test will fail with a leaked object
325 sleep(1);
326}
327
328// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
329TEST_F(DumpsysTest, DumpWithArgsRunningService) {
330 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
331
332 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
333
334 AssertOutput("I DO!");
335}
336
337// Tests 'dumpsys' with no arguments
338TEST_F(DumpsysTest, DumpMultipleServices) {
339 ExpectListServices({"running1", "stopped2", "running3"});
340 ExpectDump("running1", "dump1");
341 ExpectCheckService("stopped2", false);
342 ExpectDump("running3", "dump3");
343
344 CallMain({});
345
346 AssertRunningServices({"running1", "running3"});
347 AssertDumped("running1", "dump1");
348 AssertStopped("stopped2");
349 AssertDumped("running3", "dump3");
350}
351
352// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
353TEST_F(DumpsysTest, DumpWithSkip) {
354 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
355 ExpectDump("running1", "dump1");
356 ExpectCheckService("stopped2", false);
357 ExpectDump("skipped3", "dump3");
358 ExpectDump("running4", "dump4");
359 ExpectDump("skipped5", "dump5");
360
361 CallMain({"--skip", "skipped3", "skipped5"});
362
363 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
364 AssertDumped("running1", "dump1");
365 AssertDumped("running4", "dump4");
366 AssertStopped("stopped2");
367 AssertNotDumped("dump3");
368 AssertNotDumped("dump5");
369}