xref: /aosp_15_r20/frameworks/native/libs/binder/ndk/tests/libbinder_ndk_unit_test_host.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2023 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 <android/binder_manager.h>
18 #include <binder/IServiceManager.h>
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <utils/String16.h>
22 #include <utils/String8.h>
23 #include <utils/StrongPointer.h>
24 
25 #include <optional>
26 
27 #include "fakeservicemanager/FakeServiceManager.h"
28 
29 using android::FakeServiceManager;
30 using android::setDefaultServiceManager;
31 using android::sp;
32 using android::String16;
33 using android::String8;
34 using testing::_;
35 using testing::Eq;
36 using testing::Mock;
37 using testing::NiceMock;
38 using testing::Optional;
39 using testing::Return;
40 
41 struct MockServiceManager : FakeServiceManager {
42     MOCK_METHOD1(updatableViaApex, std::optional<String16>(const String16&));
43 };
44 
45 struct AServiceManager : testing::Test {
46     static sp<MockServiceManager> mockSM;
47 
InitMockAServiceManager48     static void InitMock() {
49         mockSM = new NiceMock<MockServiceManager>;
50         setDefaultServiceManager(mockSM);
51     }
52 
TearDownAServiceManager53     void TearDown() override { Mock::VerifyAndClear(mockSM.get()); }
54 
ExpectUpdatableViaApexReturnsAServiceManager55     void ExpectUpdatableViaApexReturns(std::optional<String16> apexName) {
56         EXPECT_CALL(*mockSM, updatableViaApex(_)).WillRepeatedly(Return(apexName));
57     }
58 };
59 
60 sp<MockServiceManager> AServiceManager::mockSM;
61 
TEST_F(AServiceManager,isUpdatableViaApex)62 TEST_F(AServiceManager, isUpdatableViaApex) {
63     auto apexFoo = String16("com.android.hardware.foo");
64     ExpectUpdatableViaApexReturns(apexFoo);
65 
66     bool isUpdatable = AServiceManager_isUpdatableViaApex("android.hardware.foo.IFoo/default");
67     EXPECT_EQ(isUpdatable, true);
68 }
69 
TEST_F(AServiceManager,isUpdatableViaApex_Not)70 TEST_F(AServiceManager, isUpdatableViaApex_Not) {
71     ExpectUpdatableViaApexReturns(std::nullopt);
72 
73     bool isUpdatable = AServiceManager_isUpdatableViaApex("android.hardware.foo.IFoo/default");
74     EXPECT_EQ(isUpdatable, false);
75 }
76 
getUpdatableApexNameCallback(const char * apexName,void * context)77 void getUpdatableApexNameCallback(const char* apexName, void* context) {
78     *(static_cast<std::optional<std::string>*>(context)) = apexName;
79 }
80 
TEST_F(AServiceManager,getUpdatableApexName)81 TEST_F(AServiceManager, getUpdatableApexName) {
82     auto apexFoo = String16("com.android.hardware.foo");
83     ExpectUpdatableViaApexReturns(apexFoo);
84 
85     std::optional<std::string> result;
86     AServiceManager_getUpdatableApexName("android.hardware.foo.IFoo/default", &result,
87                                          getUpdatableApexNameCallback);
88     EXPECT_THAT(result, Optional(std::string(String8(apexFoo))));
89 }
90 
TEST_F(AServiceManager,getUpdatableApexName_Null)91 TEST_F(AServiceManager, getUpdatableApexName_Null) {
92     ExpectUpdatableViaApexReturns(std::nullopt);
93 
94     std::optional<std::string> result;
95     AServiceManager_getUpdatableApexName("android.hardware.foo.IFoo/default", &result,
96                                          getUpdatableApexNameCallback);
97     EXPECT_THAT(result, Eq(std::nullopt));
98 }
99 
main(int argc,char * argv[])100 int main(int argc, char* argv[]) {
101     ::testing::InitGoogleTest(&argc, argv);
102     AServiceManager::InitMock();
103     return RUN_ALL_TESTS();
104 }
105