Report errors from ChromiumEnv::GetChildren in Posix.
[chromium-blink-merge.git] / media / cdm / aes_decryptor_unittest.cc
blob4132b97c4743602c2dcb25a77c99f579549d6649
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.
5 #include <string>
6 #include <vector>
8 #include "base/basictypes.h"
9 #include "base/bind.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"
18 using ::testing::_;
19 using ::testing::Gt;
20 using ::testing::IsNull;
21 using ::testing::NotNull;
22 using ::testing::SaveArg;
23 using ::testing::StrNe;
25 MATCHER(IsEmpty, "") { return arg.empty(); }
27 namespace media {
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[] =
44 "{"
45 " \"keys\": ["
46 " {"
47 " \"kty\": \"oct\","
48 " \"kid\": \"AAECAw==\","
49 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw==\""
50 " }"
51 " ]"
52 "}";
54 const char kWrongKeyAsJWK[] =
55 "{"
56 " \"keys\": ["
57 " {"
58 " \"kty\": \"oct\","
59 " \"kid\": \"AAECAw==\","
60 " \"k\": \"7u7u7u7u7u7u7u7u7u7u7g==\""
61 " }"
62 " ]"
63 "}";
65 const char kWrongSizedKeyAsJWK[] =
66 "{"
67 " \"keys\": ["
68 " {"
69 " \"kty\": \"oct\","
70 " \"kid\": \"AAECAw==\","
71 " \"k\": \"AAECAw==\""
72 " }"
73 " ]"
74 "}";
76 const uint8 kIv[] = {
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[] =
113 " \"keys\": ["
114 " {"
115 " \"kty\": \"oct\","
116 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM=\","
117 " \"k\": \"FBUWFxgZGhscHR4fICEiIw==\""
118 " }"
119 " ]"
120 "}";
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
132 // valid.
134 const SubsampleEntry kSubsampleEntriesNormal[] = {
135 { 2, 7 },
136 { 3, 11 },
137 { 1, 0 }
140 const SubsampleEntry kSubsampleEntriesWrongSize[] = {
141 { 3, 6 }, // This entry doesn't match the correct entry.
142 { 3, 11 },
143 { 1, 0 }
146 const SubsampleEntry kSubsampleEntriesInvalidTotalSize[] = {
147 { 1, 1000 }, // This entry is too large.
148 { 3, 11 },
149 { 1, 0 }
152 const SubsampleEntry kSubsampleEntriesClearOnly[] = {
153 { 7, 0 },
154 { 8, 0 },
155 { 9, 0 }
158 const SubsampleEntry kSubsampleEntriesCypherOnly[] = {
159 { 0, 6 },
160 { 0, 8 },
161 { 0, 10 }
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,
168 int offset,
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]),
177 key_id.size());
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 {
186 public:
187 AesDecryptorTest()
188 : decryptor_(
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)) {
207 protected:
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 {
217 KEY_ADDED,
218 KEY_ERROR
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));
233 } else {
234 NOTREACHED();
237 decryptor_.AddKey(&key[0], key.size(), &key_id[0], key_id.size(),
238 session_id_string_);
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) {
247 EXPECT_CALL(*this,
248 KeyError(session_id_string_, MediaKeys::kUnknownError, 0));
249 } else {
250 NOTREACHED();
253 decryptor_.AddKey(reinterpret_cast<const uint8*>(key.c_str()), key.length(),
254 NULL, 0,
255 session_id_string_);
258 MOCK_METHOD2(BufferDecrypted, void(Decryptor::Status,
259 const scoped_refptr<DecoderBuffer>&));
261 enum DecryptExpectation {
262 SUCCESS,
263 DATA_MISMATCH,
264 DATA_AND_SIZE_MISMATCH,
265 DECRYPT_ERROR
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));
276 } else {
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());
289 switch (result) {
290 case SUCCESS:
291 EXPECT_EQ(plain_text, decrypted_text);
292 break;
293 case DATA_MISMATCH:
294 EXPECT_EQ(plain_text.size(), decrypted_text.size());
295 EXPECT_NE(plain_text, decrypted_text);
296 break;
297 case DATA_AND_SIZE_MISMATCH:
298 EXPECT_NE(plain_text.size(), decrypted_text.size());
299 break;
300 case DECRYPT_ERROR:
301 EXPECT_TRUE(decrypted_text.empty());
302 break;
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(
416 encrypted_buffer,
417 std::vector<uint8>(kOriginalData2,
418 kOriginalData2 + arraysize(kOriginalData2) - 1),
419 SUCCESS));
422 TEST_F(AesDecryptorTest, CorruptedIv) {
423 GenerateKeyRequest(key_id_);
424 AddKeyAndExpect(kKeyAsJWK, KEY_ADDED);
426 std::vector<uint8> bad_iv = iv_;
427 bad_iv[1]++;
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_;
440 bad_data[1]++;
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 =
534 " \"kty\": \"oct\","
535 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM=\","
536 " \"k\": \"FBUWFxgZGhscHR4fICEiIw==\""
537 "}";
538 AddKeyAndExpect(key1, KEY_ERROR);
540 // Try a key list with multiple entries.
541 const std::string key2 =
543 " \"keys\": ["
544 " {"
545 " \"kty\": \"oct\","
546 " \"kid\": \"AAECAwQFBgcICQoLDA0ODxAREhM=\","
547 " \"k\": \"FBUWFxgZGhscHR4fICEiIw==\""
548 " },"
549 " {"
550 " \"kty\": \"oct\","
551 " \"kid\": \"JCUmJygpKissLS4vMA==\","
552 " \"k\":\"MTIzNDU2Nzg5Ojs8PT4/QA==\""
553 " }"
554 " ]"
555 "}";
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 =
591 " \"keys\": ["
592 " {"
593 " \"kty\": \"oct\","
594 " \"kid\": \"AAECAw==\","
595 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw\""
596 " }"
597 " ]"
598 "}";
599 AddKeyAndExpect(key4, KEY_ERROR);
601 // Try a key ID missing padding(=) at end of base64 string.
602 const std::string key5 =
604 " \"keys\": ["
605 " {"
606 " \"kty\": \"oct\","
607 " \"kid\": \"AAECAw\","
608 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw==\""
609 " }"
610 " ]"
611 "}";
612 AddKeyAndExpect(key5, KEY_ERROR);
614 // Try a key with invalid base64 encoding.
615 const std::string key6 =
617 " \"keys\": ["
618 " {"
619 " \"kty\": \"oct\","
620 " \"kid\": \"!@#$%^&*()==\","
621 " \"k\": \"BAUGBwgJCgsMDQ4PEBESEw==\""
622 " }"
623 " ]"
624 "}";
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_);
632 AddRawKeyAndExpect(
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);
639 } // namespace media