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
3 // found in the LICENSE file.
5 #include "components/webdata/encryptor/encryptor.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 class EncryptorTest
: public testing::Test
{
19 virtual void SetUp() {
20 #if defined(OS_MACOSX)
21 Encryptor::UseMockKeychain(true);
26 DISALLOW_COPY_AND_ASSIGN(EncryptorTest
);
29 TEST_F(EncryptorTest
, String16EncryptionDecryption
) {
30 base::string16 plaintext
;
31 base::string16 result
;
32 std::string utf8_plaintext
;
33 std::string utf8_result
;
34 std::string ciphertext
;
36 // Test borderline cases (empty strings).
37 EXPECT_TRUE(Encryptor::EncryptString16(plaintext
, &ciphertext
));
38 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext
, &result
));
39 EXPECT_EQ(plaintext
, result
);
41 // Test a simple string.
42 plaintext
= base::ASCIIToUTF16("hello");
43 EXPECT_TRUE(Encryptor::EncryptString16(plaintext
, &ciphertext
));
44 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext
, &result
));
45 EXPECT_EQ(plaintext
, result
);
47 // Test a 16-byte aligned string. This previously hit a boundary error in
48 // base::Encryptor::Crypt() on Mac.
49 plaintext
= base::ASCIIToUTF16("1234567890123456");
50 EXPECT_TRUE(Encryptor::EncryptString16(plaintext
, &ciphertext
));
51 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext
, &result
));
52 EXPECT_EQ(plaintext
, result
);
55 base::char16 wchars
[] = { 0xdbeb, 0xdf1b, 0x4e03, 0x6708, 0x8849,
56 0x661f, 0x671f, 0x56db, 0x597c, 0x4e03,
57 0x6708, 0x56db, 0x6708, 0xe407, 0xdbaf,
58 0xdeb5, 0x4ec5, 0x544b, 0x661f, 0x671f,
59 0x65e5, 0x661f, 0x671f, 0x4e94, 0xd8b1,
60 0xdce1, 0x7052, 0x5095, 0x7c0b, 0xe586, 0};
62 utf8_plaintext
= base::UTF16ToUTF8(plaintext
);
63 EXPECT_EQ(plaintext
, base::UTF8ToUTF16(utf8_plaintext
));
64 EXPECT_TRUE(Encryptor::EncryptString16(plaintext
, &ciphertext
));
65 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext
, &result
));
66 EXPECT_EQ(plaintext
, result
);
67 EXPECT_TRUE(Encryptor::DecryptString(ciphertext
, &utf8_result
));
68 EXPECT_EQ(utf8_plaintext
, base::UTF16ToUTF8(result
));
70 EXPECT_TRUE(Encryptor::EncryptString(utf8_plaintext
, &ciphertext
));
71 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext
, &result
));
72 EXPECT_EQ(plaintext
, result
);
73 EXPECT_TRUE(Encryptor::DecryptString(ciphertext
, &utf8_result
));
74 EXPECT_EQ(utf8_plaintext
, base::UTF16ToUTF8(result
));
77 TEST_F(EncryptorTest
, EncryptionDecryption
) {
78 std::string plaintext
;
80 std::string ciphertext
;
82 // Test borderline cases (empty strings).
83 ASSERT_TRUE(Encryptor::EncryptString(plaintext
, &ciphertext
));
84 ASSERT_TRUE(Encryptor::DecryptString(ciphertext
, &result
));
85 EXPECT_EQ(plaintext
, result
);
87 // Test a simple string.
89 ASSERT_TRUE(Encryptor::EncryptString(plaintext
, &ciphertext
));
90 ASSERT_TRUE(Encryptor::DecryptString(ciphertext
, &result
));
91 EXPECT_EQ(plaintext
, result
);
93 // Make sure it null terminates.
94 plaintext
.assign("hello", 3);
95 ASSERT_TRUE(Encryptor::EncryptString(plaintext
, &ciphertext
));
96 ASSERT_TRUE(Encryptor::DecryptString(ciphertext
, &result
));
97 EXPECT_EQ(plaintext
, "hel");
100 TEST_F(EncryptorTest
, CypherTextDiffers
) {
101 std::string plaintext
;
103 std::string ciphertext
;
105 // Test borderline cases (empty strings).
106 ASSERT_TRUE(Encryptor::EncryptString(plaintext
, &ciphertext
));
107 ASSERT_TRUE(Encryptor::DecryptString(ciphertext
, &result
));
108 // |cyphertext| is empty on the Mac, different on Windows.
109 EXPECT_TRUE(ciphertext
.empty() || plaintext
!= ciphertext
);
110 EXPECT_EQ(plaintext
, result
);
112 // Test a simple string.
114 ASSERT_TRUE(Encryptor::EncryptString(plaintext
, &ciphertext
));
115 ASSERT_TRUE(Encryptor::DecryptString(ciphertext
, &result
));
116 EXPECT_NE(plaintext
, ciphertext
);
117 EXPECT_EQ(plaintext
, result
);
119 // Make sure it null terminates.
120 plaintext
.assign("hello", 3);
121 ASSERT_TRUE(Encryptor::EncryptString(plaintext
, &ciphertext
));
122 ASSERT_TRUE(Encryptor::DecryptString(ciphertext
, &result
));
123 EXPECT_NE(plaintext
, ciphertext
);
124 EXPECT_EQ(result
, "hel");
127 TEST_F(EncryptorTest
, DecryptError
) {
128 std::string plaintext
;
130 std::string ciphertext
;
132 // Test a simple string, messing with ciphertext prior to decrypting.
134 ASSERT_TRUE(Encryptor::EncryptString(plaintext
, &ciphertext
));
135 EXPECT_NE(plaintext
, ciphertext
);
136 ASSERT_LT(4UL, ciphertext
.size());
137 ciphertext
[3] = ciphertext
[3] + 1;
138 EXPECT_FALSE(Encryptor::DecryptString(ciphertext
, &result
));
139 EXPECT_NE(plaintext
, result
);
140 EXPECT_TRUE(result
.empty());