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 "webkit/media/crypto/proxy_decryptor.h" | 5 #include "webkit/media/crypto/proxy_decryptor.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 using ::testing::SaveArg; | 22 using ::testing::SaveArg; |
23 using ::testing::StrictMock; | 23 using ::testing::StrictMock; |
24 | 24 |
25 using media::DecoderBuffer; | 25 using media::DecoderBuffer; |
26 using media::DecryptConfig; | 26 using media::DecryptConfig; |
27 using media::Decryptor; | 27 using media::Decryptor; |
28 | 28 |
29 namespace webkit_media { | 29 namespace webkit_media { |
30 | 30 |
31 static const uint8 kFakeKeyId[] = { 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44 }; | 31 static const uint8 kFakeKeyId[] = { 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44 }; |
| 32 static const int kFakeKeyIdSize = arraysize(kFakeKeyId); |
32 static const uint8 kFakeIv[DecryptConfig::kDecryptionKeySize] = { 0 }; | 33 static const uint8 kFakeIv[DecryptConfig::kDecryptionKeySize] = { 0 }; |
33 static const char kFakeKeySystem[] = "system.key.fake"; | 34 static const char kFakeKeySystem[] = "system.key.fake"; |
34 static const char kFakeSessionId[] = "FakeSessionId"; | 35 static const char kFakeSessionId[] = "FakeSessionId"; |
35 static const uint8 kFakeKey[] = { 0x4b, 0x65, 0x79 }; | 36 static const uint8 kFakeKey[] = { 0x4b, 0x65, 0x79 }; |
36 static const uint8 kEncryptedData[] = { 0x65, 0x6E, 0x63, 0x72, 0x79 }; | 37 static const uint8 kEncryptedData[] = { 0x65, 0x6E, 0x63, 0x72, 0x79 }; |
37 static const uint8 kDecryptedData[] = { 0x64, 0x65, 0x63, 0x72, 0x79 }; | 38 static const uint8 kDecryptedData[] = { 0x64, 0x65, 0x63, 0x72, 0x79 }; |
38 | 39 |
39 // Creates a fake non-empty encrypted buffer. | 40 // Creates a fake non-empty encrypted buffer. |
40 static scoped_refptr<DecoderBuffer> CreateFakeEncryptedBuffer() { | 41 static scoped_refptr<DecoderBuffer> CreateFakeEncryptedBuffer() { |
41 const int encrypted_frame_offset = 1; // This should be non-zero. | 42 const int encrypted_frame_offset = 1; // This should be non-zero. |
42 scoped_refptr<DecoderBuffer> encrypted_buffer = | 43 scoped_refptr<DecoderBuffer> encrypted_buffer = |
43 DecoderBuffer::CopyFrom(kEncryptedData, arraysize(kEncryptedData)); | 44 DecoderBuffer::CopyFrom(kEncryptedData, arraysize(kEncryptedData)); |
44 encrypted_buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>( | 45 encrypted_buffer->SetDecryptConfig(scoped_ptr<DecryptConfig>( |
45 new DecryptConfig( | 46 new DecryptConfig( |
46 std::string(reinterpret_cast<const char*>(kFakeKeyId), | 47 std::string(reinterpret_cast<const char*>(kFakeKeyId), |
47 arraysize(kFakeKeyId)), | 48 kFakeKeyIdSize), |
48 std::string(reinterpret_cast<const char*>(kFakeIv), | 49 std::string(reinterpret_cast<const char*>(kFakeIv), |
49 DecryptConfig::kDecryptionKeySize), | 50 DecryptConfig::kDecryptionKeySize), |
50 encrypted_frame_offset, | 51 encrypted_frame_offset, |
51 std::vector<media::SubsampleEntry>()))); | 52 std::vector<media::SubsampleEntry>()))); |
52 return encrypted_buffer; | 53 return encrypted_buffer; |
53 } | 54 } |
54 | 55 |
55 ACTION_P2(RunDecryptCB, status, buffer) { | 56 ACTION_P2(RunDecryptCB, status, buffer) { |
56 arg2.Run(status, buffer); | 57 arg2.Run(status, buffer); |
57 } | 58 } |
(...skipping 29 matching lines...) Expand all Loading... |
87 // real Decryptor, inject a MockDecryptor for testing purpose. | 88 // real Decryptor, inject a MockDecryptor for testing purpose. |
88 void GenerateKeyRequest() { | 89 void GenerateKeyRequest() { |
89 proxy_decryptor_.set_decryptor_for_testing(scoped_real_decryptor_.Pass()); | 90 proxy_decryptor_.set_decryptor_for_testing(scoped_real_decryptor_.Pass()); |
90 } | 91 } |
91 | 92 |
92 // Since we are using the MockDecryptor, we can simulate any decryption | 93 // Since we are using the MockDecryptor, we can simulate any decryption |
93 // behavior we want. Therefore, we do not care which key is really added, | 94 // behavior we want. Therefore, we do not care which key is really added, |
94 // hence always use fake key IDs and keys. | 95 // hence always use fake key IDs and keys. |
95 void AddKey() { | 96 void AddKey() { |
96 EXPECT_CALL(*real_decryptor_, AddKey(kFakeKeySystem, | 97 EXPECT_CALL(*real_decryptor_, AddKey(kFakeKeySystem, |
97 kFakeKeyId, arraysize(kFakeKeyId), | 98 kFakeKeyId, kFakeKeyIdSize, |
98 kFakeKey, arraysize(kFakeKey), | 99 kFakeKey, arraysize(kFakeKey), |
99 kFakeSessionId)); | 100 kFakeSessionId)); |
100 proxy_decryptor_.AddKey(kFakeKeySystem, | 101 proxy_decryptor_.AddKey(kFakeKeySystem, |
101 kFakeKeyId, arraysize(kFakeKeyId), | 102 kFakeKeyId, kFakeKeyIdSize, |
102 kFakeKey, arraysize(kFakeKey), | 103 kFakeKey, arraysize(kFakeKey), |
103 kFakeSessionId); | 104 kFakeSessionId); |
104 } | 105 } |
105 | 106 |
106 // The DecryptCB passed to proxy_decryptor_.Decrypt(). | 107 // The DecryptCB passed to proxy_decryptor_.Decrypt(). |
107 MOCK_METHOD2(DeliverBuffer, void(Decryptor::Status, | 108 MOCK_METHOD2(DeliverBuffer, void(Decryptor::Status, |
108 const scoped_refptr<DecoderBuffer>&)); | 109 const scoped_refptr<DecoderBuffer>&)); |
109 | 110 |
110 protected: | 111 protected: |
111 MessageLoop message_loop_; | 112 MessageLoop message_loop_; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kError, IsNull())); | 157 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kError, IsNull())); |
157 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); | 158 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); |
158 } | 159 } |
159 | 160 |
160 // Tests the case where no key is available for decryption. | 161 // Tests the case where no key is available for decryption. |
161 TEST_F(ProxyDecryptorTest, NormalDecryption_NoKey) { | 162 TEST_F(ProxyDecryptorTest, NormalDecryption_NoKey) { |
162 GenerateKeyRequest(); | 163 GenerateKeyRequest(); |
163 | 164 |
164 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) | 165 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) |
165 .WillOnce(RunDecryptCB(Decryptor::kNoKey, null_buffer_)); | 166 .WillOnce(RunDecryptCB(Decryptor::kNoKey, null_buffer_)); |
166 EXPECT_CALL(client_, NeedKeyMock("", "", NotNull(), arraysize(kFakeKeyId))); | 167 EXPECT_CALL(client_, NeedKeyMock("", "", "", NotNull(), kFakeKeyIdSize)); |
167 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); | 168 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); |
168 | 169 |
169 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, null_buffer_)); | 170 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, null_buffer_)); |
170 proxy_decryptor_.CancelDecrypt(stream_type_); | 171 proxy_decryptor_.CancelDecrypt(stream_type_); |
171 } | 172 } |
172 | 173 |
173 // Tests the case where Decrypt() is called after the right key is added. | 174 // Tests the case where Decrypt() is called after the right key is added. |
174 TEST_F(ProxyDecryptorTest, DecryptBeforeAddKey) { | 175 TEST_F(ProxyDecryptorTest, DecryptBeforeAddKey) { |
175 EXPECT_CALL(client_, NeedKeyMock("", "", NotNull(), arraysize(kFakeKeyId))); | 176 EXPECT_CALL(client_, NeedKeyMock("", "", "", NotNull(), kFakeKeyIdSize)); |
176 GenerateKeyRequest(); | 177 GenerateKeyRequest(); |
177 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) | 178 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) |
178 .WillOnce(RunDecryptCB(Decryptor::kNoKey, null_buffer_)); | 179 .WillOnce(RunDecryptCB(Decryptor::kNoKey, null_buffer_)); |
179 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); | 180 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); |
180 | 181 |
181 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) | 182 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) |
182 .WillOnce(RunDecryptCB(Decryptor::kSuccess, decrypted_buffer_)); | 183 .WillOnce(RunDecryptCB(Decryptor::kSuccess, decrypted_buffer_)); |
183 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, decrypted_buffer_)) | 184 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, decrypted_buffer_)) |
184 .WillOnce(ScheduleMessageLoopToStop(&message_loop_)); | 185 .WillOnce(ScheduleMessageLoopToStop(&message_loop_)); |
185 AddKey(); | 186 AddKey(); |
186 | 187 |
187 message_loop_.Run(); | 188 message_loop_.Run(); |
188 } | 189 } |
189 | 190 |
190 // Tests the case where Decrypt() is called before GKR() and the right key is | 191 // Tests the case where Decrypt() is called before GKR() and the right key is |
191 // added. | 192 // added. |
192 TEST_F(ProxyDecryptorTest, DecryptBeforeGenerateKeyRequest) { | 193 TEST_F(ProxyDecryptorTest, DecryptBeforeGenerateKeyRequest) { |
193 EXPECT_CALL(client_, NeedKeyMock("", "", NotNull(), arraysize(kFakeKeyId))); | 194 EXPECT_CALL(client_, NeedKeyMock("", "", "", NotNull(), kFakeKeyIdSize)); |
194 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); | 195 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); |
195 | 196 |
196 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) | 197 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) |
197 .WillOnce(RunDecryptCB(Decryptor::kSuccess, decrypted_buffer_)); | 198 .WillOnce(RunDecryptCB(Decryptor::kSuccess, decrypted_buffer_)); |
198 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, decrypted_buffer_)) | 199 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, decrypted_buffer_)) |
199 .WillOnce(ScheduleMessageLoopToStop(&message_loop_)); | 200 .WillOnce(ScheduleMessageLoopToStop(&message_loop_)); |
200 GenerateKeyRequest(); | 201 GenerateKeyRequest(); |
201 AddKey(); | 202 AddKey(); |
202 | 203 |
203 message_loop_.Run(); | 204 message_loop_.Run(); |
204 } | 205 } |
205 | 206 |
206 // Tests the case where multiple AddKey() is called to add some irrelevant keys | 207 // Tests the case where multiple AddKey() is called to add some irrelevant keys |
207 // before the real key that can decrypt |encrypted_buffer_| is added. | 208 // before the real key that can decrypt |encrypted_buffer_| is added. |
208 TEST_F(ProxyDecryptorTest, MultipleAddKeys) { | 209 TEST_F(ProxyDecryptorTest, MultipleAddKeys) { |
209 EXPECT_CALL(client_, NeedKeyMock("", "", NotNull(), arraysize(kFakeKeyId))) | 210 EXPECT_CALL(client_, NeedKeyMock("", "", "", NotNull(), kFakeKeyIdSize)) |
210 .Times(AtLeast(1)); | 211 .Times(AtLeast(1)); |
211 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); | 212 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); |
212 | 213 |
213 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) | 214 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) |
214 .WillRepeatedly(RunDecryptCB(Decryptor::kNoKey, null_buffer_)); | 215 .WillRepeatedly(RunDecryptCB(Decryptor::kNoKey, null_buffer_)); |
215 GenerateKeyRequest(); | 216 GenerateKeyRequest(); |
216 const int number_of_irrelevant_addkey = 5; | 217 const int number_of_irrelevant_addkey = 5; |
217 for (int i = 0; i < number_of_irrelevant_addkey; ++i) | 218 for (int i = 0; i < number_of_irrelevant_addkey; ++i) |
218 AddKey(); // Some irrelevant keys are added. | 219 AddKey(); // Some irrelevant keys are added. |
219 | 220 |
(...skipping 30 matching lines...) Expand all Loading... |
250 .WillOnce(ScheduleMessageLoopToStop(&message_loop_)); | 251 .WillOnce(ScheduleMessageLoopToStop(&message_loop_)); |
251 base::ResetAndReturn(&decrypt_cb).Run(Decryptor::kNoKey, null_buffer_); | 252 base::ResetAndReturn(&decrypt_cb).Run(Decryptor::kNoKey, null_buffer_); |
252 | 253 |
253 message_loop_.Run(); | 254 message_loop_.Run(); |
254 } | 255 } |
255 | 256 |
256 // Test the case where we cancel the pending decryption callback even before | 257 // Test the case where we cancel the pending decryption callback even before |
257 // GenerateKeyRequest is called. In this case, the decryptor was not even | 258 // GenerateKeyRequest is called. In this case, the decryptor was not even |
258 // created! | 259 // created! |
259 TEST_F(ProxyDecryptorTest, CancelDecryptWithoutGenerateKeyRequestCalled) { | 260 TEST_F(ProxyDecryptorTest, CancelDecryptWithoutGenerateKeyRequestCalled) { |
260 EXPECT_CALL(client_, NeedKeyMock("", "", NotNull(), arraysize(kFakeKeyId))) | 261 EXPECT_CALL(client_, NeedKeyMock("", "", "", NotNull(), kFakeKeyIdSize)) |
261 .Times(AtLeast(1)); | 262 .Times(AtLeast(1)); |
262 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); | 263 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); |
263 | 264 |
264 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, null_buffer_)) | 265 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, null_buffer_)) |
265 .WillOnce(ScheduleMessageLoopToStop(&message_loop_)); | 266 .WillOnce(ScheduleMessageLoopToStop(&message_loop_)); |
266 proxy_decryptor_.CancelDecrypt(stream_type_); | 267 proxy_decryptor_.CancelDecrypt(stream_type_); |
267 | 268 |
268 message_loop_.Run(); | 269 message_loop_.Run(); |
269 } | 270 } |
270 | 271 |
271 // Test the case where we cancel the pending decryption callback when it's | 272 // Test the case where we cancel the pending decryption callback when it's |
272 // stored in the ProxyDecryptor. | 273 // stored in the ProxyDecryptor. |
273 TEST_F(ProxyDecryptorTest, CancelDecryptWhenDecryptPendingInProxyDecryptor) { | 274 TEST_F(ProxyDecryptorTest, CancelDecryptWhenDecryptPendingInProxyDecryptor) { |
274 EXPECT_CALL(client_, NeedKeyMock("", "", NotNull(), arraysize(kFakeKeyId))) | 275 EXPECT_CALL(client_, NeedKeyMock("", "", "", NotNull(), kFakeKeyIdSize)) |
275 .Times(AtLeast(1)); | 276 .Times(AtLeast(1)); |
276 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) | 277 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) |
277 .WillRepeatedly(RunDecryptCB(Decryptor::kNoKey, null_buffer_)); | 278 .WillRepeatedly(RunDecryptCB(Decryptor::kNoKey, null_buffer_)); |
278 GenerateKeyRequest(); | 279 GenerateKeyRequest(); |
279 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); | 280 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); |
280 | 281 |
281 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, null_buffer_)) | 282 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, null_buffer_)) |
282 .WillOnce(ScheduleMessageLoopToStop(&message_loop_)); | 283 .WillOnce(ScheduleMessageLoopToStop(&message_loop_)); |
283 proxy_decryptor_.CancelDecrypt(stream_type_); | 284 proxy_decryptor_.CancelDecrypt(stream_type_); |
284 | 285 |
(...skipping 19 matching lines...) Expand all Loading... |
304 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, null_buffer_)) | 305 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, null_buffer_)) |
305 .WillOnce(ScheduleMessageLoopToStop(&message_loop_)); | 306 .WillOnce(ScheduleMessageLoopToStop(&message_loop_)); |
306 proxy_decryptor_.CancelDecrypt(stream_type_); | 307 proxy_decryptor_.CancelDecrypt(stream_type_); |
307 | 308 |
308 message_loop_.Run(); | 309 message_loop_.Run(); |
309 } | 310 } |
310 | 311 |
311 // Test the case where we try to decrypt again after the previous decrypt was | 312 // Test the case where we try to decrypt again after the previous decrypt was |
312 // canceled. | 313 // canceled. |
313 TEST_F(ProxyDecryptorTest, DecryptAfterCancelDecrypt) { | 314 TEST_F(ProxyDecryptorTest, DecryptAfterCancelDecrypt) { |
314 EXPECT_CALL(client_, NeedKeyMock("", "", NotNull(), arraysize(kFakeKeyId))) | 315 EXPECT_CALL(client_, NeedKeyMock("", "", "", NotNull(), kFakeKeyIdSize)) |
315 .Times(AtLeast(1)); | 316 .Times(AtLeast(1)); |
316 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) | 317 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) |
317 .WillRepeatedly(RunDecryptCB(Decryptor::kNoKey, null_buffer_)); | 318 .WillRepeatedly(RunDecryptCB(Decryptor::kNoKey, null_buffer_)); |
318 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); | 319 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); |
319 GenerateKeyRequest(); | 320 GenerateKeyRequest(); |
320 | 321 |
321 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, null_buffer_)); | 322 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, null_buffer_)); |
322 proxy_decryptor_.CancelDecrypt(stream_type_); | 323 proxy_decryptor_.CancelDecrypt(stream_type_); |
323 | 324 |
324 AddKey(); | 325 AddKey(); |
325 | 326 |
326 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) | 327 EXPECT_CALL(*real_decryptor_, Decrypt(stream_type_, encrypted_buffer_, _)) |
327 .WillOnce(RunDecryptCB(Decryptor::kSuccess, decrypted_buffer_)); | 328 .WillOnce(RunDecryptCB(Decryptor::kSuccess, decrypted_buffer_)); |
328 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, decrypted_buffer_)) | 329 EXPECT_CALL(*this, DeliverBuffer(Decryptor::kSuccess, decrypted_buffer_)) |
329 .WillOnce(ScheduleMessageLoopToStop(&message_loop_)); | 330 .WillOnce(ScheduleMessageLoopToStop(&message_loop_)); |
330 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); | 331 proxy_decryptor_.Decrypt(stream_type_, encrypted_buffer_, decrypt_cb_); |
331 | 332 |
332 message_loop_.Run(); | 333 message_loop_.Run(); |
333 } | 334 } |
334 | 335 |
335 } // namespace webkit_media | 336 } // namespace webkit_media |
OLD | NEW |