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