1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/cdm/cenc_utils.h"
7 #include "base/logging.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 const uint8 kKey1Data
[] = {
13 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
14 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03
16 const uint8 kKey2Data
[] = {
17 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
18 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
20 const uint8 kKey3Data
[] = {
21 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x05,
22 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x05,
24 const uint8 kKey4Data
[] = {
25 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x06,
26 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x06,
29 class CencUtilsTest
: public testing::Test
{
32 : key1_(kKey1Data
, kKey1Data
+ arraysize(kKey1Data
)),
33 key2_(kKey2Data
, kKey2Data
+ arraysize(kKey2Data
)),
34 key3_(kKey3Data
, kKey3Data
+ arraysize(kKey3Data
)),
35 key4_(kKey4Data
, kKey4Data
+ arraysize(kKey4Data
)) {}
38 // Initialize the start of the 'pssh' box (up to key_count)
39 void InitializePSSHBox(std::vector
<uint8
>* box
, uint8 size
, uint8 version
) {
40 DCHECK(box
->size() == 0);
54 box
->push_back(version
);
59 // Add Clear Key SystemID.
78 std::vector
<uint8
> MakePSSHBox(uint8 version
) {
79 std::vector
<uint8
> box
;
80 uint8 size
= (version
== 0) ? 32 : 36;
81 InitializePSSHBox(&box
, size
, version
);
83 // Add key_count (= 0).
89 // Add data_size (= 0).
97 std::vector
<uint8
> MakePSSHBox(uint8 version
,
98 const std::vector
<uint8
>& key1
) {
100 DCHECK(key1
.size() == 16);
102 std::vector
<uint8
> box
;
104 InitializePSSHBox(&box
, size
, version
);
106 // Add key_count (= 1).
113 for (int i
= 0; i
< 16; ++i
)
114 box
.push_back(key1
[i
]);
116 // Add data_size (= 0).
124 std::vector
<uint8
> MakePSSHBox(uint8 version
,
125 const std::vector
<uint8
>& key1
,
126 const std::vector
<uint8
>& key2
) {
128 DCHECK(key1
.size() == 16);
129 DCHECK(key2
.size() == 16);
131 std::vector
<uint8
> box
;
133 InitializePSSHBox(&box
, size
, version
);
135 // Add key_count (= 2).
142 for (int i
= 0; i
< 16; ++i
)
143 box
.push_back(key1
[i
]);
146 for (int i
= 0; i
< 16; ++i
)
147 box
.push_back(key2
[i
]);
149 // Add data_size (= 0).
157 const std::vector
<uint8
>& Key1() { return key1_
; }
158 const std::vector
<uint8
>& Key2() { return key2_
; }
159 const std::vector
<uint8
>& Key3() { return key3_
; }
160 const std::vector
<uint8
>& Key4() { return key4_
; }
163 std::vector
<uint8
> key1_
;
164 std::vector
<uint8
> key2_
;
165 std::vector
<uint8
> key3_
;
166 std::vector
<uint8
> key4_
;
169 TEST_F(CencUtilsTest
, EmptyPSSH
) {
170 std::vector
<std::vector
<uint8
>> key_ids
;
171 EXPECT_TRUE(GetKeyIdsForCommonSystemId(nullptr, 0, &key_ids
));
172 EXPECT_EQ(0u, key_ids
.size());
175 TEST_F(CencUtilsTest
, PSSHVersion0
) {
176 std::vector
<uint8
> box
= MakePSSHBox(0);
177 std::vector
<std::vector
<uint8
>> key_ids
;
178 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box
[0], box
.size(), &key_ids
));
179 EXPECT_EQ(0u, key_ids
.size());
182 TEST_F(CencUtilsTest
, PSSHVersion1WithNoKeys
) {
183 std::vector
<uint8
> box
= MakePSSHBox(1);
184 std::vector
<std::vector
<uint8
>> key_ids
;
185 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box
[0], box
.size(), &key_ids
));
186 EXPECT_EQ(0u, key_ids
.size());
189 TEST_F(CencUtilsTest
, PSSHVersion1WithOneKey
) {
190 std::vector
<uint8
> box
= MakePSSHBox(1, Key1());
191 std::vector
<std::vector
<uint8
>> key_ids
;
192 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box
[0], box
.size(), &key_ids
));
193 EXPECT_EQ(1u, key_ids
.size());
194 EXPECT_EQ(key_ids
[0], Key1());
197 TEST_F(CencUtilsTest
, PSSHVersion1WithTwoKeys
) {
198 std::vector
<uint8
> box
= MakePSSHBox(1, Key1(), Key2());
199 std::vector
<std::vector
<uint8
>> key_ids
;
200 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box
[0], box
.size(), &key_ids
));
201 EXPECT_EQ(2u, key_ids
.size());
202 EXPECT_EQ(key_ids
[0], Key1());
203 EXPECT_EQ(key_ids
[1], Key2());
206 TEST_F(CencUtilsTest
, PSSHVersion0Plus1
) {
207 std::vector
<uint8
> box0
= MakePSSHBox(0);
208 std::vector
<uint8
> box1
= MakePSSHBox(1, Key1());
210 // Concatentate box1 into box0.
211 for (const auto& value
: box1
)
212 box0
.push_back(value
);
214 std::vector
<std::vector
<uint8
>> key_ids
;
215 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box0
[0], box0
.size(), &key_ids
));
216 EXPECT_EQ(1u, key_ids
.size());
217 EXPECT_EQ(key_ids
[0], Key1());
220 TEST_F(CencUtilsTest
, PSSHVersion1Plus0
) {
221 std::vector
<uint8
> box0
= MakePSSHBox(0);
222 std::vector
<uint8
> box1
= MakePSSHBox(1, Key1());
224 // Concatentate box0 into box1.
225 for (const auto& value
: box0
)
226 box1
.push_back(value
);
228 std::vector
<std::vector
<uint8
>> key_ids
;
229 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box1
[0], box1
.size(), &key_ids
));
230 EXPECT_EQ(1u, key_ids
.size());
231 EXPECT_EQ(key_ids
[0], Key1());
234 TEST_F(CencUtilsTest
, MultiplePSSHVersion1
) {
235 std::vector
<uint8
> box
= MakePSSHBox(1, Key1(), Key2());
236 std::vector
<uint8
> box1
= MakePSSHBox(1, Key3());
237 std::vector
<uint8
> box2
= MakePSSHBox(1, Key4());
239 // Concatentate box1 into box.
240 for (const auto& value
: box1
)
241 box
.push_back(value
);
242 // Concatentate box2 into box.
243 for (const auto& value
: box2
)
244 box
.push_back(value
);
246 std::vector
<std::vector
<uint8
>> key_ids
;
247 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box
[0], box
.size(), &key_ids
));
248 EXPECT_EQ(4u, key_ids
.size());
249 EXPECT_EQ(key_ids
[0], Key1());
250 EXPECT_EQ(key_ids
[1], Key2());
251 EXPECT_EQ(key_ids
[2], Key3());
252 EXPECT_EQ(key_ids
[3], Key4());
255 TEST_F(CencUtilsTest
, InvalidPSSH
) {
256 std::vector
<uint8
> box
= MakePSSHBox(1, Key1(), Key2());
257 std::vector
<std::vector
<uint8
>> key_ids
;
258 for (uint32 i
= 1; i
< box
.size(); ++i
) {
259 // Modify size of data passed to be less than real size.
260 EXPECT_FALSE(GetKeyIdsForCommonSystemId(&box
[0], i
, &key_ids
));
261 // Modify starting point.
262 EXPECT_FALSE(GetKeyIdsForCommonSystemId(&box
[i
], box
.size() - i
, &key_ids
));
266 TEST_F(CencUtilsTest
, InvalidSystemID
) {
267 std::vector
<uint8
> box
= MakePSSHBox(1, Key1(), Key2());
269 // Modify the System ID.
272 std::vector
<std::vector
<uint8
>> key_ids
;
273 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box
[0], box
.size(), &key_ids
));
274 EXPECT_EQ(0u, key_ids
.size());
277 TEST_F(CencUtilsTest
, InvalidFlags
) {
278 std::vector
<uint8
> box
= MakePSSHBox(1, Key1(), Key2());
283 std::vector
<std::vector
<uint8
>> key_ids
;
284 // TODO(jrummell): This should fail as the 'pssh' box is skipped.
285 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box
[0], box
.size(), &key_ids
));
286 EXPECT_EQ(0u, key_ids
.size());
289 TEST_F(CencUtilsTest
, LongSize
) {
290 const uint8 data
[] = {
291 0x00, 0x00, 0x00, 0x01, // size = 1
292 0x70, 0x73, 0x73, 0x68, // 'pssh'
293 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4c, // longsize
295 0x00, 0x00, 0x00, // flags
296 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // SystemID
297 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
298 0x00, 0x00, 0x00, 0x02, // key count
299 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03, // key1
300 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
301 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04, // key2
302 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
303 0x00, 0x00, 0x00, 0x00 // datasize
306 std::vector
<std::vector
<uint8
>> key_ids
;
307 EXPECT_TRUE(GetKeyIdsForCommonSystemId(data
, arraysize(data
), &key_ids
));
308 EXPECT_EQ(2u, key_ids
.size());
311 TEST_F(CencUtilsTest
, NoSize
) {
312 const uint8 data
[] = {
313 0x00, 0x00, 0x00, 0x00, // size = 0
314 0x70, 0x73, 0x73, 0x68, // 'pssh'
316 0x00, 0x00, 0x00, // flags
317 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // SystemID
318 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
319 0x00, 0x00, 0x00, 0x02, // key count
320 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03, // key1
321 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
322 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04, // key2
323 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
324 0x00, 0x00, 0x00, 0x00 // datasize
327 std::vector
<std::vector
<uint8
>> key_ids
;
328 EXPECT_TRUE(GetKeyIdsForCommonSystemId(data
, arraysize(data
), &key_ids
));
329 EXPECT_EQ(2u, key_ids
.size());
332 TEST_F(CencUtilsTest
, HugeSize
) {
333 const uint8 data
[] = {
334 0x00, 0x00, 0x00, 0x01, // size = 1
335 0x70, 0x73, 0x73, 0x68, // 'pssh'
336 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // longsize = big
338 0x00, 0x00, 0x00, // flags
339 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // SystemID
340 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
341 0x00, 0x00, 0x00, 0x02, // key count
342 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03, // key1
343 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
344 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04, // key2
345 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
346 0x00, 0x00, 0x00, 0x00 // datasize
349 std::vector
<std::vector
<uint8
>> key_ids
;
350 EXPECT_FALSE(GetKeyIdsForCommonSystemId(data
, arraysize(data
), &key_ids
));