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