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_t kKey1Data
[] = {
13 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
14 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03
16 const uint8_t kKey2Data
[] = {
17 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
18 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
20 const uint8_t kKey3Data
[] = {
21 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x05,
22 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x05,
24 const uint8_t kKey4Data
[] = {
25 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x06,
26 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x06,
28 const uint8_t kCommonSystemSystemId
[] = {
29 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02,
30 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B
33 class CencUtilsTest
: public testing::Test
{
36 : key1_(kKey1Data
, kKey1Data
+ arraysize(kKey1Data
)),
37 key2_(kKey2Data
, kKey2Data
+ arraysize(kKey2Data
)),
38 key3_(kKey3Data
, kKey3Data
+ arraysize(kKey3Data
)),
39 key4_(kKey4Data
, kKey4Data
+ arraysize(kKey4Data
)),
40 common_system_system_id_(
41 kCommonSystemSystemId
,
42 kCommonSystemSystemId
+ arraysize(kCommonSystemSystemId
)) {}
45 // Initialize the start of the 'pssh' box (up to key_count)
46 void InitializePSSHBox(std::vector
<uint8_t>* box
,
49 DCHECK(box
->size() == 0);
63 box
->push_back(version
);
68 // Add Common Encryption SystemID.
69 box
->insert(box
->end(), common_system_system_id_
.begin(),
70 common_system_system_id_
.end());
73 std::vector
<uint8_t> MakePSSHBox(uint8_t version
) {
74 std::vector
<uint8_t> box
;
75 uint8_t size
= (version
== 0) ? 32 : 36;
76 InitializePSSHBox(&box
, size
, version
);
78 // Add key_count (= 0).
84 // Add data_size (= 0).
92 std::vector
<uint8_t> MakePSSHBox(uint8_t version
,
93 const std::vector
<uint8_t>& key1
) {
95 DCHECK(key1
.size() == 16);
97 std::vector
<uint8_t> box
;
99 InitializePSSHBox(&box
, size
, version
);
101 // Add key_count (= 1).
108 for (size_t i
= 0; i
< key1
.size(); ++i
)
109 box
.push_back(key1
[i
]);
111 // Add data_size (= 0).
119 std::vector
<uint8_t> MakePSSHBox(uint8_t version
,
120 const std::vector
<uint8_t>& key1
,
121 const std::vector
<uint8_t>& key2
) {
123 DCHECK(key1
.size() == 16);
124 DCHECK(key2
.size() == 16);
126 std::vector
<uint8_t> box
;
128 InitializePSSHBox(&box
, size
, version
);
130 // Add key_count (= 2).
137 for (size_t i
= 0; i
< key1
.size(); ++i
)
138 box
.push_back(key1
[i
]);
141 for (size_t i
= 0; i
< key2
.size(); ++i
)
142 box
.push_back(key2
[i
]);
144 // Add data_size (= 0).
152 void AppendData(std::vector
<uint8_t>& pssh_box
,
153 const std::vector
<uint8_t>& data
) {
154 // This assumes that |pssh_box| has been created using the routines above,
155 // and simply appends the data to the end of it. It updates the box size
156 // and sets the data size.
157 DCHECK(data
.size() < 100);
158 pssh_box
[3] += data
.size();
160 pssh_box
.push_back(data
.size());
161 pssh_box
.insert(pssh_box
.end(), data
.begin(), data
.end());
164 const std::vector
<uint8_t>& Key1() { return key1_
; }
165 const std::vector
<uint8_t>& Key2() { return key2_
; }
166 const std::vector
<uint8_t>& Key3() { return key3_
; }
167 const std::vector
<uint8_t>& Key4() { return key4_
; }
168 const std::vector
<uint8_t>& CommonSystemSystemId() {
169 return common_system_system_id_
;
173 std::vector
<uint8_t> key1_
;
174 std::vector
<uint8_t> key2_
;
175 std::vector
<uint8_t> key3_
;
176 std::vector
<uint8_t> key4_
;
177 std::vector
<uint8_t> common_system_system_id_
;
180 TEST_F(CencUtilsTest
, EmptyPSSH
) {
182 EXPECT_TRUE(ValidatePsshInput(std::vector
<uint8_t>()));
183 EXPECT_FALSE(GetKeyIdsForCommonSystemId(std::vector
<uint8_t>(), &key_ids
));
186 TEST_F(CencUtilsTest
, PSSHVersion0
) {
187 std::vector
<uint8_t> box
= MakePSSHBox(0);
189 EXPECT_TRUE(ValidatePsshInput(box
));
190 EXPECT_FALSE(GetKeyIdsForCommonSystemId(box
, &key_ids
));
193 TEST_F(CencUtilsTest
, PSSHVersion1WithNoKeys
) {
194 std::vector
<uint8_t> box
= MakePSSHBox(1);
196 EXPECT_TRUE(ValidatePsshInput(box
));
197 EXPECT_FALSE(GetKeyIdsForCommonSystemId(box
, &key_ids
));
200 TEST_F(CencUtilsTest
, PSSHVersion1WithOneKey
) {
201 std::vector
<uint8_t> box
= MakePSSHBox(1, Key1());
203 EXPECT_TRUE(ValidatePsshInput(box
));
204 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box
, &key_ids
));
205 EXPECT_EQ(1u, key_ids
.size());
206 EXPECT_EQ(key_ids
[0], Key1());
209 TEST_F(CencUtilsTest
, PSSHVersion1WithTwoKeys
) {
210 std::vector
<uint8_t> box
= MakePSSHBox(1, Key1(), Key2());
212 EXPECT_TRUE(ValidatePsshInput(box
));
213 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box
, &key_ids
));
214 EXPECT_EQ(2u, key_ids
.size());
215 EXPECT_EQ(key_ids
[0], Key1());
216 EXPECT_EQ(key_ids
[1], Key2());
219 TEST_F(CencUtilsTest
, PSSHVersion0Plus1
) {
220 std::vector
<uint8_t> box0
= MakePSSHBox(0);
221 std::vector
<uint8_t> box1
= MakePSSHBox(1, Key1());
223 // Concatentate box1 onto end of box0.
224 box0
.insert(box0
.end(), box1
.begin(), box1
.end());
225 EXPECT_TRUE(ValidatePsshInput(box0
));
227 // No key IDs returned as only the first 'pssh' box is processed.
229 EXPECT_FALSE(GetKeyIdsForCommonSystemId(box0
, &key_ids
));
232 TEST_F(CencUtilsTest
, PSSHVersion1Plus0
) {
233 std::vector
<uint8_t> box0
= MakePSSHBox(0);
234 std::vector
<uint8_t> box1
= MakePSSHBox(1, Key1());
236 // Concatentate box0 onto end of box1.
237 box1
.insert(box1
.end(), box0
.begin(), box0
.end());
240 EXPECT_TRUE(ValidatePsshInput(box1
));
241 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box1
, &key_ids
));
242 EXPECT_EQ(1u, key_ids
.size());
243 EXPECT_EQ(key_ids
[0], Key1());
246 TEST_F(CencUtilsTest
, MultiplePSSHVersion1
) {
247 std::vector
<uint8_t> box
= MakePSSHBox(1, Key1(), Key2());
248 std::vector
<uint8_t> box1
= MakePSSHBox(1, Key3());
249 std::vector
<uint8_t> box2
= MakePSSHBox(1, Key4());
251 // Concatentate box1 and box2 onto end of box.
252 box
.insert(box
.end(), box1
.begin(), box1
.end());
253 box
.insert(box
.end(), box2
.begin(), box2
.end());
256 EXPECT_TRUE(ValidatePsshInput(box
));
257 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box
, &key_ids
));
258 EXPECT_EQ(2u, key_ids
.size());
259 EXPECT_EQ(key_ids
[0], Key1());
260 EXPECT_EQ(key_ids
[1], Key2());
263 TEST_F(CencUtilsTest
, PsshBoxSmallerThanSize
) {
264 std::vector
<uint8_t> box
= MakePSSHBox(1, Key1(), Key2());
267 // Tries every buffer size less than the indicated 'pssh' box size.
268 for (size_t i
= 1; i
< box
.size(); ++i
) {
269 // Truncate the box to be less than the specified box size.
270 std::vector
<uint8_t> truncated(&box
[0], &box
[0] + i
);
271 EXPECT_FALSE(ValidatePsshInput(truncated
)) << "Failed for length " << i
;
272 EXPECT_FALSE(GetKeyIdsForCommonSystemId(truncated
, &key_ids
));
276 TEST_F(CencUtilsTest
, PsshBoxLargerThanSize
) {
277 std::vector
<uint8_t> box
= MakePSSHBox(1, Key1(), Key2());
280 // Add 20 additional bytes to |box|.
281 size_t original_size
= box
.size();
282 for (size_t i
= 0; i
< 20; ++i
)
285 // Tries every size greater than |original_size|.
286 for (size_t i
= original_size
+ 1; i
< box
.size(); ++i
) {
287 // Modify size of box passed to be less than current size.
288 std::vector
<uint8_t> truncated(&box
[0], &box
[0] + i
);
289 EXPECT_FALSE(ValidatePsshInput(truncated
)) << "Failed for length " << i
;
290 EXPECT_FALSE(GetKeyIdsForCommonSystemId(truncated
, &key_ids
));
294 TEST_F(CencUtilsTest
, UnrecognizedSystemID
) {
295 std::vector
<uint8_t> box
= MakePSSHBox(1, Key1(), Key2());
297 // Modify the System ID.
301 EXPECT_FALSE(GetKeyIdsForCommonSystemId(box
, &key_ids
));
304 TEST_F(CencUtilsTest
, InvalidFlags
) {
305 std::vector
<uint8_t> box
= MakePSSHBox(1, Key1(), Key2());
311 EXPECT_FALSE(GetKeyIdsForCommonSystemId(box
, &key_ids
));
314 TEST_F(CencUtilsTest
, LongSize
) {
315 const uint8_t data
[] = {
316 0x00, 0x00, 0x00, 0x01, // size = 1
317 0x70, 0x73, 0x73, 0x68, // 'pssh'
318 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4c, // longsize
320 0x00, 0x00, 0x00, // flags
321 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // SystemID
322 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
323 0x00, 0x00, 0x00, 0x02, // key count
324 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03, // key1
325 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
326 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04, // key2
327 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
328 0x00, 0x00, 0x00, 0x00 // datasize
333 ValidatePsshInput(std::vector
<uint8_t>(data
, data
+ arraysize(data
))));
334 EXPECT_TRUE(GetKeyIdsForCommonSystemId(
335 std::vector
<uint8_t>(data
, data
+ arraysize(data
)), &key_ids
));
336 EXPECT_EQ(2u, key_ids
.size());
339 TEST_F(CencUtilsTest
, SizeIsZero
) {
340 const uint8_t data
[] = {
341 0x00, 0x00, 0x00, 0x00, // size = 0
342 0x70, 0x73, 0x73, 0x68, // 'pssh'
344 0x00, 0x00, 0x00, // flags
345 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // SystemID
346 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
347 0x00, 0x00, 0x00, 0x02, // key count
348 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03, // key1
349 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
350 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04, // key2
351 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
352 0x00, 0x00, 0x00, 0x00 // datasize
357 ValidatePsshInput(std::vector
<uint8_t>(data
, data
+ arraysize(data
))));
358 EXPECT_TRUE(GetKeyIdsForCommonSystemId(
359 std::vector
<uint8_t>(data
, data
+ arraysize(data
)), &key_ids
));
360 EXPECT_EQ(2u, key_ids
.size());
363 TEST_F(CencUtilsTest
, HugeSize
) {
364 const uint8_t data
[] = {
365 0x00, 0x00, 0x00, 0x01, // size = 1
366 0x70, 0x73, 0x73, 0x68, // 'pssh'
367 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // longsize = big
369 0x00, 0x00, 0x00, // flags
370 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // SystemID
371 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
372 0x00, 0x00, 0x00, 0x02, // key count
373 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03, // key1
374 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
375 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04, // key2
376 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
377 0x00, 0x00, 0x00, 0x00 // datasize
381 // These calls fail as the box size is huge (0xffffffffffffffff) and there
382 // is not enough bytes in |data|.
384 ValidatePsshInput(std::vector
<uint8_t>(data
, data
+ arraysize(data
))));
385 EXPECT_FALSE(GetKeyIdsForCommonSystemId(
386 std::vector
<uint8_t>(data
, data
+ arraysize(data
)), &key_ids
));
389 TEST_F(CencUtilsTest
, GetPsshData_Version0
) {
390 const uint8_t data_bytes
[] = {0x01, 0x02, 0x03, 0x04};
391 std::vector
<uint8_t> pssh_data
;
393 std::vector
<uint8_t> box
= MakePSSHBox(0);
394 EXPECT_TRUE(GetPsshData(box
, CommonSystemSystemId(), &pssh_data
));
395 EXPECT_EQ(0u, pssh_data
.size());
397 std::vector
<uint8_t> data(data_bytes
, data_bytes
+ arraysize(data_bytes
));
398 AppendData(box
, data
);
399 EXPECT_TRUE(GetPsshData(box
, CommonSystemSystemId(), &pssh_data
));
400 EXPECT_EQ(data
, pssh_data
);
403 TEST_F(CencUtilsTest
, GetPsshData_Version1NoKeys
) {
404 const uint8_t data_bytes
[] = {0x05, 0x06, 0x07, 0x08};
405 std::vector
<uint8_t> pssh_data
;
407 std::vector
<uint8_t> box
= MakePSSHBox(1);
408 EXPECT_TRUE(GetPsshData(box
, CommonSystemSystemId(), &pssh_data
));
409 EXPECT_EQ(0u, pssh_data
.size());
411 std::vector
<uint8_t> data(data_bytes
, data_bytes
+ arraysize(data_bytes
));
412 AppendData(box
, data
);
413 EXPECT_TRUE(GetPsshData(box
, CommonSystemSystemId(), &pssh_data
));
414 EXPECT_EQ(data
, pssh_data
);
417 TEST_F(CencUtilsTest
, GetPsshData_Version1WithKeys
) {
418 const uint8_t data_bytes
[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
419 std::vector
<uint8_t> pssh_data
;
421 std::vector
<uint8_t> box
= MakePSSHBox(1, Key1());
422 EXPECT_TRUE(GetPsshData(box
, CommonSystemSystemId(), &pssh_data
));
423 EXPECT_EQ(0u, pssh_data
.size());
425 std::vector
<uint8_t> data(data_bytes
, data_bytes
+ arraysize(data_bytes
));
426 AppendData(box
, data
);
427 EXPECT_TRUE(GetPsshData(box
, CommonSystemSystemId(), &pssh_data
));
428 EXPECT_EQ(data
, pssh_data
);
431 TEST_F(CencUtilsTest
, GetPsshData_Version2
) {
432 std::vector
<uint8_t> pssh_data
;
434 std::vector
<uint8_t> box
= MakePSSHBox(1, Key1());
435 EXPECT_TRUE(GetPsshData(box
, CommonSystemSystemId(), &pssh_data
));
437 // Change the version manually, since we don't know what v2 will contain.
439 EXPECT_FALSE(GetPsshData(box
, CommonSystemSystemId(), &pssh_data
));
442 TEST_F(CencUtilsTest
, GetPsshData_Version2ThenVersion1
) {
443 std::vector
<uint8_t> pssh_data
;
445 std::vector
<uint8_t> box_v1
= MakePSSHBox(1, Key1());
446 std::vector
<uint8_t> box_v2
= MakePSSHBox(2, Key2(), Key3());
448 // Concatentate the boxes together (v2 first).
449 std::vector
<uint8_t> boxes
;
450 boxes
.insert(boxes
.end(), box_v2
.begin(), box_v2
.end());
451 boxes
.insert(boxes
.end(), box_v1
.begin(), box_v1
.end());
452 EXPECT_TRUE(GetPsshData(boxes
, CommonSystemSystemId(), &pssh_data
));
454 // GetKeyIdsForCommonSystemId() should return the single key from the v1
457 EXPECT_TRUE(GetKeyIdsForCommonSystemId(boxes
, &key_ids
));
458 EXPECT_EQ(1u, key_ids
.size());
459 EXPECT_EQ(key_ids
[0], Key1());
462 TEST_F(CencUtilsTest
, GetPsshData_Version1ThenVersion2
) {
463 std::vector
<uint8_t> pssh_data
;
465 std::vector
<uint8_t> box_v1
= MakePSSHBox(1, Key3());
466 std::vector
<uint8_t> box_v2
= MakePSSHBox(2, Key4());
468 // Concatentate the boxes together (v1 first).
469 std::vector
<uint8_t> boxes
;
470 boxes
.insert(boxes
.end(), box_v1
.begin(), box_v1
.end());
471 boxes
.insert(boxes
.end(), box_v2
.begin(), box_v2
.end());
472 EXPECT_TRUE(GetPsshData(boxes
, CommonSystemSystemId(), &pssh_data
));
474 // GetKeyIdsForCommonSystemId() should return the single key from the v1
477 EXPECT_TRUE(GetKeyIdsForCommonSystemId(boxes
, &key_ids
));
478 EXPECT_EQ(1u, key_ids
.size());
479 EXPECT_EQ(key_ids
[0], Key3());
482 TEST_F(CencUtilsTest
, GetPsshData_DifferentSystemID
) {
483 std::vector
<uint8_t> unknown_system_id(kKey1Data
,
484 kKey1Data
+ arraysize(kKey1Data
));
485 std::vector
<uint8_t> pssh_data
;
487 std::vector
<uint8_t> box
= MakePSSHBox(1, Key1());
488 EXPECT_TRUE(GetPsshData(box
, CommonSystemSystemId(), &pssh_data
));
489 EXPECT_FALSE(GetPsshData(box
, unknown_system_id
, &pssh_data
));
492 TEST_F(CencUtilsTest
, GetPsshData_MissingData
) {
493 const uint8_t data_bytes
[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
494 std::vector
<uint8_t> pssh_data
;
496 std::vector
<uint8_t> box
= MakePSSHBox(1, Key1());
497 std::vector
<uint8_t> data(data_bytes
, data_bytes
+ arraysize(data_bytes
));
498 AppendData(box
, data
);
499 EXPECT_TRUE(GetPsshData(box
, CommonSystemSystemId(), &pssh_data
));
501 // Remove some data from the end, so now the size is incorrect.
504 EXPECT_FALSE(GetPsshData(box
, CommonSystemSystemId(), &pssh_data
));
507 TEST_F(CencUtilsTest
, GetPsshData_MultiplePssh
) {
508 const uint8_t data1_bytes
[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
509 const uint8_t data2_bytes
[] = {0xa1, 0xa2, 0xa3, 0xa4};
510 std::vector
<uint8_t> pssh_data
;
512 std::vector
<uint8_t> box1
= MakePSSHBox(1, Key1());
513 std::vector
<uint8_t> data1(data1_bytes
, data1_bytes
+ arraysize(data1_bytes
));
514 AppendData(box1
, data1
);
516 std::vector
<uint8_t> box2
= MakePSSHBox(0);
517 std::vector
<uint8_t> data2(data2_bytes
, data2_bytes
+ arraysize(data2_bytes
));
518 AppendData(box2
, data2
);
520 box1
.insert(box1
.end(), box2
.begin(), box2
.end());
521 EXPECT_TRUE(GetPsshData(box1
, CommonSystemSystemId(), &pssh_data
));
522 EXPECT_EQ(data1
, pssh_data
);
523 EXPECT_NE(data2
, pssh_data
);
526 TEST_F(CencUtilsTest
, NonPsshData
) {
527 // Create a non-'pssh' box.
528 const uint8_t data
[] = {
529 0x00, 0x00, 0x00, 0x08, // size = 8
532 std::vector
<uint8_t> non_pssh_box(data
, data
+ arraysize(data
));
533 EXPECT_FALSE(ValidatePsshInput(non_pssh_box
));
535 // Make a valid 'pssh' box.
536 std::vector
<uint8_t> pssh_box
= MakePSSHBox(1, Key1());
537 EXPECT_TRUE(ValidatePsshInput(pssh_box
));
539 // Concatentate the boxes together (|pssh_box| first).
540 std::vector
<uint8_t> boxes
;
541 boxes
.insert(boxes
.end(), pssh_box
.begin(), pssh_box
.end());
542 boxes
.insert(boxes
.end(), non_pssh_box
.begin(), non_pssh_box
.end());
543 EXPECT_FALSE(ValidatePsshInput(boxes
));
545 // Repeat with |non_pssh_box| first.
547 boxes
.insert(boxes
.end(), non_pssh_box
.begin(), non_pssh_box
.end());
548 boxes
.insert(boxes
.end(), pssh_box
.begin(), pssh_box
.end());
549 EXPECT_FALSE(ValidatePsshInput(boxes
));