Merge "Refactor BindToDeviceSocketMutator." into udc-dev
diff --git a/automotive/ivn_android_device/impl/default/include/IvnAndroidDeviceService.h b/automotive/ivn_android_device/impl/default/include/IvnAndroidDeviceService.h
index c0cc9fe..0cff8fe 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 <aidl/android/hardware/automotive/ivn/EndpointInfo.h>
 #include <aidl/android/hardware/automotive/ivn/OccupantZoneInfo.h>
 #include <android/binder_auto_utils.h>
+#include <json/json.h>
 #include <vector>
 
 #include <unordered_map>
@@ -60,7 +61,10 @@
             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<int, DeviceInfo> 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 71454d5..81f18b2 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 @@
         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 @@
     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 bc8e69f..6a4d26d 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 <aidl/android/hardware/automotive/ivn/EndpointInfo.h>
 #include <aidl/android/hardware/automotive/ivn/OccupantType.h>
 #include <aidl/android/hardware/automotive/ivn/OccupantZoneInfo.h>
 #include <android-base/file.h>
@@ -26,6 +27,8 @@
 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 @@
 
     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, 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
diff --git a/automotive/ivn_android_device/vts/Android.bp b/automotive/ivn_android_device/vts/Android.bp
new file mode 100644
index 0000000..e4b9d64
--- /dev/null
+++ b/automotive/ivn_android_device/vts/Android.bp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package {
+    // See: http://go/android-license-faq
+    // A large-scale-change added 'default_applicable_licenses' to import
+    // all of the 'license_kinds' from "hardware_interfaces_license"
+    // to get the below license kinds:
+    //   SPDX-license-identifier-Apache-2.0
+    default_applicable_licenses: ["hardware_interfaces_license"],
+}
+
+cc_test {
+    name: "VtsHalIvnTargetTest",
+    srcs: [
+        "src/*.cpp",
+    ],
+    defaults: ["use_libaidlvintf_gtest_helper_static"],
+    static_libs: [
+        "libgmock",
+        "libgtest",
+        "android.hardware.automotive.ivn-V1-ndk",
+    ],
+    shared_libs: [
+        "libbinder_ndk",
+    ],
+    test_suites: [
+        "general-tests",
+        "vts",
+        "automotive-tests",
+        "automotive-general-tests",
+    ],
+    require_root: true,
+}
diff --git a/automotive/ivn_android_device/vts/OWNERS b/automotive/ivn_android_device/vts/OWNERS
new file mode 100644
index 0000000..d6969e5
--- /dev/null
+++ b/automotive/ivn_android_device/vts/OWNERS
@@ -0,0 +1,2 @@
+ericjeong@google.com
+shanyu@google.com
diff --git a/automotive/ivn_android_device/vts/src/VtsHalIvnTargetTest.cpp b/automotive/ivn_android_device/vts/src/VtsHalIvnTargetTest.cpp
new file mode 100644
index 0000000..73b9a5e
--- /dev/null
+++ b/automotive/ivn_android_device/vts/src/VtsHalIvnTargetTest.cpp
@@ -0,0 +1,167 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <aidl/Gtest.h>
+#include <aidl/Vintf.h>
+#include <aidl/android/hardware/automotive/ivn/ConnectProtocol.h>
+#include <aidl/android/hardware/automotive/ivn/EndpointInfo.h>
+#include <aidl/android/hardware/automotive/ivn/IIvnAndroidDevice.h>
+#include <android/binder_ibinder.h>
+#include <android/binder_manager.h>
+#include <gmock/gmock.h>
+#include <unordered_set>
+
+namespace aidl::android::hardware::automotive::ivn {
+
+using ::ndk::ScopedAStatus;
+using ::ndk::SpAIBinder;
+
+using ::testing::Contains;
+using ::testing::Not;
+
+class VtsHalIvnTargetTest : public ::testing::TestWithParam<std::string> {
+  public:
+    void SetUp() override {
+        std::string descriptor = GetParam();
+        AIBinder* binder = AServiceManager_checkService(descriptor.c_str());
+        ASSERT_NE(binder, nullptr) << "Failed to connect to IVN HAL";
+        mIvnHal = IIvnAndroidDevice::fromBinder(SpAIBinder(binder));
+    }
+
+    std::shared_ptr<IIvnAndroidDevice> getHal() { return mIvnHal; }
+
+  private:
+    std::shared_ptr<IIvnAndroidDevice> mIvnHal;
+
+  protected:
+    ScopedAStatus getAllDeviceIds(std::unordered_set<int>* deviceIds);
+};
+
+TEST_P(VtsHalIvnTargetTest, testDeviceIdIsUnique) {
+    std::unordered_set<int> foundDeviceIds;
+    int myDeviceId = 0;
+
+    ScopedAStatus status = getHal()->getMyDeviceId(&myDeviceId);
+
+    ASSERT_TRUE(status.isOk()) << "Failed to call getMyDeviceId, status: " << status;
+    foundDeviceIds.insert(myDeviceId);
+
+    std::vector<int> otherDeviceIds;
+
+    status = getHal()->getOtherDeviceIds(&otherDeviceIds);
+
+    ASSERT_TRUE(status.isOk()) << "Failed to call getOtherDeviceIds, status: " << status;
+
+    for (int deviceId : otherDeviceIds) {
+        EXPECT_THAT(foundDeviceIds, Not(Contains(deviceId))) << "Duplicate device ID: " << deviceId;
+        foundDeviceIds.insert(deviceId);
+    }
+}
+
+ScopedAStatus VtsHalIvnTargetTest::getAllDeviceIds(std::unordered_set<int>* deviceIds) {
+    int myDeviceId = 0;
+    ScopedAStatus status = getHal()->getMyDeviceId(&myDeviceId);
+
+    if (!status.isOk()) {
+        return status;
+    }
+    deviceIds->insert(myDeviceId);
+    std::vector<int> otherDeviceIds;
+    status = getHal()->getOtherDeviceIds(&otherDeviceIds);
+    if (!status.isOk()) {
+        return status;
+    }
+    for (int otherDeviceId : otherDeviceIds) {
+        deviceIds->insert(otherDeviceId);
+    }
+    return ScopedAStatus::ok();
+}
+
+TEST_P(VtsHalIvnTargetTest, testDeviceIdOccupantZoneMapping) {
+    std::unordered_set<int> allDeviceIds;
+
+    ScopedAStatus status = getAllDeviceIds(&allDeviceIds);
+
+    ASSERT_FALSE(allDeviceIds.empty());
+    ASSERT_TRUE(status.isOk()) << "Failed to get all device IDs, status: " << status;
+
+    std::unordered_set<int> foundOccupantZoneIds;
+
+    for (int deviceId : allDeviceIds) {
+        std::vector<OccupantZoneInfo> occupantZones;
+        status = getHal()->getOccupantZonesForDevice(deviceId, &occupantZones);
+
+        ASSERT_TRUE(status.isOk())
+                << "Failed to call getOccupantZonesForDevice, status: " << status;
+        ASSERT_FALSE(occupantZones.empty()) << "No occupant zones for device: " << deviceId;
+
+        for (const OccupantZoneInfo& occupantZone : occupantZones) {
+            int zoneId = occupantZone.zoneId;
+
+            EXPECT_THAT(foundOccupantZoneIds, Not(Contains(zoneId)))
+                    << "Duplicate zone ID: " << zoneId;
+
+            foundOccupantZoneIds.insert(zoneId);
+
+            int gotDeviceId = 0;
+            status = getHal()->getDeviceIdForOccupantZone(zoneId, &gotDeviceId);
+
+            ASSERT_TRUE(status.isOk())
+                    << "Failed to call getDeviceIdForOccupantZone, status: " << status;
+            EXPECT_EQ(deviceId, gotDeviceId);
+        }
+    }
+}
+
+TEST_P(VtsHalIvnTargetTest, testGetEndpointInfo) {
+    EndpointInfo endpointInfo;
+    std::vector<EndpointInfo> foundEndpointInfo;
+
+    ScopedAStatus status = getHal()->getMyEndpointInfo(&endpointInfo);
+
+    foundEndpointInfo.push_back(endpointInfo);
+
+    ASSERT_TRUE(status.isOk()) << "Failed to call getMyEndpointInfo, status: " << status;
+    EXPECT_EQ(endpointInfo.connectProtocol, ConnectProtocol::TCP_IP);
+
+    std::vector<int> otherDeviceIds;
+    status = getHal()->getOtherDeviceIds(&otherDeviceIds);
+
+    ASSERT_TRUE(status.isOk()) << "Failed to call getOtherDeviceIds, status: " << status;
+
+    for (int deviceId : otherDeviceIds) {
+        status = getHal()->getEndpointInfoForDevice(deviceId, &endpointInfo);
+
+        ASSERT_TRUE(status.isOk()) << "Failed to call getEndpointInfoForDevice, status: " << status;
+        EXPECT_EQ(endpointInfo.connectProtocol, ConnectProtocol::TCP_IP);
+
+        for (EndpointInfo foundInfo : foundEndpointInfo) {
+            ASSERT_NE(foundInfo, endpointInfo)
+                    << "Found duplicate endpoint info" << endpointInfo.toString();
+        }
+
+        foundEndpointInfo.push_back(endpointInfo);
+    }
+}
+
+GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VtsHalIvnTargetTest);
+
+INSTANTIATE_TEST_SUITE_P(
+        PerInstance, VtsHalIvnTargetTest,
+        testing::ValuesIn(::android::getAidlHalInstanceNames(IIvnAndroidDevice::descriptor)),
+        ::android::PrintInstanceNameToString);
+
+}  // namespace aidl::android::hardware::automotive::ivn
diff --git a/broadcastradio/aidl/android/hardware/broadcastradio/Result.aidl b/broadcastradio/aidl/android/hardware/broadcastradio/Result.aidl
index 9f7263a..f775bd0 100644
--- a/broadcastradio/aidl/android/hardware/broadcastradio/Result.aidl
+++ b/broadcastradio/aidl/android/hardware/broadcastradio/Result.aidl
@@ -39,7 +39,7 @@
     INVALID_ARGUMENTS,
 
     /**
-     * Error used when the service is of invalid state (i.e. callback
+     * Error used when the service is on invalid state (i.e. callback
      * is not registered for IBroadcastRadio).
      */
     INVALID_STATE,
