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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: media/crypto/aes_decryptor_unittest.cc
diff --git a/media/crypto/aes_decryptor_unittest.cc b/media/crypto/aes_decryptor_unittest.cc
index 0b33f61f043ed147e8a527512bffbebe36c2b541..e9c159f1f1e8184bb84b0f3f1347e64564e9f32d 100644
--- a/media/crypto/aes_decryptor_unittest.cc
+++ b/media/crypto/aes_decryptor_unittest.cc
@@ -4,36 +4,72 @@
#include <string>
+#include "base/basictypes.h"
#include "media/base/decoder_buffer.h"
#include "media/base/decrypt_config.h"
+#include "media/base/mock_filters.h"
#include "media/crypto/aes_decryptor.h"
#include "testing/gtest/include/gtest/gtest.h"
+using ::testing::_;
+
namespace media {
-// |kEncryptedData| is encrypted from |kOriginalData| using |kRightKey|, whose
-// length is |kKeySize|. Modifying any of these independently would fail the
-// test.
-static const char kOriginalData[] = "Original data.";
-static const int kEncryptedDataSize = 16;
-static const unsigned char kEncryptedData[] =
- "\x82\x3A\x76\x92\xEC\x7F\xF8\x85\xEC\x23\x52\xFB\x19\xB1\xB9\x09";
-static const int kKeySize = 16;
-static const unsigned char kRightKey[] = "A wonderful key!";
-static const unsigned char kWrongKey[] = "I'm a wrong key.";
-static const int kKeyIdSize = 9;
-static const unsigned char kKeyId1[] = "Key ID 1.";
-static const unsigned char kKeyId2[] = "Key ID 2.";
+static const char kClearKeySystem[] = "org.w3.clearkey";
+// |kEncryptedData| is encrypted from |kOriginalData| using |kRightKey|.
+// Modifying any of these independently would fail the test.
+static const uint8 kOriginalData[] = {
+ // Original data.
ddorwin 2012/06/11 21:02:40 remove redundant comments here and below.
xhwang 2012/06/12 19:01:15 Done.
+ 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.
+ 0x20, 0x64, 0x61, 0x74, 0x61, 0x2e
+};
+static const uint8 kEncryptedData[] = {
+ 0x82, 0x3A, 0x76, 0x92, 0xEC, 0x7F, 0xF8, 0x85,
+ 0xEC, 0x23, 0x52, 0xFB, 0x19, 0xB1, 0xB9, 0x09
+};
+static const uint8 kRightKey[] = {
+ // A wonderful key!
+ 0x41, 0x20, 0x77, 0x6f, 0x6e, 0x64, 0x65, 0x72,
+ 0x66, 0x75, 0x6c, 0x20, 0x6b, 0x65, 0x79, 0x21
+};
+static const uint8 kWrongKey[] = {
+ // I'm a wrong key.
+ 0x49, 0x27, 0x6d, 0x20, 0x61, 0x20, 0x77, 0x72,
+ 0x6f, 0x6e, 0x67, 0x20, 0x6b, 0x65, 0x79, 0x2e
+};
+static const uint8 kWrongSizedKey[] = { 0x20, 0x20 };
+static const uint8 kKeyId1[] = {
+ 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44, 0x20, 0x31
+};
+static const uint8 kKeyId2[] = {
+ 0x4b, 0x65, 0x79, 0x20, 0x49, 0x44, 0x20, 0x32
+};
class AesDecryptorTest : public testing::Test {
public:
AesDecryptorTest() {
- encrypted_data_ = DecoderBuffer::CopyFrom(
- kEncryptedData, kEncryptedDataSize);
+ encrypted_data_ = DecoderBuffer::CopyFrom(kEncryptedData,
+ arraysize(kEncryptedData));
+ decryptor_.Init(&client_);
}
protected:
- void SetKeyIdForEncryptedData(const uint8* key_id, int key_id_size) {
+ 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.
+ void AddKeyAndExpectToSucceed(const uint8 (&key_id)[key_id_size],
+ const uint8 (&key)[key_size]) {
+ EXPECT_CALL(client_, KeyAdded(kClearKeySystem, ""));
+ decryptor_.AddKey(kClearKeySystem, key, key_size, key_id, key_id_size, "");
+ }
+
+ 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.
+ void AddKeyAndExpectToFail(const uint8 (&key_id)[key_id_size],
+ const uint8 (&key)[key_size]) {
+ 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.
+ decryptor_.AddKey(kClearKeySystem, key, key_size, key_id, key_id_size, "");
+ }
+
+ template <int key_id_size>
+ void SetKeyIdForEncryptedData(const uint8 (&key_id)[key_id_size]) {
encrypted_data_->SetDecryptConfig(
scoped_ptr<DecryptConfig>(new DecryptConfig(key_id, key_id_size)));
}
@@ -42,7 +78,7 @@ class AesDecryptorTest : public testing::Test {
scoped_refptr<DecoderBuffer> decrypted =
decryptor_.Decrypt(encrypted_data_);
ASSERT_TRUE(decrypted);
- int data_length = sizeof(kOriginalData) - 1;
+ int data_length = sizeof(kOriginalData);
ASSERT_EQ(data_length, decrypted->GetDataSize());
EXPECT_EQ(0, memcmp(kOriginalData, decrypted->GetData(), data_length));
}
@@ -54,34 +90,45 @@ class AesDecryptorTest : public testing::Test {
}
scoped_refptr<DecoderBuffer> encrypted_data_;
+ MockDecryptorClient client_;
AesDecryptor decryptor_;
};
TEST_F(AesDecryptorTest, NormalDecryption) {
- decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize);
- SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize);
+ AddKeyAndExpectToSucceed(kKeyId1, kRightKey);
+ SetKeyIdForEncryptedData(kKeyId1);
ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed());
}
TEST_F(AesDecryptorTest, WrongKey) {
- decryptor_.AddKey(kKeyId1, kKeyIdSize, kWrongKey, kKeySize);
- SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize);
+ AddKeyAndExpectToSucceed(kKeyId1, kWrongKey);
+ SetKeyIdForEncryptedData(kKeyId1);
ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail());
}
TEST_F(AesDecryptorTest, MultipleKeys) {
- decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize);
- decryptor_.AddKey(kKeyId2, kKeyIdSize, kWrongKey, kKeySize);
- SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize);
+ AddKeyAndExpectToSucceed(kKeyId1, kRightKey);
+ AddKeyAndExpectToSucceed(kKeyId2, kWrongKey);
+ SetKeyIdForEncryptedData(kKeyId1);
ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed());
}
TEST_F(AesDecryptorTest, KeyReplacement) {
- SetKeyIdForEncryptedData(kKeyId1, kKeyIdSize);
- decryptor_.AddKey(kKeyId1, kKeyIdSize, kWrongKey, kKeySize);
+ SetKeyIdForEncryptedData(kKeyId1);
+ AddKeyAndExpectToSucceed(kKeyId1, kWrongKey);
ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToFail());
- decryptor_.AddKey(kKeyId1, kKeyIdSize, kRightKey, kKeySize);
+ AddKeyAndExpectToSucceed(kKeyId1, kRightKey);
ASSERT_NO_FATAL_FAILURE(DecryptAndExpectToSucceed());
}
+TEST_F(AesDecryptorTest, WrongSizedKey) {
+ AddKeyAndExpectToFail(kKeyId1, kWrongSizedKey);
+}
+
+TEST_F(AesDecryptorTest, GenerateKeyRequest) {
+ const uint8 init_data[] = "InitData";
ddorwin 2012/06/11 21:02:40 kInitData
xhwang 2012/06/12 19:01:15 Done.
+ 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.
+ decryptor_.GenerateKeyRequest(kClearKeySystem, init_data, sizeof(init_data));
+}
+
} // media

Powered by Google App Engine
This is Rietveld 408576698