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