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/os_crypt/os_crypt.h" | 5 #include "components/os_crypt/os_crypt.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "components/os_crypt/key_storage_mock.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 class OSCryptTest : public testing::Test { | 19 class OSCryptTest : public testing::Test { |
19 public: | 20 public: |
20 OSCryptTest() {} | 21 OSCryptTest() {} |
21 | 22 |
23 #if defined(OS_MACOSX) | |
24 void SetUp() override { OSCrypt::UseMockKeychain(true); } | |
Lei Zhang
2016/05/19 22:45:38
I'd highly prefer not to have two different implem
cfroussios
2016/05/20 16:44:52
Done.
| |
25 #endif | |
26 | |
27 #if defined(USE_LIBSECRET) | |
22 void SetUp() override { | 28 void SetUp() override { |
23 #if defined(OS_MACOSX) | 29 key_storage_static_ = &key_storage_; |
24 OSCrypt::UseMockKeychain(true); | 30 UseMockKeyStorageForTesting(true, &GetKeyStorage, &GetPassword); |
31 } | |
32 | |
33 void TearDown() override { | |
34 key_storage_static_ = nullptr; | |
35 UseMockKeyStorageForTesting(false, nullptr, nullptr); | |
36 } | |
37 | |
38 protected: | |
39 KeyStorageMock key_storage_; | |
40 | |
41 private: | |
42 // Needed, so that we can return our |key_storage_| through static methods | |
43 static KeyStorageMock* key_storage_static_; | |
Lei Zhang
2016/05/19 22:45:38
Many places use s_foo_ here, like g_foo for global
cfroussios
2016/05/20 16:44:52
Done.
| |
44 | |
45 static KeyStorageLinux* GetKeyStorage() { return key_storage_static_; } | |
46 | |
47 static std::string* GetPassword() { return key_storage_static_->GetKeyPtr(); } | |
25 #endif | 48 #endif |
26 } | |
27 | 49 |
28 private: | 50 private: |
29 DISALLOW_COPY_AND_ASSIGN(OSCryptTest); | 51 DISALLOW_COPY_AND_ASSIGN(OSCryptTest); |
30 }; | 52 }; |
31 | 53 |
54 #if defined(USE_LIBSECRET) | |
55 KeyStorageMock* OSCryptTest::key_storage_static_ = nullptr; | |
56 #endif | |
57 | |
32 TEST_F(OSCryptTest, String16EncryptionDecryption) { | 58 TEST_F(OSCryptTest, String16EncryptionDecryption) { |
33 base::string16 plaintext; | 59 base::string16 plaintext; |
34 base::string16 result; | 60 base::string16 result; |
35 std::string utf8_plaintext; | 61 std::string utf8_plaintext; |
36 std::string utf8_result; | 62 std::string utf8_result; |
37 std::string ciphertext; | 63 std::string ciphertext; |
38 | 64 |
39 // Test borderline cases (empty strings). | 65 // Test borderline cases (empty strings). |
40 EXPECT_TRUE(OSCrypt::EncryptString16(plaintext, &ciphertext)); | 66 EXPECT_TRUE(OSCrypt::EncryptString16(plaintext, &ciphertext)); |
41 EXPECT_TRUE(OSCrypt::DecryptString16(ciphertext, &result)); | 67 EXPECT_TRUE(OSCrypt::DecryptString16(ciphertext, &result)); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 ASSERT_TRUE(OSCrypt::EncryptString(plaintext, &ciphertext)); | 163 ASSERT_TRUE(OSCrypt::EncryptString(plaintext, &ciphertext)); |
138 EXPECT_NE(plaintext, ciphertext); | 164 EXPECT_NE(plaintext, ciphertext); |
139 ASSERT_LT(4UL, ciphertext.size()); | 165 ASSERT_LT(4UL, ciphertext.size()); |
140 ciphertext[3] = ciphertext[3] + 1; | 166 ciphertext[3] = ciphertext[3] + 1; |
141 EXPECT_FALSE(OSCrypt::DecryptString(ciphertext, &result)); | 167 EXPECT_FALSE(OSCrypt::DecryptString(ciphertext, &result)); |
142 EXPECT_NE(plaintext, result); | 168 EXPECT_NE(plaintext, result); |
143 EXPECT_TRUE(result.empty()); | 169 EXPECT_TRUE(result.empty()); |
144 } | 170 } |
145 | 171 |
146 } // namespace | 172 } // namespace |
OLD | NEW |