blob: 67975066424602a1b9449cc4c89416b15cc63598 [file] [log] [blame]
Henry Fanga757b1b2019-02-07 15:01:05 -08001/*
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#define LOG_TAG "mediacas_hidl_hal_test"
18
Henry Fanga757b1b2019-02-07 15:01:05 -080019#include <android-base/logging.h>
20#include <android/hardware/cas/1.0/IDescramblerBase.h>
21#include <android/hardware/cas/1.0/types.h>
22#include <android/hardware/cas/1.1/ICas.h>
23#include <android/hardware/cas/1.1/ICasListener.h>
24#include <android/hardware/cas/1.1/IMediaCasService.h>
Henry Fang8964b852021-01-08 16:09:08 -080025#include <android/hardware/cas/1.2/IMediaCasService.h>
Henry Fanga757b1b2019-02-07 15:01:05 -080026#include <android/hardware/cas/native/1.0/IDescrambler.h>
27#include <android/hardware/cas/native/1.0/types.h>
28#include <binder/MemoryDealer.h>
Dan Shi54a3b0b2019-10-15 15:22:45 -070029#include <gtest/gtest.h>
30#include <hidl/GtestPrinter.h>
Henry Fanga757b1b2019-02-07 15:01:05 -080031#include <hidl/HidlSupport.h>
32#include <hidl/HidlTransportSupport.h>
Dan Shi54a3b0b2019-10-15 15:22:45 -070033#include <hidl/ServiceManagement.h>
Henry Fanga757b1b2019-02-07 15:01:05 -080034#include <hidl/Status.h>
35#include <hidlmemory/FrameworkUtils.h>
36#include <utils/Condition.h>
37#include <utils/Mutex.h>
38
39#define CLEAR_KEY_SYSTEM_ID 0xF6D8
40#define INVALID_SYSTEM_ID 0
41#define WAIT_TIMEOUT 3000000000
42
43#define PROVISION_STR \
44 "{ " \
45 " \"id\": 21140844, " \
46 " \"name\": \"Test Title\", " \
47 " \"lowercase_organization_name\": \"Android\", " \
48 " \"asset_key\": { " \
49 " \"encryption_key\": \"nezAr3CHFrmBR9R8Tedotw==\" " \
50 " }, " \
51 " \"cas_type\": 1, " \
52 " \"track_types\": [ ] " \
53 "} "
54
55using android::Condition;
56using android::IMemory;
57using android::IMemoryHeap;
58using android::MemoryDealer;
59using android::Mutex;
60using android::sp;
61using android::hardware::fromHeap;
62using android::hardware::hidl_string;
63using android::hardware::hidl_vec;
64using android::hardware::HidlMemory;
65using android::hardware::Return;
66using android::hardware::Void;
67using android::hardware::cas::native::V1_0::BufferType;
68using android::hardware::cas::native::V1_0::DestinationBuffer;
69using android::hardware::cas::native::V1_0::IDescrambler;
70using android::hardware::cas::native::V1_0::ScramblingControl;
71using android::hardware::cas::native::V1_0::SharedBuffer;
72using android::hardware::cas::native::V1_0::SubSample;
73using android::hardware::cas::V1_0::HidlCasPluginDescriptor;
74using android::hardware::cas::V1_0::IDescramblerBase;
75using android::hardware::cas::V1_0::Status;
76using android::hardware::cas::V1_1::ICas;
77using android::hardware::cas::V1_1::ICasListener;
78using android::hardware::cas::V1_1::IMediaCasService;
79
80namespace {
81
82const uint8_t kEcmBinaryBuffer[] = {
83 0x00, 0x00, 0x01, 0xf0, 0x00, 0x50, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x46, 0x00,
84 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x27, 0x10, 0x02, 0x00,
85 0x01, 0x77, 0x01, 0x42, 0x95, 0x6c, 0x0e, 0xe3, 0x91, 0xbc, 0xfd, 0x05, 0xb1, 0x60, 0x4f,
86 0x17, 0x82, 0xa4, 0x86, 0x9b, 0x23, 0x56, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
87 0x27, 0x10, 0x02, 0x00, 0x01, 0x77, 0x01, 0x42, 0x95, 0x6c, 0xd7, 0x43, 0x62, 0xf8, 0x1c,
88 0x62, 0x19, 0x05, 0xc7, 0x3a, 0x42, 0xcd, 0xfd, 0xd9, 0x13, 0x48,
89};
90
91const SubSample kSubSamples[] = {{162, 0}, {0, 184}, {0, 184}};
92
93const uint8_t kInBinaryBuffer[] = {
94 0x00, 0x00, 0x00, 0x01, 0x09, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0xc0, 0x1e, 0xdb,
95 0x01, 0x40, 0x16, 0xec, 0x04, 0x40, 0x00, 0x00, 0x03, 0x00, 0x40, 0x00, 0x00, 0x0f, 0x03,
96 0xc5, 0x8b, 0xb8, 0x00, 0x00, 0x00, 0x01, 0x68, 0xca, 0x8c, 0xb2, 0x00, 0x00, 0x01, 0x06,
97 0x05, 0xff, 0xff, 0x70, 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7, 0x96, 0x2c, 0xd8,
98 0x20, 0xd9, 0x23, 0xee, 0xef, 0x78, 0x32, 0x36, 0x34, 0x20, 0x2d, 0x20, 0x63, 0x6f, 0x72,
99 0x65, 0x20, 0x31, 0x34, 0x32, 0x20, 0x2d, 0x20, 0x48, 0x2e, 0x32, 0x36, 0x34, 0x2f, 0x4d,
100 0x50, 0x45, 0x47, 0x2d, 0x34, 0x20, 0x41, 0x56, 0x43, 0x20, 0x63, 0x6f, 0x64, 0x65, 0x63,
101 0x20, 0x2d, 0x20, 0x43, 0x6f, 0x70, 0x79, 0x6c, 0x65, 0x66, 0x74, 0x20, 0x32, 0x30, 0x30,
102 0x33, 0x2d, 0x32, 0x30, 0x31, 0x34, 0x20, 0x2d, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f,
103 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x6c, 0x61, 0x6e, 0x2e, 0x6f,
104 0x72, 0x67, 0x2f, 0x78, 0x32, 0x36, 0x34, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x6e, 0x45, 0x21,
105 0x82, 0x38, 0xf0, 0x9d, 0x7d, 0x96, 0xe6, 0x94, 0xae, 0xe2, 0x87, 0x8f, 0x04, 0x49, 0xe5,
106 0xf6, 0x8c, 0x8b, 0x9a, 0x10, 0x18, 0xba, 0x94, 0xe9, 0x22, 0x31, 0x04, 0x7e, 0x60, 0x5b,
107 0xc4, 0x24, 0x00, 0x90, 0x62, 0x0d, 0xdc, 0x85, 0x74, 0x75, 0x78, 0xd0, 0x14, 0x08, 0xcb,
108 0x02, 0x1d, 0x7d, 0x9d, 0x34, 0xe8, 0x81, 0xb9, 0xf7, 0x09, 0x28, 0x79, 0x29, 0x8d, 0xe3,
109 0x14, 0xed, 0x5f, 0xca, 0xaf, 0xf4, 0x1c, 0x49, 0x15, 0xe1, 0x80, 0x29, 0x61, 0x76, 0x80,
110 0x43, 0xf8, 0x58, 0x53, 0x40, 0xd7, 0x31, 0x6d, 0x61, 0x81, 0x41, 0xe9, 0x77, 0x9f, 0x9c,
111 0xe1, 0x6d, 0xf2, 0xee, 0xd9, 0xc8, 0x67, 0xd2, 0x5f, 0x48, 0x73, 0xe3, 0x5c, 0xcd, 0xa7,
112 0x45, 0x58, 0xbb, 0xdd, 0x28, 0x1d, 0x68, 0xfc, 0xb4, 0xc6, 0xf6, 0x92, 0xf6, 0x30, 0x03,
113 0xaa, 0xe4, 0x32, 0xf6, 0x34, 0x51, 0x4b, 0x0f, 0x8c, 0xf9, 0xac, 0x98, 0x22, 0xfb, 0x49,
114 0xc8, 0xbf, 0xca, 0x8c, 0x80, 0x86, 0x5d, 0xd7, 0xa4, 0x52, 0xb1, 0xd9, 0xa6, 0x04, 0x4e,
115 0xb3, 0x2d, 0x1f, 0xb8, 0x35, 0xcc, 0x45, 0x6d, 0x9c, 0x20, 0xa7, 0xa4, 0x34, 0x59, 0x72,
116 0xe3, 0xae, 0xba, 0x49, 0xde, 0xd1, 0xaa, 0xee, 0x3d, 0x77, 0xfc, 0x5d, 0xc6, 0x1f, 0x9d,
117 0xac, 0xc2, 0x15, 0x66, 0xb8, 0xe1, 0x54, 0x4e, 0x74, 0x93, 0xdb, 0x9a, 0x24, 0x15, 0x6e,
118 0x20, 0xa3, 0x67, 0x3e, 0x5a, 0x24, 0x41, 0x5e, 0xb0, 0xe6, 0x35, 0x87, 0x1b, 0xc8, 0x7a,
119 0xf9, 0x77, 0x65, 0xe0, 0x01, 0xf2, 0x4c, 0xe4, 0x2b, 0xa9, 0x64, 0x96, 0x96, 0x0b, 0x46,
120 0xca, 0xea, 0x79, 0x0e, 0x78, 0xa3, 0x5f, 0x43, 0xfc, 0x47, 0x6a, 0x12, 0xfa, 0xc4, 0x33,
121 0x0e, 0x88, 0x1c, 0x19, 0x3a, 0x00, 0xc3, 0x4e, 0xb5, 0xd8, 0xfa, 0x8e, 0xf1, 0xbc, 0x3d,
122 0xb2, 0x7e, 0x50, 0x8d, 0x67, 0xc3, 0x6b, 0xed, 0xe2, 0xea, 0xa6, 0x1f, 0x25, 0x24, 0x7c,
123 0x94, 0x74, 0x50, 0x49, 0xe3, 0xc6, 0x58, 0x2e, 0xfd, 0x28, 0xb4, 0xc6, 0x73, 0xb1, 0x53,
124 0x74, 0x27, 0x94, 0x5c, 0xdf, 0x69, 0xb7, 0xa1, 0xd7, 0xf5, 0xd3, 0x8a, 0x2c, 0x2d, 0xb4,
125 0x5e, 0x8a, 0x16, 0x14, 0x54, 0x64, 0x6e, 0x00, 0x6b, 0x11, 0x59, 0x8a, 0x63, 0x38, 0x80,
126 0x76, 0xc3, 0xd5, 0x59, 0xf7, 0x3f, 0xd2, 0xfa, 0xa5, 0xca, 0x82, 0xff, 0x4a, 0x62, 0xf0,
127 0xe3, 0x42, 0xf9, 0x3b, 0x38, 0x27, 0x8a, 0x89, 0xaa, 0x50, 0x55, 0x4b, 0x29, 0xf1, 0x46,
128 0x7c, 0x75, 0xef, 0x65, 0xaf, 0x9b, 0x0d, 0x6d, 0xda, 0x25, 0x94, 0x14, 0xc1, 0x1b, 0xf0,
129 0xc5, 0x4c, 0x24, 0x0e, 0x65,
130};
131
132const uint8_t kOutRefBinaryBuffer[] = {
133 0x00, 0x00, 0x00, 0x01, 0x09, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0xc0, 0x1e, 0xdb,
134 0x01, 0x40, 0x16, 0xec, 0x04, 0x40, 0x00, 0x00, 0x03, 0x00, 0x40, 0x00, 0x00, 0x0f, 0x03,
135 0xc5, 0x8b, 0xb8, 0x00, 0x00, 0x00, 0x01, 0x68, 0xca, 0x8c, 0xb2, 0x00, 0x00, 0x01, 0x06,
136 0x05, 0xff, 0xff, 0x70, 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7, 0x96, 0x2c, 0xd8,
137 0x20, 0xd9, 0x23, 0xee, 0xef, 0x78, 0x32, 0x36, 0x34, 0x20, 0x2d, 0x20, 0x63, 0x6f, 0x72,
138 0x65, 0x20, 0x31, 0x34, 0x32, 0x20, 0x2d, 0x20, 0x48, 0x2e, 0x32, 0x36, 0x34, 0x2f, 0x4d,
139 0x50, 0x45, 0x47, 0x2d, 0x34, 0x20, 0x41, 0x56, 0x43, 0x20, 0x63, 0x6f, 0x64, 0x65, 0x63,
140 0x20, 0x2d, 0x20, 0x43, 0x6f, 0x70, 0x79, 0x6c, 0x65, 0x66, 0x74, 0x20, 0x32, 0x30, 0x30,
141 0x33, 0x2d, 0x32, 0x30, 0x31, 0x34, 0x20, 0x2d, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f,
142 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x76, 0x69, 0x64, 0x65, 0x6f, 0x6c, 0x61, 0x6e, 0x2e, 0x6f,
143 0x72, 0x67, 0x2f, 0x78, 0x32, 0x36, 0x34, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x20, 0x2d, 0x20,
144 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x3a, 0x20, 0x63, 0x61, 0x62, 0x61, 0x63, 0x3d,
145 0x30, 0x20, 0x72, 0x65, 0x66, 0x3d, 0x32, 0x20, 0x64, 0x65, 0x62, 0x6c, 0x6f, 0x63, 0x6b,
146 0x3d, 0x31, 0x3a, 0x30, 0x3a, 0x30, 0x20, 0x61, 0x6e, 0x61, 0x6c, 0x79, 0x73, 0x65, 0x3d,
147 0x30, 0x78, 0x31, 0x3a, 0x30, 0x78, 0x31, 0x31, 0x31, 0x20, 0x6d, 0x65, 0x3d, 0x68, 0x65,
148 0x78, 0x20, 0x73, 0x75, 0x62, 0x6d, 0x65, 0x3d, 0x37, 0x20, 0x70, 0x73, 0x79, 0x3d, 0x31,
149 0x20, 0x70, 0x73, 0x79, 0x5f, 0x72, 0x64, 0x3d, 0x31, 0x2e, 0x30, 0x30, 0x3a, 0x30, 0x2e,
150 0x30, 0x30, 0x20, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x5f, 0x72, 0x65, 0x66, 0x3d, 0x31, 0x20,
151 0x6d, 0x65, 0x5f, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x3d, 0x31, 0x36, 0x20, 0x63, 0x68, 0x72,
152 0x6f, 0x6d, 0x61, 0x5f, 0x6d, 0x65, 0x3d, 0x31, 0x20, 0x74, 0x72, 0x65, 0x6c, 0x6c, 0x69,
153 0x73, 0x3d, 0x31, 0x20, 0x38, 0x78, 0x38, 0x64, 0x63, 0x74, 0x3d, 0x30, 0x20, 0x63, 0x71,
154 0x6d, 0x3d, 0x30, 0x20, 0x64, 0x65, 0x61, 0x64, 0x7a, 0x6f, 0x6e, 0x65, 0x3d, 0x32, 0x31,
155 0x2c, 0x31, 0x31, 0x20, 0x66, 0x61, 0x73, 0x74, 0x5f, 0x70, 0x73, 0x6b, 0x69, 0x70, 0x3d,
156 0x31, 0x20, 0x63, 0x68, 0x72, 0x6f, 0x6d, 0x61, 0x5f, 0x71, 0x70, 0x5f, 0x6f, 0x66, 0x66,
157 0x73, 0x65, 0x74, 0x3d, 0x2d, 0x32, 0x20, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d,
158 0x36, 0x30, 0x20, 0x6c, 0x6f, 0x6f, 0x6b, 0x61, 0x68, 0x65, 0x61, 0x64, 0x5f, 0x74, 0x68,
159 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d, 0x35, 0x20, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x64, 0x5f,
160 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d, 0x30, 0x20, 0x6e, 0x72, 0x3d, 0x30, 0x20,
161 0x64, 0x65, 0x63, 0x69, 0x6d, 0x61, 0x74, 0x65, 0x3d, 0x31, 0x20, 0x69, 0x6e, 0x74, 0x65,
162 0x72, 0x6c, 0x61, 0x63, 0x65, 0x64, 0x3d, 0x30, 0x20, 0x62, 0x6c, 0x75, 0x72, 0x61, 0x79,
163 0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x3d, 0x30, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x74,
164 0x72, 0x61, 0x69, 0x6e, 0x65, 0x64, 0x5f, 0x69, 0x6e, 0x74, 0x72, 0x61, 0x3d, 0x30, 0x20,
165 0x62, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x73, 0x3d, 0x30, 0x20, 0x77, 0x65, 0x69, 0x67, 0x68,
166 0x74, 0x70, 0x3d, 0x30, 0x20, 0x6b, 0x65, 0x79, 0x69, 0x6e, 0x74, 0x3d, 0x32, 0x35, 0x30,
167 0x20, 0x6b, 0x65, 0x79, 0x69, 0x6e, 0x74, 0x5f, 0x6d, 0x69, 0x6e, 0x3d, 0x32, 0x35, 0x20,
168 0x73, 0x63, 0x65, 0x6e, 0x65,
169};
170
171class MediaCasListener : public ICasListener {
172 public:
173 virtual Return<void> onEvent(int32_t event, int32_t arg,
174 const hidl_vec<uint8_t>& data) override {
175 android::Mutex::Autolock autoLock(mMsgLock);
176 mEvent = event;
177 mEventArg = arg;
178 mEventData = data;
179
180 mEventReceived = true;
181 mMsgCondition.signal();
182 return Void();
183 }
184
185 virtual Return<void> onSessionEvent(const hidl_vec<uint8_t>& sessionId, int32_t event,
186 int32_t arg, const hidl_vec<uint8_t>& data) override {
187 android::Mutex::Autolock autoLock(mMsgLock);
188 mSessionId = sessionId;
189 mEvent = event;
190 mEventArg = arg;
191 mEventData = data;
192
193 mEventReceived = true;
194 mMsgCondition.signal();
195 return Void();
196 }
197
198 void testEventEcho(sp<ICas>& mediaCas, int32_t& event, int32_t& eventArg,
199 hidl_vec<uint8_t>& eventData);
200
201 void testSessionEventEcho(sp<ICas>& mediaCas, const hidl_vec<uint8_t>& sessionId,
202 int32_t& event, int32_t& eventArg, hidl_vec<uint8_t>& eventData);
203
204 private:
205 int32_t mEvent = -1;
206 int32_t mEventArg = -1;
207 bool mEventReceived = false;
208 hidl_vec<uint8_t> mEventData;
209 hidl_vec<uint8_t> mSessionId;
210 android::Mutex mMsgLock;
211 android::Condition mMsgCondition;
212};
213
214void MediaCasListener::testEventEcho(sp<ICas>& mediaCas, int32_t& event, int32_t& eventArg,
215 hidl_vec<uint8_t>& eventData) {
216 mEventReceived = false;
217 auto returnStatus = mediaCas->sendEvent(event, eventArg, eventData);
218 EXPECT_TRUE(returnStatus.isOk());
219 EXPECT_EQ(Status::OK, returnStatus);
220
221 android::Mutex::Autolock autoLock(mMsgLock);
222 while (!mEventReceived) {
223 if (-ETIMEDOUT == mMsgCondition.waitRelative(mMsgLock, WAIT_TIMEOUT)) {
224 EXPECT_TRUE(false) << "event not received within timeout";
225 return;
226 }
227 }
228
229 EXPECT_EQ(mEvent, event);
230 EXPECT_EQ(mEventArg, eventArg);
231 EXPECT_TRUE(mEventData == eventData);
232}
233
234void MediaCasListener::testSessionEventEcho(sp<ICas>& mediaCas, const hidl_vec<uint8_t>& sessionId,
235 int32_t& event, int32_t& eventArg,
236 hidl_vec<uint8_t>& eventData) {
237 mEventReceived = false;
238 auto returnStatus = mediaCas->sendSessionEvent(sessionId, event, eventArg, eventData);
239 EXPECT_TRUE(returnStatus.isOk());
240 EXPECT_EQ(Status::OK, returnStatus);
241
242 android::Mutex::Autolock autoLock(mMsgLock);
243 while (!mEventReceived) {
244 if (-ETIMEDOUT == mMsgCondition.waitRelative(mMsgLock, WAIT_TIMEOUT)) {
245 EXPECT_TRUE(false) << "event not received within timeout";
246 return;
247 }
248 }
249
250 EXPECT_TRUE(mSessionId == sessionId);
251 EXPECT_EQ(mEvent, event);
252 EXPECT_EQ(mEventArg, eventArg);
253 EXPECT_TRUE(mEventData == eventData);
254}
255
Dan Shi54a3b0b2019-10-15 15:22:45 -0700256class MediaCasHidlTest : public testing::TestWithParam<std::string> {
Henry Fanga757b1b2019-02-07 15:01:05 -0800257 public:
258 virtual void SetUp() override {
Henry Fang8964b852021-01-08 16:09:08 -0800259 if (android::hardware::cas::V1_2::IMediaCasService::getService(GetParam()) == nullptr) {
260 ALOGI("Descrambler is need to be tested before cas@1.2.");
261 mIsTestDescrambler = true;
262 }
Dan Shi54a3b0b2019-10-15 15:22:45 -0700263 mService = IMediaCasService::getService(GetParam());
Henry Fanga757b1b2019-02-07 15:01:05 -0800264 ASSERT_NE(mService, nullptr);
265 }
266
Dan Shi54a3b0b2019-10-15 15:22:45 -0700267 sp<IMediaCasService> mService = nullptr;
Henry Fanga757b1b2019-02-07 15:01:05 -0800268
269 protected:
270 static void description(const std::string& description) {
271 RecordProperty("description", description);
272 }
273
274 sp<ICas> mMediaCas;
275 sp<IDescramblerBase> mDescramblerBase;
276 sp<MediaCasListener> mCasListener;
Henry Fang8964b852021-01-08 16:09:08 -0800277 bool mIsTestDescrambler = false;
Henry Fanga757b1b2019-02-07 15:01:05 -0800278 typedef struct _OobInputTestParams {
279 const SubSample* subSamples;
280 uint32_t numSubSamples;
281 size_t imemSizeActual;
282 uint64_t imemOffset;
283 uint64_t imemSize;
284 uint64_t srcOffset;
285 uint64_t dstOffset;
286 } OobInputTestParams;
287
288 ::testing::AssertionResult createCasPlugin(int32_t caSystemId);
289 ::testing::AssertionResult openCasSession(std::vector<uint8_t>* sessionId);
290 ::testing::AssertionResult descrambleTestInputBuffer(const sp<IDescrambler>& descrambler,
291 Status* descrambleStatus,
292 sp<IMemory>* hidlInMemory);
293 ::testing::AssertionResult descrambleTestOobInput(const sp<IDescrambler>& descrambler,
294 Status* descrambleStatus,
295 const OobInputTestParams& params);
296};
297
298::testing::AssertionResult MediaCasHidlTest::createCasPlugin(int32_t caSystemId) {
299 auto status = mService->isSystemIdSupported(caSystemId);
300 if (!status.isOk() || !status) {
301 return ::testing::AssertionFailure();
302 }
303 status = mService->isDescramblerSupported(caSystemId);
304 if (!status.isOk() || !status) {
305 return ::testing::AssertionFailure();
306 }
307
308 mCasListener = new MediaCasListener();
309 auto pluginStatus = mService->createPluginExt(caSystemId, mCasListener);
310 if (!pluginStatus.isOk()) {
311 return ::testing::AssertionFailure();
312 }
313 mMediaCas = pluginStatus;
314 if (mMediaCas == nullptr) {
315 return ::testing::AssertionFailure();
316 }
317
318 auto descramblerStatus = mService->createDescrambler(caSystemId);
319 if (!descramblerStatus.isOk()) {
Henry Fang8964b852021-01-08 16:09:08 -0800320 if (mIsTestDescrambler) {
321 return ::testing::AssertionFailure();
322 } else {
323 ALOGI("Skip Descrambler test since it's not required in cas@1.2.");
324 return ::testing::AssertionSuccess();
325 }
Henry Fanga757b1b2019-02-07 15:01:05 -0800326 }
Henry Fang8964b852021-01-08 16:09:08 -0800327 mIsTestDescrambler = true;
328
Henry Fanga757b1b2019-02-07 15:01:05 -0800329 mDescramblerBase = descramblerStatus;
330 return ::testing::AssertionResult(mDescramblerBase != nullptr);
331}
332
333::testing::AssertionResult MediaCasHidlTest::openCasSession(std::vector<uint8_t>* sessionId) {
334 Status sessionStatus;
335 auto returnVoid = mMediaCas->openSession([&](Status status, const hidl_vec<uint8_t>& id) {
336 sessionStatus = status;
337 *sessionId = id;
338 });
339 return ::testing::AssertionResult(returnVoid.isOk() && (Status::OK == sessionStatus));
340}
341
342::testing::AssertionResult MediaCasHidlTest::descrambleTestInputBuffer(
343 const sp<IDescrambler>& descrambler, Status* descrambleStatus, sp<IMemory>* inMemory) {
344 hidl_vec<SubSample> hidlSubSamples;
345 hidlSubSamples.setToExternal(const_cast<SubSample*>(kSubSamples),
346 (sizeof(kSubSamples) / sizeof(SubSample)), false /*own*/);
347
348 sp<MemoryDealer> dealer = new MemoryDealer(sizeof(kInBinaryBuffer), "vts-cas");
349 if (nullptr == dealer.get()) {
350 ALOGE("couldn't get MemoryDealer!");
351 return ::testing::AssertionFailure();
352 }
353
354 sp<IMemory> mem = dealer->allocate(sizeof(kInBinaryBuffer));
355 if (nullptr == mem.get()) {
356 ALOGE("couldn't allocate IMemory!");
357 return ::testing::AssertionFailure();
358 }
359 *inMemory = mem;
360
361 // build HidlMemory from memory heap
362 ssize_t offset;
363 size_t size;
364 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
365 if (nullptr == heap.get()) {
366 ALOGE("couldn't get memory heap!");
367 return ::testing::AssertionFailure();
368 }
369
Ytai Ben-Tsvi11107872019-09-09 11:50:45 -0700370 uint8_t* ipBuffer = static_cast<uint8_t*>(static_cast<void*>(mem->unsecurePointer()));
Henry Fanga757b1b2019-02-07 15:01:05 -0800371 memcpy(ipBuffer, kInBinaryBuffer, sizeof(kInBinaryBuffer));
372
373 // hidlMemory is not to be passed out of scope!
374 sp<HidlMemory> hidlMemory = fromHeap(heap);
375
376 SharedBuffer srcBuffer = {
377 .heapBase = *hidlMemory, .offset = (uint64_t)offset, .size = (uint64_t)size};
378
379 DestinationBuffer dstBuffer;
380 dstBuffer.type = BufferType::SHARED_MEMORY;
381 dstBuffer.nonsecureMemory = srcBuffer;
382
383 uint32_t outBytes;
384 hidl_string detailedError;
385 auto returnVoid = descrambler->descramble(
386 ScramblingControl::EVENKEY /*2*/, hidlSubSamples, srcBuffer, 0, dstBuffer, 0,
387 [&](Status status, uint32_t bytesWritten, const hidl_string& detailedErr) {
388 *descrambleStatus = status;
389 outBytes = bytesWritten;
390 detailedError = detailedErr;
391 });
392 if (!returnVoid.isOk() || *descrambleStatus != Status::OK) {
393 ALOGI("descramble failed, trans=%s, status=%d, outBytes=%u, error=%s",
394 returnVoid.description().c_str(), *descrambleStatus, outBytes, detailedError.c_str());
395 }
396 return ::testing::AssertionResult(returnVoid.isOk());
397}
398
399::testing::AssertionResult MediaCasHidlTest::descrambleTestOobInput(
400 const sp<IDescrambler>& descrambler, Status* descrambleStatus,
401 const OobInputTestParams& params) {
402 hidl_vec<SubSample> hidlSubSamples;
403 hidlSubSamples.setToExternal(const_cast<SubSample*>(params.subSamples), params.numSubSamples,
404 false /*own*/);
405
406 sp<MemoryDealer> dealer = new MemoryDealer(params.imemSizeActual, "vts-cas");
407 if (nullptr == dealer.get()) {
408 ALOGE("couldn't get MemoryDealer!");
409 return ::testing::AssertionFailure();
410 }
411
412 sp<IMemory> mem = dealer->allocate(params.imemSizeActual);
413 if (nullptr == mem.get()) {
414 ALOGE("couldn't allocate IMemory!");
415 return ::testing::AssertionFailure();
416 }
417
418 // build HidlMemory from memory heap
419 ssize_t offset;
420 size_t size;
421 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
422 if (nullptr == heap.get()) {
423 ALOGE("couldn't get memory heap!");
424 return ::testing::AssertionFailure();
425 }
426
427 // hidlMemory is not to be passed out of scope!
428 sp<HidlMemory> hidlMemory = fromHeap(heap);
429
430 SharedBuffer srcBuffer = {
431 .heapBase = *hidlMemory,
432 .offset = (uint64_t)offset + params.imemOffset,
433 .size = (uint64_t)params.imemSize,
434 };
435
436 DestinationBuffer dstBuffer;
437 dstBuffer.type = BufferType::SHARED_MEMORY;
438 dstBuffer.nonsecureMemory = srcBuffer;
439
440 uint32_t outBytes;
441 hidl_string detailedError;
442 auto returnVoid = descrambler->descramble(
443 ScramblingControl::EVENKEY /*2*/, hidlSubSamples, srcBuffer, params.srcOffset,
444 dstBuffer, params.dstOffset,
445 [&](Status status, uint32_t bytesWritten, const hidl_string& detailedErr) {
446 *descrambleStatus = status;
447 outBytes = bytesWritten;
448 detailedError = detailedErr;
449 });
450 if (!returnVoid.isOk() || *descrambleStatus != Status::OK) {
451 ALOGI("descramble failed, trans=%s, status=%d, outBytes=%u, error=%s",
452 returnVoid.description().c_str(), *descrambleStatus, outBytes, detailedError.c_str());
453 }
454 return ::testing::AssertionResult(returnVoid.isOk());
455}
456
Dan Shi54a3b0b2019-10-15 15:22:45 -0700457TEST_P(MediaCasHidlTest, TestClearKeyApisWithSession) {
Henry Fanga757b1b2019-02-07 15:01:05 -0800458 description("Test that valid call sequences with SessionEvent send and receive");
459
460 ASSERT_TRUE(createCasPlugin(CLEAR_KEY_SYSTEM_ID));
461
462 auto returnStatus = mMediaCas->provision(hidl_string(PROVISION_STR));
463 EXPECT_TRUE(returnStatus.isOk());
464 EXPECT_EQ(Status::OK, returnStatus);
465
466 hidl_vec<uint8_t> hidlPvtData;
467 hidlPvtData.resize(256);
468 returnStatus = mMediaCas->setPrivateData(hidlPvtData);
469 EXPECT_TRUE(returnStatus.isOk());
470 EXPECT_EQ(Status::OK, returnStatus);
471
472 std::vector<uint8_t> sessionId;
473 ASSERT_TRUE(openCasSession(&sessionId));
474 returnStatus = mMediaCas->setSessionPrivateData(sessionId, hidlPvtData);
475 EXPECT_TRUE(returnStatus.isOk());
476 EXPECT_EQ(Status::OK, returnStatus);
477
478 std::vector<uint8_t> streamSessionId;
479 ASSERT_TRUE(openCasSession(&streamSessionId));
480 returnStatus = mMediaCas->setSessionPrivateData(streamSessionId, hidlPvtData);
481 EXPECT_TRUE(returnStatus.isOk());
482 EXPECT_EQ(Status::OK, returnStatus);
483
Henry Fang8964b852021-01-08 16:09:08 -0800484 if (mIsTestDescrambler) {
485 returnStatus = mDescramblerBase->setMediaCasSession(sessionId);
486 EXPECT_TRUE(returnStatus.isOk());
487 EXPECT_EQ(Status::OK, returnStatus);
Henry Fanga757b1b2019-02-07 15:01:05 -0800488
Henry Fang8964b852021-01-08 16:09:08 -0800489 returnStatus = mDescramblerBase->setMediaCasSession(streamSessionId);
490 EXPECT_TRUE(returnStatus.isOk());
491 EXPECT_EQ(Status::OK, returnStatus);
492 }
Henry Fanga757b1b2019-02-07 15:01:05 -0800493
494 hidl_vec<uint8_t> hidlNullPtr;
495 hidlNullPtr.setToExternal(static_cast<uint8_t*>(nullptr), 0);
496 returnStatus = mMediaCas->refreshEntitlements(3, hidlNullPtr);
497 EXPECT_TRUE(returnStatus.isOk());
498 EXPECT_EQ(Status::OK, returnStatus);
499
500 uint8_t refreshData[] = {0, 1, 2, 3};
501 hidl_vec<uint8_t> hidlRefreshData;
502 hidlRefreshData.setToExternal(static_cast<uint8_t*>(refreshData), sizeof(refreshData));
503 returnStatus = mMediaCas->refreshEntitlements(10, hidlRefreshData);
504 EXPECT_TRUE(returnStatus.isOk());
505 EXPECT_EQ(Status::OK, returnStatus);
506
507 int32_t eventID = 1;
508 int32_t eventArg = 2;
509 mCasListener->testEventEcho(mMediaCas, eventID, eventArg, hidlNullPtr);
510 mCasListener->testSessionEventEcho(mMediaCas, sessionId, eventID, eventArg, hidlNullPtr);
511
512 eventID = 3;
513 eventArg = 4;
514 uint8_t eventData[] = {'e', 'v', 'e', 'n', 't', 'd', 'a', 't', 'a'};
515 hidl_vec<uint8_t> hidlEventData;
516 hidlEventData.setToExternal(static_cast<uint8_t*>(eventData), sizeof(eventData));
517 mCasListener->testEventEcho(mMediaCas, eventID, eventArg, hidlEventData);
518 mCasListener->testSessionEventEcho(mMediaCas, sessionId, eventID, eventArg, hidlEventData);
519
520 uint8_t clearKeyEmmData[] = {'c', 'l', 'e', 'a', 'r', 'k', 'e', 'y', 'e', 'm', 'm'};
521 hidl_vec<uint8_t> hidlClearKeyEmm;
522 hidlClearKeyEmm.setToExternal(static_cast<uint8_t*>(clearKeyEmmData), sizeof(clearKeyEmmData));
523 returnStatus = mMediaCas->processEmm(hidlClearKeyEmm);
524 EXPECT_TRUE(returnStatus.isOk());
525 EXPECT_EQ(Status::OK, returnStatus);
526
527 hidl_vec<uint8_t> hidlEcm;
528 hidlEcm.setToExternal(const_cast<uint8_t*>(kEcmBinaryBuffer), sizeof(kEcmBinaryBuffer));
529 returnStatus = mMediaCas->processEcm(sessionId, hidlEcm);
530 EXPECT_TRUE(returnStatus.isOk());
531 EXPECT_EQ(Status::OK, returnStatus);
532 returnStatus = mMediaCas->processEcm(streamSessionId, hidlEcm);
533 EXPECT_TRUE(returnStatus.isOk());
534 EXPECT_EQ(Status::OK, returnStatus);
535
Henry Fang8964b852021-01-08 16:09:08 -0800536 if (mIsTestDescrambler) {
537 EXPECT_FALSE(mDescramblerBase->requiresSecureDecoderComponent("video/avc"));
Henry Fanga757b1b2019-02-07 15:01:05 -0800538
Henry Fang8964b852021-01-08 16:09:08 -0800539 sp<IDescrambler> descrambler;
540 descrambler = IDescrambler::castFrom(mDescramblerBase);
541 ASSERT_NE(descrambler, nullptr);
Henry Fanga757b1b2019-02-07 15:01:05 -0800542
Henry Fang8964b852021-01-08 16:09:08 -0800543 Status descrambleStatus = Status::OK;
544 sp<IMemory> dataMemory;
Henry Fanga757b1b2019-02-07 15:01:05 -0800545
Henry Fang8964b852021-01-08 16:09:08 -0800546 ASSERT_TRUE(descrambleTestInputBuffer(descrambler, &descrambleStatus, &dataMemory));
547 EXPECT_EQ(Status::OK, descrambleStatus);
Henry Fanga757b1b2019-02-07 15:01:05 -0800548
Henry Fang8964b852021-01-08 16:09:08 -0800549 ASSERT_NE(nullptr, dataMemory.get());
550 uint8_t* opBuffer = static_cast<uint8_t*>(static_cast<void*>(dataMemory->unsecurePointer()));
Henry Fanga757b1b2019-02-07 15:01:05 -0800551
Henry Fang8964b852021-01-08 16:09:08 -0800552 int compareResult =
553 memcmp(static_cast<const void*>(opBuffer),
554 static_cast<const void*>(kOutRefBinaryBuffer), sizeof(kOutRefBinaryBuffer));
555 EXPECT_EQ(0, compareResult);
Henry Fanga757b1b2019-02-07 15:01:05 -0800556
Henry Fang8964b852021-01-08 16:09:08 -0800557 returnStatus = mDescramblerBase->release();
558 EXPECT_TRUE(returnStatus.isOk());
559 EXPECT_EQ(Status::OK, returnStatus);
560 }
Henry Fanga757b1b2019-02-07 15:01:05 -0800561
562 returnStatus = mMediaCas->release();
563 EXPECT_TRUE(returnStatus.isOk());
564 EXPECT_EQ(Status::OK, returnStatus);
565}
566
567} // anonymous namespace
568
Dan Shiff985a82020-07-29 09:45:03 -0700569GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MediaCasHidlTest);
Dan Shi54a3b0b2019-10-15 15:22:45 -0700570INSTANTIATE_TEST_SUITE_P(
571 PerInstance, MediaCasHidlTest,
572 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IMediaCasService::descriptor)),
573 android::hardware::PrintInstanceNameToString);