Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(184)

Side by Side Diff: components/encryptor/encryptor_unittest.cc

Issue 183953005: Rename components's Encryptor to OSEncrypt. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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.
4
5 #include "components/encryptor/encryptor.h"
6
7 #include <string>
8
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace {
14
15 class EncryptorTest : public testing::Test {
16 public:
17 EncryptorTest() {}
18
19 virtual void SetUp() {
20 #if defined(OS_MACOSX)
21 Encryptor::UseMockKeychain(true);
22 #endif
23 }
24
25 private:
26 DISALLOW_COPY_AND_ASSIGN(EncryptorTest);
27 };
28
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;
35
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);
40
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);
46
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);
53
54 // Test Unicode.
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};
61 plaintext = wchars;
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));
69
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));
75 }
76
77 TEST_F(EncryptorTest, EncryptionDecryption) {
78 std::string plaintext;
79 std::string result;
80 std::string ciphertext;
81
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);
86
87 // Test a simple string.
88 plaintext = "hello";
89 ASSERT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext));
90 ASSERT_TRUE(Encryptor::DecryptString(ciphertext, &result));
91 EXPECT_EQ(plaintext, result);
92
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");
98 }
99
100 TEST_F(EncryptorTest, CypherTextDiffers) {
101 std::string plaintext;
102 std::string result;
103 std::string ciphertext;
104
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);
111
112 // Test a simple string.
113 plaintext = "hello";
114 ASSERT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext));
115 ASSERT_TRUE(Encryptor::DecryptString(ciphertext, &result));
116 EXPECT_NE(plaintext, ciphertext);
117 EXPECT_EQ(plaintext, result);
118
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");
125 }
126
127 TEST_F(EncryptorTest, DecryptError) {
128 std::string plaintext;
129 std::string result;
130 std::string ciphertext;
131
132 // Test a simple string, messing with ciphertext prior to decrypting.
133 plaintext = "hello";
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());
141 }
142
143 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698