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

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: 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
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
12 namespace media { 16 namespace media {
13 17
14 // |kEncryptedData| is encrypted from |kOriginalData| using |kRightKey|, whose 18 static const char kClearKeySystem[] = "org.w3.clearkey";
15 // length is |kKeySize|. Modifying any of these independently would fail the 19 // |kEncryptedData| is encrypted from |kOriginalData| using |kRightKey|.
16 // test. 20 // Modifying any of these independently would fail the test.
17 static const char kOriginalData[] = "Original data."; 21 static const uint8 kOriginalData[] = {
18 static const int kEncryptedDataSize = 16; 22 // Original data.
ddorwin 2012/06/11 21:02:40 remove redundant comments here and below.
xhwang 2012/06/12 19:01:15 Done.
19 static const unsigned char kEncryptedData[] = 23 0x4f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x61, 0x6c,
scherkus (not reviewing) 2012/06/12 03:15:58 2-space indent here + below (we treat it same as c
xhwang 2012/06/12 19:01:15 Done.
20 "\x82\x3A\x76\x92\xEC\x7F\xF8\x85\xEC\x23\x52\xFB\x19\xB1\xB9\x09"; 24 0x20, 0x64, 0x61, 0x74, 0x61, 0x2e
21 static const int kKeySize = 16; 25 };
22 static const unsigned char kRightKey[] = "A wonderful key!"; 26 static const uint8 kEncryptedData[] = {
23 static const unsigned char kWrongKey[] = "I'm a wrong key."; 27 0x82, 0x3A, 0x76, 0x92, 0xEC, 0x7F, 0xF8, 0x85,
24 static const int kKeyIdSize = 9; 28 0xEC, 0x23, 0x52, 0xFB, 0x19, 0xB1, 0xB9, 0x09
25 static const unsigned char kKeyId1[] = "Key ID 1."; 29 };
26 static const unsigned char kKeyId2[] = "Key ID 2."; 30 static const uint8 kRightKey[] = {
31 // A wonderful key!
32 0x41, 0x20, 0x77, 0x6f, 0x6e, 0x64, 0x65, 0x72,
33 0x66, 0x75, 0x6c, 0x20, 0x6b, 0x65, 0x79, 0x21
34 };
35 static const uint8 kWrongKey[] = {
36 // I'm a wrong key.
37 0x49, 0x27, 0x6d, 0x20, 0x61, 0x20, 0x77, 0x72,
38 0x6f, 0x6e, 0x67, 0x20, 0x6b, 0x65, 0x79, 0x2e
39 };
40 static const uint8 kWrongSizedKey[] = { 0x20, 0x20 };
41 static const uint8 kKeyId1[] = {
42 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44, 0x20, 0x31
43 };
44 static const uint8 kKeyId2[] = {
45 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44, 0x20, 0x32
46 };
27 47
28 class AesDecryptorTest : public testing::Test { 48 class AesDecryptorTest : public testing::Test {
29 public: 49 public:
30 AesDecryptorTest() { 50 AesDecryptorTest() {
31 encrypted_data_ = DecoderBuffer::CopyFrom( 51 encrypted_data_ = DecoderBuffer::CopyFrom(kEncryptedData,
32 kEncryptedData, kEncryptedDataSize); 52 arraysize(kEncryptedData));
53 decryptor_.Init(&client_);
33 } 54 }
34 55
35 protected: 56 protected:
36 void SetKeyIdForEncryptedData(const uint8* key_id, int key_id_size) { 57 template <int key_id_size, int key_size>
scherkus (not reviewing) 2012/06/12 03:15:58 template args use CamelCase as they're typically t
xhwang 2012/06/12 19:01:15 Done.
58 void AddKeyAndExpectToSucceed(const uint8 (&key_id)[key_id_size],
59 const uint8 (&key)[key_size]) {
60 EXPECT_CALL(client_, KeyAdded(kClearKeySystem, ""));
61 decryptor_.AddKey(kClearKeySystem, key, key_size, key_id, key_id_size, "");
62 }
63
64 template <int key_id_size, int key_size>
scherkus (not reviewing) 2012/06/12 03:15:58 ditto
xhwang 2012/06/12 19:01:15 Done.
65 void AddKeyAndExpectToFail(const uint8 (&key_id)[key_id_size],
66 const uint8 (&key)[key_size]) {
67 EXPECT_CALL(client_, KeyError(kClearKeySystem, "", _, 0));
ddorwin 2012/06/11 21:02:40 "" - are you not passing a session ID in the tests
xhwang 2012/06/12 19:01:15 Done.
68 decryptor_.AddKey(kClearKeySystem, key, key_size, key_id, key_id_size, "");
69 }
70
71 template <int key_id_size>
72 void SetKeyIdForEncryptedData(const uint8 (&key_id)[key_id_size]) {
37 encrypted_data_->SetDecryptConfig( 73 encrypted_data_->SetDecryptConfig(
38 scoped_ptr<DecryptConfig>(new DecryptConfig(key_id, key_id_size))); 74 scoped_ptr<DecryptConfig>(new DecryptConfig(key_id, key_id_size)));
39 } 75 }
40 76
41 void DecryptAndExpectToSucceed() { 77 void DecryptAndExpectToSucceed() {
42 scoped_refptr<DecoderBuffer> decrypted = 78 scoped_refptr<DecoderBuffer> decrypted =
43 decryptor_.Decrypt(encrypted_data_); 79 decryptor_.Decrypt(encrypted_data_);
44 ASSERT_TRUE(decrypted); 80 ASSERT_TRUE(decrypted);
45 int data_length = sizeof(kOriginalData) - 1; 81 int data_length = sizeof(kOriginalData);
46 ASSERT_EQ(data_length, decrypted->GetDataSize()); 82 ASSERT_EQ(data_length, decrypted->GetDataSize());
47 EXPECT_EQ(0, memcmp(kOriginalData, decrypted->GetData(), data_length)); 83 EXPECT_EQ(0, memcmp(kOriginalData, decrypted->GetData(), data_length));
48 } 84 }
49 85
50 void DecryptAndExpectToFail() { 86 void DecryptAndExpectToFail() {
51 scoped_refptr<DecoderBuffer> decrypted = 87 scoped_refptr<DecoderBuffer> decrypted =
52 decryptor_.Decrypt(encrypted_data_); 88 decryptor_.Decrypt(encrypted_data_);
53 EXPECT_FALSE(decrypted); 89 EXPECT_FALSE(decrypted);
54 } 90 }
55 91
56 scoped_refptr<DecoderBuffer> encrypted_data_; 92 scoped_refptr<DecoderBuffer> encrypted_data_;
93 MockDecryptorClient client_;
57 AesDecryptor decryptor_; 94 AesDecryptor decryptor_;
58 }; 95 };
59 96
60 TEST_F(AesDecryptorTest, NormalDecryption) { 97 TEST_F(AesDecryptorTest, NormalDecryption) {
61 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); 98 AddKeyAndExpectToSucceed(kKeyId1, kRightKey);
62 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); 99 SetKeyIdForEncryptedData(kKeyId1);
63 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); 100 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed());
64 } 101 }
65 102
66 TEST_F(AesDecryptorTest, WrongKey) { 103 TEST_F(AesDecryptorTest, WrongKey) {
67 decryptor_.AddKey(kKeyId1, kKeyIdSize, kWrongKey, kKeySize); 104 AddKeyAndExpectToSucceed(kKeyId1, kWrongKey);
68 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); 105 SetKeyIdForEncryptedData(kKeyId1);
69 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail()); 106 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail());
70 } 107 }
71 108
72 TEST_F(AesDecryptorTest, MultipleKeys) { 109 TEST_F(AesDecryptorTest, MultipleKeys) {
73 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); 110 AddKeyAndExpectToSucceed(kKeyId1, kRightKey);
74 decryptor_.AddKey(kKeyId2, kKeyIdSize, kWrongKey, kKeySize); 111 AddKeyAndExpectToSucceed(kKeyId2, kWrongKey);
75 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); 112 SetKeyIdForEncryptedData(kKeyId1);
76 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); 113 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed());
77 } 114 }
78 115
79 TEST_F(AesDecryptorTest, KeyReplacement) { 116 TEST_F(AesDecryptorTest, KeyReplacement) {
80 SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize); 117 SetKeyIdForEncryptedData(kKeyId1);
81 decryptor_.AddKey(kKeyId1, kKeyIdSize, kWrongKey, kKeySize); 118 AddKeyAndExpectToSucceed(kKeyId1, kWrongKey);
82 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail()); 119 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail());
83 decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize); 120 AddKeyAndExpectToSucceed(kKeyId1, kRightKey);
84 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed()); 121 ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed());
85 } 122 }
86 123
124 TEST_F(AesDecryptorTest, WrongSizedKey) {
125 AddKeyAndExpectToFail(kKeyId1, kWrongSizedKey);
126 }
127
128 TEST_F(AesDecryptorTest, GenerateKeyRequest) {
129 const uint8 init_data[] = "InitData";
ddorwin 2012/06/11 21:02:40 kInitData
xhwang 2012/06/12 19:01:15 Done.
130 EXPECT_CALL(client_, KeyMessageMock(kClearKeySystem, _, _, _, _));
ddorwin 2012/06/11 21:02:40 I think you can pretty easily check for: * non-emp
xhwang 2012/06/12 19:01:15 Done.
131 decryptor_.GenerateKeyRequest(kClearKeySystem, init_data, sizeof(init_data));
132 }
133
87 } // media 134 } // media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698