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