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" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "media/base/cdm_callback_promise.h" | 12 #include "media/base/cdm_callback_promise.h" |
| 13 #include "media/base/cdm_key_information.h" |
13 #include "media/base/decoder_buffer.h" | 14 #include "media/base/decoder_buffer.h" |
14 #include "media/base/decrypt_config.h" | 15 #include "media/base/decrypt_config.h" |
15 #include "media/base/mock_filters.h" | 16 #include "media/base/mock_filters.h" |
16 #include "media/cdm/aes_decryptor.h" | 17 #include "media/cdm/aes_decryptor.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 using ::testing::_; | 21 using ::testing::_; |
21 using ::testing::Gt; | 22 using ::testing::Gt; |
22 using ::testing::IsNull; | 23 using ::testing::IsNull; |
23 using ::testing::NotNull; | 24 using ::testing::NotNull; |
24 using ::testing::SaveArg; | 25 using ::testing::SaveArg; |
25 using ::testing::StrNe; | 26 using ::testing::StrNe; |
| 27 using ::testing::Unused; |
26 | 28 |
27 MATCHER(IsEmpty, "") { return arg.empty(); } | 29 MATCHER(IsEmpty, "") { return arg.empty(); } |
28 MATCHER(IsNotEmpty, "") { return !arg.empty(); } | 30 MATCHER(IsNotEmpty, "") { return !arg.empty(); } |
29 MATCHER(IsJSONDictionary, "") { | 31 MATCHER(IsJSONDictionary, "") { |
30 std::string result(arg.begin(), arg.end()); | 32 std::string result(arg.begin(), arg.end()); |
31 scoped_ptr<base::Value> root(base::JSONReader().ReadToValue(result)); | 33 scoped_ptr<base::Value> root(base::JSONReader().ReadToValue(result)); |
32 return (root.get() && root->GetType() == base::Value::TYPE_DICTIONARY); | 34 return (root.get() && root->GetType() == base::Value::TYPE_DICTIONARY); |
33 } | 35 } |
34 | 36 |
35 class GURL; | 37 class GURL; |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 | 303 |
302 // Removes the session specified by |session_id|. This should simply do a | 304 // Removes the session specified by |session_id|. This should simply do a |
303 // CloseSession(). | 305 // CloseSession(). |
304 // TODO(jrummell): Clean this up when the prefixed API is removed. | 306 // TODO(jrummell): Clean this up when the prefixed API is removed. |
305 // http://crbug.com/249976. | 307 // http://crbug.com/249976. |
306 void RemoveSession(const std::string& session_id) { | 308 void RemoveSession(const std::string& session_id) { |
307 EXPECT_CALL(*this, OnSessionClosed(session_id)); | 309 EXPECT_CALL(*this, OnSessionClosed(session_id)); |
308 decryptor_.RemoveSession(session_id, CreatePromise(RESOLVED)); | 310 decryptor_.RemoveSession(session_id, CreatePromise(RESOLVED)); |
309 } | 311 } |
310 | 312 |
| 313 MOCK_METHOD2(OnSessionKeysChangeCalled, |
| 314 void(const std::string& web_session_id, |
| 315 bool has_additional_usable_key)); |
| 316 |
| 317 void OnSessionKeysChange(const std::string& web_session_id, |
| 318 bool has_additional_usable_key, |
| 319 CdmKeysInfo keys_info) { |
| 320 keys_info_.swap(keys_info); |
| 321 OnSessionKeysChangeCalled(web_session_id, has_additional_usable_key); |
| 322 } |
| 323 |
311 // Updates the session specified by |session_id| with |key|. |result| | 324 // Updates the session specified by |session_id| with |key|. |result| |
312 // tests that the update succeeds or generates an error. | 325 // tests that the update succeeds or generates an error. |
313 void UpdateSessionAndExpect(std::string session_id, | 326 void UpdateSessionAndExpect(std::string session_id, |
314 const std::string& key, | 327 const std::string& key, |
315 PromiseResult expected_result) { | 328 PromiseResult expected_result) { |
316 DCHECK(!key.empty()); | 329 DCHECK(!key.empty()); |
317 | 330 |
318 if (expected_result == RESOLVED) { | 331 if (expected_result == RESOLVED) { |
319 EXPECT_CALL(*this, OnSessionKeysChange(session_id, true)); | 332 EXPECT_CALL(*this, OnSessionKeysChangeCalled(session_id, true)); |
320 } else { | 333 } else { |
321 EXPECT_CALL(*this, OnSessionKeysChange(_, _)).Times(0); | 334 EXPECT_CALL(*this, OnSessionKeysChangeCalled(_, _)).Times(0); |
322 } | 335 } |
323 | 336 |
324 decryptor_.UpdateSession(session_id, | 337 decryptor_.UpdateSession(session_id, |
325 reinterpret_cast<const uint8*>(key.c_str()), | 338 reinterpret_cast<const uint8*>(key.c_str()), |
326 key.length(), | 339 key.length(), |
327 CreatePromise(expected_result)); | 340 CreatePromise(expected_result)); |
328 } | 341 } |
329 | 342 |
| 343 bool KeysInfoContains(std::vector<uint8> expected) { |
| 344 for (const auto& key_id : keys_info_) { |
| 345 if (key_id->key_id == expected) |
| 346 return true; |
| 347 } |
| 348 return false; |
| 349 } |
| 350 |
330 MOCK_METHOD2(BufferDecrypted, void(Decryptor::Status, | 351 MOCK_METHOD2(BufferDecrypted, void(Decryptor::Status, |
331 const scoped_refptr<DecoderBuffer>&)); | 352 const scoped_refptr<DecoderBuffer>&)); |
332 | 353 |
333 enum DecryptExpectation { | 354 enum DecryptExpectation { |
334 SUCCESS, | 355 SUCCESS, |
335 DATA_MISMATCH, | 356 DATA_MISMATCH, |
336 DATA_AND_SIZE_MISMATCH, | 357 DATA_AND_SIZE_MISMATCH, |
337 DECRYPT_ERROR, | 358 DECRYPT_ERROR, |
338 NO_KEY | 359 NO_KEY |
339 }; | 360 }; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 case NO_KEY: | 404 case NO_KEY: |
384 EXPECT_TRUE(decrypted_text.empty()); | 405 EXPECT_TRUE(decrypted_text.empty()); |
385 break; | 406 break; |
386 } | 407 } |
387 } | 408 } |
388 | 409 |
389 MOCK_METHOD3(OnSessionMessage, | 410 MOCK_METHOD3(OnSessionMessage, |
390 void(const std::string& web_session_id, | 411 void(const std::string& web_session_id, |
391 const std::vector<uint8>& message, | 412 const std::vector<uint8>& message, |
392 const GURL& destination_url)); | 413 const GURL& destination_url)); |
393 MOCK_METHOD2(OnSessionKeysChange, | |
394 void(const std::string& web_session_id, | |
395 bool has_additional_usable_key)); | |
396 MOCK_METHOD1(OnSessionClosed, void(const std::string& web_session_id)); | 414 MOCK_METHOD1(OnSessionClosed, void(const std::string& web_session_id)); |
397 | 415 |
398 AesDecryptor decryptor_; | 416 AesDecryptor decryptor_; |
399 AesDecryptor::DecryptCB decrypt_cb_; | 417 AesDecryptor::DecryptCB decrypt_cb_; |
400 std::string web_session_id_; | 418 std::string web_session_id_; |
| 419 CdmKeysInfo keys_info_; |
401 | 420 |
402 // Constants for testing. | 421 // Constants for testing. |
403 const std::vector<uint8> original_data_; | 422 const std::vector<uint8> original_data_; |
404 const std::vector<uint8> encrypted_data_; | 423 const std::vector<uint8> encrypted_data_; |
405 const std::vector<uint8> subsample_encrypted_data_; | 424 const std::vector<uint8> subsample_encrypted_data_; |
406 const std::vector<uint8> key_id_; | 425 const std::vector<uint8> key_id_; |
407 const std::vector<uint8> iv_; | 426 const std::vector<uint8> iv_; |
408 const std::vector<SubsampleEntry> normal_subsample_entries_; | 427 const std::vector<SubsampleEntry> normal_subsample_entries_; |
409 const std::vector<SubsampleEntry> no_subsample_entries_; | 428 const std::vector<SubsampleEntry> no_subsample_entries_; |
410 }; | 429 }; |
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
842 " \"alg\": \"A128KW\"," | 861 " \"alg\": \"A128KW\"," |
843 " \"kid\": \"\"," | 862 " \"kid\": \"\"," |
844 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" | 863 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" |
845 " }" | 864 " }" |
846 " ]" | 865 " ]" |
847 "}"; | 866 "}"; |
848 UpdateSessionAndExpect(session_id, kJwksWithEmptyKeyId, REJECTED); | 867 UpdateSessionAndExpect(session_id, kJwksWithEmptyKeyId, REJECTED); |
849 CloseSession(session_id); | 868 CloseSession(session_id); |
850 } | 869 } |
851 | 870 |
| 871 TEST_F(AesDecryptorTest, GetKeyIds) { |
| 872 std::vector<uint8> key_id1(kKeyId, kKeyId + arraysize(kKeyId)); |
| 873 std::vector<uint8> key_id2(kKeyId2, kKeyId2 + arraysize(kKeyId2)); |
| 874 |
| 875 std::string session_id = CreateSession(key_id_); |
| 876 EXPECT_FALSE(KeysInfoContains(key_id1)); |
| 877 EXPECT_FALSE(KeysInfoContains(key_id2)); |
| 878 |
| 879 // Add 1 key, verify it is returned. |
| 880 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 881 EXPECT_TRUE(KeysInfoContains(key_id1)); |
| 882 EXPECT_FALSE(KeysInfoContains(key_id2)); |
| 883 |
| 884 // Add second key, verify both IDs returned. |
| 885 UpdateSessionAndExpect(session_id, kKey2AsJWK, RESOLVED); |
| 886 EXPECT_TRUE(KeysInfoContains(key_id1)); |
| 887 EXPECT_TRUE(KeysInfoContains(key_id2)); |
| 888 } |
| 889 |
852 } // namespace media | 890 } // namespace media |
OLD | NEW |