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(ValidatePsshInput(nullptr, 0));
172 EXPECT_TRUE(GetKeyIdsForCommonSystemId(nullptr, 0, &key_ids
));
173 EXPECT_EQ(0u, key_ids
.size());
176 TEST_F(CencUtilsTest
, PSSHVersion0
) {
177 std::vector
<uint8
> box
= MakePSSHBox(0);
178 std::vector
<std::vector
<uint8
>> key_ids
;
179 EXPECT_TRUE(ValidatePsshInput(&box
[0], box
.size()));
180 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box
[0], box
.size(), &key_ids
));
181 EXPECT_EQ(0u, key_ids
.size());
184 TEST_F(CencUtilsTest
, PSSHVersion1WithNoKeys
) {
185 std::vector
<uint8
> box
= MakePSSHBox(1);
186 std::vector
<std::vector
<uint8
>> key_ids
;
187 EXPECT_TRUE(ValidatePsshInput(&box
[0], box
.size()));
188 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box
[0], box
.size(), &key_ids
));
189 EXPECT_EQ(0u, key_ids
.size());
192 TEST_F(CencUtilsTest
, PSSHVersion1WithOneKey
) {
193 std::vector
<uint8
> box
= MakePSSHBox(1, Key1());
194 std::vector
<std::vector
<uint8
>> key_ids
;
195 EXPECT_TRUE(ValidatePsshInput(&box
[0], box
.size()));
196 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box
[0], box
.size(), &key_ids
));
197 EXPECT_EQ(1u, key_ids
.size());
198 EXPECT_EQ(key_ids
[0], Key1());
201 TEST_F(CencUtilsTest
, PSSHVersion1WithTwoKeys
) {
202 std::vector
<uint8
> box
= MakePSSHBox(1, Key1(), Key2());
203 std::vector
<std::vector
<uint8
>> key_ids
;
204 EXPECT_TRUE(ValidatePsshInput(&box
[0], box
.size()));
205 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box
[0], box
.size(), &key_ids
));
206 EXPECT_EQ(2u, key_ids
.size());
207 EXPECT_EQ(key_ids
[0], Key1());
208 EXPECT_EQ(key_ids
[1], Key2());
211 TEST_F(CencUtilsTest
, PSSHVersion0Plus1
) {
212 std::vector
<uint8
> box0
= MakePSSHBox(0);
213 std::vector
<uint8
> box1
= MakePSSHBox(1, Key1());
215 // Concatentate box1 into box0.
216 for (const auto& value
: box1
)
217 box0
.push_back(value
);
219 std::vector
<std::vector
<uint8
>> key_ids
;
220 EXPECT_TRUE(ValidatePsshInput(&box0
[0], box0
.size()));
221 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box0
[0], box0
.size(), &key_ids
));
222 EXPECT_EQ(1u, key_ids
.size());
223 EXPECT_EQ(key_ids
[0], Key1());
226 TEST_F(CencUtilsTest
, PSSHVersion1Plus0
) {
227 std::vector
<uint8
> box0
= MakePSSHBox(0);
228 std::vector
<uint8
> box1
= MakePSSHBox(1, Key1());
230 // Concatentate box0 into box1.
231 for (const auto& value
: box0
)
232 box1
.push_back(value
);
234 std::vector
<std::vector
<uint8
>> key_ids
;
235 EXPECT_TRUE(ValidatePsshInput(&box1
[0], box1
.size()));
236 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box1
[0], box1
.size(), &key_ids
));
237 EXPECT_EQ(1u, key_ids
.size());
238 EXPECT_EQ(key_ids
[0], Key1());
241 TEST_F(CencUtilsTest
, MultiplePSSHVersion1
) {
242 std::vector
<uint8
> box
= MakePSSHBox(1, Key1(), Key2());
243 std::vector
<uint8
> box1
= MakePSSHBox(1, Key3());
244 std::vector
<uint8
> box2
= MakePSSHBox(1, Key4());
246 // Concatentate box1 into box.
247 for (const auto& value
: box1
)
248 box
.push_back(value
);
249 // Concatentate box2 into box.
250 for (const auto& value
: box2
)
251 box
.push_back(value
);
253 std::vector
<std::vector
<uint8
>> key_ids
;
254 EXPECT_TRUE(ValidatePsshInput(&box
[0], box
.size()));
255 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box
[0], box
.size(), &key_ids
));
256 EXPECT_EQ(4u, key_ids
.size());
257 EXPECT_EQ(key_ids
[0], Key1());
258 EXPECT_EQ(key_ids
[1], Key2());
259 EXPECT_EQ(key_ids
[2], Key3());
260 EXPECT_EQ(key_ids
[3], Key4());
263 TEST_F(CencUtilsTest
, InvalidPSSH
) {
264 std::vector
<uint8
> box
= MakePSSHBox(1, Key1(), Key2());
265 std::vector
<std::vector
<uint8
>> key_ids
;
266 for (uint32 i
= 1; i
< box
.size(); ++i
) {
267 // Modify size of data passed to be less than real size.
268 EXPECT_FALSE(ValidatePsshInput(&box
[0], i
));
269 EXPECT_FALSE(GetKeyIdsForCommonSystemId(&box
[0], i
, &key_ids
));
270 // Modify starting point.
271 EXPECT_FALSE(ValidatePsshInput(&box
[i
], box
.size() - i
));
272 EXPECT_FALSE(GetKeyIdsForCommonSystemId(&box
[i
], box
.size() - i
, &key_ids
));
276 TEST_F(CencUtilsTest
, InvalidSystemID
) {
277 std::vector
<uint8
> box
= MakePSSHBox(1, Key1(), Key2());
279 // Modify the System ID.
282 std::vector
<std::vector
<uint8
>> key_ids
;
283 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box
[0], box
.size(), &key_ids
));
284 EXPECT_EQ(0u, key_ids
.size());
287 TEST_F(CencUtilsTest
, InvalidFlags
) {
288 std::vector
<uint8
> box
= MakePSSHBox(1, Key1(), Key2());
293 std::vector
<std::vector
<uint8
>> key_ids
;
294 // TODO(jrummell): This should fail as the 'pssh' box is skipped.
295 EXPECT_TRUE(GetKeyIdsForCommonSystemId(&box
[0], box
.size(), &key_ids
));
296 EXPECT_EQ(0u, key_ids
.size());
299 TEST_F(CencUtilsTest
, LongSize
) {
300 const uint8 data
[] = {
301 0x00, 0x00, 0x00, 0x01, // size = 1
302 0x70, 0x73, 0x73, 0x68, // 'pssh'
303 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4c, // longsize
305 0x00, 0x00, 0x00, // flags
306 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // SystemID
307 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
308 0x00, 0x00, 0x00, 0x02, // key count
309 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03, // key1
310 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
311 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04, // key2
312 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
313 0x00, 0x00, 0x00, 0x00 // datasize
316 std::vector
<std::vector
<uint8
>> key_ids
;
317 EXPECT_TRUE(ValidatePsshInput(data
, arraysize(data
)));
318 EXPECT_TRUE(GetKeyIdsForCommonSystemId(data
, arraysize(data
), &key_ids
));
319 EXPECT_EQ(2u, key_ids
.size());
322 TEST_F(CencUtilsTest
, NoSize
) {
323 const uint8 data
[] = {
324 0x00, 0x00, 0x00, 0x00, // size = 0
325 0x70, 0x73, 0x73, 0x68, // 'pssh'
327 0x00, 0x00, 0x00, // flags
328 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // SystemID
329 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
330 0x00, 0x00, 0x00, 0x02, // key count
331 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03, // key1
332 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
333 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04, // key2
334 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
335 0x00, 0x00, 0x00, 0x00 // datasize
338 std::vector
<std::vector
<uint8
>> key_ids
;
339 EXPECT_TRUE(ValidatePsshInput(data
, arraysize(data
)));
340 EXPECT_TRUE(GetKeyIdsForCommonSystemId(data
, arraysize(data
), &key_ids
));
341 EXPECT_EQ(2u, key_ids
.size());
344 TEST_F(CencUtilsTest
, HugeSize
) {
345 const uint8 data
[] = {
346 0x00, 0x00, 0x00, 0x01, // size = 1
347 0x70, 0x73, 0x73, 0x68, // 'pssh'
348 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // longsize = big
350 0x00, 0x00, 0x00, // flags
351 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // SystemID
352 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B,
353 0x00, 0x00, 0x00, 0x02, // key count
354 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03, // key1
355 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
356 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04, // key2
357 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
358 0x00, 0x00, 0x00, 0x00 // datasize
361 std::vector
<std::vector
<uint8
>> key_ids
;
362 EXPECT_FALSE(ValidatePsshInput(data
, arraysize(data
)));
363 EXPECT_FALSE(GetKeyIdsForCommonSystemId(data
, arraysize(data
), &key_ids
));