From 5bc39cc1398f44b447fbeb41a91e3c7e0349679d Mon Sep 17 00:00:00 2001 From: Yu Shan Date: Mon, 10 Apr 2023 15:54:54 -0700 Subject: [PATCH] Support dump and add more unit tests. Support dump in reference ivn HAL and add more unit tests. Test: atest IvnAndroidDeviceServiceUnitTest Bug: 274139217 Change-Id: I3d882dcf84f8ae89104e06deb65fbad148fad54c --- .../default/include/IvnAndroidDeviceService.h | 4 ++ .../default/src/IvnAndroidDeviceService.cpp | 20 ++++++---- .../test/IvnAndroidDeviceServiceUnittest.cpp | 39 +++++++++++++++++++ 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/automotive/ivn_android_device/impl/default/include/IvnAndroidDeviceService.h b/automotive/ivn_android_device/impl/default/include/IvnAndroidDeviceService.h index c0cc9fea2d..0cff8fe19a 100644 --- a/automotive/ivn_android_device/impl/default/include/IvnAndroidDeviceService.h +++ b/automotive/ivn_android_device/impl/default/include/IvnAndroidDeviceService.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -60,7 +61,10 @@ class IvnAndroidDeviceService int androidDeviceId, aidl::android::hardware::automotive::ivn::EndpointInfo* endpointInfo) override; + binder_status_t dump(int fd, const char** args, uint32_t numArgs) override; + private: + Json::Value mConfigRootNode; int mMyDeviceId; std::unordered_map mDeviceInfoById; std::string_view mConfigPath; diff --git a/automotive/ivn_android_device/impl/default/src/IvnAndroidDeviceService.cpp b/automotive/ivn_android_device/impl/default/src/IvnAndroidDeviceService.cpp index 71454d5d40..81f18b2267 100644 --- a/automotive/ivn_android_device/impl/default/src/IvnAndroidDeviceService.cpp +++ b/automotive/ivn_android_device/impl/default/src/IvnAndroidDeviceService.cpp @@ -54,26 +54,25 @@ bool IvnAndroidDeviceService::init() { return false; } Json::CharReaderBuilder builder; - Json::Value root; std::string errs; - if (!Json::parseFromStream(builder, configStream, &root, &errs)) { + if (!Json::parseFromStream(builder, configStream, &mConfigRootNode, &errs)) { LOG(ERROR) << "Failed to parse config JSON stream, error: " << errs; return false; } - if (!root.isObject()) { + if (!mConfigRootNode.isObject()) { LOG(ERROR) << "Root must be an object"; return false; } - if (!root.isMember("MyDeviceId")) { + if (!mConfigRootNode.isMember("MyDeviceId")) { LOG(ERROR) << "Must contain 'MyDeviceId' field"; return false; } - mMyDeviceId = root["MyDeviceId"].asInt(); - if (!root.isMember("Devices") || !root["Devices"].isArray()) { + mMyDeviceId = mConfigRootNode["MyDeviceId"].asInt(); + if (!mConfigRootNode.isMember("Devices") || !mConfigRootNode["Devices"].isArray()) { LOG(ERROR) << "Must contain 'Devices' field as array"; return false; } - Json::Value& devices = root["Devices"]; + Json::Value& devices = mConfigRootNode["Devices"]; for (unsigned int i = 0; i < devices.size(); i++) { Json::Value& device = devices[i]; int deviceId = device["DeviceId"].asInt(); @@ -190,6 +189,13 @@ ScopedAStatus IvnAndroidDeviceService::getEndpointInfoForDevice(int androidDevic return ScopedAStatus::ok(); } +binder_status_t IvnAndroidDeviceService::dump(int fd, [[maybe_unused]] const char** args, + [[maybe_unused]] uint32_t numArgs) { + dprintf(fd, "IVN Android Device debug interface, Config: \n%s\n", + mConfigRootNode.toStyledString().c_str()); + return STATUS_OK; +} + } // namespace ivn } // namespace automotive } // namespace hardware diff --git a/automotive/ivn_android_device/impl/default/test/IvnAndroidDeviceServiceUnittest.cpp b/automotive/ivn_android_device/impl/default/test/IvnAndroidDeviceServiceUnittest.cpp index bc8e69fd8f..6a4d26d8c3 100644 --- a/automotive/ivn_android_device/impl/default/test/IvnAndroidDeviceServiceUnittest.cpp +++ b/automotive/ivn_android_device/impl/default/test/IvnAndroidDeviceServiceUnittest.cpp @@ -16,6 +16,7 @@ #include "IvnAndroidDeviceService.h" +#include #include #include #include @@ -26,6 +27,8 @@ namespace hardware { namespace automotive { namespace ivn { +using ::aidl::android::hardware::automotive::ivn::ConnectProtocol; +using ::aidl::android::hardware::automotive::ivn::EndpointInfo; using ::aidl::android::hardware::automotive::ivn::OccupantType; using ::aidl::android::hardware::automotive::ivn::OccupantZoneInfo; using ::ndk::ScopedAStatus; @@ -92,6 +95,7 @@ TEST_F(IvnAndroidDeviceServiceUnitTest, TestGetOccupantZonesForDevice) { ScopedAStatus status = mService->getOccupantZonesForDevice(/*androidDeviceId=*/0, &occupantZones); + ASSERT_TRUE(status.isOk()); EXPECT_EQ(occupantZones.size(), 2); if (occupantZones.size() == 2) { @@ -104,6 +108,41 @@ TEST_F(IvnAndroidDeviceServiceUnitTest, TestGetOccupantZonesForDevice) { } } +TEST_F(IvnAndroidDeviceServiceUnitTest, TestGetMyEndpointInfo) { + EndpointInfo endpointInfo; + + ScopedAStatus status = mService->getMyEndpointInfo(&endpointInfo); + + ASSERT_TRUE(status.isOk()); + EXPECT_EQ(endpointInfo.connectProtocol, ConnectProtocol::TCP_IP); + EXPECT_EQ(endpointInfo.ipAddress, "10.10.10.1"); + EXPECT_EQ(endpointInfo.portNumber, 1234); + EXPECT_EQ(endpointInfo.hardwareId.brandName, "MyBrand"); + EXPECT_EQ(endpointInfo.hardwareId.deviceName, "MyDevice"); + EXPECT_EQ(endpointInfo.hardwareId.productName, "MyProduct"); + EXPECT_EQ(endpointInfo.hardwareId.manufacturerName, "MyCompany"); + EXPECT_EQ(endpointInfo.hardwareId.modelName, "MyModel"); + EXPECT_EQ(endpointInfo.hardwareId.serialNumber, "Serial1234"); +} + +TEST_F(IvnAndroidDeviceServiceUnitTest, TestGetEndpointInfoForDevice) { + EndpointInfo endpointInfo; + + ScopedAStatus status = mService->getEndpointInfoForDevice(/*androidDeviceId=*/0, &endpointInfo); + + ASSERT_TRUE(status.isOk()); + EXPECT_EQ(endpointInfo.connectProtocol, ConnectProtocol::TCP_IP); + EXPECT_EQ(endpointInfo.ipAddress, "10.10.10.1"); + EXPECT_EQ(endpointInfo.portNumber, 1234); + + status = mService->getEndpointInfoForDevice(/*androidDeviceId=*/1, &endpointInfo); + + ASSERT_TRUE(status.isOk()); + EXPECT_EQ(endpointInfo.connectProtocol, ConnectProtocol::TCP_IP); + EXPECT_EQ(endpointInfo.ipAddress, "10.10.10.2"); + EXPECT_EQ(endpointInfo.portNumber, 2345); +} + } // namespace ivn } // namespace automotive } // namespace hardware