1 /* 2 * Copyright (C) 2019 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 #ifndef DRM_HAL_COMMON_H 18 #define DRM_HAL_COMMON_H 19 20 #include <android/hardware/drm/1.2/ICryptoFactory.h> 21 #include <android/hardware/drm/1.2/ICryptoPlugin.h> 22 #include <android/hardware/drm/1.2/IDrmFactory.h> 23 #include <android/hardware/drm/1.2/IDrmPlugin.h> 24 #include <android/hardware/drm/1.2/IDrmPluginListener.h> 25 #include <android/hardware/drm/1.2/types.h> 26 27 #include <chrono> 28 #include <map> 29 #include <memory> 30 #include <string> 31 #include <vector> 32 33 #include "drm_hal_vendor_module_api.h" 34 #include "vendor_modules.h" 35 #include "VtsHalHidlTargetCallbackBase.h" 36 #include "VtsHalHidlTargetTestBase.h" 37 38 using ::android::hardware::drm::V1_0::EventType; 39 using ::android::hardware::drm::V1_0::KeyedVector; 40 using KeyStatusV1_0 = ::android::hardware::drm::V1_0::KeyStatus; 41 using ::android::hardware::drm::V1_0::KeyType; 42 using ::android::hardware::drm::V1_0::Mode; 43 using ::android::hardware::drm::V1_0::Pattern; 44 using ::android::hardware::drm::V1_0::SessionId; 45 using ::android::hardware::drm::V1_0::SubSample; 46 47 using ::android::hardware::drm::V1_1::ICryptoFactory; 48 49 using StatusV1_2 = ::android::hardware::drm::V1_2::Status; 50 51 using ::android::hardware::hidl_array; 52 using ::android::hardware::hidl_vec; 53 using ::android::hardware::Return; 54 using ::android::hardware::Void; 55 56 using ::android::hidl::memory::V1_0::IMemory; 57 using ::android::sp; 58 59 using ::testing::VtsHalHidlTargetTestBase; 60 61 using std::map; 62 using std::string; 63 using std::unique_ptr; 64 using std::vector; 65 66 #define EXPECT_OK(ret) EXPECT_TRUE(ret.isOk()) 67 68 #define RETURN_IF_SKIPPED \ 69 if (!vendorModule->isInstalled()) { \ 70 std::cout << "[ SKIPPED ] This drm scheme not supported." << \ 71 " library:" << GetParam() << " service-name:" << \ 72 vendorModule->getServiceName() << std::endl; \ 73 return; \ 74 } 75 76 namespace android { 77 namespace hardware { 78 namespace drm { 79 namespace V1_2 { 80 namespace vts { 81 82 class DrmHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase { 83 public: 84 // get the test environment singleton 85 static DrmHidlEnvironment* Instance() { 86 static DrmHidlEnvironment* instance = new DrmHidlEnvironment; 87 return instance; 88 } 89 90 void registerTestServices() override { 91 registerTestService<ICryptoFactory>(); 92 registerTestService<IDrmFactory>(); 93 setServiceCombMode(::testing::HalServiceCombMode::NO_COMBINATION); 94 } 95 96 private: 97 DrmHidlEnvironment() {} 98 99 GTEST_DISALLOW_COPY_AND_ASSIGN_(DrmHidlEnvironment); 100 }; 101 102 class DrmHalTest : public ::testing::TestWithParam<std::string> { 103 public: 104 static drm_vts::VendorModules* gVendorModules; 105 DrmHalTest(); 106 virtual void SetUp() override; 107 virtual void TearDown() override {} 108 109 protected: 110 hidl_array<uint8_t, 16> getVendorUUID(); 111 SessionId openSession(); 112 void closeSession(const SessionId& sessionId); 113 hidl_vec<uint8_t> loadKeys(const SessionId& sessionId, 114 const KeyType& type = KeyType::STREAMING); 115 hidl_vec<uint8_t> loadKeys(const SessionId& sessionId, 116 const DrmHalVTSVendorModule_V1::ContentConfiguration&, 117 const KeyType& type = KeyType::STREAMING); 118 hidl_vec<uint8_t> getKeyRequest(const SessionId& sessionId, 119 const DrmHalVTSVendorModule_V1::ContentConfiguration&, 120 const KeyType& type); 121 hidl_vec<uint8_t> provideKeyResponse(const SessionId& sessionId, 122 const hidl_vec<uint8_t>& keyResponse); 123 DrmHalVTSVendorModule_V1::ContentConfiguration getContent( 124 const KeyType& type = KeyType::STREAMING) const; 125 126 KeyedVector toHidlKeyedVector(const map<string, string>& params); 127 hidl_array<uint8_t, 16> toHidlArray(const vector<uint8_t>& vec); 128 129 void fillRandom(const sp<IMemory>& memory); 130 sp<IMemory> getDecryptMemory(size_t size, size_t index); 131 uint32_t decrypt(Mode mode, bool isSecure, 132 const hidl_array<uint8_t, 16>& keyId, uint8_t* iv, 133 const hidl_vec<SubSample>& subSamples, const Pattern& pattern, 134 const vector<uint8_t>& key, StatusV1_2 expectedStatus); 135 void aes_ctr_decrypt(uint8_t* dest, uint8_t* src, uint8_t* iv, 136 const hidl_vec<SubSample>& subSamples, const vector<uint8_t>& key); 137 void aes_cbc_decrypt(uint8_t* dest, uint8_t* src, uint8_t* iv, 138 const hidl_vec<SubSample>& subSamples, const vector<uint8_t>& key); 139 140 sp<IDrmFactory> drmFactory; 141 sp<ICryptoFactory> cryptoFactory; 142 sp<IDrmPlugin> drmPlugin; 143 sp<ICryptoPlugin> cryptoPlugin; 144 unique_ptr<DrmHalVTSVendorModule_V1> vendorModule; 145 const vector<DrmHalVTSVendorModule_V1::ContentConfiguration> contentConfigurations; 146 147 private: 148 sp<IDrmPlugin> createDrmPlugin(); 149 sp<ICryptoPlugin> createCryptoPlugin(); 150 151 }; 152 153 class DrmHalClearkeyTest : public DrmHalTest { 154 public: 155 virtual void SetUp() override { DrmHalTest::SetUp(); } 156 virtual void TearDown() override {} 157 void decryptWithInvalidKeys(hidl_vec<uint8_t>& invalidResponse, 158 vector<uint8_t>& iv, const Pattern& noPattern, const vector<SubSample>& subSamples); 159 }; 160 161 /** 162 * Event Handling tests 163 */ 164 extern const char *kCallbackLostState; 165 extern const char *kCallbackKeysChange; 166 167 struct ListenerEventArgs { 168 SessionId sessionId; 169 hidl_vec<KeyStatus> keyStatusList; 170 bool hasNewUsableKey; 171 }; 172 173 class DrmHalPluginListener 174 : public ::testing::VtsHalHidlTargetCallbackBase<ListenerEventArgs>, 175 public IDrmPluginListener { 176 public: 177 DrmHalPluginListener() { 178 SetWaitTimeoutDefault(std::chrono::milliseconds(500)); 179 } 180 virtual ~DrmHalPluginListener() {} 181 182 virtual Return<void> sendEvent(EventType, const hidl_vec<uint8_t>&, 183 const hidl_vec<uint8_t>& ) override { return Void(); } 184 185 virtual Return<void> sendExpirationUpdate(const hidl_vec<uint8_t>&, 186 int64_t) override { return Void(); } 187 188 virtual Return<void> sendKeysChange(const hidl_vec<uint8_t>&, 189 const hidl_vec<KeyStatusV1_0>&, bool) override { return Void(); } 190 191 virtual Return<void> sendSessionLostState(const hidl_vec<uint8_t>& sessionId) override; 192 193 virtual Return<void> sendKeysChange_1_2(const hidl_vec<uint8_t>&, 194 const hidl_vec<KeyStatus>&, bool) override; 195 196 }; 197 198 } // namespace vts 199 } // namespace V1_2 200 } // namespace drm 201 } // namespace hardware 202 } // namespace android 203 204 #endif // DRM_HAL_COMMON_H 205