Convert VtsHalDrmV1_*TargetTest to be parameterized test

Removing dependency from VTS infrastructure so that test can run
standalone, for instance with atest/TEST_MAPPING. Once this is
done for every test, VTS can use the core testing infra.

Bug: 142397658
Test: atest VtsHalDrmV1_0TargetTest VtsHalDrmV1_1TargetTest \
  VtsHalDrmV1_2TargetTest
Change-Id: Ic436d949f5b26087e32c48fac220cd5abad8a443
diff --git a/drm/1.1/vts/functional/Android.bp b/drm/1.1/vts/functional/Android.bp
index 47b02bf..fb09563 100644
--- a/drm/1.1/vts/functional/Android.bp
+++ b/drm/1.1/vts/functional/Android.bp
@@ -18,7 +18,7 @@
     name: "VtsHalDrmV1_1TargetTest",
     defaults: ["VtsHalTargetTestDefaults"],
     srcs: [
-        "drm_hal_clearkey_test.cpp"
+        "drm_hal_clearkey_test.cpp",
     ],
     static_libs: [
         "android.hardware.drm@1.0",
@@ -30,5 +30,8 @@
         "libnativehelper",
         "libssl",
     ],
-    test_suites: ["general-tests"],
+    test_suites: [
+        "general-tests",
+        "vts-core",
+    ],
 }
diff --git a/drm/1.1/vts/functional/drm_hal_clearkey_test.cpp b/drm/1.1/vts/functional/drm_hal_clearkey_test.cpp
index 6be30d3..bf99ef6 100644
--- a/drm/1.1/vts/functional/drm_hal_clearkey_test.cpp
+++ b/drm/1.1/vts/functional/drm_hal_clearkey_test.cpp
@@ -16,27 +16,25 @@
 
 #define LOG_TAG "drm_hal_clearkey_test@1.1"
 
-#include <android/hardware/drm/1.1/ICryptoFactory.h>
 #include <android/hardware/drm/1.0/ICryptoPlugin.h>
-#include <android/hardware/drm/1.1/IDrmFactory.h>
 #include <android/hardware/drm/1.0/IDrmPlugin.h>
-#include <android/hardware/drm/1.1/IDrmPlugin.h>
 #include <android/hardware/drm/1.0/types.h>
+#include <android/hardware/drm/1.1/ICryptoFactory.h>
+#include <android/hardware/drm/1.1/IDrmFactory.h>
+#include <android/hardware/drm/1.1/IDrmPlugin.h>
 #include <android/hardware/drm/1.1/types.h>
 #include <android/hidl/allocator/1.0/IAllocator.h>
 #include <android/hidl/manager/1.2/IServiceManager.h>
 #include <gtest/gtest.h>
+#include <hidl/GtestPrinter.h>
 #include <hidl/HidlSupport.h>
 #include <hidl/ServiceManagement.h>
 #include <hidlmemory/mapping.h>
 #include <log/log.h>
-#include <memory>
 #include <openssl/aes.h>
+#include <memory>
 #include <random>
 
-#include "VtsHalHidlTargetTestBase.h"
-#include "VtsHalHidlTargetTestEnvBase.h"
-
 namespace drm = ::android::hardware::drm;
 using ::android::hardware::drm::V1_0::BufferType;
 using ::android::hardware::drm::V1_0::DestinationBuffer;
@@ -94,76 +92,31 @@
     0xE2, 0x71, 0x9D, 0x58, 0xA9, 0x85, 0xB3, 0xC9,
     0x78, 0x1A, 0xB0, 0x30, 0xAF, 0x78, 0xD3, 0x0E};
 
