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 "IvnAndroidDeviceService.h"
18 
19 #include <aidl/android/hardware/automotive/ivn/EndpointInfo.h>
20 #include <aidl/android/hardware/automotive/ivn/OccupantType.h>
21 #include <aidl/android/hardware/automotive/ivn/OccupantZoneInfo.h>
22 #include <android-base/file.h>
23 #include <gtest/gtest.h>
24 
25 namespace android {
26 namespace hardware {
27 namespace automotive {
28 namespace ivn {
29 
30 using ::aidl::android::hardware::automotive::ivn::ConnectProtocol;
31 using ::aidl::android::hardware::automotive::ivn::EndpointInfo;
32 using ::aidl::android::hardware::automotive::ivn::OccupantType;
33 using ::aidl::android::hardware::automotive::ivn::OccupantZoneInfo;
34 using ::ndk::ScopedAStatus;
35 
36 class IvnAndroidDeviceServiceUnitTest : public ::testing::Test {
37   public:
SetUp()38     virtual void SetUp() override {
39         mService = ndk::SharedRefBase::make<IvnAndroidDeviceService>(
40                 android::base::GetExecutableDirectory() + "/DefaultConfig.json");
41         mService->init();
42     }
43 
44     std::shared_ptr<IvnAndroidDeviceService> mService;
45 };
46 
TEST_F(IvnAndroidDeviceServiceUnitTest,TestGetMyDeviceId)47 TEST_F(IvnAndroidDeviceServiceUnitTest, TestGetMyDeviceId) {
48     int deviceId = -1;
49 
50     ScopedAStatus status = mService->getMyDeviceId(&deviceId);
51 
52     ASSERT_TRUE(status.isOk());
53     ASSERT_EQ(deviceId, 0);
54 }
55 
TEST_F(IvnAndroidDeviceServiceUnitTest,TestGetOtherDeviceIds)56 TEST_F(IvnAndroidDeviceServiceUnitTest, TestGetOtherDeviceIds) {
57     std::vector<int> deviceIds;
58 
59     ScopedAStatus status = mService->getOtherDeviceIds(&deviceIds);
60 
61     ASSERT_TRUE(status.isOk());
62     ASSERT_EQ(deviceIds, std::vector<int>({1}));
63 }
64 
TEST_F(IvnAndroidDeviceServiceUnitTest,TestGetDeviceIdForOccupantZone)65 TEST_F(IvnAndroidDeviceServiceUnitTest, TestGetDeviceIdForOccupantZone) {
66     int deviceId = -1;
67 
68     ScopedAStatus status = mService->getDeviceIdForOccupantZone(/*zoneId=*/0, &deviceId);
69 
70     ASSERT_TRUE(status.isOk());
71     EXPECT_EQ(deviceId, 0);
72 
73     status = mService->getDeviceIdForOccupantZone(/*zoneId=*/1, &deviceId);
74 
75     ASSERT_TRUE(status.isOk());
76     EXPECT_EQ(deviceId, 0);
77 
78     status = mService->getDeviceIdForOccupantZone(/*zoneId=*/2, &deviceId);
79 
80     ASSERT_TRUE(status.isOk());
81     EXPECT_EQ(deviceId, 1);
82 
83     status = mService->getDeviceIdForOccupantZone(/*zoneId=*/3, &deviceId);
84 
85     ASSERT_TRUE(status.isOk());
86     EXPECT_EQ(deviceId, 1);
87 
88     status = mService->getDeviceIdForOccupantZone(/*zoneId=*/4, &deviceId);
89 
90     ASSERT_FALSE(status.isOk());
91 }
92 
TEST_F(IvnAndroidDeviceServiceUnitTest,TestGetOccupantZonesForDevice)93 TEST_F(IvnAndroidDeviceServiceUnitTest, TestGetOccupantZonesForDevice) {
94     std::vector<OccupantZoneInfo> occupantZones;
95 
96     ScopedAStatus status =
97             mService->getOccupantZonesForDevice(/*androidDeviceId=*/0, &occupantZones);
98 
99     ASSERT_TRUE(status.isOk());
100     EXPECT_EQ(occupantZones.size(), 2);
101     if (occupantZones.size() == 2) {
102         EXPECT_EQ(occupantZones[0].zoneId, 0);
103         EXPECT_EQ(occupantZones[0].occupantType, OccupantType::DRIVER);
104         EXPECT_EQ(occupantZones[0].seat, 1);
105         EXPECT_EQ(occupantZones[1].zoneId, 1);
106         EXPECT_EQ(occupantZones[1].occupantType, OccupantType::FRONT_PASSENGER);
107         EXPECT_EQ(occupantZones[1].seat, 4);
108     }
109 }
110 
TEST_F(IvnAndroidDeviceServiceUnitTest,TestGetMyEndpointInfo)111 TEST_F(IvnAndroidDeviceServiceUnitTest, TestGetMyEndpointInfo) {
112     EndpointInfo endpointInfo;
113 
114     ScopedAStatus status = mService->getMyEndpointInfo(&endpointInfo);
115 
116     ASSERT_TRUE(status.isOk());
117     EXPECT_EQ(endpointInfo.connectProtocol, ConnectProtocol::TCP_IP);
118     EXPECT_EQ(endpointInfo.ipAddress, "10.10.10.1");
119     EXPECT_EQ(endpointInfo.portNumber, 1234);
120     EXPECT_EQ(endpointInfo.hardwareId.brandName, "MyBrand");
121     EXPECT_EQ(endpointInfo.hardwareId.deviceName, "MyDevice");
122     EXPECT_EQ(endpointInfo.hardwareId.productName, "MyProduct");
123     EXPECT_EQ(endpointInfo.hardwareId.manufacturerName, "MyCompany");
124     EXPECT_EQ(endpointInfo.hardwareId.modelName, "MyModel");
125     EXPECT_EQ(endpointInfo.hardwareId.serialNumber, "Serial1234");
126 }
127 
TEST_F(IvnAndroidDeviceServiceUnitTest,TestGetEndpointInfoForDevice)128 TEST_F(IvnAndroidDeviceServiceUnitTest, TestGetEndpointInfoForDevice) {
129     EndpointInfo endpointInfo;
130 
131     ScopedAStatus status = mService->getEndpointInfoForDevice(/*androidDeviceId=*/0, &endpointInfo);
132 
133     ASSERT_TRUE(status.isOk());
134     EXPECT_EQ(endpointInfo.connectProtocol, ConnectProtocol::TCP_IP);
135     EXPECT_EQ(endpointInfo.ipAddress, "10.10.10.1");
136     EXPECT_EQ(endpointInfo.portNumber, 1234);
137 
138     status = mService->getEndpointInfoForDevice(/*androidDeviceId=*/1, &endpointInfo);
139 
140     ASSERT_TRUE(status.isOk());
141     EXPECT_EQ(endpointInfo.connectProtocol, ConnectProtocol::TCP_IP);
142     EXPECT_EQ(endpointInfo.ipAddress, "10.10.10.2");
143     EXPECT_EQ(endpointInfo.portNumber, 2345);
144 }
145 
146 }  // namespace ivn
147 }  // namespace automotive
148 }  // namespace hardware
149 }  // namespace android
150