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 "MockIoOveruseMonitor.h"
18 #include "MockWatchdogPerfService.h"
19 #include "MockWatchdogProcessService.h"
20 #include "MockWatchdogServiceHelper.h"
21 #include "WatchdogBinderMediator.h"
22 #include "WatchdogInternalHandler.h"
23 #include "WatchdogServiceHelper.h"
24 
25 #include <android-base/result.h>
26 #include <android/automotive/watchdog/internal/BootPhase.h>
27 #include <android/automotive/watchdog/internal/GarageMode.h>
28 #include <android/automotive/watchdog/internal/PowerCycle.h>
29 #include <android/automotive/watchdog/internal/UserState.h>
30 #include <binder/IBinder.h>
31 #include <binder/IPCThreadState.h>
32 #include <gmock/gmock.h>
33 #include <gtest/gtest.h>
34 #include <private/android_filesystem_config.h>
35 #include <utils/RefBase.h>
36 
37 #include <errno.h>
38 
39 namespace android {
40 namespace automotive {
41 namespace watchdog {
42 
43 namespace aawi = ::android::automotive::watchdog::internal;
44 
45 using aawi::GarageMode;
46 using aawi::ICarWatchdogServiceForSystem;
47 using aawi::ICarWatchdogServiceForSystemDefault;
48 using aawi::PowerCycle;
49 using aawi::ResourceOveruseConfiguration;
50 using ::android::sp;
51 using ::android::String16;
52 using ::android::base::Result;
53 using ::android::binder::Status;
54 using ::testing::_;
55 using ::testing::Pointer;
56 using ::testing::Return;
57 
58 namespace {
59 
60 class MockWatchdogBinderMediator : public WatchdogBinderMediator {
61 public:
MockWatchdogBinderMediator(const android::sp<WatchdogProcessService> & watchdogProcessService,const android::sp<WatchdogPerfServiceInterface> & watchdogPerfService,const android::sp<IWatchdogServiceHelper> & watchdogServiceHelper)62     MockWatchdogBinderMediator(const android::sp<WatchdogProcessService>& watchdogProcessService,
63                                const android::sp<WatchdogPerfServiceInterface>& watchdogPerfService,
64                                const android::sp<IWatchdogServiceHelper>& watchdogServiceHelper) :
65           WatchdogBinderMediator(watchdogProcessService, watchdogPerfService, watchdogServiceHelper,
66                                  [](const char*, const android::sp<android::IBinder>&)
67                                          -> Result<void> { return Result<void>{}; }) {}
~MockWatchdogBinderMediator()68     ~MockWatchdogBinderMediator() {}
69 
70     MOCK_METHOD(status_t, dump, (int fd, const Vector<android::String16>& args), (override));
71 };
72 
73 class ScopedChangeCallingUid : public RefBase {
74 public:
ScopedChangeCallingUid(uid_t uid)75     explicit ScopedChangeCallingUid(uid_t uid) {
76         mCallingUid = IPCThreadState::self()->getCallingUid();
77         mCallingPid = IPCThreadState::self()->getCallingPid();
78         if (mCallingUid == uid) {
79             return;
80         }
81         mChangedUid = uid;
82         int64_t token = ((int64_t)mChangedUid << 32) | mCallingPid;
83         IPCThreadState::self()->restoreCallingIdentity(token);
84     }
~ScopedChangeCallingUid()85     ~ScopedChangeCallingUid() {
86         if (mCallingUid == mChangedUid) {
87             return;
88         }
89         int64_t token = ((int64_t)mCallingUid << 32) | mCallingPid;
90         IPCThreadState::self()->restoreCallingIdentity(token);
91     }
92 
93 private:
94     uid_t mCallingUid;
95     uid_t mChangedUid;
96     pid_t mCallingPid;
97 };
98 
99 }  // namespace
100 
101 class WatchdogInternalHandlerTest : public ::testing::Test {
102 protected:
SetUp()103     virtual void SetUp() {
104         mMockWatchdogProcessService = sp<MockWatchdogProcessService>::make();
105         mMockWatchdogPerfService = sp<MockWatchdogPerfService>::make();
106         mMockWatchdogServiceHelper = sp<MockWatchdogServiceHelper>::make();
107         mMockIoOveruseMonitor = sp<MockIoOveruseMonitor>::make();
108         mMockWatchdogBinderMediator =
109                 sp<MockWatchdogBinderMediator>::make(mMockWatchdogProcessService,
110                                                      mMockWatchdogPerfService,
111                                                      mMockWatchdogServiceHelper);
112         mWatchdogInternalHandler =
113                 sp<WatchdogInternalHandler>::make(mMockWatchdogBinderMediator,
114                                                   mMockWatchdogServiceHelper,
115                                                   mMockWatchdogProcessService,
116                                                   mMockWatchdogPerfService, mMockIoOveruseMonitor);
117     }
TearDown()118     virtual void TearDown() {
119         mMockWatchdogBinderMediator.clear();
120         mMockWatchdogServiceHelper.clear();
121         mMockWatchdogProcessService.clear();
122         mMockWatchdogPerfService.clear();
123         mMockIoOveruseMonitor.clear();
124         mWatchdogInternalHandler.clear();
125         mScopedChangeCallingUid.clear();
126     }
127 
128     // Sets calling UID to imitate System's process.
setSystemCallingUid()129     void setSystemCallingUid() {
130         mScopedChangeCallingUid = sp<ScopedChangeCallingUid>::make(AID_SYSTEM);
131     }
132 
133     sp<MockWatchdogBinderMediator> mMockWatchdogBinderMediator;
134     sp<MockWatchdogServiceHelper> mMockWatchdogServiceHelper;
135     sp<MockWatchdogProcessService> mMockWatchdogProcessService;
136     sp<MockWatchdogPerfService> mMockWatchdogPerfService;
137     sp<MockIoOveruseMonitor> mMockIoOveruseMonitor;
138     sp<WatchdogInternalHandler> mWatchdogInternalHandler;
139     sp<ScopedChangeCallingUid> mScopedChangeCallingUid;
140 };
141 
TEST_F(WatchdogInternalHandlerTest,TestTerminate)142 TEST_F(WatchdogInternalHandlerTest, TestTerminate) {
143     ASSERT_NE(mWatchdogInternalHandler->mBinderMediator, nullptr);
144     ASSERT_NE(mWatchdogInternalHandler->mWatchdogServiceHelper, nullptr);
145     ASSERT_NE(mWatchdogInternalHandler->mWatchdogProcessService, nullptr);
146     ASSERT_NE(mWatchdogInternalHandler->mWatchdogPerfService, nullptr);
147     ASSERT_NE(mWatchdogInternalHandler->mIoOveruseMonitor, nullptr);
148 
149     mWatchdogInternalHandler->terminate();
150 
151     ASSERT_EQ(mWatchdogInternalHandler->mBinderMediator, nullptr);
152     ASSERT_EQ(mWatchdogInternalHandler->mWatchdogServiceHelper, nullptr);
153     ASSERT_EQ(mWatchdogInternalHandler->mWatchdogProcessService, nullptr);
154     ASSERT_EQ(mWatchdogInternalHandler->mWatchdogPerfService, nullptr);
155     ASSERT_EQ(mWatchdogInternalHandler->mIoOveruseMonitor, nullptr);
156 }
157 
TEST_F(WatchdogInternalHandlerTest,TestDump)158 TEST_F(WatchdogInternalHandlerTest, TestDump) {
159     EXPECT_CALL(*mMockWatchdogBinderMediator, dump(-1, _)).WillOnce(Return(OK));
160     ASSERT_EQ(mWatchdogInternalHandler->dump(-1, Vector<String16>()), OK);
161 }
162 
TEST_F(WatchdogInternalHandlerTest,TestRegisterCarWatchdogService)163 TEST_F(WatchdogInternalHandlerTest, TestRegisterCarWatchdogService) {
164     setSystemCallingUid();
165     sp<ICarWatchdogServiceForSystem> service = sp<ICarWatchdogServiceForSystemDefault>::make();
166     EXPECT_CALL(*mMockWatchdogServiceHelper, registerService(service))
167             .WillOnce(Return(Status::ok()));
168     EXPECT_CALL(*mMockWatchdogServiceHelper, registerService(service))
169             .WillOnce(Return(Status::ok()));
170 
171     Status status = mWatchdogInternalHandler->registerCarWatchdogService(service);
172 
173     ASSERT_TRUE(status.isOk()) << status;
174 }
175 
TEST_F(WatchdogInternalHandlerTest,TestErrorOnRegisterCarWatchdogServiceWithNonSystemCallingUid)176 TEST_F(WatchdogInternalHandlerTest, TestErrorOnRegisterCarWatchdogServiceWithNonSystemCallingUid) {
177     sp<ICarWatchdogServiceForSystem> service = sp<ICarWatchdogServiceForSystemDefault>::make();
178     EXPECT_CALL(*mMockWatchdogServiceHelper, registerService(_)).Times(0);
179 
180     Status status = mWatchdogInternalHandler->registerCarWatchdogService(service);
181 
182     ASSERT_FALSE(status.isOk()) << status;
183 }
184 
TEST_F(WatchdogInternalHandlerTest,TestErrorOnRegisterCarWatchdogServiceWithWatchdogServiceHelperError)185 TEST_F(WatchdogInternalHandlerTest,
186        TestErrorOnRegisterCarWatchdogServiceWithWatchdogServiceHelperError) {
187     setSystemCallingUid();
188     sp<ICarWatchdogServiceForSystem> service = sp<ICarWatchdogServiceForSystemDefault>::make();
189     EXPECT_CALL(*mMockWatchdogServiceHelper, registerService(service))
190             .WillOnce(Return(Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Illegal state")));
191 
192     Status status = mWatchdogInternalHandler->registerCarWatchdogService(service);
193 
194     ASSERT_FALSE(status.isOk()) << status;
195 }
196 
TEST_F(WatchdogInternalHandlerTest,TestUnregisterCarWatchdogService)197 TEST_F(WatchdogInternalHandlerTest, TestUnregisterCarWatchdogService) {
198     setSystemCallingUid();
199     sp<ICarWatchdogServiceForSystem> service = sp<ICarWatchdogServiceForSystemDefault>::make();
200     EXPECT_CALL(*mMockWatchdogServiceHelper, unregisterService(service))
201             .WillOnce(Return(Status::ok()));
202     Status status = mWatchdogInternalHandler->unregisterCarWatchdogService(service);
203     ASSERT_TRUE(status.isOk()) << status;
204 }
205 
TEST_F(WatchdogInternalHandlerTest,TestErrorOnUnregisterCarWatchdogServiceWithNonSystemCallingUid)206 TEST_F(WatchdogInternalHandlerTest,
207        TestErrorOnUnregisterCarWatchdogServiceWithNonSystemCallingUid) {
208     sp<ICarWatchdogServiceForSystem> service = sp<ICarWatchdogServiceForSystemDefault>::make();
209     EXPECT_CALL(*mMockWatchdogServiceHelper, unregisterService(service)).Times(0);
210     Status status = mWatchdogInternalHandler->unregisterCarWatchdogService(service);
211     ASSERT_FALSE(status.isOk()) << status;
212 }
TEST_F(WatchdogInternalHandlerTest,TestErrorOnUnregisterCarWatchdogServiceWithWatchdogServiceHelperError)213 TEST_F(WatchdogInternalHandlerTest,
214        TestErrorOnUnregisterCarWatchdogServiceWithWatchdogServiceHelperError) {
215     setSystemCallingUid();
216     sp<ICarWatchdogServiceForSystem> service = sp<ICarWatchdogServiceForSystemDefault>::make();
217     EXPECT_CALL(*mMockWatchdogServiceHelper, unregisterService(service))
218             .WillOnce(Return(
219                     Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Illegal argument")));
220     Status status = mWatchdogInternalHandler->unregisterCarWatchdogService(service);
221     ASSERT_FALSE(status.isOk()) << status;
222 }
223 
TEST_F(WatchdogInternalHandlerTest,TestRegisterMonitor)224 TEST_F(WatchdogInternalHandlerTest, TestRegisterMonitor) {
225     setSystemCallingUid();
226     sp<aawi::ICarWatchdogMonitor> monitor = sp<aawi::ICarWatchdogMonitorDefault>::make();
227     EXPECT_CALL(*mMockWatchdogProcessService, registerMonitor(monitor))
228             .WillOnce(Return(Status::ok()));
229     Status status = mWatchdogInternalHandler->registerMonitor(monitor);
230     ASSERT_TRUE(status.isOk()) << status;
231 }
232 
TEST_F(WatchdogInternalHandlerTest,TestErrorOnRegisterMonitorWithNonSystemCallingUid)233 TEST_F(WatchdogInternalHandlerTest, TestErrorOnRegisterMonitorWithNonSystemCallingUid) {
234     sp<aawi::ICarWatchdogMonitor> monitor = sp<aawi::ICarWatchdogMonitorDefault>::make();
235     EXPECT_CALL(*mMockWatchdogProcessService, registerMonitor(monitor)).Times(0);
236     Status status = mWatchdogInternalHandler->registerMonitor(monitor);
237     ASSERT_FALSE(status.isOk()) << status;
238 }
239 
TEST_F(WatchdogInternalHandlerTest,TestUnregisterMonitor)240 TEST_F(WatchdogInternalHandlerTest, TestUnregisterMonitor) {
241     setSystemCallingUid();
242     sp<aawi::ICarWatchdogMonitor> monitor = sp<aawi::ICarWatchdogMonitorDefault>::make();
243     EXPECT_CALL(*mMockWatchdogProcessService, unregisterMonitor(monitor))
244             .WillOnce(Return(Status::ok()));
245     Status status = mWatchdogInternalHandler->unregisterMonitor(monitor);
246     ASSERT_TRUE(status.isOk()) << status;
247 }
248 
TEST_F(WatchdogInternalHandlerTest,TestErrorOnUnregisterMonitorWithNonSystemCallingUid)249 TEST_F(WatchdogInternalHandlerTest, TestErrorOnUnregisterMonitorWithNonSystemCallingUid) {
250     sp<aawi::ICarWatchdogMonitor> monitor = sp<aawi::ICarWatchdogMonitorDefault>::make();
251     EXPECT_CALL(*mMockWatchdogProcessService, unregisterMonitor(monitor)).Times(0);
252     Status status = mWatchdogInternalHandler->unregisterMonitor(monitor);
253     ASSERT_FALSE(status.isOk()) << status;
254 }
255 
TEST_F(WatchdogInternalHandlerTest,TestCarWatchdogServiceAlive)256 TEST_F(WatchdogInternalHandlerTest, TestCarWatchdogServiceAlive) {
257     setSystemCallingUid();
258     sp<ICarWatchdogServiceForSystem> service = sp<ICarWatchdogServiceForSystemDefault>::make();
259     std::vector clientsNotResponding = {123};
260     EXPECT_CALL(*mMockWatchdogProcessService,
261                 tellCarWatchdogServiceAlive(service, clientsNotResponding, 456))
262             .WillOnce(Return(Status::ok()));
263     Status status =
264             mWatchdogInternalHandler->tellCarWatchdogServiceAlive(service, clientsNotResponding,
265                                                                   456);
266     ASSERT_TRUE(status.isOk()) << status;
267 }
268 
TEST_F(WatchdogInternalHandlerTest,TestErrorOnCarWatchdogServiceWithNonSystemCallingUid)269 TEST_F(WatchdogInternalHandlerTest, TestErrorOnCarWatchdogServiceWithNonSystemCallingUid) {
270     sp<ICarWatchdogServiceForSystem> service = sp<ICarWatchdogServiceForSystemDefault>::make();
271     std::vector clientsNotResponding = {123};
272     EXPECT_CALL(*mMockWatchdogProcessService, tellCarWatchdogServiceAlive(_, _, _)).Times(0);
273     Status status =
274             mWatchdogInternalHandler->tellCarWatchdogServiceAlive(service, clientsNotResponding,
275                                                                   456);
276     ASSERT_FALSE(status.isOk()) << status;
277 }
278 
TEST_F(WatchdogInternalHandlerTest,TestTellDumpFinished)279 TEST_F(WatchdogInternalHandlerTest, TestTellDumpFinished) {
280     setSystemCallingUid();
281     sp<aawi::ICarWatchdogMonitor> monitor = sp<aawi::ICarWatchdogMonitorDefault>::make();
282     EXPECT_CALL(*mMockWatchdogProcessService, tellDumpFinished(monitor, 456))
283             .WillOnce(Return(Status::ok()));
284     Status status = mWatchdogInternalHandler->tellDumpFinished(monitor, 456);
285     ASSERT_TRUE(status.isOk()) << status;
286 }
287 
TEST_F(WatchdogInternalHandlerTest,TestErrorOnTellDumpFinishedWithNonSystemCallingUid)288 TEST_F(WatchdogInternalHandlerTest, TestErrorOnTellDumpFinishedWithNonSystemCallingUid) {
289     sp<aawi::ICarWatchdogMonitor> monitor = sp<aawi::ICarWatchdogMonitorDefault>::make();
290     EXPECT_CALL(*mMockWatchdogProcessService, tellDumpFinished(_, _)).Times(0);
291     Status status = mWatchdogInternalHandler->tellDumpFinished(monitor, 456);
292     ASSERT_FALSE(status.isOk()) << status;
293 }
294 
TEST_F(WatchdogInternalHandlerTest,TestNotifyPowerCycleChangeToShutdownPrepare)295 TEST_F(WatchdogInternalHandlerTest, TestNotifyPowerCycleChangeToShutdownPrepare) {
296     setSystemCallingUid();
297     EXPECT_CALL(*mMockWatchdogProcessService, setEnabled(/*isEnabled=*/false)).Times(1);
298     Status status =
299             mWatchdogInternalHandler
300                     ->notifySystemStateChange(aawi::StateType::POWER_CYCLE,
301                                               static_cast<int32_t>(
302                                                       PowerCycle::POWER_CYCLE_SHUTDOWN_PREPARE),
303                                               -1);
304     ASSERT_TRUE(status.isOk()) << status;
305 }
306 
TEST_F(WatchdogInternalHandlerTest,TestNotifyPowerCycleChangeToShutdownEnter)307 TEST_F(WatchdogInternalHandlerTest, TestNotifyPowerCycleChangeToShutdownEnter) {
308     setSystemCallingUid();
309     EXPECT_CALL(*mMockWatchdogProcessService, setEnabled(/*isEnabled=*/false)).Times(1);
310     Status status =
311             mWatchdogInternalHandler
312                     ->notifySystemStateChange(aawi::StateType::POWER_CYCLE,
313                                               static_cast<int32_t>(
314                                                       PowerCycle::POWER_CYCLE_SHUTDOWN_ENTER),
315                                               -1);
316     ASSERT_TRUE(status.isOk()) << status;
317 }
318 
TEST_F(WatchdogInternalHandlerTest,TestNotifyPowerCycleChangeToResume)319 TEST_F(WatchdogInternalHandlerTest, TestNotifyPowerCycleChangeToResume) {
320     setSystemCallingUid();
321     EXPECT_CALL(*mMockWatchdogProcessService, setEnabled(/*isEnabled=*/true)).Times(1);
322     EXPECT_CALL(*mMockWatchdogPerfService, setSystemState(SystemState::NORMAL_MODE)).Times(1);
323     Status status =
324             mWatchdogInternalHandler
325                     ->notifySystemStateChange(aawi::StateType::POWER_CYCLE,
326                                               static_cast<int32_t>(PowerCycle::POWER_CYCLE_RESUME),
327                                               -1);
328     ASSERT_TRUE(status.isOk()) << status;
329 }
330 
TEST_F(WatchdogInternalHandlerTest,TestErrorOnNotifyPowerCycleChangeWithInvalidArgs)331 TEST_F(WatchdogInternalHandlerTest, TestErrorOnNotifyPowerCycleChangeWithInvalidArgs) {
332     EXPECT_CALL(*mMockWatchdogProcessService, setEnabled(_)).Times(0);
333     EXPECT_CALL(*mMockWatchdogPerfService, setSystemState(_)).Times(0);
334     aawi::StateType type = aawi::StateType::POWER_CYCLE;
335 
336     Status status = mWatchdogInternalHandler->notifySystemStateChange(type, -1, -1);
337     ASSERT_FALSE(status.isOk()) << status;
338 
339     status = mWatchdogInternalHandler->notifySystemStateChange(type, 3000, -1);
340     ASSERT_FALSE(status.isOk()) << status;
341 }
342 
TEST_F(WatchdogInternalHandlerTest,TestNotifyGarageModeOn)343 TEST_F(WatchdogInternalHandlerTest, TestNotifyGarageModeOn) {
344     setSystemCallingUid();
345     EXPECT_CALL(*mMockWatchdogPerfService, setSystemState(SystemState::GARAGE_MODE)).Times(1);
346     Status status =
347             mWatchdogInternalHandler->notifySystemStateChange(aawi::StateType::GARAGE_MODE,
348                                                               static_cast<int32_t>(
349                                                                       GarageMode::GARAGE_MODE_ON),
350                                                               -1);
351     ASSERT_TRUE(status.isOk()) << status;
352 }
353 
TEST_F(WatchdogInternalHandlerTest,TestNotifyGarageModeOff)354 TEST_F(WatchdogInternalHandlerTest, TestNotifyGarageModeOff) {
355     setSystemCallingUid();
356     EXPECT_CALL(*mMockWatchdogPerfService, setSystemState(SystemState::NORMAL_MODE)).Times(1);
357     Status status =
358             mWatchdogInternalHandler->notifySystemStateChange(aawi::StateType::GARAGE_MODE,
359                                                               static_cast<int32_t>(
360                                                                       GarageMode::GARAGE_MODE_OFF),
361                                                               -1);
362     ASSERT_TRUE(status.isOk()) << status;
363 }
364 
TEST_F(WatchdogInternalHandlerTest,TestNotifyUserStateChangeWithStartedUser)365 TEST_F(WatchdogInternalHandlerTest, TestNotifyUserStateChangeWithStartedUser) {
366     setSystemCallingUid();
367     aawi::StateType type = aawi::StateType::USER_STATE;
368     EXPECT_CALL(*mMockWatchdogProcessService, notifyUserStateChange(234567, /*isStarted=*/true));
369     Status status = mWatchdogInternalHandler
370                             ->notifySystemStateChange(type, 234567,
371                                                       static_cast<int32_t>(
372                                                               aawi::UserState::USER_STATE_STARTED));
373     ASSERT_TRUE(status.isOk()) << status;
374 }
375 
TEST_F(WatchdogInternalHandlerTest,TestNotifyUserStateChangeWithStoppedUser)376 TEST_F(WatchdogInternalHandlerTest, TestNotifyUserStateChangeWithStoppedUser) {
377     setSystemCallingUid();
378     aawi::StateType type = aawi::StateType::USER_STATE;
379     EXPECT_CALL(*mMockWatchdogProcessService, notifyUserStateChange(234567, /*isStarted=*/false));
380     Status status = mWatchdogInternalHandler
381                             ->notifySystemStateChange(type, 234567,
382                                                       static_cast<int32_t>(
383                                                               aawi::UserState::USER_STATE_STOPPED));
384     ASSERT_TRUE(status.isOk()) << status;
385 }
386 
TEST_F(WatchdogInternalHandlerTest,TestNotifyUserStateChangeWithRemovedUser)387 TEST_F(WatchdogInternalHandlerTest, TestNotifyUserStateChangeWithRemovedUser) {
388     setSystemCallingUid();
389     aawi::StateType type = aawi::StateType::USER_STATE;
390     EXPECT_CALL(*mMockIoOveruseMonitor, removeStatsForUser(/*userId=*/234567));
391     Status status = mWatchdogInternalHandler
392                             ->notifySystemStateChange(type, 234567,
393                                                       static_cast<int32_t>(
394                                                               aawi::UserState::USER_STATE_REMOVED));
395     ASSERT_TRUE(status.isOk()) << status;
396 }
397 
TEST_F(WatchdogInternalHandlerTest,TestErrorOnNotifyUserStateChangeWithInvalidArgs)398 TEST_F(WatchdogInternalHandlerTest, TestErrorOnNotifyUserStateChangeWithInvalidArgs) {
399     EXPECT_CALL(*mMockWatchdogProcessService, notifyUserStateChange(_, _)).Times(0);
400     aawi::StateType type = aawi::StateType::USER_STATE;
401 
402     Status status = mWatchdogInternalHandler->notifySystemStateChange(type, 234567, -1);
403     ASSERT_FALSE(status.isOk()) << status;
404 
405     status = mWatchdogInternalHandler->notifySystemStateChange(type, 234567, 3000);
406     ASSERT_FALSE(status.isOk()) << status;
407 }
408 
TEST_F(WatchdogInternalHandlerTest,TestNotifyBootPhaseChange)409 TEST_F(WatchdogInternalHandlerTest, TestNotifyBootPhaseChange) {
410     setSystemCallingUid();
411     aawi::StateType type = aawi::StateType::BOOT_PHASE;
412     EXPECT_CALL(*mMockWatchdogPerfService, onBootFinished()).WillOnce(Return(Result<void>()));
413     Status status =
414             mWatchdogInternalHandler
415                     ->notifySystemStateChange(type,
416                                               static_cast<int32_t>(aawi::BootPhase::BOOT_COMPLETED),
417                                               -1);
418     ASSERT_TRUE(status.isOk()) << status;
419 }
420 
TEST_F(WatchdogInternalHandlerTest,TestNotifyBootPhaseChangeWithNonBootCompletedPhase)421 TEST_F(WatchdogInternalHandlerTest, TestNotifyBootPhaseChangeWithNonBootCompletedPhase) {
422     setSystemCallingUid();
423     aawi::StateType type = aawi::StateType::BOOT_PHASE;
424     EXPECT_CALL(*mMockWatchdogPerfService, onBootFinished()).Times(0);
425     Status status = mWatchdogInternalHandler->notifySystemStateChange(type, 0, -1);
426     ASSERT_TRUE(status.isOk()) << status;
427 }
428 
TEST_F(WatchdogInternalHandlerTest,TestErrorOnNotifySystemStateChangeWithNonSystemCallingUid)429 TEST_F(WatchdogInternalHandlerTest, TestErrorOnNotifySystemStateChangeWithNonSystemCallingUid) {
430     aawi::StateType type = aawi::StateType::POWER_CYCLE;
431     EXPECT_CALL(*mMockWatchdogProcessService, setEnabled(_)).Times(0);
432     EXPECT_CALL(*mMockWatchdogPerfService, setSystemState(_)).Times(0);
433     Status status =
434             mWatchdogInternalHandler
435                     ->notifySystemStateChange(type,
436                                               static_cast<int32_t>(
437                                                       PowerCycle::POWER_CYCLE_SHUTDOWN_PREPARE),
438                                               -1);
439     ASSERT_FALSE(status.isOk()) << status;
440 }
441 
TEST_F(WatchdogInternalHandlerTest,TestUpdateResourceOveruseConfigurations)442 TEST_F(WatchdogInternalHandlerTest, TestUpdateResourceOveruseConfigurations) {
443     setSystemCallingUid();
444     EXPECT_CALL(*mMockIoOveruseMonitor, updateResourceOveruseConfigurations(_))
445             .WillOnce(Return(Result<void>()));
446     Status status = mWatchdogInternalHandler->updateResourceOveruseConfigurations(
447             std::vector<ResourceOveruseConfiguration>{});
448     ASSERT_TRUE(status.isOk()) << status;
449 }
450 
TEST_F(WatchdogInternalHandlerTest,TestErrorOnUpdateResourceOveruseConfigurationsWithNonSystemCallingUid)451 TEST_F(WatchdogInternalHandlerTest,
452        TestErrorOnUpdateResourceOveruseConfigurationsWithNonSystemCallingUid) {
453     EXPECT_CALL(*mMockIoOveruseMonitor, updateResourceOveruseConfigurations(_)).Times(0);
454     Status status = mWatchdogInternalHandler->updateResourceOveruseConfigurations(
455             std::vector<ResourceOveruseConfiguration>{});
456     ASSERT_FALSE(status.isOk()) << status;
457 }
458 
TEST_F(WatchdogInternalHandlerTest,TestGetResourceOveruseConfigurations)459 TEST_F(WatchdogInternalHandlerTest, TestGetResourceOveruseConfigurations) {
460     setSystemCallingUid();
461     std::vector<ResourceOveruseConfiguration> configs;
462     EXPECT_CALL(*mMockIoOveruseMonitor, getResourceOveruseConfigurations(Pointer(&configs)))
463             .WillOnce(Return(Result<void>()));
464     Status status = mWatchdogInternalHandler->getResourceOveruseConfigurations(&configs);
465     ASSERT_TRUE(status.isOk()) << status;
466 }
467 
TEST_F(WatchdogInternalHandlerTest,TestErrorOnGetResourceOveruseConfigurationsWithNonSystemCallingUid)468 TEST_F(WatchdogInternalHandlerTest,
469        TestErrorOnGetResourceOveruseConfigurationsWithNonSystemCallingUid) {
470     EXPECT_CALL(*mMockIoOveruseMonitor, getResourceOveruseConfigurations(_)).Times(0);
471     std::vector<ResourceOveruseConfiguration> configs;
472     Status status = mWatchdogInternalHandler->getResourceOveruseConfigurations(&configs);
473     ASSERT_FALSE(status.isOk()) << status;
474 }
475 
TEST_F(WatchdogInternalHandlerTest,TestControlProcessHealthCheck)476 TEST_F(WatchdogInternalHandlerTest, TestControlProcessHealthCheck) {
477     setSystemCallingUid();
478     EXPECT_CALL(*mMockWatchdogProcessService, setEnabled(/*isEnabled=*/true)).Times(1);
479     Status status = mWatchdogInternalHandler->controlProcessHealthCheck(false);
480     ASSERT_TRUE(status.isOk()) << status;
481 }
482 
TEST_F(WatchdogInternalHandlerTest,TestErrorOnControlProcessHealthCheckWithNonSystemCallingUid)483 TEST_F(WatchdogInternalHandlerTest, TestErrorOnControlProcessHealthCheckWithNonSystemCallingUid) {
484     EXPECT_CALL(*mMockWatchdogProcessService, setEnabled(_)).Times(0);
485     Status status = mWatchdogInternalHandler->controlProcessHealthCheck(false);
486     ASSERT_FALSE(status.isOk()) << status;
487 }
488 
489 }  // namespace watchdog
490 }  // namespace automotive
491 }  // namespace android
492