1343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme/*
2343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme * Copyright (C) 2016 The Android Open Source Project
3343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme *
4343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme * Licensed under the Apache License, Version 2.0 (the "License");
5343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme * you may not use this file except in compliance with the License.
6343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme * You may obtain a copy of the License at
7343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme *
8343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme *      http://www.apache.org/licenses/LICENSE-2.0
9343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme *
10343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme * Unless required by applicable law or agreed to in writing, software
11343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme * distributed under the License is distributed on an "AS IS" BASIS,
12343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme * See the License for the specific language governing permissions and
14343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme * limitations under the License.
15343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme */
16343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
17343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme#include "../dumpsys.h"
18343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
19343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme#include <vector>
20343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
21343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme#include <gmock/gmock.h>
22343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme#include <gtest/gtest.h>
23343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
24343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme#include <android-base/file.h>
256a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair#include <serviceutils/PriorityDumper.h>
26343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme#include <utils/String16.h>
27a0f7f2dfd0ecb0ba13efdb62b5b850e198f01a1fSteven Moreland#include <utils/String8.h>
28343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme#include <utils/Vector.h>
29343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
30343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing namespace android;
31343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
32343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::_;
33343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::Action;
34343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::ActionInterface;
35343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::DoAll;
36343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::Eq;
37343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::HasSubstr;
38343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::MakeAction;
395c8a98f725debfc38ec95c5ee9312891aabfd2d2Felipe Lemeusing ::testing::Mock;
40343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::Not;
41343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::Return;
42343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::StrEq;
43343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::Test;
44343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::WithArg;
45343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::internal::CaptureStderr;
46343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::internal::CaptureStdout;
47343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::internal::GetCapturedStderr;
48343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeusing ::testing::internal::GetCapturedStdout;
49343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
50343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeclass ServiceManagerMock : public IServiceManager {
51343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme  public:
52343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    MOCK_CONST_METHOD1(getService, sp<IBinder>(const String16&));
53343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    MOCK_CONST_METHOD1(checkService, sp<IBinder>(const String16&));
54f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    MOCK_METHOD4(addService, status_t(const String16&, const sp<IBinder>&, bool, int));
55f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    MOCK_METHOD1(listServices, Vector<String16>(int));
56343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
57343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme  protected:
58343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    MOCK_METHOD0(onAsBinder, IBinder*());
59343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme};
60343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
61343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeclass BinderMock : public BBinder {
62343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme  public:
63343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    BinderMock() {
64343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
65343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
66343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    MOCK_METHOD2(dump, status_t(int, const Vector<String16>&));
67343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme};
68343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
69343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme// gmock black magic to provide a WithArg<0>(WriteOnFd(output)) matcher
70343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemetypedef void WriteOnFdFunction(int);
71343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
72343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeclass WriteOnFdAction : public ActionInterface<WriteOnFdFunction> {
73343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme  public:
74343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    explicit WriteOnFdAction(const std::string& output) : output_(output) {
75343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
76343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    virtual Result Perform(const ArgumentTuple& args) {
77343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        int fd = ::std::tr1::get<0>(args);
78343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        android::base::WriteStringToFd(output_, fd);
79343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
80343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
81343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme  private:
82343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    std::string output_;
83343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme};
84343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
85343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme// Matcher used to emulate dump() by writing on its file descriptor.
86343175a5ffeba6d1714336913a81b3029df73dbbFelipe LemeAction<WriteOnFdFunction> WriteOnFd(const std::string& output) {
87343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    return MakeAction(new WriteOnFdAction(output));
88343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme}
89343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
90343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme// Matcher for args using Android's Vector<String16> format
91343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme// TODO: move it to some common testing library
92343175a5ffeba6d1714336913a81b3029df73dbbFelipe LemeMATCHER_P(AndroidElementsAre, expected, "") {
93343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    std::ostringstream errors;
94343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    if (arg.size() != expected.size()) {
95343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        errors << " sizes do not match (expected " << expected.size() << ", got " << arg.size()
96343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme               << ")\n";
97343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
98343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    int i = 0;
99343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    std::ostringstream actual_stream, expected_stream;
100cb057c2e6bf23b43df49282c0db26b663e4535f7Chih-Hung Hsieh    for (const String16& actual : arg) {
101a0f7f2dfd0ecb0ba13efdb62b5b850e198f01a1fSteven Moreland        std::string actual_str = String8(actual).c_str();
102343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        std::string expected_str = expected[i];
103343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        actual_stream << "'" << actual_str << "' ";
104343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        expected_stream << "'" << expected_str << "' ";
105343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        if (actual_str != expected_str) {
106343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme            errors << " element mismatch at index " << i << "\n";
107343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        }
108343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        i++;
109343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
110343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
111343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    if (!errors.str().empty()) {
112343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        errors << "\nExpected args: " << expected_stream.str()
113343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme               << "\nActual args: " << actual_stream.str();
114343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        *result_listener << errors.str();
115343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        return false;
116343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
117343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    return true;
118343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme}
119343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
120343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme// Custom action to sleep for timeout seconds
121343175a5ffeba6d1714336913a81b3029df73dbbFelipe LemeACTION_P(Sleep, timeout) {
122343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    sleep(timeout);
123343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme}
124343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
125343175a5ffeba6d1714336913a81b3029df73dbbFelipe Lemeclass DumpsysTest : public Test {
126343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme  public:
1272c3cd83603a7eefe7efe8c99cbe1c04b54cfe599Steven Moreland    DumpsysTest() : sm_(), dump_(&sm_), stdout_(), stderr_() {
128343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
129343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
130343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    void ExpectListServices(std::vector<std::string> services) {
131343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        Vector<String16> services16;
132343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        for (auto& service : services) {
133343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme            services16.add(String16(service.c_str()));
134343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        }
1356a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair        EXPECT_CALL(sm_, listServices(IServiceManager::DUMP_FLAG_PRIORITY_ALL))
136f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair            .WillRepeatedly(Return(services16));
137f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    }
138f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
1396a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    void ExpectListServicesWithPriority(std::vector<std::string> services, int dumpFlags) {
140f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair        Vector<String16> services16;
141f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair        for (auto& service : services) {
142f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair            services16.add(String16(service.c_str()));
143f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair        }
1446a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair        EXPECT_CALL(sm_, listServices(dumpFlags)).WillRepeatedly(Return(services16));
145343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
146343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
147343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    sp<BinderMock> ExpectCheckService(const char* name, bool running = true) {
148343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        sp<BinderMock> binder_mock;
149343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        if (running) {
150343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme            binder_mock = new BinderMock;
151343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        }
152343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        EXPECT_CALL(sm_, checkService(String16(name))).WillRepeatedly(Return(binder_mock));
153343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        return binder_mock;
154343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
155343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
156343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    void ExpectDump(const char* name, const std::string& output) {
157343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        sp<BinderMock> binder_mock = ExpectCheckService(name);
158343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        EXPECT_CALL(*binder_mock, dump(_, _))
159343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme            .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
160343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
161343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
162343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    void ExpectDumpWithArgs(const char* name, std::vector<std::string> args,
163343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme                            const std::string& output) {
164343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        sp<BinderMock> binder_mock = ExpectCheckService(name);
165343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        EXPECT_CALL(*binder_mock, dump(_, AndroidElementsAre(args)))
166343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme            .WillRepeatedly(DoAll(WithArg<0>(WriteOnFd(output)), Return(0)));
167343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
168343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
1695c8a98f725debfc38ec95c5ee9312891aabfd2d2Felipe Leme    sp<BinderMock> ExpectDumpAndHang(const char* name, int timeout_s, const std::string& output) {
170343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        sp<BinderMock> binder_mock = ExpectCheckService(name);
171343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        EXPECT_CALL(*binder_mock, dump(_, _))
172343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme            .WillRepeatedly(DoAll(Sleep(timeout_s), WithArg<0>(WriteOnFd(output)), Return(0)));
1735c8a98f725debfc38ec95c5ee9312891aabfd2d2Felipe Leme        return binder_mock;
174343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
175343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
176343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    void CallMain(const std::vector<std::string>& args) {
177343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        const char* argv[1024] = {"/some/virtual/dir/dumpsys"};
178343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        int argc = (int)args.size() + 1;
179343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        int i = 1;
180343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        for (const std::string& arg : args) {
181343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme            argv[i++] = arg.c_str();
182343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        }
183343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        CaptureStdout();
184343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        CaptureStderr();
185343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        int status = dump_.main(argc, const_cast<char**>(argv));
186343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        stdout_ = GetCapturedStdout();
187343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        stderr_ = GetCapturedStderr();
188343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        EXPECT_THAT(status, Eq(0));
189343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
190343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
191e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    void CallSingleService(const String16& serviceName, Vector<String16>& args, int priorityFlags,
192e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair                           bool supportsProto, std::chrono::duration<double>& elapsedDuration,
193e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair                           size_t& bytesWritten) {
194e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair        CaptureStdout();
195e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair        CaptureStderr();
196e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair        dump_.setServiceArgs(args, supportsProto, priorityFlags);
197e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair        status_t status = dump_.startDumpThread(serviceName, args);
198e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair        EXPECT_THAT(status, Eq(0));
199e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair        status = dump_.writeDump(STDOUT_FILENO, serviceName, std::chrono::milliseconds(500), false,
200e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair                                 elapsedDuration, bytesWritten);
201e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair        EXPECT_THAT(status, Eq(0));
202e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair        dump_.stopDumpThread(/* dumpCompleted = */ true);
203e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair        stdout_ = GetCapturedStdout();
204e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair        stderr_ = GetCapturedStderr();
205e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    }
206e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair
2072c3cd83603a7eefe7efe8c99cbe1c04b54cfe599Steven Moreland    void AssertRunningServices(const std::vector<std::string>& services) {
208f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair        std::string expected;
209f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair        if (services.size() > 1) {
210f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair            expected.append("Currently running services:\n");
211f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair        }
212343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        for (const std::string& service : services) {
213343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme            expected.append("  ").append(service).append("\n");
214343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        }
215343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        EXPECT_THAT(stdout_, HasSubstr(expected));
216343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
217343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
218343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    void AssertOutput(const std::string& expected) {
219343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        EXPECT_THAT(stdout_, StrEq(expected));
220343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
221343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
222343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    void AssertOutputContains(const std::string& expected) {
223343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        EXPECT_THAT(stdout_, HasSubstr(expected));
224343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
225343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
226343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    void AssertDumped(const std::string& service, const std::string& dump) {
227343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        EXPECT_THAT(stdout_, HasSubstr("DUMP OF SERVICE " + service + ":\n" + dump));
228e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair        EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
229343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
230343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
2316a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    void AssertDumpedWithPriority(const std::string& service, const std::string& dump,
2326a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair                                  const char16_t* priorityType) {
2336a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair        std::string priority = String8(priorityType).c_str();
2346a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair        EXPECT_THAT(stdout_,
2356a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair                    HasSubstr("DUMP OF SERVICE " + priority + " " + service + ":\n" + dump));
236e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair        EXPECT_THAT(stdout_, HasSubstr("was the duration of dumpsys " + service + ", ending at: "));
2376a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    }
2386a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair
239343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    void AssertNotDumped(const std::string& dump) {
240343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        EXPECT_THAT(stdout_, Not(HasSubstr(dump)));
241343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
242343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
243343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    void AssertStopped(const std::string& service) {
244343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme        EXPECT_THAT(stderr_, HasSubstr("Can't find service: " + service + "\n"));
245343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    }
246343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
247343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ServiceManagerMock sm_;
248343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    Dumpsys dump_;
249343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
250343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme  private:
251343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    std::string stdout_, stderr_;
252343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme};
253343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
254343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme// Tests 'dumpsys -l' when all services are running
255343175a5ffeba6d1714336913a81b3029df73dbbFelipe LemeTEST_F(DumpsysTest, ListAllServices) {
256343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectListServices({"Locksmith", "Valet"});
257343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectCheckService("Locksmith");
258343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectCheckService("Valet");
259343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
260343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    CallMain({"-l"});
261343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
262343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertRunningServices({"Locksmith", "Valet"});
263343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme}
264343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
265343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme// Tests 'dumpsys -l' when a service is not running
266343175a5ffeba6d1714336913a81b3029df73dbbFelipe LemeTEST_F(DumpsysTest, ListRunningServices) {
267343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectListServices({"Locksmith", "Valet"});
268343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectCheckService("Locksmith");
269343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectCheckService("Valet", false);
270343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
271343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    CallMain({"-l"});
272343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
273343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertRunningServices({"Locksmith"});
274343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertNotDumped({"Valet"});
275343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme}
276343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
277f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair// Tests 'dumpsys -l --priority HIGH'
278f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu NairTEST_F(DumpsysTest, ListAllServicesWithPriority) {
2796a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    ExpectListServicesWithPriority({"Locksmith", "Valet"}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
280f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectCheckService("Locksmith");
281f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectCheckService("Valet");
282f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
283f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    CallMain({"-l", "--priority", "HIGH"});
284f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
285f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    AssertRunningServices({"Locksmith", "Valet"});
286f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair}
287f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
288f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair// Tests 'dumpsys -l --priority HIGH' with and empty list
289f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu NairTEST_F(DumpsysTest, ListEmptyServicesWithPriority) {
2906a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    ExpectListServicesWithPriority({}, IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
291f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
292f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    CallMain({"-l", "--priority", "HIGH"});
293f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
294f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    AssertRunningServices({});
295f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair}
296f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
2976a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair// Tests 'dumpsys -l --proto'
2986a40853e06f5274d84b0fc66e349a36510d1497fVishnu NairTEST_F(DumpsysTest, ListAllServicesWithProto) {
2996a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    ExpectListServicesWithPriority({"Locksmith", "Valet", "Car"},
3006a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair                                   IServiceManager::DUMP_FLAG_PRIORITY_ALL);
3016a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    ExpectListServicesWithPriority({"Valet", "Car"}, IServiceManager::DUMP_FLAG_PROTO);
3026a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    ExpectCheckService("Car");
3036a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    ExpectCheckService("Valet");
3046a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair
3056a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    CallMain({"-l", "--proto"});
3066a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair
3076a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    AssertRunningServices({"Car", "Valet"});
3086a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair}
3096a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair
310343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme// Tests 'dumpsys service_name' on a service is running
311343175a5ffeba6d1714336913a81b3029df73dbbFelipe LemeTEST_F(DumpsysTest, DumpRunningService) {
312343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectDump("Valet", "Here's your car");
313343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
314343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    CallMain({"Valet"});
315343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
316343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertOutput("Here's your car");
317343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme}
318343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
319343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme// Tests 'dumpsys -t 1 service_name' on a service that times out after 2s
3206921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu NairTEST_F(DumpsysTest, DumpRunningServiceTimeoutInSec) {
3215c8a98f725debfc38ec95c5ee9312891aabfd2d2Felipe Leme    sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
322343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
323343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    CallMain({"-t", "1", "Valet"});
324343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
3256921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu Nair    AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (1000ms) EXPIRED");
3266921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu Nair    AssertNotDumped("Here's your car");
3276921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu Nair
3286921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu Nair    // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
3296921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu Nair    Mock::AllowLeak(binder_mock.get());
3306921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu Nair}
3316921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu Nair
3326921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu Nair// Tests 'dumpsys -T 500 service_name' on a service that times out after 2s
3336921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu NairTEST_F(DumpsysTest, DumpRunningServiceTimeoutInMs) {
3346921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu Nair    sp<BinderMock> binder_mock = ExpectDumpAndHang("Valet", 2, "Here's your car");
3356921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu Nair
3366921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu Nair    CallMain({"-T", "500", "Valet"});
3376921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu Nair
3386921f80f26cb779d2982d2e37e14aeadbc8230b7Vishnu Nair    AssertOutputContains("SERVICE 'Valet' DUMP TIMEOUT (500ms) EXPIRED");
339343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertNotDumped("Here's your car");
340343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
3415c8a98f725debfc38ec95c5ee9312891aabfd2d2Felipe Leme    // TODO(b/65056227): BinderMock is not destructed because thread is detached on dumpsys.cpp
3425c8a98f725debfc38ec95c5ee9312891aabfd2d2Felipe Leme    Mock::AllowLeak(binder_mock.get());
343343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme}
344343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
345343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme// Tests 'dumpsys service_name Y U NO HAVE ARGS' on a service that is running
346343175a5ffeba6d1714336913a81b3029df73dbbFelipe LemeTEST_F(DumpsysTest, DumpWithArgsRunningService) {
347343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectDumpWithArgs("SERVICE", {"Y", "U", "NO", "HANDLE", "ARGS"}, "I DO!");
348343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
349343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    CallMain({"SERVICE", "Y", "U", "NO", "HANDLE", "ARGS"});
350343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
351343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertOutput("I DO!");
352343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme}
353343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
35464afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair// Tests dumpsys passes the -a flag when called on all services
35564afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu NairTEST_F(DumpsysTest, PassAllFlagsToServices) {
35664afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectListServices({"Locksmith", "Valet"});
35764afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectCheckService("Locksmith");
35864afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectCheckService("Valet");
35964afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectDumpWithArgs("Locksmith", {"-a"}, "dumped1");
36064afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectDumpWithArgs("Valet", {"-a"}, "dumped2");
36164afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair
36264afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    CallMain({"-T", "500"});
36364afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair
36464afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    AssertDumped("Locksmith", "dumped1");
36564afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    AssertDumped("Valet", "dumped2");
36664afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair}
36764afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair
36864afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair// Tests dumpsys passes the -a flag when called on NORMAL priority services
36964afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu NairTEST_F(DumpsysTest, PassAllFlagsToNormalServices) {
37064afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectListServicesWithPriority({"Locksmith", "Valet"},
37164afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair                                   IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
37264afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectCheckService("Locksmith");
37364afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectCheckService("Valet");
37464afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectDumpWithArgs("Locksmith", {"-a", "--dump-priority", "NORMAL"}, "dump1");
37564afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectDumpWithArgs("Valet", {"-a", "--dump-priority", "NORMAL"}, "dump2");
37664afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair
37764afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    CallMain({"--priority", "NORMAL"});
37864afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair
37964afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    AssertDumped("Locksmith", "dump1");
38064afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    AssertDumped("Valet", "dump2");
38164afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair}
38264afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair
38364afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair// Tests dumpsys passes only priority flags when called on CRITICAL priority services
38464afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu NairTEST_F(DumpsysTest, PassPriorityFlagsToCriticalServices) {
38564afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectListServicesWithPriority({"Locksmith", "Valet"},
38664afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair                                   IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
38764afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectCheckService("Locksmith");
38864afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectCheckService("Valet");
38964afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectDumpWithArgs("Locksmith", {"--dump-priority", "CRITICAL"}, "dump1");
39064afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectDumpWithArgs("Valet", {"--dump-priority", "CRITICAL"}, "dump2");
39164afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair
39264afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    CallMain({"--priority", "CRITICAL"});
39364afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair
39464afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
39564afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
39664afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair}
39764afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair
39864afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair// Tests dumpsys passes only priority flags when called on HIGH priority services
39964afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu NairTEST_F(DumpsysTest, PassPriorityFlagsToHighServices) {
40064afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectListServicesWithPriority({"Locksmith", "Valet"},
40164afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair                                   IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
40264afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectCheckService("Locksmith");
40364afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectCheckService("Valet");
40464afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectDumpWithArgs("Locksmith", {"--dump-priority", "HIGH"}, "dump1");
40564afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    ExpectDumpWithArgs("Valet", {"--dump-priority", "HIGH"}, "dump2");
40664afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair
40764afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    CallMain({"--priority", "HIGH"});
40864afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair
40964afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    AssertDumpedWithPriority("Locksmith", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
41064afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair    AssertDumpedWithPriority("Valet", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
41164afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair}
41264afc024d760e31f3f41e0c5cb8fc543c9392ef1Vishnu Nair
413343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme// Tests 'dumpsys' with no arguments
414343175a5ffeba6d1714336913a81b3029df73dbbFelipe LemeTEST_F(DumpsysTest, DumpMultipleServices) {
415343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectListServices({"running1", "stopped2", "running3"});
416343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectDump("running1", "dump1");
417343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectCheckService("stopped2", false);
418343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectDump("running3", "dump3");
419343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
420343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    CallMain({});
421343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
422343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertRunningServices({"running1", "running3"});
423343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertDumped("running1", "dump1");
424343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertStopped("stopped2");
425343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertDumped("running3", "dump3");
426343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme}
427343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
428343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme// Tests 'dumpsys --skip skipped3 skipped5', which should skip these services
429343175a5ffeba6d1714336913a81b3029df73dbbFelipe LemeTEST_F(DumpsysTest, DumpWithSkip) {
430343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectListServices({"running1", "stopped2", "skipped3", "running4", "skipped5"});
431343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectDump("running1", "dump1");
432343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectCheckService("stopped2", false);
433343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectDump("skipped3", "dump3");
434343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectDump("running4", "dump4");
435343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    ExpectDump("skipped5", "dump5");
436343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
437343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    CallMain({"--skip", "skipped3", "skipped5"});
438343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme
439343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
440343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertDumped("running1", "dump1");
441343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertDumped("running4", "dump4");
442343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertStopped("stopped2");
443343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertNotDumped("dump3");
444343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme    AssertNotDumped("dump5");
445343175a5ffeba6d1714336913a81b3029df73dbbFelipe Leme}
446f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
447f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair// Tests 'dumpsys --skip skipped3 skipped5 --priority CRITICAL', which should skip these services
448f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu NairTEST_F(DumpsysTest, DumpWithSkipAndPriority) {
449f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectListServicesWithPriority({"running1", "stopped2", "skipped3", "running4", "skipped5"},
4506a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair                                   IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
451f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectDump("running1", "dump1");
452f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectCheckService("stopped2", false);
453f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectDump("skipped3", "dump3");
454f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectDump("running4", "dump4");
455f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectDump("skipped5", "dump5");
456f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
457f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    CallMain({"--priority", "CRITICAL", "--skip", "skipped3", "skipped5"});
458f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
459f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    AssertRunningServices({"running1", "running4", "skipped3 (skipped)", "skipped5 (skipped)"});
4606a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    AssertDumpedWithPriority("running1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
4616a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    AssertDumpedWithPriority("running4", "dump4", PriorityDumper::PRIORITY_ARG_CRITICAL);
462f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    AssertStopped("stopped2");
463f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    AssertNotDumped("dump3");
464f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    AssertNotDumped("dump5");
465f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair}
466f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
467f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair// Tests 'dumpsys --priority CRITICAL'
468f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu NairTEST_F(DumpsysTest, DumpWithPriorityCritical) {
469f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectListServicesWithPriority({"runningcritical1", "runningcritical2"},
4706a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair                                   IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
471f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectDump("runningcritical1", "dump1");
472f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectDump("runningcritical2", "dump2");
473f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
474f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    CallMain({"--priority", "CRITICAL"});
475f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
476f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    AssertRunningServices({"runningcritical1", "runningcritical2"});
4776a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    AssertDumpedWithPriority("runningcritical1", "dump1", PriorityDumper::PRIORITY_ARG_CRITICAL);
4786a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    AssertDumpedWithPriority("runningcritical2", "dump2", PriorityDumper::PRIORITY_ARG_CRITICAL);
479f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair}
480f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
481f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair// Tests 'dumpsys --priority HIGH'
482f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu NairTEST_F(DumpsysTest, DumpWithPriorityHigh) {
483f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
4846a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair                                   IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
485f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectDump("runninghigh1", "dump1");
486f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectDump("runninghigh2", "dump2");
487f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
488f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    CallMain({"--priority", "HIGH"});
489f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
490f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    AssertRunningServices({"runninghigh1", "runninghigh2"});
4916a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
4926a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
493f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair}
494f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
495f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair// Tests 'dumpsys --priority NORMAL'
496f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu NairTEST_F(DumpsysTest, DumpWithPriorityNormal) {
497f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectListServicesWithPriority({"runningnormal1", "runningnormal2"},
4986a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair                                   IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
499f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectDump("runningnormal1", "dump1");
500f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    ExpectDump("runningnormal2", "dump2");
501f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
502f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    CallMain({"--priority", "NORMAL"});
503f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair
504f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair    AssertRunningServices({"runningnormal1", "runningnormal2"});
505e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    AssertDumped("runningnormal1", "dump1");
506e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    AssertDumped("runningnormal2", "dump2");
5076a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair}
5086a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair
5096a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair// Tests 'dumpsys --proto'
5106a40853e06f5274d84b0fc66e349a36510d1497fVishnu NairTEST_F(DumpsysTest, DumpWithProto) {
5116a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    ExpectListServicesWithPriority({"run8", "run1", "run2", "run5"},
5126a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair                                   IServiceManager::DUMP_FLAG_PRIORITY_ALL);
5136a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    ExpectListServicesWithPriority({"run3", "run2", "run4", "run8"},
5146a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair                                   IServiceManager::DUMP_FLAG_PROTO);
5156a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    ExpectDump("run2", "dump1");
5166a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    ExpectDump("run8", "dump2");
5176a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair
5186a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    CallMain({"--proto"});
5196a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair
5206a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    AssertRunningServices({"run2", "run8"});
5216a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    AssertDumped("run2", "dump1");
5226a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    AssertDumped("run8", "dump2");
5236a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair}
5246a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair
5256a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair// Tests 'dumpsys --priority HIGH --proto'
5266a40853e06f5274d84b0fc66e349a36510d1497fVishnu NairTEST_F(DumpsysTest, DumpWithPriorityHighAndProto) {
5276a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    ExpectListServicesWithPriority({"runninghigh1", "runninghigh2"},
5286a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair                                   IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
5296a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    ExpectListServicesWithPriority({"runninghigh1", "runninghigh2", "runninghigh3"},
5306a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair                                   IServiceManager::DUMP_FLAG_PROTO);
5316a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair
5326a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    ExpectDump("runninghigh1", "dump1");
5336a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    ExpectDump("runninghigh2", "dump2");
5346a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair
5356a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    CallMain({"--priority", "HIGH", "--proto"});
5366a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair
5376a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    AssertRunningServices({"runninghigh1", "runninghigh2"});
5386a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    AssertDumpedWithPriority("runninghigh1", "dump1", PriorityDumper::PRIORITY_ARG_HIGH);
5396a40853e06f5274d84b0fc66e349a36510d1497fVishnu Nair    AssertDumpedWithPriority("runninghigh2", "dump2", PriorityDumper::PRIORITY_ARG_HIGH);
540f56042d6a496fca2e5cfc1f598251f77a9bc7986Vishnu Nair}
541e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair
542e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu NairTEST_F(DumpsysTest, GetBytesWritten) {
543e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    const char* serviceName = "service2";
544e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    const char* dumpContents = "dump1";
545e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    ExpectDump(serviceName, dumpContents);
546e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair
547e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    String16 service(serviceName);
548e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    Vector<String16> args;
549e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    std::chrono::duration<double> elapsedDuration;
550e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    size_t bytesWritten;
551e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair
552e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    CallSingleService(service, args, IServiceManager::DUMP_FLAG_PRIORITY_ALL,
553e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair                      /* as_proto = */ false, elapsedDuration, bytesWritten);
554e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair
555e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    AssertOutput(dumpContents);
556e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    EXPECT_THAT(bytesWritten, Eq(strlen(dumpContents)));
557e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair}
558e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair
559e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu NairTEST_F(DumpsysTest, WriteDumpWithoutThreadStart) {
560e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    std::chrono::duration<double> elapsedDuration;
561e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    size_t bytesWritten;
562e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    status_t status =
563e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair        dump_.writeDump(STDOUT_FILENO, String16("service"), std::chrono::milliseconds(500),
564e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair                        /* as_proto = */ false, elapsedDuration, bytesWritten);
565e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair    EXPECT_THAT(status, Eq(INVALID_OPERATION));
566e4f617485463ae35c7e2474c4f725ddf824ebfbbVishnu Nair}