| 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 "media/base/decoder_buffer.h" | 10 #include "media/base/decoder_buffer.h" |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 : decryptor_(base::Bind(&AesDecryptorTest::OnSessionCreated, | 188 : decryptor_(base::Bind(&AesDecryptorTest::OnSessionCreated, |
| 189 base::Unretained(this)), | 189 base::Unretained(this)), |
| 190 base::Bind(&AesDecryptorTest::OnSessionMessage, | 190 base::Bind(&AesDecryptorTest::OnSessionMessage, |
| 191 base::Unretained(this)), | 191 base::Unretained(this)), |
| 192 base::Bind(&AesDecryptorTest::OnSessionReady, | 192 base::Bind(&AesDecryptorTest::OnSessionReady, |
| 193 base::Unretained(this)), | 193 base::Unretained(this)), |
| 194 base::Bind(&AesDecryptorTest::OnSessionClosed, | 194 base::Bind(&AesDecryptorTest::OnSessionClosed, |
| 195 base::Unretained(this)), | 195 base::Unretained(this)), |
| 196 base::Bind(&AesDecryptorTest::OnSessionError, | 196 base::Bind(&AesDecryptorTest::OnSessionError, |
| 197 base::Unretained(this))), | 197 base::Unretained(this))), |
| 198 reference_id_(MediaKeys::kInvalidReferenceId), | 198 session_id_(MediaKeys::kInvalidSessionId), |
| 199 decrypt_cb_(base::Bind(&AesDecryptorTest::BufferDecrypted, | 199 decrypt_cb_(base::Bind(&AesDecryptorTest::BufferDecrypted, |
| 200 base::Unretained(this))), | 200 base::Unretained(this))), |
| 201 original_data_(kOriginalData, kOriginalData + kOriginalDataSize), | 201 original_data_(kOriginalData, kOriginalData + kOriginalDataSize), |
| 202 encrypted_data_(kEncryptedData, | 202 encrypted_data_(kEncryptedData, |
| 203 kEncryptedData + arraysize(kEncryptedData)), | 203 kEncryptedData + arraysize(kEncryptedData)), |
| 204 subsample_encrypted_data_( | 204 subsample_encrypted_data_( |
| 205 kSubsampleEncryptedData, | 205 kSubsampleEncryptedData, |
| 206 kSubsampleEncryptedData + arraysize(kSubsampleEncryptedData)), | 206 kSubsampleEncryptedData + arraysize(kSubsampleEncryptedData)), |
| 207 key_id_(kKeyId, kKeyId + arraysize(kKeyId)), | 207 key_id_(kKeyId, kKeyId + arraysize(kKeyId)), |
| 208 iv_(kIv, kIv + arraysize(kIv)), | 208 iv_(kIv, kIv + arraysize(kIv)), |
| 209 normal_subsample_entries_( | 209 normal_subsample_entries_( |
| 210 kSubsampleEntriesNormal, | 210 kSubsampleEntriesNormal, |
| 211 kSubsampleEntriesNormal + arraysize(kSubsampleEntriesNormal)) { | 211 kSubsampleEntriesNormal + arraysize(kSubsampleEntriesNormal)) { |
| 212 } | 212 } |
| 213 | 213 |
| 214 protected: | 214 protected: |
| 215 void CreateSession(const std::vector<uint8>& key_id) { | 215 void CreateSession(const std::vector<uint8>& key_id) { |
| 216 reference_id_ = 6; | 216 session_id_ = 6; |
| 217 DCHECK(!key_id.empty()); | 217 DCHECK(!key_id.empty()); |
| 218 EXPECT_CALL(*this, OnSessionCreated(reference_id_, StrNe(std::string()))); | 218 EXPECT_CALL(*this, OnSessionCreated(session_id_, StrNe(std::string()))); |
| 219 EXPECT_CALL(*this, OnSessionMessage(reference_id_, key_id, "")); | 219 EXPECT_CALL(*this, OnSessionMessage(session_id_, key_id, "")); |
| 220 EXPECT_TRUE(decryptor_.CreateSession( | 220 EXPECT_TRUE(decryptor_.CreateSession( |
| 221 reference_id_, std::string(), &key_id[0], key_id.size())); | 221 session_id_, std::string(), &key_id[0], key_id.size())); |
| 222 } | 222 } |
| 223 | 223 |
| 224 enum AddKeyExpectation { | 224 enum AddKeyExpectation { |
| 225 KEY_ADDED, | 225 KEY_ADDED, |
| 226 KEY_ERROR | 226 KEY_ERROR |
| 227 }; | 227 }; |
| 228 | 228 |
| 229 void AddKeyAndExpect(const std::string& key, AddKeyExpectation result) { | 229 void AddKeyAndExpect(const std::string& key, AddKeyExpectation result) { |
| 230 DCHECK(!key.empty()); | 230 DCHECK(!key.empty()); |
| 231 | 231 |
| 232 if (result == KEY_ADDED) { | 232 if (result == KEY_ADDED) { |
| 233 EXPECT_CALL(*this, OnSessionReady(reference_id_)); | 233 EXPECT_CALL(*this, OnSessionReady(session_id_)); |
| 234 } else if (result == KEY_ERROR) { | 234 } else if (result == KEY_ERROR) { |
| 235 EXPECT_CALL(*this, | 235 EXPECT_CALL(*this, |
| 236 OnSessionError(reference_id_, MediaKeys::kUnknownError, 0)); | 236 OnSessionError(session_id_, MediaKeys::kUnknownError, 0)); |
| 237 } else { | 237 } else { |
| 238 NOTREACHED(); | 238 NOTREACHED(); |
| 239 } | 239 } |
| 240 | 240 |
| 241 decryptor_.UpdateSession(reference_id_, | 241 decryptor_.UpdateSession( |
| 242 reinterpret_cast<const uint8*>(key.c_str()), | 242 session_id_, reinterpret_cast<const uint8*>(key.c_str()), key.length()); |
| 243 key.length()); | |
| 244 } | 243 } |
| 245 | 244 |
| 246 MOCK_METHOD2(BufferDecrypted, void(Decryptor::Status, | 245 MOCK_METHOD2(BufferDecrypted, void(Decryptor::Status, |
| 247 const scoped_refptr<DecoderBuffer>&)); | 246 const scoped_refptr<DecoderBuffer>&)); |
| 248 | 247 |
| 249 enum DecryptExpectation { | 248 enum DecryptExpectation { |
| 250 SUCCESS, | 249 SUCCESS, |
| 251 DATA_MISMATCH, | 250 DATA_MISMATCH, |
| 252 DATA_AND_SIZE_MISMATCH, | 251 DATA_AND_SIZE_MISMATCH, |
| 253 DECRYPT_ERROR | 252 DECRYPT_ERROR |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 case DATA_AND_SIZE_MISMATCH: | 284 case DATA_AND_SIZE_MISMATCH: |
| 286 EXPECT_NE(plain_text.size(), decrypted_text.size()); | 285 EXPECT_NE(plain_text.size(), decrypted_text.size()); |
| 287 break; | 286 break; |
| 288 case DECRYPT_ERROR: | 287 case DECRYPT_ERROR: |
| 289 EXPECT_TRUE(decrypted_text.empty()); | 288 EXPECT_TRUE(decrypted_text.empty()); |
| 290 break; | 289 break; |
| 291 } | 290 } |
| 292 } | 291 } |
| 293 | 292 |
| 294 MOCK_METHOD2(OnSessionCreated, | 293 MOCK_METHOD2(OnSessionCreated, |
| 295 void(uint32 reference_id, const std::string& session_id)); | 294 void(uint32 session_id, const std::string& web_session_id)); |
| 296 MOCK_METHOD3(OnSessionMessage, | 295 MOCK_METHOD3(OnSessionMessage, |
| 297 void(uint32 reference_id, | 296 void(uint32 session_id, |
| 298 const std::vector<uint8>& message, | 297 const std::vector<uint8>& message, |
| 299 const std::string& default_url)); | 298 const std::string& default_url)); |
| 300 MOCK_METHOD1(OnSessionReady, void(uint32 reference_id)); | 299 MOCK_METHOD1(OnSessionReady, void(uint32 session_id)); |
| 301 MOCK_METHOD1(OnSessionClosed, void(uint32 reference_id)); | 300 MOCK_METHOD1(OnSessionClosed, void(uint32 session_id)); |
| 302 MOCK_METHOD3(OnSessionError, | 301 MOCK_METHOD3(OnSessionError, |
| 303 void(uint32 reference_id, MediaKeys::KeyError, int system_code)); | 302 void(uint32 session_id, MediaKeys::KeyError, int system_code)); |
| 304 | 303 |
| 305 AesDecryptor decryptor_; | 304 AesDecryptor decryptor_; |
| 306 uint32 reference_id_; | 305 uint32 session_id_; |
| 307 AesDecryptor::DecryptCB decrypt_cb_; | 306 AesDecryptor::DecryptCB decrypt_cb_; |
| 308 | 307 |
| 309 // Constants for testing. | 308 // Constants for testing. |
| 310 const std::vector<uint8> original_data_; | 309 const std::vector<uint8> original_data_; |
| 311 const std::vector<uint8> encrypted_data_; | 310 const std::vector<uint8> encrypted_data_; |
| 312 const std::vector<uint8> subsample_encrypted_data_; | 311 const std::vector<uint8> subsample_encrypted_data_; |
| 313 const std::vector<uint8> key_id_; | 312 const std::vector<uint8> key_id_; |
| 314 const std::vector<uint8> iv_; | 313 const std::vector<uint8> iv_; |
| 315 const std::vector<SubsampleEntry> normal_subsample_entries_; | 314 const std::vector<SubsampleEntry> normal_subsample_entries_; |
| 316 const std::vector<SubsampleEntry> no_subsample_entries_; | 315 const std::vector<SubsampleEntry> no_subsample_entries_; |
| 317 }; | 316 }; |
| 318 | 317 |
| 319 TEST_F(AesDecryptorTest, CreateSessionWithNullInitData) { | 318 TEST_F(AesDecryptorTest, CreateSessionWithNullInitData) { |
| 320 reference_id_ = 8; | 319 session_id_ = 8; |
| 321 EXPECT_CALL(*this, OnSessionMessage(reference_id_, IsEmpty(), "")); | 320 EXPECT_CALL(*this, OnSessionMessage(session_id_, IsEmpty(), "")); |
| 322 EXPECT_CALL(*this, OnSessionCreated(reference_id_, StrNe(std::string()))); | 321 EXPECT_CALL(*this, OnSessionCreated(session_id_, StrNe(std::string()))); |
| 323 EXPECT_TRUE(decryptor_.CreateSession(reference_id_, std::string(), NULL, 0)); | 322 EXPECT_TRUE(decryptor_.CreateSession(session_id_, std::string(), NULL, 0)); |
| 324 } | 323 } |
| 325 | 324 |
| 326 TEST_F(AesDecryptorTest, MultipleCreateSession) { | 325 TEST_F(AesDecryptorTest, MultipleCreateSession) { |
| 327 uint32 reference_id1 = 10; | 326 uint32 session_id1 = 10; |
| 328 EXPECT_CALL(*this, OnSessionMessage(reference_id1, IsEmpty(), "")); | 327 EXPECT_CALL(*this, OnSessionMessage(session_id1, IsEmpty(), "")); |
| 329 EXPECT_CALL(*this, OnSessionCreated(reference_id1, StrNe(std::string()))); | 328 EXPECT_CALL(*this, OnSessionCreated(session_id1, StrNe(std::string()))); |
| 330 EXPECT_TRUE(decryptor_.CreateSession(reference_id1, std::string(), NULL, 0)); | 329 EXPECT_TRUE(decryptor_.CreateSession(session_id1, std::string(), NULL, 0)); |
| 331 | 330 |
| 332 uint32 reference_id2 = 11; | 331 uint32 session_id2 = 11; |
| 333 EXPECT_CALL(*this, OnSessionMessage(reference_id2, IsEmpty(), "")); | 332 EXPECT_CALL(*this, OnSessionMessage(session_id2, IsEmpty(), "")); |
| 334 EXPECT_CALL(*this, OnSessionCreated(reference_id2, StrNe(std::string()))); | 333 EXPECT_CALL(*this, OnSessionCreated(session_id2, StrNe(std::string()))); |
| 335 EXPECT_TRUE(decryptor_.CreateSession(reference_id2, std::string(), NULL, 0)); | 334 EXPECT_TRUE(decryptor_.CreateSession(session_id2, std::string(), NULL, 0)); |
| 336 | 335 |
| 337 uint32 reference_id3 = 23; | 336 uint32 session_id3 = 23; |
| 338 EXPECT_CALL(*this, OnSessionMessage(reference_id3, IsEmpty(), "")); | 337 EXPECT_CALL(*this, OnSessionMessage(session_id3, IsEmpty(), "")); |
| 339 EXPECT_CALL(*this, OnSessionCreated(reference_id3, StrNe(std::string()))); | 338 EXPECT_CALL(*this, OnSessionCreated(session_id3, StrNe(std::string()))); |
| 340 EXPECT_TRUE(decryptor_.CreateSession(reference_id3, std::string(), NULL, 0)); | 339 EXPECT_TRUE(decryptor_.CreateSession(session_id3, std::string(), NULL, 0)); |
| 341 } | 340 } |
| 342 | 341 |
| 343 TEST_F(AesDecryptorTest, NormalDecryption) { | 342 TEST_F(AesDecryptorTest, NormalDecryption) { |
| 344 CreateSession(key_id_); | 343 CreateSession(key_id_); |
| 345 AddKeyAndExpect(kKeyAsJWK, KEY_ADDED); | 344 AddKeyAndExpect(kKeyAsJWK, KEY_ADDED); |
| 346 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 345 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 347 encrypted_data_, key_id_, iv_, 0, no_subsample_entries_); | 346 encrypted_data_, key_id_, iv_, 0, no_subsample_entries_); |
| 348 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); | 347 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); |
| 349 } | 348 } |
| 350 | 349 |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 654 " \"kty\": \"oct\"," | 653 " \"kty\": \"oct\"," |
| 655 " \"kid\": \"\"," | 654 " \"kid\": \"\"," |
| 656 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" | 655 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" |
| 657 " }" | 656 " }" |
| 658 " ]" | 657 " ]" |
| 659 "}"; | 658 "}"; |
| 660 AddKeyAndExpect(kJwksWithEmptyKeyId, KEY_ERROR); | 659 AddKeyAndExpect(kJwksWithEmptyKeyId, KEY_ERROR); |
| 661 } | 660 } |
| 662 | 661 |
| 663 } // namespace media | 662 } // namespace media |
| OLD | NEW |