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