Suresh Sivaraman | f1fbb44 | 2017-09-15 11:51:15 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #define LOG_TAG "mediacas_hidl_hal_test" |
| 18 | |
| 19 | #include <VtsHalHidlTargetTestBase.h> |
| 20 | #include <android-base/logging.h> |
| 21 | #include <android/hardware/cas/1.0/ICas.h> |
| 22 | #include <android/hardware/cas/1.0/ICasListener.h> |
| 23 | #include <android/hardware/cas/1.0/IDescramblerBase.h> |
| 24 | #include <android/hardware/cas/1.0/IMediaCasService.h> |
| 25 | #include <hidl/HidlSupport.h> |
| 26 | #include <hidl/HidlTransportSupport.h> |
| 27 | |
| 28 | #include <cinttypes> |
| 29 | #include <utility> |
| 30 | |
| 31 | // CA System Ids used for testing |
| 32 | #define CLEAR_KEY_SYSTEM_ID 0xF6D8 |
| 33 | #define INVALID_SYSTEM_ID 0 |
| 34 | |
| 35 | using android::Condition; |
| 36 | using android::hardware::cas::V1_0::ICas; |
| 37 | using android::hardware::cas::V1_0::ICasListener; |
| 38 | using android::hardware::cas::V1_0::IDescramblerBase; |
| 39 | using android::hardware::cas::V1_0::IMediaCasService; |
| 40 | using android::hardware::cas::V1_0::HidlCasPluginDescriptor; |
| 41 | using android::hardware::Void; |
| 42 | using android::hardware::hidl_vec; |
| 43 | using android::hardware::Return; |
| 44 | using android::Mutex; |
| 45 | using android::sp; |
| 46 | |
| 47 | namespace { |
| 48 | |
| 49 | class MediaCasHidlTest : public ::testing::VtsHalHidlTargetTestBase { |
| 50 | public: |
| 51 | virtual void SetUp() override { |
| 52 | mService = ::testing::VtsHalHidlTargetTestBase::getService<IMediaCasService>(); |
| 53 | ASSERT_NE(mService, nullptr); |
| 54 | } |
| 55 | |
| 56 | sp<IMediaCasService> mService; |
| 57 | |
| 58 | protected: |
| 59 | static void description(const std::string& description) { |
| 60 | RecordProperty("description", description); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | class MediaCasListener : public ICasListener { |
| 65 | public: |
| 66 | virtual ::android::hardware::Return<void> onEvent( |
| 67 | int32_t event, int32_t arg, const ::android::hardware::hidl_vec<uint8_t>& data) override { |
| 68 | ALOGI("Info: received event: %d, arg: %d, size: %zu", event, arg, data.size()); |
| 69 | |
| 70 | return Void(); |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | TEST_F(MediaCasHidlTest, EnumeratePlugins) { |
| 75 | description("Test enumerate plugins"); |
| 76 | hidl_vec<HidlCasPluginDescriptor> descriptors; |
| 77 | EXPECT_TRUE(mService |
| 78 | ->enumeratePlugins([&descriptors]( |
| 79 | hidl_vec<HidlCasPluginDescriptor> const& desc) { descriptors = desc; }) |
| 80 | .isOk()); |
| 81 | |
| 82 | if (descriptors.size() == 0) { |
| 83 | ALOGW("[ WARN ] enumeratePlugins list empty"); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | sp<MediaCasListener> casListener = new MediaCasListener(); |
| 88 | for (size_t i = 0; i < descriptors.size(); i++) { |
| 89 | int32_t caSystemId = descriptors[i].caSystemId; |
| 90 | bool status = mService->isSystemIdSupported(caSystemId); |
| 91 | ASSERT_EQ(status, true); |
| 92 | |
| 93 | status = mService->isDescramblerSupported(caSystemId); |
| 94 | ASSERT_EQ(status, true); |
| 95 | |
| 96 | sp<ICas> mediaCas = mService->createPlugin(caSystemId, casListener); |
| 97 | ASSERT_NE(mediaCas, nullptr); |
| 98 | |
| 99 | sp<IDescramblerBase> descramblerBase = mService->createDescrambler(caSystemId); |
| 100 | ASSERT_NE(descramblerBase, nullptr); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | TEST_F(MediaCasHidlTest, TestInvalidSystemIdFails) { |
| 105 | description("Test failure for invalid system ID"); |
| 106 | sp<MediaCasListener> casListener = new MediaCasListener(); |
| 107 | |
| 108 | ASSERT_FALSE(mService->isSystemIdSupported(INVALID_SYSTEM_ID)); |
| 109 | ASSERT_FALSE(mService->isDescramblerSupported(INVALID_SYSTEM_ID)); |
| 110 | |
| 111 | sp<ICas> mediaCas = mService->createPlugin(INVALID_SYSTEM_ID, casListener); |
| 112 | EXPECT_EQ(mediaCas, nullptr); |
| 113 | |
| 114 | sp<IDescramblerBase> descramblerBase = mService->createDescrambler(INVALID_SYSTEM_ID); |
| 115 | EXPECT_EQ(descramblerBase, nullptr); |
| 116 | } |
| 117 | |
| 118 | TEST_F(MediaCasHidlTest, TestClearKeyPluginInstalled) { |
| 119 | description("Test if ClearKey plugin is installed"); |
| 120 | hidl_vec<HidlCasPluginDescriptor> descriptors; |
| 121 | EXPECT_TRUE(mService |
| 122 | ->enumeratePlugins([&descriptors]( |
| 123 | hidl_vec<HidlCasPluginDescriptor> const& _desc) { descriptors = _desc; }) |
| 124 | .isOk()); |
| 125 | |
| 126 | if (descriptors.size() == 0) { |
| 127 | ALOGW("[ WARN ] enumeratePlugins list empty"); |
| 128 | } |
| 129 | |
| 130 | for (size_t i = 0; i < descriptors.size(); i++) { |
| 131 | int32_t caSystemId = descriptors[i].caSystemId; |
| 132 | if (CLEAR_KEY_SYSTEM_ID == caSystemId) { |
| 133 | return; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | ASSERT_TRUE(false) << "ClearKey plugin not installed"; |
| 138 | } |
| 139 | |
| 140 | } // anonymous namespace |
| 141 | |
| 142 | int main(int argc, char** argv) { |
| 143 | ::testing::InitGoogleTest(&argc, argv); |
| 144 | int status = RUN_ALL_TESTS(); |
| 145 | LOG(INFO) << "Test result = " << status; |
| 146 | return status; |
| 147 | } |