OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 <memory> | |
6 #include <string> | |
7 #include <utility> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/macros.h" | |
11 #include "components/os_crypt/key_storage_libsecret.h" | |
12 #include "components/os_crypt/key_storage_linux.h" | |
13 #include "components/os_crypt/key_storage_mock.h" | |
14 #include "components/os_crypt/libsecret_util_linux.h" | |
15 #include "components/os_crypt/os_crypt.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 namespace { | |
19 | |
20 typedef std::string MockSecretValue; | |
Lei Zhang
2016/05/19 22:45:38
using foo = bar, instead of typedef bar foo.
cfroussios
2016/05/20 16:44:52
Done.
| |
21 | |
22 // Replaces some of LibsecretLoader's methods with mocked ones. | |
23 class MockLibsecretLoader : public LibsecretLoader { | |
24 public: | |
25 // Sets up the minimum mock implementation necessary for |Libsecret| to work. | |
26 // Also resets the state to mock a clean database. | |
27 static bool ResetForOSCrypt(); | |
28 | |
29 // Set OSCrypt's password in the libsecret mock to a specific value | |
Lei Zhang
2016/05/19 22:45:38
Sets
cfroussios
2016/05/20 16:44:52
Done.
| |
30 static void SetOSCryptPassword(const char*); | |
31 | |
32 // Releases memory and restores LibsecretLoader to an uninitialized state. | |
33 static void TearDown(); | |
34 | |
35 private: | |
36 static std::string* stored_password_mock_ptr_; | |
37 | |
38 // These methods are used to redirect calls through LibsecretLoader | |
39 static const gchar* mock_secret_value_get_text(MockSecretValue* value); | |
40 | |
41 static gboolean mock_secret_password_store_sync(const SecretSchema* schema, | |
42 const gchar* collection, | |
43 const gchar* label, | |
44 const gchar* password, | |
45 GCancellable* cancellable, | |
46 GError** error, | |
47 ...); | |
48 | |
49 static MockSecretValue* mock_secret_service_lookup_sync( | |
50 SecretService* service, | |
51 const SecretSchema* schema, | |
52 GHashTable* attributes, | |
53 GCancellable* cancellable, | |
54 GError** error); | |
55 | |
56 static void mock_secret_value_unref(gpointer value); | |
57 | |
58 static GList* mock_secret_service_search_sync(SecretService* service, | |
59 const SecretSchema* schema, | |
60 GHashTable* attributes, | |
61 SecretSearchFlags flags, | |
62 GCancellable* cancellable, | |
63 GError** error); | |
64 }; | |
65 | |
66 MockSecretValue* MockLibsecretLoader::stored_password_mock_ptr_ = nullptr; | |
67 | |
68 const gchar* MockLibsecretLoader::mock_secret_value_get_text( | |
69 MockSecretValue* value) { | |
70 return value->c_str(); | |
71 } | |
72 | |
73 gboolean MockLibsecretLoader::mock_secret_password_store_sync( | |
74 const SecretSchema* schema, | |
75 const gchar* collection, | |
76 const gchar* label, | |
77 const gchar* password, | |
78 GCancellable* cancellable, | |
79 GError** error, | |
80 ...) { | |
81 delete stored_password_mock_ptr_; | |
82 stored_password_mock_ptr_ = new MockSecretValue(password); | |
83 return true; | |
84 } | |
85 | |
86 MockSecretValue* MockLibsecretLoader::mock_secret_service_lookup_sync( | |
87 SecretService* service, | |
88 const SecretSchema* schema, | |
89 GHashTable* attributes, | |
90 GCancellable* cancellable, | |
91 GError** error) { | |
92 return stored_password_mock_ptr_; | |
93 } | |
94 | |
95 void MockLibsecretLoader::mock_secret_value_unref(gpointer value) {} | |
96 | |
97 GList* MockLibsecretLoader::mock_secret_service_search_sync( | |
98 SecretService* service, | |
99 const SecretSchema* schema, | |
100 GHashTable* attributes, | |
101 SecretSearchFlags flags, | |
102 GCancellable* cancellable, | |
103 GError** error) { | |
104 *error = nullptr; | |
105 return nullptr; | |
106 } | |
107 | |
108 // static | |
109 bool MockLibsecretLoader::ResetForOSCrypt() { | |
110 // 4 methods used by Libsecret.GetPassword(); | |
111 secret_password_store_sync = | |
112 &MockLibsecretLoader::mock_secret_password_store_sync; | |
113 secret_value_get_text = (decltype(&::secret_value_get_text)) & | |
114 MockLibsecretLoader::mock_secret_value_get_text; | |
115 secret_value_unref = &MockLibsecretLoader::mock_secret_value_unref; | |
116 secret_service_lookup_sync = | |
117 (decltype(&::secret_service_lookup_sync)) & | |
118 MockLibsecretLoader::mock_secret_service_lookup_sync; | |
119 // 1 method used by LibsecretLoader::EnsureLibsecretLoaded | |
120 secret_service_search_sync = | |
121 &MockLibsecretLoader::mock_secret_service_search_sync; | |
122 | |
123 delete stored_password_mock_ptr_; | |
124 stored_password_mock_ptr_ = nullptr; | |
125 libsecret_loaded_ = true; | |
126 | |
127 return true; | |
128 } | |
129 | |
130 void MockLibsecretLoader::SetOSCryptPassword(const char* value) { | |
131 delete stored_password_mock_ptr_; | |
132 stored_password_mock_ptr_ = new MockSecretValue(value); | |
133 } | |
134 | |
135 void MockLibsecretLoader::TearDown() { | |
136 delete stored_password_mock_ptr_; | |
137 stored_password_mock_ptr_ = nullptr; | |
138 libsecret_loaded_ = false; // function pointers will be restored when loading | |
139 } | |
140 | |
141 class OSCryptLinuxTest : public testing::Test { | |
142 public: | |
143 OSCryptLinuxTest() = default; | |
144 ~OSCryptLinuxTest() override = default; | |
145 | |
146 void SetUp() override { | |
147 key_storage_static_ = &key_storage_; | |
148 UseMockKeyStorageForTesting(true, &GetKeyStorage, &GetPassword); | |
149 } | |
150 | |
151 void TearDown() override { | |
152 key_storage_static_ = nullptr; | |
153 UseMockKeyStorageForTesting(false, nullptr, nullptr); | |
154 } | |
155 | |
156 protected: | |
157 KeyStorageMock key_storage_; | |
158 | |
159 private: | |
160 // Needed, so that we can return our |key_storage_| through static methods | |
161 static KeyStorageMock* key_storage_static_; | |
162 | |
163 static KeyStorageLinux* GetKeyStorage() { return key_storage_static_; } | |
164 | |
165 static std::string* GetPassword() { return key_storage_static_->GetKeyPtr(); } | |
166 | |
167 DISALLOW_COPY_AND_ASSIGN(OSCryptLinuxTest); | |
168 }; | |
169 | |
170 KeyStorageMock* OSCryptLinuxTest::key_storage_static_ = nullptr; | |
171 | |
172 TEST_F(OSCryptLinuxTest, VerifyV0) { | |
173 const std::string originaltext = "hello"; | |
174 std::string ciphertext; | |
175 std::string decipheredtext; | |
176 | |
177 key_storage_.ResetTo(""); | |
178 ciphertext = originaltext; // No encryption | |
179 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext)); | |
180 ASSERT_EQ(originaltext, decipheredtext); | |
181 } | |
182 | |
183 TEST_F(OSCryptLinuxTest, VerifyV10) { | |
184 const std::string originaltext = "hello"; | |
185 std::string ciphertext; | |
186 std::string decipheredtext; | |
187 | |
188 key_storage_.ResetTo("peanuts"); | |
189 ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext)); | |
190 key_storage_.ResetTo("not_peanuts"); | |
191 ciphertext = ciphertext.substr(3).insert(0, "v10"); | |
192 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext)); | |
193 ASSERT_EQ(originaltext, decipheredtext); | |
194 } | |
195 | |
196 TEST_F(OSCryptLinuxTest, VerifyV11) { | |
197 const std::string originaltext = "hello"; | |
198 std::string ciphertext; | |
199 std::string decipheredtext; | |
200 | |
201 key_storage_.ResetTo(""); | |
202 ASSERT_TRUE(OSCrypt::EncryptString(originaltext, &ciphertext)); | |
203 ASSERT_EQ(ciphertext.substr(0, 3), "v11"); | |
204 ASSERT_TRUE(OSCrypt::DecryptString(ciphertext, &decipheredtext)); | |
205 ASSERT_EQ(originaltext, decipheredtext); | |
206 } | |
207 | |
208 class LibsecretTest : public testing::Test { | |
209 public: | |
210 LibsecretTest() = default; | |
211 ~LibsecretTest() override = default; | |
212 | |
213 void SetUp() override { MockLibsecretLoader::ResetForOSCrypt(); } | |
214 | |
215 void TearDown() override { MockLibsecretLoader::TearDown(); } | |
216 | |
217 private: | |
218 DISALLOW_COPY_AND_ASSIGN(LibsecretTest); | |
219 }; | |
220 | |
221 TEST_F(LibsecretTest, LibsecretRepeats) { | |
222 KeyStorageLibsecret libsecret; | |
223 MockLibsecretLoader::ResetForOSCrypt(); | |
224 std::string password = libsecret.GetKey(); | |
225 EXPECT_FALSE(password.empty()); | |
226 std::string password_repeat = libsecret.GetKey(); | |
227 EXPECT_EQ(password, password_repeat); | |
228 } | |
229 | |
230 TEST_F(LibsecretTest, LibsecretCreatesRandomised) { | |
231 KeyStorageLibsecret libsecret; | |
232 MockLibsecretLoader::ResetForOSCrypt(); | |
233 std::string password = libsecret.GetKey(); | |
234 MockLibsecretLoader::ResetForOSCrypt(); | |
235 std::string password_new = libsecret.GetKey(); | |
236 EXPECT_NE(password, password_new); | |
237 } | |
238 | |
239 } // namespace | |
OLD | NEW |