OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "webkit/media/crypto/proxy_decryptor.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/bind.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/message_loop.h" | |
11 #include "media/base/decoder_buffer.h" | |
12 #include "media/base/decrypt_config.h" | |
13 #include "media/base/mock_filters.h" | |
14 #include "testing/gmock/include/gmock/gmock.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 using ::testing::_; | |
18 using ::testing::AtLeast; | |
19 using ::testing::InvokeWithoutArgs; | |
20 using ::testing::IsNull; | |
21 using ::testing::NotNull; | |
22 | |
23 using media::DecoderBuffer; | |
24 using media::DecryptConfig; | |
25 using media::Decryptor; | |
26 | |
27 namespace webkit_media { | |
28 | |
29 static const uint8 kFakeKeyId[] = { 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44 }; | |
30 static const uint8 kFakeIv[DecryptConfig::kDecryptionKeySize] = { 0 }; | |
31 static const uint8 kFakeCheckSum[] = { 0, 0 }; | |
32 static const char kFakeKeySystem[] = "system.key.fake"; | |
33 static const char kFakeSessionId[] = "FakeSessionId"; | |
34 static const uint8 kFakeKey[] = { 0x4b, 0x65, 0x79 }; | |
35 static const uint8 kEncryptedData[] = { 0x65, 0x6E, 0x63, 0x72, 0x79 }; | |
36 static const uint8 kDecryptedData[] = { 0x64, 0x65, 0x63, 0x72, 0x79 }; | |
37 | |
38 // Creates a fake non-empty encrypted buffer. | |
39 static scoped_refptr<DecoderBuffer> CreateFakeEncryptedBuffer() { | |
40 const int encrypted_frame_offset = 1; // This should be non-zero. | |
41 scoped_refptr<DecoderBuffer> encrypted_buffer = | |
42 DecoderBuffer::CopyFrom(kEncryptedData, arraysize(kEncryptedData)); | |
43 encrypted_buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>( | |
44 new DecryptConfig(kFakeKeyId, arraysize(kFakeKeyId), | |
45 kFakeIv, DecryptConfig::kDecryptionKeySize, | |
46 kFakeCheckSum, arraysize(kFakeCheckSum), | |
47 encrypted_frame_offset))); | |
48 return encrypted_buffer; | |
49 } | |
50 | |
51 ACTION_P2(RunDecryptCB, status, buffer) { | |
52 arg1.Run(status, buffer); | |
53 } | |
54 | |
55 // Tests the interaction between external Decryptor calls and concrete Decryptor | |
56 // implementations. This test is not interested in any specific Decryptor | |
57 // implementation. A MockDecryptor is used here to serve this purpose. | |
58 class ProxyDecryptorTest : public testing::Test { | |
59 public: | |
60 ProxyDecryptorTest() | |
61 : decryptor_(&client_, NULL, NULL), | |
62 real_decryptor_(new media::MockDecryptor()), | |
63 encrypted_buffer_(CreateFakeEncryptedBuffer()), | |
64 decrypted_buffer_(DecoderBuffer::CopyFrom(kDecryptedData, | |
65 arraysize(kDecryptedData))), | |
66 null_buffer_(scoped_refptr<DecoderBuffer>()), | |
67 decrypt_cb_(base::Bind(&ProxyDecryptorTest::BufferDecrypted, | |
68 base::Unretained(this))) { | |
69 } | |
70 | |
71 // Instead of calling ProxyDecryptor::GenerateKeyRequest() here to create a | |
72 // real Decryptor, inject a MockDecryptor for testing purpose. | |
73 void GenerateKeyRequest() { | |
74 decryptor_.set_decryptor(scoped_ptr<Decryptor>(real_decryptor_)); | |
75 } | |
76 | |
77 // Since we are using the MockDecryptor, we can simulate any decryption | |
78 // behavior we want. Therefore, we do not care which key is really added, | |
79 // hence always use fake key IDs and keys. | |
80 void AddKey() { | |
81 EXPECT_CALL(*real_decryptor_, AddKey(kFakeKeySystem, | |
82 kFakeKeyId, arraysize(kFakeKeyId), | |
83 kFakeKey, arraysize(kFakeKey), | |
84 kFakeSessionId)); | |
85 decryptor_.AddKey(kFakeKeySystem, | |
86 kFakeKeyId, arraysize(kFakeKeyId), | |
87 kFakeKey, arraysize(kFakeKey), | |
88 kFakeSessionId); | |
89 } | |
90 | |
91 void StopMessageLoop() { | |
92 message_loop_.PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
93 } | |
94 | |
95 MOCK_METHOD2(BufferDecrypted, void(Decryptor::DecryptStatus, | |
96 const scoped_refptr<DecoderBuffer>&)); | |
97 | |
98 protected: | |
99 MessageLoop message_loop_; | |
100 media::MockDecryptorClient client_; | |
101 ProxyDecryptor decryptor_; | |
102 media::MockDecryptor* real_decryptor_; | |
103 scoped_refptr<DecoderBuffer> encrypted_buffer_; | |
104 scoped_refptr<DecoderBuffer> decrypted_buffer_; | |
105 scoped_refptr<DecoderBuffer> null_buffer_; | |
106 Decryptor::DecryptCB decrypt_cb_; | |
107 }; | |
scherkus (not reviewing)
2012/07/28 23:07:19
DISALLLOW etc
xhwang
2012/07/30 19:58:10
Done.
| |
108 | |
109 // Tests a typical use case: GKR(), AddKey() and Decrypt() succeeds. | |
110 TEST_F(ProxyDecryptorTest, NormalDecryption_Success) { | |
111 GenerateKeyRequest(); | |
112 AddKey(); | |
113 | |
114 EXPECT_CALL(*real_decryptor_, Decrypt(encrypted_buffer_, _)) | |
115 .WillOnce(RunDecryptCB(Decryptor::kSuccess, decrypted_buffer_)); | |
116 EXPECT_CALL(*this, BufferDecrypted(Decryptor::kSuccess, decrypted_buffer_)); | |
117 decryptor_.Decrypt(encrypted_buffer_, decrypt_cb_); | |
118 } | |
119 | |
120 // Tests the case where Decrypt() fails. | |
121 TEST_F(ProxyDecryptorTest, NormalDecryption_Error) { | |
122 GenerateKeyRequest(); | |
123 AddKey(); | |
124 | |
125 EXPECT_CALL(*real_decryptor_, Decrypt(encrypted_buffer_, _)) | |
126 .WillOnce(RunDecryptCB(Decryptor::kError, null_buffer_)); | |
127 EXPECT_CALL(*this, BufferDecrypted(Decryptor::kError, IsNull())); | |
128 decryptor_.Decrypt(encrypted_buffer_, decrypt_cb_); | |
129 } | |
130 | |
131 // Tests the case where no key is available for decryption. | |
132 TEST_F(ProxyDecryptorTest, NormalDecryption_NoKey) { | |
133 GenerateKeyRequest(); | |
134 | |
135 EXPECT_CALL(*real_decryptor_, Decrypt(encrypted_buffer_, _)) | |
136 .WillOnce(RunDecryptCB(Decryptor::kNoKey, null_buffer_)); | |
137 EXPECT_CALL(client_, NeedKeyMock("", "", NotNull(), arraysize(kFakeKeyId))); | |
138 decryptor_.Decrypt(encrypted_buffer_, decrypt_cb_); | |
139 } | |
140 | |
141 // Tests the case where Decrypt() is called before GKR() is called and the right | |
142 // key is added. | |
143 TEST_F(ProxyDecryptorTest, DecryptBeforeGenerateKeyRequest) { | |
144 EXPECT_CALL(client_, NeedKeyMock("", "", NotNull(), arraysize(kFakeKeyId))); | |
145 decryptor_.Decrypt(encrypted_buffer_, decrypt_cb_); | |
146 | |
147 EXPECT_CALL(*real_decryptor_, Decrypt(encrypted_buffer_, _)) | |
148 .WillOnce(RunDecryptCB(Decryptor::kSuccess, decrypted_buffer_)); | |
149 EXPECT_CALL(*this, BufferDecrypted(Decryptor::kSuccess, decrypted_buffer_)) | |
150 .WillOnce(InvokeWithoutArgs(this, &ProxyDecryptorTest::StopMessageLoop)); | |
151 GenerateKeyRequest(); | |
152 AddKey(); | |
153 message_loop_.Run(); | |
154 } | |
155 | |
156 // Tests the case where multiple AddKey() is called to add some irrelevant keys | |
157 // before the real key that can decrypt |encrypted_buffer_| is added. | |
158 TEST_F(ProxyDecryptorTest, MultipleAddKeys) { | |
159 EXPECT_CALL(client_, NeedKeyMock("", "", NotNull(), arraysize(kFakeKeyId))) | |
160 .Times(AtLeast(1)); | |
161 decryptor_.Decrypt(encrypted_buffer_, decrypt_cb_); | |
162 | |
163 EXPECT_CALL(*real_decryptor_, Decrypt(encrypted_buffer_, _)) | |
164 .WillRepeatedly(RunDecryptCB(Decryptor::kNoKey, null_buffer_)); | |
165 GenerateKeyRequest(); | |
166 const int number_of_irrelevant_addkey = 5; | |
167 for (int i = 0; i < number_of_irrelevant_addkey; ++i) | |
168 AddKey(); // Some irrelevant keys are added. | |
169 | |
170 EXPECT_CALL(*real_decryptor_, Decrypt(encrypted_buffer_, _)) | |
171 .WillOnce(RunDecryptCB(Decryptor::kSuccess, decrypted_buffer_)); | |
172 EXPECT_CALL(*this, BufferDecrypted(Decryptor::kSuccess, decrypted_buffer_)) | |
173 .WillOnce(InvokeWithoutArgs(this, &ProxyDecryptorTest::StopMessageLoop)); | |
174 AddKey(); // Correct key added. | |
175 message_loop_.Run(); | |
176 } | |
177 | |
178 // Tests the case where Decrypt() is called multiple times (e.g. from multiple | |
179 // stream) before the right key is added via AddKey(). | |
180 TEST_F(ProxyDecryptorTest, MultiplePendingDecryptions) { | |
181 EXPECT_CALL(client_, NeedKeyMock("", "", NotNull(), arraysize(kFakeKeyId))) | |
182 .Times(AtLeast(1)); | |
183 EXPECT_CALL(*real_decryptor_, Decrypt(encrypted_buffer_, _)) | |
184 .WillRepeatedly(RunDecryptCB(Decryptor::kNoKey, null_buffer_)); | |
185 decryptor_.Decrypt(encrypted_buffer_, decrypt_cb_); | |
186 GenerateKeyRequest(); | |
187 decryptor_.Decrypt(encrypted_buffer_, decrypt_cb_); | |
188 AddKey(); // An irrelevant key is added. | |
189 decryptor_.Decrypt(encrypted_buffer_, decrypt_cb_); | |
190 | |
191 EXPECT_CALL(*real_decryptor_, Decrypt(encrypted_buffer_, _)) | |
192 .WillRepeatedly(RunDecryptCB(Decryptor::kSuccess, decrypted_buffer_)); | |
193 EXPECT_CALL(*this, BufferDecrypted(Decryptor::kSuccess, decrypted_buffer_)) | |
194 .Times(3); | |
195 AddKey(); // Correct key added. | |
196 | |
197 StopMessageLoop(); | |
scherkus (not reviewing)
2012/07/28 23:08:03
this method name is misleading as it only posts a
xhwang
2012/07/30 19:58:10
Renamed to ScheduleMessageLoopToStop().
| |
198 message_loop_.Run(); | |
199 } | |
200 | |
201 } // namespace webkit_media | |
OLD | NEW |