• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 "WatchdogProcessService.h"
18 
19 #include "gmock/gmock.h"
20 
21 namespace android {
22 namespace automotive {
23 namespace watchdog {
24 
25 using android::sp;
26 using binder::Status;
27 using ::testing::_;
28 using ::testing::Return;
29 
30 namespace {
31 
32 class MockBinder : public BBinder {
33 public:
34     MOCK_METHOD(status_t, linkToDeath,
35                 (const sp<DeathRecipient>& recipient, void* cookie, uint32_t flags), (override));
36     MOCK_METHOD(status_t, unlinkToDeath,
37                 (const wp<DeathRecipient>& recipient, void* cookie, uint32_t flags,
38                  wp<DeathRecipient>* outRecipient),
39                 (override));
40 };
41 
42 class MockCarWatchdogClient : public ICarWatchdogClientDefault {
43 public:
MockCarWatchdogClient()44     MockCarWatchdogClient() { mBinder = new MockBinder(); }
getBinder() const45     sp<MockBinder> getBinder() const { return mBinder; }
46 
47     MOCK_METHOD(IBinder*, onAsBinder, (), (override));
48 
49 private:
50     sp<MockBinder> mBinder;
51 };
52 
53 class MockCarWatchdogMonitor : public ICarWatchdogMonitorDefault {
54 public:
MockCarWatchdogMonitor()55     MockCarWatchdogMonitor() { mBinder = new MockBinder(); }
getBinder() const56     sp<MockBinder> getBinder() const { return mBinder; }
57 
58     MOCK_METHOD(IBinder*, onAsBinder, (), (override));
59 
60 private:
61     sp<MockBinder> mBinder;
62 };
63 
64 }  // namespace
65 
66 class WatchdogProcessServiceTest : public ::testing::Test {
67 protected:
SetUp()68     void SetUp() override {
69         sp<Looper> looper(Looper::prepare(/*opts=*/0));
70         mWatchdogProcessService = new WatchdogProcessService(looper);
71     }
72 
TearDown()73     void TearDown() override { mWatchdogProcessService = nullptr; }
74 
75     sp<WatchdogProcessService> mWatchdogProcessService;
76 };
77 
createMockCarWatchdogClient(status_t linkToDeathResult)78 sp<MockCarWatchdogClient> createMockCarWatchdogClient(status_t linkToDeathResult) {
79     sp<MockCarWatchdogClient> client = new MockCarWatchdogClient;
80     sp<MockBinder> binder = client->getBinder();
81     EXPECT_CALL(*binder, linkToDeath(_, nullptr, 0)).WillRepeatedly(Return(linkToDeathResult));
82     EXPECT_CALL(*binder, unlinkToDeath(_, nullptr, 0, nullptr)).WillRepeatedly(Return(OK));
83     EXPECT_CALL(*client, onAsBinder()).WillRepeatedly(Return(binder.get()));
84     return client;
85 }
86 
createMockCarWatchdogMonitor(status_t linkToDeathResult)87 sp<MockCarWatchdogMonitor> createMockCarWatchdogMonitor(status_t linkToDeathResult) {
88     sp<MockCarWatchdogMonitor> monitor = new MockCarWatchdogMonitor;
89     sp<MockBinder> binder = monitor->getBinder();
90     EXPECT_CALL(*binder, linkToDeath(_, nullptr, 0)).WillRepeatedly(Return(linkToDeathResult));
91     EXPECT_CALL(*binder, unlinkToDeath(_, nullptr, 0, nullptr)).WillRepeatedly(Return(OK));
92     EXPECT_CALL(*monitor, onAsBinder()).WillRepeatedly(Return(binder.get()));
93     return monitor;
94 }
95 
expectNormalCarWatchdogClient()96 sp<MockCarWatchdogClient> expectNormalCarWatchdogClient() {
97     return createMockCarWatchdogClient(OK);
98 }
99 
expectCarWatchdogClientBinderDied()100 sp<MockCarWatchdogClient> expectCarWatchdogClientBinderDied() {
101     return createMockCarWatchdogClient(DEAD_OBJECT);
102 }
103 
expectNormalCarWatchdogMonitor()104 sp<MockCarWatchdogMonitor> expectNormalCarWatchdogMonitor() {
105     return createMockCarWatchdogMonitor(OK);
106 }
107 
expectCarWatchdogMonitorBinderDied()108 sp<MockCarWatchdogMonitor> expectCarWatchdogMonitorBinderDied() {
109     return createMockCarWatchdogMonitor(DEAD_OBJECT);
110 }
111 
TEST_F(WatchdogProcessServiceTest,TestRegisterClient)112 TEST_F(WatchdogProcessServiceTest, TestRegisterClient) {
113     sp<MockCarWatchdogClient> client = expectNormalCarWatchdogClient();
114     Status status =
115             mWatchdogProcessService->registerClient(client, TimeoutLength::TIMEOUT_CRITICAL);
116     ASSERT_TRUE(status.isOk()) << status;
117     status = mWatchdogProcessService->registerClient(client, TimeoutLength::TIMEOUT_CRITICAL);
118     ASSERT_TRUE(status.isOk()) << status;
119 }
120 
TEST_F(WatchdogProcessServiceTest,TestUnregisterClient)121 TEST_F(WatchdogProcessServiceTest, TestUnregisterClient) {
122     sp<MockCarWatchdogClient> client = expectNormalCarWatchdogClient();
123     mWatchdogProcessService->registerClient(client, TimeoutLength::TIMEOUT_CRITICAL);
124     Status status = mWatchdogProcessService->unregisterClient(client);
125     ASSERT_TRUE(status.isOk()) << status;
126     ASSERT_FALSE(mWatchdogProcessService->unregisterClient(client).isOk())
127             << "Unregistering an unregistered client shoud return an error";
128 }
129 
TEST_F(WatchdogProcessServiceTest,TestRegisterClient_BinderDied)130 TEST_F(WatchdogProcessServiceTest, TestRegisterClient_BinderDied) {
131     sp<MockCarWatchdogClient> client = expectCarWatchdogClientBinderDied();
132     ASSERT_FALSE(
133             mWatchdogProcessService->registerClient(client, TimeoutLength::TIMEOUT_CRITICAL).isOk())
134             << "When linkToDeath fails, registerClient should return an error";
135 }
136 
TEST_F(WatchdogProcessServiceTest,TestRegisterMediator)137 TEST_F(WatchdogProcessServiceTest, TestRegisterMediator) {
138     sp<ICarWatchdogClient> mediator = expectNormalCarWatchdogClient();
139     Status status = mWatchdogProcessService->registerMediator(mediator);
140     ASSERT_TRUE(status.isOk()) << status;
141     status = mWatchdogProcessService->registerMediator(mediator);
142     ASSERT_TRUE(status.isOk()) << status;
143 }
144 
TEST_F(WatchdogProcessServiceTest,TestRegisterMediator_BinderDied)145 TEST_F(WatchdogProcessServiceTest, TestRegisterMediator_BinderDied) {
146     sp<MockCarWatchdogClient> mediator = expectCarWatchdogClientBinderDied();
147     ASSERT_FALSE(mWatchdogProcessService->registerMediator(mediator).isOk())
148             << "When linkToDeath fails, registerMediator should return an error";
149 }
150 
TEST_F(WatchdogProcessServiceTest,TestUnregisterMediator)151 TEST_F(WatchdogProcessServiceTest, TestUnregisterMediator) {
152     sp<ICarWatchdogClient> mediator = expectNormalCarWatchdogClient();
153     mWatchdogProcessService->registerMediator(mediator);
154     Status status = mWatchdogProcessService->unregisterMediator(mediator);
155     ASSERT_TRUE(status.isOk()) << status;
156     ASSERT_FALSE(mWatchdogProcessService->unregisterMediator(mediator).isOk())
157             << "Unregistering an unregistered mediator shoud return an error";
158 }
159 
TEST_F(WatchdogProcessServiceTest,TestRegisterMonitor)160 TEST_F(WatchdogProcessServiceTest, TestRegisterMonitor) {
161     sp<ICarWatchdogMonitor> monitorOne = expectNormalCarWatchdogMonitor();
162     sp<ICarWatchdogMonitor> monitorTwo = expectNormalCarWatchdogMonitor();
163     Status status = mWatchdogProcessService->registerMonitor(monitorOne);
164     ASSERT_TRUE(status.isOk()) << status;
165     status = mWatchdogProcessService->registerMonitor(monitorOne);
166     ASSERT_TRUE(status.isOk()) << status;
167     status = mWatchdogProcessService->registerMonitor(monitorTwo);
168     ASSERT_TRUE(status.isOk()) << status;
169 }
170 
TEST_F(WatchdogProcessServiceTest,TestRegisterMonitor_BinderDied)171 TEST_F(WatchdogProcessServiceTest, TestRegisterMonitor_BinderDied) {
172     sp<MockCarWatchdogMonitor> monitor = expectCarWatchdogMonitorBinderDied();
173     ASSERT_FALSE(mWatchdogProcessService->registerMonitor(monitor).isOk())
174             << "When linkToDeath fails, registerMonitor should return an error";
175 }
176 
TEST_F(WatchdogProcessServiceTest,TestUnregisterMonitor)177 TEST_F(WatchdogProcessServiceTest, TestUnregisterMonitor) {
178     sp<ICarWatchdogMonitor> monitor = expectNormalCarWatchdogMonitor();
179     mWatchdogProcessService->registerMonitor(monitor);
180     Status status = mWatchdogProcessService->unregisterMonitor(monitor);
181     ASSERT_TRUE(status.isOk()) << status;
182     ASSERT_FALSE(mWatchdogProcessService->unregisterMonitor(monitor).isOk())
183             << "Unregistering an unregistered monitor should return an error";
184 }
185 
TEST_F(WatchdogProcessServiceTest,TestTellClientAlive)186 TEST_F(WatchdogProcessServiceTest, TestTellClientAlive) {
187     sp<ICarWatchdogClient> client = expectNormalCarWatchdogClient();
188     mWatchdogProcessService->registerClient(client, TimeoutLength::TIMEOUT_CRITICAL);
189     ASSERT_FALSE(mWatchdogProcessService->tellClientAlive(client, 1234).isOk())
190             << "tellClientAlive not synced with checkIfAlive should return an error";
191 }
192 
TEST_F(WatchdogProcessServiceTest,TestTellMediatorAlive)193 TEST_F(WatchdogProcessServiceTest, TestTellMediatorAlive) {
194     sp<ICarWatchdogClient> mediator = expectNormalCarWatchdogClient();
195     mWatchdogProcessService->registerMediator(mediator);
196     std::vector<int32_t> pids = {111, 222};
197     ASSERT_FALSE(mWatchdogProcessService->tellMediatorAlive(mediator, pids, 1234).isOk())
198             << "tellMediatorAlive not synced with checkIfAlive should return an error";
199 }
200 
TEST_F(WatchdogProcessServiceTest,TestTellDumpFinished)201 TEST_F(WatchdogProcessServiceTest, TestTellDumpFinished) {
202     sp<ICarWatchdogMonitor> monitor = expectNormalCarWatchdogMonitor();
203     ASSERT_FALSE(mWatchdogProcessService->tellDumpFinished(monitor, 1234).isOk())
204             << "Unregistered monitor cannot call tellDumpFinished";
205     mWatchdogProcessService->registerMonitor(monitor);
206     Status status = mWatchdogProcessService->tellDumpFinished(monitor, 1234);
207     ASSERT_TRUE(status.isOk()) << status;
208 }
209 
210 }  // namespace watchdog
211 }  // namespace automotive
212 }  // namespace android
213