| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "chrome/browser/password_manager/encryptor.h" | 5 #include "chrome/browser/password_manager/encryptor.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 TEST(EncryptorTest, WideEncryptionDecryption) { | 14 TEST(EncryptorTest, String16EncryptionDecryption) { |
| 15 std::wstring plaintext; | 15 string16 plaintext; |
| 16 std::wstring result; | 16 string16 result; |
| 17 std::string utf8_plaintext; | 17 std::string utf8_plaintext; |
| 18 std::string utf8_result; | 18 std::string utf8_result; |
| 19 std::string ciphertext; | 19 std::string ciphertext; |
| 20 | 20 |
| 21 // test borderline cases (empty strings) | 21 // test borderline cases (empty strings) |
| 22 EXPECT_TRUE(Encryptor::EncryptWideString(plaintext, &ciphertext)); | 22 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); |
| 23 EXPECT_TRUE(Encryptor::DecryptWideString(ciphertext, &result)); | 23 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); |
| 24 EXPECT_EQ(plaintext, result); | 24 EXPECT_EQ(plaintext, result); |
| 25 | 25 |
| 26 // test a simple string | 26 // test a simple string |
| 27 plaintext = L"hello"; | 27 plaintext = ASCIIToUTF16("hello"); |
| 28 EXPECT_TRUE(Encryptor::EncryptWideString(plaintext, &ciphertext)); | 28 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); |
| 29 EXPECT_TRUE(Encryptor::DecryptWideString(ciphertext, &result)); | 29 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); |
| 30 EXPECT_EQ(plaintext, result); | 30 EXPECT_EQ(plaintext, result); |
| 31 | 31 |
| 32 // test unicode | 32 // test unicode |
| 33 std::wstring::value_type wchars[] = { 0xdbeb,0xdf1b,0x4e03,0x6708,0x8849, | 33 char16 wchars[] = { 0xdbeb, 0xdf1b, 0x4e03, 0x6708, 0x8849, |
| 34 0x661f,0x671f,0x56db,0x597c,0x4e03, | 34 0x661f, 0x671f, 0x56db, 0x597c, 0x4e03, |
| 35 0x6708,0x56db,0x6708,0xe407,0xdbaf, | 35 0x6708, 0x56db, 0x6708, 0xe407, 0xdbaf, |
| 36 0xdeb5,0x4ec5,0x544b,0x661f,0x671f, | 36 0xdeb5, 0x4ec5, 0x544b, 0x661f, 0x671f, |
| 37 0x65e5,0x661f,0x671f,0x4e94,0xd8b1, | 37 0x65e5, 0x661f, 0x671f, 0x4e94, 0xd8b1, |
| 38 0xdce1,0x7052,0x5095,0x7c0b,0xe586, 0}; | 38 0xdce1, 0x7052, 0x5095, 0x7c0b, 0xe586, 0}; |
| 39 plaintext = wchars; | 39 plaintext = wchars; |
| 40 utf8_plaintext = WideToUTF8(plaintext); | 40 utf8_plaintext = UTF16ToUTF8(plaintext); |
| 41 EXPECT_EQ(plaintext, UTF8ToWide(utf8_plaintext)); | 41 EXPECT_EQ(plaintext, UTF8ToUTF16(utf8_plaintext)); |
| 42 EXPECT_TRUE(Encryptor::EncryptWideString(plaintext, &ciphertext)); | 42 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); |
| 43 EXPECT_TRUE(Encryptor::DecryptWideString(ciphertext, &result)); | 43 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); |
| 44 EXPECT_EQ(plaintext, result); | 44 EXPECT_EQ(plaintext, result); |
| 45 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &utf8_result)); | 45 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &utf8_result)); |
| 46 EXPECT_EQ(utf8_plaintext, WideToUTF8(result)); | 46 EXPECT_EQ(utf8_plaintext, UTF16ToUTF8(result)); |
| 47 | 47 |
| 48 EXPECT_TRUE(Encryptor::EncryptString(utf8_plaintext, &ciphertext)); | 48 EXPECT_TRUE(Encryptor::EncryptString(utf8_plaintext, &ciphertext)); |
| 49 EXPECT_TRUE(Encryptor::DecryptWideString(ciphertext, &result)); | 49 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); |
| 50 EXPECT_EQ(plaintext, result); | 50 EXPECT_EQ(plaintext, result); |
| 51 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &utf8_result)); | 51 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &utf8_result)); |
| 52 EXPECT_EQ(utf8_plaintext, WideToUTF8(result)); | 52 EXPECT_EQ(utf8_plaintext, UTF16ToUTF8(result)); |
| 53 } | 53 } |
| 54 | 54 |
| 55 TEST(EncryptorTest, EncryptionDecryption) { | 55 TEST(EncryptorTest, EncryptionDecryption) { |
| 56 std::string plaintext; | 56 std::string plaintext; |
| 57 std::string result; | 57 std::string result; |
| 58 std::string ciphertext; | 58 std::string ciphertext; |
| 59 | 59 |
| 60 // test borderline cases (empty strings) | 60 // test borderline cases (empty strings) |
| 61 EXPECT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); | 61 EXPECT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); |
| 62 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &result)); | 62 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &result)); |
| 63 EXPECT_EQ(plaintext, result); | 63 EXPECT_EQ(plaintext, result); |
| 64 | 64 |
| 65 // test a simple string | 65 // test a simple string |
| 66 plaintext = "hello"; | 66 plaintext = "hello"; |
| 67 EXPECT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); | 67 EXPECT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); |
| 68 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &result)); | 68 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &result)); |
| 69 EXPECT_EQ(plaintext, result); | 69 EXPECT_EQ(plaintext, result); |
| 70 | 70 |
| 71 // Make sure it null terminates. | 71 // Make sure it null terminates. |
| 72 plaintext.assign("hello", 3); | 72 plaintext.assign("hello", 3); |
| 73 EXPECT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); | 73 EXPECT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); |
| 74 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &result)); | 74 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &result)); |
| 75 EXPECT_EQ(plaintext, "hel"); | 75 EXPECT_EQ(plaintext, "hel"); |
| 76 } | 76 } |
| 77 | 77 |
| 78 } // namespace | 78 } // namespace |
| 79 | 79 |
| OLD | NEW |