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