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 "ThreadPriorityController.h"
22 #include "WatchdogBinderMediator.h"
23 #include "WatchdogInternalHandler.h"
24 #include "WatchdogServiceHelper.h"
25
26 #include <aidl/android/automotive/watchdog/internal/BootPhase.h>
27 #include <aidl/android/automotive/watchdog/internal/GarageMode.h>
28 #include <aidl/android/automotive/watchdog/internal/PowerCycle.h>
29 #include <aidl/android/automotive/watchdog/internal/UserPackageIoUsageStats.h>
30 #include <aidl/android/automotive/watchdog/internal/UserState.h>
31 #include <android-base/result.h>
32 #include <binder/IPCThreadState.h>
33 #include <gmock/gmock.h>
34 #include <gtest/gtest.h>
35 #include <private/android_filesystem_config.h>
36 #include <utils/RefBase.h>
37
38 #include <errno.h>
39 #include <sched.h>
40 #include <unistd.h>
41
42 namespace android {
43 namespace automotive {
44 namespace watchdog {
45
46 using ::aidl::android::automotive::watchdog::internal::BootPhase;
47 using ::aidl::android::automotive::watchdog::internal::GarageMode;
48 using ::aidl::android::automotive::watchdog::internal::ICarWatchdogMonitor;
49 using ::aidl::android::automotive::watchdog::internal::ICarWatchdogMonitorDefault;
50 using ::aidl::android::automotive::watchdog::internal::ICarWatchdogServiceForSystem;
51 using ::aidl::android::automotive::watchdog::internal::ICarWatchdogServiceForSystemDefault;
52 using ::aidl::android::automotive::watchdog::internal::PowerCycle;
53 using ::aidl::android::automotive::watchdog::internal::ProcessIdentifier;
54 using ::aidl::android::automotive::watchdog::internal::ResourceOveruseConfiguration;
55 using ::aidl::android::automotive::watchdog::internal::StateType;
56 using ::aidl::android::automotive::watchdog::internal::ThreadPolicyWithPriority;
57 using ::aidl::android::automotive::watchdog::internal::UserPackageIoUsageStats;
58 using ::aidl::android::automotive::watchdog::internal::UserState;
59 using ::android::sp;
60 using ::android::String16;
61 using ::android::base::Result;
62 using ::ndk::ScopedAStatus;
63 using ::ndk::SharedRefBase;
64 using ::ndk::SpAIBinder;
65 using ::testing::_;
66 using ::testing::ByMove;
67 using ::testing::DoAll;
68 using ::testing::Eq;
69 using ::testing::Pointer;
70 using ::testing::Return;
71 using ::testing::SaveArg;
72
73 namespace {
74
75 constexpr const char kFailOnNonSystemCallingUidMessage[] =
76 "should fail with non-system calling uid";
77 constexpr const char kFailOnWatchdogServiceHelperErrMessage[] =
78 "should fail on watchdog service helper error";
79
80 class ScopedChangeCallingUid final : public RefBase {
81 public:
ScopedChangeCallingUid(uid_t uid)82 explicit ScopedChangeCallingUid(uid_t uid) {
83 mCallingUid = IPCThreadState::self()->getCallingUid();
84 mCallingPid = IPCThreadState::self()->getCallingPid();
85 if (mCallingUid == uid) {
86 return;
87 }
88 mChangedUid = uid;
89 int64_t token = ((int64_t)mChangedUid << 32) | mCallingPid;
90 IPCThreadState::self()->restoreCallingIdentity(token);
91 }
~ScopedChangeCallingUid()92 ~ScopedChangeCallingUid() {
93 if (mCallingUid == mChangedUid) {
94 return;
95 }
96 int64_t token = ((int64_t)mCallingUid << 32) | mCallingPid;
97 IPCThreadState::self()->restoreCallingIdentity(token);
98 }
99
100 private:
101 uid_t mCallingUid;
102 uid_t mChangedUid;
103 pid_t mCallingPid;
104 };
105
106 MATCHER_P(PriorityEq, priority, "") {
107 return (arg->sched_priority) == priority;
108 }
109
110 class MockThreadPriorityController : public ThreadPriorityControllerInterface {
111 public:
112 MOCK_METHOD(Result<void>, setThreadPriority,
113 (int pid, int tid, int uid, int policy, int priority), (override));
114 MOCK_METHOD(Result<void>, getThreadPriority,
115 (int pid, int tid, int uid, ThreadPolicyWithPriority* result), (override));
116 };
117
118 } // namespace
119
120 namespace internal {
121
122 class WatchdogInternalHandlerPeer final {
123 public:
WatchdogInternalHandlerPeer(WatchdogInternalHandler * handler)124 explicit WatchdogInternalHandlerPeer(WatchdogInternalHandler* handler) : mHandler(handler) {}
125
setThreadPriorityController(std::unique_ptr<ThreadPriorityControllerInterface> controller)126 void setThreadPriorityController(
127 std::unique_ptr<ThreadPriorityControllerInterface> controller) {
128 mHandler->setThreadPriorityController(std::move(controller));
129 }
130
131 private:
132 WatchdogInternalHandler* mHandler;
133 };
134
135 } // namespace internal
136
137 class WatchdogInternalHandlerTest : public ::testing::Test {
138 protected:
SetUp()139 virtual void SetUp() {
140 mMockWatchdogProcessService = sp<MockWatchdogProcessService>::make();
141 mMockWatchdogPerfService = sp<MockWatchdogPerfService>::make();
142 mMockWatchdogServiceHelper = sp<MockWatchdogServiceHelper>::make();
143 mMockIoOveruseMonitor = sp<MockIoOveruseMonitor>::make();
144 mWatchdogInternalHandler =
145 SharedRefBase::make<WatchdogInternalHandler>(mMockWatchdogServiceHelper,
146 mMockWatchdogProcessService,
147 mMockWatchdogPerfService,
148 mMockIoOveruseMonitor);
149 internal::WatchdogInternalHandlerPeer peer(mWatchdogInternalHandler.get());
150 std::unique_ptr<MockThreadPriorityController> threadPriorityController =
151 std::make_unique<MockThreadPriorityController>();
152 mThreadPriorityController = threadPriorityController.get();
153 peer.setThreadPriorityController(std::move(threadPriorityController));
154 }
TearDown()155 virtual void TearDown() {
156 mMockWatchdogServiceHelper.clear();
157 mMockWatchdogProcessService.clear();
158 mMockWatchdogPerfService.clear();
159 mMockIoOveruseMonitor.clear();
160 mWatchdogInternalHandler.reset();
161 mScopedChangeCallingUid.clear();
162 }
163
164 // Sets calling UID to imitate System's process.
setSystemCallingUid()165 void setSystemCallingUid() {
166 mScopedChangeCallingUid = sp<ScopedChangeCallingUid>::make(AID_SYSTEM);
167 }
168
169 sp<MockWatchdogServiceHelper> mMockWatchdogServiceHelper;
170 sp<MockWatchdogProcessService> mMockWatchdogProcessService;
171 sp<MockWatchdogPerfService> mMockWatchdogPerfService;
172 sp<MockIoOveruseMonitor> mMockIoOveruseMonitor;
173 std::shared_ptr<WatchdogInternalHandler> mWatchdogInternalHandler;
174 sp<ScopedChangeCallingUid> mScopedChangeCallingUid;
175 MockThreadPriorityController* mThreadPriorityController;
176 };
177
TEST_F(WatchdogInternalHandlerTest,TestTerminate)178 TEST_F(WatchdogInternalHandlerTest, TestTerminate) {
179 ASSERT_NE(mWatchdogInternalHandler->mWatchdogServiceHelper, nullptr);
180 ASSERT_NE(mWatchdogInternalHandler->mWatchdogProcessService, nullptr);
181 ASSERT_NE(mWatchdogInternalHandler->mWatchdogPerfService, nullptr);
182 ASSERT_NE(mWatchdogInternalHandler->mIoOveruseMonitor, nullptr);
183
184 mWatchdogInternalHandler->terminate();
185
186 ASSERT_EQ(mWatchdogInternalHandler->mWatchdogServiceHelper, nullptr);
187 ASSERT_EQ(mWatchdogInternalHandler->mWatchdogProcessService, nullptr);
188 ASSERT_EQ(mWatchdogInternalHandler->mWatchdogPerfService, nullptr);
189 ASSERT_EQ(mWatchdogInternalHandler->mIoOveruseMonitor, nullptr);
190 }
191
TEST_F(WatchdogInternalHandlerTest,TestDump)192 TEST_F(WatchdogInternalHandlerTest, TestDump) {
193 ASSERT_EQ(mWatchdogInternalHandler->dump(-1, /*args=*/nullptr, /*numArgs=*/0), OK);
194 }
195
TEST_F(WatchdogInternalHandlerTest,TestRegisterCarWatchdogService)196 TEST_F(WatchdogInternalHandlerTest, TestRegisterCarWatchdogService) {
197 setSystemCallingUid();
198
199 EXPECT_CALL(*mMockIoOveruseMonitor, isInitialized()).WillOnce(Return(false));
200 EXPECT_CALL(*mMockWatchdogPerfService, registerDataProcessor(Eq(mMockIoOveruseMonitor)))
201 .WillOnce(Return(Result<void>()));
202 EXPECT_CALL(*mMockWatchdogPerfService, onCarWatchdogServiceRegistered()).Times(1);
203
204 std::shared_ptr<ICarWatchdogServiceForSystem> service =
205 SharedRefBase::make<ICarWatchdogServiceForSystemDefault>();
206 EXPECT_CALL(*mMockWatchdogServiceHelper, registerService(service))
207 .WillOnce(Return(ByMove(ScopedAStatus::ok())));
208
209 auto status = mWatchdogInternalHandler->registerCarWatchdogService(service);
210
211 ASSERT_TRUE(status.isOk()) << status.getMessage();
212 }
213
TEST_F(WatchdogInternalHandlerTest,TestErrorOnRegisterCarWatchdogServiceWithNonSystemCallingUid)214 TEST_F(WatchdogInternalHandlerTest, TestErrorOnRegisterCarWatchdogServiceWithNonSystemCallingUid) {
215 EXPECT_CALL(*mMockWatchdogServiceHelper, registerService(_)).Times(0);
216
217 std::shared_ptr<ICarWatchdogServiceForSystem> service =
218 SharedRefBase::make<ICarWatchdogServiceForSystemDefault>();
219 ASSERT_FALSE(mWatchdogInternalHandler->registerCarWatchdogService(service).isOk())
220 << "registerCarWatchdogService " << kFailOnNonSystemCallingUidMessage;
221 }
222
TEST_F(WatchdogInternalHandlerTest,TestErrorOnRegisterCarWatchdogServiceWithWatchdogServiceHelperError)223 TEST_F(WatchdogInternalHandlerTest,
224 TestErrorOnRegisterCarWatchdogServiceWithWatchdogServiceHelperError) {
225 setSystemCallingUid();
226
227 std::shared_ptr<ICarWatchdogServiceForSystem> service =
228 SharedRefBase::make<ICarWatchdogServiceForSystemDefault>();
229 EXPECT_CALL(*mMockWatchdogServiceHelper, registerService(service))
230 .WillOnce(Return(ByMove(ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE,
231 "Illegal state"))));
232
233 ASSERT_FALSE(mWatchdogInternalHandler->registerCarWatchdogService(service).isOk())
234 << "registerCarWatchdogService " << kFailOnWatchdogServiceHelperErrMessage;
235 }
236
TEST_F(WatchdogInternalHandlerTest,TestUnregisterCarWatchdogService)237 TEST_F(WatchdogInternalHandlerTest, TestUnregisterCarWatchdogService) {
238 setSystemCallingUid();
239
240 std::shared_ptr<ICarWatchdogServiceForSystem> service =
241 SharedRefBase::make<ICarWatchdogServiceForSystemDefault>();
242 EXPECT_CALL(*mMockWatchdogServiceHelper, unregisterService(service))
243 .WillOnce(Return(ByMove(ScopedAStatus::ok())));
244
245 auto status = mWatchdogInternalHandler->unregisterCarWatchdogService(service);
246
247 ASSERT_TRUE(status.isOk()) << status.getMessage();
248 }
249
TEST_F(WatchdogInternalHandlerTest,TestErrorOnUnregisterCarWatchdogServiceWithNonSystemCallingUid)250 TEST_F(WatchdogInternalHandlerTest,
251 TestErrorOnUnregisterCarWatchdogServiceWithNonSystemCallingUid) {
252 std::shared_ptr<ICarWatchdogServiceForSystem> service =
253 SharedRefBase::make<ICarWatchdogServiceForSystemDefault>();
254 EXPECT_CALL(*mMockWatchdogServiceHelper, unregisterService(service)).Times(0);
255
256 ASSERT_FALSE(mWatchdogInternalHandler->unregisterCarWatchdogService(service).isOk())
257 << "unregisterCarWatchdogService " << kFailOnNonSystemCallingUidMessage;
258 }
TEST_F(WatchdogInternalHandlerTest,TestErrorOnUnregisterCarWatchdogServiceWithWatchdogServiceHelperError)259 TEST_F(WatchdogInternalHandlerTest,
260 TestErrorOnUnregisterCarWatchdogServiceWithWatchdogServiceHelperError) {
261 setSystemCallingUid();
262
263 std::shared_ptr<ICarWatchdogServiceForSystem> service =
264 SharedRefBase::make<ICarWatchdogServiceForSystemDefault>();
265 EXPECT_CALL(*mMockWatchdogServiceHelper, unregisterService(service))
266 .WillOnce(Return(
267 ByMove(ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
268 "Illegal argument"))));
269
270 ASSERT_FALSE(mWatchdogInternalHandler->unregisterCarWatchdogService(service).isOk())
271 << "unregisterCarWatchdogService " << kFailOnWatchdogServiceHelperErrMessage;
272 }
273
TEST_F(WatchdogInternalHandlerTest,TestRegisterMonitor)274 TEST_F(WatchdogInternalHandlerTest, TestRegisterMonitor) {
275 setSystemCallingUid();
276
277 std::shared_ptr<ICarWatchdogMonitor> monitor =
278 SharedRefBase::make<ICarWatchdogMonitorDefault>();
279 EXPECT_CALL(*mMockWatchdogProcessService, registerMonitor(monitor))
280 .WillOnce(Return(ByMove(ScopedAStatus::ok())));
281
282 auto status = mWatchdogInternalHandler->registerMonitor(monitor);
283
284 ASSERT_TRUE(status.isOk()) << status.getMessage();
285 }
286
TEST_F(WatchdogInternalHandlerTest,TestErrorOnRegisterMonitorWithNonSystemCallingUid)287 TEST_F(WatchdogInternalHandlerTest, TestErrorOnRegisterMonitorWithNonSystemCallingUid) {
288 std::shared_ptr<ICarWatchdogMonitor> monitor =
289 SharedRefBase::make<ICarWatchdogMonitorDefault>();
290 EXPECT_CALL(*mMockWatchdogProcessService, registerMonitor(monitor)).Times(0);
291
292 ASSERT_FALSE(mWatchdogInternalHandler->registerMonitor(monitor).isOk())
293 << "registerMonitor " << kFailOnNonSystemCallingUidMessage;
294 }
295
TEST_F(WatchdogInternalHandlerTest,TestUnregisterMonitor)296 TEST_F(WatchdogInternalHandlerTest, TestUnregisterMonitor) {
297 setSystemCallingUid();
298
299 std::shared_ptr<ICarWatchdogMonitor> monitor =
300 SharedRefBase::make<ICarWatchdogMonitorDefault>();
301 EXPECT_CALL(*mMockWatchdogProcessService, unregisterMonitor(monitor))
302 .WillOnce(Return(ByMove(ScopedAStatus::ok())));
303
304 auto status = mWatchdogInternalHandler->unregisterMonitor(monitor);
305
306 ASSERT_TRUE(status.isOk()) << status.getMessage();
307 }
308
TEST_F(WatchdogInternalHandlerTest,TestErrorOnUnregisterMonitorWithNonSystemCallingUid)309 TEST_F(WatchdogInternalHandlerTest, TestErrorOnUnregisterMonitorWithNonSystemCallingUid) {
310 std::shared_ptr<ICarWatchdogMonitor> monitor =
311 SharedRefBase::make<ICarWatchdogMonitorDefault>();
312 EXPECT_CALL(*mMockWatchdogProcessService, unregisterMonitor(monitor)).Times(0);
313
314 ASSERT_FALSE(mWatchdogInternalHandler->unregisterMonitor(monitor).isOk())
315 << "unregisterMonitor " << kFailOnNonSystemCallingUidMessage;
316 }
317
TEST_F(WatchdogInternalHandlerTest,TestCarWatchdogServiceAlive)318 TEST_F(WatchdogInternalHandlerTest, TestCarWatchdogServiceAlive) {
319 setSystemCallingUid();
320
321 std::shared_ptr<ICarWatchdogServiceForSystem> service =
322 SharedRefBase::make<ICarWatchdogServiceForSystemDefault>();
323 std::vector<ProcessIdentifier> clientsNotResponding;
324 ProcessIdentifier processIdentifier;
325 processIdentifier.pid = 123;
326 clientsNotResponding.push_back(processIdentifier);
327 EXPECT_CALL(*mMockWatchdogProcessService,
328 tellCarWatchdogServiceAlive(service, clientsNotResponding, 456))
329 .WillOnce(Return(ByMove(ScopedAStatus::ok())));
330
331 auto status = mWatchdogInternalHandler->tellCarWatchdogServiceAlive(service,
332 clientsNotResponding, 456);
333
334 ASSERT_TRUE(status.isOk()) << status.getMessage();
335 }
336
TEST_F(WatchdogInternalHandlerTest,TestErrorOnCarWatchdogServiceWithNonSystemCallingUid)337 TEST_F(WatchdogInternalHandlerTest, TestErrorOnCarWatchdogServiceWithNonSystemCallingUid) {
338 EXPECT_CALL(*mMockWatchdogProcessService, tellCarWatchdogServiceAlive(_, _, _)).Times(0);
339
340 std::shared_ptr<ICarWatchdogServiceForSystem> service =
341 SharedRefBase::make<ICarWatchdogServiceForSystemDefault>();
342 std::vector<ProcessIdentifier> clientsNotResponding;
343 ProcessIdentifier processIdentifier;
344 processIdentifier.pid = 123;
345 clientsNotResponding.push_back(processIdentifier);
346 auto status = mWatchdogInternalHandler->tellCarWatchdogServiceAlive(service,
347 clientsNotResponding, 456);
348
349 ASSERT_FALSE(status.isOk()) << "tellCarWatchdogServiceAlive "
350 << kFailOnNonSystemCallingUidMessage;
351 }
352
TEST_F(WatchdogInternalHandlerTest,TestTellDumpFinished)353 TEST_F(WatchdogInternalHandlerTest, TestTellDumpFinished) {
354 setSystemCallingUid();
355
356 std::shared_ptr<ICarWatchdogMonitor> monitor =
357 SharedRefBase::make<ICarWatchdogMonitorDefault>();
358 ProcessIdentifier processIdentifier;
359 processIdentifier.pid = 456;
360 EXPECT_CALL(*mMockWatchdogProcessService, tellDumpFinished(monitor, processIdentifier))
361 .WillOnce(Return(ByMove(ScopedAStatus::ok())));
362
363 auto status = mWatchdogInternalHandler->tellDumpFinished(monitor, processIdentifier);
364
365 ASSERT_TRUE(status.isOk()) << status.getMessage();
366 }
367
TEST_F(WatchdogInternalHandlerTest,TestErrorOnTellDumpFinishedWithNonSystemCallingUid)368 TEST_F(WatchdogInternalHandlerTest, TestErrorOnTellDumpFinishedWithNonSystemCallingUid) {
369 EXPECT_CALL(*mMockWatchdogProcessService, tellDumpFinished(_, _)).Times(0);
370
371 ProcessIdentifier processIdentifier;
372 processIdentifier.pid = 456;
373 std::shared_ptr<ICarWatchdogMonitor> monitor =
374 SharedRefBase::make<ICarWatchdogMonitorDefault>();
375
376 ASSERT_FALSE(mWatchdogInternalHandler->tellDumpFinished(monitor, processIdentifier).isOk())
377 << "tellDumpFinished " << kFailOnNonSystemCallingUidMessage;
378 }
379
TEST_F(WatchdogInternalHandlerTest,TestNotifyPowerCycleChangeToShutdownPrepare)380 TEST_F(WatchdogInternalHandlerTest, TestNotifyPowerCycleChangeToShutdownPrepare) {
381 setSystemCallingUid();
382 EXPECT_CALL(*mMockWatchdogProcessService, setEnabled(/*isEnabled=*/false)).Times(1);
383
384 auto status =
385 mWatchdogInternalHandler
386 ->notifySystemStateChange(StateType::POWER_CYCLE,
387 static_cast<int32_t>(
388 PowerCycle::POWER_CYCLE_SHUTDOWN_PREPARE),
389 -1);
390
391 ASSERT_TRUE(status.isOk()) << status.getMessage();
392 }
393
TEST_F(WatchdogInternalHandlerTest,TestNotifyPowerCycleChangeToShutdownEnter)394 TEST_F(WatchdogInternalHandlerTest, TestNotifyPowerCycleChangeToShutdownEnter) {
395 setSystemCallingUid();
396 EXPECT_CALL(*mMockWatchdogProcessService, setEnabled(/*isEnabled=*/false)).Times(1);
397 EXPECT_CALL(*mMockWatchdogPerfService, onShutdownEnter()).Times(1);
398
399 auto status = mWatchdogInternalHandler
400 ->notifySystemStateChange(StateType::POWER_CYCLE,
401 static_cast<int32_t>(
402 PowerCycle::POWER_CYCLE_SHUTDOWN_ENTER),
403 -1);
404
405 ASSERT_TRUE(status.isOk()) << status.getMessage();
406 }
407
TEST_F(WatchdogInternalHandlerTest,TestNotifyPowerCycleChangeToResume)408 TEST_F(WatchdogInternalHandlerTest, TestNotifyPowerCycleChangeToResume) {
409 setSystemCallingUid();
410
411 EXPECT_CALL(*mMockWatchdogProcessService, setEnabled(/*isEnabled=*/true)).Times(1);
412
413 auto status =
414 mWatchdogInternalHandler
415 ->notifySystemStateChange(StateType::POWER_CYCLE,
416 static_cast<int32_t>(PowerCycle::POWER_CYCLE_RESUME),
417 -1);
418
419 ASSERT_TRUE(status.isOk()) << status.getMessage();
420 }
421
TEST_F(WatchdogInternalHandlerTest,TestNotifyPowerCycleChangeToSuspendExit)422 TEST_F(WatchdogInternalHandlerTest, TestNotifyPowerCycleChangeToSuspendExit) {
423 setSystemCallingUid();
424
425 EXPECT_CALL(*mMockWatchdogPerfService, onSuspendExit()).Times(1);
426
427 auto status = mWatchdogInternalHandler
428 ->notifySystemStateChange(StateType::POWER_CYCLE,
429 static_cast<int32_t>(
430 PowerCycle::POWER_CYCLE_SUSPEND_EXIT),
431 -1);
432
433 ASSERT_TRUE(status.isOk()) << status.getMessage();
434 }
435
TEST_F(WatchdogInternalHandlerTest,TestErrorOnNotifyPowerCycleChangeWithInvalidArgs)436 TEST_F(WatchdogInternalHandlerTest, TestErrorOnNotifyPowerCycleChangeWithInvalidArgs) {
437 EXPECT_CALL(*mMockWatchdogProcessService, setEnabled(_)).Times(0);
438 EXPECT_CALL(*mMockWatchdogPerfService, setSystemState(_)).Times(0);
439
440 StateType type = StateType::POWER_CYCLE;
441
442 ASSERT_FALSE(mWatchdogInternalHandler->notifySystemStateChange(type, -1, -1).isOk())
443 << "notifySystemStateChange should fail with negative power cycle";
444
445 ASSERT_FALSE(mWatchdogInternalHandler->notifySystemStateChange(type, 3000, -1).isOk())
446 << "notifySystemStateChange should fail with invalid power cycle";
447 }
448
TEST_F(WatchdogInternalHandlerTest,TestNotifyGarageModeOn)449 TEST_F(WatchdogInternalHandlerTest, TestNotifyGarageModeOn) {
450 setSystemCallingUid();
451
452 EXPECT_CALL(*mMockWatchdogPerfService, setSystemState(SystemState::GARAGE_MODE)).Times(1);
453
454 auto status =
455 mWatchdogInternalHandler->notifySystemStateChange(StateType::GARAGE_MODE,
456 static_cast<int32_t>(
457 GarageMode::GARAGE_MODE_ON),
458 -1);
459
460 ASSERT_TRUE(status.isOk()) << status.getMessage();
461 }
462
TEST_F(WatchdogInternalHandlerTest,TestNotifyGarageModeOff)463 TEST_F(WatchdogInternalHandlerTest, TestNotifyGarageModeOff) {
464 setSystemCallingUid();
465
466 EXPECT_CALL(*mMockWatchdogPerfService, setSystemState(SystemState::NORMAL_MODE)).Times(1);
467
468 auto status =
469 mWatchdogInternalHandler->notifySystemStateChange(StateType::GARAGE_MODE,
470 static_cast<int32_t>(
471 GarageMode::GARAGE_MODE_OFF),
472 -1);
473
474 ASSERT_TRUE(status.isOk()) << status.getMessage();
475 }
476
TEST_F(WatchdogInternalHandlerTest,TestOnUserStateChangeWithStartedUser)477 TEST_F(WatchdogInternalHandlerTest, TestOnUserStateChangeWithStartedUser) {
478 setSystemCallingUid();
479 StateType type = StateType::USER_STATE;
480
481 EXPECT_CALL(*mMockWatchdogProcessService, onUserStateChange(234567, /*isStarted=*/true));
482
483 auto status =
484 mWatchdogInternalHandler
485 ->notifySystemStateChange(type, 234567,
486 static_cast<int32_t>(UserState::USER_STATE_STARTED));
487
488 ASSERT_TRUE(status.isOk()) << status.getMessage();
489 }
490
TEST_F(WatchdogInternalHandlerTest,TestOnUserStateChangeWithSwitchingUser)491 TEST_F(WatchdogInternalHandlerTest, TestOnUserStateChangeWithSwitchingUser) {
492 setSystemCallingUid();
493 StateType type = StateType::USER_STATE;
494
495 EXPECT_CALL(*mMockWatchdogPerfService,
496 onUserStateChange(234567, UserState::USER_STATE_SWITCHING));
497
498 auto status = mWatchdogInternalHandler
499 ->notifySystemStateChange(type, 234567,
500 static_cast<int32_t>(
501 UserState::USER_STATE_SWITCHING));
502
503 ASSERT_TRUE(status.isOk()) << status.getMessage();
504 }
505
TEST_F(WatchdogInternalHandlerTest,TestOnUserStateChangeWithUnlockingUser)506 TEST_F(WatchdogInternalHandlerTest, TestOnUserStateChangeWithUnlockingUser) {
507 setSystemCallingUid();
508 StateType type = StateType::USER_STATE;
509
510 EXPECT_CALL(*mMockWatchdogPerfService,
511 onUserStateChange(234567, UserState::USER_STATE_UNLOCKING));
512
513 auto status = mWatchdogInternalHandler
514 ->notifySystemStateChange(type, 234567,
515 static_cast<int32_t>(
516 UserState::USER_STATE_UNLOCKING));
517
518 ASSERT_TRUE(status.isOk()) << status.getMessage();
519 }
520
TEST_F(WatchdogInternalHandlerTest,TestOnUserStateChangeWithPostUnlockedUser)521 TEST_F(WatchdogInternalHandlerTest, TestOnUserStateChangeWithPostUnlockedUser) {
522 setSystemCallingUid();
523 StateType type = StateType::USER_STATE;
524
525 EXPECT_CALL(*mMockWatchdogPerfService,
526 onUserStateChange(234567, UserState::USER_STATE_POST_UNLOCKED));
527
528 auto status = mWatchdogInternalHandler
529 ->notifySystemStateChange(type, 234567,
530 static_cast<int32_t>(
531 UserState::USER_STATE_POST_UNLOCKED));
532
533 ASSERT_TRUE(status.isOk()) << status.getMessage();
534 }
535
TEST_F(WatchdogInternalHandlerTest,TestOnUserStateChangeWithStoppedUser)536 TEST_F(WatchdogInternalHandlerTest, TestOnUserStateChangeWithStoppedUser) {
537 setSystemCallingUid();
538 StateType type = StateType::USER_STATE;
539
540 EXPECT_CALL(*mMockWatchdogProcessService, onUserStateChange(234567, /*isStarted=*/false));
541
542 auto status =
543 mWatchdogInternalHandler
544 ->notifySystemStateChange(type, 234567,
545 static_cast<int32_t>(UserState::USER_STATE_STOPPED));
546
547 ASSERT_TRUE(status.isOk()) << status.getMessage();
548 }
549
TEST_F(WatchdogInternalHandlerTest,TestOnUserStateChangeWithRemovedUser)550 TEST_F(WatchdogInternalHandlerTest, TestOnUserStateChangeWithRemovedUser) {
551 setSystemCallingUid();
552
553 EXPECT_CALL(*mMockIoOveruseMonitor, removeStatsForUser(/*userId=*/234567));
554
555 StateType type = StateType::USER_STATE;
556 auto status =
557 mWatchdogInternalHandler
558 ->notifySystemStateChange(type, 234567,
559 static_cast<int32_t>(UserState::USER_STATE_REMOVED));
560
561 ASSERT_TRUE(status.isOk()) << status.getMessage();
562 }
563
TEST_F(WatchdogInternalHandlerTest,TestErrorOnOnUserStateChangeWithInvalidArgs)564 TEST_F(WatchdogInternalHandlerTest, TestErrorOnOnUserStateChangeWithInvalidArgs) {
565 EXPECT_CALL(*mMockWatchdogProcessService, onUserStateChange(_, _)).Times(0);
566
567 StateType type = StateType::USER_STATE;
568
569 ASSERT_FALSE(mWatchdogInternalHandler->notifySystemStateChange(type, 234567, -1).isOk())
570 << "notifySystemStateChange should fail with negative user state";
571
572 ASSERT_FALSE(mWatchdogInternalHandler->notifySystemStateChange(type, 234567, 3000).isOk())
573 << "notifySystemStateChange should fail with invalid user state";
574 }
575
TEST_F(WatchdogInternalHandlerTest,TestNotifyBootPhaseChange)576 TEST_F(WatchdogInternalHandlerTest, TestNotifyBootPhaseChange) {
577 setSystemCallingUid();
578
579 EXPECT_CALL(*mMockWatchdogPerfService, onBootFinished()).WillOnce(Return(Result<void>()));
580
581 StateType type = StateType::BOOT_PHASE;
582 auto status =
583 mWatchdogInternalHandler->notifySystemStateChange(type,
584 static_cast<int32_t>(
585 BootPhase::BOOT_COMPLETED),
586 -1);
587
588 ASSERT_TRUE(status.isOk()) << status.getMessage();
589 }
590
TEST_F(WatchdogInternalHandlerTest,TestNotifyBootPhaseChangeWithNonBootCompletedPhase)591 TEST_F(WatchdogInternalHandlerTest, TestNotifyBootPhaseChangeWithNonBootCompletedPhase) {
592 setSystemCallingUid();
593
594 EXPECT_CALL(*mMockWatchdogPerfService, onBootFinished()).Times(0);
595
596 StateType type = StateType::BOOT_PHASE;
597 auto status = mWatchdogInternalHandler->notifySystemStateChange(type, 0, -1);
598
599 ASSERT_TRUE(status.isOk()) << status.getMessage();
600 }
601
TEST_F(WatchdogInternalHandlerTest,TestErrorOnNotifySystemStateChangeWithNonSystemCallingUid)602 TEST_F(WatchdogInternalHandlerTest, TestErrorOnNotifySystemStateChangeWithNonSystemCallingUid) {
603 EXPECT_CALL(*mMockWatchdogProcessService, setEnabled(_)).Times(0);
604 EXPECT_CALL(*mMockWatchdogPerfService, setSystemState(_)).Times(0);
605
606 StateType type = StateType::POWER_CYCLE;
607 auto status =
608 mWatchdogInternalHandler
609 ->notifySystemStateChange(type,
610 static_cast<int32_t>(
611 PowerCycle::POWER_CYCLE_SHUTDOWN_PREPARE),
612 -1);
613
614 ASSERT_FALSE(status.isOk()) << "notifySystemStateChange " << kFailOnNonSystemCallingUidMessage;
615 }
616
TEST_F(WatchdogInternalHandlerTest,TestUpdateResourceOveruseConfigurations)617 TEST_F(WatchdogInternalHandlerTest, TestUpdateResourceOveruseConfigurations) {
618 setSystemCallingUid();
619
620 EXPECT_CALL(*mMockIoOveruseMonitor, updateResourceOveruseConfigurations(_))
621 .WillOnce(Return(Result<void>()));
622
623 auto status = mWatchdogInternalHandler->updateResourceOveruseConfigurations(
624 std::vector<ResourceOveruseConfiguration>{});
625
626 ASSERT_TRUE(status.isOk()) << status.getMessage();
627 }
628
TEST_F(WatchdogInternalHandlerTest,TestErrorOnUpdateResourceOveruseConfigurationsWithNonSystemCallingUid)629 TEST_F(WatchdogInternalHandlerTest,
630 TestErrorOnUpdateResourceOveruseConfigurationsWithNonSystemCallingUid) {
631 EXPECT_CALL(*mMockIoOveruseMonitor, updateResourceOveruseConfigurations(_)).Times(0);
632
633 auto status = mWatchdogInternalHandler->updateResourceOveruseConfigurations(
634 std::vector<ResourceOveruseConfiguration>{});
635
636 ASSERT_FALSE(status.isOk()) << "updateResourceOveruseConfigurations "
637 << kFailOnNonSystemCallingUidMessage;
638 }
639
TEST_F(WatchdogInternalHandlerTest,TestGetResourceOveruseConfigurations)640 TEST_F(WatchdogInternalHandlerTest, TestGetResourceOveruseConfigurations) {
641 setSystemCallingUid();
642
643 std::vector<ResourceOveruseConfiguration> configs;
644 EXPECT_CALL(*mMockIoOveruseMonitor, getResourceOveruseConfigurations(Pointer(&configs)))
645 .WillOnce(Return(Result<void>()));
646
647 auto status = mWatchdogInternalHandler->getResourceOveruseConfigurations(&configs);
648
649 ASSERT_TRUE(status.isOk()) << status.getMessage();
650 }
651
TEST_F(WatchdogInternalHandlerTest,TestErrorOnGetResourceOveruseConfigurationsWithNonSystemCallingUid)652 TEST_F(WatchdogInternalHandlerTest,
653 TestErrorOnGetResourceOveruseConfigurationsWithNonSystemCallingUid) {
654 EXPECT_CALL(*mMockIoOveruseMonitor, getResourceOveruseConfigurations(_)).Times(0);
655
656 std::vector<ResourceOveruseConfiguration> configs;
657
658 ASSERT_FALSE(mWatchdogInternalHandler->getResourceOveruseConfigurations(&configs).isOk())
659 << "getResourceOveruseConfigurations " << kFailOnNonSystemCallingUidMessage;
660 }
661
TEST_F(WatchdogInternalHandlerTest,TestControlProcessHealthCheck)662 TEST_F(WatchdogInternalHandlerTest, TestControlProcessHealthCheck) {
663 setSystemCallingUid();
664
665 EXPECT_CALL(*mMockWatchdogProcessService, setEnabled(/*isEnabled=*/true)).Times(1);
666
667 auto status = mWatchdogInternalHandler->controlProcessHealthCheck(/*enable=*/true);
668
669 ASSERT_TRUE(status.isOk()) << status.getMessage();
670 }
671
TEST_F(WatchdogInternalHandlerTest,TestErrorOnControlProcessHealthCheckWithNonSystemCallingUid)672 TEST_F(WatchdogInternalHandlerTest, TestErrorOnControlProcessHealthCheckWithNonSystemCallingUid) {
673 EXPECT_CALL(*mMockWatchdogProcessService, setEnabled(_)).Times(0);
674
675 ASSERT_FALSE(mWatchdogInternalHandler->controlProcessHealthCheck(/*enable=*/true).isOk())
676 << "controlProcessHealthCheck " << kFailOnNonSystemCallingUidMessage;
677 }
678
TEST_F(WatchdogInternalHandlerTest,TestSetThreadPriority)679 TEST_F(WatchdogInternalHandlerTest, TestSetThreadPriority) {
680 setSystemCallingUid();
681 int testPid = 1;
682 int testTid = 2;
683 int testUid = 3;
684 int policy = SCHED_FIFO;
685 int priority = 1;
686 EXPECT_CALL(*mThreadPriorityController,
687 setThreadPriority(testPid, testTid, testUid, policy, priority))
688 .WillOnce(Return(Result<void>()));
689
690 auto status = mWatchdogInternalHandler->setThreadPriority(testPid, testTid, testUid, policy,
691 priority);
692
693 ASSERT_TRUE(status.isOk()) << status.getMessage();
694 }
695
TEST_F(WatchdogInternalHandlerTest,TestGetThreadPriority)696 TEST_F(WatchdogInternalHandlerTest, TestGetThreadPriority) {
697 setSystemCallingUid();
698 int testPid = 1;
699 int testTid = 2;
700 int testUid = 3;
701 int expectedPolicy = SCHED_FIFO;
702 int expectedPriority = 1;
703 EXPECT_CALL(*mThreadPriorityController, getThreadPriority(testPid, testTid, testUid, _))
704 .WillOnce([expectedPolicy, expectedPriority](int, int, int,
705 ThreadPolicyWithPriority* result) {
706 result->policy = expectedPolicy;
707 result->priority = expectedPriority;
708 return Result<void>();
709 });
710
711 ThreadPolicyWithPriority actual;
712 auto status = mWatchdogInternalHandler->getThreadPriority(testPid, testTid, testUid, &actual);
713
714 ASSERT_TRUE(status.isOk()) << status.getMessage();
715 EXPECT_EQ(actual.policy, expectedPolicy);
716 EXPECT_EQ(actual.priority, expectedPriority);
717 }
718
TEST_F(WatchdogInternalHandlerTest,TestOnAidlVhalPidFetched)719 TEST_F(WatchdogInternalHandlerTest, TestOnAidlVhalPidFetched) {
720 setSystemCallingUid();
721
722 int vhalPid = 56423;
723 EXPECT_CALL(*mMockWatchdogProcessService, onAidlVhalPidFetched(vhalPid)).Times(1);
724
725 auto status = mWatchdogInternalHandler->onAidlVhalPidFetched(vhalPid);
726
727 ASSERT_TRUE(status.isOk()) << status.getMessage();
728 }
729
TEST_F(WatchdogInternalHandlerTest,TestErrorOnOnAidlVhalPidFetchedWithNonSystemCallingUid)730 TEST_F(WatchdogInternalHandlerTest, TestErrorOnOnAidlVhalPidFetchedWithNonSystemCallingUid) {
731 EXPECT_CALL(*mMockWatchdogProcessService, onAidlVhalPidFetched(_)).Times(0);
732
733 ASSERT_FALSE(mWatchdogInternalHandler->onAidlVhalPidFetched(56423).isOk())
734 << "onAidlVhalPidFetched " << kFailOnNonSystemCallingUidMessage;
735 }
736
TEST_F(WatchdogInternalHandlerTest,TestOnTodayIoUsageStatsFetched)737 TEST_F(WatchdogInternalHandlerTest, TestOnTodayIoUsageStatsFetched) {
738 setSystemCallingUid();
739
740 std::vector<UserPackageIoUsageStats> userPackageIoUsageStats = {};
741 EXPECT_CALL(*mMockIoOveruseMonitor, onTodayIoUsageStatsFetched(userPackageIoUsageStats))
742 .Times(1);
743
744 auto status = mWatchdogInternalHandler->onTodayIoUsageStatsFetched(userPackageIoUsageStats);
745
746 ASSERT_TRUE(status.isOk()) << status.getMessage();
747 }
748
TEST_F(WatchdogInternalHandlerTest,TestErrorOnOnTodayIoUsageStatsFetchedWithNonSystemCallingUid)749 TEST_F(WatchdogInternalHandlerTest, TestErrorOnOnTodayIoUsageStatsFetchedWithNonSystemCallingUid) {
750 EXPECT_CALL(*mMockIoOveruseMonitor, onTodayIoUsageStatsFetched(_)).Times(0);
751
752 ASSERT_FALSE(mWatchdogInternalHandler->onTodayIoUsageStatsFetched({}).isOk())
753 << "onTodayIoUsageStatsFetched " << kFailOnNonSystemCallingUidMessage;
754 }
755
756 } // namespace watchdog
757 } // namespace automotive
758 } // namespace android
759