OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 6 |
7 #include "media/base/data_buffer.h" | 7 #include "media/base/decoder_buffer.h" |
8 #include "media/base/decrypt_config.h" | 8 #include "media/base/decrypt_config.h" |
9 #include "media/crypto/aes_decryptor.h" | 9 #include "media/crypto/aes_decryptor.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace media { | 12 namespace media { |
13 | 13 |
14 // |kEncryptedData| is encrypted from |kOriginalData| using |kRightKey|, whose | 14 // |kEncryptedData| is encrypted from |kOriginalData| using |kRightKey|, whose |
15 // length is |kKeySize|. Modifying any of these independently would fail the | 15 // length is |kKeySize|. Modifying any of these independently would fail the |
16 // test. | 16 // test. |
17 static const char kOriginalData[] = "Original data."; | 17 static const char kOriginalData[] = "Original data."; |
18 static const int kEncryptedDataSize = 16; | 18 static const int kEncryptedDataSize = 16; |
19 static const unsigned char kEncryptedData[] = | 19 static const unsigned char kEncryptedData[] = |
20 "\x82\x3A\x76\x92\xEC\x7F\xF8\x85\xEC\x23\x52\xFB\x19\xB1\xB9\x09"; | 20 "\x82\x3A\x76\x92\xEC\x7F\xF8\x85\xEC\x23\x52\xFB\x19\xB1\xB9\x09"; |
21 static const int kKeySize = 16; | 21 static const int kKeySize = 16; |
22 static const unsigned char kRightKey[] = "A wonderful key!"; | 22 static const unsigned char kRightKey[] = "A wonderful key!"; |
23 static const unsigned char kWrongKey[] = "I'm a wrong key."; | 23 static const unsigned char kWrongKey[] = "I'm a wrong key."; |
24 static const int kKeyIdSize = 9; | 24 static const int kKeyIdSize = 9; |
25 static const unsigned char kKeyId1[] = "Key ID 1."; | 25 static const unsigned char kKeyId1[] = "Key ID 1."; |
26 static const unsigned char kKeyId2[] = "Key ID 2."; | 26 static const unsigned char kKeyId2[] = "Key ID 2."; |
27 | 27 |
28 class AesDecryptorTest : public testing::Test { | 28 class AesDecryptorTest : public testing::Test { |
29 public: | 29 public: |
30 AesDecryptorTest() { | 30 AesDecryptorTest() { |
31 encrypted_data_ = DataBuffer::CopyFrom(kEncryptedData, kEncryptedDataSize); | 31 encrypted_data_ = DecoderBuffer::CopyFrom( |
| 32 kEncryptedData, kEncryptedDataSize); |
32 } | 33 } |
33 | 34 |
34 protected: | 35 protected: |
35 void SetKeyIdForEncryptedData(const uint8* key_id, int key_id_size) { | 36 void SetKeyIdForEncryptedData(const uint8* key_id, int key_id_size) { |
36 encrypted_data_->SetDecryptConfig( | 37 encrypted_data_->SetDecryptConfig( |
37 scoped_ptr<DecryptConfig>(new DecryptConfig(key_id, key_id_size))); | 38 scoped_ptr<DecryptConfig>(new DecryptConfig(key_id, key_id_size))); |
38 } | 39 } |
39 | 40 |
40 void DecryptAndExpectToSucceed() { | 41 void DecryptAndExpectToSucceed() { |
41 scoped_refptr<Buffer> decrypted = decryptor_.Decrypt(encrypted_data_); | 42 scoped_refptr<DecoderBuffer> decrypted = |
| 43 decryptor_.Decrypt(encrypted_data_); |
42 ASSERT_TRUE(decrypted); | 44 ASSERT_TRUE(decrypted); |
43 int data_length = sizeof(kOriginalData) - 1; | 45 int data_length = sizeof(kOriginalData) - 1; |
44 ASSERT_EQ(data_length, decrypted->GetDataSize()); | 46 ASSERT_EQ(data_length, decrypted->GetDataSize()); |
45 EXPECT_EQ(0, memcmp(kOriginalData, decrypted->GetData(), data_length)); | 47 EXPECT_EQ(0, memcmp(kOriginalData, decrypted->GetData(), data_length)); |
46 } | 48 } |
47 | 49 |
48 void DecryptAndExpectToFail() { | 50 void DecryptAndExpectToFail() { |
49 scoped_refptr<Buffer> decrypted = decryptor_.Decrypt(encrypted_data_); | 51 scoped_refptr<DecoderBuffer> decrypted = |
| 52 decryptor_.Decrypt(encrypted_data_); |
50 EXPECT_FALSE(decrypted); | 53 EXPECT_FALSE(decrypted); |
51 } | 54 } |
52 | 55 |
53 scoped_refptr<DataBuffer> encrypted_data_; | 56 scoped_refptr<DecoderBuffer> encrypted_data_; |
54 AesDecryptor decryptor_; | 57 AesDecryptor decryptor_; |
55 }; | 58 }; |
56 | 59 |
57 TEST_F(AesDecryptorTest, NormalDecryption) { | 60 TEST_F(AesDecryptorTest, NormalDecryption) { |
58 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); | 61 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); |
59 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); | 62 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); |
60 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); | 63 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); |
61 } | 64 } |
62 | 65 |
63 TEST_F(AesDecryptorTest, WrongKey) { | 66 TEST_F(AesDecryptorTest, WrongKey) { |
(...skipping 11 matching lines...) Expand all Loading... |
75 | 78 |
76 TEST_F(AesDecryptorTest, KeyReplacement) { | 79 TEST_F(AesDecryptorTest, KeyReplacement) { |
77 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); | 80 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); |
78 decryptor_.AddKey(kKeyId1, kKeyIdSize, kWrongKey, kKeySize); | 81 decryptor_.AddKey(kKeyId1, kKeyIdSize, kWrongKey, kKeySize); |
79 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail()); | 82 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail()); |
80 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); | 83 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); |
81 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); | 84 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); |
82 } | 85 } |
83 | 86 |
84 } // media | 87 } // media |
OLD | NEW |