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> |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 25 | #include <serviceutils/PriorityDumper.h> |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 26 | #include <utils/String16.h> |
Steven Moreland | a0f7f2d | 2017-03-09 22:59:32 -0800 | [diff] [blame] | 27 | #include <utils/String8.h> |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 28 | #include <utils/Vector.h> |
| 29 | |
| 30 | using namespace android; |
| 31 | |
| 32 | using ::testing::_; |
| 33 | using ::testing::Action; |
| 34 | using ::testing::ActionInterface; |
| 35 | using ::testing::DoAll; |
| 36 | using ::testing::Eq; |
| 37 | using ::testing::HasSubstr; |
| 38 | using ::testing::MakeAction; |
Felipe Leme | 5c8a98f | 2017-08-25 13:39:04 -0700 | [diff] [blame] | 39 | using ::testing::Mock; |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 40 | using ::testing::Not; |
| 41 | using ::testing::Return; |
| 42 | using ::testing::StrEq; |
| 43 | using ::testing::Test; |
| 44 | using ::testing::WithArg; |
| 45 | using ::testing::internal::CaptureStderr; |
| 46 | using ::testing::internal::CaptureStdout; |
| 47 | using ::testing::internal::GetCapturedStderr; |
| 48 | using ::testing::internal::GetCapturedStdout; |
| 49 | |
| 50 | class ServiceManagerMock : public IServiceManager { |
| 51 | public: |
| 52 | MOCK_CONST_METHOD1(getService, sp<IBinder>(const String16&)); |
| 53 | MOCK_CONST_METHOD1(checkService, sp<IBinder>(const String16&)); |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 54 | MOCK_METHOD4(addService, status_t(const String16&, const sp<IBinder>&, bool, int)); |
| 55 | MOCK_METHOD1(listServices, Vector<String16>(int)); |
Steven Moreland | 1c47b58 | 2019-08-27 18:05:27 -0700 | [diff] [blame] | 56 | MOCK_METHOD1(waitForService, sp<IBinder>(const String16&)); |
Steven Moreland | b82b8f8 | 2019-10-28 10:52:34 -0700 | [diff] [blame] | 57 | MOCK_METHOD1(isDeclared, bool(const String16&)); |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 58 | protected: |
| 59 | MOCK_METHOD0(onAsBinder, IBinder*()); |
| 60 | }; |
| 61 | |
| 62 | class BinderMock : public BBinder { |
| 63 | public: |
| 64 | BinderMock() { |
| 65 | } |
| 66 | |
| 67 | MOCK_METHOD2(dump, status_t(int, const Vector<String16>&)); |
| 68 | }; |
| 69 | |
| 70 | // gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher |
| 71 | typedef void WriteOnFdFunction(int); |
| 72 | |
| 73 | class WriteOnFdAction : public ActionInterface<WriteOnFdFunction> { |
| 74 | public: |
| 75 | explicit WriteOnFdAction(const std::string& output) : output_(output) { |
| 76 | } |
| 77 | virtual Result Perform(const ArgumentTuple& args) { |
Haibo Huang | 21f3655 | 2018-07-10 19:48:18 -0700 | [diff] [blame] | 78 | int fd = ::testing::get<0>(args); |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 79 | android::base::WriteStringToFd(output_, fd); |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | std::string output_; |
| 84 | }; |
| 85 | |
| 86 | // Matcher used to emulate dump() by writing on its file descriptor. |
| 87 | Action<WriteOnFdFunction> WriteOnFd(const std::string& output) { |
| 88 | return MakeAction(new WriteOnFdAction(output)); |
| 89 | } |
| 90 | |
| 91 | // Matcher for args using Android's Vector<String16> format |
| 92 | // TODO: move it to some common testing library |
| 93 | MATCHER_P(AndroidElementsAre, expected, "") { |
| 94 | std::ostringstream errors; |
| 95 | if (arg.size() != expected.size()) { |
| 96 | errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size() |
| 97 | << ")\n"; |
| 98 | } |
| 99 | int i = 0; |
| 100 | std::ostringstream actual_stream, expected_stream; |
Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 101 | for (const String16& actual : arg) { |
Steven Moreland | a0f7f2d | 2017-03-09 22:59:32 -0800 | [diff] [blame] | 102 | std::string actual_str = String8(actual).c_str(); |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 103 | std::string expected_str = expected[i]; |
| 104 | actual_stream << "'" << actual_str << "' "; |
| 105 | expected_stream << "'" << expected_str << "' "; |
| 106 | if (actual_str != expected_str) { |
| 107 | errors << " element mismatch at index " << i << "\n"; |
| 108 | } |
| 109 | i++; |
| 110 | } |
| 111 | |
| 112 | if (!errors.str().empty()) { |
| 113 | errors << "\nExpected args: " << expected_stream.str() |
| 114 | << "\nActual args: " << actual_stream.str(); |
| 115 | *result_listener << errors.str(); |
| 116 | return false; |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | // Custom action to sleep for timeout seconds |
| 122 | ACTION_P(Sleep, timeout) { |
| 123 | sleep(timeout); |
| 124 | } |
| 125 | |
| 126 | class DumpsysTest : public Test { |
| 127 | public: |
Steven Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 128 | DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() { |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void ExpectListServices(std::vector<std::string> services) { |
| 132 | Vector<String16> services16; |
| 133 | for (auto& service : services) { |
| 134 | services16.add(String16(service.c_str())); |
| 135 | } |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 136 | EXPECT_CALL(sm_, listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL)) |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 137 | .WillRepeatedly(Return(services16)); |
| 138 | } |
| 139 | |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 140 | void ExpectListServicesWithPriority(std::vector<std::string> services, int dumpFlags) { |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 141 | Vector<String16> services16; |
| 142 | for (auto& service : services) { |
| 143 | services16.add(String16(service.c_str())); |
| 144 | } |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 145 | EXPECT_CALL(sm_, listServices(dumpFlags)).WillRepeatedly(Return(services16)); |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | sp<BinderMock> ExpectCheckService(const char* name, bool running = true) { |
| 149 | sp<BinderMock> binder_mock; |
| 150 | if (running) { |
| 151 | binder_mock = new BinderMock; |
| 152 | } |
| 153 | EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock)); |
| 154 | return binder_mock; |
| 155 | } |
| 156 | |
| 157 | void ExpectDump(const char* name, const std::string& output) { |
| 158 | sp<BinderMock> binder_mock = ExpectCheckService(name); |
| 159 | EXPECT_CALL(*binder_mock, dump(_, _)) |
| 160 | .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0))); |
| 161 | } |
| 162 | |
| 163 | void ExpectDumpWithArgs(const char* name, std::vector<std::string> args, |
| 164 | const std::string& output) { |
| 165 | sp<BinderMock> binder_mock = ExpectCheckService(name); |
| 166 | EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args))) |
| 167 | .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0))); |
| 168 | } |
| 169 | |
Felipe Leme | 5c8a98f | 2017-08-25 13:39:04 -0700 | [diff] [blame] | 170 | 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] | 171 | sp<BinderMock> binder_mock = ExpectCheckService(name); |
| 172 | EXPECT_CALL(*binder_mock, dump(_, _)) |
| 173 | .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0))); |
Felipe Leme | 5c8a98f | 2017-08-25 13:39:04 -0700 | [diff] [blame] | 174 | return binder_mock; |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void CallMain(const std::vector<std::string>& args) { |
| 178 | const char* argv[1024] = {"/some/virtual/dir/dumpsys"}; |
| 179 | int argc = (int)args.size() + 1; |
| 180 | int i = 1; |
| 181 | for (const std::string& arg : args) { |
| 182 | argv[i++] = arg.c_str(); |
| 183 | } |
| 184 | CaptureStdout(); |
| 185 | CaptureStderr(); |
| 186 | int status = dump_.main(argc, const_cast<char**>(argv)); |
| 187 | stdout_ = GetCapturedStdout(); |
| 188 | stderr_ = GetCapturedStderr(); |
| 189 | EXPECT_THAT(status, Eq(0)); |
| 190 | } |
| 191 | |
Steven Moreland | a6ddb9a | 2019-09-27 16:41:02 +0000 | [diff] [blame] | 192 | void CallSingleService(const String16& serviceName, Vector<String16>& args, int priorityFlags, |
| 193 | bool supportsProto, std::chrono::duration<double>& elapsedDuration, |
| 194 | size_t& bytesWritten) { |
| 195 | CaptureStdout(); |
| 196 | CaptureStderr(); |
| 197 | dump_.setServiceArgs(args, supportsProto, priorityFlags); |
Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 198 | status_t status = dump_.startDumpThread(Dumpsys::Type::DUMP, serviceName, args); |
Steven Moreland | a6ddb9a | 2019-09-27 16:41:02 +0000 | [diff] [blame] | 199 | EXPECT_THAT(status, Eq(0)); |
| 200 | status = dump_.writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(500), false, |
| 201 | elapsedDuration, bytesWritten); |
| 202 | EXPECT_THAT(status, Eq(0)); |
| 203 | dump_.stopDumpThread(/* dumpCompleted = */ true); |
| 204 | stdout_ = GetCapturedStdout(); |
| 205 | stderr_ = GetCapturedStderr(); |
| 206 | } |
| 207 | |
Steven Moreland | 2c3cd83 | 2017-02-13 23:44:17 +0000 | [diff] [blame] | 208 | void AssertRunningServices(const std::vector<std::string>& services) { |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 209 | std::string expected; |
| 210 | if (services.size() > 1) { |
| 211 | expected.append("Currently running services:\n"); |
| 212 | } |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 213 | for (const std::string& service : services) { |
| 214 | expected.append(" ").append(service).append("\n"); |
| 215 | } |
| 216 | EXPECT_THAT(stdout_, HasSubstr(expected)); |
| 217 | } |
| 218 | |
Steven Moreland | a6ddb9a | 2019-09-27 16:41:02 +0000 | [diff] [blame] | 219 | void AssertOutput(const std::string& expected) { |
| 220 | EXPECT_THAT(stdout_, StrEq(expected)); |
| 221 | } |
| 222 | |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 223 | void AssertOutputContains(const std::string& expected) { |
| 224 | EXPECT_THAT(stdout_, HasSubstr(expected)); |
| 225 | } |
| 226 | |
| 227 | void AssertDumped(const std::string& service, const std::string& dump) { |
Steven Moreland | a6ddb9a | 2019-09-27 16:41:02 +0000 | [diff] [blame] | 228 | EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump)); |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 229 | EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: ")); |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 232 | void AssertDumpedWithPriority(const std::string& service, const std::string& dump, |
| 233 | const char16_t* priorityType) { |
| 234 | std::string priority = String8(priorityType).c_str(); |
| 235 | EXPECT_THAT(stdout_, |
Steven Moreland | a6ddb9a | 2019-09-27 16:41:02 +0000 | [diff] [blame] | 236 | HasSubstr("DUMP OF SERVICE " + priority + " " + service + ":\n" + dump)); |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 237 | EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: ")); |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 240 | void AssertNotDumped(const std::string& dump) { |
| 241 | EXPECT_THAT(stdout_, Not(HasSubstr(dump))); |
| 242 | } |
| 243 | |
| 244 | void AssertStopped(const std::string& service) { |
| 245 | EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n")); |
| 246 | } |
| 247 | |
| 248 | ServiceManagerMock sm_; |
| 249 | Dumpsys dump_; |
| 250 | |
| 251 | private: |
| 252 | std::string stdout_, stderr_; |
| 253 | }; |
| 254 | |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 255 | // Tests 'dumpsys -l' when all services are running |
| 256 | TEST_F(DumpsysTest, ListAllServices) { |
| 257 | ExpectListServices({"Locksmith", "Valet"}); |
| 258 | ExpectCheckService("Locksmith"); |
| 259 | ExpectCheckService("Valet"); |
| 260 | |
| 261 | CallMain({"-l"}); |
| 262 | |
| 263 | AssertRunningServices({"Locksmith", "Valet"}); |
| 264 | } |
| 265 | |
| 266 | // Tests 'dumpsys -l' when a service is not running |
| 267 | TEST_F(DumpsysTest, ListRunningServices) { |
| 268 | ExpectListServices({"Locksmith", "Valet"}); |
| 269 | ExpectCheckService("Locksmith"); |
| 270 | ExpectCheckService("Valet", false); |
| 271 | |
| 272 | CallMain({"-l"}); |
| 273 | |
| 274 | AssertRunningServices({"Locksmith"}); |
| 275 | AssertNotDumped({"Valet"}); |
| 276 | } |
| 277 | |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 278 | // Tests 'dumpsys -l --priority HIGH' |
| 279 | TEST_F(DumpsysTest, ListAllServicesWithPriority) { |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 280 | ExpectListServicesWithPriority({"Locksmith", "Valet"}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH); |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 281 | ExpectCheckService("Locksmith"); |
| 282 | ExpectCheckService("Valet"); |
| 283 | |
| 284 | CallMain({"-l", "--priority", "HIGH"}); |
| 285 | |
| 286 | AssertRunningServices({"Locksmith", "Valet"}); |
| 287 | } |
| 288 | |
| 289 | // Tests 'dumpsys -l --priority HIGH' with and empty list |
| 290 | TEST_F(DumpsysTest, ListEmptyServicesWithPriority) { |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 291 | ExpectListServicesWithPriority({}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH); |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 292 | |
| 293 | CallMain({"-l", "--priority", "HIGH"}); |
| 294 | |
| 295 | AssertRunningServices({}); |
| 296 | } |
| 297 | |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 298 | // Tests 'dumpsys -l --proto' |
| 299 | TEST_F(DumpsysTest, ListAllServicesWithProto) { |
| 300 | ExpectListServicesWithPriority({"Locksmith", "Valet", "Car"}, |
| 301 | IServiceManager::DUMP_FLAG_PRIORITY_ALL); |
| 302 | ExpectListServicesWithPriority({"Valet", "Car"}, IServiceManager::DUMP_FLAG_PROTO); |
| 303 | ExpectCheckService("Car"); |
| 304 | ExpectCheckService("Valet"); |
| 305 | |
| 306 | CallMain({"-l", "--proto"}); |
| 307 | |
| 308 | AssertRunningServices({"Car", "Valet"}); |
| 309 | } |
| 310 | |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 311 | // Tests 'dumpsys service_name' on a service is running |
| 312 | TEST_F(DumpsysTest, DumpRunningService) { |
| 313 | ExpectDump("Valet", "Here's your car"); |
| 314 | |
| 315 | CallMain({"Valet"}); |
| 316 | |
Steven Moreland | a6ddb9a | 2019-09-27 16:41:02 +0000 | [diff] [blame] | 317 | AssertOutput("Here's your car"); |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | // Tests 'dumpsys -t 1 service_name' on a service that times out after 2s |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 321 | TEST_F(DumpsysTest, DumpRunningServiceTimeoutInSec) { |
Felipe Leme | 5c8a98f | 2017-08-25 13:39:04 -0700 | [diff] [blame] | 322 | sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car"); |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 323 | |
| 324 | CallMain({"-t", "1", "Valet"}); |
| 325 | |
Vishnu Nair | 6921f80 | 2017-11-22 09:17:23 -0800 | [diff] [blame] | 326 | AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1000ms) EXPIRED"); |
| 327 | AssertNotDumped("Here's your car"); |
| 328 | |
| 329 | // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp |
| 330 | Mock::AllowLeak(binder_mock.get()); |
| 331 | } |
| 332 | |
| 333 | // Tests 'dumpsys -T 500 service_name' on a service that times out after 2s |
| 334 | TEST_F(DumpsysTest, DumpRunningServiceTimeoutInMs) { |
| 335 | sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car"); |
| 336 | |
| 337 | CallMain({"-T", "500", "Valet"}); |
| 338 | |
| 339 | AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (500ms) EXPIRED"); |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 340 | AssertNotDumped("Here's your car"); |
| 341 | |
Felipe Leme | 5c8a98f | 2017-08-25 13:39:04 -0700 | [diff] [blame] | 342 | // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp |
| 343 | Mock::AllowLeak(binder_mock.get()); |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running |
| 347 | TEST_F(DumpsysTest, DumpWithArgsRunningService) { |
| 348 | ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!"); |
| 349 | |
| 350 | CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"}); |
| 351 | |
Steven Moreland | a6ddb9a | 2019-09-27 16:41:02 +0000 | [diff] [blame] | 352 | AssertOutput("I DO!"); |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Vishnu Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 355 | // Tests dumpsys passes the -a flag when called on all services |
| 356 | TEST_F(DumpsysTest, PassAllFlagsToServices) { |
| 357 | ExpectListServices({"Locksmith", "Valet"}); |
| 358 | ExpectCheckService("Locksmith"); |
| 359 | ExpectCheckService("Valet"); |
| 360 | ExpectDumpWithArgs("Locksmith", {"-a"}, "dumped1"); |
| 361 | ExpectDumpWithArgs("Valet", {"-a"}, "dumped2"); |
| 362 | |
| 363 | CallMain({"-T", "500"}); |
| 364 | |
| 365 | AssertDumped("Locksmith", "dumped1"); |
| 366 | AssertDumped("Valet", "dumped2"); |
| 367 | } |
| 368 | |
| 369 | // Tests dumpsys passes the -a flag when called on NORMAL priority services |
| 370 | TEST_F(DumpsysTest, PassAllFlagsToNormalServices) { |
| 371 | ExpectListServicesWithPriority({"Locksmith", "Valet"}, |
| 372 | IServiceManager::DUMP_FLAG_PRIORITY_NORMAL); |
| 373 | ExpectCheckService("Locksmith"); |
| 374 | ExpectCheckService("Valet"); |
Vishnu Nair | 3919e1c | 2018-05-01 17:01:00 -0700 | [diff] [blame] | 375 | ExpectDumpWithArgs("Locksmith", {"--dump-priority", "NORMAL", "-a"}, "dump1"); |
| 376 | ExpectDumpWithArgs("Valet", {"--dump-priority", "NORMAL", "-a"}, "dump2"); |
Vishnu Nair | 64afc02 | 2018-02-01 15:29:34 -0800 | [diff] [blame] | 377 | |
| 378 | CallMain({"--priority", "NORMAL"}); |
| 379 | |
| 380 | AssertDumped("Locksmith", "dump1"); |
| 381 | AssertDumped("Valet", "dump2"); |
| 382 | } |
| 383 | |
| 384 | // Tests dumpsys passes only priority flags when called on CRITICAL priority services |
| 385 | TEST_F(DumpsysTest, PassPriorityFlagsToCriticalServices) { |
| 386 | ExpectListServicesWithPriority({"Locksmith", "Valet"}, |
| 387 | IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL); |
| 388 | ExpectCheckService("Locksmith"); |
| 389 | ExpectCheckService("Valet"); |
| 390 | ExpectDumpWithArgs("Locksmith", {"--dump-priority", "CRITICAL"}, "dump1"); |
| 391 | ExpectDumpWithArgs("Valet", {"--dump-priority", "CRITICAL"}, "dump2"); |
| 392 | |
| 393 | CallMain({"--priority", "CRITICAL"}); |
| 394 | |
| 395 | AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL); |
| 396 | AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL); |
| 397 | } |
| 398 | |
| 399 | // Tests dumpsys passes only priority flags when called on HIGH priority services |
| 400 | TEST_F(DumpsysTest, PassPriorityFlagsToHighServices) { |
| 401 | ExpectListServicesWithPriority({"Locksmith", "Valet"}, |
| 402 | IServiceManager::DUMP_FLAG_PRIORITY_HIGH); |
| 403 | ExpectCheckService("Locksmith"); |
| 404 | ExpectCheckService("Valet"); |
| 405 | ExpectDumpWithArgs("Locksmith", {"--dump-priority", "HIGH"}, "dump1"); |
| 406 | ExpectDumpWithArgs("Valet", {"--dump-priority", "HIGH"}, "dump2"); |
| 407 | |
| 408 | CallMain({"--priority", "HIGH"}); |
| 409 | |
| 410 | AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_HIGH); |
| 411 | AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_HIGH); |
| 412 | } |
| 413 | |
Felipe Leme | 343175a | 2016-08-02 18:57:37 -0700 | [diff] [blame] | 414 | // Tests 'dumpsys' with no arguments |
| 415 | TEST_F(DumpsysTest, DumpMultipleServices) { |
| 416 | ExpectListServices({"running1", "stopped2", "running3"}); |
| 417 | ExpectDump("running1", "dump1"); |
| 418 | ExpectCheckService("stopped2", false); |
| 419 | ExpectDump("running3", "dump3"); |
| 420 | |
| 421 | CallMain({}); |
| 422 | |
| 423 | AssertRunningServices({"running1", "running3"}); |
| 424 | AssertDumped("running1", "dump1"); |
| 425 | AssertStopped("stopped2"); |
| 426 | AssertDumped("running3", "dump3"); |
| 427 | } |
| 428 | |
| 429 | // Tests 'dumpsys --skip skipped3 skipped5', which should skip these services |
| 430 | TEST_F(DumpsysTest, DumpWithSkip) { |
| 431 | ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"}); |
| 432 | ExpectDump("running1", "dump1"); |
| 433 | ExpectCheckService("stopped2", false); |
| 434 | ExpectDump("skipped3", "dump3"); |
| 435 | ExpectDump("running4", "dump4"); |
| 436 | ExpectDump("skipped5", "dump5"); |
| 437 | |
| 438 | CallMain({"--skip", "skipped3", "skipped5"}); |
| 439 | |
| 440 | AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"}); |
| 441 | AssertDumped("running1", "dump1"); |
| 442 | AssertDumped("running4", "dump4"); |
| 443 | AssertStopped("stopped2"); |
| 444 | AssertNotDumped("dump3"); |
| 445 | AssertNotDumped("dump5"); |
| 446 | } |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 447 | |
| 448 | // Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services |
| 449 | TEST_F(DumpsysTest, DumpWithSkipAndPriority) { |
| 450 | ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"}, |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 451 | IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL); |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 452 | ExpectDump("running1", "dump1"); |
| 453 | ExpectCheckService("stopped2", false); |
| 454 | ExpectDump("skipped3", "dump3"); |
| 455 | ExpectDump("running4", "dump4"); |
| 456 | ExpectDump("skipped5", "dump5"); |
| 457 | |
| 458 | CallMain({"--priority", "CRITICAL", "--skip", "skipped3", "skipped5"}); |
| 459 | |
| 460 | AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"}); |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 461 | AssertDumpedWithPriority("running1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL); |
| 462 | AssertDumpedWithPriority("running4", "dump4", PriorityDumper::PRIORITY_ARG_CRITICAL); |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 463 | AssertStopped("stopped2"); |
| 464 | AssertNotDumped("dump3"); |
| 465 | AssertNotDumped("dump5"); |
| 466 | } |
| 467 | |
| 468 | // Tests 'dumpsys --priority CRITICAL' |
| 469 | TEST_F(DumpsysTest, DumpWithPriorityCritical) { |
| 470 | ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"}, |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 471 | IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL); |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 472 | ExpectDump("runningcritical1", "dump1"); |
| 473 | ExpectDump("runningcritical2", "dump2"); |
| 474 | |
| 475 | CallMain({"--priority", "CRITICAL"}); |
| 476 | |
| 477 | AssertRunningServices({"runningcritical1", "runningcritical2"}); |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 478 | AssertDumpedWithPriority("runningcritical1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL); |
| 479 | AssertDumpedWithPriority("runningcritical2", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL); |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | // Tests 'dumpsys --priority HIGH' |
| 483 | TEST_F(DumpsysTest, DumpWithPriorityHigh) { |
| 484 | ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"}, |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 485 | IServiceManager::DUMP_FLAG_PRIORITY_HIGH); |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 486 | ExpectDump("runninghigh1", "dump1"); |
| 487 | ExpectDump("runninghigh2", "dump2"); |
| 488 | |
| 489 | CallMain({"--priority", "HIGH"}); |
| 490 | |
| 491 | AssertRunningServices({"runninghigh1", "runninghigh2"}); |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 492 | AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH); |
| 493 | AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH); |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // Tests 'dumpsys --priority NORMAL' |
| 497 | TEST_F(DumpsysTest, DumpWithPriorityNormal) { |
| 498 | ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"}, |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 499 | IServiceManager::DUMP_FLAG_PRIORITY_NORMAL); |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 500 | ExpectDump("runningnormal1", "dump1"); |
| 501 | ExpectDump("runningnormal2", "dump2"); |
| 502 | |
| 503 | CallMain({"--priority", "NORMAL"}); |
| 504 | |
| 505 | AssertRunningServices({"runningnormal1", "runningnormal2"}); |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 506 | AssertDumped("runningnormal1", "dump1"); |
| 507 | AssertDumped("runningnormal2", "dump2"); |
Vishnu Nair | 6a40853 | 2017-10-24 09:11:27 -0700 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | // Tests 'dumpsys --proto' |
| 511 | TEST_F(DumpsysTest, DumpWithProto) { |
| 512 | ExpectListServicesWithPriority({"run8", "run1", "run2", "run5"}, |
| 513 | IServiceManager::DUMP_FLAG_PRIORITY_ALL); |
| 514 | ExpectListServicesWithPriority({"run3", "run2", "run4", "run8"}, |
| 515 | IServiceManager::DUMP_FLAG_PROTO); |
| 516 | ExpectDump("run2", "dump1"); |
| 517 | ExpectDump("run8", "dump2"); |
| 518 | |
| 519 | CallMain({"--proto"}); |
| 520 | |
| 521 | AssertRunningServices({"run2", "run8"}); |
| 522 | AssertDumped("run2", "dump1"); |
| 523 | AssertDumped("run8", "dump2"); |
| 524 | } |
| 525 | |
| 526 | // Tests 'dumpsys --priority HIGH --proto' |
| 527 | TEST_F(DumpsysTest, DumpWithPriorityHighAndProto) { |
| 528 | ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"}, |
| 529 | IServiceManager::DUMP_FLAG_PRIORITY_HIGH); |
| 530 | ExpectListServicesWithPriority({"runninghigh1", "runninghigh2", "runninghigh3"}, |
| 531 | IServiceManager::DUMP_FLAG_PROTO); |
| 532 | |
| 533 | ExpectDump("runninghigh1", "dump1"); |
| 534 | ExpectDump("runninghigh2", "dump2"); |
| 535 | |
| 536 | CallMain({"--priority", "HIGH", "--proto"}); |
| 537 | |
| 538 | AssertRunningServices({"runninghigh1", "runninghigh2"}); |
| 539 | AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH); |
| 540 | AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH); |
Vishnu Nair | f56042d | 2017-09-19 15:25:10 -0700 | [diff] [blame] | 541 | } |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 542 | |
Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 543 | // Tests 'dumpsys --pid' |
| 544 | TEST_F(DumpsysTest, ListAllServicesWithPid) { |
| 545 | ExpectListServices({"Locksmith", "Valet"}); |
| 546 | ExpectCheckService("Locksmith"); |
| 547 | ExpectCheckService("Valet"); |
| 548 | |
| 549 | CallMain({"--pid"}); |
| 550 | |
| 551 | AssertRunningServices({"Locksmith", "Valet"}); |
| 552 | AssertOutputContains(std::to_string(getpid())); |
| 553 | } |
| 554 | |
| 555 | // Tests 'dumpsys --pid service_name' |
| 556 | TEST_F(DumpsysTest, ListServiceWithPid) { |
| 557 | ExpectCheckService("Locksmith"); |
| 558 | |
| 559 | CallMain({"--pid", "Locksmith"}); |
| 560 | |
| 561 | AssertOutput(std::to_string(getpid()) + "\n"); |
| 562 | } |
| 563 | |
Steven Moreland | a6ddb9a | 2019-09-27 16:41:02 +0000 | [diff] [blame] | 564 | TEST_F(DumpsysTest, GetBytesWritten) { |
| 565 | const char* serviceName = "service2"; |
| 566 | const char* dumpContents = "dump1"; |
| 567 | ExpectDump(serviceName, dumpContents); |
| 568 | |
| 569 | String16 service(serviceName); |
| 570 | Vector<String16> args; |
| 571 | std::chrono::duration<double> elapsedDuration; |
| 572 | size_t bytesWritten; |
| 573 | |
| 574 | CallSingleService(service, args, IServiceManager::DUMP_FLAG_PRIORITY_ALL, |
| 575 | /* as_proto = */ false, elapsedDuration, bytesWritten); |
| 576 | |
| 577 | AssertOutput(dumpContents); |
| 578 | EXPECT_THAT(bytesWritten, Eq(strlen(dumpContents))); |
| 579 | } |
| 580 | |
Vishnu Nair | e4f6174 | 2017-12-21 08:30:28 -0800 | [diff] [blame] | 581 | TEST_F(DumpsysTest, WriteDumpWithoutThreadStart) { |
| 582 | std::chrono::duration<double> elapsedDuration; |
| 583 | size_t bytesWritten; |
| 584 | status_t status = |
| 585 | dump_.writeDump(STDOUT_FILENO, String16("service"), std::chrono::milliseconds(500), |
| 586 | /* as_proto = */ false, elapsedDuration, bytesWritten); |
| 587 | EXPECT_THAT(status, Eq(INVALID_OPERATION)); |
Steven Moreland | 5a30d34 | 2019-10-08 13:53:28 -0700 | [diff] [blame] | 588 | } |