@@ -50,13 +50,13 @@
     NOT_SUPPORTED,
 
     /**
-     * Error used when a tune, seek, step or operation is not completed
-     * within {@link IBroadcastRadio#LIST_COMPLETE_TIMEOUT_MS}.
+     * Error used when a tune, seek, or step operation is not completed
+     * within {@link IBroadcastRadio#TUNER_TIMEOUT_MS}.
      */
     TIMEOUT,
 
     /**
-     * Error used when a tune, seek, step or operation is canceled before
+     * Error used when a tune, seek, or step operation is canceled before
      * being processed.
      */
     CANCELED,
diff --git a/security/rkp/aidl/vts/functional/VtsRemotelyProvisionedComponentTests.cpp b/security/rkp/aidl/vts/functional/VtsRemotelyProvisionedComponentTests.cpp
index 8906543..62463eb 100644
--- a/security/rkp/aidl/vts/functional/VtsRemotelyProvisionedComponentTests.cpp
+++ b/security/rkp/aidl/vts/functional/VtsRemotelyProvisionedComponentTests.cpp
@@ -702,6 +702,7 @@
  * Generate an empty certificate request with all possible length of challenge, and decrypt and
  * verify the structure and content.
  */
+// @VsrTest = 3.10-015
 TEST_P(CertificateRequestV2Test, EmptyRequest) {
     bytevec csr;
 
@@ -721,6 +722,7 @@
  * Generate a non-empty certificate request with all possible length of challenge.  Decrypt, parse
  * and validate the contents.
  */
+// @VsrTest = 3.10-015
 TEST_P(CertificateRequestV2Test, NonEmptyRequest) {
     generateKeys(false /* testMode */, 1 /* numKeys */);
 
@@ -753,6 +755,7 @@
  * Generate a non-empty certificate request.  Make sure contents are reproducible but allow for the
  * signature to be different since algorithms including ECDSA P-256 can include a random value.
  */
+// @VsrTest = 3.10-015
 TEST_P(CertificateRequestV2Test, NonEmptyRequestReproducible) {
     generateKeys(false /* testMode */, 1 /* numKeys */);
 
@@ -776,6 +779,7 @@
 /**
  * Generate a non-empty certificate request with multiple keys.
  */
+// @VsrTest = 3.10-015
 TEST_P(CertificateRequestV2Test, NonEmptyRequestMultipleKeys) {
     generateKeys(false /* testMode */, rpcHardwareInfo.supportedNumKeysInCsr /* numKeys */);
 
@@ -849,6 +853,7 @@
 /**
  * Generate a CSR and verify DeviceInfo against IDs attested by KeyMint.
  */
+// @VsrTest = 3.10-015
 TEST_P(CertificateRequestV2Test, DeviceInfo) {
     // See if there is a matching IKeyMintDevice for this IRemotelyProvisionedComponent.
     std::shared_ptr<IKeyMintDevice> keyMint;