blob: df0c85934f4d2f9a5f2dfbc5e15a27cede145903 [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
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053019#include <android-base/logging.h>
20#include <android/hardware/cas/1.0/ICas.h>
21#include <android/hardware/cas/1.0/ICasListener.h>
22#include <android/hardware/cas/1.0/IDescramblerBase.h>
23#include <android/hardware/cas/1.0/IMediaCasService.h>
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053024#include <android/hardware/cas/1.0/types.h>
Henry Fang8964b852021-01-08 16:09:08 -080025#include <android/hardware/cas/1.2/IMediaCasService.h>
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053026#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>
Dan Shi54a3b0b2019-10-15 15:22:45 -070029#include <gtest/gtest.h>
30#include <hidl/GtestPrinter.h>
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053031#include <hidl/HidlSupport.h>
32#include <hidl/HidlTransportSupport.h>
Dan Shi54a3b0b2019-10-15 15:22:45 -070033#include <hidl/ServiceManagement.h>
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053034#include <hidl/Status.h>
Chong Zhangd2eb83e2018-02-13 18:19:43 -080035#include <hidlmemory/FrameworkUtils.h>
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053036#include <utils/Condition.h>
37#include <utils/Mutex.h>
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053038
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053039#define CLEAR_KEY_SYSTEM_ID 0xF6D8
40#define INVALID_SYSTEM_ID 0
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053041#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 "} "
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053054
55using android::Condition;
56using android::hardware::cas::V1_0::ICas;
57using android::hardware::cas::V1_0::ICasListener;
58using android::hardware::cas::V1_0::IDescramblerBase;
Chong Zhangd2eb83e2018-02-13 18:19:43 -080059using android::hardware::cas::V1_0::Status;
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053060using android::hardware::cas::native::V1_0::IDescrambler;
61using android::hardware::cas::native::V1_0::SubSample;
62using android::hardware::cas::native::V1_0::SharedBuffer;
63using android::hardware::cas::native::V1_0::DestinationBuffer;
64using android::hardware::cas::native::V1_0::BufferType;
65using android::hardware::cas::native::V1_0::ScramblingControl;
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053066using android::hardware::cas::V1_0::IMediaCasService;
67using android::hardware::cas::V1_0::HidlCasPluginDescriptor;
Chong Zhangd2eb83e2018-02-13 18:19:43 -080068using android::hardware::fromHeap;
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053069using android::hardware::hidl_vec;
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053070using android::hardware::hidl_string;
Chong Zhangd2eb83e2018-02-13 18:19:43 -080071using android::hardware::HidlMemory;
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053072using android::hardware::Return;
Chong Zhangd2eb83e2018-02-13 18:19:43 -080073using android::hardware::Void;
Chong Zhang704d5862017-10-10 13:03:18 -070074using android::IMemory;
75using android::IMemoryHeap;
76using android::MemoryDealer;
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +053077using android::Mutex;
78using android::sp;
79
80namespace {
81
Suresh Sivaraman0645aec2017-09-21 18:44:24 +053082const 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, 0x01,
95 0x40, 0x16, 0xec, 0x04, 0x40, 0x00, 0x00, 0x03, 0x00, 0x40, 0x00, 0x00, 0x0f, 0x03, 0xc5, 0x8b,
96 0xb8, 0x00, 0x00, 0x00, 0x01, 0x68, 0xca, 0x8c, 0xb2, 0x00, 0x00, 0x01, 0x06, 0x05, 0xff, 0xff,
97 0x70, 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7, 0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee,
98 0xef, 0x78, 0x32, 0x36, 0x34, 0x20, 0x2d, 0x20, 0x63, 0x6f, 0x72, 0x65, 0x20, 0x31, 0x34, 0x32,
99 0x20, 0x2d, 0x20, 0x48, 0x2e, 0x32, 0x36, 0x34, 0x2f, 0x4d, 0x50, 0x45, 0x47, 0x2d, 0x34, 0x20,
100 0x41, 0x56, 0x43, 0x20, 0x63, 0x6f, 0x64, 0x65, 0x63, 0x20, 0x2d, 0x20, 0x43, 0x6f, 0x70, 0x79,
101 0x6c, 0x65, 0x66, 0x74, 0x20, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x32, 0x30, 0x31, 0x34, 0x20, 0x2d,
102 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x76, 0x69, 0x64, 0x65,
103 0x6f, 0x6c, 0x61, 0x6e, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x78, 0x32, 0x36, 0x34, 0x2e, 0x68, 0x74,
104 0x6d, 0x6c, 0x6e, 0x45, 0x21, 0x82, 0x38, 0xf0, 0x9d, 0x7d, 0x96, 0xe6, 0x94, 0xae, 0xe2, 0x87,
105 0x8f, 0x04, 0x49, 0xe5, 0xf6, 0x8c, 0x8b, 0x9a, 0x10, 0x18, 0xba, 0x94, 0xe9, 0x22, 0x31, 0x04,
106 0x7e, 0x60, 0x5b, 0xc4, 0x24, 0x00, 0x90, 0x62, 0x0d, 0xdc, 0x85, 0x74, 0x75, 0x78, 0xd0, 0x14,
107 0x08, 0xcb, 0x02, 0x1d, 0x7d, 0x9d, 0x34, 0xe8, 0x81, 0xb9, 0xf7, 0x09, 0x28, 0x79, 0x29, 0x8d,
108 0xe3, 0x14, 0xed, 0x5f, 0xca, 0xaf, 0xf4, 0x1c, 0x49, 0x15, 0xe1, 0x80, 0x29, 0x61, 0x76, 0x80,
109 0x43, 0xf8, 0x58, 0x53, 0x40, 0xd7, 0x31, 0x6d, 0x61, 0x81, 0x41, 0xe9, 0x77, 0x9f, 0x9c, 0xe1,
110 0x6d, 0xf2, 0xee, 0xd9, 0xc8, 0x67, 0xd2, 0x5f, 0x48, 0x73, 0xe3, 0x5c, 0xcd, 0xa7, 0x45, 0x58,
111 0xbb, 0xdd, 0x28, 0x1d, 0x68, 0xfc, 0xb4, 0xc6, 0xf6, 0x92, 0xf6, 0x30, 0x03, 0xaa, 0xe4, 0x32,
112 0xf6, 0x34, 0x51, 0x4b, 0x0f, 0x8c, 0xf9, 0xac, 0x98, 0x22, 0xfb, 0x49, 0xc8, 0xbf, 0xca, 0x8c,
113 0x80, 0x86, 0x5d, 0xd7, 0xa4, 0x52, 0xb1, 0xd9, 0xa6, 0x04, 0x4e, 0xb3, 0x2d, 0x1f, 0xb8, 0x35,
114 0xcc, 0x45, 0x6d, 0x9c, 0x20, 0xa7, 0xa4, 0x34, 0x59, 0x72, 0xe3, 0xae, 0xba, 0x49, 0xde, 0xd1,
115 0xaa, 0xee, 0x3d, 0x77, 0xfc, 0x5d, 0xc6, 0x1f, 0x9d, 0xac, 0xc2, 0x15, 0x66, 0xb8, 0xe1, 0x54,
116 0x4e, 0x74, 0x93, 0xdb, 0x9a, 0x24, 0x15, 0x6e, 0x20, 0xa3, 0x67, 0x3e, 0x5a, 0x24, 0x41, 0x5e,
117 0xb0, 0xe6, 0x35, 0x87, 0x1b, 0xc8, 0x7a, 0xf9, 0x77, 0x65, 0xe0, 0x01, 0xf2, 0x4c, 0xe4, 0x2b,
118 0xa9, 0x64, 0x96, 0x96, 0x0b, 0x46, 0xca, 0xea, 0x79, 0x0e, 0x78, 0xa3, 0x5f, 0x43, 0xfc, 0x47,
119 0x6a, 0x12, 0xfa, 0xc4, 0x33, 0x0e, 0x88, 0x1c, 0x19, 0x3a, 0x00, 0xc3, 0x4e, 0xb5, 0xd8, 0xfa,
120 0x8e, 0xf1, 0xbc, 0x3d, 0xb2, 0x7e, 0x50, 0x8d, 0x67, 0xc3, 0x6b, 0xed, 0xe2, 0xea, 0xa6, 0x1f,
121 0x25, 0x24, 0x7c, 0x94, 0x74, 0x50, 0x49, 0xe3, 0xc6, 0x58, 0x2e, 0xfd, 0x28, 0xb4, 0xc6, 0x73,
122 0xb1, 0x53, 0x74, 0x27, 0x94, 0x5c, 0xdf, 0x69, 0xb7, 0xa1, 0xd7, 0xf5, 0xd3, 0x8a, 0x2c, 0x2d,
123 0xb4, 0x5e, 0x8a, 0x16, 0x14, 0x54, 0x64, 0x6e, 0x00, 0x6b, 0x11, 0x59, 0x8a, 0x63, 0x38, 0x80,
124 0x76, 0xc3, 0xd5, 0x59, 0xf7, 0x3f, 0xd2, 0xfa, 0xa5, 0xca, 0x82, 0xff, 0x4a, 0x62, 0xf0, 0xe3,
125 0x42, 0xf9, 0x3b, 0x38, 0x27, 0x8a, 0x89, 0xaa, 0x50, 0x55, 0x4b, 0x29, 0xf1, 0x46, 0x7c, 0x75,
126 0xef, 0x65, 0xaf, 0x9b, 0x0d, 0x6d, 0xda, 0x25, 0x94, 0x14, 0xc1, 0x1b, 0xf0, 0xc5, 0x4c, 0x24,
127 0x0e, 0x65,
128};
129
130const uint8_t kOutRefBinaryBuffer[] = {
131 0x00, 0x00, 0x00, 0x01, 0x09, 0xf0, 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0xc0, 0x1e, 0xdb, 0x01,
132 0x40, 0x16, 0xec, 0x04, 0x40, 0x00, 0x00, 0x03, 0x00, 0x40, 0x00, 0x00, 0x0f, 0x03, 0xc5, 0x8b,
133 0xb8, 0x00, 0x00, 0x00, 0x01, 0x68, 0xca, 0x8c, 0xb2, 0x00, 0x00, 0x01, 0x06, 0x05, 0xff, 0xff,
134 0x70, 0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7, 0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee,
135 0xef, 0x78, 0x32, 0x36, 0x34, 0x20, 0x2d, 0x20, 0x63, 0x6f, 0x72, 0x65, 0x20, 0x31, 0x34, 0x32,
136 0x20, 0x2d, 0x20, 0x48, 0x2e, 0x32, 0x36, 0x34, 0x2f, 0x4d, 0x50, 0x45, 0x47, 0x2d, 0x34, 0x20,
137 0x41, 0x56, 0x43, 0x20, 0x63, 0x6f, 0x64, 0x65, 0x63, 0x20, 0x2d, 0x20, 0x43, 0x6f, 0x70, 0x79,
138 0x6c, 0x65, 0x66, 0x74, 0x20, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x32, 0x30, 0x31, 0x34, 0x20, 0x2d,
139 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x76, 0x69, 0x64, 0x65,
140 0x6f, 0x6c, 0x61, 0x6e, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x78, 0x32, 0x36, 0x34, 0x2e, 0x68, 0x74,
141 0x6d, 0x6c, 0x20, 0x2d, 0x20, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x3a, 0x20, 0x63, 0x61,
142 0x62, 0x61, 0x63, 0x3d, 0x30, 0x20, 0x72, 0x65, 0x66, 0x3d, 0x32, 0x20, 0x64, 0x65, 0x62, 0x6c,
143 0x6f, 0x63, 0x6b, 0x3d, 0x31, 0x3a, 0x30, 0x3a, 0x30, 0x20, 0x61, 0x6e, 0x61, 0x6c, 0x79, 0x73,
144 0x65, 0x3d, 0x30, 0x78, 0x31, 0x3a, 0x30, 0x78, 0x31, 0x31, 0x31, 0x20, 0x6d, 0x65, 0x3d, 0x68,
145 0x65, 0x78, 0x20, 0x73, 0x75, 0x62, 0x6d, 0x65, 0x3d, 0x37, 0x20, 0x70, 0x73, 0x79, 0x3d, 0x31,
146 0x20, 0x70, 0x73, 0x79, 0x5f, 0x72, 0x64, 0x3d, 0x31, 0x2e, 0x30, 0x30, 0x3a, 0x30, 0x2e, 0x30,
147 0x30, 0x20, 0x6d, 0x69, 0x78, 0x65, 0x64, 0x5f, 0x72, 0x65, 0x66, 0x3d, 0x31, 0x20, 0x6d, 0x65,
148 0x5f, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x3d, 0x31, 0x36, 0x20, 0x63, 0x68, 0x72, 0x6f, 0x6d, 0x61,
149 0x5f, 0x6d, 0x65, 0x3d, 0x31, 0x20, 0x74, 0x72, 0x65, 0x6c, 0x6c, 0x69, 0x73, 0x3d, 0x31, 0x20,
150 0x38, 0x78, 0x38, 0x64, 0x63, 0x74, 0x3d, 0x30, 0x20, 0x63, 0x71, 0x6d, 0x3d, 0x30, 0x20, 0x64,
151 0x65, 0x61, 0x64, 0x7a, 0x6f, 0x6e, 0x65, 0x3d, 0x32, 0x31, 0x2c, 0x31, 0x31, 0x20, 0x66, 0x61,
152 0x73, 0x74, 0x5f, 0x70, 0x73, 0x6b, 0x69, 0x70, 0x3d, 0x31, 0x20, 0x63, 0x68, 0x72, 0x6f, 0x6d,
153 0x61, 0x5f, 0x71, 0x70, 0x5f, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x3d, 0x2d, 0x32, 0x20, 0x74,
154 0x68, 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d, 0x36, 0x30, 0x20, 0x6c, 0x6f, 0x6f, 0x6b, 0x61, 0x68,
155 0x65, 0x61, 0x64, 0x5f, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d, 0x35, 0x20, 0x73, 0x6c,
156 0x69, 0x63, 0x65, 0x64, 0x5f, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64, 0x73, 0x3d, 0x30, 0x20, 0x6e,
157 0x72, 0x3d, 0x30, 0x20, 0x64, 0x65, 0x63, 0x69, 0x6d, 0x61, 0x74, 0x65, 0x3d, 0x31, 0x20, 0x69,
158 0x6e, 0x74, 0x65, 0x72, 0x6c, 0x61, 0x63, 0x65, 0x64, 0x3d, 0x30, 0x20, 0x62, 0x6c, 0x75, 0x72,
159 0x61, 0x79, 0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x3d, 0x30, 0x20, 0x63, 0x6f, 0x6e, 0x73,
160 0x74, 0x72, 0x61, 0x69, 0x6e, 0x65, 0x64, 0x5f, 0x69, 0x6e, 0x74, 0x72, 0x61, 0x3d, 0x30, 0x20,
161 0x62, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x73, 0x3d, 0x30, 0x20, 0x77, 0x65, 0x69, 0x67, 0x68, 0x74,
162 0x70, 0x3d, 0x30, 0x20, 0x6b, 0x65, 0x79, 0x69, 0x6e, 0x74, 0x3d, 0x32, 0x35, 0x30, 0x20, 0x6b,
163 0x65, 0x79, 0x69, 0x6e, 0x74, 0x5f, 0x6d, 0x69, 0x6e, 0x3d, 0x32, 0x35, 0x20, 0x73, 0x63, 0x65,
164 0x6e, 0x65,
165};
166
167class MediaCasListener : public ICasListener {
168 public:
169 virtual Return<void> onEvent(int32_t event, int32_t arg,
170 const hidl_vec<uint8_t>& data) override {
171 android::Mutex::Autolock autoLock(mMsgLock);
172 mEvent = event;
173 mEventArg = arg;
174 mEventData = data;
175
176 mEventReceived = true;
177 mMsgCondition.signal();
178 return Void();
179 }
180
181 void testEventEcho(sp<ICas>& mediaCas, int32_t& event, int32_t& eventArg,
182 hidl_vec<uint8_t>& eventData);
183
184 private:
185 int32_t mEvent = -1;
186 int32_t mEventArg = -1;
187 bool mEventReceived = false;
188 hidl_vec<uint8_t> mEventData;
189 android::Mutex mMsgLock;
190 android::Condition mMsgCondition;
191};
192
193void MediaCasListener::testEventEcho(sp<ICas>& mediaCas, int32_t& event, int32_t& eventArg,
194 hidl_vec<uint8_t>& eventData) {
195 mEventReceived = false;
196 auto returnStatus = mediaCas->sendEvent(event, eventArg, eventData);
197 EXPECT_TRUE(returnStatus.isOk());
198 EXPECT_EQ(Status::OK, returnStatus);
199
200 android::Mutex::Autolock autoLock(mMsgLock);
201 while (!mEventReceived) {
202 if (-ETIMEDOUT == mMsgCondition.waitRelative(mMsgLock, WAIT_TIMEOUT)) {
203 EXPECT_TRUE(false) << "event not received within timeout";
204 return;
205 }
206 }
207
208 EXPECT_EQ(mEvent, event);
209 EXPECT_EQ(mEventArg, eventArg);
210 EXPECT_TRUE(mEventData == eventData);
211}
212
Dan Shi54a3b0b2019-10-15 15:22:45 -0700213class MediaCasHidlTest : public testing::TestWithParam<std::string> {
214 public:
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530215 virtual void SetUp() override {
Henry Fang8964b852021-01-08 16:09:08 -0800216 if (android::hardware::cas::V1_2::IMediaCasService::getService(GetParam()) == nullptr) {
217 ALOGI("Descrambler is need to be tested before cas@1.2.");
218 mIsTestDescrambler = true;
219 }
Dan Shi54a3b0b2019-10-15 15:22:45 -0700220 mService = IMediaCasService::getService(GetParam());
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530221 ASSERT_NE(mService, nullptr);
222 }
223
Dan Shi54a3b0b2019-10-15 15:22:45 -0700224 sp<IMediaCasService> mService = nullptr;
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530225
Dan Shi54a3b0b2019-10-15 15:22:45 -0700226 protected:
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530227 static void description(const std::string& description) {
228 RecordProperty("description", description);
229 }
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530230
231 sp<ICas> mMediaCas;
232 sp<IDescramblerBase> mDescramblerBase;
233 sp<MediaCasListener> mCasListener;
Henry Fang8964b852021-01-08 16:09:08 -0800234 bool mIsTestDescrambler = false;
Chong Zhang95cc5af2017-10-25 13:40:28 -0700235 typedef struct _OobInputTestParams {
236 const SubSample* subSamples;
237 uint32_t numSubSamples;
238 size_t imemSizeActual;
239 uint64_t imemOffset;
240 uint64_t imemSize;
241 uint64_t srcOffset;
242 uint64_t dstOffset;
243 } OobInputTestParams;
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530244
245 ::testing::AssertionResult createCasPlugin(int32_t caSystemId);
246 ::testing::AssertionResult openCasSession(std::vector<uint8_t>* sessionId);
Chong Zhang95cc5af2017-10-25 13:40:28 -0700247 ::testing::AssertionResult descrambleTestInputBuffer(
248 const sp<IDescrambler>& descrambler,
249 Status* descrambleStatus,
250 sp<IMemory>* hidlInMemory);
251 ::testing::AssertionResult descrambleTestOobInput(
252 const sp<IDescrambler>& descrambler,
253 Status* descrambleStatus,
254 const OobInputTestParams& params);
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530255};
256
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530257::testing::AssertionResult MediaCasHidlTest::createCasPlugin(int32_t caSystemId) {
258 auto status = mService->isSystemIdSupported(caSystemId);
259 if (!status.isOk() || !status) {
260 return ::testing::AssertionFailure();
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530261 }
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530262 status = mService->isDescramblerSupported(caSystemId);
263 if (!status.isOk() || !status) {
264 return ::testing::AssertionFailure();
265 }
266
267 mCasListener = new MediaCasListener();
268 auto pluginStatus = mService->createPlugin(caSystemId, mCasListener);
269 if (!pluginStatus.isOk()) {
270 return ::testing::AssertionFailure();
271 }
272 mMediaCas = pluginStatus;
273 if (mMediaCas == nullptr) {
274 return ::testing::AssertionFailure();
275 }
276
277 auto descramblerStatus = mService->createDescrambler(caSystemId);
278 if (!descramblerStatus.isOk()) {
Henry Fang8964b852021-01-08 16:09:08 -0800279 if (mIsTestDescrambler) {
280 return ::testing::AssertionFailure();
281 } else {
282 ALOGI("Skip Descrambler test since it's not required in cas@1.2.");
283 return ::testing::AssertionSuccess();
284 }
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530285 }
Henry Fang8964b852021-01-08 16:09:08 -0800286 mIsTestDescrambler = true;
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530287 mDescramblerBase = descramblerStatus;
288 return ::testing::AssertionResult(mDescramblerBase != nullptr);
289}
290
291::testing::AssertionResult MediaCasHidlTest::openCasSession(std::vector<uint8_t>* sessionId) {
292 Status sessionStatus;
293 auto returnVoid = mMediaCas->openSession([&](Status status, const hidl_vec<uint8_t>& id) {
294 sessionStatus = status;
295 *sessionId = id;
296 });
297 return ::testing::AssertionResult(returnVoid.isOk() && (Status::OK == sessionStatus));
298}
299
300::testing::AssertionResult MediaCasHidlTest::descrambleTestInputBuffer(
Chong Zhang704d5862017-10-10 13:03:18 -0700301 const sp<IDescrambler>& descrambler, Status* descrambleStatus, sp<IMemory>* inMemory) {
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530302 hidl_vec<SubSample> hidlSubSamples;
303 hidlSubSamples.setToExternal(const_cast<SubSample*>(kSubSamples),
304 (sizeof(kSubSamples) / sizeof(SubSample)), false /*own*/);
Chong Zhang704d5862017-10-10 13:03:18 -0700305
306 sp<MemoryDealer> dealer = new MemoryDealer(sizeof(kInBinaryBuffer), "vts-cas");
307 if (nullptr == dealer.get()) {
308 ALOGE("couldn't get MemoryDealer!");
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530309 return ::testing::AssertionFailure();
310 }
311
Chong Zhang704d5862017-10-10 13:03:18 -0700312 sp<IMemory> mem = dealer->allocate(sizeof(kInBinaryBuffer));
313 if (nullptr == mem.get()) {
314 ALOGE("couldn't allocate IMemory!");
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530315 return ::testing::AssertionFailure();
316 }
Chong Zhang704d5862017-10-10 13:03:18 -0700317 *inMemory = mem;
318
Chong Zhangd2eb83e2018-02-13 18:19:43 -0800319 // build HidlMemory from memory heap
Chong Zhang704d5862017-10-10 13:03:18 -0700320 ssize_t offset;
321 size_t size;
322 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
323 if (nullptr == heap.get()) {
324 ALOGE("couldn't get memory heap!");
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530325 return ::testing::AssertionFailure();
326 }
327
Ytai Ben-Tsvi11107872019-09-09 11:50:45 -0700328 uint8_t* ipBuffer = static_cast<uint8_t*>(static_cast<void*>(mem->unsecurePointer()));
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530329 memcpy(ipBuffer, kInBinaryBuffer, sizeof(kInBinaryBuffer));
330
Chong Zhangd2eb83e2018-02-13 18:19:43 -0800331 // hidlMemory is not to be passed out of scope!
332 sp<HidlMemory> hidlMemory = fromHeap(heap);
333
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530334 SharedBuffer srcBuffer = {
Chong Zhangd2eb83e2018-02-13 18:19:43 -0800335 .heapBase = *hidlMemory,
Chong Zhang704d5862017-10-10 13:03:18 -0700336 .offset = (uint64_t) offset,
337 .size = (uint64_t) size
338 };
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530339
340 DestinationBuffer dstBuffer;
341 dstBuffer.type = BufferType::SHARED_MEMORY;
342 dstBuffer.nonsecureMemory = srcBuffer;
343
344 uint32_t outBytes;
345 hidl_string detailedError;
346 auto returnVoid = descrambler->descramble(
347 ScramblingControl::EVENKEY /*2*/, hidlSubSamples, srcBuffer, 0, dstBuffer, 0,
348 [&](Status status, uint32_t bytesWritten, const hidl_string& detailedErr) {
349 *descrambleStatus = status;
350 outBytes = bytesWritten;
351 detailedError = detailedErr;
352 });
353 if (!returnVoid.isOk() || *descrambleStatus != Status::OK) {
354 ALOGI("descramble failed, trans=%s, status=%d, outBytes=%u, error=%s",
355 returnVoid.description().c_str(), *descrambleStatus, outBytes, detailedError.c_str());
356 }
357 return ::testing::AssertionResult(returnVoid.isOk());
358}
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530359
Chong Zhang95cc5af2017-10-25 13:40:28 -0700360::testing::AssertionResult MediaCasHidlTest::descrambleTestOobInput(
361 const sp<IDescrambler>& descrambler,
362 Status* descrambleStatus,
363 const OobInputTestParams& params) {
364 hidl_vec<SubSample> hidlSubSamples;
365 hidlSubSamples.setToExternal(
366 const_cast<SubSample*>(params.subSamples), params.numSubSamples, false /*own*/);
367
368 sp<MemoryDealer> dealer = new MemoryDealer(params.imemSizeActual, "vts-cas");
369 if (nullptr == dealer.get()) {
370 ALOGE("couldn't get MemoryDealer!");
371 return ::testing::AssertionFailure();
372 }
373
374 sp<IMemory> mem = dealer->allocate(params.imemSizeActual);
375 if (nullptr == mem.get()) {
376 ALOGE("couldn't allocate IMemory!");
377 return ::testing::AssertionFailure();
378 }
379
Chong Zhangd2eb83e2018-02-13 18:19:43 -0800380 // build HidlMemory from memory heap
Chong Zhang95cc5af2017-10-25 13:40:28 -0700381 ssize_t offset;
382 size_t size;
383 sp<IMemoryHeap> heap = mem->getMemory(&offset, &size);
384 if (nullptr == heap.get()) {
385 ALOGE("couldn't get memory heap!");
386 return ::testing::AssertionFailure();
387 }
388
Chong Zhangd2eb83e2018-02-13 18:19:43 -0800389 // hidlMemory is not to be passed out of scope!
390 sp<HidlMemory> hidlMemory = fromHeap(heap);
Chong Zhang95cc5af2017-10-25 13:40:28 -0700391
392 SharedBuffer srcBuffer = {
Chong Zhangd2eb83e2018-02-13 18:19:43 -0800393 .heapBase = *hidlMemory,
Chong Zhang95cc5af2017-10-25 13:40:28 -0700394 .offset = (uint64_t) offset + params.imemOffset,
395 .size = (uint64_t) params.imemSize,
396 };
397
398 DestinationBuffer dstBuffer;
399 dstBuffer.type = BufferType::SHARED_MEMORY;
400 dstBuffer.nonsecureMemory = srcBuffer;
401
402 uint32_t outBytes;
403 hidl_string detailedError;
404 auto returnVoid = descrambler->descramble(
405 ScramblingControl::EVENKEY /*2*/, hidlSubSamples,
406 srcBuffer,
407 params.srcOffset,
408 dstBuffer,
409 params.dstOffset,
410 [&](Status status, uint32_t bytesWritten, const hidl_string& detailedErr) {
411 *descrambleStatus = status;
412 outBytes = bytesWritten;
413 detailedError = detailedErr;
414 });
415 if (!returnVoid.isOk() || *descrambleStatus != Status::OK) {
416 ALOGI("descramble failed, trans=%s, status=%d, outBytes=%u, error=%s",
417 returnVoid.description().c_str(), *descrambleStatus, outBytes, detailedError.c_str());
418 }
419 return ::testing::AssertionResult(returnVoid.isOk());
420}
421
Dan Shi54a3b0b2019-10-15 15:22:45 -0700422TEST_P(MediaCasHidlTest, EnumeratePlugins) {
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530423 description("Test enumerate plugins");
424 hidl_vec<HidlCasPluginDescriptor> descriptors;
425 EXPECT_TRUE(mService
426 ->enumeratePlugins([&descriptors](
427 hidl_vec<HidlCasPluginDescriptor> const& desc) { descriptors = desc; })
428 .isOk());
429
430 if (descriptors.size() == 0) {
431 ALOGW("[ WARN ] enumeratePlugins list empty");
432 return;
433 }
434
435 sp<MediaCasListener> casListener = new MediaCasListener();
436 for (size_t i = 0; i < descriptors.size(); i++) {
437 int32_t caSystemId = descriptors[i].caSystemId;
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530438
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530439 ASSERT_TRUE(createCasPlugin(caSystemId));
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530440 }
441}
442
Dan Shi54a3b0b2019-10-15 15:22:45 -0700443TEST_P(MediaCasHidlTest, TestInvalidSystemIdFails) {
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530444 description("Test failure for invalid system ID");
445 sp<MediaCasListener> casListener = new MediaCasListener();
446
447 ASSERT_FALSE(mService->isSystemIdSupported(INVALID_SYSTEM_ID));
448 ASSERT_FALSE(mService->isDescramblerSupported(INVALID_SYSTEM_ID));
449
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530450 auto pluginStatus = mService->createPlugin(INVALID_SYSTEM_ID, casListener);
451 ASSERT_TRUE(pluginStatus.isOk());
452 sp<ICas> mediaCas = pluginStatus;
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530453 EXPECT_EQ(mediaCas, nullptr);
454
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530455 auto descramblerStatus = mService->createDescrambler(INVALID_SYSTEM_ID);
456 ASSERT_TRUE(descramblerStatus.isOk());
457 sp<IDescramblerBase> descramblerBase = descramblerStatus;
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530458 EXPECT_EQ(descramblerBase, nullptr);
459}
460
Dan Shi54a3b0b2019-10-15 15:22:45 -0700461TEST_P(MediaCasHidlTest, TestClearKeyPluginInstalled) {
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530462 description("Test if ClearKey plugin is installed");
463 hidl_vec<HidlCasPluginDescriptor> descriptors;
464 EXPECT_TRUE(mService
465 ->enumeratePlugins([&descriptors](
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530466 hidl_vec<HidlCasPluginDescriptor> const& desc) { descriptors = desc; })
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530467 .isOk());
468
469 if (descriptors.size() == 0) {
470 ALOGW("[ WARN ] enumeratePlugins list empty");
471 }
472
473 for (size_t i = 0; i < descriptors.size(); i++) {
474 int32_t caSystemId = descriptors[i].caSystemId;
475 if (CLEAR_KEY_SYSTEM_ID == caSystemId) {
476 return;
477 }
478 }
479
480 ASSERT_TRUE(false) << "ClearKey plugin not installed";
481}
482
Dan Shi54a3b0b2019-10-15 15:22:45 -0700483TEST_P(MediaCasHidlTest, TestClearKeyApis) {
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530484 description("Test that valid call sequences succeed");
485
486 ASSERT_TRUE(createCasPlugin(CLEAR_KEY_SYSTEM_ID));
487
488 auto returnStatus = mMediaCas->provision(hidl_string(PROVISION_STR));
489 EXPECT_TRUE(returnStatus.isOk());
490 EXPECT_EQ(Status::OK, returnStatus);
491
492 hidl_vec<uint8_t> hidlPvtData;
493 hidlPvtData.resize(256);
494 returnStatus = mMediaCas->setPrivateData(hidlPvtData);
495 EXPECT_TRUE(returnStatus.isOk());
496 EXPECT_EQ(Status::OK, returnStatus);
497
498 std::vector<uint8_t> sessionId;
499 ASSERT_TRUE(openCasSession(&sessionId));
500 returnStatus = mMediaCas->setSessionPrivateData(sessionId, hidlPvtData);
501 EXPECT_TRUE(returnStatus.isOk());
502 EXPECT_EQ(Status::OK, returnStatus);
503
504 std::vector<uint8_t> streamSessionId;
505 ASSERT_TRUE(openCasSession(&streamSessionId));
506 returnStatus = mMediaCas->setSessionPrivateData(streamSessionId, hidlPvtData);
507 EXPECT_TRUE(returnStatus.isOk());
508 EXPECT_EQ(Status::OK, returnStatus);
Henry Fang8964b852021-01-08 16:09:08 -0800509 if (mIsTestDescrambler) {
510 returnStatus = mDescramblerBase->setMediaCasSession(sessionId);
511 EXPECT_TRUE(returnStatus.isOk());
512 EXPECT_EQ(Status::OK, returnStatus);
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530513
Henry Fang8964b852021-01-08 16:09:08 -0800514 returnStatus = mDescramblerBase->setMediaCasSession(streamSessionId);
515 EXPECT_TRUE(returnStatus.isOk());
516 EXPECT_EQ(Status::OK, returnStatus);
517 }
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530518
519 hidl_vec<uint8_t> hidlNullPtr;
520 hidlNullPtr.setToExternal(static_cast<uint8_t*>(nullptr), 0);
521 returnStatus = mMediaCas->refreshEntitlements(3, hidlNullPtr);
522 EXPECT_TRUE(returnStatus.isOk());
523 EXPECT_EQ(Status::OK, returnStatus);
524
525 uint8_t refreshData[] = {0, 1, 2, 3};
526 hidl_vec<uint8_t> hidlRefreshData;
527 hidlRefreshData.setToExternal(static_cast<uint8_t*>(refreshData), sizeof(refreshData));
528 returnStatus = mMediaCas->refreshEntitlements(10, hidlRefreshData);
529 EXPECT_TRUE(returnStatus.isOk());
530 EXPECT_EQ(Status::OK, returnStatus);
531
532 int32_t eventID = 1;
533 int32_t eventArg = 2;
534 mCasListener->testEventEcho(mMediaCas, eventID, eventArg, hidlNullPtr);
535
536 eventID = 3;
537 eventArg = 4;
538 uint8_t eventData[] = {'e', 'v', 'e', 'n', 't', 'd', 'a', 't', 'a'};
539 hidl_vec<uint8_t> hidlEventData;
540 hidlEventData.setToExternal(static_cast<uint8_t*>(eventData), sizeof(eventData));
541 mCasListener->testEventEcho(mMediaCas, eventID, eventArg, hidlEventData);
542
543 uint8_t clearKeyEmmData[] = {'c', 'l', 'e', 'a', 'r', 'k', 'e', 'y', 'e', 'm', 'm'};
544 hidl_vec<uint8_t> hidlClearKeyEmm;
545 hidlClearKeyEmm.setToExternal(static_cast<uint8_t*>(clearKeyEmmData), sizeof(clearKeyEmmData));
546 returnStatus = mMediaCas->processEmm(hidlClearKeyEmm);
547 EXPECT_TRUE(returnStatus.isOk());
548 EXPECT_EQ(Status::OK, returnStatus);
549
550 hidl_vec<uint8_t> hidlEcm;
551 hidlEcm.setToExternal(const_cast<uint8_t*>(kEcmBinaryBuffer), sizeof(kEcmBinaryBuffer));
552 returnStatus = mMediaCas->processEcm(sessionId, hidlEcm);
553 EXPECT_TRUE(returnStatus.isOk());
554 EXPECT_EQ(Status::OK, returnStatus);
555 returnStatus = mMediaCas->processEcm(streamSessionId, hidlEcm);
556 EXPECT_TRUE(returnStatus.isOk());
557 EXPECT_EQ(Status::OK, returnStatus);
558
Henry Fang8964b852021-01-08 16:09:08 -0800559 if (mIsTestDescrambler) {
560 EXPECT_FALSE(mDescramblerBase->requiresSecureDecoderComponent("video/avc"));
Suresh Sivaraman6d176aa2017-10-04 14:37:36 +0530561
Henry Fang8964b852021-01-08 16:09:08 -0800562 sp<IDescrambler> descrambler;
563 descrambler = IDescrambler::castFrom(mDescramblerBase);
564 ASSERT_NE(descrambler, nullptr);
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530565
Henry Fang8964b852021-01-08 16:09:08 -0800566 Status descrambleStatus = Status::OK;
567 sp<IMemory> dataMemory;
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530568
Henry Fang8964b852021-01-08 16:09:08 -0800569 ASSERT_TRUE(descrambleTestInputBuffer(descrambler, &descrambleStatus, &dataMemory));
570 EXPECT_EQ(Status::OK, descrambleStatus);
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530571
Henry Fang8964b852021-01-08 16:09:08 -0800572 ASSERT_NE(nullptr, dataMemory.get());
573 uint8_t* opBuffer =
574 static_cast<uint8_t*>(static_cast<void*>(dataMemory->unsecurePointer()));
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530575
Henry Fang8964b852021-01-08 16:09:08 -0800576 int compareResult =
577 memcmp(static_cast<const void*>(opBuffer),
578 static_cast<const void*>(kOutRefBinaryBuffer), sizeof(kOutRefBinaryBuffer));
579 EXPECT_EQ(0, compareResult);
Suresh Sivaraman6d176aa2017-10-04 14:37:36 +0530580
Henry Fang8964b852021-01-08 16:09:08 -0800581 returnStatus = mDescramblerBase->release();
582 EXPECT_TRUE(returnStatus.isOk());
583 EXPECT_EQ(Status::OK, returnStatus);
584 }
Suresh Sivaraman6d176aa2017-10-04 14:37:36 +0530585
586 returnStatus = mMediaCas->release();
587 EXPECT_TRUE(returnStatus.isOk());
588 EXPECT_EQ(Status::OK, returnStatus);
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530589}
590
Dan Shi54a3b0b2019-10-15 15:22:45 -0700591TEST_P(MediaCasHidlTest, TestClearKeySessionClosedAfterRelease) {
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530592 description("Test that all sessions are closed after a MediaCas object is released");
593
594 ASSERT_TRUE(createCasPlugin(CLEAR_KEY_SYSTEM_ID));
595
596 auto returnStatus = mMediaCas->provision(hidl_string(PROVISION_STR));
597 EXPECT_TRUE(returnStatus.isOk());
598 EXPECT_EQ(Status::OK, returnStatus);
599
600 std::vector<uint8_t> sessionId;
601 ASSERT_TRUE(openCasSession(&sessionId));
602 std::vector<uint8_t> streamSessionId;
603 ASSERT_TRUE(openCasSession(&streamSessionId));
604
605 returnStatus = mMediaCas->release();
606 EXPECT_TRUE(returnStatus.isOk());
607 EXPECT_EQ(Status::OK, returnStatus);
608
Henry Fang8964b852021-01-08 16:09:08 -0800609 if (mIsTestDescrambler) {
610 returnStatus = mDescramblerBase->setMediaCasSession(sessionId);
611 EXPECT_TRUE(returnStatus.isOk());
612 EXPECT_EQ(Status::ERROR_CAS_SESSION_NOT_OPENED, returnStatus);
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530613
Henry Fang8964b852021-01-08 16:09:08 -0800614 returnStatus = mDescramblerBase->setMediaCasSession(streamSessionId);
615 EXPECT_TRUE(returnStatus.isOk());
616 EXPECT_EQ(Status::ERROR_CAS_SESSION_NOT_OPENED, returnStatus);
617 }
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530618}
619
Dan Shi54a3b0b2019-10-15 15:22:45 -0700620TEST_P(MediaCasHidlTest, TestClearKeyErrors) {
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530621 description("Test that invalid call sequences fail with expected error codes");
622
623 ASSERT_TRUE(createCasPlugin(CLEAR_KEY_SYSTEM_ID));
624
625 /*
626 * Test MediaCas error codes
627 */
628 // Provision should fail with an invalid asset string
629 auto returnStatus = mMediaCas->provision(hidl_string("invalid asset string"));
630 EXPECT_TRUE(returnStatus.isOk());
631 EXPECT_EQ(Status::ERROR_CAS_NO_LICENSE, returnStatus);
632
633 // Open a session, then close it so that it should become invalid
634 std::vector<uint8_t> invalidSessionId;
635 ASSERT_TRUE(openCasSession(&invalidSessionId));
636 returnStatus = mMediaCas->closeSession(invalidSessionId);
637 EXPECT_TRUE(returnStatus.isOk());
638 EXPECT_EQ(Status::OK, returnStatus);
639
640 // processEcm should fail with an invalid session id
641 hidl_vec<uint8_t> hidlEcm;
642 hidlEcm.setToExternal(const_cast<uint8_t*>(kEcmBinaryBuffer), sizeof(kEcmBinaryBuffer));
643 returnStatus = mMediaCas->processEcm(invalidSessionId, hidlEcm);
644 EXPECT_TRUE(returnStatus.isOk());
645 EXPECT_EQ(Status::ERROR_CAS_SESSION_NOT_OPENED, returnStatus);
646
647 std::vector<uint8_t> sessionId;
648 ASSERT_TRUE(openCasSession(&sessionId));
649
650 // processEcm should fail without provisioning
651 hidlEcm.setToExternal(const_cast<uint8_t*>(kEcmBinaryBuffer), sizeof(kEcmBinaryBuffer));
652 returnStatus = mMediaCas->processEcm(sessionId, hidlEcm);
653 EXPECT_TRUE(returnStatus.isOk());
654 EXPECT_EQ(Status::ERROR_CAS_NOT_PROVISIONED, returnStatus);
655
656 returnStatus = mMediaCas->provision(hidl_string(PROVISION_STR));
657 EXPECT_TRUE(returnStatus.isOk());
658 EXPECT_EQ(Status::OK, returnStatus);
659
660 // processEcm should fail with ecm buffer that's too short
661 hidlEcm.setToExternal(const_cast<uint8_t*>(kEcmBinaryBuffer), 8);
662 returnStatus = mMediaCas->processEcm(sessionId, hidlEcm);
663 EXPECT_TRUE(returnStatus.isOk());
664 EXPECT_EQ(Status::BAD_VALUE, returnStatus);
665
666 // processEcm should fail with ecm with bad descriptor count
667 uint8_t badDescriptor[sizeof(kEcmBinaryBuffer)];
668 memcpy(badDescriptor, kEcmBinaryBuffer, sizeof(kEcmBinaryBuffer));
669 badDescriptor[17] = 0x03; // change the descriptor count field to 3 (invalid)
670 hidlEcm.setToExternal(static_cast<uint8_t*>(badDescriptor), sizeof(badDescriptor));
671 returnStatus = mMediaCas->processEcm(sessionId, hidlEcm);
672 EXPECT_TRUE(returnStatus.isOk());
673 EXPECT_EQ(Status::ERROR_CAS_UNKNOWN, returnStatus);
674
Henry Fang8964b852021-01-08 16:09:08 -0800675 if (mIsTestDescrambler) {
676 /*
677 * Test MediaDescrambler error codes
678 */
679 // setMediaCasSession should fail with an invalid session id
680 returnStatus = mDescramblerBase->setMediaCasSession(invalidSessionId);
681 EXPECT_TRUE(returnStatus.isOk());
682 EXPECT_EQ(Status::ERROR_CAS_SESSION_NOT_OPENED, returnStatus);
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530683
Henry Fang8964b852021-01-08 16:09:08 -0800684 // descramble should fail without a valid session
685 sp<IDescrambler> descrambler;
686 descrambler = IDescrambler::castFrom(mDescramblerBase);
687 ASSERT_NE(descrambler, nullptr);
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530688
Henry Fang8964b852021-01-08 16:09:08 -0800689 Status descrambleStatus = Status::OK;
690 sp<IMemory> dataMemory;
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530691
Henry Fang8964b852021-01-08 16:09:08 -0800692 ASSERT_TRUE(descrambleTestInputBuffer(descrambler, &descrambleStatus, &dataMemory));
693 EXPECT_EQ(Status::ERROR_CAS_DECRYPT_UNIT_NOT_INITIALIZED, descrambleStatus);
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530694
Henry Fang8964b852021-01-08 16:09:08 -0800695 // Now set a valid session, should still fail because no valid ecm is processed
696 returnStatus = mDescramblerBase->setMediaCasSession(sessionId);
697 EXPECT_TRUE(returnStatus.isOk());
698 EXPECT_EQ(Status::OK, returnStatus);
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530699
Henry Fang8964b852021-01-08 16:09:08 -0800700 ASSERT_TRUE(descrambleTestInputBuffer(descrambler, &descrambleStatus, &dataMemory));
701 EXPECT_EQ(Status::ERROR_CAS_DECRYPT, descrambleStatus);
Suresh Sivaraman6d176aa2017-10-04 14:37:36 +0530702
Henry Fang8964b852021-01-08 16:09:08 -0800703 // Verify that requiresSecureDecoderComponent handles empty mime
704 EXPECT_FALSE(mDescramblerBase->requiresSecureDecoderComponent(""));
Suresh Sivaraman6d176aa2017-10-04 14:37:36 +0530705
Henry Fang8964b852021-01-08 16:09:08 -0800706 // Verify that requiresSecureDecoderComponent handles invalid mime
707 EXPECT_FALSE(mDescramblerBase->requiresSecureDecoderComponent("bad"));
708 }
Suresh Sivaraman0645aec2017-09-21 18:44:24 +0530709}
710
Dan Shi54a3b0b2019-10-15 15:22:45 -0700711TEST_P(MediaCasHidlTest, TestClearKeyOobFails) {
Chong Zhang95cc5af2017-10-25 13:40:28 -0700712 description("Test that oob descramble request fails with expected error");
713
714 ASSERT_TRUE(createCasPlugin(CLEAR_KEY_SYSTEM_ID));
715
716 auto returnStatus = mMediaCas->provision(hidl_string(PROVISION_STR));
717 EXPECT_TRUE(returnStatus.isOk());
718 EXPECT_EQ(Status::OK, returnStatus);
719
720 std::vector<uint8_t> sessionId;
721 ASSERT_TRUE(openCasSession(&sessionId));
722
Henry Fang8964b852021-01-08 16:09:08 -0800723 if (mIsTestDescrambler) {
724 returnStatus = mDescramblerBase->setMediaCasSession(sessionId);
725 EXPECT_TRUE(returnStatus.isOk());
726 EXPECT_EQ(Status::OK, returnStatus);
727 }
Chong Zhang95cc5af2017-10-25 13:40:28 -0700728
729 hidl_vec<uint8_t> hidlEcm;
730 hidlEcm.setToExternal(const_cast<uint8_t*>(kEcmBinaryBuffer), sizeof(kEcmBinaryBuffer));
731 returnStatus = mMediaCas->processEcm(sessionId, hidlEcm);
732 EXPECT_TRUE(returnStatus.isOk());
733 EXPECT_EQ(Status::OK, returnStatus);
734
Henry Fang8964b852021-01-08 16:09:08 -0800735 if (mIsTestDescrambler) {
736 sp<IDescrambler> descrambler = IDescrambler::castFrom(mDescramblerBase);
737 ASSERT_NE(nullptr, descrambler.get());
Chong Zhang95cc5af2017-10-25 13:40:28 -0700738
Henry Fang8964b852021-01-08 16:09:08 -0800739 Status descrambleStatus = Status::OK;
Chong Zhang95cc5af2017-10-25 13:40:28 -0700740
Henry Fang8964b852021-01-08 16:09:08 -0800741 // test invalid src buffer offset
742 ASSERT_TRUE(
743 descrambleTestOobInput(descrambler, &descrambleStatus,
744 {.subSamples = kSubSamples,
745 .numSubSamples = sizeof(kSubSamples) / sizeof(SubSample),
746 .imemSizeActual = sizeof(kInBinaryBuffer),
747 .imemOffset = 0xcccccc,
748 .imemSize = sizeof(kInBinaryBuffer),
749 .srcOffset = 0,
750 .dstOffset = 0}));
751 EXPECT_EQ(Status::BAD_VALUE, descrambleStatus);
Chong Zhang95cc5af2017-10-25 13:40:28 -0700752
Henry Fang8964b852021-01-08 16:09:08 -0800753 // test invalid src buffer size
754 ASSERT_TRUE(
755 descrambleTestOobInput(descrambler, &descrambleStatus,
756 {.subSamples = kSubSamples,
757 .numSubSamples = sizeof(kSubSamples) / sizeof(SubSample),
758 .imemSizeActual = sizeof(kInBinaryBuffer),
759 .imemOffset = 0,
760 .imemSize = 0xcccccc,
761 .srcOffset = 0,
762 .dstOffset = 0}));
763 EXPECT_EQ(Status::BAD_VALUE, descrambleStatus);
Chong Zhang95cc5af2017-10-25 13:40:28 -0700764
Henry Fang8964b852021-01-08 16:09:08 -0800765 // test invalid src buffer size
766 ASSERT_TRUE(
767 descrambleTestOobInput(descrambler, &descrambleStatus,
768 {.subSamples = kSubSamples,
769 .numSubSamples = sizeof(kSubSamples) / sizeof(SubSample),
770 .imemSizeActual = sizeof(kInBinaryBuffer),
771 .imemOffset = 1,
772 .imemSize = (uint64_t)-1,
773 .srcOffset = 0,
774 .dstOffset = 0}));
775 EXPECT_EQ(Status::BAD_VALUE, descrambleStatus);
Chong Zhang95cc5af2017-10-25 13:40:28 -0700776
Henry Fang8964b852021-01-08 16:09:08 -0800777 // test invalid srcOffset
778 ASSERT_TRUE(
779 descrambleTestOobInput(descrambler, &descrambleStatus,
780 {.subSamples = kSubSamples,
781 .numSubSamples = sizeof(kSubSamples) / sizeof(SubSample),
782 .imemSizeActual = sizeof(kInBinaryBuffer),
783 .imemOffset = 0,
784 .imemSize = sizeof(kInBinaryBuffer),
785 .srcOffset = 0xcccccc,
786 .dstOffset = 0}));
787 EXPECT_EQ(Status::BAD_VALUE, descrambleStatus);
Chong Zhang95cc5af2017-10-25 13:40:28 -0700788
Henry Fang8964b852021-01-08 16:09:08 -0800789 // test invalid dstOffset
790 ASSERT_TRUE(
791 descrambleTestOobInput(descrambler, &descrambleStatus,
792 {.subSamples = kSubSamples,
793 .numSubSamples = sizeof(kSubSamples) / sizeof(SubSample),
794 .imemSizeActual = sizeof(kInBinaryBuffer),
795 .imemOffset = 0,
796 .imemSize = sizeof(kInBinaryBuffer),
797 .srcOffset = 0,
798 .dstOffset = 0xcccccc}));
799 EXPECT_EQ(Status::BAD_VALUE, descrambleStatus);
Chong Zhang95cc5af2017-10-25 13:40:28 -0700800
Henry Fang8964b852021-01-08 16:09:08 -0800801 // test detection of oob subsample sizes
802 const SubSample invalidSubSamples1[] = {{162, 0}, {0, 184}, {0, 0xdddddd}};
Chong Zhang95cc5af2017-10-25 13:40:28 -0700803
Henry Fang8964b852021-01-08 16:09:08 -0800804 ASSERT_TRUE(descrambleTestOobInput(
805 descrambler, &descrambleStatus,
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 EXPECT_EQ(Status::BAD_VALUE, descrambleStatus);
Chong Zhang95cc5af2017-10-25 13:40:28 -0700814
Henry Fang8964b852021-01-08 16:09:08 -0800815 // test detection of overflowing subsample sizes
816 const SubSample invalidSubSamples2[] = {{162, 0}, {0, 184}, {2, (uint32_t)-1}};
Chong Zhang95cc5af2017-10-25 13:40:28 -0700817
Henry Fang8964b852021-01-08 16:09:08 -0800818 ASSERT_TRUE(descrambleTestOobInput(
819 descrambler, &descrambleStatus,
820 {.subSamples = invalidSubSamples2,
821 .numSubSamples = sizeof(invalidSubSamples2) / sizeof(SubSample),
822 .imemSizeActual = sizeof(kInBinaryBuffer),
823 .imemOffset = 0,
824 .imemSize = sizeof(kInBinaryBuffer),
825 .srcOffset = 0,
826 .dstOffset = 0}));
827 EXPECT_EQ(Status::BAD_VALUE, descrambleStatus);
Chong Zhang95cc5af2017-10-25 13:40:28 -0700828
Henry Fang8964b852021-01-08 16:09:08 -0800829 returnStatus = mDescramblerBase->release();
830 EXPECT_TRUE(returnStatus.isOk());
831 EXPECT_EQ(Status::OK, returnStatus);
832 }
Chong Zhang95cc5af2017-10-25 13:40:28 -0700833 returnStatus = mMediaCas->release();
834 EXPECT_TRUE(returnStatus.isOk());
835 EXPECT_EQ(Status::OK, returnStatus);
836}
837
Suresh Sivaramanf1fbb442017-09-15 11:51:15 +0530838} // anonymous namespace
839
Dan Shiff985a82020-07-29 09:45:03 -0700840GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MediaCasHidlTest);
Dan Shi54a3b0b2019-10-15 15:22:45 -0700841INSTANTIATE_TEST_SUITE_P(
842 PerInstance, MediaCasHidlTest,
843 testing::ValuesIn(android::hardware::getAllHalInstanceNames(IMediaCasService::descriptor)),
844 android::hardware::PrintInstanceNameToString);