Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: media/cdm/cenc_utils_unittest.cc

Issue 1149023002: Combine 'pssh' parsing routines. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/cdm/cenc_utils.h" 5 #include "media/cdm/cenc_utils.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 namespace media { 10 namespace media {
11 11
12 const uint8_t kKey1Data[] = { 12 const uint8_t kKey1Data[] = {
13 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03, 13 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03,
14 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03 14 0x7E, 0x57, 0x1D, 0x03, 0x7E, 0x57, 0x1D, 0x03
15 }; 15 };
16 const uint8_t kKey2Data[] = { 16 const uint8_t kKey2Data[] = {
17 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04, 17 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
18 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04, 18 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x04,
19 }; 19 };
20 const uint8_t kKey3Data[] = { 20 const uint8_t kKey3Data[] = {
21 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x05, 21 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x05,
22 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x05, 22 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x05,
23 }; 23 };
24 const uint8_t kKey4Data[] = { 24 const uint8_t kKey4Data[] = {
25 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x06, 25 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x06,
26 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x06, 26 0x7E, 0x57, 0x1D, 0x04, 0x7E, 0x57, 0x1D, 0x06,
27 }; 27 };
28 const uint8_t kClearKeyUuid[] = {
ddorwin 2015/06/04 18:08:45 s/CK/CommonSystem/
jrummell 2015/06/04 21:47:10 Done.
29 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02,
30 0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B
31 };
28 32
29 class CencUtilsTest : public testing::Test { 33 class CencUtilsTest : public testing::Test {
30 public: 34 public:
31 CencUtilsTest() 35 CencUtilsTest()
32 : key1_(kKey1Data, kKey1Data + arraysize(kKey1Data)), 36 : key1_(kKey1Data, kKey1Data + arraysize(kKey1Data)),
33 key2_(kKey2Data, kKey2Data + arraysize(kKey2Data)), 37 key2_(kKey2Data, kKey2Data + arraysize(kKey2Data)),
34 key3_(kKey3Data, kKey3Data + arraysize(kKey3Data)), 38 key3_(kKey3Data, kKey3Data + arraysize(kKey3Data)),
35 key4_(kKey4Data, kKey4Data + arraysize(kKey4Data)) {} 39 key4_(kKey4Data, kKey4Data + arraysize(kKey4Data)) {}
36 40
37 protected: 41 protected:
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 box.push_back(key2[i]); 153 box.push_back(key2[i]);
150 154
151 // Add data_size (= 0). 155 // Add data_size (= 0).
152 box.push_back(0); 156 box.push_back(0);
153 box.push_back(0); 157 box.push_back(0);
154 box.push_back(0); 158 box.push_back(0);
155 box.push_back(0); 159 box.push_back(0);
156 return box; 160 return box;
157 } 161 }
158 162
163 void AppendData(std::vector<uint8_t>& pssh_box,
164 const std::vector<uint8_t>& data) {
165 // This assumes that |pssh_box| has been created using the routines above,
166 // and simply appends the data to the end of it. It updates the box size
167 // and sets the data size.
168 DCHECK(data.size() < 100);
169 pssh_box[3] += data.size();
170 pssh_box.pop_back();
171 pssh_box.push_back(data.size());
172 pssh_box.insert(pssh_box.end(), data.begin(), data.end());
173 }
174
159 const std::vector<uint8_t>& Key1() { return key1_; } 175 const std::vector<uint8_t>& Key1() { return key1_; }
160 const std::vector<uint8_t>& Key2() { return key2_; } 176 const std::vector<uint8_t>& Key2() { return key2_; }
161 const std::vector<uint8_t>& Key3() { return key3_; } 177 const std::vector<uint8_t>& Key3() { return key3_; }
162 const std::vector<uint8_t>& Key4() { return key4_; } 178 const std::vector<uint8_t>& Key4() { return key4_; }
163 179
164 private: 180 private:
165 std::vector<uint8_t> key1_; 181 std::vector<uint8_t> key1_;
166 std::vector<uint8_t> key2_; 182 std::vector<uint8_t> key2_;
167 std::vector<uint8_t> key3_; 183 std::vector<uint8_t> key3_;
168 std::vector<uint8_t> key4_; 184 std::vector<uint8_t> key4_;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box, &key_ids)); 223 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box, &key_ids));
208 EXPECT_EQ(2u, key_ids.size()); 224 EXPECT_EQ(2u, key_ids.size());
209 EXPECT_EQ(key_ids[0], Key1()); 225 EXPECT_EQ(key_ids[0], Key1());
210 EXPECT_EQ(key_ids[1], Key2()); 226 EXPECT_EQ(key_ids[1], Key2());
211 } 227 }
212 228
213 TEST_F(CencUtilsTest, PSSHVersion0Plus1) { 229 TEST_F(CencUtilsTest, PSSHVersion0Plus1) {
214 std::vector<uint8_t> box0 = MakePSSHBox(0); 230 std::vector<uint8_t> box0 = MakePSSHBox(0);
215 std::vector<uint8_t> box1 = MakePSSHBox(1, Key1()); 231 std::vector<uint8_t> box1 = MakePSSHBox(1, Key1());
216 232
217 // Concatentate box1 into box0. 233 // Concatentate box1 onto end of box0.
218 for (const auto& value : box1) 234 box0.insert(box0.end(), box1.begin(), box1.end());
219 box0.push_back(value);
220 235
221 KeyIdList key_ids; 236 KeyIdList key_ids;
222 EXPECT_TRUE(ValidatePsshInput(box0)); 237 EXPECT_TRUE(ValidatePsshInput(box0));
223 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box0, &key_ids)); 238 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box0, &key_ids));
224 EXPECT_EQ(1u, key_ids.size()); 239 EXPECT_EQ(1u, key_ids.size());
225 EXPECT_EQ(key_ids[0], Key1()); 240 EXPECT_EQ(key_ids[0], Key1());
226 } 241 }
227 242
228 TEST_F(CencUtilsTest, PSSHVersion1Plus0) { 243 TEST_F(CencUtilsTest, PSSHVersion1Plus0) {
229 std::vector<uint8_t> box0 = MakePSSHBox(0); 244 std::vector<uint8_t> box0 = MakePSSHBox(0);
230 std::vector<uint8_t> box1 = MakePSSHBox(1, Key1()); 245 std::vector<uint8_t> box1 = MakePSSHBox(1, Key1());
231 246
232 // Concatentate box0 into box1. 247 // Concatentate box0 onto end of box1.
233 for (const auto& value : box0) 248 box1.insert(box1.end(), box0.begin(), box0.end());
234 box1.push_back(value);
235 249
236 KeyIdList key_ids; 250 KeyIdList key_ids;
237 EXPECT_TRUE(ValidatePsshInput(box1)); 251 EXPECT_TRUE(ValidatePsshInput(box1));
238 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box1, &key_ids)); 252 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box1, &key_ids));
239 EXPECT_EQ(1u, key_ids.size()); 253 EXPECT_EQ(1u, key_ids.size());
240 EXPECT_EQ(key_ids[0], Key1()); 254 EXPECT_EQ(key_ids[0], Key1());
241 } 255 }
242 256
243 TEST_F(CencUtilsTest, MultiplePSSHVersion1) { 257 TEST_F(CencUtilsTest, MultiplePSSHVersion1) {
244 std::vector<uint8_t> box = MakePSSHBox(1, Key1(), Key2()); 258 std::vector<uint8_t> box = MakePSSHBox(1, Key1(), Key2());
245 std::vector<uint8_t> box1 = MakePSSHBox(1, Key3()); 259 std::vector<uint8_t> box1 = MakePSSHBox(1, Key3());
246 std::vector<uint8_t> box2 = MakePSSHBox(1, Key4()); 260 std::vector<uint8_t> box2 = MakePSSHBox(1, Key4());
247 261
248 // Concatentate box1 into box. 262 // Concatentate box1 and box2 onto end of box.
249 for (const auto& value : box1) 263 box.insert(box.end(), box1.begin(), box1.end());
250 box.push_back(value); 264 box.insert(box.end(), box2.begin(), box2.end());
251 // Concatentate box2 into box.
252 for (const auto& value : box2)
253 box.push_back(value);
254 265
255 KeyIdList key_ids; 266 KeyIdList key_ids;
256 EXPECT_TRUE(ValidatePsshInput(box)); 267 EXPECT_TRUE(ValidatePsshInput(box));
257 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box, &key_ids)); 268 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box, &key_ids));
258 EXPECT_EQ(4u, key_ids.size()); 269 EXPECT_EQ(4u, key_ids.size());
259 EXPECT_EQ(key_ids[0], Key1()); 270 EXPECT_EQ(key_ids[0], Key1());
260 EXPECT_EQ(key_ids[1], Key2()); 271 EXPECT_EQ(key_ids[1], Key2());
261 EXPECT_EQ(key_ids[2], Key3()); 272 EXPECT_EQ(key_ids[2], Key3());
262 EXPECT_EQ(key_ids[3], Key4()); 273 EXPECT_EQ(key_ids[3], Key4());
263 } 274 }
264 275
265 TEST_F(CencUtilsTest, InvalidPSSH) { 276 TEST_F(CencUtilsTest, InvalidPSSH) {
266 std::vector<uint8_t> box = MakePSSHBox(1, Key1(), Key2()); 277 std::vector<uint8_t> box = MakePSSHBox(1, Key1(), Key2());
267 KeyIdList key_ids; 278 KeyIdList key_ids;
268 for (uint32 i = 1; i < box.size(); ++i) { 279 for (uint32 i = 1; i < box.size(); ++i) {
269 // Modify size of data passed to be less than real size. 280 // Modify size of data passed to be less than real size.
270 std::vector<uint8_t> truncated(&box[0], &box[0] + i); 281 std::vector<uint8_t> truncated(&box[0], &box[0] + i);
271 EXPECT_FALSE(ValidatePsshInput(truncated)); 282 EXPECT_FALSE(ValidatePsshInput(truncated)) << "Failed for length " << i;
272 EXPECT_FALSE(GetKeyIdsForCommonSystemId(truncated, &key_ids)); 283 EXPECT_FALSE(GetKeyIdsForCommonSystemId(truncated, &key_ids));
273 // Modify starting point. 284 // Modify starting point.
274 std::vector<uint8_t> changed_offset(&box[i], &box[i] + box.size() - i); 285 std::vector<uint8_t> changed_offset(&box[i], &box[i] + box.size() - i);
275 EXPECT_FALSE(ValidatePsshInput(changed_offset)); 286 EXPECT_FALSE(ValidatePsshInput(changed_offset)) << "Failed for offset "
287 << i;
276 EXPECT_FALSE(GetKeyIdsForCommonSystemId(changed_offset, &key_ids)); 288 EXPECT_FALSE(GetKeyIdsForCommonSystemId(changed_offset, &key_ids));
277 } 289 }
278 } 290 }
279 291
280 TEST_F(CencUtilsTest, InvalidSystemID) { 292 TEST_F(CencUtilsTest, InvalidSystemID) {
281 std::vector<uint8_t> box = MakePSSHBox(1, Key1(), Key2()); 293 std::vector<uint8_t> box = MakePSSHBox(1, Key1(), Key2());
282 294
283 // Modify the System ID. 295 // Modify the System ID.
284 ++box[20]; 296 ++box[20];
285 297
286 KeyIdList key_ids; 298 KeyIdList key_ids;
287 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box, &key_ids)); 299 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box, &key_ids));
288 EXPECT_EQ(0u, key_ids.size()); 300 EXPECT_EQ(0u, key_ids.size());
289 } 301 }
290 302
291 TEST_F(CencUtilsTest, InvalidFlags) { 303 TEST_F(CencUtilsTest, InvalidFlags) {
292 std::vector<uint8_t> box = MakePSSHBox(1, Key1(), Key2()); 304 std::vector<uint8_t> box = MakePSSHBox(1, Key1(), Key2());
293 305
294 // Modify flags. 306 // Modify flags.
295 box[10] = 3; 307 box[10] = 3;
296 308
297 KeyIdList key_ids; 309 KeyIdList key_ids;
298 // TODO(jrummell): This should fail as the 'pssh' box is skipped. 310 EXPECT_FALSE(GetKeyIdsForCommonSystemId(box, &key_ids));
299 EXPECT_TRUE(GetKeyIdsForCommonSystemId(box, &key_ids));
300 EXPECT_EQ(0u, key_ids.size());
301 } 311 }
302 312
303 TEST_F(CencUtilsTest, LongSize) { 313 TEST_F(CencUtilsTest, LongSize) {
304 const uint8_t data[] = { 314 const uint8_t data[] = {
305 0x00, 0x00, 0x00, 0x01, // size = 1 315 0x00, 0x00, 0x00, 0x01, // size = 1
306 0x70, 0x73, 0x73, 0x68, // 'pssh' 316 0x70, 0x73, 0x73, 0x68, // 'pssh'
307 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4c, // longsize 317 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4c, // longsize
308 0x01, // version 318 0x01, // version
309 0x00, 0x00, 0x00, // flags 319 0x00, 0x00, 0x00, // flags
310 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // SystemID 320 0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02, // SystemID
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 0x00, 0x00, 0x00, 0x00 // datasize 376 0x00, 0x00, 0x00, 0x00 // datasize
367 }; 377 };
368 378
369 KeyIdList key_ids; 379 KeyIdList key_ids;
370 EXPECT_FALSE( 380 EXPECT_FALSE(
371 ValidatePsshInput(std::vector<uint8_t>(data, data + arraysize(data)))); 381 ValidatePsshInput(std::vector<uint8_t>(data, data + arraysize(data))));
372 EXPECT_FALSE(GetKeyIdsForCommonSystemId( 382 EXPECT_FALSE(GetKeyIdsForCommonSystemId(
373 std::vector<uint8_t>(data, data + arraysize(data)), &key_ids)); 383 std::vector<uint8_t>(data, data + arraysize(data)), &key_ids));
374 } 384 }
375 385
386 TEST_F(CencUtilsTest, GetPsshDataVersion0) {
ddorwin 2015/06/04 18:08:45 Add an underscore to all of these: GetPsshData_<te
jrummell 2015/06/04 21:47:10 Done.
387 std::vector<uint8_t> clear_key(kClearKeyUuid,
388 kClearKeyUuid + arraysize(kClearKeyUuid));
389 std::vector<uint8_t> pssh_data;
390
391 std::vector<uint8_t> box = MakePSSHBox(0);
392 EXPECT_TRUE(GetPsshData(box, clear_key, &pssh_data));
393 EXPECT_EQ(0u, pssh_data.size());
394
395 std::vector<uint8_t> data = {0x01, 0x02, 0x03, 0x04};
396 AppendData(box, data);
397 EXPECT_TRUE(GetPsshData(box, clear_key, &pssh_data));
398 EXPECT_EQ(data, pssh_data);
399 }
400
401 TEST_F(CencUtilsTest, GetPsshDataVersion1NoKeys) {
402 std::vector<uint8_t> clear_key(kClearKeyUuid,
403 kClearKeyUuid + arraysize(kClearKeyUuid));
404 std::vector<uint8_t> pssh_data;
405
406 std::vector<uint8_t> box = MakePSSHBox(1);
407 EXPECT_TRUE(GetPsshData(box, clear_key, &pssh_data));
408 EXPECT_EQ(0u, pssh_data.size());
409
410 std::vector<uint8_t> data = {0x05, 0x06, 0x07, 0x08};
411 AppendData(box, data);
412 EXPECT_TRUE(GetPsshData(box, clear_key, &pssh_data));
413 EXPECT_EQ(data, pssh_data);
414 }
415
416 TEST_F(CencUtilsTest, GetPsshDataVersion1WithKeys) {
417 std::vector<uint8_t> clear_key(kClearKeyUuid,
418 kClearKeyUuid + arraysize(kClearKeyUuid));
419 std::vector<uint8_t> pssh_data;
420
421 std::vector<uint8_t> box = MakePSSHBox(1, Key1());
422 EXPECT_TRUE(GetPsshData(box, clear_key, &pssh_data));
423 EXPECT_EQ(0u, pssh_data.size());
424
425 std::vector<uint8_t> data = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
426 AppendData(box, data);
427 EXPECT_TRUE(GetPsshData(box, clear_key, &pssh_data));
428 EXPECT_EQ(data, pssh_data);
429 }
430
431 TEST_F(CencUtilsTest, GetPsshDataVersion2) {
ddorwin 2015/06/04 18:08:45 Should test Version2 then 1 and vice versa.
jrummell 2015/06/04 21:47:10 Done.
432 std::vector<uint8_t> clear_key(kClearKeyUuid,
433 kClearKeyUuid + arraysize(kClearKeyUuid));
434 std::vector<uint8_t> pssh_data;
435
436 std::vector<uint8_t> box = MakePSSHBox(1, Key1());
437 EXPECT_TRUE(GetPsshData(box, clear_key, &pssh_data));
438
439 // Change the version manually, since we don't know what v2 will contain.
440 box[8] = 2;
441 EXPECT_FALSE(GetPsshData(box, clear_key, &pssh_data));
442 }
443
444 TEST_F(CencUtilsTest, GetPsshDataDifferentSystemID) {
445 std::vector<uint8_t> clear_key(kClearKeyUuid,
446 kClearKeyUuid + arraysize(kClearKeyUuid));
447 std::vector<uint8_t> unknown_system_id(kKey1Data,
448 kKey1Data + arraysize(kKey1Data));
449 std::vector<uint8_t> pssh_data;
450
451 std::vector<uint8_t> box = MakePSSHBox(1, Key1());
452 EXPECT_TRUE(GetPsshData(box, clear_key, &pssh_data));
453 EXPECT_FALSE(GetPsshData(box, unknown_system_id, &pssh_data));
454 }
455
456 TEST_F(CencUtilsTest, GetPsshDataMissingData) {
457 std::vector<uint8_t> clear_key(kClearKeyUuid,
458 kClearKeyUuid + arraysize(kClearKeyUuid));
459 std::vector<uint8_t> pssh_data;
460
461 std::vector<uint8_t> box = MakePSSHBox(1, Key1());
462 std::vector<uint8_t> data = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
463 AppendData(box, data);
464 EXPECT_TRUE(GetPsshData(box, clear_key, &pssh_data));
465
466 // Remove some data from the end, so now the size is incorrect.
467 box.pop_back();
468 box.pop_back();
469 EXPECT_FALSE(GetPsshData(box, clear_key, &pssh_data));
470 }
471
472 TEST_F(CencUtilsTest, GetPsshDataMultiplePssh) {
473 std::vector<uint8_t> clear_key(kClearKeyUuid,
474 kClearKeyUuid + arraysize(kClearKeyUuid));
475 std::vector<uint8_t> pssh_data;
476
477 std::vector<uint8_t> box1 = MakePSSHBox(1, Key1());
478 std::vector<uint8_t> data1 = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
479 AppendData(box1, data1);
480
481 std::vector<uint8_t> box2 = MakePSSHBox(0);
482 std::vector<uint8_t> data2 = {0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8};
483 AppendData(box2, data2);
484
485 box1.insert(box1.end(), box2.begin(), box2.end());
486 EXPECT_TRUE(GetPsshData(box1, clear_key, &pssh_data));
487 EXPECT_EQ(data1, pssh_data);
488 EXPECT_NE(data2, pssh_data);
489 }
376 } // namespace media 490 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698