| 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 "components/webdata/encryptor/encryptor.h" | 5 #include "components/webdata/encryptor/encryptor.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 std::string utf8_plaintext; | 32 std::string utf8_plaintext; |
| 33 std::string utf8_result; | 33 std::string utf8_result; |
| 34 std::string ciphertext; | 34 std::string ciphertext; |
| 35 | 35 |
| 36 // Test borderline cases (empty strings). | 36 // Test borderline cases (empty strings). |
| 37 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); | 37 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); |
| 38 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); | 38 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); |
| 39 EXPECT_EQ(plaintext, result); | 39 EXPECT_EQ(plaintext, result); |
| 40 | 40 |
| 41 // Test a simple string. | 41 // Test a simple string. |
| 42 plaintext = ASCIIToUTF16("hello"); | 42 plaintext = base::ASCIIToUTF16("hello"); |
| 43 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); | 43 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); |
| 44 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); | 44 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); |
| 45 EXPECT_EQ(plaintext, result); | 45 EXPECT_EQ(plaintext, result); |
| 46 | 46 |
| 47 // Test a 16-byte aligned string. This previously hit a boundary error in | 47 // Test a 16-byte aligned string. This previously hit a boundary error in |
| 48 // base::Encryptor::Crypt() on Mac. | 48 // base::Encryptor::Crypt() on Mac. |
| 49 plaintext = ASCIIToUTF16("1234567890123456"); | 49 plaintext = base::ASCIIToUTF16("1234567890123456"); |
| 50 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); | 50 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); |
| 51 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); | 51 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); |
| 52 EXPECT_EQ(plaintext, result); | 52 EXPECT_EQ(plaintext, result); |
| 53 | 53 |
| 54 // Test Unicode. | 54 // Test Unicode. |
| 55 char16 wchars[] = { 0xdbeb, 0xdf1b, 0x4e03, 0x6708, 0x8849, | 55 char16 wchars[] = { 0xdbeb, 0xdf1b, 0x4e03, 0x6708, 0x8849, |
| 56 0x661f, 0x671f, 0x56db, 0x597c, 0x4e03, | 56 0x661f, 0x671f, 0x56db, 0x597c, 0x4e03, |
| 57 0x6708, 0x56db, 0x6708, 0xe407, 0xdbaf, | 57 0x6708, 0x56db, 0x6708, 0xe407, 0xdbaf, |
| 58 0xdeb5, 0x4ec5, 0x544b, 0x661f, 0x671f, | 58 0xdeb5, 0x4ec5, 0x544b, 0x661f, 0x671f, |
| 59 0x65e5, 0x661f, 0x671f, 0x4e94, 0xd8b1, | 59 0x65e5, 0x661f, 0x671f, 0x4e94, 0xd8b1, |
| 60 0xdce1, 0x7052, 0x5095, 0x7c0b, 0xe586, 0}; | 60 0xdce1, 0x7052, 0x5095, 0x7c0b, 0xe586, 0}; |
| 61 plaintext = wchars; | 61 plaintext = wchars; |
| 62 utf8_plaintext = UTF16ToUTF8(plaintext); | 62 utf8_plaintext = base::UTF16ToUTF8(plaintext); |
| 63 EXPECT_EQ(plaintext, UTF8ToUTF16(utf8_plaintext)); | 63 EXPECT_EQ(plaintext, base::UTF8ToUTF16(utf8_plaintext)); |
| 64 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); | 64 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); |
| 65 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); | 65 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); |
| 66 EXPECT_EQ(plaintext, result); | 66 EXPECT_EQ(plaintext, result); |
| 67 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &utf8_result)); | 67 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &utf8_result)); |
| 68 EXPECT_EQ(utf8_plaintext, UTF16ToUTF8(result)); | 68 EXPECT_EQ(utf8_plaintext, base::UTF16ToUTF8(result)); |
| 69 | 69 |
| 70 EXPECT_TRUE(Encryptor::EncryptString(utf8_plaintext, &ciphertext)); | 70 EXPECT_TRUE(Encryptor::EncryptString(utf8_plaintext, &ciphertext)); |
| 71 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); | 71 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); |
| 72 EXPECT_EQ(plaintext, result); | 72 EXPECT_EQ(plaintext, result); |
| 73 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &utf8_result)); | 73 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &utf8_result)); |
| 74 EXPECT_EQ(utf8_plaintext, UTF16ToUTF8(result)); | 74 EXPECT_EQ(utf8_plaintext, base::UTF16ToUTF8(result)); |
| 75 } | 75 } |
| 76 | 76 |
| 77 TEST_F(EncryptorTest, EncryptionDecryption) { | 77 TEST_F(EncryptorTest, EncryptionDecryption) { |
| 78 std::string plaintext; | 78 std::string plaintext; |
| 79 std::string result; | 79 std::string result; |
| 80 std::string ciphertext; | 80 std::string ciphertext; |
| 81 | 81 |
| 82 // Test borderline cases (empty strings). | 82 // Test borderline cases (empty strings). |
| 83 ASSERT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); | 83 ASSERT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); |
| 84 ASSERT_TRUE(Encryptor::DecryptString(ciphertext, &result)); | 84 ASSERT_TRUE(Encryptor::DecryptString(ciphertext, &result)); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 ASSERT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); | 134 ASSERT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); |
| 135 EXPECT_NE(plaintext, ciphertext); | 135 EXPECT_NE(plaintext, ciphertext); |
| 136 ASSERT_LT(4UL, ciphertext.size()); | 136 ASSERT_LT(4UL, ciphertext.size()); |
| 137 ciphertext[3] = ciphertext[3] + 1; | 137 ciphertext[3] = ciphertext[3] + 1; |
| 138 EXPECT_FALSE(Encryptor::DecryptString(ciphertext, &result)); | 138 EXPECT_FALSE(Encryptor::DecryptString(ciphertext, &result)); |
| 139 EXPECT_NE(plaintext, result); | 139 EXPECT_NE(plaintext, result); |
| 140 EXPECT_TRUE(result.empty()); | 140 EXPECT_TRUE(result.empty()); |
| 141 } | 141 } |
| 142 | 142 |
| 143 } // namespace | 143 } // namespace |
| OLD | NEW |