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