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/decoder_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 = 14; |
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 "\x0\x93\x3d\xf6\xd6\x53\xfc\xcd\x9c\x53\x46\xed\x8c\x60"; |
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 static const uint64 kIv = 0; |
| 28 static const int kDummyHmacDataSize = 12; |
| 29 static const unsigned char kDummyHmacData[] = "Unused HMAC."; |
27 | 30 |
28 class AesDecryptorTest : public testing::Test { | 31 class AesDecryptorTest : public testing::Test { |
29 public: | 32 public: |
30 AesDecryptorTest() { | 33 AesDecryptorTest() { |
31 encrypted_data_ = DecoderBuffer::CopyFrom( | 34 encrypted_data_ = DecoderBuffer::CopyFrom( |
32 kEncryptedData, kEncryptedDataSize); | 35 kEncryptedData, kEncryptedDataSize); |
33 } | 36 } |
34 | 37 |
35 protected: | 38 protected: |
36 void SetKeyIdForEncryptedData(const uint8* key_id, int key_id_size) { | 39 void SetKeyIdForEncryptedData(const uint8* key_id, int key_id_size) { |
37 encrypted_data_->SetDecryptConfig( | 40 encrypted_data_->SetDecryptConfig( |
38 scoped_ptr<DecryptConfig>(new DecryptConfig(key_id, key_id_size))); | 41 scoped_ptr<DecryptConfig>(new DecryptConfig(kDummyHmacData, |
| 42 kDummyHmacDataSize, |
| 43 kIv, |
| 44 key_id, key_id_size))); |
39 } | 45 } |
40 | 46 |
41 void DecryptAndExpectToSucceed() { | 47 void DecryptAndExpectToSucceed() { |
42 scoped_refptr<DecoderBuffer> decrypted = | 48 scoped_refptr<DecoderBuffer> decrypted = |
43 decryptor_.Decrypt(encrypted_data_); | 49 decryptor_.Decrypt(encrypted_data_); |
44 ASSERT_TRUE(decrypted); | 50 ASSERT_TRUE(decrypted); |
45 int data_length = sizeof(kOriginalData) - 1; | 51 int data_length = sizeof(kOriginalData) - 1; |
46 ASSERT_EQ(data_length, decrypted->GetDataSize()); | 52 ASSERT_EQ(data_length, decrypted->GetDataSize()); |
47 EXPECT_EQ(0, memcmp(kOriginalData, decrypted->GetData(), data_length)); | 53 EXPECT_EQ(0, memcmp(kOriginalData, decrypted->GetData(), data_length)); |
48 } | 54 } |
49 | 55 |
50 void DecryptAndExpectToFail() { | 56 void DecryptAndExpectToFail() { |
51 scoped_refptr<DecoderBuffer> decrypted = | 57 scoped_refptr<DecoderBuffer> decrypted = |
52 decryptor_.Decrypt(encrypted_data_); | 58 decryptor_.Decrypt(encrypted_data_); |
53 EXPECT_FALSE(decrypted); | 59 EXPECT_TRUE(decrypted); |
| 60 EXPECT_NE(0, memcmp(kOriginalData, |
| 61 decrypted->GetData(), |
| 62 decrypted->GetDataSize())); |
54 } | 63 } |
55 | 64 |
56 scoped_refptr<DecoderBuffer> encrypted_data_; | 65 scoped_refptr<DecoderBuffer> encrypted_data_; |
57 AesDecryptor decryptor_; | 66 AesDecryptor decryptor_; |
58 }; | 67 }; |
59 | 68 |
60 TEST_F(AesDecryptorTest, NormalDecryption) { | 69 TEST_F(AesDecryptorTest, NormalDecryption) { |
61 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); | 70 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); |
62 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); | 71 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); |
63 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); | 72 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); |
(...skipping 14 matching lines...) Expand all Loading... |
78 | 87 |
79 TEST_F(AesDecryptorTest, KeyReplacement) { | 88 TEST_F(AesDecryptorTest, KeyReplacement) { |
80 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); | 89 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); |
81 decryptor_.AddKey(kKeyId1, kKeyIdSize, kWrongKey, kKeySize); | 90 decryptor_.AddKey(kKeyId1, kKeyIdSize, kWrongKey, kKeySize); |
82 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail()); | 91 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail()); |
83 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); | 92 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); |
84 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); | 93 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); |
85 } | 94 } |
86 | 95 |
87 } // media | 96 } // media |
OLD | NEW |