-// Test environment for drm
-class DrmHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
-   public:
-    // get the test environment singleton
-    static DrmHidlEnvironment* Instance() {
-        static DrmHidlEnvironment* instance = new DrmHidlEnvironment;
-        return instance;
-    }
-
-    virtual void HidlSetUp() override { ALOGI("SetUp DrmHidlEnvironment"); }
-
-    virtual void HidlTearDown() override { ALOGI("TearDown DrmHidlEnvironment"); }
-
-    void registerTestServices() override {
-        registerTestService<ICryptoFactory>();
-        registerTestService<IDrmFactory>();
-        setServiceCombMode(::testing::HalServiceCombMode::NO_COMBINATION);
-    }
-
-   private:
-    DrmHidlEnvironment() {}
-
-    GTEST_DISALLOW_COPY_AND_ASSIGN_(DrmHidlEnvironment);
-};
-
-
-class DrmHalClearkeyTest : public ::testing::VtsHalHidlTargetTestBase {
-public:
-    virtual void SetUp() override {
+class DrmHalClearkeyTest : public ::testing::TestWithParam<std::string> {
+  public:
+    void SetUp() override {
         const ::testing::TestInfo* const test_info =
                 ::testing::UnitTest::GetInstance()->current_test_info();
 
         ALOGD("DrmHalClearkeyTest: Running test %s.%s", test_info->test_case_name(),
                 test_info->name());
 
-        auto manager = android::hardware::defaultServiceManager1_2();
-        ASSERT_NE(nullptr, manager.get());
-        manager->listManifestByInterface(IDrmFactory::descriptor,
-                [&](const hidl_vec<hidl_string> &registered) {
-                    for (const auto &instance : registered) {
-                        sp<IDrmFactory> drmFactory =
-                                ::testing::VtsHalHidlTargetTestBase::getService<IDrmFactory>(instance);
-                        drmPlugin = createDrmPlugin(drmFactory);
-                        if (drmPlugin != nullptr) {
-                            break;
-                        }
-                    }
-                }
-            );
+        const std::string instance = GetParam();
 
-        manager->listManifestByInterface(ICryptoFactory::descriptor,
-                [&](const hidl_vec<hidl_string> &registered) {
-                    for (const auto &instance : registered) {
-                        sp<ICryptoFactory> cryptoFactory =
-                                ::testing::VtsHalHidlTargetTestBase::getService<ICryptoFactory>(instance);
-                        cryptoPlugin = createCryptoPlugin(cryptoFactory);
-                        if (cryptoPlugin != nullptr) {
-                            break;
-                        }
-                    }
-                }
-            );
+        sp<IDrmFactory> drmFactory = IDrmFactory::getService(instance);
+        drmPlugin = createDrmPlugin(drmFactory);
+        sp<ICryptoFactory> cryptoFactory = ICryptoFactory::getService(instance);
+        cryptoPlugin = createCryptoPlugin(cryptoFactory);
 
-        ASSERT_NE(nullptr, drmPlugin.get()) << "Can't find clearkey drm@1.1 plugin";
-        ASSERT_NE(nullptr, cryptoPlugin.get()) << "Can't find clearkey crypto@1.1 plugin";
+        if (drmPlugin == nullptr || cryptoPlugin == nullptr) {
+            if (instance == "clearkey") {
+                ASSERT_NE(nullptr, drmPlugin.get()) << "Can't get clearkey drm@1.1 plugin";
+                ASSERT_NE(nullptr, cryptoPlugin.get()) << "Can't get clearkey crypto@1.1 plugin";
+            }
+            GTEST_SKIP() << "Instance does not support clearkey";
+        }
     }
 
-
-    virtual void TearDown() override {}
-
     SessionId openSession();
     SessionId openSession(SecurityLevel level);
     void closeSession(const SessionId& sessionId);
@@ -176,9 +129,8 @@
         }
         sp<IDrmPlugin> plugin = nullptr;
         auto res = drmFactory->createPlugin(
-                kClearKeyUUID, "",
-                        [&](Status status, const sp<drm::V1_0::IDrmPlugin>& pluginV1_0) {
-                    EXPECT_EQ(Status::OK, status);
+                kClearKeyUUID, "", [&](Status status, const sp<drm::V1_0::IDrmPlugin>& pluginV1_0) {
+                    EXPECT_EQ(Status::OK == status, pluginV1_0 != nullptr);
                     plugin = IDrmPlugin::castFrom(pluginV1_0);
                 });
 
@@ -196,8 +148,8 @@
         hidl_vec<uint8_t> initVec;
         auto res = cryptoFactory->createPlugin(
                 kClearKeyUUID, initVec,
-                        [&](Status status, const sp<drm::V1_0::ICryptoPlugin>& pluginV1_0) {
-                    EXPECT_EQ(Status::OK, status);
+                [&](Status status, const sp<drm::V1_0::ICryptoPlugin>& pluginV1_0) {
+                    EXPECT_EQ(Status::OK == status, pluginV1_0 != nullptr);
                     plugin = pluginV1_0;
                 });
         if (!res.isOk()) {
@@ -265,7 +217,6 @@
  sp<ICryptoPlugin> cryptoPlugin;
 };
 
-
 /**
  * Helper method to open a session and verify that a non-empty
  * session ID is returned
@@ -371,7 +322,7 @@
 /**
  * Test openSession negative case: security level higher than supported
  */
-TEST_F(DrmHalClearkeyTest, OpenSessionBadLevel) {
+TEST_P(DrmHalClearkeyTest, OpenSessionBadLevel) {
     auto res = drmPlugin->openSession_1_1(SecurityLevel::HW_SECURE_ALL,
             [&](Status status, const SessionId& /* id */) {
                 EXPECT_EQ(Status::ERROR_DRM_CANNOT_HANDLE, status);
@@ -382,7 +333,7 @@
 /**
  * Test getKeyRequest_1_1 via loadKeys
  */
-TEST_F(DrmHalClearkeyTest, GetKeyRequest) {
+TEST_P(DrmHalClearkeyTest, GetKeyRequest) {
     auto sessionId = openSession();
     loadKeys(sessionId);
     closeSession(sessionId);
@@ -391,7 +342,7 @@
 /**
  * A get key request should fail if no sessionId is provided
  */
-TEST_F(DrmHalClearkeyTest, GetKeyRequestNoSession) {
+TEST_P(DrmHalClearkeyTest, GetKeyRequestNoSession) {
     SessionId invalidSessionId;
     hidl_vec<uint8_t> initData;
     hidl_string mimeType = "video/mp4";
@@ -409,7 +360,7 @@
  * Test that the plugin returns the expected error code in
  * this case.
  */
-TEST_F(DrmHalClearkeyTest, GetKeyRequestOfflineKeyTypeNotSupported) {
+TEST_P(DrmHalClearkeyTest, GetKeyRequestOfflineKeyTypeNotSupported) {
     auto sessionId = openSession();
     hidl_vec<uint8_t> initData;
     hidl_string mimeType = "video/mp4";
@@ -429,7 +380,7 @@
 /**
  * Test that the plugin returns valid connected and max HDCP levels
  */
-TEST_F(DrmHalClearkeyTest, GetHdcpLevels) {
+TEST_P(DrmHalClearkeyTest, GetHdcpLevels) {
     auto res = drmPlugin->getHdcpLevels(
             [&](Status status, const HdcpLevel &connectedLevel,
                 const HdcpLevel &maxLevel) {
@@ -448,7 +399,7 @@
 /**
  * Test that the plugin returns default open and max session counts
  */
-TEST_F(DrmHalClearkeyTest, GetDefaultSessionCounts) {
+TEST_P(DrmHalClearkeyTest, GetDefaultSessionCounts) {
     auto res = drmPlugin->getNumberOfSessions(
             [&](Status status, uint32_t currentSessions,
                     uint32_t maxSessions) {
@@ -464,7 +415,7 @@
  * Test that the plugin returns valid open and max session counts
  * after a session is opened.
  */
-TEST_F(DrmHalClearkeyTest, GetOpenSessionCounts) {
+TEST_P(DrmHalClearkeyTest, GetOpenSessionCounts) {
     uint32_t initialSessions = 0;
     auto res = drmPlugin->getNumberOfSessions(
             [&](Status status, uint32_t currentSessions,
@@ -505,7 +456,7 @@
  * Test that the plugin returns the same security level
  * by default as when it is requested explicitly
  */
-TEST_F(DrmHalClearkeyTest, GetDefaultSecurityLevel) {
+TEST_P(DrmHalClearkeyTest, GetDefaultSecurityLevel) {
     SessionId session = openSession();
     SecurityLevel defaultLevel;
     auto res = drmPlugin->getSecurityLevel(session,
@@ -530,7 +481,7 @@
  * Test that the plugin returns the lowest security level
  * when it is requested
  */
-TEST_F(DrmHalClearkeyTest, GetSecurityLevel) {
+TEST_P(DrmHalClearkeyTest, GetSecurityLevel) {
     SessionId session = openSession(SecurityLevel::SW_SECURE_CRYPTO);
     auto res = drmPlugin->getSecurityLevel(session,
             [&](Status status, SecurityLevel level) {
@@ -545,7 +496,7 @@
  * Test that the plugin returns the documented error
  * when requesting the security level for an invalid sessionId
  */
-TEST_F(DrmHalClearkeyTest, GetSecurityLevelInvalidSessionId) {
+TEST_P(DrmHalClearkeyTest, GetSecurityLevelInvalidSessionId) {
     SessionId session;
     auto res = drmPlugin->getSecurityLevel(session,
             [&](Status status, SecurityLevel /*level*/) {
@@ -557,7 +508,7 @@
 /**
  * Test metrics are set appropriately for open and close operations.
  */
-TEST_F(DrmHalClearkeyTest, GetMetricsOpenClose) {
+TEST_P(DrmHalClearkeyTest, GetMetricsOpenClose) {
     SessionId sessionId = openSession();
     // The first close should be successful.
     closeSession(sessionId);
@@ -589,7 +540,7 @@
 /**
  * Test that there are no secure stop ids after clearing them
  */
-TEST_F(DrmHalClearkeyTest, GetSecureStopIdsCleared) {
+TEST_P(DrmHalClearkeyTest, GetSecureStopIdsCleared) {
     auto stat = drmPlugin->removeAllSecureStops();
     EXPECT_OK(stat);
 
@@ -604,7 +555,7 @@
 /**
  * Test that there are secure stop ids after loading keys once
  */
-TEST_F(DrmHalClearkeyTest, GetSecureStopIdsOnce) {
+TEST_P(DrmHalClearkeyTest, GetSecureStopIdsOnce) {
     auto stat = drmPlugin->removeAllSecureStops();
     EXPECT_OK(stat);
 
@@ -639,7 +590,7 @@
  * Test that the clearkey plugin reports no secure stops when
  * there are none.
  */
-TEST_F(DrmHalClearkeyTest, GetNoSecureStops) {
+TEST_P(DrmHalClearkeyTest, GetNoSecureStops) {
     auto stat = drmPlugin->removeAllSecureStops();
     EXPECT_OK(stat);
 
@@ -654,7 +605,7 @@
 /**
  * Test get/remove of one secure stop
  */
-TEST_F(DrmHalClearkeyTest, GetOneSecureStopAndRemoveIt) {
+TEST_P(DrmHalClearkeyTest, GetOneSecureStopAndRemoveIt) {
     auto stat = drmPlugin->removeAllSecureStops();
     EXPECT_OK(stat);
 
@@ -688,7 +639,7 @@
 /**
  * Test that there are no secure stops after clearing them
  */
-TEST_F(DrmHalClearkeyTest, GetSecureStopsCleared) {
+TEST_P(DrmHalClearkeyTest, GetSecureStopsCleared) {
     auto stat = drmPlugin->removeAllSecureStops();
     EXPECT_OK(stat);
 
@@ -703,7 +654,7 @@
 /**
  * Test that there are secure stops after loading keys once
  */
-TEST_F(DrmHalClearkeyTest, GetSecureStopsOnce) {
+TEST_P(DrmHalClearkeyTest, GetSecureStopsOnce) {
     auto stat = drmPlugin->removeAllSecureStops();
     EXPECT_OK(stat);
 
@@ -738,7 +689,7 @@
  * Test that releasing a secure stop with empty
  * release message fails with the documented error
  */
-TEST_F(DrmHalClearkeyTest, ReleaseEmptySecureStop) {
+TEST_P(DrmHalClearkeyTest, ReleaseEmptySecureStop) {
     SecureStopRelease emptyRelease = {.opaqueData = hidl_vec<uint8_t>()};
     Status status = drmPlugin->releaseSecureStops(emptyRelease);
     EXPECT_EQ(Status::BAD_VALUE, status);
@@ -763,8 +714,7 @@
 /**
  * Test that releasing one secure stop works
  */
-TEST_F(DrmHalClearkeyTest, ReleaseOneSecureStop) {
-
+TEST_P(DrmHalClearkeyTest, ReleaseOneSecureStop) {
     auto stat = drmPlugin->removeAllSecureStops();
     EXPECT_OK(stat);
 
@@ -792,12 +742,11 @@
     EXPECT_OK(res);
 }
 
-
 /**
  * Test that removing a secure stop with an empty ID returns
  * documented error
  */
-TEST_F(DrmHalClearkeyTest, RemoveEmptySecureStopId) {
+TEST_P(DrmHalClearkeyTest, RemoveEmptySecureStopId) {
     hidl_vec<uint8_t> emptyId;
     auto stat = drmPlugin->removeSecureStop(emptyId);
     EXPECT_OK(stat);
@@ -808,7 +757,7 @@
  * Test that removing a secure stop after it has already
  * been removed fails with the documented error code.
  */
-TEST_F(DrmHalClearkeyTest, RemoveRemovedSecureStopId) {
+TEST_P(DrmHalClearkeyTest, RemoveRemovedSecureStopId) {
     auto stat = drmPlugin->removeAllSecureStops();
     EXPECT_OK(stat);
 
@@ -835,7 +784,7 @@
 /**
  * Test that removing a secure stop by id works
  */
-TEST_F(DrmHalClearkeyTest, RemoveSecureStopById) {
+TEST_P(DrmHalClearkeyTest, RemoveSecureStopById) {
     auto stat = drmPlugin->removeAllSecureStops();
     EXPECT_OK(stat);
 
@@ -863,12 +812,16 @@
     EXPECT_OK(res);
 }
 
+static const std::set<std::string> kAllInstances = [] {
+    std::vector<std::string> drmInstances =
+            android::hardware::getAllHalInstanceNames(IDrmFactory::descriptor);
+    std::vector<std::string> cryptoInstances =
+            android::hardware::getAllHalInstanceNames(ICryptoFactory::descriptor);
+    std::set<std::string> allInstances;
+    allInstances.insert(drmInstances.begin(), drmInstances.end());
+    allInstances.insert(cryptoInstances.begin(), cryptoInstances.end());
+    return allInstances;
+}();
 
-int main(int argc, char** argv) {
-    ::testing::AddGlobalTestEnvironment(DrmHidlEnvironment::Instance());
-    ::testing::InitGoogleTest(&argc, argv);
-    DrmHidlEnvironment::Instance()->init(&argc, argv);
-    int status = RUN_ALL_TESTS();
-    ALOGI("Test result = %d", status);
-    return status;
-}
+INSTANTIATE_TEST_SUITE_P(PerInstance, DrmHalClearkeyTest, testing::ValuesIn(kAllInstances),
+                         android::hardware::PrintInstanceNameToString);