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