OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 6 |
7 #include "base/basictypes.h" | |
7 #include "media/base/decoder_buffer.h" | 8 #include "media/base/decoder_buffer.h" |
8 #include "media/base/decrypt_config.h" | 9 #include "media/base/decrypt_config.h" |
10 #include "media/base/mock_filters.h" | |
9 #include "media/crypto/aes_decryptor.h" | 11 #include "media/crypto/aes_decryptor.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 | 13 |
14 using ::testing::_; | |
15 using ::testing::Gt; | |
16 using ::testing::NotNull; | |
17 using ::testing::SaveArg; | |
18 using ::testing::StrNe; | |
19 | |
12 namespace media { | 20 namespace media { |
13 | 21 |
14 // |kEncryptedData| is encrypted from |kOriginalData| using |kRightKey|, whose | 22 static const char kClearKeySystem[] = "org.w3.clearkey"; |
15 // length is |kKeySize|. Modifying any of these independently would fail the | 23 static const uint8 kInitData[] = { 0x69, 0x6e, 0x69, 0x74 }; |
16 // test. | 24 // |kEncryptedData| is encrypted from |kOriginalData| using |kRightKey|. |
17 static const char kOriginalData[] = "Original data."; | 25 // Modifying any of these independently would fail the test. |
18 static const int kEncryptedDataSize = 16; | 26 static const uint8 kOriginalData[] = { |
19 static const unsigned char kEncryptedData[] = | 27 0x4f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x61, 0x6c, |
20 "\x82\x3A\x76\x92\xEC\x7F\xF8\x85\xEC\x23\x52\xFB\x19\xB1\xB9\x09"; | 28 0x20, 0x64, 0x61, 0x74, 0x61, 0x2e |
21 static const int kKeySize = 16; | 29 }; |
22 static const unsigned char kRightKey[] = "A wonderful key!"; | 30 static const uint8 kEncryptedData[] = { |
23 static const unsigned char kWrongKey[] = "I'm a wrong key."; | 31 0x82, 0x3A, 0x76, 0x92, 0xEC, 0x7F, 0xF8, 0x85, |
24 static const int kKeyIdSize = 9; | 32 0xEC, 0x23, 0x52, 0xFB, 0x19, 0xB1, 0xB9, 0x09 |
25 static const unsigned char kKeyId1[] = "Key ID 1."; | 33 }; |
26 static const unsigned char kKeyId2[] = "Key ID 2."; | 34 static const uint8 kRightKey[] = { |
35 0x41, 0x20, 0x77, 0x6f, 0x6e, 0x64, 0x65, 0x72, | |
36 0x66, 0x75, 0x6c, 0x20, 0x6b, 0x65, 0x79, 0x21 | |
37 }; | |
38 static const uint8 kWrongKey[] = { | |
39 0x49, 0x27, 0x6d, 0x20, 0x61, 0x20, 0x77, 0x72, | |
40 0x6f, 0x6e, 0x67, 0x20, 0x6b, 0x65, 0x79, 0x2e | |
41 }; | |
42 static const uint8 kWrongSizedKey[] = { 0x20, 0x20 }; | |
43 static const uint8 kKeyId1[] = { | |
44 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44, 0x20, 0x31 | |
45 }; | |
46 static const uint8 kKeyId2[] = { | |
47 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44, 0x20, 0x32 | |
48 }; | |
27 | 49 |
28 class AesDecryptorTest : public testing::Test { | 50 class AesDecryptorTest : public testing::Test { |
29 public: | 51 public: |
30 AesDecryptorTest() { | 52 AesDecryptorTest() : decryptor_(&client_) { |
31 encrypted_data_ = DecoderBuffer::CopyFrom( | 53 encrypted_data_ = DecoderBuffer::CopyFrom(kEncryptedData, |
32 kEncryptedData, kEncryptedDataSize); | 54 arraysize(kEncryptedData)); |
55 EXPECT_CALL(client_, KeyMessageMock(kClearKeySystem, StrNe(std::string()), | |
ddorwin
2012/06/14 21:00:13
This is the type of work that would normally be do
xhwang
2012/06/15 01:41:04
Done.
| |
56 NotNull(), Gt(0), "")) | |
57 .WillOnce(SaveArg<1>(&session_id_string_)); | |
58 decryptor_.GenerateKeyRequest(kClearKeySystem, | |
59 kInitData, arraysize(kInitData)); | |
33 } | 60 } |
34 | 61 |
35 protected: | 62 protected: |
36 void SetKeyIdForEncryptedData(const uint8* key_id, int key_id_size) { | 63 template <int KeyIdSize, int KeySize> |
64 void AddKeyAndExpectToSucceed(const uint8 (&key_id)[KeyIdSize], | |
65 const uint8 (&key)[KeySize]) { | |
66 EXPECT_CALL(client_, KeyAdded(kClearKeySystem, session_id_string_)); | |
67 decryptor_.AddKey(kClearKeySystem, key, KeySize, key_id, KeyIdSize, | |
68 session_id_string_); | |
69 } | |
70 | |
71 template <int KeyIdSize, int KeySize> | |
72 void AddKeyAndExpectToFail(const uint8 (&key_id)[KeyIdSize], | |
73 const uint8 (&key)[KeySize]) { | |
74 EXPECT_CALL(client_, KeyError(kClearKeySystem, session_id_string_, | |
75 AesDecryptor::kUnknownError, 0)); | |
76 decryptor_.AddKey(kClearKeySystem, key, KeySize, key_id, KeyIdSize, | |
77 session_id_string_); | |
78 } | |
79 | |
80 template <int KeyIdSize> | |
81 void SetKeyIdForEncryptedData(const uint8 (&key_id)[KeyIdSize]) { | |
37 encrypted_data_->SetDecryptConfig( | 82 encrypted_data_->SetDecryptConfig( |
38 scoped_ptr<DecryptConfig>(new DecryptConfig(key_id, key_id_size))); | 83 scoped_ptr<DecryptConfig>(new DecryptConfig(key_id, KeyIdSize))); |
39 } | 84 } |
40 | 85 |
41 void DecryptAndExpectToSucceed() { | 86 void DecryptAndExpectToSucceed() { |
42 scoped_refptr<DecoderBuffer> decrypted = | 87 scoped_refptr<DecoderBuffer> decrypted = |
43 decryptor_.Decrypt(encrypted_data_); | 88 decryptor_.Decrypt(encrypted_data_); |
44 ASSERT_TRUE(decrypted); | 89 ASSERT_TRUE(decrypted); |
45 int data_length = sizeof(kOriginalData) - 1; | 90 int data_length = sizeof(kOriginalData); |
46 ASSERT_EQ(data_length, decrypted->GetDataSize()); | 91 ASSERT_EQ(data_length, decrypted->GetDataSize()); |
47 EXPECT_EQ(0, memcmp(kOriginalData, decrypted->GetData(), data_length)); | 92 EXPECT_EQ(0, memcmp(kOriginalData, decrypted->GetData(), data_length)); |
48 } | 93 } |
49 | 94 |
50 void DecryptAndExpectToFail() { | 95 void DecryptAndExpectToFail() { |
51 scoped_refptr<DecoderBuffer> decrypted = | 96 scoped_refptr<DecoderBuffer> decrypted = |
52 decryptor_.Decrypt(encrypted_data_); | 97 decryptor_.Decrypt(encrypted_data_); |
53 EXPECT_FALSE(decrypted); | 98 EXPECT_FALSE(decrypted); |
54 } | 99 } |
55 | 100 |
56 scoped_refptr<DecoderBuffer> encrypted_data_; | 101 scoped_refptr<DecoderBuffer> encrypted_data_; |
102 MockDecryptorClient client_; | |
57 AesDecryptor decryptor_; | 103 AesDecryptor decryptor_; |
104 std::string session_id_string_; | |
58 }; | 105 }; |
59 | 106 |
60 TEST_F(AesDecryptorTest, NormalDecryption) { | 107 TEST_F(AesDecryptorTest, NormalDecryption) { |
61 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); | 108 AddKeyAndExpectToSucceed(kKeyId1, kRightKey); |
62 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); | 109 SetKeyIdForEncryptedData(kKeyId1); |
63 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); | 110 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); |
64 } | 111 } |
65 | 112 |
66 TEST_F(AesDecryptorTest, WrongKey) { | 113 TEST_F(AesDecryptorTest, WrongKey) { |
67 decryptor_.AddKey(kKeyId1, kKeyIdSize, kWrongKey, kKeySize); | 114 AddKeyAndExpectToSucceed(kKeyId1, kWrongKey); |
68 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); | 115 SetKeyIdForEncryptedData(kKeyId1); |
69 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail()); | 116 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail()); |
70 } | 117 } |
71 | 118 |
72 TEST_F(AesDecryptorTest, MultipleKeys) { | 119 TEST_F(AesDecryptorTest, MultipleKeys) { |
73 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); | 120 AddKeyAndExpectToSucceed(kKeyId1, kRightKey); |
74 decryptor_.AddKey(kKeyId2, kKeyIdSize, kWrongKey, kKeySize); | 121 AddKeyAndExpectToSucceed(kKeyId2, kWrongKey); |
75 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); | 122 SetKeyIdForEncryptedData(kKeyId1); |
76 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); | 123 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); |
77 } | 124 } |
78 | 125 |
79 TEST_F(AesDecryptorTest, KeyReplacement) { | 126 TEST_F(AesDecryptorTest, KeyReplacement) { |
80 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); | 127 SetKeyIdForEncryptedData(kKeyId1); |
81 decryptor_.AddKey(kKeyId1, kKeyIdSize, kWrongKey, kKeySize); | 128 AddKeyAndExpectToSucceed(kKeyId1, kWrongKey); |
82 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail()); | 129 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail()); |
83 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); | 130 AddKeyAndExpectToSucceed(kKeyId1, kRightKey); |
84 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); | 131 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); |
85 } | 132 } |
86 | 133 |
134 TEST_F(AesDecryptorTest, WrongSizedKey) { | |
135 AddKeyAndExpectToFail(kKeyId1, kWrongSizedKey); | |
136 } | |
137 | |
87 } // media | 138 } // media |
OLD | NEW |