blob: 193253ac7f38e0f5373e6fd2fdd9ec840243c139 [file] [log] [blame]
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +05301/*
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>
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053025#include <android/hardware/cas/1.0/types.h>
26#include <android/hardware/cas/native/1.0/IDescrambler.h>
27#include <android/hardware/cas/native/1.0/types.h>
Chong Zhang704d5862017-10-10 13:03:18 -070028#include <binder/MemoryDealer.h>
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053029#include <hidl/HidlSupport.h>
30#include <hidl/HidlTransportSupport.h>
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053031#include <hidl/Status.h>
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053032#include <utils/Condition.h>
33#include <utils/Mutex.h>
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053034
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053035#define CLEAR_KEY_SYSTEM_ID 0xF6D8
36#define INVALID_SYSTEM_ID 0
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053037#define WAIT_TIMEOUT 3000000000
38
39#define PROVISION_STR \
40 "{ " \
41 " \"id\": 21140844, " \
42 " \"name\": \"Test Title\", " \
43 " \"lowercase_organization_name\": \"Android\", " \
44 " \"asset_key\": { " \
45 " \"encryption_key\": \"nezAr3CHFrmBR9R8Tedotw==\" " \
46 " }, " \
47 " \"cas_type\": 1, " \
48 " \"track_types\": [ ] " \
49 "} "
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053050
51using android::Condition;
52using android::hardware::cas::V1_0::ICas;
53using android::hardware::cas::V1_0::ICasListener;
54using android::hardware::cas::V1_0::IDescramblerBase;
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053055using android::hardware::cas::native::V1_0::IDescrambler;
56using android::hardware::cas::native::V1_0::SubSample;
57using android::hardware::cas::native::V1_0::SharedBuffer;
58using android::hardware::cas::native::V1_0::DestinationBuffer;
59using android::hardware::cas::native::V1_0::BufferType;
60using android::hardware::cas::native::V1_0::ScramblingControl;
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053061using android::hardware::cas::V1_0::IMediaCasService;
62using android::hardware::cas::V1_0::HidlCasPluginDescriptor;
63using android::hardware::Void;
64using android::hardware::hidl_vec;
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053065using android::hardware::hidl_string;
66using android::hardware::hidl_handle;
67using android::hardware::hidl_memory;
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053068using android::hardware::Return;
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053069using android::hardware::cas::V1_0::Status;
Chong Zhang704d5862017-10-10 13:03:18 -070070using android::IMemory;
71using android::IMemoryHeap;
72using android::MemoryDealer;
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053073using android::Mutex;
74using android::sp;
75
76namespace {
77
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053078const uint8_t kEcmBinaryBuffer[] = {
79 0x00, 0x00, 0x01, 0xf0, 0x00, 0x50, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x46, 0x00,
80 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x27, 0x10, 0x02, 0x00,
81 0x01, 0x77, 0x01, 0x42, 0x95, 0x6c, 0x0e, 0xe3, 0x91, 0xbc, 0xfd, 0x05, 0xb1, 0x60, 0x4f,
82 0x17, 0x82, 0xa4, 0x86, 0x9b, 0x23, 0x56, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
83 0x27, 0x10, 0x02, 0x00, 0x01, 0x77, 0x01, 0x42, 0x95, 0x6c, 0xd7, 0x43, 0x62, 0xf8, 0x1c,
84 0x62, 0x19, 0x05, 0xc7, 0x3a, 0x42, 0xcd, 0xfd, 0xd9, 0x13, 0x48,
85};
86
87const SubSample kSubSamples[] = {{162, 0}, {0, 184}, {0, 184}};
88
89const uint8_t kInBinaryBuffer[] = {
90 0x00, 0x00, 0x00, 0x01, 0x09, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0xc0, 0x1e, 0xdb, 0x01,
91 0x40, 0x16, 0xec, 0x04, 0x40, 0x00, 0x00, 0x03, 0x00, 0x40, 0x00, 0x00, 0x0f, 0x03, 0xc5, 0x8b,
92 0xb8, 0x00, 0x00, 0x00, 0x01, 0x68, 0xca, 0x8c, 0xb2, 0x00, 0x00, 0x01, 0x06, 0x05, 0xff, 0xff,
93 0x70, 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7, 0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee,
94 0xef, 0x78, 0x32, 0x36, 0x34, 0x20, 0x2d, 0x20, 0x63, 0x6f, 0x72, 0x65, 0x20, 0x31, 0x34, 0x32,
95 0x20, 0x2d, 0x20, 0x48, 0x2e, 0x32, 0x36, 0x34, 0x2f, 0x4d, 0x50, 0x45, 0x47, 0x2d, 0x34, 0x20,
96 0x41, 0x56, 0x43, 0x20, 0x63, 0x6f, 0x64, 0x65, 0x63, 0x20, 0x2d, 0x20, 0x43, 0x6f, 0x70, 0x79,
97 0x6c, 0x65, 0x66, 0x74, 0x20, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x32, 0x30, 0x31, 0x34, 0x20, 0x2d,
98 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x76, 0x69, 0x64, 0x65,
99 0x6f, 0x6c, 0x61, 0x6e, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x78, 0x32, 0x36, 0x34, 0x2e, 0x68, 0x74,
100 0x6d, 0x6c, 0x6e, 0x45, 0x21, 0x82, 0x38, 0xf0, 0x9d, 0x7d, 0x96, 0xe6, 0x94, 0xae, 0xe2, 0x87,
101 0x8f, 0x04, 0x49, 0xe5, 0xf6, 0x8c, 0x8b, 0x9a, 0x10, 0x18, 0xba, 0x94, 0xe9, 0x22, 0x31, 0x04,
102 0x7e, 0x60, 0x5b, 0xc4, 0x24, 0x00, 0x90, 0x62, 0x0d, 0xdc, 0x85, 0x74, 0x75, 0x78, 0xd0, 0x14,
103 0x08, 0xcb, 0x02, 0x1d, 0x7d, 0x9d, 0x34, 0xe8, 0x81, 0xb9, 0xf7, 0x09, 0x28, 0x79, 0x29, 0x8d,
104 0xe3, 0x14, 0xed, 0x5f, 0xca, 0xaf, 0xf4, 0x1c, 0x49, 0x15, 0xe1, 0x80, 0x29, 0x61, 0x76, 0x80,
105 0x43, 0xf8, 0x58, 0x53, 0x40, 0xd7, 0x31, 0x6d, 0x61, 0x81, 0x41, 0xe9, 0x77, 0x9f, 0x9c, 0xe1,
106 0x6d, 0xf2, 0xee, 0xd9, 0xc8, 0x67, 0xd2, 0x5f, 0x48, 0x73, 0xe3, 0x5c, 0xcd, 0xa7, 0x45, 0x58,
107 0xbb, 0xdd, 0x28, 0x1d, 0x68, 0xfc, 0xb4, 0xc6, 0xf6, 0x92, 0xf6, 0x30, 0x03, 0xaa, 0xe4, 0x32,
108 0xf6, 0x34, 0x51, 0x4b, 0x0f, 0x8c, 0xf9, 0xac, 0x98, 0x22, 0xfb, 0x49, 0xc8, 0xbf, 0xca, 0x8c,
109 0x80, 0x86, 0x5d, 0xd7, 0xa4, 0x52, 0xb1, 0xd9, 0xa6, 0x04, 0x4e, 0xb3, 0x2d, 0x1f, 0xb8, 0x35,
110 0xcc, 0x45, 0x6d, 0x9c, 0x20, 0xa7, 0xa4, 0x34, 0x59, 0x72, 0xe3, 0xae, 0xba, 0x49, 0xde, 0xd1,
111 0xaa, 0xee, 0x3d, 0x77, 0xfc, 0x5d, 0xc6, 0x1f, 0x9d, 0xac, 0xc2, 0x15, 0x66, 0xb8, 0xe1, 0x54,
112 0x4e, 0x74, 0x93, 0xdb, 0x9a, 0x24, 0x15, 0x6e, 0x20, 0xa3, 0x67, 0x3e, 0x5a, 0x24, 0x41, 0x5e,
113 0xb0, 0xe6, 0x35, 0x87, 0x1b, 0xc8, 0x7a, 0xf9, 0x77, 0x65, 0xe0, 0x01, 0xf2, 0x4c, 0xe4, 0x2b,
114 0xa9, 0x64, 0x96, 0x96, 0x0b, 0x46, 0xca, 0xea, 0x79, 0x0e, 0x78, 0xa3, 0x5f, 0x43, 0xfc, 0x47,
115 0x6a, 0x12, 0xfa, 0xc4, 0x33, 0x0e, 0x88, 0x1c, 0x19, 0x3a, 0x00, 0xc3, 0x4e, 0xb5, 0xd8, 0xfa,
116 0x8e, 0xf1, 0xbc, 0x3d, 0xb2, 0x7e, 0x50, 0x8d, 0x67, 0xc3, 0x6b, 0xed, 0xe2, 0xea, 0xa6, 0x1f,
117 0x25, 0x24, 0x7c, 0x94, 0x74, 0x50, 0x49, 0xe3, 0xc6, 0x58, 0x2e, 0xfd, 0x28, 0xb4, 0xc6, 0x73,
118 0xb1, 0x53, 0x74, 0x27, 0x94, 0x5c, 0xdf, 0x69, 0xb7, 0xa1, 0xd7, 0xf5, 0xd3, 0x8a, 0x2c, 0x2d,
119 0xb4, 0x5e, 0x8a, 0x16, 0x14, 0x54, 0x64, 0x6e, 0x00, 0x6b, 0x11, 0x59, 0x8a, 0x63, 0x38, 0x80,
120 0x76, 0xc3, 0xd5, 0x59, 0xf7, 0x3f, 0xd2, 0xfa, 0xa5, 0xca, 0x82, 0xff, 0x4a, 0x62, 0xf0, 0xe3,
121 0x42, 0xf9, 0x3b, 0x38, 0x27, 0x8a, 0x89, 0xaa, 0x50, 0x55, 0x4b, 0x29, 0xf1, 0x46, 0x7c, 0x75,
122 0xef, 0x65, 0xaf, 0x9b, 0x0d, 0x6d, 0xda, 0x25, 0x94, 0x14, 0xc1, 0x1b, 0xf0, 0xc5, 0x4c, 0x24,
123 0x0e, 0x65,
124};
125
126const uint8_t kOutRefBinaryBuffer[] = {
127 0x00, 0x00, 0x00, 0x01, 0x09, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0xc0, 0x1e, 0xdb, 0x01,
128 0x40, 0x16, 0xec, 0x04, 0x40, 0x00, 0x00, 0x03, 0x00, 0x40, 0x00, 0x00, 0x0f, 0x03, 0xc5, 0x8b,
129 0xb8, 0x00, 0x00, 0x00, 0x01, 0x68, 0xca, 0x8c, 0xb2, 0x00, 0x00, 0x01, 0x06, 0x05, 0xff, 0xff,
130 0x70, 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7, 0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee,
131 0xef, 0x78, 0x32, 0x36, 0x34, 0x20, 0x2d, 0x20, 0x63, 0x6f, 0x72, 0x65, 0x20, 0x31, 0x34, 0x32,
132 0x20, 0x2d, 0x20, 0x48, 0x2e, 0x32, 0x36, 0x34, 0x2f, 0x4d, 0x50, 0x45, 0x47, 0x2d, 0x34, 0x20,
133 0x41, 0x56, 0x43, 0x20, 0x63, 0x6f, 0x64, 0x65, 0x63, 0x20, 0x2d, 0x20, 0x43, 0x6f, 0x70, 0x79,
134 0x6c, 0x65, 0x66, 0x74, 0x20, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x32, 0x30, 0x31, 0x34, 0x20, 0x2d,
135 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x76, 0x69, 0x64, 0x65,
136 0x6f, 0x6c, 0x61, 0x6e, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x78, 0x32, 0x36, 0x34, 0x2e, 0x68, 0x74,
137 0x6d, 0x6c, 0x20, 0x2d, 0x20, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x3a, 0x20, 0x63, 0x61,
138 0x62, 0x61, 0x63, 0x3d, 0x30, 0x20, 0x72, 0x65, 0x66, 0x3d, 0x32, 0x20, 0x64, 0x65, 0x62, 0x6c,
139 0x6f, 0x63, 0x6b, 0x3d, 0x31, 0x3a, 0x30, 0x3a, 0x30, 0x20, 0x61, 0x6e, 0x61, 0x6c, 0x79, 0x73,
140 0x65, 0x3d, 0x30, 0x78, 0x31, 0x3a, 0x30, 0x78, 0x31, 0x31, 0x31, 0x20, 0x6d, 0x65, 0x3d, 0x68,
141 0x65, 0x78, 0x20, 0x73, 0x75, 0x62, 0x6d, 0x65, 0x3d, 0x37, 0x20, 0x70, 0x73, 0x79, 0x3d, 0x31,
142 0x20, 0x70, 0x73, 0x79, 0x5f, 0x72, 0x64, 0x3d, 0x31, 0x2e, 0x30, 0x30, 0x3a, 0x30, 0x2e, 0x30,
143 0x30, 0x20, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x5f, 0x72, 0x65, 0x66, 0x3d, 0x31, 0x20, 0x6d, 0x65,
144 0x5f, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x3d, 0x31, 0x36, 0x20, 0x63, 0x68, 0x72, 0x6f, 0x6d, 0x61,
145 0x5f, 0x6d, 0x65, 0x3d, 0x31, 0x20, 0x74, 0x72, 0x65, 0x6c, 0x6c, 0x69, 0x73, 0x3d, 0x31, 0x20,
146 0x38, 0x78, 0x38, 0x64, 0x63, 0x74, 0x3d, 0x30, 0x20, 0x63, 0x71, 0x6d, 0x3d, 0x30, 0x20, 0x64,
147 0x65, 0x61, 0x64, 0x7a, 0x6f, 0x6e, 0x65, 0x3d, 0x32, 0x31, 0x2c, 0x31, 0x31, 0x20, 0x66, 0x61,
148 0x73, 0x74, 0x5f, 0x70, 0x73, 0x6b, 0x69, 0x70, 0x3d, 0x31, 0x20, 0x63, 0x68, 0x72, 0x6f, 0x6d,
149 0x61, 0x5f, 0x71, 0x70, 0x5f, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x3d, 0x2d, 0x32, 0x20, 0x74,
150 0x68, 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d, 0x36, 0x30, 0x20, 0x6c, 0x6f, 0x6f, 0x6b, 0x61, 0x68,
151 0x65, 0x61, 0x64, 0x5f, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d, 0x35, 0x20, 0x73, 0x6c,
152 0x69, 0x63, 0x65, 0x64, 0x5f, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d, 0x30, 0x20, 0x6e,
153 0x72, 0x3d, 0x30, 0x20, 0x64, 0x65, 0x63, 0x69, 0x6d, 0x61, 0x74, 0x65, 0x3d, 0x31, 0x20, 0x69,
154 0x6e, 0x74, 0x65, 0x72, 0x6c, 0x61, 0x63, 0x65, 0x64, 0x3d, 0x30, 0x20, 0x62, 0x6c, 0x75, 0x72,
155 0x61, 0x79, 0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x3d, 0x30, 0x20, 0x63, 0x6f, 0x6e, 0x73,
156 0x74, 0x72, 0x61, 0x69, 0x6e, 0x65, 0x64, 0x5f, 0x69, 0x6e, 0x74, 0x72, 0x61, 0x3d, 0x30, 0x20,
157 0x62, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x73, 0x3d, 0x30, 0x20, 0x77, 0x65, 0x69, 0x67, 0x68, 0x74,
158 0x70, 0x3d, 0x30, 0x20, 0x6b, 0x65, 0x79, 0x69, 0x6e, 0x74, 0x3d, 0x32, 0x35, 0x30, 0x20, 0x6b,
159 0x65, 0x79, 0x69, 0x6e, 0x74, 0x5f, 0x6d, 0x69, 0x6e, 0x3d, 0x32, 0x35, 0x20, 0x73, 0x63, 0x65,
160 0x6e, 0x65,
161};
162
163class MediaCasListener : public ICasListener {
164 public:
165 virtual Return<void> onEvent(int32_t event, int32_t arg,
166 const hidl_vec<uint8_t>& data) override {
167 android::Mutex::Autolock autoLock(mMsgLock);
168 mEvent = event;
169 mEventArg = arg;
170 mEventData = data;
171
172 mEventReceived = true;
173 mMsgCondition.signal();
174 return Void();
175 }
176
177 void testEventEcho(sp<ICas>& mediaCas, int32_t& event, int32_t& eventArg,
178 hidl_vec<uint8_t>& eventData);
179
180 private:
181 int32_t mEvent = -1;
182 int32_t mEventArg = -1;
183 bool mEventReceived = false;
184 hidl_vec<uint8_t> mEventData;
185 android::Mutex mMsgLock;
186 android::Condition mMsgCondition;
187};
188
189void MediaCasListener::testEventEcho(sp<ICas>& mediaCas, int32_t& event, int32_t& eventArg,
190 hidl_vec<uint8_t>& eventData) {
191 mEventReceived = false;
192 auto returnStatus = mediaCas->sendEvent(event, eventArg, eventData);
193 EXPECT_TRUE(returnStatus.isOk());
194 EXPECT_EQ(Status::OK, returnStatus);
195
196 android::Mutex::Autolock autoLock(mMsgLock);
197 while (!mEventReceived) {
198 if (-ETIMEDOUT == mMsgCondition.waitRelative(mMsgLock, WAIT_TIMEOUT)) {
199 EXPECT_TRUE(false) << "event not received within timeout";
200 return;
201 }
202 }
203
204 EXPECT_EQ(mEvent, event);
205 EXPECT_EQ(mEventArg, eventArg);
206 EXPECT_TRUE(mEventData == eventData);
207}
208
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530209class MediaCasHidlTest : public ::testing::VtsHalHidlTargetTestBase {
210 public:
211 virtual void SetUp() override {
212 mService = ::testing::VtsHalHidlTargetTestBase::getService<IMediaCasService>();
213 ASSERT_NE(mService, nullptr);
214 }
215
216 sp<IMediaCasService> mService;
217
218 protected:
219 static void description(const std::string& description) {
220 RecordProperty("description", description);
221 }
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530222
223 sp<ICas> mMediaCas;
224 sp<IDescramblerBase> mDescramblerBase;
225 sp<MediaCasListener> mCasListener;
Chong Zhang95cc5af2017-10-25 13:40:28 -0700226 typedef struct _OobInputTestParams {
227 const SubSample* subSamples;
228 uint32_t numSubSamples;
229 size_t imemSizeActual;
230 uint64_t imemOffset;
231 uint64_t imemSize;
232 uint64_t srcOffset;
233 uint64_t dstOffset;
234 } OobInputTestParams;
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530235
236 ::testing::AssertionResult createCasPlugin(int32_t caSystemId);
237 ::testing::AssertionResult openCasSession(std::vector<uint8_t>* sessionId);
Chong Zhang95cc5af2017-10-25 13:40:28 -0700238 ::testing::AssertionResult descrambleTestInputBuffer(
239 const sp<IDescrambler>& descrambler,
240 Status* descrambleStatus,
241 sp<IMemory>* hidlInMemory);
242 ::testing::AssertionResult descrambleTestOobInput(
243 const sp<IDescrambler>& descrambler,
244 Status* descrambleStatus,
245 const OobInputTestParams& params);
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530246};
247
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530248::testing::AssertionResult MediaCasHidlTest::createCasPlugin(int32_t caSystemId) {
249 auto status = mService->isSystemIdSupported(caSystemId);
250 if (!status.isOk() || !status) {
251 return ::testing::AssertionFailure();
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530252 }
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530253 status = mService->isDescramblerSupported(caSystemId);
254 if (!status.isOk() || !status) {
255 return ::testing::AssertionFailure();
256 }
257
258 mCasListener = new MediaCasListener();
259 auto pluginStatus = mService->createPlugin(caSystemId, mCasListener);
260 if (!pluginStatus.isOk()) {
261 return ::testing::AssertionFailure();
262 }
263 mMediaCas = pluginStatus;
264 if (mMediaCas == nullptr) {
265 return ::testing::AssertionFailure();
266 }
267
268 auto descramblerStatus = mService->createDescrambler(caSystemId);
269 if (!descramblerStatus.isOk()) {
270 return ::testing::AssertionFailure();
271 }
272 mDescramblerBase = descramblerStatus;
273 return ::testing::AssertionResult(mDescramblerBase != nullptr);
274}
275
276::testing::AssertionResult MediaCasHidlTest::openCasSession(std::vector<uint8_t>* sessionId) {
277 Status sessionStatus;
278 auto returnVoid = mMediaCas->openSession([&](Status status, const hidl_vec<uint8_t>& id) {
279 sessionStatus = status;
280 *sessionId = id;
281 });
282 return ::testing::AssertionResult(returnVoid.isOk() && (Status::OK == sessionStatus));
283}
284
285::testing::AssertionResult MediaCasHidlTest::descrambleTestInputBuffer(
Chong Zhang704d5862017-10-10 13:03:18 -0700286 const sp<IDescrambler>& descrambler, Status* descrambleStatus, sp<IMemory>* inMemory) {
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530287 hidl_vec<SubSample> hidlSubSamples;
288 hidlSubSamples.setToExternal(const_cast<SubSample*>(kSubSamples),
289 (sizeof(kSubSamples) / sizeof(SubSample)), false /*own*/);
Chong Zhang704d5862017-10-10 13:03:18 -0700290
291 sp<MemoryDealer> dealer = new MemoryDealer(sizeof(kInBinaryBuffer), "vts-cas");
292 if (nullptr == dealer.get()) {
293 ALOGE("couldn't get MemoryDealer!");
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530294 return ::testing::AssertionFailure();
295 }
296
Chong Zhang704d5862017-10-10 13:03:18 -0700297 sp<IMemory> mem = dealer->allocate(sizeof(kInBinaryBuffer));
298 if (nullptr == mem.get()) {
299 ALOGE("couldn't allocate IMemory!");
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530300 return ::testing::AssertionFailure();
301 }
Chong Zhang704d5862017-10-10 13:03:18 -0700302 *inMemory = mem;
303
304 // build hidl_memory from memory heap
305 ssize_t offset;
306 size_t size;
307 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
308 if (nullptr == heap.get()) {
309 ALOGE("couldn't get memory heap!");
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530310 return ::testing::AssertionFailure();
311 }
312
Chong Zhang704d5862017-10-10 13:03:18 -0700313 native_handle_t* nativeHandle = native_handle_create(1, 0);
314 if (!nativeHandle) {
315 ALOGE("failed to create native handle!");
316 return ::testing::AssertionFailure();
317 }
318 nativeHandle->data[0] = heap->getHeapID();
319
320 uint8_t* ipBuffer = static_cast<uint8_t*>(static_cast<void*>(mem->pointer()));
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530321 memcpy(ipBuffer, kInBinaryBuffer, sizeof(kInBinaryBuffer));
322
323 SharedBuffer srcBuffer = {
Chong Zhang704d5862017-10-10 13:03:18 -0700324 .heapBase = hidl_memory("ashmem", hidl_handle(nativeHandle), heap->getSize()),
325 .offset = (uint64_t) offset,
326 .size = (uint64_t) size
327 };
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530328
329 DestinationBuffer dstBuffer;
330 dstBuffer.type = BufferType::SHARED_MEMORY;
331 dstBuffer.nonsecureMemory = srcBuffer;
332
333 uint32_t outBytes;
334 hidl_string detailedError;
335 auto returnVoid = descrambler->descramble(
336 ScramblingControl::EVENKEY /*2*/, hidlSubSamples, srcBuffer, 0, dstBuffer, 0,
337 [&](Status status, uint32_t bytesWritten, const hidl_string& detailedErr) {
338 *descrambleStatus = status;
339 outBytes = bytesWritten;
340 detailedError = detailedErr;
341 });
342 if (!returnVoid.isOk() || *descrambleStatus != Status::OK) {
343 ALOGI("descramble failed, trans=%s, status=%d, outBytes=%u, error=%s",
344 returnVoid.description().c_str(), *descrambleStatus, outBytes, detailedError.c_str());
345 }
346 return ::testing::AssertionResult(returnVoid.isOk());
347}
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530348
Chong Zhang95cc5af2017-10-25 13:40:28 -0700349::testing::AssertionResult MediaCasHidlTest::descrambleTestOobInput(
350 const sp<IDescrambler>& descrambler,
351 Status* descrambleStatus,
352 const OobInputTestParams& params) {
353 hidl_vec<SubSample> hidlSubSamples;
354 hidlSubSamples.setToExternal(
355 const_cast<SubSample*>(params.subSamples), params.numSubSamples, false /*own*/);
356
357 sp<MemoryDealer> dealer = new MemoryDealer(params.imemSizeActual, "vts-cas");
358 if (nullptr == dealer.get()) {
359 ALOGE("couldn't get MemoryDealer!");
360 return ::testing::AssertionFailure();
361 }
362
363 sp<IMemory> mem = dealer->allocate(params.imemSizeActual);
364 if (nullptr == mem.get()) {
365 ALOGE("couldn't allocate IMemory!");
366 return ::testing::AssertionFailure();
367 }
368
369 // build hidl_memory from memory heap
370 ssize_t offset;
371 size_t size;
372 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
373 if (nullptr == heap.get()) {
374 ALOGE("couldn't get memory heap!");
375 return ::testing::AssertionFailure();
376 }
377
378 native_handle_t* nativeHandle = native_handle_create(1, 0);
379 if (!nativeHandle) {
380 ALOGE("failed to create native handle!");
381 return ::testing::AssertionFailure();
382 }
383 nativeHandle->data[0] = heap->getHeapID();
384
385 SharedBuffer srcBuffer = {
386 .heapBase = hidl_memory("ashmem", hidl_handle(nativeHandle), heap->getSize()),
387 .offset = (uint64_t) offset + params.imemOffset,
388 .size = (uint64_t) params.imemSize,
389 };
390
391 DestinationBuffer dstBuffer;
392 dstBuffer.type = BufferType::SHARED_MEMORY;
393 dstBuffer.nonsecureMemory = srcBuffer;
394
395 uint32_t outBytes;
396 hidl_string detailedError;
397 auto returnVoid = descrambler->descramble(
398 ScramblingControl::EVENKEY /*2*/, hidlSubSamples,
399 srcBuffer,
400 params.srcOffset,
401 dstBuffer,
402 params.dstOffset,
403 [&](Status status, uint32_t bytesWritten, const hidl_string& detailedErr) {
404 *descrambleStatus = status;
405 outBytes = bytesWritten;
406 detailedError = detailedErr;
407 });
408 if (!returnVoid.isOk() || *descrambleStatus != Status::OK) {
409 ALOGI("descramble failed, trans=%s, status=%d, outBytes=%u, error=%s",
410 returnVoid.description().c_str(), *descrambleStatus, outBytes, detailedError.c_str());
411 }
412 return ::testing::AssertionResult(returnVoid.isOk());
413}
414
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530415TEST_F(MediaCasHidlTest, EnumeratePlugins) {
416 description("Test enumerate plugins");
417 hidl_vec<HidlCasPluginDescriptor> descriptors;
418 EXPECT_TRUE(mService
419 ->enumeratePlugins([&descriptors](
420 hidl_vec<HidlCasPluginDescriptor> const& desc) { descriptors = desc; })
421 .isOk());
422
423 if (descriptors.size() == 0) {
424 ALOGW("[ WARN ] enumeratePlugins list empty");
425 return;
426 }
427
428 sp<MediaCasListener> casListener = new MediaCasListener();
429 for (size_t i = 0; i < descriptors.size(); i++) {
430 int32_t caSystemId = descriptors[i].caSystemId;
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530431
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530432 ASSERT_TRUE(createCasPlugin(caSystemId));
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530433 }
434}
435
436TEST_F(MediaCasHidlTest, TestInvalidSystemIdFails) {
437 description("Test failure for invalid system ID");
438 sp<MediaCasListener> casListener = new MediaCasListener();
439
440 ASSERT_FALSE(mService->isSystemIdSupported(INVALID_SYSTEM_ID));
441 ASSERT_FALSE(mService->isDescramblerSupported(INVALID_SYSTEM_ID));
442
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530443 auto pluginStatus = mService->createPlugin(INVALID_SYSTEM_ID, casListener);
444 ASSERT_TRUE(pluginStatus.isOk());
445 sp<ICas> mediaCas = pluginStatus;
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530446 EXPECT_EQ(mediaCas, nullptr);
447
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530448 auto descramblerStatus = mService->createDescrambler(INVALID_SYSTEM_ID);
449 ASSERT_TRUE(descramblerStatus.isOk());
450 sp<IDescramblerBase> descramblerBase = descramblerStatus;
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530451 EXPECT_EQ(descramblerBase, nullptr);
452}
453
454TEST_F(MediaCasHidlTest, TestClearKeyPluginInstalled) {
455 description("Test if ClearKey plugin is installed");
456 hidl_vec<HidlCasPluginDescriptor> descriptors;
457 EXPECT_TRUE(mService
458 ->enumeratePlugins([&descriptors](
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530459 hidl_vec<HidlCasPluginDescriptor> const& desc) { descriptors = desc; })
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530460 .isOk());
461
462 if (descriptors.size() == 0) {
463 ALOGW("[ WARN ] enumeratePlugins list empty");
464 }
465
466 for (size_t i = 0; i < descriptors.size(); i++) {
467 int32_t caSystemId = descriptors[i].caSystemId;
468 if (CLEAR_KEY_SYSTEM_ID == caSystemId) {
469 return;
470 }
471 }
472
473 ASSERT_TRUE(false) << "ClearKey plugin not installed";
474}
475
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530476TEST_F(MediaCasHidlTest, TestClearKeyApis) {
477 description("Test that valid call sequences succeed");
478
479 ASSERT_TRUE(createCasPlugin(CLEAR_KEY_SYSTEM_ID));
480
481 auto returnStatus = mMediaCas->provision(hidl_string(PROVISION_STR));
482 EXPECT_TRUE(returnStatus.isOk());
483 EXPECT_EQ(Status::OK, returnStatus);
484
485 hidl_vec<uint8_t> hidlPvtData;
486 hidlPvtData.resize(256);
487 returnStatus = mMediaCas->setPrivateData(hidlPvtData);
488 EXPECT_TRUE(returnStatus.isOk());
489 EXPECT_EQ(Status::OK, returnStatus);
490
491 std::vector<uint8_t> sessionId;
492 ASSERT_TRUE(openCasSession(&sessionId));
493 returnStatus = mMediaCas->setSessionPrivateData(sessionId, hidlPvtData);
494 EXPECT_TRUE(returnStatus.isOk());
495 EXPECT_EQ(Status::OK, returnStatus);
496
497 std::vector<uint8_t> streamSessionId;
498 ASSERT_TRUE(openCasSession(&streamSessionId));
499 returnStatus = mMediaCas->setSessionPrivateData(streamSessionId, hidlPvtData);
500 EXPECT_TRUE(returnStatus.isOk());
501 EXPECT_EQ(Status::OK, returnStatus);
502
503 returnStatus = mDescramblerBase->setMediaCasSession(sessionId);
504 EXPECT_TRUE(returnStatus.isOk());
505 EXPECT_EQ(Status::OK, returnStatus);
506
507 returnStatus = mDescramblerBase->setMediaCasSession(streamSessionId);
508 EXPECT_TRUE(returnStatus.isOk());
509 EXPECT_EQ(Status::OK, returnStatus);
510
511 hidl_vec<uint8_t> hidlNullPtr;
512 hidlNullPtr.setToExternal(static_cast<uint8_t*>(nullptr), 0);
513 returnStatus = mMediaCas->refreshEntitlements(3, hidlNullPtr);
514 EXPECT_TRUE(returnStatus.isOk());
515 EXPECT_EQ(Status::OK, returnStatus);
516
517 uint8_t refreshData[] = {0, 1, 2, 3};
518 hidl_vec<uint8_t> hidlRefreshData;
519 hidlRefreshData.setToExternal(static_cast<uint8_t*>(refreshData), sizeof(refreshData));
520 returnStatus = mMediaCas->refreshEntitlements(10, hidlRefreshData);
521 EXPECT_TRUE(returnStatus.isOk());
522 EXPECT_EQ(Status::OK, returnStatus);
523
524 int32_t eventID = 1;
525 int32_t eventArg = 2;
526 mCasListener->testEventEcho(mMediaCas, eventID, eventArg, hidlNullPtr);
527
528 eventID = 3;
529 eventArg = 4;
530 uint8_t eventData[] = {'e', 'v', 'e', 'n', 't', 'd', 'a', 't', 'a'};
531 hidl_vec<uint8_t> hidlEventData;
532 hidlEventData.setToExternal(static_cast<uint8_t*>(eventData), sizeof(eventData));
533 mCasListener->testEventEcho(mMediaCas, eventID, eventArg, hidlEventData);
534
535 uint8_t clearKeyEmmData[] = {'c', 'l', 'e', 'a', 'r', 'k', 'e', 'y', 'e', 'm', 'm'};
536 hidl_vec<uint8_t> hidlClearKeyEmm;
537 hidlClearKeyEmm.setToExternal(static_cast<uint8_t*>(clearKeyEmmData), sizeof(clearKeyEmmData));
538 returnStatus = mMediaCas->processEmm(hidlClearKeyEmm);
539 EXPECT_TRUE(returnStatus.isOk());
540 EXPECT_EQ(Status::OK, returnStatus);
541
542 hidl_vec<uint8_t> hidlEcm;
543 hidlEcm.setToExternal(const_cast<uint8_t*>(kEcmBinaryBuffer), sizeof(kEcmBinaryBuffer));
544 returnStatus = mMediaCas->processEcm(sessionId, hidlEcm);
545 EXPECT_TRUE(returnStatus.isOk());
546 EXPECT_EQ(Status::OK, returnStatus);
547 returnStatus = mMediaCas->processEcm(streamSessionId, hidlEcm);
548 EXPECT_TRUE(returnStatus.isOk());
549 EXPECT_EQ(Status::OK, returnStatus);
550
Suresh Sivaraman6d176aa2017-10-04 14:37:36 +0530551 EXPECT_FALSE(mDescramblerBase->requiresSecureDecoderComponent("video/avc"));
552
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530553 sp<IDescrambler> descrambler;
554 descrambler = IDescrambler::castFrom(mDescramblerBase);
555 ASSERT_NE(descrambler, nullptr);
556
557 Status descrambleStatus = Status::OK;
Chong Zhang704d5862017-10-10 13:03:18 -0700558 sp<IMemory> dataMemory;
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530559
Chong Zhang704d5862017-10-10 13:03:18 -0700560 ASSERT_TRUE(descrambleTestInputBuffer(descrambler, &descrambleStatus, &dataMemory));
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530561 EXPECT_EQ(Status::OK, descrambleStatus);
562
Chong Zhang704d5862017-10-10 13:03:18 -0700563 ASSERT_NE(nullptr, dataMemory.get());
564 uint8_t* opBuffer = static_cast<uint8_t*>(static_cast<void*>(dataMemory->pointer()));
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530565
566 int compareResult =
567 memcmp(static_cast<const void*>(opBuffer), static_cast<const void*>(kOutRefBinaryBuffer),
568 sizeof(kOutRefBinaryBuffer));
569 EXPECT_EQ(0, compareResult);
Suresh Sivaraman6d176aa2017-10-04 14:37:36 +0530570
571 returnStatus = mDescramblerBase->release();
572 EXPECT_TRUE(returnStatus.isOk());
573 EXPECT_EQ(Status::OK, returnStatus);
574
575 returnStatus = mMediaCas->release();
576 EXPECT_TRUE(returnStatus.isOk());
577 EXPECT_EQ(Status::OK, returnStatus);
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530578}
579
580TEST_F(MediaCasHidlTest, TestClearKeySessionClosedAfterRelease) {
581 description("Test that all sessions are closed after a MediaCas object is released");
582
583 ASSERT_TRUE(createCasPlugin(CLEAR_KEY_SYSTEM_ID));
584
585 auto returnStatus = mMediaCas->provision(hidl_string(PROVISION_STR));
586 EXPECT_TRUE(returnStatus.isOk());
587 EXPECT_EQ(Status::OK, returnStatus);
588
589 std::vector<uint8_t> sessionId;
590 ASSERT_TRUE(openCasSession(&sessionId));
591 std::vector<uint8_t> streamSessionId;
592 ASSERT_TRUE(openCasSession(&streamSessionId));
593
594 returnStatus = mMediaCas->release();
595 EXPECT_TRUE(returnStatus.isOk());
596 EXPECT_EQ(Status::OK, returnStatus);
597
598 returnStatus = mDescramblerBase->setMediaCasSession(sessionId);
599 EXPECT_TRUE(returnStatus.isOk());
600 EXPECT_EQ(Status::ERROR_CAS_SESSION_NOT_OPENED, returnStatus);
601
602 returnStatus = mDescramblerBase->setMediaCasSession(streamSessionId);
603 EXPECT_TRUE(returnStatus.isOk());
604 EXPECT_EQ(Status::ERROR_CAS_SESSION_NOT_OPENED, returnStatus);
605}
606
607TEST_F(MediaCasHidlTest, TestClearKeyErrors) {
608 description("Test that invalid call sequences fail with expected error codes");
609
610 ASSERT_TRUE(createCasPlugin(CLEAR_KEY_SYSTEM_ID));
611
612 /*
613 * Test MediaCas error codes
614 */
615 // Provision should fail with an invalid asset string
616 auto returnStatus = mMediaCas->provision(hidl_string("invalid asset string"));
617 EXPECT_TRUE(returnStatus.isOk());
618 EXPECT_EQ(Status::ERROR_CAS_NO_LICENSE, returnStatus);
619
620 // Open a session, then close it so that it should become invalid
621 std::vector<uint8_t> invalidSessionId;
622 ASSERT_TRUE(openCasSession(&invalidSessionId));
623 returnStatus = mMediaCas->closeSession(invalidSessionId);
624 EXPECT_TRUE(returnStatus.isOk());
625 EXPECT_EQ(Status::OK, returnStatus);
626
627 // processEcm should fail with an invalid session id
628 hidl_vec<uint8_t> hidlEcm;
629 hidlEcm.setToExternal(const_cast<uint8_t*>(kEcmBinaryBuffer), sizeof(kEcmBinaryBuffer));
630 returnStatus = mMediaCas->processEcm(invalidSessionId, hidlEcm);
631 EXPECT_TRUE(returnStatus.isOk());
632 EXPECT_EQ(Status::ERROR_CAS_SESSION_NOT_OPENED, returnStatus);
633
634 std::vector<uint8_t> sessionId;
635 ASSERT_TRUE(openCasSession(&sessionId));
636
637 // processEcm should fail without provisioning
638 hidlEcm.setToExternal(const_cast<uint8_t*>(kEcmBinaryBuffer), sizeof(kEcmBinaryBuffer));
639 returnStatus = mMediaCas->processEcm(sessionId, hidlEcm);
640 EXPECT_TRUE(returnStatus.isOk());
641 EXPECT_EQ(Status::ERROR_CAS_NOT_PROVISIONED, returnStatus);
642
643 returnStatus = mMediaCas->provision(hidl_string(PROVISION_STR));
644 EXPECT_TRUE(returnStatus.isOk());
645 EXPECT_EQ(Status::OK, returnStatus);
646
647 // processEcm should fail with ecm buffer that's too short
648 hidlEcm.setToExternal(const_cast<uint8_t*>(kEcmBinaryBuffer), 8);
649 returnStatus = mMediaCas->processEcm(sessionId, hidlEcm);
650 EXPECT_TRUE(returnStatus.isOk());
651 EXPECT_EQ(Status::BAD_VALUE, returnStatus);
652
653 // processEcm should fail with ecm with bad descriptor count
654 uint8_t badDescriptor[sizeof(kEcmBinaryBuffer)];
655 memcpy(badDescriptor, kEcmBinaryBuffer, sizeof(kEcmBinaryBuffer));
656 badDescriptor[17] = 0x03; // change the descriptor count field to 3 (invalid)
657 hidlEcm.setToExternal(static_cast<uint8_t*>(badDescriptor), sizeof(badDescriptor));
658 returnStatus = mMediaCas->processEcm(sessionId, hidlEcm);
659 EXPECT_TRUE(returnStatus.isOk());
660 EXPECT_EQ(Status::ERROR_CAS_UNKNOWN, returnStatus);
661
662 /*
663 * Test MediaDescrambler error codes
664 */
665 // setMediaCasSession should fail with an invalid session id
666 returnStatus = mDescramblerBase->setMediaCasSession(invalidSessionId);
667 EXPECT_TRUE(returnStatus.isOk());
668 EXPECT_EQ(Status::ERROR_CAS_SESSION_NOT_OPENED, returnStatus);
669
670 // descramble should fail without a valid session
671 sp<IDescrambler> descrambler;
672 descrambler = IDescrambler::castFrom(mDescramblerBase);
673 ASSERT_NE(descrambler, nullptr);
674
675 Status descrambleStatus = Status::OK;
Chong Zhang704d5862017-10-10 13:03:18 -0700676 sp<IMemory> dataMemory;
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530677
Chong Zhang704d5862017-10-10 13:03:18 -0700678 ASSERT_TRUE(descrambleTestInputBuffer(descrambler, &descrambleStatus, &dataMemory));
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530679 EXPECT_EQ(Status::ERROR_CAS_DECRYPT_UNIT_NOT_INITIALIZED, descrambleStatus);
680
681 // Now set a valid session, should still fail because no valid ecm is processed
682 returnStatus = mDescramblerBase->setMediaCasSession(sessionId);
683 EXPECT_TRUE(returnStatus.isOk());
684 EXPECT_EQ(Status::OK, returnStatus);
685
Chong Zhang704d5862017-10-10 13:03:18 -0700686 ASSERT_TRUE(descrambleTestInputBuffer(descrambler, &descrambleStatus, &dataMemory));
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530687 EXPECT_EQ(Status::ERROR_CAS_DECRYPT, descrambleStatus);
Suresh Sivaraman6d176aa2017-10-04 14:37:36 +0530688
689 // Verify that requiresSecureDecoderComponent handles empty mime
690 EXPECT_FALSE(mDescramblerBase->requiresSecureDecoderComponent(""));
691
692 // Verify that requiresSecureDecoderComponent handles invalid mime
693 EXPECT_FALSE(mDescramblerBase->requiresSecureDecoderComponent("bad"));
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530694}
695
Chong Zhang95cc5af2017-10-25 13:40:28 -0700696TEST_F(MediaCasHidlTest, TestClearKeyOobFails) {
697 description("Test that oob descramble request fails with expected error");
698
699 ASSERT_TRUE(createCasPlugin(CLEAR_KEY_SYSTEM_ID));
700
701 auto returnStatus = mMediaCas->provision(hidl_string(PROVISION_STR));
702 EXPECT_TRUE(returnStatus.isOk());
703 EXPECT_EQ(Status::OK, returnStatus);
704
705 std::vector<uint8_t> sessionId;
706 ASSERT_TRUE(openCasSession(&sessionId));
707
708 returnStatus = mDescramblerBase->setMediaCasSession(sessionId);
709 EXPECT_TRUE(returnStatus.isOk());
710 EXPECT_EQ(Status::OK, returnStatus);
711
712 hidl_vec<uint8_t> hidlEcm;
713 hidlEcm.setToExternal(const_cast<uint8_t*>(kEcmBinaryBuffer), sizeof(kEcmBinaryBuffer));
714 returnStatus = mMediaCas->processEcm(sessionId, hidlEcm);
715 EXPECT_TRUE(returnStatus.isOk());
716 EXPECT_EQ(Status::OK, returnStatus);
717
718 sp<IDescrambler> descrambler = IDescrambler::castFrom(mDescramblerBase);
719 ASSERT_NE(nullptr, descrambler.get());
720
721 Status descrambleStatus = Status::OK;
722
723 // test invalid src buffer offset
724 ASSERT_TRUE(descrambleTestOobInput(
725 descrambler,
726 &descrambleStatus,
727 {
728 .subSamples = kSubSamples,
729 .numSubSamples = sizeof(kSubSamples)/sizeof(SubSample),
730 .imemSizeActual = sizeof(kInBinaryBuffer),
731 .imemOffset = 0xcccccc,
732 .imemSize = sizeof(kInBinaryBuffer),
733 .srcOffset = 0,
734 .dstOffset = 0
735 }));
736 EXPECT_EQ(Status::BAD_VALUE, descrambleStatus);
737
738 // test invalid src buffer size
739 ASSERT_TRUE(descrambleTestOobInput(
740 descrambler,
741 &descrambleStatus,
742 {
743 .subSamples = kSubSamples,
744 .numSubSamples = sizeof(kSubSamples)/sizeof(SubSample),
745 .imemSizeActual = sizeof(kInBinaryBuffer),
746 .imemOffset = 0,
747 .imemSize = 0xcccccc,
748 .srcOffset = 0,
749 .dstOffset = 0
750 }));
751 EXPECT_EQ(Status::BAD_VALUE, descrambleStatus);
752
753 // test invalid src buffer size
754 ASSERT_TRUE(descrambleTestOobInput(
755 descrambler,
756 &descrambleStatus,
757 {
758 .subSamples = kSubSamples,
759 .numSubSamples = sizeof(kSubSamples)/sizeof(SubSample),
760 .imemSizeActual = sizeof(kInBinaryBuffer),
761 .imemOffset = 1,
762 .imemSize = (uint64_t)-1,
763 .srcOffset = 0,
764 .dstOffset = 0
765 }));
766 EXPECT_EQ(Status::BAD_VALUE, descrambleStatus);
767
768 // test invalid srcOffset
769 ASSERT_TRUE(descrambleTestOobInput(
770 descrambler,
771 &descrambleStatus,
772 {
773 .subSamples = kSubSamples,
774 .numSubSamples = sizeof(kSubSamples)/sizeof(SubSample),
775 .imemSizeActual = sizeof(kInBinaryBuffer),
776 .imemOffset = 0,
777 .imemSize = sizeof(kInBinaryBuffer),
778 .srcOffset = 0xcccccc,
779 .dstOffset = 0
780 }));
781 EXPECT_EQ(Status::BAD_VALUE, descrambleStatus);
782
783 // test invalid dstOffset
784 ASSERT_TRUE(descrambleTestOobInput(
785 descrambler,
786 &descrambleStatus,
787 {
788 .subSamples = kSubSamples,
789 .numSubSamples = sizeof(kSubSamples)/sizeof(SubSample),
790 .imemSizeActual = sizeof(kInBinaryBuffer),
791 .imemOffset = 0,
792 .imemSize = sizeof(kInBinaryBuffer),
793 .srcOffset = 0,
794 .dstOffset = 0xcccccc
795 }));
796 EXPECT_EQ(Status::BAD_VALUE, descrambleStatus);
797
798 // test detection of oob subsample sizes
799 const SubSample invalidSubSamples1[] =
800 {{162, 0}, {0, 184}, {0, 0xdddddd}};
801
802 ASSERT_TRUE(descrambleTestOobInput(
803 descrambler,
804 &descrambleStatus,
805 {
806 .subSamples = invalidSubSamples1,
807 .numSubSamples = sizeof(invalidSubSamples1)/sizeof(SubSample),
808 .imemSizeActual = sizeof(kInBinaryBuffer),
809 .imemOffset = 0,
810 .imemSize = sizeof(kInBinaryBuffer),
811 .srcOffset = 0,
812 .dstOffset = 0
813 }));
814 EXPECT_EQ(Status::BAD_VALUE, descrambleStatus);
815
816 // test detection of overflowing subsample sizes
817 const SubSample invalidSubSamples2[] =
818 {{162, 0}, {0, 184}, {2, (uint32_t)-1}};
819
820 ASSERT_TRUE(descrambleTestOobInput(
821 descrambler,
822 &descrambleStatus,
823 {
824 .subSamples = invalidSubSamples2,
825 .numSubSamples = sizeof(invalidSubSamples2)/sizeof(SubSample),
826 .imemSizeActual = sizeof(kInBinaryBuffer),
827 .imemOffset = 0,
828 .imemSize = sizeof(kInBinaryBuffer),
829 .srcOffset = 0,
830 .dstOffset = 0
831 }));
832 EXPECT_EQ(Status::BAD_VALUE, descrambleStatus);
833
834 returnStatus = mDescramblerBase->release();
835 EXPECT_TRUE(returnStatus.isOk());
836 EXPECT_EQ(Status::OK, returnStatus);
837
838 returnStatus = mMediaCas->release();
839 EXPECT_TRUE(returnStatus.isOk());
840 EXPECT_EQ(Status::OK, returnStatus);
841}
842
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530843} // anonymous namespace
844
845int main(int argc, char** argv) {
846 ::testing::InitGoogleTest(&argc, argv);
847 int status = RUN_ALL_TESTS();
848 LOG(INFO) << "Test result = " << status;
849 return status;
850}