blob: cfd53e578c3e1bd56fb725699c7812e38988e033 [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&));
53 MOCK_METHOD3(addService, status_t(const String16&, const sp<IBinder>&, bool));
54 MOCK_METHOD0(listServices, Vector<String16>());
55
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;
99 for (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 }
134 EXPECT_CALL(sm_, listServices()).WillRepeatedly(Return(services16));
135 }
136
137 sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
138 sp<BinderMock> binder_mock;
139 if (running) {
140 binder_mock = new BinderMock;
141 }
142 EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
143 return binder_mock;
144 }
145
146 void ExpectDump(const char* name, const std::string& output) {
147 sp<BinderMock> binder_mock = ExpectCheckService(name);
148 EXPECT_CALL(*binder_mock, dump(_, _))
149 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
150 }
151
152 void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
153 const std::string& output) {
154 sp<BinderMock> binder_mock = ExpectCheckService(name);
155 EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
156 .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
157 }
158
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700159 sp<BinderMock> ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
Felipe Leme343175a2016-08-02 18:57:37 -0700160 sp<BinderMock> binder_mock = ExpectCheckService(name);
161 EXPECT_CALL(*binder_mock, dump(_, _))
162 .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700163 return binder_mock;
Felipe Leme343175a2016-08-02 18:57:37 -0700164 }
165
166 void CallMain(const std::vector<std::string>& args) {
167 const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
168 int argc = (int)args.size() + 1;
169 int i = 1;
170 for (const std::string& arg : args) {
171 argv[i++] = arg.c_str();
172 }
173 CaptureStdout();
174 CaptureStderr();
175 int status = dump_.main(argc, const_cast<char**>(argv));
176 stdout_ = GetCapturedStdout();
177 stderr_ = GetCapturedStderr();
178 EXPECT_THAT(status, Eq(0));
179 }
180
Steven Moreland2c3cd832017-02-13 23:44:17 +0000181 void AssertRunningServices(const std::vector<std::string>& services) {
182 std::string expected("Currently running services:\n");
Felipe Leme343175a2016-08-02 18:57:37 -0700183 for (const std::string& service : services) {
184 expected.append(" ").append(service).append("\n");
185 }
186 EXPECT_THAT(stdout_, HasSubstr(expected));
187 }
188
189 void AssertOutput(const std::string& expected) {
190 EXPECT_THAT(stdout_, StrEq(expected));
191 }
192
193 void AssertOutputContains(const std::string& expected) {
194 EXPECT_THAT(stdout_, HasSubstr(expected));
195 }
196
197 void AssertDumped(const std::string& service, const std::string& dump) {
198 EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
199 }
200
201 void AssertNotDumped(const std::string& dump) {
202 EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
203 }
204
205 void AssertStopped(const std::string& service) {
206 EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
207 }
208
209 ServiceManagerMock sm_;
210 Dumpsys dump_;
211
212 private:
213 std::string stdout_, stderr_;
214};
215
Felipe Leme343175a2016-08-02 18:57:37 -0700216// Tests 'dumpsys -l' when all services are running
217TEST_F(DumpsysTest, ListAllServices) {
218 ExpectListServices({"Locksmith", "Valet"});
219 ExpectCheckService("Locksmith");
220 ExpectCheckService("Valet");
221
222 CallMain({"-l"});
223
224 AssertRunningServices({"Locksmith", "Valet"});
225}
226
227// Tests 'dumpsys -l' when a service is not running
228TEST_F(DumpsysTest, ListRunningServices) {
229 ExpectListServices({"Locksmith", "Valet"});
230 ExpectCheckService("Locksmith");
231 ExpectCheckService("Valet", false);
232
233 CallMain({"-l"});
234
235 AssertRunningServices({"Locksmith"});
236 AssertNotDumped({"Valet"});
237}
238
239// Tests 'dumpsys service_name' on a service is running
240TEST_F(DumpsysTest, DumpRunningService) {
241 ExpectDump("Valet", "Here's your car");
242
243 CallMain({"Valet"});
244
245 AssertOutput("Here's your car");
246}
247
248// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
249TEST_F(DumpsysTest, DumpRunningServiceTimeout) {
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700250 sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
Felipe Leme343175a2016-08-02 18:57:37 -0700251
252 CallMain({"-t", "1", "Valet"});
253
254 AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1s) EXPIRED");
255 AssertNotDumped("Here's your car");
256
Felipe Leme5c8a98f2017-08-25 13:39:04 -0700257 // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
258 Mock::AllowLeak(binder_mock.get());
Felipe Leme343175a2016-08-02 18:57:37 -0700259}
260
261// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
262TEST_F(DumpsysTest, DumpWithArgsRunningService) {
263 ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
264
265 CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
266
267 AssertOutput("I DO!");
268}
269
270// Tests 'dumpsys' with no arguments
271TEST_F(DumpsysTest, DumpMultipleServices) {
272 ExpectListServices({"running1", "stopped2", "running3"});
273 ExpectDump("running1", "dump1");
274 ExpectCheckService("stopped2", false);
275 ExpectDump("running3", "dump3");
276
277 CallMain({});
278
279 AssertRunningServices({"running1", "running3"});
280 AssertDumped("running1", "dump1");
281 AssertStopped("stopped2");
282 AssertDumped("running3", "dump3");
283}
284
285// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
286TEST_F(DumpsysTest, DumpWithSkip) {
287 ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
288 ExpectDump("running1", "dump1");
289 ExpectCheckService("stopped2", false);
290 ExpectDump("skipped3", "dump3");
291 ExpectDump("running4", "dump4");
292 ExpectDump("skipped5", "dump5");
293
294 CallMain({"--skip", "skipped3", "skipped5"});
295
296 AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
297 AssertDumped("running1", "dump1");
298 AssertDumped("running4", "dump4");
299 AssertStopped("stopped2");
300 AssertNotDumped("dump3");
301 AssertNotDumped("dump5");
302}