OLD | NEW |
---|---|
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 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
204 } | 204 } |
205 | 205 |
206 enum PromiseResult { RESOLVED, REJECTED }; | 206 enum PromiseResult { RESOLVED, REJECTED }; |
207 | 207 |
208 class AesDecryptorTest : public testing::Test { | 208 class AesDecryptorTest : public testing::Test { |
209 public: | 209 public: |
210 AesDecryptorTest() | 210 AesDecryptorTest() |
211 : decryptor_(base::Bind(&AesDecryptorTest::OnSessionMessage, | 211 : decryptor_(base::Bind(&AesDecryptorTest::OnSessionMessage, |
212 base::Unretained(this)), | 212 base::Unretained(this)), |
213 base::Bind(&AesDecryptorTest::OnSessionClosed, | 213 base::Bind(&AesDecryptorTest::OnSessionClosed, |
214 base::Unretained(this)), | |
215 base::Bind(&AesDecryptorTest::OnSessionKeysChange, | |
214 base::Unretained(this))), | 216 base::Unretained(this))), |
215 decrypt_cb_(base::Bind(&AesDecryptorTest::BufferDecrypted, | 217 decrypt_cb_(base::Bind(&AesDecryptorTest::BufferDecrypted, |
216 base::Unretained(this))), | 218 base::Unretained(this))), |
217 original_data_(kOriginalData, kOriginalData + kOriginalDataSize), | 219 original_data_(kOriginalData, kOriginalData + kOriginalDataSize), |
218 encrypted_data_(kEncryptedData, | 220 encrypted_data_(kEncryptedData, |
219 kEncryptedData + arraysize(kEncryptedData)), | 221 kEncryptedData + arraysize(kEncryptedData)), |
220 subsample_encrypted_data_( | 222 subsample_encrypted_data_( |
221 kSubsampleEncryptedData, | 223 kSubsampleEncryptedData, |
222 kSubsampleEncryptedData + arraysize(kSubsampleEncryptedData)), | 224 kSubsampleEncryptedData + arraysize(kSubsampleEncryptedData)), |
223 key_id_(kKeyId, kKeyId + arraysize(kKeyId)), | 225 key_id_(kKeyId, kKeyId + arraysize(kKeyId)), |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
300 decryptor_.CreateSession(std::string(), | 302 decryptor_.CreateSession(std::string(), |
301 &key_id[0], | 303 &key_id[0], |
302 key_id.size(), | 304 key_id.size(), |
303 MediaKeys::TEMPORARY_SESSION, | 305 MediaKeys::TEMPORARY_SESSION, |
304 CreateSessionPromise(RESOLVED)); | 306 CreateSessionPromise(RESOLVED)); |
305 // This expects the promise to be called synchronously, which is the case | 307 // This expects the promise to be called synchronously, which is the case |
306 // for AesDecryptor. | 308 // for AesDecryptor. |
307 return web_session_id_; | 309 return web_session_id_; |
308 } | 310 } |
309 | 311 |
310 // Releases the session specified by |session_id|. | 312 // Closes the session specified by |session_id|. |
311 void ReleaseSession(const std::string& session_id) { | 313 void CloseSession(const std::string& session_id) { |
312 EXPECT_CALL(*this, OnSessionClosed(session_id)); | 314 EXPECT_CALL(*this, OnSessionClosed(session_id)); |
313 decryptor_.ReleaseSession(session_id, CreatePromise(RESOLVED)); | 315 decryptor_.CloseSession(session_id, CreatePromise(RESOLVED)); |
316 } | |
317 | |
318 // Removes the session specified by |session_id|. This should simply do a | |
319 // CloseSession(). | |
ddorwin
2014/09/12 21:46:31
Note that this should be updated when prefixed is
jrummell
2014/09/15 18:22:40
Done.
| |
320 void RemoveSession(const std::string& session_id) { | |
321 EXPECT_CALL(*this, OnSessionClosed(session_id)); | |
322 decryptor_.RemoveSession(session_id, CreatePromise(RESOLVED)); | |
314 } | 323 } |
315 | 324 |
316 // Updates the session specified by |session_id| with |key|. |result| | 325 // Updates the session specified by |session_id| with |key|. |result| |
317 // tests that the update succeeds or generates an error. | 326 // tests that the update succeeds or generates an error. |
318 void UpdateSessionAndExpect(std::string session_id, | 327 void UpdateSessionAndExpect(std::string session_id, |
319 const std::string& key, | 328 const std::string& key, |
320 PromiseResult result) { | 329 PromiseResult result) { |
ddorwin
2014/09/12 21:46:31
expected_result?
jrummell
2014/09/15 18:22:40
Done.
| |
321 DCHECK(!key.empty()); | 330 DCHECK(!key.empty()); |
322 | 331 |
332 if (result == RESOLVED) { | |
333 EXPECT_CALL(*this, OnSessionKeysChange(session_id, true)); | |
334 } else { | |
335 EXPECT_CALL(*this, OnSessionKeysChange(_, _)).Times(0); | |
336 } | |
337 | |
323 decryptor_.UpdateSession(session_id, | 338 decryptor_.UpdateSession(session_id, |
324 reinterpret_cast<const uint8*>(key.c_str()), | 339 reinterpret_cast<const uint8*>(key.c_str()), |
325 key.length(), | 340 key.length(), |
326 CreatePromise(result)); | 341 CreatePromise(result)); |
327 } | 342 } |
328 | 343 |
329 void GetUsableKeyIdsAndExpect(const std::string& session_id, | 344 void GetUsableKeyIdsAndExpect(const std::string& session_id, |
330 PromiseResult expected_result, | 345 PromiseResult expected_result, |
331 uint32 expected_count) { | 346 uint32 expected_count) { |
332 decryptor_.GetUsableKeyIds( | 347 decryptor_.GetUsableKeyIds( |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
399 case NO_KEY: | 414 case NO_KEY: |
400 EXPECT_TRUE(decrypted_text.empty()); | 415 EXPECT_TRUE(decrypted_text.empty()); |
401 break; | 416 break; |
402 } | 417 } |
403 } | 418 } |
404 | 419 |
405 MOCK_METHOD3(OnSessionMessage, | 420 MOCK_METHOD3(OnSessionMessage, |
406 void(const std::string& web_session_id, | 421 void(const std::string& web_session_id, |
407 const std::vector<uint8>& message, | 422 const std::vector<uint8>& message, |
408 const GURL& destination_url)); | 423 const GURL& destination_url)); |
424 MOCK_METHOD2(OnSessionKeysChange, | |
425 void(const std::string& web_session_id, | |
426 bool has_additional_usable_key)); | |
409 MOCK_METHOD1(OnSessionClosed, void(const std::string& web_session_id)); | 427 MOCK_METHOD1(OnSessionClosed, void(const std::string& web_session_id)); |
410 | 428 |
411 AesDecryptor decryptor_; | 429 AesDecryptor decryptor_; |
412 AesDecryptor::DecryptCB decrypt_cb_; | 430 AesDecryptor::DecryptCB decrypt_cb_; |
413 std::string web_session_id_; | 431 std::string web_session_id_; |
414 | 432 |
415 // Copy of the vector from the last successful call to | 433 // Copy of the vector from the last successful call to |
416 // OnResolveWithUsableKeyIds(). | 434 // OnResolveWithUsableKeyIds(). |
417 KeyIdsVector useable_key_ids_; | 435 KeyIdsVector useable_key_ids_; |
418 | 436 |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
640 | 658 |
641 std::vector<SubsampleEntry> cypher_only_subsample_entries( | 659 std::vector<SubsampleEntry> cypher_only_subsample_entries( |
642 kSubsampleEntriesCypherOnly, | 660 kSubsampleEntriesCypherOnly, |
643 kSubsampleEntriesCypherOnly + arraysize(kSubsampleEntriesCypherOnly)); | 661 kSubsampleEntriesCypherOnly + arraysize(kSubsampleEntriesCypherOnly)); |
644 | 662 |
645 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 663 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
646 encrypted_data_, key_id_, iv_, cypher_only_subsample_entries); | 664 encrypted_data_, key_id_, iv_, cypher_only_subsample_entries); |
647 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); | 665 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); |
648 } | 666 } |
649 | 667 |
650 TEST_F(AesDecryptorTest, ReleaseSession) { | 668 TEST_F(AesDecryptorTest, CloseSession) { |
651 std::string session_id = CreateSession(key_id_); | 669 std::string session_id = CreateSession(key_id_); |
652 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 670 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
653 encrypted_data_, key_id_, iv_, no_subsample_entries_); | 671 encrypted_data_, key_id_, iv_, no_subsample_entries_); |
672 | |
673 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); | |
674 ASSERT_NO_FATAL_FAILURE( | |
675 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); | |
676 | |
677 CloseSession(session_id); | |
678 } | |
679 | |
680 TEST_F(AesDecryptorTest, RemoveSession) { | |
ddorwin
2014/09/12 21:46:31
ditto
jrummell
2014/09/15 18:22:40
Done.
| |
681 std::string session_id = CreateSession(key_id_); | |
682 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | |
683 encrypted_data_, key_id_, iv_, no_subsample_entries_); | |
654 | 684 |
655 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); | 685 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
656 ASSERT_NO_FATAL_FAILURE( | 686 ASSERT_NO_FATAL_FAILURE( |
657 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); | 687 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); |
658 | 688 |
659 ReleaseSession(session_id); | 689 RemoveSession(session_id); |
660 } | 690 } |
661 | 691 |
662 TEST_F(AesDecryptorTest, NoKeyAfterReleaseSession) { | 692 TEST_F(AesDecryptorTest, NoKeyAfterCloseSession) { |
663 std::string session_id = CreateSession(key_id_); | 693 std::string session_id = CreateSession(key_id_); |
664 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 694 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
665 encrypted_data_, key_id_, iv_, no_subsample_entries_); | 695 encrypted_data_, key_id_, iv_, no_subsample_entries_); |
666 | 696 |
667 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); | 697 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
668 ASSERT_NO_FATAL_FAILURE( | 698 ASSERT_NO_FATAL_FAILURE( |
669 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); | 699 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); |
670 | 700 |
671 ReleaseSession(session_id); | 701 CloseSession(session_id); |
672 ASSERT_NO_FATAL_FAILURE( | 702 ASSERT_NO_FATAL_FAILURE( |
673 DecryptAndExpect(encrypted_buffer, original_data_, NO_KEY)); | 703 DecryptAndExpect(encrypted_buffer, original_data_, NO_KEY)); |
674 } | 704 } |
675 | 705 |
676 TEST_F(AesDecryptorTest, LatestKeyUsed) { | 706 TEST_F(AesDecryptorTest, LatestKeyUsed) { |
677 std::string session_id1 = CreateSession(key_id_); | 707 std::string session_id1 = CreateSession(key_id_); |
678 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 708 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
679 encrypted_data_, key_id_, iv_, no_subsample_entries_); | 709 encrypted_data_, key_id_, iv_, no_subsample_entries_); |
680 | 710 |
681 // Add alternate key, buffer should not be decoded properly. | 711 // Add alternate key, buffer should not be decoded properly. |
682 UpdateSessionAndExpect(session_id1, kKeyAlternateAsJWK, RESOLVED); | 712 UpdateSessionAndExpect(session_id1, kKeyAlternateAsJWK, RESOLVED); |
683 ASSERT_NO_FATAL_FAILURE( | 713 ASSERT_NO_FATAL_FAILURE( |
684 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH)); | 714 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH)); |
685 | 715 |
686 // Create a second session with a correct key value for key_id_. | 716 // Create a second session with a correct key value for key_id_. |
687 std::string session_id2 = CreateSession(key_id_); | 717 std::string session_id2 = CreateSession(key_id_); |
688 UpdateSessionAndExpect(session_id2, kKeyAsJWK, RESOLVED); | 718 UpdateSessionAndExpect(session_id2, kKeyAsJWK, RESOLVED); |
689 | 719 |
690 // Should be able to decode with latest key. | 720 // Should be able to decode with latest key. |
691 ASSERT_NO_FATAL_FAILURE( | 721 ASSERT_NO_FATAL_FAILURE( |
692 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); | 722 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); |
693 } | 723 } |
694 | 724 |
695 TEST_F(AesDecryptorTest, LatestKeyUsedAfterReleaseSession) { | 725 TEST_F(AesDecryptorTest, LatestKeyUsedAfterCloseSession) { |
696 std::string session_id1 = CreateSession(key_id_); | 726 std::string session_id1 = CreateSession(key_id_); |
697 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 727 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
698 encrypted_data_, key_id_, iv_, no_subsample_entries_); | 728 encrypted_data_, key_id_, iv_, no_subsample_entries_); |
699 UpdateSessionAndExpect(session_id1, kKeyAsJWK, RESOLVED); | 729 UpdateSessionAndExpect(session_id1, kKeyAsJWK, RESOLVED); |
700 ASSERT_NO_FATAL_FAILURE( | 730 ASSERT_NO_FATAL_FAILURE( |
701 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); | 731 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); |
702 | 732 |
703 // Create a second session with a different key value for key_id_. | 733 // Create a second session with a different key value for key_id_. |
704 std::string session_id2 = CreateSession(key_id_); | 734 std::string session_id2 = CreateSession(key_id_); |
705 UpdateSessionAndExpect(session_id2, kKeyAlternateAsJWK, RESOLVED); | 735 UpdateSessionAndExpect(session_id2, kKeyAlternateAsJWK, RESOLVED); |
706 | 736 |
707 // Should not be able to decode with new key. | 737 // Should not be able to decode with new key. |
708 ASSERT_NO_FATAL_FAILURE( | 738 ASSERT_NO_FATAL_FAILURE( |
709 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH)); | 739 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH)); |
710 | 740 |
711 // Close second session, should revert to original key. | 741 // Close second session, should revert to original key. |
712 ReleaseSession(session_id2); | 742 CloseSession(session_id2); |
713 ASSERT_NO_FATAL_FAILURE( | 743 ASSERT_NO_FATAL_FAILURE( |
714 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); | 744 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); |
715 } | 745 } |
716 | 746 |
717 TEST_F(AesDecryptorTest, JWKKey) { | 747 TEST_F(AesDecryptorTest, JWKKey) { |
718 std::string session_id = CreateSession(key_id_); | 748 std::string session_id = CreateSession(key_id_); |
719 | 749 |
720 // Try a simple JWK key (i.e. not in a set) | 750 // Try a simple JWK key (i.e. not in a set) |
721 const std::string kJwkSimple = | 751 const std::string kJwkSimple = |
722 "{" | 752 "{" |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
834 "{" | 864 "{" |
835 " \"keys\": [" | 865 " \"keys\": [" |
836 " {" | 866 " {" |
837 " \"kty\": \"oct\"," | 867 " \"kty\": \"oct\"," |
838 " \"kid\": \"\"," | 868 " \"kid\": \"\"," |
839 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" | 869 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" |
840 " }" | 870 " }" |
841 " ]" | 871 " ]" |
842 "}"; | 872 "}"; |
843 UpdateSessionAndExpect(session_id, kJwksWithEmptyKeyId, REJECTED); | 873 UpdateSessionAndExpect(session_id, kJwksWithEmptyKeyId, REJECTED); |
844 ReleaseSession(session_id); | 874 CloseSession(session_id); |
845 } | 875 } |
846 | 876 |
847 TEST_F(AesDecryptorTest, GetKeyIds) { | 877 TEST_F(AesDecryptorTest, GetKeyIds) { |
848 std::vector<uint8> key_id1(kKeyId, kKeyId + arraysize(kKeyId)); | 878 std::vector<uint8> key_id1(kKeyId, kKeyId + arraysize(kKeyId)); |
849 std::vector<uint8> key_id2(kKeyId2, kKeyId2 + arraysize(kKeyId2)); | 879 std::vector<uint8> key_id2(kKeyId2, kKeyId2 + arraysize(kKeyId2)); |
850 | 880 |
851 std::string session_id = CreateSession(key_id_); | 881 std::string session_id = CreateSession(key_id_); |
852 GetUsableKeyIdsAndExpect(session_id, RESOLVED, 0); | 882 GetUsableKeyIdsAndExpect(session_id, RESOLVED, 0); |
853 EXPECT_FALSE(UsableKeyIdsContains(key_id1)); | 883 EXPECT_FALSE(UsableKeyIdsContains(key_id1)); |
854 EXPECT_FALSE(UsableKeyIdsContains(key_id2)); | 884 EXPECT_FALSE(UsableKeyIdsContains(key_id2)); |
855 | 885 |
856 // Add 1 key, verify ID is returned. | 886 // Add 1 key, verify ID is returned. |
857 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); | 887 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
858 GetUsableKeyIdsAndExpect(session_id, RESOLVED, 1); | 888 GetUsableKeyIdsAndExpect(session_id, RESOLVED, 1); |
859 EXPECT_TRUE(UsableKeyIdsContains(key_id1)); | 889 EXPECT_TRUE(UsableKeyIdsContains(key_id1)); |
860 EXPECT_FALSE(UsableKeyIdsContains(key_id2)); | 890 EXPECT_FALSE(UsableKeyIdsContains(key_id2)); |
861 | 891 |
862 // Add second key, verify both IDs returned. | 892 // Add second key, verify both IDs returned. |
863 UpdateSessionAndExpect(session_id, kKey2AsJWK, RESOLVED); | 893 UpdateSessionAndExpect(session_id, kKey2AsJWK, RESOLVED); |
864 GetUsableKeyIdsAndExpect(session_id, RESOLVED, 2); | 894 GetUsableKeyIdsAndExpect(session_id, RESOLVED, 2); |
865 EXPECT_TRUE(UsableKeyIdsContains(key_id1)); | 895 EXPECT_TRUE(UsableKeyIdsContains(key_id1)); |
866 EXPECT_TRUE(UsableKeyIdsContains(key_id2)); | 896 EXPECT_TRUE(UsableKeyIdsContains(key_id2)); |
867 } | 897 } |
868 | 898 |
869 } // namespace media | 899 } // namespace media |
OLD | NEW |