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