| 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/cdm_promise.h" |
| 10 #include "media/base/decoder_buffer.h" | 11 #include "media/base/decoder_buffer.h" |
| 11 #include "media/base/decrypt_config.h" | 12 #include "media/base/decrypt_config.h" |
| 12 #include "media/base/mock_filters.h" | 13 #include "media/base/mock_filters.h" |
| 13 #include "media/cdm/aes_decryptor.h" | 14 #include "media/cdm/aes_decryptor.h" |
| 14 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 using ::testing::_; | 18 using ::testing::_; |
| 18 using ::testing::Gt; | 19 using ::testing::Gt; |
| 19 using ::testing::IsNull; | 20 using ::testing::IsNull; |
| 20 using ::testing::NotNull; | 21 using ::testing::NotNull; |
| 21 using ::testing::SaveArg; | 22 using ::testing::SaveArg; |
| 22 using ::testing::StrNe; | 23 using ::testing::StrNe; |
| 23 | 24 |
| 24 MATCHER(IsEmpty, "") { return arg.empty(); } | 25 MATCHER(IsEmpty, "") { return arg.empty(); } |
| 26 MATCHER(IsNotEmpty, "") { return !arg.empty(); } |
| 25 | 27 |
| 26 class GURL; | 28 class GURL; |
| 27 | 29 |
| 28 namespace media { | 30 namespace media { |
| 29 | 31 |
| 30 const uint8 kOriginalData[] = "Original subsample data."; | 32 const uint8 kOriginalData[] = "Original subsample data."; |
| 31 const int kOriginalDataSize = 24; | 33 const int kOriginalDataSize = 24; |
| 32 | 34 |
| 33 // In the examples below, 'k'(key) has to be 16 bytes, and will always require | 35 // In the examples below, 'k'(key) has to be 16 bytes, and will always require |
| 34 // 2 bytes of padding. 'kid'(keyid) is variable length, and may require 0, 1, | 36 // 2 bytes of padding. 'kid'(keyid) is variable length, and may require 0, 1, |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 std::string key_id_string( | 188 std::string key_id_string( |
| 187 reinterpret_cast<const char*>(key_id.empty() ? NULL : &key_id[0]), | 189 reinterpret_cast<const char*>(key_id.empty() ? NULL : &key_id[0]), |
| 188 key_id.size()); | 190 key_id.size()); |
| 189 std::string iv_string( | 191 std::string iv_string( |
| 190 reinterpret_cast<const char*>(iv.empty() ? NULL : &iv[0]), iv.size()); | 192 reinterpret_cast<const char*>(iv.empty() ? NULL : &iv[0]), iv.size()); |
| 191 encrypted_buffer->set_decrypt_config(scoped_ptr<DecryptConfig>( | 193 encrypted_buffer->set_decrypt_config(scoped_ptr<DecryptConfig>( |
| 192 new DecryptConfig(key_id_string, iv_string, subsample_entries))); | 194 new DecryptConfig(key_id_string, iv_string, subsample_entries))); |
| 193 return encrypted_buffer; | 195 return encrypted_buffer; |
| 194 } | 196 } |
| 195 | 197 |
| 198 enum PromiseResult { RESOLVED, REJECTED }; |
| 199 |
| 196 class AesDecryptorTest : public testing::Test { | 200 class AesDecryptorTest : public testing::Test { |
| 197 public: | 201 public: |
| 198 AesDecryptorTest() | 202 AesDecryptorTest() |
| 199 : decryptor_(base::Bind(&AesDecryptorTest::OnSessionCreated, | 203 : decryptor_(base::Bind(&AesDecryptorTest::OnSessionMessage, |
| 200 base::Unretained(this)), | |
| 201 base::Bind(&AesDecryptorTest::OnSessionMessage, | |
| 202 base::Unretained(this)), | |
| 203 base::Bind(&AesDecryptorTest::OnSessionReady, | |
| 204 base::Unretained(this)), | |
| 205 base::Bind(&AesDecryptorTest::OnSessionClosed, | |
| 206 base::Unretained(this)), | |
| 207 base::Bind(&AesDecryptorTest::OnSessionError, | |
| 208 base::Unretained(this))), | 204 base::Unretained(this))), |
| 209 decrypt_cb_(base::Bind(&AesDecryptorTest::BufferDecrypted, | 205 decrypt_cb_(base::Bind(&AesDecryptorTest::BufferDecrypted, |
| 210 base::Unretained(this))), | 206 base::Unretained(this))), |
| 211 original_data_(kOriginalData, kOriginalData + kOriginalDataSize), | 207 original_data_(kOriginalData, kOriginalData + kOriginalDataSize), |
| 212 encrypted_data_(kEncryptedData, | 208 encrypted_data_(kEncryptedData, |
| 213 kEncryptedData + arraysize(kEncryptedData)), | 209 kEncryptedData + arraysize(kEncryptedData)), |
| 214 subsample_encrypted_data_( | 210 subsample_encrypted_data_( |
| 215 kSubsampleEncryptedData, | 211 kSubsampleEncryptedData, |
| 216 kSubsampleEncryptedData + arraysize(kSubsampleEncryptedData)), | 212 kSubsampleEncryptedData + arraysize(kSubsampleEncryptedData)), |
| 217 key_id_(kKeyId, kKeyId + arraysize(kKeyId)), | 213 key_id_(kKeyId, kKeyId + arraysize(kKeyId)), |
| 218 iv_(kIv, kIv + arraysize(kIv)), | 214 iv_(kIv, kIv + arraysize(kIv)), |
| 219 normal_subsample_entries_( | 215 normal_subsample_entries_( |
| 220 kSubsampleEntriesNormal, | 216 kSubsampleEntriesNormal, |
| 221 kSubsampleEntriesNormal + arraysize(kSubsampleEntriesNormal)), | 217 kSubsampleEntriesNormal + arraysize(kSubsampleEntriesNormal)) { |
| 222 next_session_id_(1) { | |
| 223 } | 218 } |
| 224 | 219 |
| 225 protected: | 220 protected: |
| 221 void OnResolveWithSession(PromiseResult expected, |
| 222 const std::string& web_session_id) { |
| 223 EXPECT_EQ(expected, RESOLVED); |
| 224 EXPECT_GT(web_session_id.length(), 0ul); |
| 225 web_session_id_ = web_session_id; |
| 226 } |
| 227 |
| 228 void OnResolve(PromiseResult expected) { |
| 229 EXPECT_EQ(expected, RESOLVED); |
| 230 } |
| 231 |
| 232 void OnReject(PromiseResult expected, |
| 233 MediaKeys::Exception exception_code, |
| 234 uint32 system_code, |
| 235 const std::string& error_message) { |
| 236 EXPECT_EQ(expected, REJECTED); |
| 237 } |
| 238 |
| 239 scoped_ptr<SimpleCdmPromise> CreatePromise(PromiseResult expected) { |
| 240 scoped_ptr<SimpleCdmPromise> promise(new SimpleCdmPromise( |
| 241 base::Bind( |
| 242 &AesDecryptorTest::OnResolve, base::Unretained(this), expected), |
| 243 base::Bind( |
| 244 &AesDecryptorTest::OnReject, base::Unretained(this), expected))); |
| 245 return promise.Pass(); |
| 246 } |
| 247 |
| 248 scoped_ptr<NewSessionCdmPromise> CreateSessionPromise( |
| 249 PromiseResult expected) { |
| 250 scoped_ptr<NewSessionCdmPromise> promise(new NewSessionCdmPromise( |
| 251 base::Bind(&AesDecryptorTest::OnResolveWithSession, |
| 252 base::Unretained(this), |
| 253 expected), |
| 254 base::Bind( |
| 255 &AesDecryptorTest::OnReject, base::Unretained(this), expected))); |
| 256 return promise.Pass(); |
| 257 } |
| 258 |
| 226 // Creates a new session using |key_id|. Returns the session ID. | 259 // Creates a new session using |key_id|. Returns the session ID. |
| 227 uint32 CreateSession(const std::vector<uint8>& key_id) { | 260 std::string CreateSession(const std::vector<uint8>& key_id) { |
| 228 DCHECK(!key_id.empty()); | 261 DCHECK(!key_id.empty()); |
| 229 uint32 session_id = next_session_id_++; | 262 EXPECT_CALL(*this, |
| 230 EXPECT_CALL(*this, OnSessionCreated(session_id, StrNe(std::string()))); | 263 OnSessionMessage(IsNotEmpty(), key_id, GURL::EmptyGURL())); |
| 231 EXPECT_CALL(*this, OnSessionMessage(session_id, key_id, GURL::EmptyGURL())); | 264 decryptor_.CreateSession(std::string(), |
| 232 EXPECT_TRUE(decryptor_.CreateSession( | 265 &key_id[0], |
| 233 session_id, std::string(), &key_id[0], key_id.size())); | 266 key_id.size(), |
| 234 return session_id; | 267 MediaKeys::TEMPORARY_SESSION, |
| 268 CreateSessionPromise(RESOLVED)); |
| 269 // This expects the promise to be called synchronously, which is the case |
| 270 // for AesDecryptor. |
| 271 return web_session_id_; |
| 235 } | 272 } |
| 236 | 273 |
| 237 // Releases the session specified by |session_id|. | 274 // Releases the session specified by |session_id|. |
| 238 void ReleaseSession(uint32 session_id) { | 275 void ReleaseSession(const std::string& session_id) { |
| 239 EXPECT_CALL(*this, OnSessionClosed(session_id)); | 276 decryptor_.ReleaseSession(session_id, CreatePromise(RESOLVED)); |
| 240 decryptor_.ReleaseSession(session_id); | |
| 241 } | 277 } |
| 242 | 278 |
| 243 enum UpdateSessionExpectation { | |
| 244 SESSION_READY, | |
| 245 SESSION_ERROR | |
| 246 }; | |
| 247 | |
| 248 // Updates the session specified by |session_id| with |key|. |result| | 279 // Updates the session specified by |session_id| with |key|. |result| |
| 249 // tests that the update succeeds or generates an error. | 280 // tests that the update succeeds or generates an error. |
| 250 void UpdateSessionAndExpect(uint32 session_id, | 281 void UpdateSessionAndExpect(std::string session_id, |
| 251 const std::string& key, | 282 const std::string& key, |
| 252 UpdateSessionExpectation result) { | 283 PromiseResult result) { |
| 253 DCHECK(!key.empty()); | 284 DCHECK(!key.empty()); |
| 254 | 285 |
| 255 switch (result) { | 286 decryptor_.UpdateSession(session_id, |
| 256 case SESSION_READY: | 287 reinterpret_cast<const uint8*>(key.c_str()), |
| 257 EXPECT_CALL(*this, OnSessionReady(session_id)); | 288 key.length(), |
| 258 break; | 289 CreatePromise(result)); |
| 259 case SESSION_ERROR: | |
| 260 EXPECT_CALL(*this, | |
| 261 OnSessionError(session_id, MediaKeys::kUnknownError, 0)); | |
| 262 break; | |
| 263 } | |
| 264 | |
| 265 decryptor_.UpdateSession( | |
| 266 session_id, reinterpret_cast<const uint8*>(key.c_str()), key.length()); | |
| 267 } | 290 } |
| 268 | 291 |
| 269 MOCK_METHOD2(BufferDecrypted, void(Decryptor::Status, | 292 MOCK_METHOD2(BufferDecrypted, void(Decryptor::Status, |
| 270 const scoped_refptr<DecoderBuffer>&)); | 293 const scoped_refptr<DecoderBuffer>&)); |
| 271 | 294 |
| 272 enum DecryptExpectation { | 295 enum DecryptExpectation { |
| 273 SUCCESS, | 296 SUCCESS, |
| 274 DATA_MISMATCH, | 297 DATA_MISMATCH, |
| 275 DATA_AND_SIZE_MISMATCH, | 298 DATA_AND_SIZE_MISMATCH, |
| 276 DECRYPT_ERROR, | 299 DECRYPT_ERROR, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 case DATA_AND_SIZE_MISMATCH: | 341 case DATA_AND_SIZE_MISMATCH: |
| 319 EXPECT_NE(plain_text.size(), decrypted_text.size()); | 342 EXPECT_NE(plain_text.size(), decrypted_text.size()); |
| 320 break; | 343 break; |
| 321 case DECRYPT_ERROR: | 344 case DECRYPT_ERROR: |
| 322 case NO_KEY: | 345 case NO_KEY: |
| 323 EXPECT_TRUE(decrypted_text.empty()); | 346 EXPECT_TRUE(decrypted_text.empty()); |
| 324 break; | 347 break; |
| 325 } | 348 } |
| 326 } | 349 } |
| 327 | 350 |
| 328 MOCK_METHOD2(OnSessionCreated, | |
| 329 void(uint32 session_id, const std::string& web_session_id)); | |
| 330 MOCK_METHOD3(OnSessionMessage, | 351 MOCK_METHOD3(OnSessionMessage, |
| 331 void(uint32 session_id, | 352 void(const std::string& web_session_id, |
| 332 const std::vector<uint8>& message, | 353 const std::vector<uint8>& message, |
| 333 const GURL& destination_url)); | 354 const GURL& destination_url)); |
| 334 MOCK_METHOD1(OnSessionReady, void(uint32 session_id)); | |
| 335 MOCK_METHOD1(OnSessionClosed, void(uint32 session_id)); | |
| 336 MOCK_METHOD3(OnSessionError, | |
| 337 void(uint32 session_id, | |
| 338 MediaKeys::KeyError, | |
| 339 uint32 system_code)); | |
| 340 | 355 |
| 341 AesDecryptor decryptor_; | 356 AesDecryptor decryptor_; |
| 342 AesDecryptor::DecryptCB decrypt_cb_; | 357 AesDecryptor::DecryptCB decrypt_cb_; |
| 358 std::string web_session_id_; |
| 343 | 359 |
| 344 // Constants for testing. | 360 // Constants for testing. |
| 345 const std::vector<uint8> original_data_; | 361 const std::vector<uint8> original_data_; |
| 346 const std::vector<uint8> encrypted_data_; | 362 const std::vector<uint8> encrypted_data_; |
| 347 const std::vector<uint8> subsample_encrypted_data_; | 363 const std::vector<uint8> subsample_encrypted_data_; |
| 348 const std::vector<uint8> key_id_; | 364 const std::vector<uint8> key_id_; |
| 349 const std::vector<uint8> iv_; | 365 const std::vector<uint8> iv_; |
| 350 const std::vector<SubsampleEntry> normal_subsample_entries_; | 366 const std::vector<SubsampleEntry> normal_subsample_entries_; |
| 351 const std::vector<SubsampleEntry> no_subsample_entries_; | 367 const std::vector<SubsampleEntry> no_subsample_entries_; |
| 352 | |
| 353 // Generate new session ID every time | |
| 354 uint32 next_session_id_; | |
| 355 }; | 368 }; |
| 356 | 369 |
| 357 TEST_F(AesDecryptorTest, CreateSessionWithNullInitData) { | 370 TEST_F(AesDecryptorTest, CreateSessionWithNullInitData) { |
| 358 uint32 session_id = 8; | |
| 359 EXPECT_CALL(*this, | 371 EXPECT_CALL(*this, |
| 360 OnSessionMessage(session_id, IsEmpty(), GURL::EmptyGURL())); | 372 OnSessionMessage(IsNotEmpty(), IsEmpty(), GURL::EmptyGURL())); |
| 361 EXPECT_CALL(*this, OnSessionCreated(session_id, StrNe(std::string()))); | 373 decryptor_.CreateSession(std::string(), |
| 362 EXPECT_TRUE(decryptor_.CreateSession(session_id, std::string(), NULL, 0)); | 374 NULL, |
| 375 0, |
| 376 MediaKeys::TEMPORARY_SESSION, |
| 377 CreateSessionPromise(RESOLVED)); |
| 363 } | 378 } |
| 364 | 379 |
| 365 TEST_F(AesDecryptorTest, MultipleCreateSession) { | 380 TEST_F(AesDecryptorTest, MultipleCreateSession) { |
| 366 uint32 session_id1 = 10; | |
| 367 EXPECT_CALL(*this, | 381 EXPECT_CALL(*this, |
| 368 OnSessionMessage(session_id1, IsEmpty(), GURL::EmptyGURL())); | 382 OnSessionMessage(IsNotEmpty(), IsEmpty(), GURL::EmptyGURL())); |
| 369 EXPECT_CALL(*this, OnSessionCreated(session_id1, StrNe(std::string()))); | 383 decryptor_.CreateSession(std::string(), |
| 370 EXPECT_TRUE(decryptor_.CreateSession(session_id1, std::string(), NULL, 0)); | 384 NULL, |
| 385 0, |
| 386 MediaKeys::TEMPORARY_SESSION, |
| 387 CreateSessionPromise(RESOLVED)); |
| 371 | 388 |
| 372 uint32 session_id2 = 11; | |
| 373 EXPECT_CALL(*this, | 389 EXPECT_CALL(*this, |
| 374 OnSessionMessage(session_id2, IsEmpty(), GURL::EmptyGURL())); | 390 OnSessionMessage(IsNotEmpty(), IsEmpty(), GURL::EmptyGURL())); |
| 375 EXPECT_CALL(*this, OnSessionCreated(session_id2, StrNe(std::string()))); | 391 decryptor_.CreateSession(std::string(), |
| 376 EXPECT_TRUE(decryptor_.CreateSession(session_id2, std::string(), NULL, 0)); | 392 NULL, |
| 393 0, |
| 394 MediaKeys::TEMPORARY_SESSION, |
| 395 CreateSessionPromise(RESOLVED)); |
| 377 | 396 |
| 378 uint32 session_id3 = 23; | |
| 379 EXPECT_CALL(*this, | 397 EXPECT_CALL(*this, |
| 380 OnSessionMessage(session_id3, IsEmpty(), GURL::EmptyGURL())); | 398 OnSessionMessage(IsNotEmpty(), IsEmpty(), GURL::EmptyGURL())); |
| 381 EXPECT_CALL(*this, OnSessionCreated(session_id3, StrNe(std::string()))); | 399 decryptor_.CreateSession(std::string(), |
| 382 EXPECT_TRUE(decryptor_.CreateSession(session_id3, std::string(), NULL, 0)); | 400 NULL, |
| 401 0, |
| 402 MediaKeys::TEMPORARY_SESSION, |
| 403 CreateSessionPromise(RESOLVED)); |
| 383 } | 404 } |
| 384 | 405 |
| 385 TEST_F(AesDecryptorTest, NormalDecryption) { | 406 TEST_F(AesDecryptorTest, NormalDecryption) { |
| 386 uint32 session_id = CreateSession(key_id_); | 407 std::string session_id = CreateSession(key_id_); |
| 387 UpdateSessionAndExpect(session_id, kKeyAsJWK, SESSION_READY); | 408 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 388 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 409 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 389 encrypted_data_, key_id_, iv_, no_subsample_entries_); | 410 encrypted_data_, key_id_, iv_, no_subsample_entries_); |
| 390 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); | 411 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); |
| 391 } | 412 } |
| 392 | 413 |
| 393 TEST_F(AesDecryptorTest, UnencryptedFrame) { | 414 TEST_F(AesDecryptorTest, UnencryptedFrame) { |
| 394 // An empty iv string signals that the frame is unencrypted. | 415 // An empty iv string signals that the frame is unencrypted. |
| 395 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 416 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 396 original_data_, key_id_, std::vector<uint8>(), no_subsample_entries_); | 417 original_data_, key_id_, std::vector<uint8>(), no_subsample_entries_); |
| 397 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); | 418 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); |
| 398 } | 419 } |
| 399 | 420 |
| 400 TEST_F(AesDecryptorTest, WrongKey) { | 421 TEST_F(AesDecryptorTest, WrongKey) { |
| 401 uint32 session_id = CreateSession(key_id_); | 422 std::string session_id = CreateSession(key_id_); |
| 402 UpdateSessionAndExpect(session_id, kWrongKeyAsJWK, SESSION_READY); | 423 UpdateSessionAndExpect(session_id, kWrongKeyAsJWK, RESOLVED); |
| 403 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 424 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 404 encrypted_data_, key_id_, iv_, no_subsample_entries_); | 425 encrypted_data_, key_id_, iv_, no_subsample_entries_); |
| 405 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); | 426 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); |
| 406 } | 427 } |
| 407 | 428 |
| 408 TEST_F(AesDecryptorTest, NoKey) { | 429 TEST_F(AesDecryptorTest, NoKey) { |
| 409 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 430 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 410 encrypted_data_, key_id_, iv_, no_subsample_entries_); | 431 encrypted_data_, key_id_, iv_, no_subsample_entries_); |
| 411 EXPECT_CALL(*this, BufferDecrypted(AesDecryptor::kNoKey, IsNull())); | 432 EXPECT_CALL(*this, BufferDecrypted(AesDecryptor::kNoKey, IsNull())); |
| 412 decryptor_.Decrypt(Decryptor::kVideo, encrypted_buffer, decrypt_cb_); | 433 decryptor_.Decrypt(Decryptor::kVideo, encrypted_buffer, decrypt_cb_); |
| 413 } | 434 } |
| 414 | 435 |
| 415 TEST_F(AesDecryptorTest, KeyReplacement) { | 436 TEST_F(AesDecryptorTest, KeyReplacement) { |
| 416 uint32 session_id = CreateSession(key_id_); | 437 std::string session_id = CreateSession(key_id_); |
| 417 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 438 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 418 encrypted_data_, key_id_, iv_, no_subsample_entries_); | 439 encrypted_data_, key_id_, iv_, no_subsample_entries_); |
| 419 | 440 |
| 420 UpdateSessionAndExpect(session_id, kWrongKeyAsJWK, SESSION_READY); | 441 UpdateSessionAndExpect(session_id, kWrongKeyAsJWK, RESOLVED); |
| 421 ASSERT_NO_FATAL_FAILURE(DecryptAndExpect( | 442 ASSERT_NO_FATAL_FAILURE(DecryptAndExpect( |
| 422 encrypted_buffer, original_data_, DATA_MISMATCH)); | 443 encrypted_buffer, original_data_, DATA_MISMATCH)); |
| 423 | 444 |
| 424 UpdateSessionAndExpect(session_id, kKeyAsJWK, SESSION_READY); | 445 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 425 ASSERT_NO_FATAL_FAILURE( | 446 ASSERT_NO_FATAL_FAILURE( |
| 426 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); | 447 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); |
| 427 } | 448 } |
| 428 | 449 |
| 429 TEST_F(AesDecryptorTest, WrongSizedKey) { | 450 TEST_F(AesDecryptorTest, WrongSizedKey) { |
| 430 uint32 session_id = CreateSession(key_id_); | 451 std::string session_id = CreateSession(key_id_); |
| 431 UpdateSessionAndExpect(session_id, kWrongSizedKeyAsJWK, SESSION_ERROR); | 452 UpdateSessionAndExpect(session_id, kWrongSizedKeyAsJWK, REJECTED); |
| 432 } | 453 } |
| 433 | 454 |
| 434 TEST_F(AesDecryptorTest, MultipleKeysAndFrames) { | 455 TEST_F(AesDecryptorTest, MultipleKeysAndFrames) { |
| 435 uint32 session_id = CreateSession(key_id_); | 456 std::string session_id = CreateSession(key_id_); |
| 436 UpdateSessionAndExpect(session_id, kKeyAsJWK, SESSION_READY); | 457 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 437 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 458 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 438 encrypted_data_, key_id_, iv_, no_subsample_entries_); | 459 encrypted_data_, key_id_, iv_, no_subsample_entries_); |
| 439 ASSERT_NO_FATAL_FAILURE( | 460 ASSERT_NO_FATAL_FAILURE( |
| 440 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); | 461 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); |
| 441 | 462 |
| 442 UpdateSessionAndExpect(session_id, kKey2AsJWK, SESSION_READY); | 463 UpdateSessionAndExpect(session_id, kKey2AsJWK, RESOLVED); |
| 443 | 464 |
| 444 // The first key is still available after we added a second key. | 465 // The first key is still available after we added a second key. |
| 445 ASSERT_NO_FATAL_FAILURE( | 466 ASSERT_NO_FATAL_FAILURE( |
| 446 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); | 467 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); |
| 447 | 468 |
| 448 // The second key is also available. | 469 // The second key is also available. |
| 449 encrypted_buffer = CreateEncryptedBuffer( | 470 encrypted_buffer = CreateEncryptedBuffer( |
| 450 std::vector<uint8>(kEncryptedData2, | 471 std::vector<uint8>(kEncryptedData2, |
| 451 kEncryptedData2 + arraysize(kEncryptedData2)), | 472 kEncryptedData2 + arraysize(kEncryptedData2)), |
| 452 std::vector<uint8>(kKeyId2, kKeyId2 + arraysize(kKeyId2)), | 473 std::vector<uint8>(kKeyId2, kKeyId2 + arraysize(kKeyId2)), |
| 453 std::vector<uint8>(kIv2, kIv2 + arraysize(kIv2)), | 474 std::vector<uint8>(kIv2, kIv2 + arraysize(kIv2)), |
| 454 no_subsample_entries_); | 475 no_subsample_entries_); |
| 455 ASSERT_NO_FATAL_FAILURE(DecryptAndExpect( | 476 ASSERT_NO_FATAL_FAILURE(DecryptAndExpect( |
| 456 encrypted_buffer, | 477 encrypted_buffer, |
| 457 std::vector<uint8>(kOriginalData2, | 478 std::vector<uint8>(kOriginalData2, |
| 458 kOriginalData2 + arraysize(kOriginalData2) - 1), | 479 kOriginalData2 + arraysize(kOriginalData2) - 1), |
| 459 SUCCESS)); | 480 SUCCESS)); |
| 460 } | 481 } |
| 461 | 482 |
| 462 TEST_F(AesDecryptorTest, CorruptedIv) { | 483 TEST_F(AesDecryptorTest, CorruptedIv) { |
| 463 uint32 session_id = CreateSession(key_id_); | 484 std::string session_id = CreateSession(key_id_); |
| 464 UpdateSessionAndExpect(session_id, kKeyAsJWK, SESSION_READY); | 485 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 465 | 486 |
| 466 std::vector<uint8> bad_iv = iv_; | 487 std::vector<uint8> bad_iv = iv_; |
| 467 bad_iv[1]++; | 488 bad_iv[1]++; |
| 468 | 489 |
| 469 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 490 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 470 encrypted_data_, key_id_, bad_iv, no_subsample_entries_); | 491 encrypted_data_, key_id_, bad_iv, no_subsample_entries_); |
| 471 | 492 |
| 472 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); | 493 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); |
| 473 } | 494 } |
| 474 | 495 |
| 475 TEST_F(AesDecryptorTest, CorruptedData) { | 496 TEST_F(AesDecryptorTest, CorruptedData) { |
| 476 uint32 session_id = CreateSession(key_id_); | 497 std::string session_id = CreateSession(key_id_); |
| 477 UpdateSessionAndExpect(session_id, kKeyAsJWK, SESSION_READY); | 498 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 478 | 499 |
| 479 std::vector<uint8> bad_data = encrypted_data_; | 500 std::vector<uint8> bad_data = encrypted_data_; |
| 480 bad_data[1]++; | 501 bad_data[1]++; |
| 481 | 502 |
| 482 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 503 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 483 bad_data, key_id_, iv_, no_subsample_entries_); | 504 bad_data, key_id_, iv_, no_subsample_entries_); |
| 484 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); | 505 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); |
| 485 } | 506 } |
| 486 | 507 |
| 487 TEST_F(AesDecryptorTest, EncryptedAsUnencryptedFailure) { | 508 TEST_F(AesDecryptorTest, EncryptedAsUnencryptedFailure) { |
| 488 uint32 session_id = CreateSession(key_id_); | 509 std::string session_id = CreateSession(key_id_); |
| 489 UpdateSessionAndExpect(session_id, kKeyAsJWK, SESSION_READY); | 510 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 490 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 511 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 491 encrypted_data_, key_id_, std::vector<uint8>(), no_subsample_entries_); | 512 encrypted_data_, key_id_, std::vector<uint8>(), no_subsample_entries_); |
| 492 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); | 513 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); |
| 493 } | 514 } |
| 494 | 515 |
| 495 TEST_F(AesDecryptorTest, SubsampleDecryption) { | 516 TEST_F(AesDecryptorTest, SubsampleDecryption) { |
| 496 uint32 session_id = CreateSession(key_id_); | 517 std::string session_id = CreateSession(key_id_); |
| 497 UpdateSessionAndExpect(session_id, kKeyAsJWK, SESSION_READY); | 518 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 498 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 519 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 499 subsample_encrypted_data_, key_id_, iv_, normal_subsample_entries_); | 520 subsample_encrypted_data_, key_id_, iv_, normal_subsample_entries_); |
| 500 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); | 521 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); |
| 501 } | 522 } |
| 502 | 523 |
| 503 // Ensures noninterference of data offset and subsample mechanisms. We never | 524 // Ensures noninterference of data offset and subsample mechanisms. We never |
| 504 // expect to encounter this in the wild, but since the DecryptConfig doesn't | 525 // expect to encounter this in the wild, but since the DecryptConfig doesn't |
| 505 // disallow such a configuration, it should be covered. | 526 // disallow such a configuration, it should be covered. |
| 506 TEST_F(AesDecryptorTest, SubsampleDecryptionWithOffset) { | 527 TEST_F(AesDecryptorTest, SubsampleDecryptionWithOffset) { |
| 507 uint32 session_id = CreateSession(key_id_); | 528 std::string session_id = CreateSession(key_id_); |
| 508 UpdateSessionAndExpect(session_id, kKeyAsJWK, SESSION_READY); | 529 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 509 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 530 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 510 subsample_encrypted_data_, key_id_, iv_, normal_subsample_entries_); | 531 subsample_encrypted_data_, key_id_, iv_, normal_subsample_entries_); |
| 511 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); | 532 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); |
| 512 } | 533 } |
| 513 | 534 |
| 514 TEST_F(AesDecryptorTest, SubsampleWrongSize) { | 535 TEST_F(AesDecryptorTest, SubsampleWrongSize) { |
| 515 uint32 session_id = CreateSession(key_id_); | 536 std::string session_id = CreateSession(key_id_); |
| 516 UpdateSessionAndExpect(session_id, kKeyAsJWK, SESSION_READY); | 537 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 517 | 538 |
| 518 std::vector<SubsampleEntry> subsample_entries_wrong_size( | 539 std::vector<SubsampleEntry> subsample_entries_wrong_size( |
| 519 kSubsampleEntriesWrongSize, | 540 kSubsampleEntriesWrongSize, |
| 520 kSubsampleEntriesWrongSize + arraysize(kSubsampleEntriesWrongSize)); | 541 kSubsampleEntriesWrongSize + arraysize(kSubsampleEntriesWrongSize)); |
| 521 | 542 |
| 522 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 543 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 523 subsample_encrypted_data_, key_id_, iv_, subsample_entries_wrong_size); | 544 subsample_encrypted_data_, key_id_, iv_, subsample_entries_wrong_size); |
| 524 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); | 545 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH); |
| 525 } | 546 } |
| 526 | 547 |
| 527 TEST_F(AesDecryptorTest, SubsampleInvalidTotalSize) { | 548 TEST_F(AesDecryptorTest, SubsampleInvalidTotalSize) { |
| 528 uint32 session_id = CreateSession(key_id_); | 549 std::string session_id = CreateSession(key_id_); |
| 529 UpdateSessionAndExpect(session_id, kKeyAsJWK, SESSION_READY); | 550 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 530 | 551 |
| 531 std::vector<SubsampleEntry> subsample_entries_invalid_total_size( | 552 std::vector<SubsampleEntry> subsample_entries_invalid_total_size( |
| 532 kSubsampleEntriesInvalidTotalSize, | 553 kSubsampleEntriesInvalidTotalSize, |
| 533 kSubsampleEntriesInvalidTotalSize + | 554 kSubsampleEntriesInvalidTotalSize + |
| 534 arraysize(kSubsampleEntriesInvalidTotalSize)); | 555 arraysize(kSubsampleEntriesInvalidTotalSize)); |
| 535 | 556 |
| 536 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 557 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 537 subsample_encrypted_data_, key_id_, iv_, | 558 subsample_encrypted_data_, key_id_, iv_, |
| 538 subsample_entries_invalid_total_size); | 559 subsample_entries_invalid_total_size); |
| 539 DecryptAndExpect(encrypted_buffer, original_data_, DECRYPT_ERROR); | 560 DecryptAndExpect(encrypted_buffer, original_data_, DECRYPT_ERROR); |
| 540 } | 561 } |
| 541 | 562 |
| 542 // No cypher bytes in any of the subsamples. | 563 // No cypher bytes in any of the subsamples. |
| 543 TEST_F(AesDecryptorTest, SubsampleClearBytesOnly) { | 564 TEST_F(AesDecryptorTest, SubsampleClearBytesOnly) { |
| 544 uint32 session_id = CreateSession(key_id_); | 565 std::string session_id = CreateSession(key_id_); |
| 545 UpdateSessionAndExpect(session_id, kKeyAsJWK, SESSION_READY); | 566 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 546 | 567 |
| 547 std::vector<SubsampleEntry> clear_only_subsample_entries( | 568 std::vector<SubsampleEntry> clear_only_subsample_entries( |
| 548 kSubsampleEntriesClearOnly, | 569 kSubsampleEntriesClearOnly, |
| 549 kSubsampleEntriesClearOnly + arraysize(kSubsampleEntriesClearOnly)); | 570 kSubsampleEntriesClearOnly + arraysize(kSubsampleEntriesClearOnly)); |
| 550 | 571 |
| 551 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 572 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 552 original_data_, key_id_, iv_, clear_only_subsample_entries); | 573 original_data_, key_id_, iv_, clear_only_subsample_entries); |
| 553 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); | 574 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); |
| 554 } | 575 } |
| 555 | 576 |
| 556 // No clear bytes in any of the subsamples. | 577 // No clear bytes in any of the subsamples. |
| 557 TEST_F(AesDecryptorTest, SubsampleCypherBytesOnly) { | 578 TEST_F(AesDecryptorTest, SubsampleCypherBytesOnly) { |
| 558 uint32 session_id = CreateSession(key_id_); | 579 std::string session_id = CreateSession(key_id_); |
| 559 UpdateSessionAndExpect(session_id, kKeyAsJWK, SESSION_READY); | 580 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 560 | 581 |
| 561 std::vector<SubsampleEntry> cypher_only_subsample_entries( | 582 std::vector<SubsampleEntry> cypher_only_subsample_entries( |
| 562 kSubsampleEntriesCypherOnly, | 583 kSubsampleEntriesCypherOnly, |
| 563 kSubsampleEntriesCypherOnly + arraysize(kSubsampleEntriesCypherOnly)); | 584 kSubsampleEntriesCypherOnly + arraysize(kSubsampleEntriesCypherOnly)); |
| 564 | 585 |
| 565 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 586 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 566 encrypted_data_, key_id_, iv_, cypher_only_subsample_entries); | 587 encrypted_data_, key_id_, iv_, cypher_only_subsample_entries); |
| 567 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); | 588 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS); |
| 568 } | 589 } |
| 569 | 590 |
| 570 TEST_F(AesDecryptorTest, ReleaseSession) { | 591 TEST_F(AesDecryptorTest, ReleaseSession) { |
| 571 uint32 session_id = CreateSession(key_id_); | 592 std::string session_id = CreateSession(key_id_); |
| 572 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 593 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 573 encrypted_data_, key_id_, iv_, no_subsample_entries_); | 594 encrypted_data_, key_id_, iv_, no_subsample_entries_); |
| 574 | 595 |
| 575 UpdateSessionAndExpect(session_id, kKeyAsJWK, SESSION_READY); | 596 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 576 ASSERT_NO_FATAL_FAILURE( | 597 ASSERT_NO_FATAL_FAILURE( |
| 577 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); | 598 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); |
| 578 | 599 |
| 579 ReleaseSession(session_id); | 600 ReleaseSession(session_id); |
| 580 } | 601 } |
| 581 | 602 |
| 582 TEST_F(AesDecryptorTest, NoKeyAfterReleaseSession) { | 603 TEST_F(AesDecryptorTest, NoKeyAfterReleaseSession) { |
| 583 uint32 session_id = CreateSession(key_id_); | 604 std::string session_id = CreateSession(key_id_); |
| 584 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 605 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 585 encrypted_data_, key_id_, iv_, no_subsample_entries_); | 606 encrypted_data_, key_id_, iv_, no_subsample_entries_); |
| 586 | 607 |
| 587 UpdateSessionAndExpect(session_id, kKeyAsJWK, SESSION_READY); | 608 UpdateSessionAndExpect(session_id, kKeyAsJWK, RESOLVED); |
| 588 ASSERT_NO_FATAL_FAILURE( | 609 ASSERT_NO_FATAL_FAILURE( |
| 589 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); | 610 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); |
| 590 | 611 |
| 591 ReleaseSession(session_id); | 612 ReleaseSession(session_id); |
| 592 ASSERT_NO_FATAL_FAILURE( | 613 ASSERT_NO_FATAL_FAILURE( |
| 593 DecryptAndExpect(encrypted_buffer, original_data_, NO_KEY)); | 614 DecryptAndExpect(encrypted_buffer, original_data_, NO_KEY)); |
| 594 } | 615 } |
| 595 | 616 |
| 596 TEST_F(AesDecryptorTest, LatestKeyUsed) { | 617 TEST_F(AesDecryptorTest, LatestKeyUsed) { |
| 597 uint32 session_id1 = CreateSession(key_id_); | 618 std::string session_id1 = CreateSession(key_id_); |
| 598 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 619 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 599 encrypted_data_, key_id_, iv_, no_subsample_entries_); | 620 encrypted_data_, key_id_, iv_, no_subsample_entries_); |
| 600 | 621 |
| 601 // Add alternate key, buffer should not be decoded properly. | 622 // Add alternate key, buffer should not be decoded properly. |
| 602 UpdateSessionAndExpect(session_id1, kKeyAlternateAsJWK, SESSION_READY); | 623 UpdateSessionAndExpect(session_id1, kKeyAlternateAsJWK, RESOLVED); |
| 603 ASSERT_NO_FATAL_FAILURE( | 624 ASSERT_NO_FATAL_FAILURE( |
| 604 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH)); | 625 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH)); |
| 605 | 626 |
| 606 // Create a second session with a correct key value for key_id_. | 627 // Create a second session with a correct key value for key_id_. |
| 607 uint32 session_id2 = CreateSession(key_id_); | 628 std::string session_id2 = CreateSession(key_id_); |
| 608 UpdateSessionAndExpect(session_id2, kKeyAsJWK, SESSION_READY); | 629 UpdateSessionAndExpect(session_id2, kKeyAsJWK, RESOLVED); |
| 609 | 630 |
| 610 // Should be able to decode with latest key. | 631 // Should be able to decode with latest key. |
| 611 ASSERT_NO_FATAL_FAILURE( | 632 ASSERT_NO_FATAL_FAILURE( |
| 612 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); | 633 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); |
| 613 } | 634 } |
| 614 | 635 |
| 615 TEST_F(AesDecryptorTest, LatestKeyUsedAfterReleaseSession) { | 636 TEST_F(AesDecryptorTest, LatestKeyUsedAfterReleaseSession) { |
| 616 uint32 session_id1 = CreateSession(key_id_); | 637 std::string session_id1 = CreateSession(key_id_); |
| 617 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( | 638 scoped_refptr<DecoderBuffer> encrypted_buffer = CreateEncryptedBuffer( |
| 618 encrypted_data_, key_id_, iv_, no_subsample_entries_); | 639 encrypted_data_, key_id_, iv_, no_subsample_entries_); |
| 619 UpdateSessionAndExpect(session_id1, kKeyAsJWK, SESSION_READY); | 640 UpdateSessionAndExpect(session_id1, kKeyAsJWK, RESOLVED); |
| 620 ASSERT_NO_FATAL_FAILURE( | 641 ASSERT_NO_FATAL_FAILURE( |
| 621 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); | 642 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); |
| 622 | 643 |
| 623 // Create a second session with a different key value for key_id_. | 644 // Create a second session with a different key value for key_id_. |
| 624 uint32 session_id2 = CreateSession(key_id_); | 645 std::string session_id2 = CreateSession(key_id_); |
| 625 UpdateSessionAndExpect(session_id2, kKeyAlternateAsJWK, SESSION_READY); | 646 UpdateSessionAndExpect(session_id2, kKeyAlternateAsJWK, RESOLVED); |
| 626 | 647 |
| 627 // Should not be able to decode with new key. | 648 // Should not be able to decode with new key. |
| 628 ASSERT_NO_FATAL_FAILURE( | 649 ASSERT_NO_FATAL_FAILURE( |
| 629 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH)); | 650 DecryptAndExpect(encrypted_buffer, original_data_, DATA_MISMATCH)); |
| 630 | 651 |
| 631 // Close second session, should revert to original key. | 652 // Close second session, should revert to original key. |
| 632 ReleaseSession(session_id2); | 653 ReleaseSession(session_id2); |
| 633 ASSERT_NO_FATAL_FAILURE( | 654 ASSERT_NO_FATAL_FAILURE( |
| 634 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); | 655 DecryptAndExpect(encrypted_buffer, original_data_, SUCCESS)); |
| 635 } | 656 } |
| 636 | 657 |
| 637 TEST_F(AesDecryptorTest, JWKKey) { | 658 TEST_F(AesDecryptorTest, JWKKey) { |
| 638 uint32 session_id = CreateSession(key_id_); | 659 std::string session_id = CreateSession(key_id_); |
| 639 | 660 |
| 640 // Try a simple JWK key (i.e. not in a set) | 661 // Try a simple JWK key (i.e. not in a set) |
| 641 const std::string kJwkSimple = | 662 const std::string kJwkSimple = |
| 642 "{" | 663 "{" |
| 643 " \"kty\": \"oct\"," | 664 " \"kty\": \"oct\"," |
| 644 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM\"," | 665 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM\"," |
| 645 " \"k\": \"FBUWFxgZGhscHR4fICEiIw\"" | 666 " \"k\": \"FBUWFxgZGhscHR4fICEiIw\"" |
| 646 "}"; | 667 "}"; |
| 647 UpdateSessionAndExpect(session_id, kJwkSimple, SESSION_ERROR); | 668 UpdateSessionAndExpect(session_id, kJwkSimple, REJECTED); |
| 648 | 669 |
| 649 // Try a key list with multiple entries. | 670 // Try a key list with multiple entries. |
| 650 const std::string kJwksMultipleEntries = | 671 const std::string kJwksMultipleEntries = |
| 651 "{" | 672 "{" |
| 652 " \"keys\": [" | 673 " \"keys\": [" |
| 653 " {" | 674 " {" |
| 654 " \"kty\": \"oct\"," | 675 " \"kty\": \"oct\"," |
| 655 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM\"," | 676 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM\"," |
| 656 " \"k\": \"FBUWFxgZGhscHR4fICEiIw\"" | 677 " \"k\": \"FBUWFxgZGhscHR4fICEiIw\"" |
| 657 " }," | 678 " }," |
| 658 " {" | 679 " {" |
| 659 " \"kty\": \"oct\"," | 680 " \"kty\": \"oct\"," |
| 660 " \"kid\": \"JCUmJygpKissLS4vMA\"," | 681 " \"kid\": \"JCUmJygpKissLS4vMA\"," |
| 661 " \"k\":\"MTIzNDU2Nzg5Ojs8PT4/QA\"" | 682 " \"k\":\"MTIzNDU2Nzg5Ojs8PT4/QA\"" |
| 662 " }" | 683 " }" |
| 663 " ]" | 684 " ]" |
| 664 "}"; | 685 "}"; |
| 665 UpdateSessionAndExpect(session_id, kJwksMultipleEntries, SESSION_READY); | 686 UpdateSessionAndExpect(session_id, kJwksMultipleEntries, RESOLVED); |
| 666 | 687 |
| 667 // Try a key with no spaces and some \n plus additional fields. | 688 // Try a key with no spaces and some \n plus additional fields. |
| 668 const std::string kJwksNoSpaces = | 689 const std::string kJwksNoSpaces = |
| 669 "\n\n{\"something\":1,\"keys\":[{\n\n\"kty\":\"oct\",\"alg\":\"A128KW\"," | 690 "\n\n{\"something\":1,\"keys\":[{\n\n\"kty\":\"oct\",\"alg\":\"A128KW\"," |
| 670 "\"kid\":\"AAECAwQFBgcICQoLDA0ODxAREhM\",\"k\":\"GawgguFyGrWKav7AX4VKUg" | 691 "\"kid\":\"AAECAwQFBgcICQoLDA0ODxAREhM\",\"k\":\"GawgguFyGrWKav7AX4VKUg" |
| 671 "\",\"foo\":\"bar\"}]}\n\n"; | 692 "\",\"foo\":\"bar\"}]}\n\n"; |
| 672 UpdateSessionAndExpect(session_id, kJwksNoSpaces, SESSION_READY); | 693 UpdateSessionAndExpect(session_id, kJwksNoSpaces, RESOLVED); |
| 673 | 694 |
| 674 // Try some non-ASCII characters. | 695 // Try some non-ASCII characters. |
| 675 UpdateSessionAndExpect(session_id, | 696 UpdateSessionAndExpect( |
| 676 "This is not ASCII due to \xff\xfe\xfd in it.", | 697 session_id, "This is not ASCII due to \xff\xfe\xfd in it.", REJECTED); |
| 677 SESSION_ERROR); | |
| 678 | 698 |
| 679 // Try a badly formatted key. Assume that the JSON parser is fully tested, | 699 // Try a badly formatted key. Assume that the JSON parser is fully tested, |
| 680 // so we won't try a lot of combinations. However, need a test to ensure | 700 // so we won't try a lot of combinations. However, need a test to ensure |
| 681 // that the code doesn't crash if invalid JSON received. | 701 // that the code doesn't crash if invalid JSON received. |
| 682 UpdateSessionAndExpect(session_id, "This is not a JSON key.", SESSION_ERROR); | 702 UpdateSessionAndExpect(session_id, "This is not a JSON key.", REJECTED); |
| 683 | 703 |
| 684 // Try passing some valid JSON that is not a dictionary at the top level. | 704 // Try passing some valid JSON that is not a dictionary at the top level. |
| 685 UpdateSessionAndExpect(session_id, "40", SESSION_ERROR); | 705 UpdateSessionAndExpect(session_id, "40", REJECTED); |
| 686 | 706 |
| 687 // Try an empty dictionary. | 707 // Try an empty dictionary. |
| 688 UpdateSessionAndExpect(session_id, "{ }", SESSION_ERROR); | 708 UpdateSessionAndExpect(session_id, "{ }", REJECTED); |
| 689 | 709 |
| 690 // Try an empty 'keys' dictionary. | 710 // Try an empty 'keys' dictionary. |
| 691 UpdateSessionAndExpect(session_id, "{ \"keys\": [] }", SESSION_ERROR); | 711 UpdateSessionAndExpect(session_id, "{ \"keys\": [] }", REJECTED); |
| 692 | 712 |
| 693 // Try with 'keys' not a dictionary. | 713 // Try with 'keys' not a dictionary. |
| 694 UpdateSessionAndExpect(session_id, "{ \"keys\":\"1\" }", SESSION_ERROR); | 714 UpdateSessionAndExpect(session_id, "{ \"keys\":\"1\" }", REJECTED); |
| 695 | 715 |
| 696 // Try with 'keys' a list of integers. | 716 // Try with 'keys' a list of integers. |
| 697 UpdateSessionAndExpect( | 717 UpdateSessionAndExpect(session_id, "{ \"keys\": [ 1, 2, 3 ] }", REJECTED); |
| 698 session_id, "{ \"keys\": [ 1, 2, 3 ] }", SESSION_ERROR); | |
| 699 | 718 |
| 700 // Try padding(=) at end of 'k' base64 string. | 719 // Try padding(=) at end of 'k' base64 string. |
| 701 const std::string kJwksWithPaddedKey = | 720 const std::string kJwksWithPaddedKey = |
| 702 "{" | 721 "{" |
| 703 " \"keys\": [" | 722 " \"keys\": [" |
| 704 " {" | 723 " {" |
| 705 " \"kty\": \"oct\"," | 724 " \"kty\": \"oct\"," |
| 706 " \"kid\": \"AAECAw\"," | 725 " \"kid\": \"AAECAw\"," |
| 707 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw==\"" | 726 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw==\"" |
| 708 " }" | 727 " }" |
| 709 " ]" | 728 " ]" |
| 710 "}"; | 729 "}"; |
| 711 UpdateSessionAndExpect(session_id, kJwksWithPaddedKey, SESSION_ERROR); | 730 UpdateSessionAndExpect(session_id, kJwksWithPaddedKey, REJECTED); |
| 712 | 731 |
| 713 // Try padding(=) at end of 'kid' base64 string. | 732 // Try padding(=) at end of 'kid' base64 string. |
| 714 const std::string kJwksWithPaddedKeyId = | 733 const std::string kJwksWithPaddedKeyId = |
| 715 "{" | 734 "{" |
| 716 " \"keys\": [" | 735 " \"keys\": [" |
| 717 " {" | 736 " {" |
| 718 " \"kty\": \"oct\"," | 737 " \"kty\": \"oct\"," |
| 719 " \"kid\": \"AAECAw==\"," | 738 " \"kid\": \"AAECAw==\"," |
| 720 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" | 739 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" |
| 721 " }" | 740 " }" |
| 722 " ]" | 741 " ]" |
| 723 "}"; | 742 "}"; |
| 724 UpdateSessionAndExpect(session_id, kJwksWithPaddedKeyId, SESSION_ERROR); | 743 UpdateSessionAndExpect(session_id, kJwksWithPaddedKeyId, REJECTED); |
| 725 | 744 |
| 726 // Try a key with invalid base64 encoding. | 745 // Try a key with invalid base64 encoding. |
| 727 const std::string kJwksWithInvalidBase64 = | 746 const std::string kJwksWithInvalidBase64 = |
| 728 "{" | 747 "{" |
| 729 " \"keys\": [" | 748 " \"keys\": [" |
| 730 " {" | 749 " {" |
| 731 " \"kty\": \"oct\"," | 750 " \"kty\": \"oct\"," |
| 732 " \"kid\": \"!@#$%^&*()\"," | 751 " \"kid\": \"!@#$%^&*()\"," |
| 733 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" | 752 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" |
| 734 " }" | 753 " }" |
| 735 " ]" | 754 " ]" |
| 736 "}"; | 755 "}"; |
| 737 UpdateSessionAndExpect(session_id, kJwksWithInvalidBase64, SESSION_ERROR); | 756 UpdateSessionAndExpect(session_id, kJwksWithInvalidBase64, REJECTED); |
| 738 | 757 |
| 739 // Try a 3-byte 'kid' where no base64 padding is required. | 758 // Try a 3-byte 'kid' where no base64 padding is required. |
| 740 // |kJwksMultipleEntries| above has 2 'kid's that require 1 and 2 padding | 759 // |kJwksMultipleEntries| above has 2 'kid's that require 1 and 2 padding |
| 741 // bytes. Note that 'k' has to be 16 bytes, so it will always require padding. | 760 // bytes. Note that 'k' has to be 16 bytes, so it will always require padding. |
| 742 const std::string kJwksWithNoPadding = | 761 const std::string kJwksWithNoPadding = |
| 743 "{" | 762 "{" |
| 744 " \"keys\": [" | 763 " \"keys\": [" |
| 745 " {" | 764 " {" |
| 746 " \"kty\": \"oct\"," | 765 " \"kty\": \"oct\"," |
| 747 " \"kid\": \"Kiss\"," | 766 " \"kid\": \"Kiss\"," |
| 748 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" | 767 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" |
| 749 " }" | 768 " }" |
| 750 " ]" | 769 " ]" |
| 751 "}"; | 770 "}"; |
| 752 UpdateSessionAndExpect(session_id, kJwksWithNoPadding, SESSION_READY); | 771 UpdateSessionAndExpect(session_id, kJwksWithNoPadding, RESOLVED); |
| 753 | 772 |
| 754 // Empty key id. | 773 // Empty key id. |
| 755 const std::string kJwksWithEmptyKeyId = | 774 const std::string kJwksWithEmptyKeyId = |
| 756 "{" | 775 "{" |
| 757 " \"keys\": [" | 776 " \"keys\": [" |
| 758 " {" | 777 " {" |
| 759 " \"kty\": \"oct\"," | 778 " \"kty\": \"oct\"," |
| 760 " \"kid\": \"\"," | 779 " \"kid\": \"\"," |
| 761 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" | 780 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\"" |
| 762 " }" | 781 " }" |
| 763 " ]" | 782 " ]" |
| 764 "}"; | 783 "}"; |
| 765 UpdateSessionAndExpect(session_id, kJwksWithEmptyKeyId, SESSION_ERROR); | 784 UpdateSessionAndExpect(session_id, kJwksWithEmptyKeyId, REJECTED); |
| 766 ReleaseSession(session_id); | 785 ReleaseSession(session_id); |
| 767 } | 786 } |
| 768 | 787 |
| 769 } // namespace media | 788 } // namespace media |
| OLD | NEW |