| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/encryptor/encryptor.h" | 5 #include "components/encryptor/os_crypt.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" |
| 9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 class EncryptorTest : public testing::Test { | 16 class OSCryptTest : public testing::Test { |
| 16 public: | 17 public: |
| 17 EncryptorTest() {} | 18 OSCryptTest() {} |
| 18 | 19 |
| 19 virtual void SetUp() { | 20 virtual void SetUp() OVERRIDE { |
| 20 #if defined(OS_MACOSX) | 21 #if defined(OS_MACOSX) |
| 21 Encryptor::UseMockKeychain(true); | 22 OSCrypt::UseMockKeychain(true); |
| 22 #endif | 23 #endif |
| 23 } | 24 } |
| 24 | 25 |
| 25 private: | 26 private: |
| 26 DISALLOW_COPY_AND_ASSIGN(EncryptorTest); | 27 DISALLOW_COPY_AND_ASSIGN(OSCryptTest); |
| 27 }; | 28 }; |
| 28 | 29 |
| 29 TEST_F(EncryptorTest, String16EncryptionDecryption) { | 30 TEST_F(OSCryptTest, String16EncryptionDecryption) { |
| 30 base::string16 plaintext; | 31 base::string16 plaintext; |
| 31 base::string16 result; | 32 base::string16 result; |
| 32 std::string utf8_plaintext; | 33 std::string utf8_plaintext; |
| 33 std::string utf8_result; | 34 std::string utf8_result; |
| 34 std::string ciphertext; | 35 std::string ciphertext; |
| 35 | 36 |
| 36 // Test borderline cases (empty strings). | 37 // Test borderline cases (empty strings). |
| 37 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); | 38 EXPECT_TRUE(OSCrypt::EncryptString16(plaintext, &ciphertext)); |
| 38 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); | 39 EXPECT_TRUE(OSCrypt::DecryptString16(ciphertext, &result)); |
| 39 EXPECT_EQ(plaintext, result); | 40 EXPECT_EQ(plaintext, result); |
| 40 | 41 |
| 41 // Test a simple string. | 42 // Test a simple string. |
| 42 plaintext = base::ASCIIToUTF16("hello"); | 43 plaintext = base::ASCIIToUTF16("hello"); |
| 43 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); | 44 EXPECT_TRUE(OSCrypt::EncryptString16(plaintext, &ciphertext)); |
| 44 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); | 45 EXPECT_TRUE(OSCrypt::DecryptString16(ciphertext, &result)); |
| 45 EXPECT_EQ(plaintext, result); | 46 EXPECT_EQ(plaintext, result); |
| 46 | 47 |
| 47 // Test a 16-byte aligned string. This previously hit a boundary error in | 48 // Test a 16-byte aligned string. This previously hit a boundary error in |
| 48 // base::Encryptor::Crypt() on Mac. | 49 // base::OSCrypt::Crypt() on Mac. |
| 49 plaintext = base::ASCIIToUTF16("1234567890123456"); | 50 plaintext = base::ASCIIToUTF16("1234567890123456"); |
| 50 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); | 51 EXPECT_TRUE(OSCrypt::EncryptString16(plaintext, &ciphertext)); |
| 51 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); | 52 EXPECT_TRUE(OSCrypt::DecryptString16(ciphertext, &result)); |
| 52 EXPECT_EQ(plaintext, result); | 53 EXPECT_EQ(plaintext, result); |
| 53 | 54 |
| 54 // Test Unicode. | 55 // Test Unicode. |
| 55 base::char16 wchars[] = { 0xdbeb, 0xdf1b, 0x4e03, 0x6708, 0x8849, | 56 base::char16 wchars[] = { 0xdbeb, 0xdf1b, 0x4e03, 0x6708, 0x8849, |
| 56 0x661f, 0x671f, 0x56db, 0x597c, 0x4e03, | 57 0x661f, 0x671f, 0x56db, 0x597c, 0x4e03, |
| 57 0x6708, 0x56db, 0x6708, 0xe407, 0xdbaf, | 58 0x6708, 0x56db, 0x6708, 0xe407, 0xdbaf, |
| 58 0xdeb5, 0x4ec5, 0x544b, 0x661f, 0x671f, | 59 0xdeb5, 0x4ec5, 0x544b, 0x661f, 0x671f, |
| 59 0x65e5, 0x661f, 0x671f, 0x4e94, 0xd8b1, | 60 0x65e5, 0x661f, 0x671f, 0x4e94, 0xd8b1, |
| 60 0xdce1, 0x7052, 0x5095, 0x7c0b, 0xe586, 0}; | 61 0xdce1, 0x7052, 0x5095, 0x7c0b, 0xe586, 0}; |
| 61 plaintext = wchars; | 62 plaintext = wchars; |
| 62 utf8_plaintext = base::UTF16ToUTF8(plaintext); | 63 utf8_plaintext = base::UTF16ToUTF8(plaintext); |
| 63 EXPECT_EQ(plaintext, base::UTF8ToUTF16(utf8_plaintext)); | 64 EXPECT_EQ(plaintext, base::UTF8ToUTF16(utf8_plaintext)); |
| 64 EXPECT_TRUE(Encryptor::EncryptString16(plaintext, &ciphertext)); | 65 EXPECT_TRUE(OSCrypt::EncryptString16(plaintext, &ciphertext)); |
| 65 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); | 66 EXPECT_TRUE(OSCrypt::DecryptString16(ciphertext, &result)); |
| 66 EXPECT_EQ(plaintext, result); | 67 EXPECT_EQ(plaintext, result); |
| 67 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &utf8_result)); | 68 EXPECT_TRUE(OSCrypt::DecryptString(ciphertext, &utf8_result)); |
| 68 EXPECT_EQ(utf8_plaintext, base::UTF16ToUTF8(result)); | 69 EXPECT_EQ(utf8_plaintext, base::UTF16ToUTF8(result)); |
| 69 | 70 |
| 70 EXPECT_TRUE(Encryptor::EncryptString(utf8_plaintext, &ciphertext)); | 71 EXPECT_TRUE(OSCrypt::EncryptString(utf8_plaintext, &ciphertext)); |
| 71 EXPECT_TRUE(Encryptor::DecryptString16(ciphertext, &result)); | 72 EXPECT_TRUE(OSCrypt::DecryptString16(ciphertext, &result)); |
| 72 EXPECT_EQ(plaintext, result); | 73 EXPECT_EQ(plaintext, result); |
| 73 EXPECT_TRUE(Encryptor::DecryptString(ciphertext, &utf8_result)); | 74 EXPECT_TRUE(OSCrypt::DecryptString(ciphertext, &utf8_result)); |
| 74 EXPECT_EQ(utf8_plaintext, base::UTF16ToUTF8(result)); | 75 EXPECT_EQ(utf8_plaintext, base::UTF16ToUTF8(result)); |
| 75 } | 76 } |
| 76 | 77 |
| 77 TEST_F(EncryptorTest, EncryptionDecryption) { | 78 TEST_F(OSCryptTest, EncryptionDecryption) { |
| 78 std::string plaintext; | 79 std::string plaintext; |
| 79 std::string result; | 80 std::string result; |
| 80 std::string ciphertext; | 81 std::string ciphertext; |
| 81 | 82 |
| 82 // Test borderline cases (empty strings). | 83 // Test borderline cases (empty strings). |
| 83 ASSERT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); | 84 ASSERT_TRUE(OSCrypt::EncryptString(plaintext, &ciphertext)); |
| 84 ASSERT_TRUE(Encryptor::DecryptString(ciphertext, &result)); | 85 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &result)); |
| 85 EXPECT_EQ(plaintext, result); | 86 EXPECT_EQ(plaintext, result); |
| 86 | 87 |
| 87 // Test a simple string. | 88 // Test a simple string. |
| 88 plaintext = "hello"; | 89 plaintext = "hello"; |
| 89 ASSERT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); | 90 ASSERT_TRUE(OSCrypt::EncryptString(plaintext, &ciphertext)); |
| 90 ASSERT_TRUE(Encryptor::DecryptString(ciphertext, &result)); | 91 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &result)); |
| 91 EXPECT_EQ(plaintext, result); | 92 EXPECT_EQ(plaintext, result); |
| 92 | 93 |
| 93 // Make sure it null terminates. | 94 // Make sure it null terminates. |
| 94 plaintext.assign("hello", 3); | 95 plaintext.assign("hello", 3); |
| 95 ASSERT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); | 96 ASSERT_TRUE(OSCrypt::EncryptString(plaintext, &ciphertext)); |
| 96 ASSERT_TRUE(Encryptor::DecryptString(ciphertext, &result)); | 97 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &result)); |
| 97 EXPECT_EQ(plaintext, "hel"); | 98 EXPECT_EQ(plaintext, "hel"); |
| 98 } | 99 } |
| 99 | 100 |
| 100 TEST_F(EncryptorTest, CypherTextDiffers) { | 101 TEST_F(OSCryptTest, CypherTextDiffers) { |
| 101 std::string plaintext; | 102 std::string plaintext; |
| 102 std::string result; | 103 std::string result; |
| 103 std::string ciphertext; | 104 std::string ciphertext; |
| 104 | 105 |
| 105 // Test borderline cases (empty strings). | 106 // Test borderline cases (empty strings). |
| 106 ASSERT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); | 107 ASSERT_TRUE(OSCrypt::EncryptString(plaintext, &ciphertext)); |
| 107 ASSERT_TRUE(Encryptor::DecryptString(ciphertext, &result)); | 108 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &result)); |
| 108 // |cyphertext| is empty on the Mac, different on Windows. | 109 // |cyphertext| is empty on the Mac, different on Windows. |
| 109 EXPECT_TRUE(ciphertext.empty() || plaintext != ciphertext); | 110 EXPECT_TRUE(ciphertext.empty() || plaintext != ciphertext); |
| 110 EXPECT_EQ(plaintext, result); | 111 EXPECT_EQ(plaintext, result); |
| 111 | 112 |
| 112 // Test a simple string. | 113 // Test a simple string. |
| 113 plaintext = "hello"; | 114 plaintext = "hello"; |
| 114 ASSERT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); | 115 ASSERT_TRUE(OSCrypt::EncryptString(plaintext, &ciphertext)); |
| 115 ASSERT_TRUE(Encryptor::DecryptString(ciphertext, &result)); | 116 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &result)); |
| 116 EXPECT_NE(plaintext, ciphertext); | 117 EXPECT_NE(plaintext, ciphertext); |
| 117 EXPECT_EQ(plaintext, result); | 118 EXPECT_EQ(plaintext, result); |
| 118 | 119 |
| 119 // Make sure it null terminates. | 120 // Make sure it null terminates. |
| 120 plaintext.assign("hello", 3); | 121 plaintext.assign("hello", 3); |
| 121 ASSERT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); | 122 ASSERT_TRUE(OSCrypt::EncryptString(plaintext, &ciphertext)); |
| 122 ASSERT_TRUE(Encryptor::DecryptString(ciphertext, &result)); | 123 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &result)); |
| 123 EXPECT_NE(plaintext, ciphertext); | 124 EXPECT_NE(plaintext, ciphertext); |
| 124 EXPECT_EQ(result, "hel"); | 125 EXPECT_EQ(result, "hel"); |
| 125 } | 126 } |
| 126 | 127 |
| 127 TEST_F(EncryptorTest, DecryptError) { | 128 TEST_F(OSCryptTest, DecryptError) { |
| 128 std::string plaintext; | 129 std::string plaintext; |
| 129 std::string result; | 130 std::string result; |
| 130 std::string ciphertext; | 131 std::string ciphertext; |
| 131 | 132 |
| 132 // Test a simple string, messing with ciphertext prior to decrypting. | 133 // Test a simple string, messing with ciphertext prior to decrypting. |
| 133 plaintext = "hello"; | 134 plaintext = "hello"; |
| 134 ASSERT_TRUE(Encryptor::EncryptString(plaintext, &ciphertext)); | 135 ASSERT_TRUE(OSCrypt::EncryptString(plaintext, &ciphertext)); |
| 135 EXPECT_NE(plaintext, ciphertext); | 136 EXPECT_NE(plaintext, ciphertext); |
| 136 ASSERT_LT(4UL, ciphertext.size()); | 137 ASSERT_LT(4UL, ciphertext.size()); |
| 137 ciphertext[3] = ciphertext[3] + 1; | 138 ciphertext[3] = ciphertext[3] + 1; |
| 138 EXPECT_FALSE(Encryptor::DecryptString(ciphertext, &result)); | 139 EXPECT_FALSE(OSCrypt::DecryptString(ciphertext, &result)); |
| 139 EXPECT_NE(plaintext, result); | 140 EXPECT_NE(plaintext, result); |
| 140 EXPECT_TRUE(result.empty()); | 141 EXPECT_TRUE(result.empty()); |
| 141 } | 142 } |
| 142 | 143 |
| 143 } // namespace | 144 } // namespace |
| OLD | NEW |