| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Moreland | a0f7f2d | 2017-03-09 22:59:32 -0800 | [diff] [blame] | 26 | #include <utils/String8.h> | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 27 | #include <utils/Vector.h> | 
|  | 28 |  | 
|  | 29 | using namespace android; | 
|  | 30 |  | 
|  | 31 | using ::testing::_; | 
|  | 32 | using ::testing::Action; | 
|  | 33 | using ::testing::ActionInterface; | 
|  | 34 | using ::testing::DoAll; | 
|  | 35 | using ::testing::Eq; | 
|  | 36 | using ::testing::HasSubstr; | 
|  | 37 | using ::testing::MakeAction; | 
| Felipe Leme | 5c8a98f | 2017-08-25 13:39:04 -0700 | [diff] [blame] | 38 | using ::testing::Mock; | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 39 | using ::testing::Not; | 
|  | 40 | using ::testing::Return; | 
|  | 41 | using ::testing::StrEq; | 
|  | 42 | using ::testing::Test; | 
|  | 43 | using ::testing::WithArg; | 
|  | 44 | using ::testing::internal::CaptureStderr; | 
|  | 45 | using ::testing::internal::CaptureStdout; | 
|  | 46 | using ::testing::internal::GetCapturedStderr; | 
|  | 47 | using ::testing::internal::GetCapturedStdout; | 
|  | 48 |  | 
|  | 49 | class 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 |  | 
|  | 60 | class 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 | 
|  | 69 | typedef void WriteOnFdFunction(int); | 
|  | 70 |  | 
|  | 71 | class 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. | 
|  | 85 | Action<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 | 
|  | 91 | MATCHER_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 Moreland | a0f7f2d | 2017-03-09 22:59:32 -0800 | [diff] [blame] | 100 | std::string actual_str = String8(actual).c_str(); | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 101 | 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 | 
|  | 120 | ACTION_P(Sleep, timeout) { | 
|  | 121 | sleep(timeout); | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | class DumpsysTest : public Test { | 
|  | 125 | public: | 
| Steven Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 126 | DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() { | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 127 | } | 
|  | 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 Leme | 5c8a98f | 2017-08-25 13:39:04 -0700 | [diff] [blame] | 159 | sp<BinderMock> ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) { | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 160 | 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 Leme | 5c8a98f | 2017-08-25 13:39:04 -0700 | [diff] [blame] | 163 | return binder_mock; | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 164 | } | 
|  | 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 Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 181 | void AssertRunningServices(const std::vector<std::string>& services) { | 
|  | 182 | std::string expected("Currently running services:\n"); | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 183 | 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 Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 216 | // Tests 'dumpsys -l' when all services are running | 
|  | 217 | TEST_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 | 
|  | 228 | TEST_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 | 
|  | 240 | TEST_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 | 
|  | 249 | TEST_F(DumpsysTest, DumpRunningServiceTimeout) { | 
| Felipe Leme | 5c8a98f | 2017-08-25 13:39:04 -0700 | [diff] [blame] | 250 | sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car"); | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 251 |  | 
|  | 252 | CallMain({"-t", "1", "Valet"}); | 
|  | 253 |  | 
|  | 254 | AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1s) EXPIRED"); | 
|  | 255 | AssertNotDumped("Here's your car"); | 
|  | 256 |  | 
| Felipe Leme | 5c8a98f | 2017-08-25 13:39:04 -0700 | [diff] [blame] | 257 | // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp | 
|  | 258 | Mock::AllowLeak(binder_mock.get()); | 
| Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 259 | } | 
|  | 260 |  | 
|  | 261 | // Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running | 
|  | 262 | TEST_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 | 
|  | 271 | TEST_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 | 
|  | 286 | TEST_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 | } |