diff options
author | xhwang@chromium.org <xhwang@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-23 23:51:43 +0000 |
---|---|---|
committer | xhwang@chromium.org <xhwang@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-23 23:51:43 +0000 |
commit | de0398885ecf52461c9f14c4e43a9851a44006e8 (patch) | |
tree | a68a828fd764e26ef6359de68ab8bf5c45a95720 /crypto/encryptor_unittest.cc | |
parent | 35c83a09965168db7670efb4803554c0bdc8187f (diff) | |
download | chromium_src-de0398885ecf52461c9f14c4e43a9851a44006e8.zip chromium_src-de0398885ecf52461c9f14c4e43a9851a44006e8.tar.gz chromium_src-de0398885ecf52461c9f14c4e43a9851a44006e8.tar.bz2 |
Add a wrong key test into EncryptorTest.
The behavior of crypto::Encryptor::Decrypt() funcion is different on different platforms. Add this test to test this.
BUG=124434
TEST=this is a new test
Review URL: http://codereview.chromium.org/10146012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@133569 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'crypto/encryptor_unittest.cc')
-rw-r--r-- | crypto/encryptor_unittest.cc | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/crypto/encryptor_unittest.cc b/crypto/encryptor_unittest.cc index 7f2caf9..09ec968 100644 --- a/crypto/encryptor_unittest.cc +++ b/crypto/encryptor_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -35,6 +35,50 @@ TEST(EncryptorTest, EncryptDecrypt) { EXPECT_EQ(plaintext, decypted); } +TEST(EncryptorTest, DecryptWrongKey) { + scoped_ptr<crypto::SymmetricKey> key( + crypto::SymmetricKey::DeriveKeyFromPassword( + crypto::SymmetricKey::AES, "password", "saltiest", 1000, 256)); + EXPECT_TRUE(NULL != key.get()); + + scoped_ptr<crypto::SymmetricKey> wrong_key( + crypto::SymmetricKey::DeriveKeyFromPassword( + crypto::SymmetricKey::AES, "wrongword", "sweetest", 1000, 256)); + EXPECT_TRUE(NULL != wrong_key.get()); + + crypto::Encryptor encryptor; + // The IV must be exactly as long as the cipher block size. + std::string iv("the iv: 16 bytes"); + EXPECT_EQ(16U, iv.size()); + EXPECT_TRUE(encryptor.Init(key.get(), crypto::Encryptor::CBC, iv)); + + std::string plaintext("this is the plaintext"); + std::string ciphertext; + EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); + + static const unsigned char expected_ciphertext[] = { + 0x7D, 0x67, 0x5B, 0x53, 0xE6, 0xD8, 0x0F, 0x27, + 0x74, 0xB1, 0x90, 0xFE, 0x6E, 0x58, 0x4A, 0xA0, + 0x0E, 0x35, 0xE3, 0x01, 0xC0, 0xFE, 0x9A, 0xD8, + 0x48, 0x1D, 0x42, 0xB0, 0xBA, 0x21, 0xB2, 0x0C + }; + + ASSERT_EQ(arraysize(expected_ciphertext), ciphertext.size()); + for (size_t i = 0; i < ciphertext.size(); ++i) { + ASSERT_EQ(expected_ciphertext[i], + static_cast<unsigned char>(ciphertext[i])); + } + + crypto::Encryptor decryptor; + EXPECT_TRUE(decryptor.Init(wrong_key.get(), crypto::Encryptor::CBC, iv)); + std::string decypted; + // TODO(wtc): On Linux, Encryptor::Decrypt() doesn't always return false when + // wrong key is provided. See crbug.com/124434. Remove #if when bug is fixed. +#if !defined(USE_NSS) + EXPECT_FALSE(decryptor.Decrypt(ciphertext, &decypted)); +#endif +} + // CTR mode encryption is only implemented using NSS. #if defined(USE_NSS) |