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 "MockAIBinderDeathRegistrationWrapper.h"
18 #include "MockCarWatchdogServiceForSystem.h"
19 #include "MockWatchdogProcessService.h"
20 #include "PackageInfoTestUtils.h"
21 #include "WatchdogServiceHelper.h"
22 
23 #include <binder/IBinder.h>
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26 #include <utils/RefBase.h>
27 
28 namespace android {
29 namespace automotive {
30 namespace watchdog {
31 
32 namespace {
33 
34 using ::aidl::android::automotive::watchdog::TimeoutLength;
35 using ::aidl::android::automotive::watchdog::internal::ApplicationCategoryType;
36 using ::aidl::android::automotive::watchdog::internal::ComponentType;
37 using ::aidl::android::automotive::watchdog::internal::ICarWatchdogServiceForSystem;
38 using ::aidl::android::automotive::watchdog::internal::PackageInfo;
39 using ::aidl::android::automotive::watchdog::internal::PackageIoOveruseStats;
40 using ::aidl::android::automotive::watchdog::internal::ResourceOveruseStats;
41 using ::aidl::android::automotive::watchdog::internal::ResourceStats;
42 using ::aidl::android::automotive::watchdog::internal::ResourceUsageStats;
43 using ::aidl::android::automotive::watchdog::internal::UidType;
44 using ::aidl::android::automotive::watchdog::internal::UserPackageIoUsageStats;
45 using ::android::RefBase;
46 using ::android::sp;
47 using ::android::base::Error;
48 using ::android::base::Result;
49 using ::ndk::ScopedAStatus;
50 using ::ndk::SharedRefBase;
51 using ::testing::_;
52 using ::testing::ByMove;
53 using ::testing::DoAll;
54 using ::testing::Eq;
55 using ::testing::IsEmpty;
56 using ::testing::Return;
57 using ::testing::SetArgPointee;
58 using ::testing::UnorderedElementsAreArray;
59 
60 using InternalTimeoutLength = ::aidl::android::automotive::watchdog::internal::TimeoutLength;
61 
62 constexpr const char kFailOnNoCarWatchdogServiceMessage[] =
63         "should fail when no car watchdog service registered with the helper";
64 constexpr const char kFailOnCarWatchdogServiceErrMessage[] =
65         "should fail when car watchdog service API return error";
66 
67 }  // namespace
68 
69 namespace internal {
70 
71 class WatchdogServiceHelperPeer : public RefBase {
72 public:
WatchdogServiceHelperPeer(const sp<WatchdogServiceHelper> & helper)73     explicit WatchdogServiceHelperPeer(const sp<WatchdogServiceHelper>& helper) : mHelper(helper) {}
~WatchdogServiceHelperPeer()74     ~WatchdogServiceHelperPeer() { mHelper.clear(); }
75 
init(const sp<WatchdogProcessServiceInterface> & watchdogProcessService,const sp<AIBinderDeathRegistrationWrapperInterface> & deathRegistrationWrapper)76     Result<void> init(
77             const sp<WatchdogProcessServiceInterface>& watchdogProcessService,
78             const sp<AIBinderDeathRegistrationWrapperInterface>& deathRegistrationWrapper) {
79         mHelper->mDeathRegistrationWrapper = deathRegistrationWrapper;
80         return mHelper->init(watchdogProcessService);
81     }
82 
terminate()83     void terminate() { mHelper->terminate(); }
84 
85 private:
86     sp<WatchdogServiceHelper> mHelper;
87 };
88 
89 }  // namespace internal
90 
91 class WatchdogServiceHelperTest : public ::testing::Test {
92 protected:
SetUp()93     virtual void SetUp() {
94         mMockWatchdogProcessService = sp<MockWatchdogProcessService>::make();
95         mMockDeathRegistrationWrapper = sp<MockAIBinderDeathRegistrationWrapper>::make();
96         mWatchdogServiceHelper = sp<WatchdogServiceHelper>::make();
97         mWatchdogServiceHelperPeer =
98                 sp<internal::WatchdogServiceHelperPeer>::make(mWatchdogServiceHelper);
99         mMockCarWatchdogServiceForSystem = SharedRefBase::make<MockCarWatchdogServiceForSystem>();
100 
101         auto result = mWatchdogServiceHelperPeer->init(mMockWatchdogProcessService,
102                                                        mMockDeathRegistrationWrapper);
103         ASSERT_RESULT_OK(result);
104     }
105 
TearDown()106     virtual void TearDown() {
107         if (mWatchdogServiceHelper->isServiceConnected()) {
108             expectUnlinkToDeath(mMockCarWatchdogServiceForSystem->asBinder().get(),
109                                 std::move(ScopedAStatus::ok()));
110             EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(_)).Times(1);
111         }
112         mWatchdogServiceHelperPeer->terminate();
113         mWatchdogServiceHelperPeer.clear();
114         mWatchdogServiceHelper.clear();
115 
116         mMockWatchdogProcessService.clear();
117         mMockDeathRegistrationWrapper.clear();
118         mMockCarWatchdogServiceForSystem.reset();
119         mWatchdogServiceHelperPeer.clear();
120     }
121 
registerCarWatchdogService()122     void registerCarWatchdogService() {
123         expectLinkToDeath(mMockCarWatchdogServiceForSystem->asBinder().get(),
124                           std::move(ScopedAStatus::ok()));
125         EXPECT_CALL(*mMockWatchdogProcessService, registerCarWatchdogService(_, _))
126                 .WillOnce(Return(ByMove(ScopedAStatus::ok())));
127 
128         auto status = mWatchdogServiceHelper->registerService(mMockCarWatchdogServiceForSystem);
129 
130         ASSERT_TRUE(status.isOk()) << status.getMessage();
131         ASSERT_TRUE(mWatchdogServiceHelper->isServiceConnected());
132     }
133 
getCarWatchdogServiceForSystemCookie()134     void* getCarWatchdogServiceForSystemCookie() {
135         return static_cast<void*>(mMockCarWatchdogServiceForSystem->asBinder().get());
136     }
137 
expectLinkToDeath(AIBinder * aiBinder,ndk::ScopedAStatus expectedStatus)138     void expectLinkToDeath(AIBinder* aiBinder, ndk::ScopedAStatus expectedStatus) {
139         EXPECT_CALL(*mMockDeathRegistrationWrapper,
140                     linkToDeath(Eq(aiBinder), _, static_cast<void*>(aiBinder)))
141                 .WillOnce(Return(ByMove(std::move(expectedStatus))));
142     }
143 
expectUnlinkToDeath(AIBinder * aiBinder,ndk::ScopedAStatus expectedStatus)144     void expectUnlinkToDeath(AIBinder* aiBinder, ndk::ScopedAStatus expectedStatus) {
145         EXPECT_CALL(*mMockDeathRegistrationWrapper,
146                     unlinkToDeath(Eq(aiBinder), _, static_cast<void*>(aiBinder)))
147                 .WillOnce(Return(ByMove(std::move(expectedStatus))));
148     }
149 
expectNoLinkToDeath(AIBinder * aiBinder)150     void expectNoLinkToDeath(AIBinder* aiBinder) {
151         EXPECT_CALL(*mMockDeathRegistrationWrapper,
152                     linkToDeath(Eq(aiBinder), _, static_cast<void*>(aiBinder)))
153                 .Times(0);
154     }
155 
expectNoUnlinkToDeath(AIBinder * aiBinder)156     void expectNoUnlinkToDeath(AIBinder* aiBinder) {
157         EXPECT_CALL(*mMockDeathRegistrationWrapper,
158                     unlinkToDeath(Eq(aiBinder), _, static_cast<void*>(aiBinder)))
159                 .Times(0);
160     }
161 
162     sp<WatchdogServiceHelper> mWatchdogServiceHelper;
163     sp<MockWatchdogProcessService> mMockWatchdogProcessService;
164     sp<MockAIBinderDeathRegistrationWrapper> mMockDeathRegistrationWrapper;
165     std::shared_ptr<MockCarWatchdogServiceForSystem> mMockCarWatchdogServiceForSystem;
166     sp<internal::WatchdogServiceHelperPeer> mWatchdogServiceHelperPeer;
167 };
168 
TEST_F(WatchdogServiceHelperTest,TestInit)169 TEST_F(WatchdogServiceHelperTest, TestInit) {
170     sp<WatchdogServiceHelper> helper = sp<WatchdogServiceHelper>::make();
171     sp<MockWatchdogProcessService> mockWatchdogProcessService =
172             sp<MockWatchdogProcessService>::make();
173 
174     ASSERT_RESULT_OK(helper->init(mockWatchdogProcessService));
175 }
176 
TEST_F(WatchdogServiceHelperTest,TestErrorOnInitWithNullWatchdogProcessServiceInstance)177 TEST_F(WatchdogServiceHelperTest, TestErrorOnInitWithNullWatchdogProcessServiceInstance) {
178     sp<WatchdogServiceHelper> helper = sp<WatchdogServiceHelper>::make();
179 
180     auto result = helper->init(nullptr);
181 
182     ASSERT_FALSE(result.ok())
183             << "Watchdog service helper init should fail on null watchdog process service instance";
184 }
185 
TEST_F(WatchdogServiceHelperTest,TestTerminate)186 TEST_F(WatchdogServiceHelperTest, TestTerminate) {
187     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
188     expectUnlinkToDeath(mMockCarWatchdogServiceForSystem->asBinder().get(),
189                         std::move(ScopedAStatus::ok()));
190 
191     mWatchdogServiceHelper->terminate();
192 
193     ASSERT_EQ(mWatchdogServiceHelper->mService, nullptr);
194 }
195 
TEST_F(WatchdogServiceHelperTest,TestRegisterService)196 TEST_F(WatchdogServiceHelperTest, TestRegisterService) {
197     auto binder = mMockCarWatchdogServiceForSystem->asBinder();
198     auto serviceHelperInterface = sp<WatchdogServiceHelperInterface>(mWatchdogServiceHelper);
199 
200     expectLinkToDeath(binder.get(), std::move(ScopedAStatus::ok()));
201     EXPECT_CALL(*mMockWatchdogProcessService,
202                 registerCarWatchdogService(binder, serviceHelperInterface))
203             .WillOnce(Return(ByMove(ScopedAStatus::ok())));
204 
205     auto status = mWatchdogServiceHelper->registerService(mMockCarWatchdogServiceForSystem);
206     ASSERT_TRUE(status.isOk()) << status.getMessage();
207     ASSERT_TRUE(mWatchdogServiceHelper->isServiceConnected());
208 
209     expectNoLinkToDeath(binder.get());
210     EXPECT_CALL(*mMockWatchdogProcessService, registerCarWatchdogService(_, _)).Times(0);
211 
212     status = mWatchdogServiceHelper->registerService(mMockCarWatchdogServiceForSystem);
213     ASSERT_TRUE(status.isOk()) << status.getMessage();
214     ASSERT_TRUE(mWatchdogServiceHelper->isServiceConnected());
215 }
216 
TEST_F(WatchdogServiceHelperTest,TestErrorOnRegisterServiceWithBinderDied)217 TEST_F(WatchdogServiceHelperTest, TestErrorOnRegisterServiceWithBinderDied) {
218     auto binder = mMockCarWatchdogServiceForSystem->asBinder();
219     auto serviceHelperInterface = sp<WatchdogServiceHelperInterface>(mWatchdogServiceHelper);
220     expectLinkToDeath(binder.get(),
221                       std::move(ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED)));
222     EXPECT_CALL(*mMockWatchdogProcessService,
223                 registerCarWatchdogService(binder, serviceHelperInterface))
224             .WillOnce(Return(ByMove(ScopedAStatus::ok())));
225     EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(binder)).Times(1);
226 
227     ASSERT_FALSE(mWatchdogServiceHelper->registerService(mMockCarWatchdogServiceForSystem).isOk())
228             << "Failed to return error on register service with dead binder";
229     ASSERT_FALSE(mWatchdogServiceHelper->isServiceConnected());
230 }
231 
TEST_F(WatchdogServiceHelperTest,TestErrorOnRegisterServiceWithWatchdogProcessServiceError)232 TEST_F(WatchdogServiceHelperTest, TestErrorOnRegisterServiceWithWatchdogProcessServiceError) {
233     auto binder = mMockCarWatchdogServiceForSystem->asBinder();
234     auto serviceHelperInterface = sp<WatchdogServiceHelperInterface>(mWatchdogServiceHelper);
235     expectNoLinkToDeath(binder.get());
236     expectNoUnlinkToDeath(binder.get());
237     EXPECT_CALL(*mMockWatchdogProcessService,
238                 registerCarWatchdogService(binder, serviceHelperInterface))
239             .WillOnce(Return(ByMove(ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE))));
240 
241     ASSERT_FALSE(mWatchdogServiceHelper->registerService(mMockCarWatchdogServiceForSystem).isOk())
242             << "Failed to return error on error from watchdog process service";
243     ASSERT_FALSE(mWatchdogServiceHelper->isServiceConnected());
244 }
245 
TEST_F(WatchdogServiceHelperTest,TestErrorOnRegisterServiceWithDeadBinder)246 TEST_F(WatchdogServiceHelperTest, TestErrorOnRegisterServiceWithDeadBinder) {
247     auto binder = mMockCarWatchdogServiceForSystem->asBinder();
248     auto serviceHelperInterface = sp<WatchdogServiceHelperInterface>(mWatchdogServiceHelper);
249     expectLinkToDeath(binder.get(),
250                       std::move(ScopedAStatus::fromExceptionCode(EX_TRANSACTION_FAILED)));
251     EXPECT_CALL(*mMockWatchdogProcessService,
252                 registerCarWatchdogService(binder, serviceHelperInterface))
253             .WillOnce(Return(ByMove(ScopedAStatus::ok())));
254     EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(binder)).Times(1);
255 
256     ASSERT_FALSE(mWatchdogServiceHelper->registerService(mMockCarWatchdogServiceForSystem).isOk())
257             << "Failed to return error on register service with dead binder";
258     ASSERT_FALSE(mWatchdogServiceHelper->isServiceConnected());
259 }
260 
TEST_F(WatchdogServiceHelperTest,TestUnregisterService)261 TEST_F(WatchdogServiceHelperTest, TestUnregisterService) {
262     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
263 
264     auto binder = mMockCarWatchdogServiceForSystem->asBinder();
265     expectUnlinkToDeath(binder.get(), std::move(ScopedAStatus::ok()));
266     EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(binder)).Times(1);
267 
268     auto status = mWatchdogServiceHelper->unregisterService(mMockCarWatchdogServiceForSystem);
269 
270     ASSERT_TRUE(status.isOk()) << status.getMessage();
271     ASSERT_FALSE(mWatchdogServiceHelper->isServiceConnected());
272 
273     expectNoUnlinkToDeath(binder.get());
274     EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(_)).Times(0);
275 
276     ASSERT_FALSE(mWatchdogServiceHelper->unregisterService(mMockCarWatchdogServiceForSystem).isOk())
277             << "Unregistering an unregistered service should return an error";
278 }
279 
TEST_F(WatchdogServiceHelperTest,TestHandleBinderDeath)280 TEST_F(WatchdogServiceHelperTest, TestHandleBinderDeath) {
281     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
282 
283     auto binder = mMockCarWatchdogServiceForSystem->asBinder();
284     EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(binder)).Times(1);
285 
286     mWatchdogServiceHelper->handleBinderDeath(static_cast<void*>(binder.get()));
287 
288     ASSERT_FALSE(mWatchdogServiceHelper->isServiceConnected());
289 
290     EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(_)).Times(0);
291 
292     ASSERT_FALSE(mWatchdogServiceHelper->unregisterService(mMockCarWatchdogServiceForSystem).isOk())
293             << "Unregistering a dead service should return an error";
294 }
295 
TEST_F(WatchdogServiceHelperTest,TestCheckIfAlive)296 TEST_F(WatchdogServiceHelperTest, TestCheckIfAlive) {
297     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
298 
299     EXPECT_CALL(*mMockCarWatchdogServiceForSystem,
300                 checkIfAlive(0, InternalTimeoutLength::TIMEOUT_CRITICAL))
301             .WillOnce(Return(ByMove(ScopedAStatus::ok())));
302 
303     auto status = mWatchdogServiceHelper->checkIfAlive(mMockCarWatchdogServiceForSystem->asBinder(),
304                                                        0, TimeoutLength::TIMEOUT_CRITICAL);
305 
306     ASSERT_TRUE(status.isOk()) << status.getMessage();
307 }
308 
TEST_F(WatchdogServiceHelperTest,TestErrorOnCheckIfAliveWithNotRegisteredCarWatchdogServiceBinder)309 TEST_F(WatchdogServiceHelperTest,
310        TestErrorOnCheckIfAliveWithNotRegisteredCarWatchdogServiceBinder) {
311     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
312 
313     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, checkIfAlive(_, _)).Times(0);
314 
315     auto notRegisteredService = SharedRefBase::make<MockCarWatchdogServiceForSystem>();
316     auto status = mWatchdogServiceHelper->checkIfAlive(notRegisteredService->asBinder(), 0,
317                                                        TimeoutLength::TIMEOUT_CRITICAL);
318 
319     ASSERT_FALSE(status.isOk()) << "checkIfAlive should fail when the given car watchdog service"
320                                    "binder is not registered with the helper";
321 }
322 
TEST_F(WatchdogServiceHelperTest,TestErrorOnCheckIfAliveWithNoCarWatchdogServiceRegistered)323 TEST_F(WatchdogServiceHelperTest, TestErrorOnCheckIfAliveWithNoCarWatchdogServiceRegistered) {
324     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, checkIfAlive(_, _)).Times(0);
325 
326     auto status = mWatchdogServiceHelper->checkIfAlive(mMockCarWatchdogServiceForSystem->asBinder(),
327                                                        0, TimeoutLength::TIMEOUT_CRITICAL);
328 
329     ASSERT_FALSE(status.isOk()) << "checkIfAlive " << kFailOnNoCarWatchdogServiceMessage;
330 }
331 
TEST_F(WatchdogServiceHelperTest,TestErrorOnCheckIfAliveWithErrorStatusFromCarWatchdogService)332 TEST_F(WatchdogServiceHelperTest, TestErrorOnCheckIfAliveWithErrorStatusFromCarWatchdogService) {
333     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
334 
335     EXPECT_CALL(*mMockCarWatchdogServiceForSystem,
336                 checkIfAlive(0, InternalTimeoutLength::TIMEOUT_CRITICAL))
337             .WillOnce(Return(ByMove(ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE,
338                                                                                 "Illegal state"))));
339 
340     auto status = mWatchdogServiceHelper->checkIfAlive(mMockCarWatchdogServiceForSystem->asBinder(),
341                                                        0, TimeoutLength::TIMEOUT_CRITICAL);
342     ASSERT_FALSE(status.isOk()) << "checkIfAlive " << kFailOnCarWatchdogServiceErrMessage;
343 }
344 
TEST_F(WatchdogServiceHelperTest,TestPrepareProcessTermination)345 TEST_F(WatchdogServiceHelperTest, TestPrepareProcessTermination) {
346     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
347 
348     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, prepareProcessTermination())
349             .WillOnce(Return(ByMove(ScopedAStatus::ok())));
350 
351     EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(_)).Times(0);
352 
353     auto status = mWatchdogServiceHelper->prepareProcessTermination(
354             mMockCarWatchdogServiceForSystem->asBinder());
355 
356     ASSERT_TRUE(status.isOk()) << status.getMessage();
357 
358     ASSERT_FALSE(mWatchdogServiceHelper->isServiceConnected());
359 }
360 
TEST_F(WatchdogServiceHelperTest,TestErrorOnPrepareProcessTerminationWithNotRegisteredCarWatchdogServiceBinder)361 TEST_F(WatchdogServiceHelperTest,
362        TestErrorOnPrepareProcessTerminationWithNotRegisteredCarWatchdogServiceBinder) {
363     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
364 
365     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, prepareProcessTermination()).Times(0);
366 
367     EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(_)).Times(0);
368 
369     auto notRegisteredService = SharedRefBase::make<MockCarWatchdogServiceForSystem>();
370     auto status =
371             mWatchdogServiceHelper->prepareProcessTermination(notRegisteredService->asBinder());
372 
373     ASSERT_FALSE(status.isOk()) << "prepareProcessTermination should fail when the given car "
374                                    "watchdog service binder is not registered with the helper";
375 }
376 
TEST_F(WatchdogServiceHelperTest,TestErrorOnPrepareProcessTerminationWithNoCarWatchdogServiceRegistered)377 TEST_F(WatchdogServiceHelperTest,
378        TestErrorOnPrepareProcessTerminationWithNoCarWatchdogServiceRegistered) {
379     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, prepareProcessTermination()).Times(0);
380 
381     EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(_)).Times(0);
382 
383     ASSERT_FALSE(mWatchdogServiceHelper
384                          ->prepareProcessTermination(mMockCarWatchdogServiceForSystem->asBinder())
385                          .isOk())
386             << "prepareProcessTermination " << kFailOnNoCarWatchdogServiceMessage;
387 }
388 
TEST_F(WatchdogServiceHelperTest,TestErrorOnPrepareProcessTerminationWithErrorStatusFromCarWatchdogService)389 TEST_F(WatchdogServiceHelperTest,
390        TestErrorOnPrepareProcessTerminationWithErrorStatusFromCarWatchdogService) {
391     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
392 
393     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, prepareProcessTermination())
394             .WillOnce(Return(ByMove(ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE,
395                                                                                 "Illegal state"))));
396 
397     EXPECT_CALL(*mMockWatchdogProcessService, unregisterCarWatchdogService(_)).Times(0);
398 
399     ASSERT_FALSE(mWatchdogServiceHelper
400                          ->prepareProcessTermination(mMockCarWatchdogServiceForSystem->asBinder())
401                          .isOk())
402             << "prepareProcessTermination " << kFailOnCarWatchdogServiceErrMessage;
403 }
404 
TEST_F(WatchdogServiceHelperTest,TestGetPackageInfosForUids)405 TEST_F(WatchdogServiceHelperTest, TestGetPackageInfosForUids) {
406     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
407 
408     std::vector<int32_t> uids = {1000};
409     std::vector<std::string> prefixesStr = {"vendor.package"};
410     std::vector<PackageInfo> expectedPackageInfo{
411             constructPackageInfo("vendor.package.A", 120000, UidType::NATIVE, ComponentType::VENDOR,
412                                  ApplicationCategoryType::OTHERS),
413             constructPackageInfo("third_party.package.B", 130000, UidType::APPLICATION,
414                                  ComponentType::THIRD_PARTY, ApplicationCategoryType::OTHERS),
415     };
416 
417     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, getPackageInfosForUids(uids, prefixesStr, _))
418             .WillOnce(DoAll(SetArgPointee<2>(expectedPackageInfo),
419                             Return(ByMove(ScopedAStatus::ok()))));
420 
421     std::vector<PackageInfo> actualPackageInfo;
422     auto status =
423             mWatchdogServiceHelper->getPackageInfosForUids(uids, prefixesStr, &actualPackageInfo);
424 
425     ASSERT_TRUE(status.isOk()) << status.getMessage();
426     EXPECT_THAT(actualPackageInfo, UnorderedElementsAreArray(expectedPackageInfo));
427 }
428 
TEST_F(WatchdogServiceHelperTest,TestErrorOnGetPackageInfosForUidsWithNoCarWatchdogServiceRegistered)429 TEST_F(WatchdogServiceHelperTest,
430        TestErrorOnGetPackageInfosForUidsWithNoCarWatchdogServiceRegistered) {
431     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, getPackageInfosForUids(_, _, _)).Times(0);
432 
433     std::vector<int32_t> uids;
434     std::vector<std::string> prefixes;
435     std::vector<PackageInfo> actualPackageInfo;
436     auto status =
437             mWatchdogServiceHelper->getPackageInfosForUids(uids, prefixes, &actualPackageInfo);
438 
439     ASSERT_FALSE(status.isOk()) << "getPackageInfosForUids " << kFailOnNoCarWatchdogServiceMessage;
440     EXPECT_THAT(actualPackageInfo, IsEmpty());
441 }
442 
TEST_F(WatchdogServiceHelperTest,TestErrorOnGetPackageInfosForUidsWithErrorStatusFromCarWatchdogService)443 TEST_F(WatchdogServiceHelperTest,
444        TestErrorOnGetPackageInfosForUidsWithErrorStatusFromCarWatchdogService) {
445     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
446 
447     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, getPackageInfosForUids(_, _, _))
448             .WillOnce(Return(ByMove(ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE,
449                                                                                 "Illegal state"))));
450 
451     std::vector<int32_t> uids;
452     std::vector<std::string> prefixes;
453     std::vector<PackageInfo> actualPackageInfo;
454     auto status =
455             mWatchdogServiceHelper->getPackageInfosForUids(uids, prefixes, &actualPackageInfo);
456 
457     ASSERT_FALSE(status.isOk()) << "getPackageInfosForUids " << kFailOnCarWatchdogServiceErrMessage;
458     ASSERT_TRUE(actualPackageInfo.empty());
459 }
460 
TEST_F(WatchdogServiceHelperTest,TestResetResourceOveruseStats)461 TEST_F(WatchdogServiceHelperTest, TestResetResourceOveruseStats) {
462     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
463 
464     std::vector<std::string> packageNames = {"system.daemon"};
465     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, resetResourceOveruseStats(packageNames))
466             .WillOnce(Return(ByMove(ScopedAStatus::ok())));
467 
468     auto status = mWatchdogServiceHelper->resetResourceOveruseStats(packageNames);
469 
470     ASSERT_TRUE(status.isOk()) << status.getMessage();
471 }
472 
TEST_F(WatchdogServiceHelperTest,TestErrorsOnResetResourceOveruseStatsWithNoCarWatchdogServiceRegistered)473 TEST_F(WatchdogServiceHelperTest,
474        TestErrorsOnResetResourceOveruseStatsWithNoCarWatchdogServiceRegistered) {
475     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, resetResourceOveruseStats(_)).Times(0);
476 
477     ASSERT_FALSE(mWatchdogServiceHelper->resetResourceOveruseStats({}).isOk())
478             << "resetResourceOveruseStats " << kFailOnNoCarWatchdogServiceMessage;
479 }
480 
TEST_F(WatchdogServiceHelperTest,TestErrorsOnResetResourceOveruseStatsWithErrorStatusFromCarWatchdogService)481 TEST_F(WatchdogServiceHelperTest,
482        TestErrorsOnResetResourceOveruseStatsWithErrorStatusFromCarWatchdogService) {
483     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
484 
485     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, resetResourceOveruseStats(_))
486             .WillOnce(Return(ByMove(ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE,
487                                                                                 "Illegal state"))));
488 
489     ASSERT_FALSE(mWatchdogServiceHelper->resetResourceOveruseStats({}).isOk())
490             << "resetResourceOveruseStats " << kFailOnCarWatchdogServiceErrMessage;
491 }
492 
TEST_F(WatchdogServiceHelperTest,TestRequestTodayIoUsageStats)493 TEST_F(WatchdogServiceHelperTest, TestRequestTodayIoUsageStats) {
494     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
495 
496     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, requestTodayIoUsageStats())
497             .WillOnce(Return(ByMove(ScopedAStatus::ok())));
498 
499     auto status = mWatchdogServiceHelper->requestTodayIoUsageStats();
500 
501     ASSERT_TRUE(status.isOk()) << status.getMessage();
502 }
503 
TEST_F(WatchdogServiceHelperTest,TestErrorOnRequestTodayIoUsageStatsWithNoCarWatchdogServiceRegistered)504 TEST_F(WatchdogServiceHelperTest,
505        TestErrorOnRequestTodayIoUsageStatsWithNoCarWatchdogServiceRegistered) {
506     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, requestTodayIoUsageStats()).Times(0);
507 
508     ASSERT_FALSE(mWatchdogServiceHelper->requestTodayIoUsageStats().isOk())
509             << "requestTodayIoUsageStats " << kFailOnNoCarWatchdogServiceMessage;
510 }
511 
TEST_F(WatchdogServiceHelperTest,TestErrorOnRequestTodayIoUsageStatsWithErrorStatusFromCarWatchdogService)512 TEST_F(WatchdogServiceHelperTest,
513        TestErrorOnRequestTodayIoUsageStatsWithErrorStatusFromCarWatchdogService) {
514     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
515 
516     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, requestTodayIoUsageStats())
517             .WillOnce(Return(ByMove(ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE,
518                                                                                 "Illegal state"))));
519 
520     ASSERT_FALSE(mWatchdogServiceHelper->requestTodayIoUsageStats().isOk())
521             << "requestTodayIoUsageStats " << kFailOnCarWatchdogServiceErrMessage;
522 }
523 
TEST_F(WatchdogServiceHelperTest,TestOnLatestResourceStats)524 TEST_F(WatchdogServiceHelperTest, TestOnLatestResourceStats) {
525     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
526 
527     PackageIoOveruseStats stats;
528     stats.uid = 101000;
529     stats.ioOveruseStats.killableOnOveruse = true;
530     stats.ioOveruseStats.startTime = 99898;
531     stats.ioOveruseStats.durationInSeconds = 12345;
532     stats.ioOveruseStats.totalOveruses = 10;
533     stats.shouldNotify = true;
534     std::vector<PackageIoOveruseStats> expectedIoOveruseStats = {stats};
535 
536     std::vector<ResourceStats> expectedResourceStats;
537     expectedResourceStats.push_back({
538             .resourceOveruseStats = std::make_optional<ResourceOveruseStats>({
539                     .packageIoOveruseStats = expectedIoOveruseStats,
540             }),
541     });
542 
543     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, onLatestResourceStats(expectedResourceStats))
544             .WillOnce(Return(ByMove(ScopedAStatus::ok())));
545 
546     auto status = mWatchdogServiceHelper->onLatestResourceStats(expectedResourceStats);
547 
548     ASSERT_TRUE(status.isOk()) << status.getMessage();
549 }
550 
TEST_F(WatchdogServiceHelperTest,TestErrorsOnLatestResourceStatsWithNoCarWatchdogServiceRegistered)551 TEST_F(WatchdogServiceHelperTest,
552        TestErrorsOnLatestResourceStatsWithNoCarWatchdogServiceRegistered) {
553     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, onLatestResourceStats(_)).Times(0);
554 
555     ASSERT_FALSE(mWatchdogServiceHelper->onLatestResourceStats({}).isOk())
556             << "onLatestResourceStats " << kFailOnNoCarWatchdogServiceMessage;
557 }
558 
TEST_F(WatchdogServiceHelperTest,TestErrorsOnLatestResourceStatsWithErrorStatusFromCarWatchdogService)559 TEST_F(WatchdogServiceHelperTest,
560        TestErrorsOnLatestResourceStatsWithErrorStatusFromCarWatchdogService) {
561     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
562 
563     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, onLatestResourceStats(_))
564             .WillOnce(Return(ByMove(ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE,
565                                                                                 "Illegal state"))));
566 
567     ASSERT_FALSE(mWatchdogServiceHelper->onLatestResourceStats({}).isOk())
568             << "onLatestResourceStats " << kFailOnCarWatchdogServiceErrMessage;
569 }
570 
TEST_F(WatchdogServiceHelperTest,TestRequestAidlVhalPid)571 TEST_F(WatchdogServiceHelperTest, TestRequestAidlVhalPid) {
572     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
573 
574     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, requestAidlVhalPid())
575             .WillOnce(Return(ByMove(ScopedAStatus::ok())));
576 
577     auto status = mWatchdogServiceHelper->requestAidlVhalPid();
578 
579     ASSERT_TRUE(status.isOk()) << status.getMessage();
580 }
581 
TEST_F(WatchdogServiceHelperTest,TestRequestAidlVhalPidWithNoCarWatchdogServiceRegistered)582 TEST_F(WatchdogServiceHelperTest, TestRequestAidlVhalPidWithNoCarWatchdogServiceRegistered) {
583     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, requestAidlVhalPid()).Times(0);
584 
585     ASSERT_FALSE(mWatchdogServiceHelper->requestAidlVhalPid().isOk())
586             << "requestAidlVhalPid " << kFailOnNoCarWatchdogServiceMessage;
587 }
588 
TEST_F(WatchdogServiceHelperTest,TestRequestAidlVhalPidWithErrorStatusFromCarWatchdogService)589 TEST_F(WatchdogServiceHelperTest, TestRequestAidlVhalPidWithErrorStatusFromCarWatchdogService) {
590     ASSERT_NO_FATAL_FAILURE(registerCarWatchdogService());
591 
592     EXPECT_CALL(*mMockCarWatchdogServiceForSystem, requestAidlVhalPid())
593             .WillOnce(Return(ByMove(ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_STATE,
594                                                                                 "Illegal state"))));
595 
596     ASSERT_FALSE(mWatchdogServiceHelper->requestAidlVhalPid().isOk())
597             << "requestAidlVhalPid " << kFailOnCarWatchdogServiceErrMessage;
598 }
599 
600 }  // namespace watchdog
601 }  // namespace automotive
602 }  // namespace android
603