| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/crypto/encryptor.h" | 5 #include "base/crypto/encryptor.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/crypto/symmetric_key.h" | 9 #include "base/crypto/symmetric_key.h" |
| 10 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 #if defined(USE_NSS) || defined(OS_MACOSX) | 14 TEST(EncryptorTest, EncryptDecrypt) { |
| 15 #define MAYBE(name) name | |
| 16 #else | |
| 17 #define MAYBE(name) DISABLED_ ## name | |
| 18 #endif | |
| 19 | |
| 20 TEST(EncryptorTest, MAYBE(EncryptDecrypt)) { | |
| 21 scoped_ptr<base::SymmetricKey> key(base::SymmetricKey::DeriveKeyFromPassword( | 15 scoped_ptr<base::SymmetricKey> key(base::SymmetricKey::DeriveKeyFromPassword( |
| 22 base::SymmetricKey::AES, "password", "saltiest", 1000, 256)); | 16 base::SymmetricKey::AES, "password", "saltiest", 1000, 256)); |
| 23 EXPECT_TRUE(NULL != key.get()); | 17 EXPECT_TRUE(NULL != key.get()); |
| 24 | 18 |
| 25 base::Encryptor encryptor; | 19 base::Encryptor encryptor; |
| 26 // The IV must be exactly as long a the cipher block size. | 20 // The IV must be exactly as long a the cipher block size. |
| 27 std::string iv("the iv: 16 bytes"); | 21 std::string iv("the iv: 16 bytes"); |
| 28 EXPECT_TRUE(encryptor.Init(key.get(), base::Encryptor::CBC, iv)); | 22 EXPECT_TRUE(encryptor.Init(key.get(), base::Encryptor::CBC, iv)); |
| 29 | 23 |
| 30 std::string plaintext("this is the plaintext"); | 24 std::string plaintext("this is the plaintext"); |
| 31 std::string ciphertext; | 25 std::string ciphertext; |
| 32 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); | 26 EXPECT_TRUE(encryptor.Encrypt(plaintext, &ciphertext)); |
| 33 | 27 |
| 34 EXPECT_LT(0U, ciphertext.size()); | 28 EXPECT_LT(0U, ciphertext.size()); |
| 35 | 29 |
| 36 std::string decypted; | 30 std::string decypted; |
| 37 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); | 31 EXPECT_TRUE(encryptor.Decrypt(ciphertext, &decypted)); |
| 38 | 32 |
| 39 EXPECT_EQ(plaintext, decypted); | 33 EXPECT_EQ(plaintext, decypted); |
| 40 } | 34 } |
| OLD | NEW |