OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "components/os_crypt/key_storage_libsecret.h" | 9 #include "components/os_crypt/key_storage_libsecret.h" |
10 #include "components/os_crypt/libsecret_util_linux.h" | 10 #include "components/os_crypt/libsecret_util_linux.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 // Mock functions use MockSecretValue, where SecretValue would appear, and are | 15 // Mock functions use MockSecretValue, where SecretValue would appear, and are |
16 // cast to the correct signature. We can reduce SecretValue to an std::string, | 16 // cast to the correct signature. We can reduce SecretValue to an std::string, |
17 // because we don't use anything else from it. | 17 // because we don't use anything else from it. |
18 using MockSecretValue = std::string; | 18 using MockSecretValue = std::string; |
| 19 // Likewise, we only need a SecretValue from SecretItem. |
| 20 using MockSecretItem = MockSecretValue; |
19 | 21 |
20 const SecretSchema kKeystoreSchemaV1 = { | 22 const SecretSchema kKeystoreSchemaV1 = { |
21 "chrome_libsecret_os_crypt_password", | 23 "chrome_libsecret_os_crypt_password", |
22 SECRET_SCHEMA_NONE, | 24 SECRET_SCHEMA_NONE, |
23 { | 25 { |
24 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, | 26 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, |
25 }}; | 27 }}; |
26 | 28 |
27 const SecretSchema kKeystoreSchemaV2 = { | 29 const SecretSchema kKeystoreSchemaV2 = { |
28 "chrome_libsecret_os_crypt_password_v2", | 30 "chrome_libsecret_os_crypt_password_v2", |
(...skipping 25 matching lines...) Expand all Loading... |
54 static const gchar* mock_secret_value_get_text(MockSecretValue* value); | 56 static const gchar* mock_secret_value_get_text(MockSecretValue* value); |
55 | 57 |
56 static gboolean mock_secret_password_store_sync(const SecretSchema* schema, | 58 static gboolean mock_secret_password_store_sync(const SecretSchema* schema, |
57 const gchar* collection, | 59 const gchar* collection, |
58 const gchar* label, | 60 const gchar* label, |
59 const gchar* password, | 61 const gchar* password, |
60 GCancellable* cancellable, | 62 GCancellable* cancellable, |
61 GError** error, | 63 GError** error, |
62 ...); | 64 ...); |
63 | 65 |
64 static MockSecretValue* mock_secret_service_lookup_sync( | |
65 SecretService* service, | |
66 const SecretSchema* schema, | |
67 GHashTable* attributes, | |
68 GCancellable* cancellable, | |
69 GError** error); | |
70 | |
71 static void mock_secret_value_unref(gpointer value); | 66 static void mock_secret_value_unref(gpointer value); |
72 | 67 |
73 static GList* mock_secret_service_search_sync(SecretService* service, | 68 static GList* mock_secret_service_search_sync(SecretService* service, |
74 const SecretSchema* schema, | 69 const SecretSchema* schema, |
75 GHashTable* attributes, | 70 GHashTable* attributes, |
76 SecretSearchFlags flags, | 71 SecretSearchFlags flags, |
77 GCancellable* cancellable, | 72 GCancellable* cancellable, |
78 GError** error); | 73 GError** error); |
79 | 74 |
80 static gboolean mock_secret_password_clear_sync(const SecretSchema* schema, | 75 static gboolean mock_secret_password_clear_sync(const SecretSchema* schema, |
81 GCancellable* cancellable, | 76 GCancellable* cancellable, |
82 GError** error, | 77 GError** error, |
83 ...); | 78 ...); |
84 | 79 |
| 80 static MockSecretValue* mock_secret_item_get_secret(MockSecretItem* item); |
| 81 |
85 // MockLibsecretLoader owns these objects. | 82 // MockLibsecretLoader owns these objects. |
86 static MockSecretValue* stored_password_mock_ptr_; | 83 static MockSecretValue* stored_password_mock_ptr_; |
87 static MockSecretValue* deprecated_password_mock_ptr_; | 84 static MockSecretValue* deprecated_password_mock_ptr_; |
88 }; | 85 }; |
89 | 86 |
90 MockSecretValue* MockLibsecretLoader::stored_password_mock_ptr_ = nullptr; | 87 MockSecretValue* MockLibsecretLoader::stored_password_mock_ptr_ = nullptr; |
91 MockSecretValue* MockLibsecretLoader::deprecated_password_mock_ptr_ = nullptr; | 88 MockSecretValue* MockLibsecretLoader::deprecated_password_mock_ptr_ = nullptr; |
92 | 89 |
93 const gchar* MockLibsecretLoader::mock_secret_value_get_text( | 90 const gchar* MockLibsecretLoader::mock_secret_value_get_text( |
94 MockSecretValue* value) { | 91 MockSecretValue* value) { |
95 return value->c_str(); | 92 return value->c_str(); |
96 } | 93 } |
97 | 94 |
98 // static | 95 // static |
99 gboolean MockLibsecretLoader::mock_secret_password_store_sync( | 96 gboolean MockLibsecretLoader::mock_secret_password_store_sync( |
100 const SecretSchema* schema, | 97 const SecretSchema* schema, |
101 const gchar* collection, | 98 const gchar* collection, |
102 const gchar* label, | 99 const gchar* label, |
103 const gchar* password, | 100 const gchar* password, |
104 GCancellable* cancellable, | 101 GCancellable* cancellable, |
105 GError** error, | 102 GError** error, |
106 ...) { | 103 ...) { |
107 EXPECT_STREQ(kKeystoreSchemaV2.name, schema->name); | 104 EXPECT_STREQ(kKeystoreSchemaV2.name, schema->name); |
108 delete stored_password_mock_ptr_; | 105 delete stored_password_mock_ptr_; |
109 stored_password_mock_ptr_ = new MockSecretValue(password); | 106 stored_password_mock_ptr_ = new MockSecretValue(password); |
110 return true; | 107 return true; |
111 } | 108 } |
112 | 109 |
113 // static | 110 // static |
114 MockSecretValue* MockLibsecretLoader::mock_secret_service_lookup_sync( | |
115 SecretService* service, | |
116 const SecretSchema* schema, | |
117 GHashTable* attributes, | |
118 GCancellable* cancellable, | |
119 GError** error) { | |
120 bool is_known_schema = strcmp(schema->name, kKeystoreSchemaV2.name) == 0 || | |
121 strcmp(schema->name, kKeystoreSchemaV1.name) == 0; | |
122 EXPECT_TRUE(is_known_schema); | |
123 | |
124 if (strcmp(schema->name, kKeystoreSchemaV2.name) == 0) | |
125 return stored_password_mock_ptr_; | |
126 else if (strcmp(schema->name, kKeystoreSchemaV1.name) == 0) | |
127 return deprecated_password_mock_ptr_; | |
128 | |
129 NOTREACHED(); | |
130 return nullptr; | |
131 } | |
132 | |
133 // static | |
134 void MockLibsecretLoader::mock_secret_value_unref(gpointer value) {} | 111 void MockLibsecretLoader::mock_secret_value_unref(gpointer value) {} |
135 | 112 |
136 // static | 113 // static |
137 GList* MockLibsecretLoader::mock_secret_service_search_sync( | 114 GList* MockLibsecretLoader::mock_secret_service_search_sync( |
138 SecretService* service, | 115 SecretService* service, |
139 const SecretSchema* schema, | 116 const SecretSchema* schema, |
140 GHashTable* attributes, | 117 GHashTable* attributes, |
141 SecretSearchFlags flags, | 118 SecretSearchFlags flags, |
142 GCancellable* cancellable, | 119 GCancellable* cancellable, |
143 GError** error) { | 120 GError** error) { |
144 *error = nullptr; | 121 bool is_known_schema = strcmp(schema->name, kKeystoreSchemaV2.name) == 0 || |
145 return nullptr; | 122 strcmp(schema->name, kKeystoreSchemaV1.name) == 0; |
| 123 EXPECT_TRUE(is_known_schema); |
| 124 |
| 125 EXPECT_TRUE(flags & SECRET_SEARCH_UNLOCK); |
| 126 EXPECT_TRUE(flags & SECRET_SEARCH_LOAD_SECRETS); |
| 127 |
| 128 MockSecretItem* item = nullptr; |
| 129 if (strcmp(schema->name, kKeystoreSchemaV2.name) == 0) |
| 130 item = stored_password_mock_ptr_; |
| 131 else if (strcmp(schema->name, kKeystoreSchemaV1.name) == 0) |
| 132 item = deprecated_password_mock_ptr_; |
| 133 |
| 134 GList* result = nullptr; |
| 135 result = g_list_append(result, item); |
| 136 g_clear_error(error); |
| 137 return result; |
146 } | 138 } |
147 | 139 |
148 // static | 140 // static |
149 gboolean MockLibsecretLoader::mock_secret_password_clear_sync( | 141 gboolean MockLibsecretLoader::mock_secret_password_clear_sync( |
150 const SecretSchema* schema, | 142 const SecretSchema* schema, |
151 GCancellable* cancellable, | 143 GCancellable* cancellable, |
152 GError** error, | 144 GError** error, |
153 ...) { | 145 ...) { |
| 146 // We would only delete entries in the deprecated schema. |
154 EXPECT_STREQ(kKeystoreSchemaV1.name, schema->name); | 147 EXPECT_STREQ(kKeystoreSchemaV1.name, schema->name); |
155 delete deprecated_password_mock_ptr_; | 148 delete deprecated_password_mock_ptr_; |
156 deprecated_password_mock_ptr_ = nullptr; | 149 deprecated_password_mock_ptr_ = nullptr; |
157 return true; | 150 return true; |
158 } | 151 } |
159 | 152 |
160 // static | 153 // static |
| 154 MockSecretValue* MockLibsecretLoader::mock_secret_item_get_secret( |
| 155 MockSecretItem* item) { |
| 156 return item; |
| 157 } |
| 158 |
| 159 // static |
161 bool MockLibsecretLoader::ResetForOSCrypt() { | 160 bool MockLibsecretLoader::ResetForOSCrypt() { |
162 // 4 methods used by KeyStorageLibsecret | 161 // Methods used by KeyStorageLibsecret |
163 secret_password_store_sync = | 162 secret_password_store_sync = |
164 &MockLibsecretLoader::mock_secret_password_store_sync; | 163 &MockLibsecretLoader::mock_secret_password_store_sync; |
165 secret_value_get_text = (decltype(&::secret_value_get_text)) & | 164 secret_value_get_text = (decltype(&::secret_value_get_text)) & |
166 MockLibsecretLoader::mock_secret_value_get_text; | 165 MockLibsecretLoader::mock_secret_value_get_text; |
167 secret_value_unref = &MockLibsecretLoader::mock_secret_value_unref; | 166 secret_value_unref = &MockLibsecretLoader::mock_secret_value_unref; |
168 secret_service_lookup_sync = | 167 secret_service_search_sync = |
169 (decltype(&::secret_service_lookup_sync)) & | 168 &MockLibsecretLoader::mock_secret_service_search_sync; |
170 MockLibsecretLoader::mock_secret_service_lookup_sync; | 169 secret_item_get_secret = |
| 170 (decltype(&::secret_item_get_secret))mock_secret_item_get_secret; |
171 // Used by Migrate() | 171 // Used by Migrate() |
172 secret_password_clear_sync = | 172 secret_password_clear_sync = |
173 &MockLibsecretLoader::mock_secret_password_clear_sync; | 173 &MockLibsecretLoader::mock_secret_password_clear_sync; |
174 // 1 method used by LibsecretLoader::EnsureLibsecretLoaded() | |
175 secret_service_search_sync = | |
176 &MockLibsecretLoader::mock_secret_service_search_sync; | |
177 | 174 |
178 delete stored_password_mock_ptr_; | 175 delete stored_password_mock_ptr_; |
179 stored_password_mock_ptr_ = nullptr; | 176 stored_password_mock_ptr_ = nullptr; |
180 libsecret_loaded_ = true; | 177 libsecret_loaded_ = true; |
181 | 178 |
182 return true; | 179 return true; |
183 } | 180 } |
184 | 181 |
185 // static | 182 // static |
186 void MockLibsecretLoader::SetOSCryptPassword(const char* value) { | 183 void MockLibsecretLoader::SetOSCryptPassword(const char* value) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
235 | 232 |
236 TEST_F(LibsecretTest, LibsecretMigratesFromSchemaV1ToV2) { | 233 TEST_F(LibsecretTest, LibsecretMigratesFromSchemaV1ToV2) { |
237 KeyStorageLibsecret libsecret; | 234 KeyStorageLibsecret libsecret; |
238 MockLibsecretLoader::ResetForOSCrypt(); | 235 MockLibsecretLoader::ResetForOSCrypt(); |
239 MockLibsecretLoader::SetDeprecatedOSCryptPassword("swallow"); | 236 MockLibsecretLoader::SetDeprecatedOSCryptPassword("swallow"); |
240 std::string password = libsecret.GetKey(); | 237 std::string password = libsecret.GetKey(); |
241 EXPECT_EQ("swallow", password); | 238 EXPECT_EQ("swallow", password); |
242 } | 239 } |
243 | 240 |
244 } // namespace | 241 } // namespace |
OLD | NEW |