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

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

Issue 1002193005: Properly determine if a new key was added in AesDecryptor::Update() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix pipeline test Created 5 years, 9 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
« no previous file with comments | « media/cdm/aes_decryptor.cc ('k') | media/test/pipeline_integration_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 <string> 5 #include <string>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 bool has_additional_usable_key, 316 bool has_additional_usable_key,
317 CdmKeysInfo keys_info) { 317 CdmKeysInfo keys_info) {
318 keys_info_.swap(keys_info); 318 keys_info_.swap(keys_info);
319 OnSessionKeysChangeCalled(session_id, has_additional_usable_key); 319 OnSessionKeysChangeCalled(session_id, has_additional_usable_key);
320 } 320 }
321 321
322 // Updates the session specified by |session_id| with |key|. |result| 322 // Updates the session specified by |session_id| with |key|. |result|
323 // tests that the update succeeds or generates an error. 323 // tests that the update succeeds or generates an error.
324 void UpdateSessionAndExpect(std::string session_id, 324 void UpdateSessionAndExpect(std::string session_id,
325 const std::string& key, 325 const std::string& key,
326 PromiseResult expected_result) { 326 PromiseResult expected_result,
327 bool new_key_expected) {
327 DCHECK(!key.empty()); 328 DCHECK(!key.empty());
328 329
329 if (expected_result == RESOLVED) { 330 if (expected_result == RESOLVED) {
330 EXPECT_CALL(*this, OnSessionKeysChangeCalled(session_id, true)); 331 EXPECT_CALL(*this,
332 OnSessionKeysChangeCalled(session_id, new_key_expected));
331 } else { 333 } else {
332 EXPECT_CALL(*this, OnSessionKeysChangeCalled(_, _)).Times(0); 334 EXPECT_CALL(*this, OnSessionKeysChangeCalled(_, _)).Times(0);
333 } 335 }
334 336
335 decryptor_.UpdateSession(session_id, 337 decryptor_.UpdateSession(session_id,
336 reinterpret_cast<const uint8*>(key.c_str()), 338 reinterpret_cast<const uint8*>(key.c_str()),
337 key.length(), 339 key.length(),
338 CreatePromise(expected_result)); 340 CreatePromise(expected_result));
339 } 341 }
340 342
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 EXPECT_CALL(*this, OnSessionMessage(IsNotEmpty(), _, IsJSONDictionary(), 486 EXPECT_CALL(*this, OnSessionMessage(IsNotEmpty(), _, IsJSONDictionary(),
485 GURL::EmptyGURL())); 487 GURL::EmptyGURL()));
486 decryptor_.CreateSessionAndGenerateRequest( 488 decryptor_.CreateSessionAndGenerateRequest(
487 MediaKeys::TEMPORARY_SESSION, "keyids", 489 MediaKeys::TEMPORARY_SESSION, "keyids",
488 reinterpret_cast<const uint8*>(init_data), arraysize(init_data) - 1, 490 reinterpret_cast<const uint8*>(init_data), arraysize(init_data) - 1,
489 CreateSessionPromise(RESOLVED)); 491 CreateSessionPromise(RESOLVED));
490 } 492 }
491 493
492 TEST_F(AesDecryptorTest, NormalDecryption) { 494 TEST_F(AesDecryptorTest, NormalDecryption) {
493 std::string session_id = CreateSession(key_id_); 495 std::string session_id = CreateSession(key_id_);
494 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 496 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
495 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 497 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
496 encrypted_data_, key_id_, iv_, no_subsample_entries_); 498 encrypted_data_, key_id_, iv_, no_subsample_entries_);
497 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); 499 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS);
498 } 500 }
499 501
500 TEST_F(AesDecryptorTest, UnencryptedFrame) { 502 TEST_F(AesDecryptorTest, UnencryptedFrame) {
501 // An empty iv string signals that the frame is unencrypted. 503 // An empty iv string signals that the frame is unencrypted.
502 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 504 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
503 original_data_, key_id_, std::vector<uint8>(), no_subsample_entries_); 505 original_data_, key_id_, std::vector<uint8>(), no_subsample_entries_);
504 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); 506 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS);
505 } 507 }
506 508
507 TEST_F(AesDecryptorTest, WrongKey) { 509 TEST_F(AesDecryptorTest, WrongKey) {
508 std::string session_id = CreateSession(key_id_); 510 std::string session_id = CreateSession(key_id_);
509 UpdateSessionAndExpect(session_id, kWrongKeyAsJWK, RESOLVED); 511 UpdateSessionAndExpect(session_id, kWrongKeyAsJWK, RESOLVED, true);
510 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 512 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
511 encrypted_data_, key_id_, iv_, no_subsample_entries_); 513 encrypted_data_, key_id_, iv_, no_subsample_entries_);
512 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); 514 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH);
513 } 515 }
514 516
515 TEST_F(AesDecryptorTest, NoKey) { 517 TEST_F(AesDecryptorTest, NoKey) {
516 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 518 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
517 encrypted_data_, key_id_, iv_, no_subsample_entries_); 519 encrypted_data_, key_id_, iv_, no_subsample_entries_);
518 EXPECT_CALL(*this, BufferDecrypted(AesDecryptor::kNoKey, IsNull())); 520 EXPECT_CALL(*this, BufferDecrypted(AesDecryptor::kNoKey, IsNull()));
519 decryptor_.Decrypt(Decryptor::kVideo, encrypted_buffer, decrypt_cb_); 521 decryptor_.Decrypt(Decryptor::kVideo, encrypted_buffer, decrypt_cb_);
520 } 522 }
521 523
522 TEST_F(AesDecryptorTest, KeyReplacement) { 524 TEST_F(AesDecryptorTest, KeyReplacement) {
523 std::string session_id = CreateSession(key_id_); 525 std::string session_id = CreateSession(key_id_);
524 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 526 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
525 encrypted_data_, key_id_, iv_, no_subsample_entries_); 527 encrypted_data_, key_id_, iv_, no_subsample_entries_);
526 528
527 UpdateSessionAndExpect(session_id, kWrongKeyAsJWK, RESOLVED); 529 UpdateSessionAndExpect(session_id, kWrongKeyAsJWK, RESOLVED, true);
528 ASSERT_NO_FATAL_FAILURE(DecryptAndExpect( 530 ASSERT_NO_FATAL_FAILURE(DecryptAndExpect(
529 encrypted_buffer, original_data_, DATA_MISMATCH)); 531 encrypted_buffer, original_data_, DATA_MISMATCH));
530 532
531 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 533 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, false);
532 ASSERT_NO_FATAL_FAILURE( 534 ASSERT_NO_FATAL_FAILURE(
533 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); 535 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
534 } 536 }
535 537
536 TEST_F(AesDecryptorTest, WrongSizedKey) { 538 TEST_F(AesDecryptorTest, WrongSizedKey) {
537 std::string session_id = CreateSession(key_id_); 539 std::string session_id = CreateSession(key_id_);
538 UpdateSessionAndExpect(session_id, kWrongSizedKeyAsJWK, REJECTED); 540 UpdateSessionAndExpect(session_id, kWrongSizedKeyAsJWK, REJECTED, true);
539 } 541 }
540 542
541 TEST_F(AesDecryptorTest, MultipleKeysAndFrames) { 543 TEST_F(AesDecryptorTest, MultipleKeysAndFrames) {
542 std::string session_id = CreateSession(key_id_); 544 std::string session_id = CreateSession(key_id_);
543 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 545 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
544 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 546 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
545 encrypted_data_, key_id_, iv_, no_subsample_entries_); 547 encrypted_data_, key_id_, iv_, no_subsample_entries_);
546 ASSERT_NO_FATAL_FAILURE( 548 ASSERT_NO_FATAL_FAILURE(
547 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); 549 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
548 550
549 UpdateSessionAndExpect(session_id, kKey2AsJWK, RESOLVED); 551 UpdateSessionAndExpect(session_id, kKey2AsJWK, RESOLVED, true);
550 552
551 // The first key is still available after we added a second key. 553 // The first key is still available after we added a second key.
552 ASSERT_NO_FATAL_FAILURE( 554 ASSERT_NO_FATAL_FAILURE(
553 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); 555 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
554 556
555 // The second key is also available. 557 // The second key is also available.
556 encrypted_buffer = CreateEncryptedBuffer( 558 encrypted_buffer = CreateEncryptedBuffer(
557 std::vector<uint8>(kEncryptedData2, 559 std::vector<uint8>(kEncryptedData2,
558 kEncryptedData2 + arraysize(kEncryptedData2)), 560 kEncryptedData2 + arraysize(kEncryptedData2)),
559 std::vector<uint8>(kKeyId2, kKeyId2 + arraysize(kKeyId2)), 561 std::vector<uint8>(kKeyId2, kKeyId2 + arraysize(kKeyId2)),
560 std::vector<uint8>(kIv2, kIv2 + arraysize(kIv2)), 562 std::vector<uint8>(kIv2, kIv2 + arraysize(kIv2)),
561 no_subsample_entries_); 563 no_subsample_entries_);
562 ASSERT_NO_FATAL_FAILURE(DecryptAndExpect( 564 ASSERT_NO_FATAL_FAILURE(DecryptAndExpect(
563 encrypted_buffer, 565 encrypted_buffer,
564 std::vector<uint8>(kOriginalData2, 566 std::vector<uint8>(kOriginalData2,
565 kOriginalData2 + arraysize(kOriginalData2) - 1), 567 kOriginalData2 + arraysize(kOriginalData2) - 1),
566 SUCCESS)); 568 SUCCESS));
567 } 569 }
568 570
569 TEST_F(AesDecryptorTest, CorruptedIv) { 571 TEST_F(AesDecryptorTest, CorruptedIv) {
570 std::string session_id = CreateSession(key_id_); 572 std::string session_id = CreateSession(key_id_);
571 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 573 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
572 574
573 std::vector<uint8> bad_iv = iv_; 575 std::vector<uint8> bad_iv = iv_;
574 bad_iv[1]++; 576 bad_iv[1]++;
575 577
576 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 578 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
577 encrypted_data_, key_id_, bad_iv, no_subsample_entries_); 579 encrypted_data_, key_id_, bad_iv, no_subsample_entries_);
578 580
579 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); 581 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH);
580 } 582 }
581 583
582 TEST_F(AesDecryptorTest, CorruptedData) { 584 TEST_F(AesDecryptorTest, CorruptedData) {
583 std::string session_id = CreateSession(key_id_); 585 std::string session_id = CreateSession(key_id_);
584 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 586 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
585 587
586 std::vector<uint8> bad_data = encrypted_data_; 588 std::vector<uint8> bad_data = encrypted_data_;
587 bad_data[1]++; 589 bad_data[1]++;
588 590
589 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 591 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
590 bad_data, key_id_, iv_, no_subsample_entries_); 592 bad_data, key_id_, iv_, no_subsample_entries_);
591 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); 593 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH);
592 } 594 }
593 595
594 TEST_F(AesDecryptorTest, EncryptedAsUnencryptedFailure) { 596 TEST_F(AesDecryptorTest, EncryptedAsUnencryptedFailure) {
595 std::string session_id = CreateSession(key_id_); 597 std::string session_id = CreateSession(key_id_);
596 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 598 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
597 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 599 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
598 encrypted_data_, key_id_, std::vector<uint8>(), no_subsample_entries_); 600 encrypted_data_, key_id_, std::vector<uint8>(), no_subsample_entries_);
599 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); 601 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH);
600 } 602 }
601 603
602 TEST_F(AesDecryptorTest, SubsampleDecryption) { 604 TEST_F(AesDecryptorTest, SubsampleDecryption) {
603 std::string session_id = CreateSession(key_id_); 605 std::string session_id = CreateSession(key_id_);
604 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 606 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
605 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 607 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
606 subsample_encrypted_data_, key_id_, iv_, normal_subsample_entries_); 608 subsample_encrypted_data_, key_id_, iv_, normal_subsample_entries_);
607 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); 609 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS);
608 } 610 }
609 611
610 // Ensures noninterference of data offset and subsample mechanisms. We never 612 // Ensures noninterference of data offset and subsample mechanisms. We never
611 // expect to encounter this in the wild, but since the DecryptConfig doesn't 613 // expect to encounter this in the wild, but since the DecryptConfig doesn't
612 // disallow such a configuration, it should be covered. 614 // disallow such a configuration, it should be covered.
613 TEST_F(AesDecryptorTest, SubsampleDecryptionWithOffset) { 615 TEST_F(AesDecryptorTest, SubsampleDecryptionWithOffset) {
614 std::string session_id = CreateSession(key_id_); 616 std::string session_id = CreateSession(key_id_);
615 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 617 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
616 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 618 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
617 subsample_encrypted_data_, key_id_, iv_, normal_subsample_entries_); 619 subsample_encrypted_data_, key_id_, iv_, normal_subsample_entries_);
618 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); 620 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS);
619 } 621 }
620 622
621 TEST_F(AesDecryptorTest, SubsampleWrongSize) { 623 TEST_F(AesDecryptorTest, SubsampleWrongSize) {
622 std::string session_id = CreateSession(key_id_); 624 std::string session_id = CreateSession(key_id_);
623 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 625 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
624 626
625 std::vector<SubsampleEntry> subsample_entries_wrong_size( 627 std::vector<SubsampleEntry> subsample_entries_wrong_size(
626 kSubsampleEntriesWrongSize, 628 kSubsampleEntriesWrongSize,
627 kSubsampleEntriesWrongSize + arraysize(kSubsampleEntriesWrongSize)); 629 kSubsampleEntriesWrongSize + arraysize(kSubsampleEntriesWrongSize));
628 630
629 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 631 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
630 subsample_encrypted_data_, key_id_, iv_, subsample_entries_wrong_size); 632 subsample_encrypted_data_, key_id_, iv_, subsample_entries_wrong_size);
631 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); 633 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH);
632 } 634 }
633 635
634 TEST_F(AesDecryptorTest, SubsampleInvalidTotalSize) { 636 TEST_F(AesDecryptorTest, SubsampleInvalidTotalSize) {
635 std::string session_id = CreateSession(key_id_); 637 std::string session_id = CreateSession(key_id_);
636 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 638 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
637 639
638 std::vector<SubsampleEntry> subsample_entries_invalid_total_size( 640 std::vector<SubsampleEntry> subsample_entries_invalid_total_size(
639 kSubsampleEntriesInvalidTotalSize, 641 kSubsampleEntriesInvalidTotalSize,
640 kSubsampleEntriesInvalidTotalSize + 642 kSubsampleEntriesInvalidTotalSize +
641 arraysize(kSubsampleEntriesInvalidTotalSize)); 643 arraysize(kSubsampleEntriesInvalidTotalSize));
642 644
643 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 645 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
644 subsample_encrypted_data_, key_id_, iv_, 646 subsample_encrypted_data_, key_id_, iv_,
645 subsample_entries_invalid_total_size); 647 subsample_entries_invalid_total_size);
646 DecryptAndExpect(encrypted_buffer, original_data_, DECRYPT_ERROR); 648 DecryptAndExpect(encrypted_buffer, original_data_, DECRYPT_ERROR);
647 } 649 }
648 650
649 // No cypher bytes in any of the subsamples. 651 // No cypher bytes in any of the subsamples.
650 TEST_F(AesDecryptorTest, SubsampleClearBytesOnly) { 652 TEST_F(AesDecryptorTest, SubsampleClearBytesOnly) {
651 std::string session_id = CreateSession(key_id_); 653 std::string session_id = CreateSession(key_id_);
652 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 654 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
653 655
654 std::vector<SubsampleEntry> clear_only_subsample_entries( 656 std::vector<SubsampleEntry> clear_only_subsample_entries(
655 kSubsampleEntriesClearOnly, 657 kSubsampleEntriesClearOnly,
656 kSubsampleEntriesClearOnly + arraysize(kSubsampleEntriesClearOnly)); 658 kSubsampleEntriesClearOnly + arraysize(kSubsampleEntriesClearOnly));
657 659
658 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 660 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
659 original_data_, key_id_, iv_, clear_only_subsample_entries); 661 original_data_, key_id_, iv_, clear_only_subsample_entries);
660 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); 662 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS);
661 } 663 }
662 664
663 // No clear bytes in any of the subsamples. 665 // No clear bytes in any of the subsamples.
664 TEST_F(AesDecryptorTest, SubsampleCypherBytesOnly) { 666 TEST_F(AesDecryptorTest, SubsampleCypherBytesOnly) {
665 std::string session_id = CreateSession(key_id_); 667 std::string session_id = CreateSession(key_id_);
666 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 668 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
667 669
668 std::vector<SubsampleEntry> cypher_only_subsample_entries( 670 std::vector<SubsampleEntry> cypher_only_subsample_entries(
669 kSubsampleEntriesCypherOnly, 671 kSubsampleEntriesCypherOnly,
670 kSubsampleEntriesCypherOnly + arraysize(kSubsampleEntriesCypherOnly)); 672 kSubsampleEntriesCypherOnly + arraysize(kSubsampleEntriesCypherOnly));
671 673
672 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 674 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
673 encrypted_data_, key_id_, iv_, cypher_only_subsample_entries); 675 encrypted_data_, key_id_, iv_, cypher_only_subsample_entries);
674 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); 676 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS);
675 } 677 }
676 678
677 TEST_F(AesDecryptorTest, CloseSession) { 679 TEST_F(AesDecryptorTest, CloseSession) {
678 std::string session_id = CreateSession(key_id_); 680 std::string session_id = CreateSession(key_id_);
679 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 681 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
680 encrypted_data_, key_id_, iv_, no_subsample_entries_); 682 encrypted_data_, key_id_, iv_, no_subsample_entries_);
681 683
682 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 684 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
683 ASSERT_NO_FATAL_FAILURE( 685 ASSERT_NO_FATAL_FAILURE(
684 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); 686 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
685 687
686 CloseSession(session_id); 688 CloseSession(session_id);
687 } 689 }
688 690
689 TEST_F(AesDecryptorTest, RemoveSession) { 691 TEST_F(AesDecryptorTest, RemoveSession) {
690 // TODO(jrummell): Clean this up when the prefixed API is removed. 692 // TODO(jrummell): Clean this up when the prefixed API is removed.
691 // http://crbug.com/249976. 693 // http://crbug.com/249976.
692 std::string session_id = CreateSession(key_id_); 694 std::string session_id = CreateSession(key_id_);
693 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 695 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
694 encrypted_data_, key_id_, iv_, no_subsample_entries_); 696 encrypted_data_, key_id_, iv_, no_subsample_entries_);
695 697
696 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 698 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
697 ASSERT_NO_FATAL_FAILURE( 699 ASSERT_NO_FATAL_FAILURE(
698 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); 700 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
699 701
700 RemoveSession(session_id); 702 RemoveSession(session_id);
701 } 703 }
702 704
703 TEST_F(AesDecryptorTest, NoKeyAfterCloseSession) { 705 TEST_F(AesDecryptorTest, NoKeyAfterCloseSession) {
704 std::string session_id = CreateSession(key_id_); 706 std::string session_id = CreateSession(key_id_);
705 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 707 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
706 encrypted_data_, key_id_, iv_, no_subsample_entries_); 708 encrypted_data_, key_id_, iv_, no_subsample_entries_);
707 709
708 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 710 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
709 ASSERT_NO_FATAL_FAILURE( 711 ASSERT_NO_FATAL_FAILURE(
710 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); 712 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
711 713
712 CloseSession(session_id); 714 CloseSession(session_id);
713 ASSERT_NO_FATAL_FAILURE( 715 ASSERT_NO_FATAL_FAILURE(
714 DecryptAndExpect(encrypted_buffer, original_data_, NO_KEY)); 716 DecryptAndExpect(encrypted_buffer, original_data_, NO_KEY));
715 } 717 }
716 718
717 TEST_F(AesDecryptorTest, LatestKeyUsed) { 719 TEST_F(AesDecryptorTest, LatestKeyUsed) {
718 std::string session_id1 = CreateSession(key_id_); 720 std::string session_id1 = CreateSession(key_id_);
719 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 721 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
720 encrypted_data_, key_id_, iv_, no_subsample_entries_); 722 encrypted_data_, key_id_, iv_, no_subsample_entries_);
721 723
722 // Add alternate key, buffer should not be decoded properly. 724 // Add alternate key, buffer should not be decoded properly.
723 UpdateSessionAndExpect(session_id1, kKeyAlternateAsJWK, RESOLVED); 725 UpdateSessionAndExpect(session_id1, kKeyAlternateAsJWK, RESOLVED, true);
724 ASSERT_NO_FATAL_FAILURE( 726 ASSERT_NO_FATAL_FAILURE(
725 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH)); 727 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH));
726 728
727 // Create a second session with a correct key value for key_id_. 729 // Create a second session with a correct key value for key_id_.
728 std::string session_id2 = CreateSession(key_id_); 730 std::string session_id2 = CreateSession(key_id_);
729 UpdateSessionAndExpect(session_id2, kKeyAsJWK, RESOLVED); 731 UpdateSessionAndExpect(session_id2, kKeyAsJWK, RESOLVED, true);
730 732
731 // Should be able to decode with latest key. 733 // Should be able to decode with latest key.
732 ASSERT_NO_FATAL_FAILURE( 734 ASSERT_NO_FATAL_FAILURE(
733 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); 735 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
734 } 736 }
735 737
736 TEST_F(AesDecryptorTest, LatestKeyUsedAfterCloseSession) { 738 TEST_F(AesDecryptorTest, LatestKeyUsedAfterCloseSession) {
737 std::string session_id1 = CreateSession(key_id_); 739 std::string session_id1 = CreateSession(key_id_);
738 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( 740 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer(
739 encrypted_data_, key_id_, iv_, no_subsample_entries_); 741 encrypted_data_, key_id_, iv_, no_subsample_entries_);
740 UpdateSessionAndExpect(session_id1, kKeyAsJWK, RESOLVED); 742 UpdateSessionAndExpect(session_id1, kKeyAsJWK, RESOLVED, true);
741 ASSERT_NO_FATAL_FAILURE( 743 ASSERT_NO_FATAL_FAILURE(
742 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); 744 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
743 745
744 // Create a second session with a different key value for key_id_. 746 // Create a second session with a different key value for key_id_.
745 std::string session_id2 = CreateSession(key_id_); 747 std::string session_id2 = CreateSession(key_id_);
746 UpdateSessionAndExpect(session_id2, kKeyAlternateAsJWK, RESOLVED); 748 UpdateSessionAndExpect(session_id2, kKeyAlternateAsJWK, RESOLVED, true);
747 749
748 // Should not be able to decode with new key. 750 // Should not be able to decode with new key.
749 ASSERT_NO_FATAL_FAILURE( 751 ASSERT_NO_FATAL_FAILURE(
750 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH)); 752 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH));
751 753
752 // Close second session, should revert to original key. 754 // Close second session, should revert to original key.
753 CloseSession(session_id2); 755 CloseSession(session_id2);
754 ASSERT_NO_FATAL_FAILURE( 756 ASSERT_NO_FATAL_FAILURE(
755 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); 757 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS));
756 } 758 }
757 759
758 TEST_F(AesDecryptorTest, JWKKey) { 760 TEST_F(AesDecryptorTest, JWKKey) {
759 std::string session_id = CreateSession(key_id_); 761 std::string session_id = CreateSession(key_id_);
760 762
761 // Try a simple JWK key (i.e. not in a set) 763 // Try a simple JWK key (i.e. not in a set)
762 const std::string kJwkSimple = 764 const std::string kJwkSimple =
763 "{" 765 "{"
764 " \"kty\": \"oct\"," 766 " \"kty\": \"oct\","
765 " \"alg\": \"A128KW\"," 767 " \"alg\": \"A128KW\","
766 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM\"," 768 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM\","
767 " \"k\": \"FBUWFxgZGhscHR4fICEiIw\"" 769 " \"k\": \"FBUWFxgZGhscHR4fICEiIw\""
768 "}"; 770 "}";
769 UpdateSessionAndExpect(session_id, kJwkSimple, REJECTED); 771 UpdateSessionAndExpect(session_id, kJwkSimple, REJECTED, true);
770 772
771 // Try a key list with multiple entries. 773 // Try a key list with multiple entries.
772 const std::string kJwksMultipleEntries = 774 const std::string kJwksMultipleEntries =
773 "{" 775 "{"
774 " \"keys\": [" 776 " \"keys\": ["
775 " {" 777 " {"
776 " \"kty\": \"oct\"," 778 " \"kty\": \"oct\","
777 " \"alg\": \"A128KW\"," 779 " \"alg\": \"A128KW\","
778 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM\"," 780 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM\","
779 " \"k\": \"FBUWFxgZGhscHR4fICEiIw\"" 781 " \"k\": \"FBUWFxgZGhscHR4fICEiIw\""
780 " }," 782 " },"
781 " {" 783 " {"
782 " \"kty\": \"oct\"," 784 " \"kty\": \"oct\","
783 " \"alg\": \"A128KW\"," 785 " \"alg\": \"A128KW\","
784 " \"kid\": \"JCUmJygpKissLS4vMA\"," 786 " \"kid\": \"JCUmJygpKissLS4vMA\","
785 " \"k\":\"MTIzNDU2Nzg5Ojs8PT4_QA\"" 787 " \"k\":\"MTIzNDU2Nzg5Ojs8PT4_QA\""
786 " }" 788 " }"
787 " ]" 789 " ]"
788 "}"; 790 "}";
789 UpdateSessionAndExpect(session_id, kJwksMultipleEntries, RESOLVED); 791 UpdateSessionAndExpect(session_id, kJwksMultipleEntries, RESOLVED, true);
790 792
791 // Try a key with no spaces and some \n plus additional fields. 793 // Try a key with no spaces and some \n plus additional fields.
792 const std::string kJwksNoSpaces = 794 const std::string kJwksNoSpaces =
793 "\n\n{\"something\":1,\"keys\":[{\n\n\"kty\":\"oct\",\"alg\":\"A128KW\"," 795 "\n\n{\"something\":1,\"keys\":[{\n\n\"kty\":\"oct\",\"alg\":\"A128KW\","
794 "\"kid\":\"AAECAwQFBgcICQoLDA0ODxAREhM\",\"k\":\"GawgguFyGrWKav7AX4VKUg" 796 "\"kid\":\"AQIDBAUGBwgJCgsMCg4PAA\",\"k\":\"GawgguFyGrWKav7AX4VKUg"
795 "\",\"foo\":\"bar\"}]}\n\n"; 797 "\",\"foo\":\"bar\"}]}\n\n";
796 UpdateSessionAndExpect(session_id, kJwksNoSpaces, RESOLVED); 798 UpdateSessionAndExpect(session_id, kJwksNoSpaces, RESOLVED, true);
797 799
798 // Try some non-ASCII characters. 800 // Try some non-ASCII characters.
799 UpdateSessionAndExpect( 801 UpdateSessionAndExpect(session_id,
800 session_id, "This is not ASCII due to \xff\xfe\xfd in it.", REJECTED); 802 "This is not ASCII due to \xff\xfe\xfd in it.",
803 REJECTED, true);
801 804
802 // Try a badly formatted key. Assume that the JSON parser is fully tested, 805 // Try a badly formatted key. Assume that the JSON parser is fully tested,
803 // so we won't try a lot of combinations. However, need a test to ensure 806 // so we won't try a lot of combinations. However, need a test to ensure
804 // that the code doesn't crash if invalid JSON received. 807 // that the code doesn't crash if invalid JSON received.
805 UpdateSessionAndExpect(session_id, "This is not a JSON key.", REJECTED); 808 UpdateSessionAndExpect(session_id, "This is not a JSON key.", REJECTED, true);
806 809
807 // Try passing some valid JSON that is not a dictionary at the top level. 810 // Try passing some valid JSON that is not a dictionary at the top level.
808 UpdateSessionAndExpect(session_id, "40", REJECTED); 811 UpdateSessionAndExpect(session_id, "40", REJECTED, true);
809 812
810 // Try an empty dictionary. 813 // Try an empty dictionary.
811 UpdateSessionAndExpect(session_id, "{ }", REJECTED); 814 UpdateSessionAndExpect(session_id, "{ }", REJECTED, true);
812 815
813 // Try an empty 'keys' dictionary. 816 // Try an empty 'keys' dictionary.
814 UpdateSessionAndExpect(session_id, "{ \"keys\": [] }", REJECTED); 817 UpdateSessionAndExpect(session_id, "{ \"keys\": [] }", REJECTED, true);
815 818
816 // Try with 'keys' not a dictionary. 819 // Try with 'keys' not a dictionary.
817 UpdateSessionAndExpect(session_id, "{ \"keys\":\"1\" }", REJECTED); 820 UpdateSessionAndExpect(session_id, "{ \"keys\":\"1\" }", REJECTED, true);
818 821
819 // Try with 'keys' a list of integers. 822 // Try with 'keys' a list of integers.
820 UpdateSessionAndExpect(session_id, "{ \"keys\": [ 1, 2, 3 ] }", REJECTED); 823 UpdateSessionAndExpect(session_id, "{ \"keys\": [ 1, 2, 3 ] }", REJECTED,
824 true);
821 825
822 // Try padding(=) at end of 'k' base64 string. 826 // Try padding(=) at end of 'k' base64 string.
823 const std::string kJwksWithPaddedKey = 827 const std::string kJwksWithPaddedKey =
824 "{" 828 "{"
825 " \"keys\": [" 829 " \"keys\": ["
826 " {" 830 " {"
827 " \"kty\": \"oct\"," 831 " \"kty\": \"oct\","
828 " \"alg\": \"A128KW\"," 832 " \"alg\": \"A128KW\","
829 " \"kid\": \"AAECAw\"," 833 " \"kid\": \"AAECAw\","
830 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw==\"" 834 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw==\""
831 " }" 835 " }"
832 " ]" 836 " ]"
833 "}"; 837 "}";
834 UpdateSessionAndExpect(session_id, kJwksWithPaddedKey, REJECTED); 838 UpdateSessionAndExpect(session_id, kJwksWithPaddedKey, REJECTED, true);
835 839
836 // Try padding(=) at end of 'kid' base64 string. 840 // Try padding(=) at end of 'kid' base64 string.
837 const std::string kJwksWithPaddedKeyId = 841 const std::string kJwksWithPaddedKeyId =
838 "{" 842 "{"
839 " \"keys\": [" 843 " \"keys\": ["
840 " {" 844 " {"
841 " \"kty\": \"oct\"," 845 " \"kty\": \"oct\","
842 " \"alg\": \"A128KW\"," 846 " \"alg\": \"A128KW\","
843 " \"kid\": \"AAECAw==\"," 847 " \"kid\": \"AAECAw==\","
844 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" 848 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\""
845 " }" 849 " }"
846 " ]" 850 " ]"
847 "}"; 851 "}";
848 UpdateSessionAndExpect(session_id, kJwksWithPaddedKeyId, REJECTED); 852 UpdateSessionAndExpect(session_id, kJwksWithPaddedKeyId, REJECTED, true);
849 853
850 // Try a key with invalid base64 encoding. 854 // Try a key with invalid base64 encoding.
851 const std::string kJwksWithInvalidBase64 = 855 const std::string kJwksWithInvalidBase64 =
852 "{" 856 "{"
853 " \"keys\": [" 857 " \"keys\": ["
854 " {" 858 " {"
855 " \"kty\": \"oct\"," 859 " \"kty\": \"oct\","
856 " \"alg\": \"A128KW\"," 860 " \"alg\": \"A128KW\","
857 " \"kid\": \"!@#$%^&*()\"," 861 " \"kid\": \"!@#$%^&*()\","
858 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" 862 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\""
859 " }" 863 " }"
860 " ]" 864 " ]"
861 "}"; 865 "}";
862 UpdateSessionAndExpect(session_id, kJwksWithInvalidBase64, REJECTED); 866 UpdateSessionAndExpect(session_id, kJwksWithInvalidBase64, REJECTED, true);
863 867
864 // Try a 3-byte 'kid' where no base64 padding is required. 868 // Try a 3-byte 'kid' where no base64 padding is required.
865 // |kJwksMultipleEntries| above has 2 'kid's that require 1 and 2 padding 869 // |kJwksMultipleEntries| above has 2 'kid's that require 1 and 2 padding
866 // bytes. Note that 'k' has to be 16 bytes, so it will always require padding. 870 // bytes. Note that 'k' has to be 16 bytes, so it will always require padding.
867 const std::string kJwksWithNoPadding = 871 const std::string kJwksWithNoPadding =
868 "{" 872 "{"
869 " \"keys\": [" 873 " \"keys\": ["
870 " {" 874 " {"
871 " \"kty\": \"oct\"," 875 " \"kty\": \"oct\","
872 " \"alg\": \"A128KW\"," 876 " \"alg\": \"A128KW\","
873 " \"kid\": \"Kiss\"," 877 " \"kid\": \"Kiss\","
874 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" 878 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\""
875 " }" 879 " }"
876 " ]" 880 " ]"
877 "}"; 881 "}";
878 UpdateSessionAndExpect(session_id, kJwksWithNoPadding, RESOLVED); 882 UpdateSessionAndExpect(session_id, kJwksWithNoPadding, RESOLVED, true);
879 883
880 // Empty key id. 884 // Empty key id.
881 const std::string kJwksWithEmptyKeyId = 885 const std::string kJwksWithEmptyKeyId =
882 "{" 886 "{"
883 " \"keys\": [" 887 " \"keys\": ["
884 " {" 888 " {"
885 " \"kty\": \"oct\"," 889 " \"kty\": \"oct\","
886 " \"alg\": \"A128KW\"," 890 " \"alg\": \"A128KW\","
887 " \"kid\": \"\"," 891 " \"kid\": \"\","
888 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" 892 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\""
889 " }" 893 " }"
890 " ]" 894 " ]"
891 "}"; 895 "}";
892 UpdateSessionAndExpect(session_id, kJwksWithEmptyKeyId, REJECTED); 896 UpdateSessionAndExpect(session_id, kJwksWithEmptyKeyId, REJECTED, true);
893 CloseSession(session_id); 897 CloseSession(session_id);
894 } 898 }
895 899
896 TEST_F(AesDecryptorTest, GetKeyIds) { 900 TEST_F(AesDecryptorTest, GetKeyIds) {
897 std::vector<uint8> key_id1(kKeyId, kKeyId + arraysize(kKeyId)); 901 std::vector<uint8> key_id1(kKeyId, kKeyId + arraysize(kKeyId));
898 std::vector<uint8> key_id2(kKeyId2, kKeyId2 + arraysize(kKeyId2)); 902 std::vector<uint8> key_id2(kKeyId2, kKeyId2 + arraysize(kKeyId2));
899 903
900 std::string session_id = CreateSession(key_id_); 904 std::string session_id = CreateSession(key_id_);
901 EXPECT_FALSE(KeysInfoContains(key_id1)); 905 EXPECT_FALSE(KeysInfoContains(key_id1));
902 EXPECT_FALSE(KeysInfoContains(key_id2)); 906 EXPECT_FALSE(KeysInfoContains(key_id2));
903 907
904 // Add 1 key, verify it is returned. 908 // Add 1 key, verify it is returned.
905 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); 909 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
906 EXPECT_TRUE(KeysInfoContains(key_id1)); 910 EXPECT_TRUE(KeysInfoContains(key_id1));
907 EXPECT_FALSE(KeysInfoContains(key_id2)); 911 EXPECT_FALSE(KeysInfoContains(key_id2));
908 912
909 // Add second key, verify both IDs returned. 913 // Add second key, verify both IDs returned.
910 UpdateSessionAndExpect(session_id, kKey2AsJWK, RESOLVED); 914 UpdateSessionAndExpect(session_id, kKey2AsJWK, RESOLVED, true);
911 EXPECT_TRUE(KeysInfoContains(key_id1)); 915 EXPECT_TRUE(KeysInfoContains(key_id1));
912 EXPECT_TRUE(KeysInfoContains(key_id2)); 916 EXPECT_TRUE(KeysInfoContains(key_id2));
913 } 917 }
914 918
919 TEST_F(AesDecryptorTest, NoKeysChangeForSameKey) {
920 std::vector<uint8> key_id(kKeyId, kKeyId + arraysize(kKeyId));
921
922 std::string session_id = CreateSession(key_id_);
923 EXPECT_FALSE(KeysInfoContains(key_id));
924
925 // Add key, verify it is returned.
926 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, true);
927 EXPECT_TRUE(KeysInfoContains(key_id));
928
929 // Add key a second time.
930 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED, false);
931 EXPECT_TRUE(KeysInfoContains(key_id));
932
933 // Create a new session. Add key, should indicate key added for this session.
934 std::string session_id2 = CreateSession(key_id_);
935 UpdateSessionAndExpect(session_id2, kKeyAsJWK, RESOLVED, true);
936 }
937
915 } // namespace media 938 } // namespace media
OLDNEW
« no previous file with comments | « media/cdm/aes_decryptor.cc ('k') | media/test/pipeline_integration_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698