VtsTrebleVintfTest: provide all instances to CheckDeprecation.

It is possible that matrix specify a regular expression on instances,
so there is no way to find an exact instance name and use GetHalService.
Use listByInterface() instead to get all instances and report
to CheckDeprecation.

For passthrough service manager, listByInterface doesn't work, so
a list of instance names are provided by CheckDeprecation as a hint.
This list includes the legacy list of <instance> tags. Hence,
the deprecation of <regex-instance> instances cannot be enforced.

Bug: 73738616
Test: run this test on walleye
Change-Id: I210dfebba3155926fe5737d3a318acb5b06b6583
diff --git a/treble/vintf/vts_treble_vintf_test.cpp b/treble/vintf/vts_treble_vintf_test.cpp
index 5e5ab8c..e237d95 100644
--- a/treble/vintf/vts_treble_vintf_test.cpp
+++ b/treble/vintf/vts_treble_vintf_test.cpp
@@ -27,6 +27,7 @@
 #include <thread>
 #include <vector>
 
+#include <android-base/logging.h>
 #include <android-base/properties.h>
 #include <android-base/strings.h>
 #include <android/hidl/manager/1.0/IServiceManager.h>
@@ -181,6 +182,9 @@
   sp<IBase> GetHalService(const FQName &fq_name, const string &instance_name,
                           Transport, bool log = true);
 
+  static vector<string> GetInstanceNames(const sp<IServiceManager> &manager,
+                                         const FQName &fq_name);
+
   static vector<string> GetInterfaceChain(const sp<IBase> &service);
 
   // Default service manager.
@@ -247,6 +251,17 @@
   return base;
 }
 
+vector<string> VtsTrebleVintfTest::GetInstanceNames(
+    const sp<IServiceManager> &manager, const FQName &fq_name) {
+  vector<string> ret;
+  auto status =
+      manager->listByInterface(fq_name.string(), [&](const auto &out) {
+        for (const auto &e : out) ret.push_back(e);
+      });
+  EXPECT_TRUE(status.isOk()) << status.description();
+  return ret;
+}
+
 vector<string> VtsTrebleVintfTest::GetInterfaceChain(const sp<IBase> &service) {
   vector<string> iface_chain{};
   service->interfaceChain([&iface_chain](const hidl_vec<hidl_string> &chain) {
@@ -573,51 +588,81 @@
 // minor version is served.
 TEST_F(DeprecateTest, NoDeprcatedHalsOnManager) {
   // Predicate for whether an instance is served through service manager.
-  // Return {is instance in service manager, highest minor version}
+  // Return {instance name, highest minor version}
   // where "highest minor version" is the first element in getInterfaceChain()
-  // that has the same "package", major version as "version", "interface" and
-  // "instance", but a higher minor version than "version".
-  VintfObject::IsInstanceInUse is_instance_served =
-      [this](const string &package, Version version, const string &interface,
-             const string &instance) {
-        FQName fq_name(package, to_string(version), interface);
-        for (auto transport : {Transport::HWBINDER, Transport::PASSTHROUGH}) {
-          auto service =
-              GetHalService(fq_name, instance, transport, false /* log */);
-          if (service == nullptr) {
-            continue;  // try next transport
+  // that has the same "package", major version as "version" and "interface"
+  // but a higher minor version than "version".
+  VintfObject::ListInstances list_instances = [this](const string &package,
+                                                     Version version,
+                                                     const string &interface,
+                                                     const vector<string>
+                                                         &instance_hints) {
+    vector<std::pair<string, Version>> ret;
+
+    FQName fq_name(package, to_string(version), interface);
+    for (auto transport : {Transport::HWBINDER, Transport::PASSTHROUGH}) {
+      const vector<string> &instance_names =
+          transport == Transport::HWBINDER
+              ? GetInstanceNames(default_manager_, fq_name)
+              : instance_hints;
+
+      for (auto instance : instance_names) {
+        auto service =
+            GetHalService(fq_name, instance, transport, false /* log */);
+        if (service == nullptr) {
+          if (transport == Transport::PASSTHROUGH) {
+            CHECK(std::find(instance_hints.begin(), instance_hints.end(),
+                            instance) != instance_hints.end())
+                << "existing <instance>'s: ["
+                << android::base::Join(instance_hints, ",")
+                << "] but instance=" << instance;
+            continue;  // name is from instance_hints, so ignore
           }
-          vector<string> iface_chain = GetInterfaceChain(service);
-          for (const auto &fq_interface_str : iface_chain) {
-            FQName fq_interface;
-            if (!FQName::parse(fq_interface_str, &fq_interface)) {
-              // Allow CheckDeprecation to proceed with some sensible default
-              ADD_FAILURE() << "'" << fq_interface_str
-                            << "' (returned by getInterfaceChain())"
-                            << "is not a valid fully-qualified name.";
-              return std::make_pair(true, version);
-            }
-            if (fq_interface.package() == package) {
-              Version fq_version{fq_interface.getPackageMajorVersion(),
-                                 fq_interface.getPackageMinorVersion()};
-              if (fq_version.minorAtLeast(version)) {
-                return std::make_pair(true, fq_version);
-              }
+          ADD_FAILURE()
+              << fq_name.string() << "/" << instance
+              << " is registered to hwservicemanager but cannot be retrieved.";
+          continue;
+        }
+
+        vector<string> iface_chain = GetInterfaceChain(service);
+        bool done = false;
+        for (const auto &fq_interface_str : iface_chain) {
+          FQName fq_interface;
+          if (!FQName::parse(fq_interface_str, &fq_interface)) {
+            // Allow CheckDeprecation to proceed with some sensible default
+            ADD_FAILURE() << "'" << fq_interface_str
+                          << "' (returned by getInterfaceChain())"
+                          << "is not a valid fully-qualified name.";
+            ret.push_back(std::make_pair(instance, version));
+            done = true;
+            continue;
+          }
+          if (fq_interface.package() == package) {
+            Version fq_version{fq_interface.getPackageMajorVersion(),
+                               fq_interface.getPackageMinorVersion()};
+            if (fq_version.minorAtLeast(version)) {
+              ret.push_back(std::make_pair(instance, fq_version));
+              done = true;
+              break;
             }
           }
+        }
+        if (!done) {
           // Allow CheckDeprecation to proceed with some sensible default
           ADD_FAILURE() << "getInterfaceChain() does not return interface name "
                         << "with at least minor version'" << package << "@"
                         << version << "'; returned values are ["
                         << android::base::Join(iface_chain, ", ") << "]";
-          return std::make_pair(true, version);
+          ret.push_back(std::make_pair(instance, version));
         }
+      }
+    }
 
-        return std::make_pair(false, Version{});
-      };
+    return ret;
+  };
   string error;
   EXPECT_EQ(android::vintf::NO_DEPRECATED_HALS,
-            VintfObject::CheckDeprecation(is_instance_served, &error))
+            VintfObject::CheckDeprecation(list_instances, &error))
       << error;
 }