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 <string> | |
6 | |
7 #include "base/macros.h" | |
8 #include "components/os_crypt/key_storage_libsecret.h" | |
9 #include "components/os_crypt/libsecret_util_linux.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace { | |
13 | |
14 // Mock functions use MockSecretValue, where SecretValue would appear, and are | |
15 // cast to the correct signature. We can reduce SecretValue to an std::string, | |
16 // because we don't use anything else from it. | |
17 using MockSecretValue = std::string; | |
18 | |
19 const SecretSchema kKeystoreSchemaV1 = { | |
20 "chrome_libsecret_os_crypt_password", | |
21 SECRET_SCHEMA_NONE, | |
22 { | |
23 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, | |
24 }}; | |
25 | |
26 const SecretSchema kKeystoreSchemaV2 = { | |
27 "chrome_libsecret_os_crypt_password_v2", | |
28 SECRET_SCHEMA_DONT_MATCH_NAME, | |
29 { | |
30 {"application", SECRET_SCHEMA_ATTRIBUTE_STRING}, | |
31 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, | |
32 }}; | |
33 | |
34 // Replaces some of LibsecretLoader's methods with mocked ones. | |
35 class MockLibsecretLoader : public LibsecretLoader { | |
36 public: | |
37 // Sets up the minimum mock implementation necessary for OSCrypt to work | |
38 // with Libsecret. Also resets the state to mock a clean database. | |
39 static bool ResetForOSCrypt(); | |
40 | |
41 // Sets OSCrypt's password in the libsecret mock to a specific value | |
42 static void SetOSCryptPassword(const char*); | |
43 | |
44 // Releases memory and restores LibsecretLoader to an uninitialized state. | |
45 static void TearDown(); | |
46 | |
47 // Set whether there is an old password that needs to be migrated from the | |
48 // deprecated schema. Null means no such password. See crbug.com/639298 | |
49 static void SetDeprecatedOSCryptPassword(const char* value); | |
50 | |
51 private: | |
52 // These methods are used to redirect calls through LibsecretLoader | |
53 static const gchar* mock_secret_value_get_text(MockSecretValue* value); | |
54 | |
55 static gboolean mock_secret_password_store_sync(const SecretSchema* schema, | |
56 const gchar* collection, | |
57 const gchar* label, | |
58 const gchar* password, | |
59 GCancellable* cancellable, | |
60 GError** error, | |
61 ...); | |
62 | |
63 static MockSecretValue* mock_secret_service_lookup_sync( | |
64 SecretService* service, | |
65 const SecretSchema* schema, | |
66 GHashTable* attributes, | |
67 GCancellable* cancellable, | |
68 GError** error); | |
69 | |
70 static void mock_secret_value_unref(gpointer value); | |
71 | |
72 static GList* mock_secret_service_search_sync(SecretService* service, | |
73 const SecretSchema* schema, | |
74 GHashTable* attributes, | |
75 SecretSearchFlags flags, | |
76 GCancellable* cancellable, | |
77 GError** error); | |
78 | |
79 static gboolean mock_secret_password_clear_sync(const SecretSchema* schema, | |
80 GCancellable* cancellable, | |
81 GError** error, | |
82 ...); | |
83 | |
84 // MockLibsecretLoader owns these objects. | |
85 static MockSecretValue* stored_password_mock_ptr_; | |
86 static MockSecretValue* deprecated_password_mock_ptr_; | |
87 }; | |
88 | |
89 MockSecretValue* MockLibsecretLoader::stored_password_mock_ptr_ = nullptr; | |
90 MockSecretValue* MockLibsecretLoader::deprecated_password_mock_ptr_ = nullptr; | |
91 | |
92 const gchar* MockLibsecretLoader::mock_secret_value_get_text( | |
93 MockSecretValue* value) { | |
94 return value->c_str(); | |
95 } | |
96 | |
97 // static | |
98 gboolean MockLibsecretLoader::mock_secret_password_store_sync( | |
99 const SecretSchema* schema, | |
100 const gchar* collection, | |
101 const gchar* label, | |
102 const gchar* password, | |
103 GCancellable* cancellable, | |
104 GError** error, | |
105 ...) { | |
106 EXPECT_STREQ(kKeystoreSchemaV2.name, schema->name); | |
107 delete stored_password_mock_ptr_; | |
108 stored_password_mock_ptr_ = new MockSecretValue(password); | |
109 return true; | |
110 } | |
111 | |
112 // static | |
113 MockSecretValue* MockLibsecretLoader::mock_secret_service_lookup_sync( | |
114 SecretService* service, | |
115 const SecretSchema* schema, | |
116 GHashTable* attributes, | |
117 GCancellable* cancellable, | |
118 GError** error) { | |
119 bool is_known_schema = strcmp(schema->name, kKeystoreSchemaV2.name) == 0 || | |
120 strcmp(schema->name, kKeystoreSchemaV1.name) == 0; | |
121 EXPECT_TRUE(is_known_schema); | |
122 | |
123 if (strcmp(schema->name, kKeystoreSchemaV2.name) == 0) | |
124 return stored_password_mock_ptr_; | |
125 else if (strcmp(schema->name, kKeystoreSchemaV1.name) == 0) | |
126 return deprecated_password_mock_ptr_; | |
127 | |
128 NOTREACHED(); | |
129 return nullptr; | |
130 } | |
131 | |
132 // static | |
133 void MockLibsecretLoader::mock_secret_value_unref(gpointer value) {} | |
134 | |
135 // static | |
136 GList* MockLibsecretLoader::mock_secret_service_search_sync( | |
137 SecretService* service, | |
138 const SecretSchema* schema, | |
139 GHashTable* attributes, | |
140 SecretSearchFlags flags, | |
141 GCancellable* cancellable, | |
142 GError** error) { | |
143 *error = nullptr; | |
144 return nullptr; | |
145 } | |
146 | |
147 // static | |
148 gboolean MockLibsecretLoader::mock_secret_password_clear_sync( | |
149 const SecretSchema* schema, | |
150 GCancellable* cancellable, | |
151 GError** error, | |
152 ...) { | |
153 EXPECT_STREQ(kKeystoreSchemaV1.name, schema->name); | |
154 delete deprecated_password_mock_ptr_; | |
155 deprecated_password_mock_ptr_ = nullptr; | |
156 return true; | |
157 } | |
158 | |
159 // static | |
160 bool MockLibsecretLoader::ResetForOSCrypt() { | |
161 // 4 methods used by KeyStorageLibsecret | |
162 secret_password_store_sync = | |
163 &MockLibsecretLoader::mock_secret_password_store_sync; | |
164 secret_value_get_text = (decltype(&::secret_value_get_text)) & | |
165 MockLibsecretLoader::mock_secret_value_get_text; | |
166 secret_value_unref = &MockLibsecretLoader::mock_secret_value_unref; | |
167 secret_service_lookup_sync = | |
168 (decltype(&::secret_service_lookup_sync)) & | |
169 MockLibsecretLoader::mock_secret_service_lookup_sync; | |
170 // Used by Migrate() | |
171 secret_password_clear_sync = | |
172 &MockLibsecretLoader::mock_secret_password_clear_sync; | |
173 // 1 method used by LibsecretLoader::EnsureLibsecretLoaded() | |
174 secret_service_search_sync = | |
175 &MockLibsecretLoader::mock_secret_service_search_sync; | |
176 | |
177 delete stored_password_mock_ptr_; | |
178 stored_password_mock_ptr_ = nullptr; | |
179 libsecret_loaded_ = true; | |
180 | |
181 return true; | |
182 } | |
183 | |
184 // static | |
185 void MockLibsecretLoader::SetOSCryptPassword(const char* value) { | |
186 delete stored_password_mock_ptr_; | |
187 stored_password_mock_ptr_ = new MockSecretValue(value); | |
188 } | |
189 | |
190 // static | |
191 void MockLibsecretLoader::SetDeprecatedOSCryptPassword(const char* value) { | |
192 delete deprecated_password_mock_ptr_; | |
193 deprecated_password_mock_ptr_ = new MockSecretValue(value); | |
194 } | |
195 | |
196 // static | |
197 void MockLibsecretLoader::TearDown() { | |
198 delete stored_password_mock_ptr_; | |
199 stored_password_mock_ptr_ = nullptr; | |
200 libsecret_loaded_ = | |
201 false; // Function pointers will be restored when loading. | |
202 } | |
203 | |
204 class LibsecretTest : public testing::Test { | |
205 public: | |
206 LibsecretTest() = default; | |
207 ~LibsecretTest() override = default; | |
208 | |
209 void SetUp() override { MockLibsecretLoader::ResetForOSCrypt(); } | |
210 | |
211 void TearDown() override { MockLibsecretLoader::TearDown(); } | |
212 | |
213 private: | |
214 DISALLOW_COPY_AND_ASSIGN(LibsecretTest); | |
215 }; | |
216 | |
217 TEST_F(LibsecretTest, LibsecretRepeats) { | |
218 KeyStorageLibsecret libsecret; | |
219 MockLibsecretLoader::ResetForOSCrypt(); | |
220 std::string password = libsecret.GetKey(); | |
221 EXPECT_FALSE(password.empty()); | |
222 std::string password_repeat = libsecret.GetKey(); | |
223 EXPECT_EQ(password, password_repeat); | |
224 } | |
225 | |
226 TEST_F(LibsecretTest, LibsecretCreatesRandomised) { | |
227 KeyStorageLibsecret libsecret; | |
228 MockLibsecretLoader::ResetForOSCrypt(); | |
229 std::string password = libsecret.GetKey(); | |
230 MockLibsecretLoader::ResetForOSCrypt(); | |
231 std::string password_new = libsecret.GetKey(); | |
232 EXPECT_NE(password, password_new); | |
233 } | |
234 | |
235 TEST_F(LibsecretTest, LibsecretMigratesFromSchemaV1ToV2) { | |
236 KeyStorageLibsecret libsecret; | |
237 MockLibsecretLoader::ResetForOSCrypt(); | |
238 MockLibsecretLoader::SetDeprecatedOSCryptPassword("swallow"); | |
239 std::string password = libsecret.GetKey(); | |
240 EXPECT_EQ("swallow", password); | |
241 } | |
242 | |
243 } // namespace | |
OLD | NEW |