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