1 /**
2 * Copyright (c) 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 "MockCarWatchdogServiceForSystem.h"
18 #include "MockWatchdogProcessService.h"
19 #include "PackageInfoTestUtils.h"
20 #include "WatchdogServiceHelper.h"
21
22 #include <binder/IBinder.h>
23 #include <gmock/gmock.h>
24 #include <gtest/gtest.h>
25 #include <utils/RefBase.h>
26
27 namespace android {
28 namespace automotive {
29 namespace watchdog {
30
31 namespace {
32
33 namespace aawi = ::android::automotive::watchdog::internal;
34
35 using aawi::ApplicationCategoryType;
36 using aawi::ComponentType;
37 using aawi::ICarWatchdogServiceForSystem;
38 using aawi::PackageInfo;
39 using aawi::PackageIoOveruseStats;
40 using aawi::UidType;
41 using aawi::UserPackageIoUsageStats;
42 using ::android::IBinder;
43 using ::android::RefBase;
44 using ::android::sp;
45 using ::android::base::Error;
46 using ::android::base::Result;
47 using ::android::binder::Status;
48 using ::testing::_;
49 using ::testing::DoAll;
50 using ::testing::IsEmpty;
51 using ::testing::Return;
52 using ::testing::SetArgPointee;
53 using ::testing::UnorderedElementsAreArray;
54
sampleUserPackageIoUsageStats(userid_t userId,const std::string & packageName)55 UserPackageIoUsageStats sampleUserPackageIoUsageStats(userid_t userId,
56 const std::string& packageName) {
57 UserPackageIoUsageStats stats;
58 stats.userId = userId;
59 stats.packageName = packageName;
60 stats.ioUsageStats.writtenBytes.foregroundBytes = 100;
61 stats.ioUsageStats.writtenBytes.backgroundBytes = 200;
62 stats.ioUsageStats.writtenBytes.garageModeBytes = 300;
63 stats.ioUsageStats.forgivenWriteBytes.foregroundBytes = 1100;
64 stats.ioUsageStats.forgivenWriteBytes.backgroundBytes = 1200;
65 stats.ioUsageStats.forgivenWriteBytes.garageModeBytes = 1300;
66 stats.ioUsageStats.totalOveruses = 10;
67 return stats;
68 }
69
70 } // namespace
71
72 namespace internal {
73
74 class WatchdogServiceHelperPeer : public RefBase {
75 public:
WatchdogServiceHelperPeer(const sp<WatchdogServiceHelper> & helper)76 explicit WatchdogServiceHelperPeer(const sp<WatchdogServiceHelper>& helper) : mHelper(helper) {}
~WatchdogServiceHelperPeer()77 ~WatchdogServiceHelperPeer() { mHelper.clear(); }
78
init(const android::sp<WatchdogProcessServiceInterface> & watchdogProcessService)79 Result<void> init(const android::sp<WatchdogProcessServiceInterface>& watchdogProcessService) {
80 return mHelper->init(watchdogProcessService);
81 }
82
getCarWatchdogServiceForSystem()83 const sp<ICarWatchdogServiceForSystem> getCarWatchdogServiceForSystem() {
84 return mHelper->mService;
85 }
86
terminate()87 void terminate() { mHelper->terminate(); }
88
89 private:
90 sp<WatchdogServiceHelper> mHelper;
91 };
92
93 } // namespace internal
94
95 class WatchdogServiceHelperTest : public ::testing::Test {
96 protected:
SetUp()97 virtual void SetUp() {
98 mMockWatchdogProcessService = sp<MockWatchdogProcessService>::make();
99 mWatchdogServiceHelper = sp<WatchdogServiceHelper>::make();
100 mWatchdogServiceHelperPeer =
101 sp<internal::WatchdogServiceHelperPeer>::make(mWatchdogServiceHelper);
102 mMockCarWatchdogServiceForSystem = sp<MockCarWatchdogServiceForSystem>::make();
103 mMockCarWatchdogServiceForSystemBinder = mMockCarWatchdogServiceForSystem->getBinder();
104
105 EXPECT_CALL(*mMockWatchdogProcessService, registerWatchdogServiceHelper(_))
106 .WillOnce(Return(Result<void>()));
107 auto result = mWatchdogServiceHelperPeer->init(mMockWatchdogProcessService);
108 ASSERT_RESULT_OK(result);
109 }
110
TearDown()111 virtual void TearDown() {
112 if (mWatchdogServiceHelperPeer->getCarWatchdogServiceForSystem() != nullptr) {
113 EXPECT_CALL(*mMockCarWatchdogServiceForSystemBinder,
114 unlinkToDeath(_, nullptr, 0, nullptr))
115 .WillOnce(Return(OK));
116 EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(_)).Times(1);
117 }
118 mWatchdogServiceHelperPeer->terminate();
119 mWatchdogServiceHelperPeer.clear();
120 mWatchdogServiceHelper.clear();
121
122 mMockWatchdogProcessService.clear();
123 mMockCarWatchdogServiceForSystem.clear();
124 mMockCarWatchdogServiceForSystemBinder.clear();
125 }
126
registerCarWatchdogService()127 void registerCarWatchdogService() {
128 EXPECT_CALL(*mMockCarWatchdogServiceForSystemBinder, linkToDeath(_, nullptr, 0))
129 .WillOnce(Return(OK));
130 EXPECT_CALL(*mMockWatchdogProcessService, registerCarWatchdogService(_))
131 .WillOnce(Return(Status::ok()));
132
133 Status status = mWatchdogServiceHelper->registerService(mMockCarWatchdogServiceForSystem);
134 ASSERT_TRUE(status.isOk()) << status;
135 ASSERT_NE(mWatchdogServiceHelperPeer->getCarWatchdogServiceForSystem(), nullptr);
136 }
137
138 sp<WatchdogServiceHelper> mWatchdogServiceHelper;
139 sp<MockWatchdogProcessService> mMockWatchdogProcessService;
140 sp<MockCarWatchdogServiceForSystem> mMockCarWatchdogServiceForSystem;
141 sp<MockBinder> mMockCarWatchdogServiceForSystemBinder;
142 sp<internal::WatchdogServiceHelperPeer> mWatchdogServiceHelperPeer;
143 };
144
TEST_F(WatchdogServiceHelperTest,TestInit)145 TEST_F(WatchdogServiceHelperTest, TestInit) {
146 sp<WatchdogServiceHelper> helper = sp<WatchdogServiceHelper>::make();
147 sp<MockWatchdogProcessService> mockWatchdogProcessService =
148 sp<MockWatchdogProcessService>::make();
149
150 EXPECT_CALL(*mockWatchdogProcessService, registerWatchdogServiceHelper(_))
151 .WillOnce(Return(Result<void>()));
152
153 ASSERT_RESULT_OK(helper->init(mockWatchdogProcessService));
154 }
155
TEST_F(WatchdogServiceHelperTest,TestErrorOnInitWithErrorFromWatchdogProcessServiceRegistration)156 TEST_F(WatchdogServiceHelperTest, TestErrorOnInitWithErrorFromWatchdogProcessServiceRegistration) {
157 sp<WatchdogServiceHelper> helper = sp<WatchdogServiceHelper>::make();
158 sp<MockWatchdogProcessService> mockWatchdogProcessService =
159 sp<MockWatchdogProcessService>::make();
160
161 EXPECT_CALL(*mockWatchdogProcessService, registerWatchdogServiceHelper(_))
162 .WillOnce([](const sp<WatchdogServiceHelperInterface>&) -> Result<void> {
163 return Error() << "Failed to register";
164 });
165
166 auto result = helper->init(mockWatchdogProcessService);
167
168 ASSERT_FALSE(result.ok()) << "Watchdog service helper init should fail on error from "
169 << "watchdog process service registration error";
170 }
171
TEST_F(WatchdogServiceHelperTest,TestErrorOnInitWithNullWatchdogProcessServiceInstance)172 TEST_F(WatchdogServiceHelperTest, TestErrorOnInitWithNullWatchdogProcessServiceInstance) {
173 sp<WatchdogServiceHelper> helper = sp<WatchdogServiceHelper>::make();
174
175 auto result = helper->init(nullptr);
176
177 ASSERT_FALSE(result.ok())
178 << "Watchdog service helper init should fail on null watchdog process service instance";
179 }
180
TEST_F(WatchdogServiceHelperTest,TestTerminate)181 TEST_F(WatchdogServiceHelperTest, TestTerminate) {
182 registerCarWatchdogService();
183 EXPECT_CALL(*(mMockCarWatchdogServiceForSystem->getBinder()),
184 unlinkToDeath(_, nullptr, 0, nullptr))
185 .WillOnce(Return(OK));
186
187 mWatchdogServiceHelper->terminate();
188
189 ASSERT_EQ(mWatchdogServiceHelper->mService, nullptr);
190 }
191
TEST_F(WatchdogServiceHelperTest,TestRegisterService)192 TEST_F(WatchdogServiceHelperTest, TestRegisterService) {
193 sp<IBinder> binder = static_cast<sp<IBinder>>(mMockCarWatchdogServiceForSystemBinder);
194 EXPECT_CALL(*mMockCarWatchdogServiceForSystemBinder, linkToDeath(_, nullptr, 0))
195 .WillOnce(Return(OK));
196 EXPECT_CALL(*mMockWatchdogProcessService, registerCarWatchdogService(binder))
197 .WillOnce(Return(Status::ok()));
198
199 Status status = mWatchdogServiceHelper->registerService(mMockCarWatchdogServiceForSystem);
200 ASSERT_TRUE(status.isOk()) << status;
201 ASSERT_NE(mWatchdogServiceHelperPeer->getCarWatchdogServiceForSystem(), nullptr);
202
203 EXPECT_CALL(*mMockCarWatchdogServiceForSystemBinder, linkToDeath(_, nullptr, 0)).Times(0);
204 EXPECT_CALL(*mMockWatchdogProcessService, registerCarWatchdogService(_)).Times(0);
205
206 status = mWatchdogServiceHelper->registerService(mMockCarWatchdogServiceForSystem);
207 ASSERT_TRUE(status.isOk()) << status;
208 ASSERT_NE(mWatchdogServiceHelperPeer->getCarWatchdogServiceForSystem(), nullptr);
209 }
210
TEST_F(WatchdogServiceHelperTest,TestErrorOnRegisterServiceWithBinderDied)211 TEST_F(WatchdogServiceHelperTest, TestErrorOnRegisterServiceWithBinderDied) {
212 sp<IBinder> binder = static_cast<sp<IBinder>>(mMockCarWatchdogServiceForSystemBinder);
213 EXPECT_CALL(*mMockWatchdogProcessService, registerCarWatchdogService(binder))
214 .WillOnce(Return(Status::ok()));
215 EXPECT_CALL(*mMockCarWatchdogServiceForSystemBinder, linkToDeath(_, nullptr, 0))
216 .WillOnce(Return(DEAD_OBJECT));
217 EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(binder)).Times(1);
218
219 ASSERT_FALSE(mWatchdogServiceHelper->registerService(mMockCarWatchdogServiceForSystem).isOk())
220 << "Failed to return error on register service with dead binder";
221 ASSERT_EQ(mWatchdogServiceHelperPeer->getCarWatchdogServiceForSystem(), nullptr);
222 }
223
TEST_F(WatchdogServiceHelperTest,TestErrorOnRegisterServiceWithWatchdogProcessServiceError)224 TEST_F(WatchdogServiceHelperTest, TestErrorOnRegisterServiceWithWatchdogProcessServiceError) {
225 sp<IBinder> binder = static_cast<sp<IBinder>>(mMockCarWatchdogServiceForSystemBinder);
226 EXPECT_CALL(*mMockCarWatchdogServiceForSystemBinder, linkToDeath(_, nullptr, 0)).Times(0);
227 EXPECT_CALL(*mMockCarWatchdogServiceForSystemBinder, unlinkToDeath(_, nullptr, 0, nullptr))
228 .Times(0);
229 EXPECT_CALL(*mMockWatchdogProcessService, registerCarWatchdogService(binder))
230 .WillOnce(Return(Status::fromExceptionCode(Status::EX_ILLEGAL_STATE)));
231
232 ASSERT_FALSE(mWatchdogServiceHelper->registerService(mMockCarWatchdogServiceForSystem).isOk())
233 << "Failed to return error on error from watchdog process service";
234 ASSERT_EQ(mWatchdogServiceHelperPeer->getCarWatchdogServiceForSystem(), nullptr);
235 }
236
TEST_F(WatchdogServiceHelperTest,TestUnregisterService)237 TEST_F(WatchdogServiceHelperTest, TestUnregisterService) {
238 registerCarWatchdogService();
239 sp<IBinder> binder = static_cast<sp<IBinder>>(mMockCarWatchdogServiceForSystemBinder);
240 EXPECT_CALL(*mMockCarWatchdogServiceForSystemBinder, unlinkToDeath(_, nullptr, 0, nullptr))
241 .WillOnce(Return(OK));
242 EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(binder)).Times(1);
243
244 Status status = mWatchdogServiceHelper->unregisterService(mMockCarWatchdogServiceForSystem);
245 ASSERT_TRUE(status.isOk()) << status;
246 ASSERT_EQ(mWatchdogServiceHelperPeer->getCarWatchdogServiceForSystem(), nullptr);
247
248 EXPECT_CALL(*mMockCarWatchdogServiceForSystemBinder, unlinkToDeath(_, nullptr, 0, nullptr))
249 .Times(0);
250 EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(_)).Times(0);
251
252 status = mWatchdogServiceHelper->unregisterService(mMockCarWatchdogServiceForSystem);
253 ASSERT_FALSE(status.isOk()) << "Unregistering an unregistered service should return an error: "
254 << status;
255 }
256
TEST_F(WatchdogServiceHelperTest,TestCheckIfAlive)257 TEST_F(WatchdogServiceHelperTest, TestCheckIfAlive) {
258 registerCarWatchdogService();
259 EXPECT_CALL(*mMockCarWatchdogServiceForSystem,
260 checkIfAlive(0, aawi::TimeoutLength::TIMEOUT_CRITICAL))
261 .WillOnce(Return(Status::ok()));
262 Status status = mWatchdogServiceHelper->checkIfAlive(mMockCarWatchdogServiceForSystemBinder, 0,
263 TimeoutLength::TIMEOUT_CRITICAL);
264 ASSERT_TRUE(status.isOk()) << status;
265 }
266
TEST_F(WatchdogServiceHelperTest,TestErrorOnCheckIfAliveWithNotRegisteredCarWatchdogServiceBinder)267 TEST_F(WatchdogServiceHelperTest,
268 TestErrorOnCheckIfAliveWithNotRegisteredCarWatchdogServiceBinder) {
269 registerCarWatchdogService();
270 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, checkIfAlive(_, _)).Times(0);
271 Status status = mWatchdogServiceHelper->checkIfAlive(sp<MockBinder>::make(), 0,
272 TimeoutLength::TIMEOUT_CRITICAL);
273 ASSERT_FALSE(status.isOk()) << "checkIfAlive should fail when the given car watchdog service "
274 "binder is not registered with the helper";
275 }
276
TEST_F(WatchdogServiceHelperTest,TestErrorOnCheckIfAliveWithNoCarWatchdogServiceRegistered)277 TEST_F(WatchdogServiceHelperTest, TestErrorOnCheckIfAliveWithNoCarWatchdogServiceRegistered) {
278 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, checkIfAlive(_, _)).Times(0);
279 Status status = mWatchdogServiceHelper->checkIfAlive(mMockCarWatchdogServiceForSystemBinder, 0,
280 TimeoutLength::TIMEOUT_CRITICAL);
281 ASSERT_FALSE(status.isOk())
282 << "checkIfAlive should fail when no car watchdog service registered with the helper";
283 }
284
TEST_F(WatchdogServiceHelperTest,TestErrorOnCheckIfAliveWithErrorStatusFromCarWatchdogService)285 TEST_F(WatchdogServiceHelperTest, TestErrorOnCheckIfAliveWithErrorStatusFromCarWatchdogService) {
286 registerCarWatchdogService();
287 EXPECT_CALL(*mMockCarWatchdogServiceForSystem,
288 checkIfAlive(0, aawi::TimeoutLength::TIMEOUT_CRITICAL))
289 .WillOnce(Return(Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Illegal state")));
290 Status status = mWatchdogServiceHelper->checkIfAlive(mMockCarWatchdogServiceForSystemBinder, 0,
291 TimeoutLength::TIMEOUT_CRITICAL);
292 ASSERT_FALSE(status.isOk())
293 << "checkIfAlive should fail when car watchdog service API returns error";
294 }
295
TEST_F(WatchdogServiceHelperTest,TestPrepareProcessTermination)296 TEST_F(WatchdogServiceHelperTest, TestPrepareProcessTermination) {
297 registerCarWatchdogService();
298 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, prepareProcessTermination())
299 .WillOnce(Return(Status::ok()));
300 Status status = mWatchdogServiceHelper->prepareProcessTermination(
301 mMockCarWatchdogServiceForSystemBinder);
302 ASSERT_EQ(mWatchdogServiceHelperPeer->getCarWatchdogServiceForSystem(), nullptr);
303 ASSERT_TRUE(status.isOk()) << status;
304 }
305
TEST_F(WatchdogServiceHelperTest,TestErrorOnPrepareProcessTerminationWithNotRegisteredCarWatchdogServiceBinder)306 TEST_F(WatchdogServiceHelperTest,
307 TestErrorOnPrepareProcessTerminationWithNotRegisteredCarWatchdogServiceBinder) {
308 registerCarWatchdogService();
309 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, prepareProcessTermination()).Times(0);
310 Status status = mWatchdogServiceHelper->prepareProcessTermination(sp<MockBinder>::make());
311 ASSERT_FALSE(status.isOk()) << "prepareProcessTermination should fail when the given car "
312 "watchdog service binder is not registered with the helper";
313 }
314
TEST_F(WatchdogServiceHelperTest,TestErrorOnPrepareProcessTerminationWithNoCarWatchdogServiceRegistered)315 TEST_F(WatchdogServiceHelperTest,
316 TestErrorOnPrepareProcessTerminationWithNoCarWatchdogServiceRegistered) {
317 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, prepareProcessTermination()).Times(0);
318 Status status = mWatchdogServiceHelper->prepareProcessTermination(
319 mMockCarWatchdogServiceForSystemBinder);
320 ASSERT_FALSE(status.isOk()) << "prepareProcessTermination should fail when no car watchdog "
321 "service registered with the helper";
322 }
323
TEST_F(WatchdogServiceHelperTest,TestErrorOnPrepareProcessTerminationWithErrorStatusFromCarWatchdogService)324 TEST_F(WatchdogServiceHelperTest,
325 TestErrorOnPrepareProcessTerminationWithErrorStatusFromCarWatchdogService) {
326 registerCarWatchdogService();
327
328 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, prepareProcessTermination())
329 .WillOnce(Return(Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Illegal state")));
330
331 Status status = mWatchdogServiceHelper->prepareProcessTermination(
332 mMockCarWatchdogServiceForSystemBinder);
333
334 ASSERT_FALSE(status.isOk())
335 << "prepareProcessTermination should fail when car watchdog service API returns error";
336 }
337
TEST_F(WatchdogServiceHelperTest,TestGetPackageInfosForUids)338 TEST_F(WatchdogServiceHelperTest, TestGetPackageInfosForUids) {
339 std::vector<int32_t> uids = {1000};
340 std::vector<std::string> prefixesStr = {"vendor.package"};
341 std::vector<PackageInfo> expectedPackageInfo{
342 constructPackageInfo("vendor.package.A", 120000, UidType::NATIVE, ComponentType::VENDOR,
343 ApplicationCategoryType::OTHERS),
344 constructPackageInfo("third_party.package.B", 130000, UidType::APPLICATION,
345 ComponentType::THIRD_PARTY, ApplicationCategoryType::OTHERS),
346 };
347 std::vector<PackageInfo> actualPackageInfo;
348
349 registerCarWatchdogService();
350
351 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, getPackageInfosForUids(uids, prefixesStr, _))
352 .WillOnce(DoAll(SetArgPointee<2>(expectedPackageInfo), Return(Status::ok())));
353
354 Status status =
355 mWatchdogServiceHelper->getPackageInfosForUids(uids, prefixesStr, &actualPackageInfo);
356
357 ASSERT_TRUE(status.isOk()) << status;
358 EXPECT_THAT(actualPackageInfo, UnorderedElementsAreArray(expectedPackageInfo));
359 }
360
TEST_F(WatchdogServiceHelperTest,TestErrorOnGetPackageInfosForUidsWithNoCarWatchdogServiceRegistered)361 TEST_F(WatchdogServiceHelperTest,
362 TestErrorOnGetPackageInfosForUidsWithNoCarWatchdogServiceRegistered) {
363 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, getPackageInfosForUids(_, _, _)).Times(0);
364
365 std::vector<int32_t> uids;
366 std::vector<std::string> prefixes;
367 std::vector<PackageInfo> actualPackageInfo;
368 Status status =
369 mWatchdogServiceHelper->getPackageInfosForUids(uids, prefixes, &actualPackageInfo);
370
371 ASSERT_FALSE(status.isOk()) << "getPackageInfosForUids should fail when no "
372 "car watchdog service registered with the helper";
373 EXPECT_THAT(actualPackageInfo, IsEmpty());
374 }
375
TEST_F(WatchdogServiceHelperTest,TestErrorOnGetPackageInfosForUidsWithErrorStatusFromCarWatchdogService)376 TEST_F(WatchdogServiceHelperTest,
377 TestErrorOnGetPackageInfosForUidsWithErrorStatusFromCarWatchdogService) {
378 registerCarWatchdogService();
379 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, getPackageInfosForUids(_, _, _))
380 .WillOnce(Return(Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Illegal state")));
381
382 std::vector<int32_t> uids;
383 std::vector<std::string> prefixes;
384 std::vector<PackageInfo> actualPackageInfo;
385 Status status =
386 mWatchdogServiceHelper->getPackageInfosForUids(uids, prefixes, &actualPackageInfo);
387
388 ASSERT_FALSE(status.isOk()) << "getPackageInfosForUids should fail when car watchdog "
389 "service API returns error";
390 ASSERT_TRUE(actualPackageInfo.empty());
391 }
392
TEST_F(WatchdogServiceHelperTest,TestLatestIoOveruseStats)393 TEST_F(WatchdogServiceHelperTest, TestLatestIoOveruseStats) {
394 PackageIoOveruseStats stats;
395 stats.uid = 101000;
396 stats.ioOveruseStats.killableOnOveruse = true;
397 stats.ioOveruseStats.startTime = 99898;
398 stats.ioOveruseStats.durationInSeconds = 12345;
399 stats.ioOveruseStats.totalOveruses = 10;
400 stats.shouldNotify = true;
401 std::vector<PackageIoOveruseStats> expectedIoOveruseStats = {stats};
402
403 registerCarWatchdogService();
404
405 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, latestIoOveruseStats(expectedIoOveruseStats))
406 .WillOnce(Return(Status::ok()));
407
408 Status status = mWatchdogServiceHelper->latestIoOveruseStats(expectedIoOveruseStats);
409
410 ASSERT_TRUE(status.isOk()) << status;
411 }
412
TEST_F(WatchdogServiceHelperTest,TestErrorsOnLatestIoOveruseStatsWithNoCarWatchdogServiceRegistered)413 TEST_F(WatchdogServiceHelperTest,
414 TestErrorsOnLatestIoOveruseStatsWithNoCarWatchdogServiceRegistered) {
415 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, latestIoOveruseStats(_)).Times(0);
416
417 Status status = mWatchdogServiceHelper->latestIoOveruseStats({});
418
419 ASSERT_FALSE(status.isOk()) << "latetstIoOveruseStats should fail when no "
420 "car watchdog service registered with the helper";
421 }
422
TEST_F(WatchdogServiceHelperTest,TestErrorsOnLatestIoOveruseStatsWithErrorStatusFromCarWatchdogService)423 TEST_F(WatchdogServiceHelperTest,
424 TestErrorsOnLatestIoOveruseStatsWithErrorStatusFromCarWatchdogService) {
425 registerCarWatchdogService();
426
427 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, latestIoOveruseStats(_))
428 .WillOnce(Return(Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Illegal state")));
429
430 Status status = mWatchdogServiceHelper->latestIoOveruseStats({});
431
432 ASSERT_FALSE(status.isOk()) << "latetstIoOveruseStats should fail when car watchdog "
433 "service API returns error";
434 }
435
TEST_F(WatchdogServiceHelperTest,TestResetResourceOveruseStats)436 TEST_F(WatchdogServiceHelperTest, TestResetResourceOveruseStats) {
437 registerCarWatchdogService();
438
439 std::vector<std::string> packageNames = {"system.daemon"};
440 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, resetResourceOveruseStats(packageNames))
441 .WillOnce(Return(Status::ok()));
442
443 Status status = mWatchdogServiceHelper->resetResourceOveruseStats(packageNames);
444
445 ASSERT_TRUE(status.isOk()) << status;
446 }
447
TEST_F(WatchdogServiceHelperTest,TestErrorsOnResetResourceOveruseStatsWithNoCarWatchdogServiceRegistered)448 TEST_F(WatchdogServiceHelperTest,
449 TestErrorsOnResetResourceOveruseStatsWithNoCarWatchdogServiceRegistered) {
450 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, resetResourceOveruseStats(_)).Times(0);
451
452 Status status = mWatchdogServiceHelper->resetResourceOveruseStats({});
453
454 ASSERT_FALSE(status.isOk()) << "resetResourceOveruseStats should fail when no "
455 "car watchdog service registered with the helper";
456 }
457
TEST_F(WatchdogServiceHelperTest,TestErrorsOnResetResourceOveruseStatsWithErrorStatusFromCarWatchdogService)458 TEST_F(WatchdogServiceHelperTest,
459 TestErrorsOnResetResourceOveruseStatsWithErrorStatusFromCarWatchdogService) {
460 registerCarWatchdogService();
461
462 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, resetResourceOveruseStats(_))
463 .WillOnce(Return(Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Illegal state")));
464
465 Status status = mWatchdogServiceHelper->resetResourceOveruseStats({});
466
467 ASSERT_FALSE(status.isOk()) << "resetResourceOveruseStats should fail when car watchdog "
468 "service API returns error";
469 }
470
TEST_F(WatchdogServiceHelperTest,TestGetTodayIoUsageStats)471 TEST_F(WatchdogServiceHelperTest, TestGetTodayIoUsageStats) {
472 std::vector<UserPackageIoUsageStats>
473 expectedStats{sampleUserPackageIoUsageStats(10, "vendor.package"),
474 sampleUserPackageIoUsageStats(11, "third_party.package")};
475
476 registerCarWatchdogService();
477
478 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, getTodayIoUsageStats(_))
479 .WillOnce(DoAll(SetArgPointee<0>(expectedStats), Return(Status::ok())));
480
481 std::vector<UserPackageIoUsageStats> actualStats;
482 Status status = mWatchdogServiceHelper->getTodayIoUsageStats(&actualStats);
483
484 ASSERT_TRUE(status.isOk()) << status;
485 EXPECT_THAT(actualStats, UnorderedElementsAreArray(expectedStats));
486 }
487
TEST_F(WatchdogServiceHelperTest,TestErrorOnGetTodayIoUsageStatsWithNoCarWatchdogServiceRegistered)488 TEST_F(WatchdogServiceHelperTest,
489 TestErrorOnGetTodayIoUsageStatsWithNoCarWatchdogServiceRegistered) {
490 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, getTodayIoUsageStats(_)).Times(0);
491
492 std::vector<UserPackageIoUsageStats> actualStats;
493 Status status = mWatchdogServiceHelper->getTodayIoUsageStats(&actualStats);
494
495 ASSERT_FALSE(status.isOk()) << "getTodayIoUsageStats should fail when no "
496 "car watchdog service registered with the helper";
497 EXPECT_THAT(actualStats, IsEmpty());
498 }
499
TEST_F(WatchdogServiceHelperTest,TestErrorOnGetTodayIoUsageStatsWithErrorStatusFromCarWatchdogService)500 TEST_F(WatchdogServiceHelperTest,
501 TestErrorOnGetTodayIoUsageStatsWithErrorStatusFromCarWatchdogService) {
502 registerCarWatchdogService();
503
504 EXPECT_CALL(*mMockCarWatchdogServiceForSystem, getTodayIoUsageStats(_))
505 .WillOnce(Return(Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Illegal state")));
506
507 std::vector<UserPackageIoUsageStats> actualStats;
508 Status status = mWatchdogServiceHelper->getTodayIoUsageStats(&actualStats);
509
510 ASSERT_FALSE(status.isOk()) << "getTodayIoUsageStats should fail when car watchdog "
511 "service API returns error";
512 ASSERT_TRUE(actualStats.empty());
513 }
514
515 } // namespace watchdog
516 } // namespace automotive
517 } // namespace android
518