| OLD | NEW |
| 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/cdm_adapter.h" | 5 #include "media/cdm/cdm_adapter.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "media/base/cdm_callback_promise.h" | 14 #include "media/base/cdm_callback_promise.h" |
| 15 #include "media/base/cdm_key_information.h" | 15 #include "media/base/cdm_key_information.h" |
| 16 #include "media/base/media_keys.h" | 16 #include "media/base/media_keys.h" |
| 17 #include "media/base/mock_filters.h" |
| 17 #include "media/cdm/cdm_file_io.h" | 18 #include "media/cdm/cdm_file_io.h" |
| 18 #include "media/cdm/external_clear_key_test_helper.h" | 19 #include "media/cdm/external_clear_key_test_helper.h" |
| 19 #include "media/cdm/simple_cdm_allocator.h" | 20 #include "media/cdm/simple_cdm_allocator.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 23 |
| 23 using ::testing::_; | 24 using ::testing::_; |
| 24 using ::testing::SaveArg; | 25 using ::testing::SaveArg; |
| 26 using ::testing::StrictMock; |
| 27 |
| 25 MATCHER(IsNotEmpty, "") { | 28 MATCHER(IsNotEmpty, "") { |
| 26 return !arg.empty(); | 29 return !arg.empty(); |
| 27 } | 30 } |
| 28 | 31 |
| 29 // TODO(jrummell): These tests are a subset of those in aes_decryptor_unittest. | 32 // TODO(jrummell): These tests are a subset of those in aes_decryptor_unittest. |
| 30 // Refactor aes_decryptor_unittest.cc to handle AesDecryptor directly and | 33 // Refactor aes_decryptor_unittest.cc to handle AesDecryptor directly and |
| 31 // via CdmAdapter once CdmAdapter supports decrypting functionality. There | 34 // via CdmAdapter once CdmAdapter supports decrypting functionality. There |
| 32 // will also be tests that only CdmAdapter supports, like file IO, which | 35 // will also be tests that only CdmAdapter supports, like file IO, which |
| 33 // will need to be handled separately. | 36 // will need to be handled separately. |
| 34 | 37 |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 protected: | 84 protected: |
| 82 // Initializes the adapter. |expected_result| tests that the call succeeds | 85 // Initializes the adapter. |expected_result| tests that the call succeeds |
| 83 // or generates an error. | 86 // or generates an error. |
| 84 void InitializeAndExpect(base::FilePath library_path, | 87 void InitializeAndExpect(base::FilePath library_path, |
| 85 ExpectedResult expected_result) { | 88 ExpectedResult expected_result) { |
| 86 CdmConfig cdm_config; // default settings of false are sufficient. | 89 CdmConfig cdm_config; // default settings of false are sufficient. |
| 87 std::unique_ptr<CdmAllocator> allocator(new SimpleCdmAllocator()); | 90 std::unique_ptr<CdmAllocator> allocator(new SimpleCdmAllocator()); |
| 88 CdmAdapter::Create( | 91 CdmAdapter::Create( |
| 89 helper_.KeySystemName(), library_path, cdm_config, std::move(allocator), | 92 helper_.KeySystemName(), library_path, cdm_config, std::move(allocator), |
| 90 base::Bind(&CdmAdapterTest::CreateCdmFileIO, base::Unretained(this)), | 93 base::Bind(&CdmAdapterTest::CreateCdmFileIO, base::Unretained(this)), |
| 91 base::Bind(&CdmAdapterTest::OnSessionMessage, base::Unretained(this)), | 94 base::Bind(&MockCdmClient::OnSessionMessage, |
| 92 base::Bind(&CdmAdapterTest::OnSessionClosed, base::Unretained(this)), | 95 base::Unretained(&cdm_client_)), |
| 93 base::Bind(&CdmAdapterTest::OnSessionKeysChange, | 96 base::Bind(&MockCdmClient::OnSessionClosed, |
| 94 base::Unretained(this)), | 97 base::Unretained(&cdm_client_)), |
| 95 base::Bind(&CdmAdapterTest::OnSessionExpirationUpdate, | 98 base::Bind(&MockCdmClient::OnSessionKeysChange, |
| 96 base::Unretained(this)), | 99 base::Unretained(&cdm_client_)), |
| 100 base::Bind(&MockCdmClient::OnSessionExpirationUpdate, |
| 101 base::Unretained(&cdm_client_)), |
| 97 base::Bind(&CdmAdapterTest::OnCdmCreated, base::Unretained(this), | 102 base::Bind(&CdmAdapterTest::OnCdmCreated, base::Unretained(this), |
| 98 expected_result)); | 103 expected_result)); |
| 99 RunUntilIdle(); | 104 RunUntilIdle(); |
| 100 } | 105 } |
| 101 | 106 |
| 102 // Creates a new session using |key_id|. |session_id_| will be set | 107 // Creates a new session using |key_id|. |session_id_| will be set |
| 103 // when the promise is resolved. |expected_result| tests that | 108 // when the promise is resolved. |expected_result| tests that |
| 104 // CreateSessionAndGenerateRequest() succeeds or generates an error. | 109 // CreateSessionAndGenerateRequest() succeeds or generates an error. |
| 105 void CreateSessionAndExpect(EmeInitDataType data_type, | 110 void CreateSessionAndExpect(EmeInitDataType data_type, |
| 106 const std::vector<uint8_t>& key_id, | 111 const std::vector<uint8_t>& key_id, |
| 107 ExpectedResult expected_result) { | 112 ExpectedResult expected_result) { |
| 108 DCHECK(!key_id.empty()); | 113 DCHECK(!key_id.empty()); |
| 109 | 114 |
| 110 if (expected_result == SUCCESS) { | 115 if (expected_result == SUCCESS) { |
| 111 EXPECT_CALL(*this, OnSessionMessage(IsNotEmpty(), _, _)); | 116 EXPECT_CALL(cdm_client_, OnSessionMessage(IsNotEmpty(), _, _)); |
| 112 } | 117 } |
| 113 | 118 |
| 114 adapter_->CreateSessionAndGenerateRequest( | 119 adapter_->CreateSessionAndGenerateRequest( |
| 115 MediaKeys::TEMPORARY_SESSION, data_type, key_id, | 120 MediaKeys::TEMPORARY_SESSION, data_type, key_id, |
| 116 CreateSessionPromise(expected_result)); | 121 CreateSessionPromise(expected_result)); |
| 117 RunUntilIdle(); | 122 RunUntilIdle(); |
| 118 } | 123 } |
| 119 | 124 |
| 120 // Loads the session specified by |session_id|. |expected_result| tests | 125 // Loads the session specified by |session_id|. |expected_result| tests |
| 121 // that LoadSession() succeeds or generates an error. | 126 // that LoadSession() succeeds or generates an error. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 132 // Updates the session specified by |session_id| with |key|. |expected_result| | 137 // Updates the session specified by |session_id| with |key|. |expected_result| |
| 133 // tests that the update succeeds or generates an error. |new_key_expected| | 138 // tests that the update succeeds or generates an error. |new_key_expected| |
| 134 // is the expected parameter when the SessionKeysChange event happens. | 139 // is the expected parameter when the SessionKeysChange event happens. |
| 135 void UpdateSessionAndExpect(std::string session_id, | 140 void UpdateSessionAndExpect(std::string session_id, |
| 136 const std::string& key, | 141 const std::string& key, |
| 137 ExpectedResult expected_result, | 142 ExpectedResult expected_result, |
| 138 bool new_key_expected) { | 143 bool new_key_expected) { |
| 139 DCHECK(!key.empty()); | 144 DCHECK(!key.empty()); |
| 140 | 145 |
| 141 if (expected_result == SUCCESS) { | 146 if (expected_result == SUCCESS) { |
| 142 EXPECT_CALL(*this, | 147 EXPECT_CALL(cdm_client_, |
| 143 OnSessionKeysChangeCalled(session_id, new_key_expected)); | 148 OnSessionKeysChangeCalled(session_id, new_key_expected)); |
| 144 } else { | 149 } else { |
| 145 EXPECT_CALL(*this, OnSessionKeysChangeCalled(_, _)).Times(0); | 150 EXPECT_CALL(cdm_client_, OnSessionKeysChangeCalled(_, _)).Times(0); |
| 146 } | 151 } |
| 147 | 152 |
| 148 adapter_->UpdateSession(session_id, | 153 adapter_->UpdateSession(session_id, |
| 149 std::vector<uint8_t>(key.begin(), key.end()), | 154 std::vector<uint8_t>(key.begin(), key.end()), |
| 150 CreatePromise(expected_result)); | 155 CreatePromise(expected_result)); |
| 151 RunUntilIdle(); | 156 RunUntilIdle(); |
| 152 } | 157 } |
| 153 | 158 |
| 154 base::FilePath ExternalClearKeyLibrary() { return helper_.LibraryPath(); } | 159 base::FilePath ExternalClearKeyLibrary() { return helper_.LibraryPath(); } |
| 155 | 160 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 } | 215 } |
| 211 | 216 |
| 212 // Methods used for promise resolved/rejected. | 217 // Methods used for promise resolved/rejected. |
| 213 MOCK_METHOD0(OnResolve, void()); | 218 MOCK_METHOD0(OnResolve, void()); |
| 214 MOCK_METHOD1(OnResolveWithSession, void(const std::string& session_id)); | 219 MOCK_METHOD1(OnResolveWithSession, void(const std::string& session_id)); |
| 215 MOCK_METHOD3(OnReject, | 220 MOCK_METHOD3(OnReject, |
| 216 void(CdmPromise::Exception exception_code, | 221 void(CdmPromise::Exception exception_code, |
| 217 uint32_t system_code, | 222 uint32_t system_code, |
| 218 const std::string& error_message)); | 223 const std::string& error_message)); |
| 219 | 224 |
| 220 // Methods used for the events possibly generated by CdmAdapater. | 225 StrictMock<MockCdmClient> cdm_client_; |
| 221 MOCK_METHOD3(OnSessionMessage, | |
| 222 void(const std::string& session_id, | |
| 223 MediaKeys::MessageType message_type, | |
| 224 const std::vector<uint8_t>& message)); | |
| 225 MOCK_METHOD1(OnSessionClosed, void(const std::string& session_id)); | |
| 226 MOCK_METHOD2(OnSessionKeysChangeCalled, | |
| 227 void(const std::string& session_id, | |
| 228 bool has_additional_usable_key)); | |
| 229 void OnSessionKeysChange(const std::string& session_id, | |
| 230 bool has_additional_usable_key, | |
| 231 CdmKeysInfo keys_info) { | |
| 232 // MOCK methods don't like CdmKeysInfo. | |
| 233 OnSessionKeysChangeCalled(session_id, has_additional_usable_key); | |
| 234 } | |
| 235 MOCK_METHOD2(OnSessionExpirationUpdate, | |
| 236 void(const std::string& session_id, base::Time new_expiry_time)); | |
| 237 | 226 |
| 238 // Helper class to load/unload External Clear Key Library. | 227 // Helper class to load/unload External Clear Key Library. |
| 239 ExternalClearKeyTestHelper helper_; | 228 ExternalClearKeyTestHelper helper_; |
| 240 | 229 |
| 241 // Keep track of the loaded CDM. | 230 // Keep track of the loaded CDM. |
| 242 scoped_refptr<MediaKeys> adapter_; | 231 scoped_refptr<MediaKeys> adapter_; |
| 243 | 232 |
| 244 // |session_id_| is the latest result of calling CreateSession(). | 233 // |session_id_| is the latest result of calling CreateSession(). |
| 245 std::string session_id_; | 234 std::string session_id_; |
| 246 | 235 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 TEST_F(CdmAdapterTest, UpdateSessionWithBadData) { | 303 TEST_F(CdmAdapterTest, UpdateSessionWithBadData) { |
| 315 InitializeAndExpect(ExternalClearKeyLibrary(), SUCCESS); | 304 InitializeAndExpect(ExternalClearKeyLibrary(), SUCCESS); |
| 316 | 305 |
| 317 std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId)); | 306 std::vector<uint8_t> key_id(kKeyId, kKeyId + arraysize(kKeyId)); |
| 318 CreateSessionAndExpect(EmeInitDataType::WEBM, key_id, SUCCESS); | 307 CreateSessionAndExpect(EmeInitDataType::WEBM, key_id, SUCCESS); |
| 319 | 308 |
| 320 UpdateSessionAndExpect(SessionId(), "random data", FAILURE, true); | 309 UpdateSessionAndExpect(SessionId(), "random data", FAILURE, true); |
| 321 } | 310 } |
| 322 | 311 |
| 323 } // namespace media | 312 } // namespace media |
| OLD | NEW |