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 <aidl/Gtest.h>
18 #include <aidl/Vintf.h>
19 #include <aidl/android/hardware/automotive/ivn/ConnectProtocol.h>
20 #include <aidl/android/hardware/automotive/ivn/EndpointInfo.h>
21 #include <aidl/android/hardware/automotive/ivn/IIvnAndroidDevice.h>
22 #include <android/binder_ibinder.h>
23 #include <android/binder_manager.h>
24 #include <gmock/gmock.h>
25 #include <unordered_set>
26 
27 namespace aidl::android::hardware::automotive::ivn {
28 
29 using ::ndk::ScopedAStatus;
30 using ::ndk::SpAIBinder;
31 
32 using ::testing::Contains;
33 using ::testing::Not;
34 
35 class VtsHalIvnTargetTest : public ::testing::TestWithParam<std::string> {
36   public:
SetUp()37     void SetUp() override {
38         std::string descriptor = GetParam();
39         AIBinder* binder = AServiceManager_checkService(descriptor.c_str());
40         ASSERT_NE(binder, nullptr) << "Failed to connect to IVN HAL";
41         mIvnHal = IIvnAndroidDevice::fromBinder(SpAIBinder(binder));
42     }
43 
getHal()44     std::shared_ptr<IIvnAndroidDevice> getHal() { return mIvnHal; }
45 
46   private:
47     std::shared_ptr<IIvnAndroidDevice> mIvnHal;
48 
49   protected:
50     ScopedAStatus getAllDeviceIds(std::unordered_set<int>* deviceIds);
51 };
52 
TEST_P(VtsHalIvnTargetTest,testDeviceIdIsUnique)53 TEST_P(VtsHalIvnTargetTest, testDeviceIdIsUnique) {
54     std::unordered_set<int> foundDeviceIds;
55     int myDeviceId = 0;
56 
57     ScopedAStatus status = getHal()->getMyDeviceId(&myDeviceId);
58 
59     ASSERT_TRUE(status.isOk()) << "Failed to call getMyDeviceId, status: " << status;
60     foundDeviceIds.insert(myDeviceId);
61 
62     std::vector<int> otherDeviceIds;
63 
64     status = getHal()->getOtherDeviceIds(&otherDeviceIds);
65 
66     ASSERT_TRUE(status.isOk()) << "Failed to call getOtherDeviceIds, status: " << status;
67 
68     for (int deviceId : otherDeviceIds) {
69         EXPECT_THAT(foundDeviceIds, Not(Contains(deviceId))) << "Duplicate device ID: " << deviceId;
70         foundDeviceIds.insert(deviceId);
71     }
72 }
73 
getAllDeviceIds(std::unordered_set<int> * deviceIds)74 ScopedAStatus VtsHalIvnTargetTest::getAllDeviceIds(std::unordered_set<int>* deviceIds) {
75     int myDeviceId = 0;
76     ScopedAStatus status = getHal()->getMyDeviceId(&myDeviceId);
77 
78     if (!status.isOk()) {
79         return status;
80     }
81     deviceIds->insert(myDeviceId);
82     std::vector<int> otherDeviceIds;
83     status = getHal()->getOtherDeviceIds(&otherDeviceIds);
84     if (!status.isOk()) {
85         return status;
86     }
87     for (int otherDeviceId : otherDeviceIds) {
88         deviceIds->insert(otherDeviceId);
89     }
90     return ScopedAStatus::ok();
91 }
92 
TEST_P(VtsHalIvnTargetTest,testDeviceIdOccupantZoneMapping)93 TEST_P(VtsHalIvnTargetTest, testDeviceIdOccupantZoneMapping) {
94     std::unordered_set<int> allDeviceIds;
95 
96     ScopedAStatus status = getAllDeviceIds(&allDeviceIds);
97 
98     ASSERT_FALSE(allDeviceIds.empty());
99     ASSERT_TRUE(status.isOk()) << "Failed to get all device IDs, status: " << status;
100 
101     std::unordered_set<int> foundOccupantZoneIds;
102 
103     for (int deviceId : allDeviceIds) {
104         std::vector<OccupantZoneInfo> occupantZones;
105         status = getHal()->getOccupantZonesForDevice(deviceId, &occupantZones);
106 
107         ASSERT_TRUE(status.isOk())
108                 << "Failed to call getOccupantZonesForDevice, status: " << status;
109         ASSERT_FALSE(occupantZones.empty()) << "No occupant zones for device: " << deviceId;
110 
111         for (const OccupantZoneInfo& occupantZone : occupantZones) {
112             int zoneId = occupantZone.zoneId;
113 
114             EXPECT_THAT(foundOccupantZoneIds, Not(Contains(zoneId)))
115                     << "Duplicate zone ID: " << zoneId;
116 
117             foundOccupantZoneIds.insert(zoneId);
118 
119             int gotDeviceId = 0;
120             status = getHal()->getDeviceIdForOccupantZone(zoneId, &gotDeviceId);
121 
122             ASSERT_TRUE(status.isOk())
123                     << "Failed to call getDeviceIdForOccupantZone, status: " << status;
124             EXPECT_EQ(deviceId, gotDeviceId);
125         }
126     }
127 }
128 
TEST_P(VtsHalIvnTargetTest,testGetEndpointInfo)129 TEST_P(VtsHalIvnTargetTest, testGetEndpointInfo) {
130     EndpointInfo endpointInfo;
131     std::vector<EndpointInfo> foundEndpointInfo;
132 
133     ScopedAStatus status = getHal()->getMyEndpointInfo(&endpointInfo);
134 
135     foundEndpointInfo.push_back(endpointInfo);
136 
137     ASSERT_TRUE(status.isOk()) << "Failed to call getMyEndpointInfo, status: " << status;
138     EXPECT_EQ(endpointInfo.connectProtocol, ConnectProtocol::TCP_IP);
139 
140     std::vector<int> otherDeviceIds;
141     status = getHal()->getOtherDeviceIds(&otherDeviceIds);
142 
143     ASSERT_TRUE(status.isOk()) << "Failed to call getOtherDeviceIds, status: " << status;
144 
145     for (int deviceId : otherDeviceIds) {
146         status = getHal()->getEndpointInfoForDevice(deviceId, &endpointInfo);
147 
148         ASSERT_TRUE(status.isOk()) << "Failed to call getEndpointInfoForDevice, status: " << status;
149         EXPECT_EQ(endpointInfo.connectProtocol, ConnectProtocol::TCP_IP);
150 
151         for (EndpointInfo foundInfo : foundEndpointInfo) {
152             ASSERT_NE(foundInfo, endpointInfo)
153                     << "Found duplicate endpoint info" << endpointInfo.toString();
154         }
155 
156         foundEndpointInfo.push_back(endpointInfo);
157     }
158 }
159 
160 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VtsHalIvnTargetTest);
161 
162 INSTANTIATE_TEST_SUITE_P(
163         PerInstance, VtsHalIvnTargetTest,
164         testing::ValuesIn(::android::getAidlHalInstanceNames(IIvnAndroidDevice::descriptor)),
165         ::android::PrintInstanceNameToString);
166 
167 }  // namespace aidl::android::hardware::automotive::ivn
168