| Felipe Leme | b6e2449 | 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> | 
|  | 26 | #include <utils/Vector.h> | 
|  | 27 |  | 
|  | 28 | using namespace android; | 
|  | 29 |  | 
|  | 30 | using ::testing::_; | 
|  | 31 | using ::testing::Action; | 
|  | 32 | using ::testing::ActionInterface; | 
|  | 33 | using ::testing::DoAll; | 
|  | 34 | using ::testing::Eq; | 
|  | 35 | using ::testing::HasSubstr; | 
|  | 36 | using ::testing::MakeAction; | 
|  | 37 | using ::testing::Not; | 
|  | 38 | using ::testing::Return; | 
|  | 39 | using ::testing::StrEq; | 
|  | 40 | using ::testing::Test; | 
|  | 41 | using ::testing::WithArg; | 
|  | 42 | using ::testing::internal::CaptureStderr; | 
|  | 43 | using ::testing::internal::CaptureStdout; | 
|  | 44 | using ::testing::internal::GetCapturedStderr; | 
|  | 45 | using ::testing::internal::GetCapturedStdout; | 
|  | 46 |  | 
|  | 47 | class ServiceManagerMock : public IServiceManager { | 
|  | 48 | public: | 
|  | 49 | MOCK_CONST_METHOD1(getService, sp<IBinder>(const String16&)); | 
|  | 50 | MOCK_CONST_METHOD1(checkService, sp<IBinder>(const String16&)); | 
|  | 51 | MOCK_METHOD3(addService, status_t(const String16&, const sp<IBinder>&, bool)); | 
|  | 52 | MOCK_METHOD0(listServices, Vector<String16>()); | 
|  | 53 |  | 
|  | 54 | protected: | 
|  | 55 | MOCK_METHOD0(onAsBinder, IBinder*()); | 
|  | 56 | }; | 
|  | 57 |  | 
|  | 58 | class BinderMock : public BBinder { | 
|  | 59 | public: | 
|  | 60 | BinderMock() { | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | MOCK_METHOD2(dump, status_t(int, const Vector<String16>&)); | 
|  | 64 | }; | 
|  | 65 |  | 
|  | 66 | // gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher | 
|  | 67 | typedef void WriteOnFdFunction(int); | 
|  | 68 |  | 
|  | 69 | class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> { | 
|  | 70 | public: | 
|  | 71 | explicit WriteOnFdAction(const std::string& output) : output_(output) { | 
|  | 72 | } | 
|  | 73 | virtual Result Perform(const ArgumentTuple& args) { | 
|  | 74 | int fd = ::std::tr1::get<0>(args); | 
|  | 75 | android::base::WriteStringToFd(output_, fd); | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | private: | 
|  | 79 | std::string output_; | 
|  | 80 | }; | 
|  | 81 |  | 
|  | 82 | // Matcher used to emulate dump() by writing on its file descriptor. | 
|  | 83 | Action<WriteOnFdFunction> WriteOnFd(const std::string& output) { | 
|  | 84 | return MakeAction(new WriteOnFdAction(output)); | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | // Matcher for args using Android's Vector<String16> format | 
|  | 88 | // TODO: move it to some common testing library | 
|  | 89 | MATCHER_P(AndroidElementsAre, expected, "") { | 
|  | 90 | std::ostringstream errors; | 
|  | 91 | if (arg.size() != expected.size()) { | 
|  | 92 | errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size() | 
|  | 93 | << ")\n"; | 
|  | 94 | } | 
|  | 95 | int i = 0; | 
|  | 96 | std::ostringstream actual_stream, expected_stream; | 
|  | 97 | for (String16 actual : arg) { | 
|  | 98 | std::string actual_str = String16::std_string(actual); | 
|  | 99 | std::string expected_str = expected[i]; | 
|  | 100 | actual_stream << "'" << actual_str << "' "; | 
|  | 101 | expected_stream << "'" << expected_str << "' "; | 
|  | 102 | if (actual_str != expected_str) { | 
|  | 103 | errors << " element mismatch at index " << i << "\n"; | 
|  | 104 | } | 
|  | 105 | i++; | 
|  | 106 | } | 
|  | 107 |  | 
|  | 108 | if (!errors.str().empty()) { | 
|  | 109 | errors << "\nExpected args: " << expected_stream.str() | 
|  | 110 | << "\nActual args: " << actual_stream.str(); | 
|  | 111 | *result_listener << errors.str(); | 
|  | 112 | return false; | 
|  | 113 | } | 
|  | 114 | return true; | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | // Custom action to sleep for timeout seconds | 
|  | 118 | ACTION_P(Sleep, timeout) { | 
|  | 119 | sleep(timeout); | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | class DumpsysTest : public Test { | 
|  | 123 | public: | 
|  | 124 | DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() { | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | void ExpectListServices(std::vector<std::string> services) { | 
|  | 128 | Vector<String16> services16; | 
|  | 129 | for (auto& service : services) { | 
|  | 130 | services16.add(String16(service.c_str())); | 
|  | 131 | } | 
|  | 132 | EXPECT_CALL(sm_, listServices()).WillRepeatedly(Return(services16)); | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | sp<BinderMock> ExpectCheckService(const char* name, bool running = true) { | 
|  | 136 | sp<BinderMock> binder_mock; | 
|  | 137 | if (running) { | 
|  | 138 | binder_mock = new BinderMock; | 
|  | 139 | } | 
|  | 140 | EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock)); | 
|  | 141 | return binder_mock; | 
|  | 142 | } | 
|  | 143 |  | 
|  | 144 | void ExpectDump(const char* name, const std::string& output) { | 
|  | 145 | sp<BinderMock> binder_mock = ExpectCheckService(name); | 
|  | 146 | EXPECT_CALL(*binder_mock, dump(_, _)) | 
|  | 147 | .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0))); | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | void ExpectDumpWithArgs(const char* name, std::vector<std::string> args, | 
|  | 151 | const std::string& output) { | 
|  | 152 | sp<BinderMock> binder_mock = ExpectCheckService(name); | 
|  | 153 | EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args))) | 
|  | 154 | .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0))); | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | void ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) { | 
|  | 158 | sp<BinderMock> binder_mock = ExpectCheckService(name); | 
|  | 159 | EXPECT_CALL(*binder_mock, dump(_, _)) | 
|  | 160 | .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0))); | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | void CallMain(const std::vector<std::string>& args) { | 
|  | 164 | const char* argv[1024] = {"/some/virtual/dir/dumpsys"}; | 
|  | 165 | int argc = (int)args.size() + 1; | 
|  | 166 | int i = 1; | 
|  | 167 | for (const std::string& arg : args) { | 
|  | 168 | argv[i++] = arg.c_str(); | 
|  | 169 | } | 
|  | 170 | CaptureStdout(); | 
|  | 171 | CaptureStderr(); | 
|  | 172 | int status = dump_.main(argc, const_cast<char**>(argv)); | 
|  | 173 | stdout_ = GetCapturedStdout(); | 
|  | 174 | stderr_ = GetCapturedStderr(); | 
|  | 175 | EXPECT_THAT(status, Eq(0)); | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | void AssertRunningServices(const std::vector<std::string>& services) { | 
|  | 179 | std::string expected("Currently running services:\n"); | 
|  | 180 | for (const std::string& service : services) { | 
|  | 181 | expected.append("  ").append(service).append("\n"); | 
|  | 182 | } | 
|  | 183 | EXPECT_THAT(stdout_, HasSubstr(expected)); | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | void AssertOutput(const std::string& expected) { | 
|  | 187 | EXPECT_THAT(stdout_, StrEq(expected)); | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | void AssertOutputContains(const std::string& expected) { | 
|  | 191 | EXPECT_THAT(stdout_, HasSubstr(expected)); | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | void AssertDumped(const std::string& service, const std::string& dump) { | 
|  | 195 | EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump)); | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | void AssertNotDumped(const std::string& dump) { | 
|  | 199 | EXPECT_THAT(stdout_, Not(HasSubstr(dump))); | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | void AssertStopped(const std::string& service) { | 
|  | 203 | EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n")); | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | ServiceManagerMock sm_; | 
|  | 207 | Dumpsys dump_; | 
|  | 208 |  | 
|  | 209 | private: | 
|  | 210 | std::string stdout_, stderr_; | 
|  | 211 | }; | 
|  | 212 |  | 
|  | 213 | // Tests 'dumpsys -l' when all services are running | 
|  | 214 | TEST_F(DumpsysTest, ListAllServices) { | 
|  | 215 | ExpectListServices({"Locksmith", "Valet"}); | 
|  | 216 | ExpectCheckService("Locksmith"); | 
|  | 217 | ExpectCheckService("Valet"); | 
|  | 218 |  | 
|  | 219 | CallMain({"-l"}); | 
|  | 220 |  | 
|  | 221 | AssertRunningServices({"Locksmith", "Valet"}); | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 | // Tests 'dumpsys -l' when a service is not running | 
|  | 225 | TEST_F(DumpsysTest, ListRunningServices) { | 
|  | 226 | ExpectListServices({"Locksmith", "Valet"}); | 
|  | 227 | ExpectCheckService("Locksmith"); | 
|  | 228 | ExpectCheckService("Valet", false); | 
|  | 229 |  | 
|  | 230 | CallMain({"-l"}); | 
|  | 231 |  | 
|  | 232 | AssertRunningServices({"Locksmith"}); | 
|  | 233 | AssertNotDumped({"Valet"}); | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | // Tests 'dumpsys service_name' on a service is running | 
|  | 237 | TEST_F(DumpsysTest, DumpRunningService) { | 
|  | 238 | ExpectDump("Valet", "Here's your car"); | 
|  | 239 |  | 
|  | 240 | CallMain({"Valet"}); | 
|  | 241 |  | 
|  | 242 | AssertOutput("Here's your car"); | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | // Tests 'dumpsys -t 1 service_name' on a service that times out after 2s | 
|  | 246 | TEST_F(DumpsysTest, DumpRunningServiceTimeout) { | 
|  | 247 | ExpectDumpAndHang("Valet", 2, "Here's your car"); | 
|  | 248 |  | 
|  | 249 | CallMain({"-t", "1", "Valet"}); | 
|  | 250 |  | 
|  | 251 | AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1s) EXPIRED"); | 
|  | 252 | AssertNotDumped("Here's your car"); | 
|  | 253 |  | 
|  | 254 | // Must wait so binder mock is deleted, otherwise test will fail with a leaked object | 
|  | 255 | sleep(1); | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | // Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running | 
|  | 259 | TEST_F(DumpsysTest, DumpWithArgsRunningService) { | 
|  | 260 | ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!"); | 
|  | 261 |  | 
|  | 262 | CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"}); | 
|  | 263 |  | 
|  | 264 | AssertOutput("I DO!"); | 
|  | 265 | } | 
|  | 266 |  | 
|  | 267 | // Tests 'dumpsys' with no arguments | 
|  | 268 | TEST_F(DumpsysTest, DumpMultipleServices) { | 
|  | 269 | ExpectListServices({"running1", "stopped2", "running3"}); | 
|  | 270 | ExpectDump("running1", "dump1"); | 
|  | 271 | ExpectCheckService("stopped2", false); | 
|  | 272 | ExpectDump("running3", "dump3"); | 
|  | 273 |  | 
|  | 274 | CallMain({}); | 
|  | 275 |  | 
|  | 276 | AssertRunningServices({"running1", "running3"}); | 
|  | 277 | AssertDumped("running1", "dump1"); | 
|  | 278 | AssertStopped("stopped2"); | 
|  | 279 | AssertDumped("running3", "dump3"); | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | // Tests 'dumpsys --skip skipped3 skipped5', which should skip these services | 
|  | 283 | TEST_F(DumpsysTest, DumpWithSkip) { | 
|  | 284 | ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"}); | 
|  | 285 | ExpectDump("running1", "dump1"); | 
|  | 286 | ExpectCheckService("stopped2", false); | 
|  | 287 | ExpectDump("skipped3", "dump3"); | 
|  | 288 | ExpectDump("running4", "dump4"); | 
|  | 289 | ExpectDump("skipped5", "dump5"); | 
|  | 290 |  | 
|  | 291 | CallMain({"--skip", "skipped3", "skipped5"}); | 
|  | 292 |  | 
|  | 293 | AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"}); | 
|  | 294 | AssertDumped("running1", "dump1"); | 
|  | 295 | AssertDumped("running4", "dump4"); | 
|  | 296 | AssertStopped("stopped2"); | 
|  | 297 | AssertNotDumped("dump3"); | 
|  | 298 | AssertNotDumped("dump5"); | 
|  | 299 | } |