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
3 // found in the LICENSE file.
8 #include "base/basictypes.h"
10 #include "media/base/decoder_buffer.h"
11 #include "media/base/decrypt_config.h"
12 #include "media/base/mock_filters.h"
13 #include "media/cdm/aes_decryptor.h"
14 #include "media/webm/webm_constants.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 using ::testing::IsNull
;
21 using ::testing::NotNull
;
22 using ::testing::SaveArg
;
23 using ::testing::StrNe
;
25 MATCHER(IsEmpty
, "") { return arg
.empty(); }
29 const uint8 kOriginalData
[] = "Original subsample data.";
30 const int kOriginalDataSize
= 24;
32 const uint8 kKeyId
[] = {
33 // base64 equivalent is AAECAw==
34 0x00, 0x01, 0x02, 0x03
37 const uint8 kKey
[] = {
38 // base64 equivalent is BAUGBwgJCgsMDQ4PEBESEw==
39 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
40 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13
43 const char kKeyAsJWK
[] =
48 " \"kid\": \"AAECAw==\","
49 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw==\""
54 const char kWrongKeyAsJWK
[] =
59 " \"kid\": \"AAECAw==\","
60 " \"k\": \"7u7u7u7u7u7u7u7u7u7u7g==\""
65 const char kWrongSizedKeyAsJWK
[] =
70 " \"kid\": \"AAECAw==\","
71 " \"k\": \"AAECAw==\""
77 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
81 // kOriginalData encrypted with kKey and kIv but without any subsamples (or
82 // equivalently using kSubsampleEntriesCypherOnly).
83 const uint8 kEncryptedData
[] = {
84 0x2f, 0x03, 0x09, 0xef, 0x71, 0xaf, 0x31, 0x16,
85 0xfa, 0x9d, 0x18, 0x43, 0x1e, 0x96, 0x71, 0xb5,
86 0xbf, 0xf5, 0x30, 0x53, 0x9a, 0x20, 0xdf, 0x95
89 // kOriginalData encrypted with kSubsampleKey and kSubsampleIv using
90 // kSubsampleEntriesNormal.
91 const uint8 kSubsampleEncryptedData
[] = {
92 0x4f, 0x72, 0x09, 0x16, 0x09, 0xe6, 0x79, 0xad,
93 0x70, 0x73, 0x75, 0x62, 0x09, 0xbb, 0x83, 0x1d,
94 0x4d, 0x08, 0xd7, 0x78, 0xa4, 0xa7, 0xf1, 0x2e
97 const uint8 kOriginalData2
[] = "Changed Original data.";
99 const uint8 kIv2
[] = {
100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
104 const uint8 kKeyId2
[] = {
105 // base64 equivalent is AAECAwQFBgcICQoLDA0ODxAREhM=
106 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
107 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
108 0x10, 0x11, 0x12, 0x13
111 const char kKey2AsJWK
[] =
116 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM=\","
117 " \"k\": \"FBUWFxgZGhscHR4fICEiIw==\""
122 // 'k' in bytes is x14x15x16x17x18x19x1ax1bx1cx1dx1ex1fx20x21x22x23
124 const uint8 kEncryptedData2
[] = {
125 0x57, 0x66, 0xf4, 0x12, 0x1a, 0xed, 0xb5, 0x79,
126 0x1c, 0x8e, 0x25, 0xd7, 0x17, 0xe7, 0x5e, 0x16,
127 0xe3, 0x40, 0x08, 0x27, 0x11, 0xe9
130 // Subsample entries for testing. The sum of |cypher_bytes| and |clear_bytes| of
131 // all entries must be equal to kOriginalDataSize to make the subsample entries
134 const SubsampleEntry kSubsampleEntriesNormal
[] = {
140 const SubsampleEntry kSubsampleEntriesWrongSize
[] = {
141 { 3, 6 }, // This entry doesn't match the correct entry.
146 const SubsampleEntry kSubsampleEntriesInvalidTotalSize
[] = {
147 { 1, 1000 }, // This entry is too large.
152 const SubsampleEntry kSubsampleEntriesClearOnly
[] = {
158 const SubsampleEntry kSubsampleEntriesCypherOnly
[] = {
164 static scoped_refptr
<DecoderBuffer
> CreateEncryptedBuffer(
165 const std::vector
<uint8
>& data
,
166 const std::vector
<uint8
>& key_id
,
167 const std::vector
<uint8
>& iv
,
169 const std::vector
<SubsampleEntry
>& subsample_entries
) {
170 DCHECK(!data
.empty());
171 int padded_size
= offset
+ data
.size();
172 scoped_refptr
<DecoderBuffer
> encrypted_buffer(new DecoderBuffer(padded_size
));
173 memcpy(encrypted_buffer
->writable_data() + offset
, &data
[0], data
.size());
174 CHECK(encrypted_buffer
.get());
175 std::string
key_id_string(
176 reinterpret_cast<const char*>(key_id
.empty() ? NULL
: &key_id
[0]),
178 std::string
iv_string(
179 reinterpret_cast<const char*>(iv
.empty() ? NULL
: &iv
[0]), iv
.size());
180 encrypted_buffer
->set_decrypt_config(scoped_ptr
<DecryptConfig
>(
181 new DecryptConfig(key_id_string
, iv_string
, offset
, subsample_entries
)));
182 return encrypted_buffer
;
185 class AesDecryptorTest
: public testing::Test
{
189 base::Bind(&AesDecryptorTest::KeyAdded
, base::Unretained(this)),
190 base::Bind(&AesDecryptorTest::KeyError
, base::Unretained(this)),
191 base::Bind(&AesDecryptorTest::KeyMessage
, base::Unretained(this))),
192 decrypt_cb_(base::Bind(&AesDecryptorTest::BufferDecrypted
,
193 base::Unretained(this))),
194 original_data_(kOriginalData
, kOriginalData
+ kOriginalDataSize
),
195 encrypted_data_(kEncryptedData
,
196 kEncryptedData
+ arraysize(kEncryptedData
)),
197 subsample_encrypted_data_(
198 kSubsampleEncryptedData
,
199 kSubsampleEncryptedData
+ arraysize(kSubsampleEncryptedData
)),
200 key_id_(kKeyId
, kKeyId
+ arraysize(kKeyId
)),
201 iv_(kIv
, kIv
+ arraysize(kIv
)),
202 normal_subsample_entries_(
203 kSubsampleEntriesNormal
,
204 kSubsampleEntriesNormal
+ arraysize(kSubsampleEntriesNormal
)) {
208 void GenerateKeyRequest(const std::vector
<uint8
>& key_id
) {
209 DCHECK(!key_id
.empty());
210 EXPECT_CALL(*this, KeyMessage(StrNe(std::string()), key_id
, ""))
211 .WillOnce(SaveArg
<0>(&session_id_string_
));
212 EXPECT_TRUE(decryptor_
.GenerateKeyRequest(
213 std::string(), &key_id
[0], key_id
.size()));
216 enum AddKeyExpectation
{
221 void AddRawKeyAndExpect(const std::vector
<uint8
>& key_id
,
222 const std::vector
<uint8
>& key
,
223 AddKeyExpectation result
) {
224 // TODO(jrummell): Remove once raw keys no longer supported.
225 DCHECK(!key_id
.empty());
226 DCHECK(!key
.empty());
228 if (result
== KEY_ADDED
) {
229 EXPECT_CALL(*this, KeyAdded(session_id_string_
));
230 } else if (result
== KEY_ERROR
) {
231 EXPECT_CALL(*this, KeyError(session_id_string_
,
232 MediaKeys::kUnknownError
, 0));
237 decryptor_
.AddKey(&key
[0], key
.size(), &key_id
[0], key_id
.size(),
241 void AddKeyAndExpect(const std::string
& key
, AddKeyExpectation result
) {
242 DCHECK(!key
.empty());
244 if (result
== KEY_ADDED
) {
245 EXPECT_CALL(*this, KeyAdded(session_id_string_
));
246 } else if (result
== KEY_ERROR
) {
248 KeyError(session_id_string_
, MediaKeys::kUnknownError
, 0));
253 decryptor_
.AddKey(reinterpret_cast<const uint8
*>(key
.c_str()), key
.length(),
258 MOCK_METHOD2(BufferDecrypted
, void(Decryptor::Status
,
259 const scoped_refptr
<DecoderBuffer
>&));
261 enum DecryptExpectation
{
264 DATA_AND_SIZE_MISMATCH
,
268 void DecryptAndExpect(const scoped_refptr
<DecoderBuffer
>& encrypted
,
269 const std::vector
<uint8
>& plain_text
,
270 DecryptExpectation result
) {
271 scoped_refptr
<DecoderBuffer
> decrypted
;
273 if (result
!= DECRYPT_ERROR
) {
274 EXPECT_CALL(*this, BufferDecrypted(Decryptor::kSuccess
, NotNull()))
275 .WillOnce(SaveArg
<1>(&decrypted
));
277 EXPECT_CALL(*this, BufferDecrypted(Decryptor::kError
, IsNull()))
278 .WillOnce(SaveArg
<1>(&decrypted
));
281 decryptor_
.Decrypt(Decryptor::kVideo
, encrypted
, decrypt_cb_
);
283 std::vector
<uint8
> decrypted_text
;
284 if (decrypted
&& decrypted
->data_size()) {
285 decrypted_text
.assign(
286 decrypted
->data(), decrypted
->data() + decrypted
->data_size());
291 EXPECT_EQ(plain_text
, decrypted_text
);
294 EXPECT_EQ(plain_text
.size(), decrypted_text
.size());
295 EXPECT_NE(plain_text
, decrypted_text
);
297 case DATA_AND_SIZE_MISMATCH
:
298 EXPECT_NE(plain_text
.size(), decrypted_text
.size());
301 EXPECT_TRUE(decrypted_text
.empty());
306 MOCK_METHOD1(KeyAdded
, void(const std::string
&));
307 MOCK_METHOD3(KeyError
, void(const std::string
&,
308 MediaKeys::KeyError
, int));
309 MOCK_METHOD3(KeyMessage
, void(const std::string
& session_id
,
310 const std::vector
<uint8
>& message
,
311 const std::string
& default_url
));
313 AesDecryptor decryptor_
;
314 std::string session_id_string_
;
315 AesDecryptor::DecryptCB decrypt_cb_
;
317 // Constants for testing.
318 const std::vector
<uint8
> original_data_
;
319 const std::vector
<uint8
> encrypted_data_
;
320 const std::vector
<uint8
> subsample_encrypted_data_
;
321 const std::vector
<uint8
> key_id_
;
322 const std::vector
<uint8
> iv_
;
323 const std::vector
<SubsampleEntry
> normal_subsample_entries_
;
324 const std::vector
<SubsampleEntry
> no_subsample_entries_
;
327 TEST_F(AesDecryptorTest
, GenerateKeyRequestWithNullInitData
) {
328 EXPECT_CALL(*this, KeyMessage(StrNe(std::string()), IsEmpty(), ""));
329 EXPECT_TRUE(decryptor_
.GenerateKeyRequest(std::string(), NULL
, 0));
332 TEST_F(AesDecryptorTest
, NormalDecryption
) {
333 GenerateKeyRequest(key_id_
);
334 AddKeyAndExpect(kKeyAsJWK
, KEY_ADDED
);
335 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
336 encrypted_data_
, key_id_
, iv_
, 0, no_subsample_entries_
);
337 DecryptAndExpect(encrypted_buffer
, original_data_
, SUCCESS
);
340 TEST_F(AesDecryptorTest
, DecryptionWithOffset
) {
341 GenerateKeyRequest(key_id_
);
342 AddKeyAndExpect(kKeyAsJWK
, KEY_ADDED
);
343 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
344 encrypted_data_
, key_id_
, iv_
, 23, no_subsample_entries_
);
345 DecryptAndExpect(encrypted_buffer
, original_data_
, SUCCESS
);
348 TEST_F(AesDecryptorTest
, UnencryptedFrame
) {
349 // An empty iv string signals that the frame is unencrypted.
350 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
351 original_data_
, key_id_
, std::vector
<uint8
>(), 0, no_subsample_entries_
);
352 DecryptAndExpect(encrypted_buffer
, original_data_
, SUCCESS
);
355 TEST_F(AesDecryptorTest
, WrongKey
) {
356 GenerateKeyRequest(key_id_
);
357 AddKeyAndExpect(kWrongKeyAsJWK
, KEY_ADDED
);
358 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
359 encrypted_data_
, key_id_
, iv_
, 0, no_subsample_entries_
);
360 DecryptAndExpect(encrypted_buffer
, original_data_
, DATA_MISMATCH
);
363 TEST_F(AesDecryptorTest
, NoKey
) {
364 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
365 encrypted_data_
, key_id_
, iv_
, 0, no_subsample_entries_
);
366 EXPECT_CALL(*this, BufferDecrypted(AesDecryptor::kNoKey
, IsNull()));
367 decryptor_
.Decrypt(Decryptor::kVideo
, encrypted_buffer
, decrypt_cb_
);
370 TEST_F(AesDecryptorTest
, KeyReplacement
) {
371 GenerateKeyRequest(key_id_
);
372 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
373 encrypted_data_
, key_id_
, iv_
, 0, no_subsample_entries_
);
375 AddKeyAndExpect(kWrongKeyAsJWK
, KEY_ADDED
);
376 ASSERT_NO_FATAL_FAILURE(DecryptAndExpect(
377 encrypted_buffer
, original_data_
, DATA_MISMATCH
));
379 AddKeyAndExpect(kKeyAsJWK
, KEY_ADDED
);
380 ASSERT_NO_FATAL_FAILURE(
381 DecryptAndExpect(encrypted_buffer
, original_data_
, SUCCESS
));
384 TEST_F(AesDecryptorTest
, WrongSizedKey
) {
385 GenerateKeyRequest(key_id_
);
386 AddKeyAndExpect(kWrongSizedKeyAsJWK
, KEY_ERROR
);
388 // Repeat for a raw key. Use "-1" to create a wrong sized key.
389 std::vector
<uint8
> wrong_sized_key(kKey
, kKey
+ arraysize(kKey
) - 1);
390 AddRawKeyAndExpect(key_id_
, wrong_sized_key
, KEY_ERROR
);
393 TEST_F(AesDecryptorTest
, MultipleKeysAndFrames
) {
394 GenerateKeyRequest(key_id_
);
395 AddKeyAndExpect(kKeyAsJWK
, KEY_ADDED
);
396 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
397 encrypted_data_
, key_id_
, iv_
, 10, no_subsample_entries_
);
398 ASSERT_NO_FATAL_FAILURE(
399 DecryptAndExpect(encrypted_buffer
, original_data_
, SUCCESS
));
401 AddKeyAndExpect(kKey2AsJWK
, KEY_ADDED
);
403 // The first key is still available after we added a second key.
404 ASSERT_NO_FATAL_FAILURE(
405 DecryptAndExpect(encrypted_buffer
, original_data_
, SUCCESS
));
407 // The second key is also available.
408 encrypted_buffer
= CreateEncryptedBuffer(
409 std::vector
<uint8
>(kEncryptedData2
,
410 kEncryptedData2
+ arraysize(kEncryptedData2
)),
411 std::vector
<uint8
>(kKeyId2
, kKeyId2
+ arraysize(kKeyId2
)),
412 std::vector
<uint8
>(kIv2
, kIv2
+ arraysize(kIv2
)),
414 no_subsample_entries_
);
415 ASSERT_NO_FATAL_FAILURE(DecryptAndExpect(
417 std::vector
<uint8
>(kOriginalData2
,
418 kOriginalData2
+ arraysize(kOriginalData2
) - 1),
422 TEST_F(AesDecryptorTest
, CorruptedIv
) {
423 GenerateKeyRequest(key_id_
);
424 AddKeyAndExpect(kKeyAsJWK
, KEY_ADDED
);
426 std::vector
<uint8
> bad_iv
= iv_
;
429 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
430 encrypted_data_
, key_id_
, bad_iv
, 0, no_subsample_entries_
);
432 DecryptAndExpect(encrypted_buffer
, original_data_
, DATA_MISMATCH
);
435 TEST_F(AesDecryptorTest
, CorruptedData
) {
436 GenerateKeyRequest(key_id_
);
437 AddKeyAndExpect(kKeyAsJWK
, KEY_ADDED
);
439 std::vector
<uint8
> bad_data
= encrypted_data_
;
442 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
443 bad_data
, key_id_
, iv_
, 0, no_subsample_entries_
);
444 DecryptAndExpect(encrypted_buffer
, original_data_
, DATA_MISMATCH
);
447 TEST_F(AesDecryptorTest
, EncryptedAsUnencryptedFailure
) {
448 GenerateKeyRequest(key_id_
);
449 AddKeyAndExpect(kKeyAsJWK
, KEY_ADDED
);
450 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
451 encrypted_data_
, key_id_
, std::vector
<uint8
>(), 0, no_subsample_entries_
);
452 DecryptAndExpect(encrypted_buffer
, original_data_
, DATA_MISMATCH
);
455 TEST_F(AesDecryptorTest
, SubsampleDecryption
) {
456 GenerateKeyRequest(key_id_
);
457 AddKeyAndExpect(kKeyAsJWK
, KEY_ADDED
);
458 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
459 subsample_encrypted_data_
, key_id_
, iv_
, 0, normal_subsample_entries_
);
460 DecryptAndExpect(encrypted_buffer
, original_data_
, SUCCESS
);
463 // Ensures noninterference of data offset and subsample mechanisms. We never
464 // expect to encounter this in the wild, but since the DecryptConfig doesn't
465 // disallow such a configuration, it should be covered.
466 TEST_F(AesDecryptorTest
, SubsampleDecryptionWithOffset
) {
467 GenerateKeyRequest(key_id_
);
468 AddKeyAndExpect(kKeyAsJWK
, KEY_ADDED
);
469 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
470 subsample_encrypted_data_
, key_id_
, iv_
, 23, normal_subsample_entries_
);
471 DecryptAndExpect(encrypted_buffer
, original_data_
, SUCCESS
);
474 TEST_F(AesDecryptorTest
, SubsampleWrongSize
) {
475 GenerateKeyRequest(key_id_
);
476 AddKeyAndExpect(kKeyAsJWK
, KEY_ADDED
);
478 std::vector
<SubsampleEntry
> subsample_entries_wrong_size(
479 kSubsampleEntriesWrongSize
,
480 kSubsampleEntriesWrongSize
+ arraysize(kSubsampleEntriesWrongSize
));
482 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
483 subsample_encrypted_data_
, key_id_
, iv_
, 0, subsample_entries_wrong_size
);
484 DecryptAndExpect(encrypted_buffer
, original_data_
, DATA_MISMATCH
);
487 TEST_F(AesDecryptorTest
, SubsampleInvalidTotalSize
) {
488 GenerateKeyRequest(key_id_
);
489 AddKeyAndExpect(kKeyAsJWK
, KEY_ADDED
);
491 std::vector
<SubsampleEntry
> subsample_entries_invalid_total_size(
492 kSubsampleEntriesInvalidTotalSize
,
493 kSubsampleEntriesInvalidTotalSize
+
494 arraysize(kSubsampleEntriesInvalidTotalSize
));
496 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
497 subsample_encrypted_data_
, key_id_
, iv_
, 0,
498 subsample_entries_invalid_total_size
);
499 DecryptAndExpect(encrypted_buffer
, original_data_
, DECRYPT_ERROR
);
502 // No cypher bytes in any of the subsamples.
503 TEST_F(AesDecryptorTest
, SubsampleClearBytesOnly
) {
504 GenerateKeyRequest(key_id_
);
505 AddKeyAndExpect(kKeyAsJWK
, KEY_ADDED
);
507 std::vector
<SubsampleEntry
> clear_only_subsample_entries(
508 kSubsampleEntriesClearOnly
,
509 kSubsampleEntriesClearOnly
+ arraysize(kSubsampleEntriesClearOnly
));
511 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
512 original_data_
, key_id_
, iv_
, 0, clear_only_subsample_entries
);
513 DecryptAndExpect(encrypted_buffer
, original_data_
, SUCCESS
);
516 // No clear bytes in any of the subsamples.
517 TEST_F(AesDecryptorTest
, SubsampleCypherBytesOnly
) {
518 GenerateKeyRequest(key_id_
);
519 AddKeyAndExpect(kKeyAsJWK
, KEY_ADDED
);
521 std::vector
<SubsampleEntry
> cypher_only_subsample_entries(
522 kSubsampleEntriesCypherOnly
,
523 kSubsampleEntriesCypherOnly
+ arraysize(kSubsampleEntriesCypherOnly
));
525 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
526 encrypted_data_
, key_id_
, iv_
, 0, cypher_only_subsample_entries
);
527 DecryptAndExpect(encrypted_buffer
, original_data_
, SUCCESS
);
530 TEST_F(AesDecryptorTest
, JWKKey
) {
531 // Try a simple JWK key (i.e. not in a set)
532 const std::string key1
=
535 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM=\","
536 " \"k\": \"FBUWFxgZGhscHR4fICEiIw==\""
538 AddKeyAndExpect(key1
, KEY_ERROR
);
540 // Try a key list with multiple entries.
541 const std::string key2
=
546 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM=\","
547 " \"k\": \"FBUWFxgZGhscHR4fICEiIw==\""
551 " \"kid\": \"JCUmJygpKissLS4vMA==\","
552 " \"k\":\"MTIzNDU2Nzg5Ojs8PT4/QA==\""
556 AddKeyAndExpect(key2
, KEY_ADDED
);
558 // Try a key with no spaces and some \n plus additional fields.
559 const std::string key3
=
560 "\n\n{\"something\":1,\"keys\":[{\n\n\"kty\":\"oct\",\"alg\":\"A128KW\","
561 "\"kid\":\"AAECAwQFBgcICQoLDA0ODxAREhM=\",\"k\":\"GawgguFyGrWKav7AX4VKUg="
562 "=\",\"foo\":\"bar\"}]}\n\n";
563 AddKeyAndExpect(key3
, KEY_ADDED
);
565 // Try some non-ASCII characters.
566 AddKeyAndExpect("This is not ASCII due to \xff\xfe\xfd in it.", KEY_ERROR
);
568 // Try a badly formatted key. Assume that the JSON parser is fully tested,
569 // so we won't try a lot of combinations. However, need a test to ensure
570 // that the code doesn't crash if invalid JSON received.
571 AddKeyAndExpect("This is not a JSON key.", KEY_ERROR
);
573 // Try passing some valid JSON that is not a dictionary at the top level.
574 AddKeyAndExpect("40", KEY_ERROR
);
576 // Try an empty dictionary.
577 AddKeyAndExpect("{ }", KEY_ERROR
);
579 // Try an empty 'keys' dictionary.
580 AddKeyAndExpect("{ \"keys\": [] }", KEY_ERROR
);
582 // Try with 'keys' not a dictionary.
583 AddKeyAndExpect("{ \"keys\":\"1\" }", KEY_ERROR
);
585 // Try with 'keys' a list of integers.
586 AddKeyAndExpect("{ \"keys\": [ 1, 2, 3 ] }", KEY_ERROR
);
588 // Try a key missing padding(=) at end of base64 string.
589 const std::string key4
=
594 " \"kid\": \"AAECAw==\","
595 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\""
599 AddKeyAndExpect(key4
, KEY_ERROR
);
601 // Try a key ID missing padding(=) at end of base64 string.
602 const std::string key5
=
607 " \"kid\": \"AAECAw\","
608 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw==\""
612 AddKeyAndExpect(key5
, KEY_ERROR
);
614 // Try a key with invalid base64 encoding.
615 const std::string key6
=
620 " \"kid\": \"!@#$%^&*()==\","
621 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw==\""
625 AddKeyAndExpect(key6
, KEY_ERROR
);
628 TEST_F(AesDecryptorTest
, RawKey
) {
629 // Verify that v0.1b keys (raw key) is still supported. Raw keys are
630 // 16 bytes long. Use the undecoded value of |kKey|.
631 GenerateKeyRequest(key_id_
);
633 key_id_
, std::vector
<uint8
>(kKey
, kKey
+ arraysize(kKey
)), KEY_ADDED
);
634 scoped_refptr
<DecoderBuffer
> encrypted_buffer
= CreateEncryptedBuffer(
635 encrypted_data_
, key_id_
, iv_
, 0, no_subsample_entries_
);
636 DecryptAndExpect(encrypted_buffer
, original_data_
, SUCCESS
);