Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(241)

Side by Side Diff: media/crypto/aes_decryptor_unittest.cc

Issue 10534096: Generalize AesDecryptor to make it more spec compliant. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/crypto/aes_decryptor.cc ('k') | media/crypto/decryptor_client.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "base/basictypes.h"
7 #include "media/base/decoder_buffer.h" 8 #include "media/base/decoder_buffer.h"
8 #include "media/base/decrypt_config.h" 9 #include "media/base/decrypt_config.h"
10 #include "media/base/mock_filters.h"
9 #include "media/crypto/aes_decryptor.h" 11 #include "media/crypto/aes_decryptor.h"
10 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
11 13
14 using ::testing::_;
15 using ::testing::Gt;
16 using ::testing::NotNull;
17 using ::testing::SaveArg;
18 using ::testing::StrNe;
19
12 namespace media { 20 namespace media {
13 21
14 // |kEncryptedData| is encrypted from |kOriginalData| using |kRightKey|, whose 22 static const char kClearKeySystem[] = "org.w3.clearkey";
15 // length is |kKeySize|. Modifying any of these independently would fail the 23 static const uint8 kInitData[] = { 0x69, 0x6e, 0x69, 0x74 };
16 // test. 24 // |kEncryptedData| is encrypted from |kOriginalData| using |kRightKey|.
17 static const char kOriginalData[] = "Original data."; 25 // Modifying any of these independently would fail the test.
18 static const int kEncryptedDataSize = 16; 26 static const uint8 kOriginalData[] = {
19 static const unsigned char kEncryptedData[] = 27 0x4f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x61, 0x6c,
20 "\x82\x3A\x76\x92\xEC\x7F\xF8\x85\xEC\x23\x52\xFB\x19\xB1\xB9\x09"; 28 0x20, 0x64, 0x61, 0x74, 0x61, 0x2e
21 static const int kKeySize = 16; 29 };
22 static const unsigned char kRightKey[] = "A wonderful key!"; 30 static const uint8 kEncryptedData[] = {
23 static const unsigned char kWrongKey[] = "I'm a wrong key."; 31 0x82, 0x3A, 0x76, 0x92, 0xEC, 0x7F, 0xF8, 0x85,
24 static const int kKeyIdSize = 9; 32 0xEC, 0x23, 0x52, 0xFB, 0x19, 0xB1, 0xB9, 0x09
25 static const unsigned char kKeyId1[] = "Key ID 1."; 33 };
26 static const unsigned char kKeyId2[] = "Key ID 2."; 34 static const uint8 kRightKey[] = {
35 0x41, 0x20, 0x77, 0x6f, 0x6e, 0x64, 0x65, 0x72,
36 0x66, 0x75, 0x6c, 0x20, 0x6b, 0x65, 0x79, 0x21
37 };
38 static const uint8 kWrongKey[] = {
39 0x49, 0x27, 0x6d, 0x20, 0x61, 0x20, 0x77, 0x72,
40 0x6f, 0x6e, 0x67, 0x20, 0x6b, 0x65, 0x79, 0x2e
41 };
42 static const uint8 kWrongSizedKey[] = { 0x20, 0x20 };
43 static const uint8 kKeyId1[] = {
44 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44, 0x20, 0x31
45 };
46 static const uint8 kKeyId2[] = {
47 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44, 0x20, 0x32
48 };
27 49
28 class AesDecryptorTest : public testing::Test { 50 class AesDecryptorTest : public testing::Test {
29 public: 51 public:
30 AesDecryptorTest() { 52 AesDecryptorTest() : decryptor_(&client_) {
31 encrypted_data_ = DecoderBuffer::CopyFrom( 53 encrypted_data_ = DecoderBuffer::CopyFrom(kEncryptedData,
32 kEncryptedData, kEncryptedDataSize); 54 arraysize(kEncryptedData));
33 } 55 }
34 56
35 protected: 57 protected:
36 void SetKeyIdForEncryptedData(const uint8* key_id, int key_id_size) { 58 void GenerateKeyRequest() {
59 EXPECT_CALL(client_, KeyMessageMock(kClearKeySystem, StrNe(std::string()),
60 NotNull(), Gt(0), ""))
61 .WillOnce(SaveArg<1>(&session_id_string_));
62 decryptor_.GenerateKeyRequest(kClearKeySystem,
63 kInitData, arraysize(kInitData));
64 }
65
66 template <int KeyIdSize, int KeySize>
67 void AddKeyAndExpectToSucceed(const uint8 (&key_id)[KeyIdSize],
68 const uint8 (&key)[KeySize]) {
69 EXPECT_CALL(client_, KeyAdded(kClearKeySystem, session_id_string_));
70 decryptor_.AddKey(kClearKeySystem, key, KeySize, key_id, KeyIdSize,
71 session_id_string_);
72 }
73
74 template <int KeyIdSize, int KeySize>
75 void AddKeyAndExpectToFail(const uint8 (&key_id)[KeyIdSize],
76 const uint8 (&key)[KeySize]) {
77 EXPECT_CALL(client_, KeyError(kClearKeySystem, session_id_string_,
78 AesDecryptor::kUnknownError, 0));
79 decryptor_.AddKey(kClearKeySystem, key, KeySize, key_id, KeyIdSize,
80 session_id_string_);
81 }
82
83 template <int KeyIdSize>
84 void SetKeyIdForEncryptedData(const uint8 (&key_id)[KeyIdSize]) {
37 encrypted_data_->SetDecryptConfig( 85 encrypted_data_->SetDecryptConfig(
38 scoped_ptr<DecryptConfig>(new DecryptConfig(key_id, key_id_size))); 86 scoped_ptr<DecryptConfig>(new DecryptConfig(key_id, KeyIdSize)));
39 } 87 }
40 88
41 void DecryptAndExpectToSucceed() { 89 void DecryptAndExpectToSucceed() {
42 scoped_refptr<DecoderBuffer> decrypted = 90 scoped_refptr<DecoderBuffer> decrypted =
43 decryptor_.Decrypt(encrypted_data_); 91 decryptor_.Decrypt(encrypted_data_);
44 ASSERT_TRUE(decrypted); 92 ASSERT_TRUE(decrypted);
45 int data_length = sizeof(kOriginalData) - 1; 93 int data_length = sizeof(kOriginalData);
46 ASSERT_EQ(data_length, decrypted->GetDataSize()); 94 ASSERT_EQ(data_length, decrypted->GetDataSize());
47 EXPECT_EQ(0, memcmp(kOriginalData, decrypted->GetData(), data_length)); 95 EXPECT_EQ(0, memcmp(kOriginalData, decrypted->GetData(), data_length));
48 } 96 }
49 97
50 void DecryptAndExpectToFail() { 98 void DecryptAndExpectToFail() {
51 scoped_refptr<DecoderBuffer> decrypted = 99 scoped_refptr<DecoderBuffer> decrypted =
52 decryptor_.Decrypt(encrypted_data_); 100 decryptor_.Decrypt(encrypted_data_);
53 EXPECT_FALSE(decrypted); 101 EXPECT_FALSE(decrypted);
54 } 102 }
55 103
56 scoped_refptr<DecoderBuffer> encrypted_data_; 104 scoped_refptr<DecoderBuffer> encrypted_data_;
105 MockDecryptorClient client_;
57 AesDecryptor decryptor_; 106 AesDecryptor decryptor_;
107 std::string session_id_string_;
58 }; 108 };
59 109
60 TEST_F(AesDecryptorTest, NormalDecryption) { 110 TEST_F(AesDecryptorTest, NormalDecryption) {
61 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); 111 GenerateKeyRequest();
62 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); 112 AddKeyAndExpectToSucceed(kKeyId1, kRightKey);
113 SetKeyIdForEncryptedData(kKeyId1);
63 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); 114 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed());
64 } 115 }
65 116
66 TEST_F(AesDecryptorTest, WrongKey) { 117 TEST_F(AesDecryptorTest, WrongKey) {
67 decryptor_.AddKey(kKeyId1, kKeyIdSize, kWrongKey, kKeySize); 118 GenerateKeyRequest();
68 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); 119 AddKeyAndExpectToSucceed(kKeyId1, kWrongKey);
120 SetKeyIdForEncryptedData(kKeyId1);
69 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail()); 121 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail());
70 } 122 }
71 123
72 TEST_F(AesDecryptorTest, MultipleKeys) { 124 TEST_F(AesDecryptorTest, MultipleKeys) {
73 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); 125 GenerateKeyRequest();
74 decryptor_.AddKey(kKeyId2, kKeyIdSize, kWrongKey, kKeySize); 126 AddKeyAndExpectToSucceed(kKeyId1, kRightKey);
75 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); 127 AddKeyAndExpectToSucceed(kKeyId2, kWrongKey);
128 SetKeyIdForEncryptedData(kKeyId1);
76 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); 129 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed());
77 } 130 }
78 131
79 TEST_F(AesDecryptorTest, KeyReplacement) { 132 TEST_F(AesDecryptorTest, KeyReplacement) {
80 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); 133 GenerateKeyRequest();
81 decryptor_.AddKey(kKeyId1, kKeyIdSize, kWrongKey, kKeySize); 134 SetKeyIdForEncryptedData(kKeyId1);
135 AddKeyAndExpectToSucceed(kKeyId1, kWrongKey);
82 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail()); 136 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail());
83 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); 137 AddKeyAndExpectToSucceed(kKeyId1, kRightKey);
84 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); 138 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed());
85 } 139 }
86 140
141 TEST_F(AesDecryptorTest, WrongSizedKey) {
142 GenerateKeyRequest();
143 AddKeyAndExpectToFail(kKeyId1, kWrongSizedKey);
144 }
145
87 } // media 146 } // media
OLDNEW
« no previous file with comments | « media/crypto/aes_decryptor.cc ('k') | media/crypto/decryptor_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698