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