Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(516)

Side by Side Diff: components/os_crypt/os_crypt_util_linux_unittest.cc

Issue 2273723002: Migrate Libsecret for OSCrypt to a new schema (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: feedback Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/os_crypt/key_storage_libsecret.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/macros.h" 7 #include "base/macros.h"
8 #include "components/os_crypt/key_storage_libsecret.h" 8 #include "components/os_crypt/key_storage_libsecret.h"
9 #include "components/os_crypt/libsecret_util_linux.h" 9 #include "components/os_crypt/libsecret_util_linux.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 namespace { 12 namespace {
13 13
14 // Mock functions use MockSecretValue, where SecretValue would appear, and are 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, 15 // cast to the correct signature. We can reduce SecretValue to an std::string,
16 // because we don't use anything else from it. 16 // because we don't use anything else from it.
17 using MockSecretValue = std::string; 17 using MockSecretValue = std::string;
18 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
19 // Replaces some of LibsecretLoader's methods with mocked ones. 34 // Replaces some of LibsecretLoader's methods with mocked ones.
20 class MockLibsecretLoader : public LibsecretLoader { 35 class MockLibsecretLoader : public LibsecretLoader {
21 public: 36 public:
22 // Sets up the minimum mock implementation necessary for OSCrypt to work 37 // Sets up the minimum mock implementation necessary for OSCrypt to work
23 // with Libsecret. Also resets the state to mock a clean database. 38 // with Libsecret. Also resets the state to mock a clean database.
24 static bool ResetForOSCrypt(); 39 static bool ResetForOSCrypt();
25 40
26 // Sets OSCrypt's password in the libsecret mock to a specific value 41 // Sets OSCrypt's password in the libsecret mock to a specific value
27 static void SetOSCryptPassword(const char*); 42 static void SetOSCryptPassword(const char*);
28 43
29 // Releases memory and restores LibsecretLoader to an uninitialized state. 44 // Releases memory and restores LibsecretLoader to an uninitialized state.
30 static void TearDown(); 45 static void TearDown();
31 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
32 private: 51 private:
33 // These methods are used to redirect calls through LibsecretLoader 52 // These methods are used to redirect calls through LibsecretLoader
34 static const gchar* mock_secret_value_get_text(MockSecretValue* value); 53 static const gchar* mock_secret_value_get_text(MockSecretValue* value);
35 54
36 static gboolean mock_secret_password_store_sync(const SecretSchema* schema, 55 static gboolean mock_secret_password_store_sync(const SecretSchema* schema,
37 const gchar* collection, 56 const gchar* collection,
38 const gchar* label, 57 const gchar* label,
39 const gchar* password, 58 const gchar* password,
40 GCancellable* cancellable, 59 GCancellable* cancellable,
41 GError** error, 60 GError** error,
42 ...); 61 ...);
43 62
44 static MockSecretValue* mock_secret_service_lookup_sync( 63 static MockSecretValue* mock_secret_service_lookup_sync(
45 SecretService* service, 64 SecretService* service,
46 const SecretSchema* schema, 65 const SecretSchema* schema,
47 GHashTable* attributes, 66 GHashTable* attributes,
48 GCancellable* cancellable, 67 GCancellable* cancellable,
49 GError** error); 68 GError** error);
50 69
51 static void mock_secret_value_unref(gpointer value); 70 static void mock_secret_value_unref(gpointer value);
52 71
53 static GList* mock_secret_service_search_sync(SecretService* service, 72 static GList* mock_secret_service_search_sync(SecretService* service,
54 const SecretSchema* schema, 73 const SecretSchema* schema,
55 GHashTable* attributes, 74 GHashTable* attributes,
56 SecretSearchFlags flags, 75 SecretSearchFlags flags,
57 GCancellable* cancellable, 76 GCancellable* cancellable,
58 GError** error); 77 GError** error);
59 78
60 // MockLibsecretLoader owns this object. 79 static gboolean mock_secret_password_clear_sync(const SecretSchema* schema,
80 GCancellable* cancellable,
81 GError** error,
82 ...);
83
84 // MockLibsecretLoader owns these objects.
61 static MockSecretValue* stored_password_mock_ptr_; 85 static MockSecretValue* stored_password_mock_ptr_;
86 static MockSecretValue* deprecated_password_mock_ptr_;
62 }; 87 };
63 88
64 MockSecretValue* MockLibsecretLoader::stored_password_mock_ptr_ = nullptr; 89 MockSecretValue* MockLibsecretLoader::stored_password_mock_ptr_ = nullptr;
90 MockSecretValue* MockLibsecretLoader::deprecated_password_mock_ptr_ = nullptr;
65 91
66 const gchar* MockLibsecretLoader::mock_secret_value_get_text( 92 const gchar* MockLibsecretLoader::mock_secret_value_get_text(
67 MockSecretValue* value) { 93 MockSecretValue* value) {
68 return value->c_str(); 94 return value->c_str();
69 } 95 }
70 96
71 // static 97 // static
72 gboolean MockLibsecretLoader::mock_secret_password_store_sync( 98 gboolean MockLibsecretLoader::mock_secret_password_store_sync(
73 const SecretSchema* schema, 99 const SecretSchema* schema,
74 const gchar* collection, 100 const gchar* collection,
75 const gchar* label, 101 const gchar* label,
76 const gchar* password, 102 const gchar* password,
77 GCancellable* cancellable, 103 GCancellable* cancellable,
78 GError** error, 104 GError** error,
79 ...) { 105 ...) {
106 EXPECT_STREQ(kKeystoreSchemaV2.name, schema->name);
80 delete stored_password_mock_ptr_; 107 delete stored_password_mock_ptr_;
81 stored_password_mock_ptr_ = new MockSecretValue(password); 108 stored_password_mock_ptr_ = new MockSecretValue(password);
82 return true; 109 return true;
83 } 110 }
84 111
85 // static 112 // static
86 MockSecretValue* MockLibsecretLoader::mock_secret_service_lookup_sync( 113 MockSecretValue* MockLibsecretLoader::mock_secret_service_lookup_sync(
87 SecretService* service, 114 SecretService* service,
88 const SecretSchema* schema, 115 const SecretSchema* schema,
89 GHashTable* attributes, 116 GHashTable* attributes,
90 GCancellable* cancellable, 117 GCancellable* cancellable,
91 GError** error) { 118 GError** error) {
92 return stored_password_mock_ptr_; 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;
93 } 130 }
94 131
95 // static 132 // static
96 void MockLibsecretLoader::mock_secret_value_unref(gpointer value) {} 133 void MockLibsecretLoader::mock_secret_value_unref(gpointer value) {}
97 134
98 // static 135 // static
99 GList* MockLibsecretLoader::mock_secret_service_search_sync( 136 GList* MockLibsecretLoader::mock_secret_service_search_sync(
100 SecretService* service, 137 SecretService* service,
101 const SecretSchema* schema, 138 const SecretSchema* schema,
102 GHashTable* attributes, 139 GHashTable* attributes,
103 SecretSearchFlags flags, 140 SecretSearchFlags flags,
104 GCancellable* cancellable, 141 GCancellable* cancellable,
105 GError** error) { 142 GError** error) {
106 *error = nullptr; 143 *error = nullptr;
107 return nullptr; 144 return nullptr;
108 } 145 }
109 146
110 // static 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
111 bool MockLibsecretLoader::ResetForOSCrypt() { 160 bool MockLibsecretLoader::ResetForOSCrypt() {
112 // 4 methods used by KeyStorageLibsecret::GetKey() 161 // 4 methods used by KeyStorageLibsecret
113 secret_password_store_sync = 162 secret_password_store_sync =
114 &MockLibsecretLoader::mock_secret_password_store_sync; 163 &MockLibsecretLoader::mock_secret_password_store_sync;
115 secret_value_get_text = (decltype(&::secret_value_get_text)) & 164 secret_value_get_text = (decltype(&::secret_value_get_text)) &
116 MockLibsecretLoader::mock_secret_value_get_text; 165 MockLibsecretLoader::mock_secret_value_get_text;
117 secret_value_unref = &MockLibsecretLoader::mock_secret_value_unref; 166 secret_value_unref = &MockLibsecretLoader::mock_secret_value_unref;
118 secret_service_lookup_sync = 167 secret_service_lookup_sync =
119 (decltype(&::secret_service_lookup_sync)) & 168 (decltype(&::secret_service_lookup_sync)) &
120 MockLibsecretLoader::mock_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;
121 // 1 method used by LibsecretLoader::EnsureLibsecretLoaded() 173 // 1 method used by LibsecretLoader::EnsureLibsecretLoaded()
122 secret_service_search_sync = 174 secret_service_search_sync =
123 &MockLibsecretLoader::mock_secret_service_search_sync; 175 &MockLibsecretLoader::mock_secret_service_search_sync;
124 176
125 delete stored_password_mock_ptr_; 177 delete stored_password_mock_ptr_;
126 stored_password_mock_ptr_ = nullptr; 178 stored_password_mock_ptr_ = nullptr;
127 libsecret_loaded_ = true; 179 libsecret_loaded_ = true;
128 180
129 return true; 181 return true;
130 } 182 }
131 183
132 // static 184 // static
133 void MockLibsecretLoader::SetOSCryptPassword(const char* value) { 185 void MockLibsecretLoader::SetOSCryptPassword(const char* value) {
134 delete stored_password_mock_ptr_; 186 delete stored_password_mock_ptr_;
135 stored_password_mock_ptr_ = new MockSecretValue(value); 187 stored_password_mock_ptr_ = new MockSecretValue(value);
136 } 188 }
137 189
138 // static 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
139 void MockLibsecretLoader::TearDown() { 197 void MockLibsecretLoader::TearDown() {
140 delete stored_password_mock_ptr_; 198 delete stored_password_mock_ptr_;
141 stored_password_mock_ptr_ = nullptr; 199 stored_password_mock_ptr_ = nullptr;
142 libsecret_loaded_ = 200 libsecret_loaded_ =
143 false; // Function pointers will be restored when loading. 201 false; // Function pointers will be restored when loading.
144 } 202 }
145 203
146 class LibsecretTest : public testing::Test { 204 class LibsecretTest : public testing::Test {
147 public: 205 public:
148 LibsecretTest() = default; 206 LibsecretTest() = default;
(...skipping 18 matching lines...) Expand all
167 225
168 TEST_F(LibsecretTest, LibsecretCreatesRandomised) { 226 TEST_F(LibsecretTest, LibsecretCreatesRandomised) {
169 KeyStorageLibsecret libsecret; 227 KeyStorageLibsecret libsecret;
170 MockLibsecretLoader::ResetForOSCrypt(); 228 MockLibsecretLoader::ResetForOSCrypt();
171 std::string password = libsecret.GetKey(); 229 std::string password = libsecret.GetKey();
172 MockLibsecretLoader::ResetForOSCrypt(); 230 MockLibsecretLoader::ResetForOSCrypt();
173 std::string password_new = libsecret.GetKey(); 231 std::string password_new = libsecret.GetKey();
174 EXPECT_NE(password, password_new); 232 EXPECT_NE(password, password_new);
175 } 233 }
176 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
177 } // namespace 243 } // namespace
OLDNEW
« no previous file with comments | « components/os_crypt/key_storage_libsecret.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698