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 // Helper method to make a local copy of |usable_key_ids|. | |
314 void SaveUsableKeys(Unused, Unused, const CdmKeysInfo& usable_key_ids) { | |
xhwang
2015/01/05 22:53:15
If you pass CdmKeysInfo by value, you can probably
jrummell
2015/01/06 02:36:25
MOCK functions don't like ScopedVector, so changed
| |
315 usable_key_ids_.clear(); | |
316 for (const auto& key_id : usable_key_ids) { | |
317 scoped_ptr<CdmKeyInformation> new_key_id(new CdmKeyInformation); | |
318 new_key_id->key_id = key_id->key_id; | |
319 new_key_id->status = key_id->status; | |
320 new_key_id->system_code = key_id->system_code; | |
321 usable_key_ids_.push_back(new_key_id.release()); | |
xhwang
2015/01/05 22:53:15
It seems here we save ALL key IDs regardless of it
jrummell
2015/01/06 02:36:25
Removed.
| |
322 } | |
323 } | |
324 | |
311 // Updates the session specified by |session_id| with |key|. |result| | 325 // Updates the session specified by |session_id| with |key|. |result| |
312 // tests that the update succeeds or generates an error. | 326 // tests that the update succeeds or generates an error. |
313 void UpdateSessionAndExpect(std::string session_id, | 327 void UpdateSessionAndExpect(std::string session_id, |
314 const std::string& key, | 328 const std::string& key, |
315 PromiseResult expected_result) { | 329 PromiseResult expected_result) { |
316 DCHECK(!key.empty()); | 330 DCHECK(!key.empty()); |
317 | 331 |
318 if (expected_result == RESOLVED) { | 332 if (expected_result == RESOLVED) { |
319 EXPECT_CALL(*this, OnSessionKeysChange(session_id, true)); | 333 EXPECT_CALL(*this, OnSessionKeysChange(session_id, true, _)) |
334 .WillOnce(Invoke(this, &AesDecryptorTest::SaveUsableKeys)); | |
320 } else { | 335 } else { |
321 EXPECT_CALL(*this, OnSessionKeysChange(_, _)).Times(0); | 336 EXPECT_CALL(*this, OnSessionKeysChange(_, _, _)).Times(0); |
322 } | 337 } |
323 | 338 |
324 decryptor_.UpdateSession(session_id, | 339 decryptor_.UpdateSession(session_id, |
325 reinterpret_cast<const uint8*>(key.c_str()), | 340 reinterpret_cast<const uint8*>(key.c_str()), |
326 key.length(), | 341 key.length(), |
327 CreatePromise(expected_result)); | 342 CreatePromise(expected_result)); |
328 } | 343 } |
329 | 344 |
345 bool UsableKeyIdsContains(std::vector<uint8> expected) { | |
xhwang
2015/01/05 22:53:15
Ditto about "Usable".
jrummell
2015/01/06 02:36:25
Done.
| |
346 for (const auto& key_id : usable_key_ids_) { | |
347 if (key_id->key_id == expected) | |
348 return true; | |
349 } | |
350 return false; | |
351 } | |
352 | |
330 MOCK_METHOD2(BufferDecrypted, void(Decryptor::Status, | 353 MOCK_METHOD2(BufferDecrypted, void(Decryptor::Status, |
331 const scoped_refptr<DecoderBuffer>&)); | 354 const scoped_refptr<DecoderBuffer>&)); |
332 | 355 |
333 enum DecryptExpectation { | 356 enum DecryptExpectation { |
334 SUCCESS, | 357 SUCCESS, |
335 DATA_MISMATCH, | 358 DATA_MISMATCH, |
336 DATA_AND_SIZE_MISMATCH, | 359 DATA_AND_SIZE_MISMATCH, |
337 DECRYPT_ERROR, | 360 DECRYPT_ERROR, |
338 NO_KEY | 361 NO_KEY |
339 }; | 362 }; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
383 case NO_KEY: | 406 case NO_KEY: |
384 EXPECT_TRUE(decrypted_text.empty()); | 407 EXPECT_TRUE(decrypted_text.empty()); |
385 break; | 408 break; |
386 } | 409 } |
387 } | 410 } |
388 | 411 |
389 MOCK_METHOD3(OnSessionMessage, | 412 MOCK_METHOD3(OnSessionMessage, |
390 void(const std::string& web_session_id, | 413 void(const std::string& web_session_id, |
391 const std::vector<uint8>& message, | 414 const std::vector<uint8>& message, |
392 const GURL& destination_url)); | 415 const GURL& destination_url)); |
393 MOCK_METHOD2(OnSessionKeysChange, | 416 MOCK_METHOD3(OnSessionKeysChange, |
394 void(const std::string& web_session_id, | 417 void(const std::string& web_session_id, |
395 bool has_additional_usable_key)); | 418 bool has_additional_usable_key, |
419 const CdmKeysInfo& keys_info)); | |
396 MOCK_METHOD1(OnSessionClosed, void(const std::string& web_session_id)); | 420 MOCK_METHOD1(OnSessionClosed, void(const std::string& web_session_id)); |
397 | 421 |
398 AesDecryptor decryptor_; | 422 AesDecryptor decryptor_; |
399 AesDecryptor::DecryptCB decrypt_cb_; | 423 AesDecryptor::DecryptCB decrypt_cb_; |
400 std::string web_session_id_; | 424 std::string web_session_id_; |
425 CdmKeysInfo usable_key_ids_; | |
xhwang
2015/01/05 22:53:15
ditto about "usable".
jrummell
2015/01/06 02:36:25
Done.
| |
401 | 426 |
402 // Constants for testing. | 427 // Constants for testing. |
403 const std::vector<uint8> original_data_; | 428 const std::vector<uint8> original_data_; |
404 const std::vector<uint8> encrypted_data_; | 429 const std::vector<uint8> encrypted_data_; |
405 const std::vector<uint8> subsample_encrypted_data_; | 430 const std::vector<uint8> subsample_encrypted_data_; |
406 const std::vector<uint8> key_id_; | 431 const std::vector<uint8> key_id_; |
407 const std::vector<uint8> iv_; | 432 const std::vector<uint8> iv_; |
408 const std::vector<SubsampleEntry> normal_subsample_entries_; | 433 const std::vector<SubsampleEntry> normal_subsample_entries_; |
409 const std::vector<SubsampleEntry> no_subsample_entries_; | 434 const std::vector<SubsampleEntry> no_subsample_entries_; |
410 }; | 435 }; |
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
842 " \"alg\": \"A128KW\"," | 867 " \"alg\": \"A128KW\"," |
843 " \"kid\": \"\"," | 868 " \"kid\": \"\"," |
844 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" | 869 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" |
845 " }" | 870 " }" |
846 " ]" | 871 " ]" |
847 "}"; | 872 "}"; |
848 UpdateSessionAndExpect(session_id, kJwksWithEmptyKeyId, REJECTED); | 873 UpdateSessionAndExpect(session_id, kJwksWithEmptyKeyId, REJECTED); |
849 CloseSession(session_id); | 874 CloseSession(session_id); |
850 } | 875 } |
851 | 876 |
877 TEST_F(AesDecryptorTest, GetKeyIds) { | |
878 std::vector<uint8> key_id1(kKeyId, kKeyId + arraysize(kKeyId)); | |
879 std::vector<uint8> key_id2(kKeyId2, kKeyId2 + arraysize(kKeyId2)); | |
880 | |
881 std::string session_id = CreateSession(key_id_); | |
882 EXPECT_FALSE(UsableKeyIdsContains(key_id1)); | |
883 EXPECT_FALSE(UsableKeyIdsContains(key_id2)); | |
884 | |
885 // Add 1 key, verify it is returned. | |
886 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); | |
887 EXPECT_TRUE(UsableKeyIdsContains(key_id1)); | |
888 EXPECT_FALSE(UsableKeyIdsContains(key_id2)); | |
889 | |
890 // Add second key, verify both IDs returned. | |
891 UpdateSessionAndExpect(session_id, kKey2AsJWK, RESOLVED); | |
892 EXPECT_TRUE(UsableKeyIdsContains(key_id1)); | |
893 EXPECT_TRUE(UsableKeyIdsContains(key_id2)); | |
894 } | |
895 | |
852 } // namespace media | 896 } // namespace media |
OLD | NEW |