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

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

Issue 2444313002: Remove function casting from Libsecret tests (Closed)
Patch Set: typo Created 4 years, 1 month 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
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/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 expect MockSecretValue, where SecretValue appears, and cast it
16 // cast to the correct signature. We can reduce SecretValue to an std::string, 16 // to the mock type. We can reduce SecretValue to an std::string, because we
17 // because we don't use anything else from it. 17 // 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. 19 // Likewise, we only need a SecretValue from SecretItem.
20 using MockSecretItem = MockSecretValue; 20 using MockSecretItem = MockSecretValue;
21 21
22 const SecretSchema kKeystoreSchemaV1 = { 22 const SecretSchema kKeystoreSchemaV1 = {
23 "chrome_libsecret_os_crypt_password", 23 "chrome_libsecret_os_crypt_password",
24 SECRET_SCHEMA_NONE, 24 SECRET_SCHEMA_NONE,
25 { 25 {
26 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING}, 26 {nullptr, SECRET_SCHEMA_ATTRIBUTE_STRING},
27 }}; 27 }};
(...skipping 18 matching lines...) Expand all
46 46
47 // Releases memory and restores LibsecretLoader to an uninitialized state. 47 // Releases memory and restores LibsecretLoader to an uninitialized state.
48 static void TearDown(); 48 static void TearDown();
49 49
50 // Set whether there is an old password that needs to be migrated from the 50 // Set whether there is an old password that needs to be migrated from the
51 // deprecated schema. Null means no such password. See crbug.com/639298 51 // deprecated schema. Null means no such password. See crbug.com/639298
52 static void SetDeprecatedOSCryptPassword(const char* value); 52 static void SetDeprecatedOSCryptPassword(const char* value);
53 53
54 private: 54 private:
55 // These methods are used to redirect calls through LibsecretLoader 55 // These methods are used to redirect calls through LibsecretLoader
56 static const gchar* mock_secret_value_get_text(MockSecretValue* value); 56 static const gchar* mock_secret_value_get_text(SecretValue* value);
57 57
58 static gboolean mock_secret_password_store_sync(const SecretSchema* schema, 58 static gboolean mock_secret_password_store_sync(const SecretSchema* schema,
59 const gchar* collection, 59 const gchar* collection,
60 const gchar* label, 60 const gchar* label,
61 const gchar* password, 61 const gchar* password,
62 GCancellable* cancellable, 62 GCancellable* cancellable,
63 GError** error, 63 GError** error,
64 ...); 64 ...);
65 65
66 static void mock_secret_value_unref(gpointer value); 66 static void mock_secret_value_unref(gpointer value);
67 67
68 static GList* mock_secret_service_search_sync(SecretService* service, 68 static GList* mock_secret_service_search_sync(SecretService* service,
69 const SecretSchema* schema, 69 const SecretSchema* schema,
70 GHashTable* attributes, 70 GHashTable* attributes,
71 SecretSearchFlags flags, 71 SecretSearchFlags flags,
72 GCancellable* cancellable, 72 GCancellable* cancellable,
73 GError** error); 73 GError** error);
74 74
75 static gboolean mock_secret_password_clear_sync(const SecretSchema* schema, 75 static gboolean mock_secret_password_clear_sync(const SecretSchema* schema,
76 GCancellable* cancellable, 76 GCancellable* cancellable,
77 GError** error, 77 GError** error,
78 ...); 78 ...);
79 79
80 static MockSecretValue* mock_secret_item_get_secret(MockSecretItem* item); 80 static SecretValue* mock_secret_item_get_secret(SecretItem* item);
81 81
82 // MockLibsecretLoader owns these objects. 82 // MockLibsecretLoader owns these objects.
83 static MockSecretValue* stored_password_mock_ptr_; 83 static MockSecretValue* stored_password_mock_ptr_;
84 static MockSecretValue* deprecated_password_mock_ptr_; 84 static MockSecretValue* deprecated_password_mock_ptr_;
85 }; 85 };
86 86
87 MockSecretValue* MockLibsecretLoader::stored_password_mock_ptr_ = nullptr; 87 MockSecretValue* MockLibsecretLoader::stored_password_mock_ptr_ = nullptr;
88 MockSecretValue* MockLibsecretLoader::deprecated_password_mock_ptr_ = nullptr; 88 MockSecretValue* MockLibsecretLoader::deprecated_password_mock_ptr_ = nullptr;
89 89
90 const gchar* MockLibsecretLoader::mock_secret_value_get_text( 90 const gchar* MockLibsecretLoader::mock_secret_value_get_text(
91 MockSecretValue* value) { 91 SecretValue* value) {
92 return value->c_str(); 92 MockSecretValue* mock_value = reinterpret_cast<MockSecretValue*>(value);
93 return mock_value->c_str();
93 } 94 }
94 95
95 // static 96 // static
96 gboolean MockLibsecretLoader::mock_secret_password_store_sync( 97 gboolean MockLibsecretLoader::mock_secret_password_store_sync(
97 const SecretSchema* schema, 98 const SecretSchema* schema,
98 const gchar* collection, 99 const gchar* collection,
99 const gchar* label, 100 const gchar* label,
100 const gchar* password, 101 const gchar* password,
101 GCancellable* cancellable, 102 GCancellable* cancellable,
102 GError** error, 103 GError** error,
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 GError** error, 145 GError** error,
145 ...) { 146 ...) {
146 // We would only delete entries in the deprecated schema. 147 // We would only delete entries in the deprecated schema.
147 EXPECT_STREQ(kKeystoreSchemaV1.name, schema->name); 148 EXPECT_STREQ(kKeystoreSchemaV1.name, schema->name);
148 delete deprecated_password_mock_ptr_; 149 delete deprecated_password_mock_ptr_;
149 deprecated_password_mock_ptr_ = nullptr; 150 deprecated_password_mock_ptr_ = nullptr;
150 return true; 151 return true;
151 } 152 }
152 153
153 // static 154 // static
154 MockSecretValue* MockLibsecretLoader::mock_secret_item_get_secret( 155 SecretValue* MockLibsecretLoader::mock_secret_item_get_secret(
155 MockSecretItem* item) { 156 SecretItem* item) {
156 return item; 157 static_assert(std::is_same<MockSecretValue, MockSecretItem>::value,
158 "mock_secret_item_get_secret() assumes that the only thing we "
159 "need from MockSercetItem is the MockSecretValue");
160 return reinterpret_cast<SecretValue*>(item);
157 } 161 }
158 162
159 // static 163 // static
160 bool MockLibsecretLoader::ResetForOSCrypt() { 164 bool MockLibsecretLoader::ResetForOSCrypt() {
161 // Methods used by KeyStorageLibsecret 165 // Methods used by KeyStorageLibsecret
162 secret_password_store_sync = 166 secret_password_store_sync =
163 &MockLibsecretLoader::mock_secret_password_store_sync; 167 &MockLibsecretLoader::mock_secret_password_store_sync;
164 secret_value_get_text = (decltype(&::secret_value_get_text)) & 168 secret_value_get_text = &MockLibsecretLoader::mock_secret_value_get_text;
165 MockLibsecretLoader::mock_secret_value_get_text;
166 secret_value_unref = &MockLibsecretLoader::mock_secret_value_unref; 169 secret_value_unref = &MockLibsecretLoader::mock_secret_value_unref;
167 secret_service_search_sync = 170 secret_service_search_sync =
168 &MockLibsecretLoader::mock_secret_service_search_sync; 171 &MockLibsecretLoader::mock_secret_service_search_sync;
169 secret_item_get_secret = 172 secret_item_get_secret = &MockLibsecretLoader::mock_secret_item_get_secret;
170 (decltype(&::secret_item_get_secret))mock_secret_item_get_secret;
171 // Used by Migrate() 173 // Used by Migrate()
172 secret_password_clear_sync = 174 secret_password_clear_sync =
173 &MockLibsecretLoader::mock_secret_password_clear_sync; 175 &MockLibsecretLoader::mock_secret_password_clear_sync;
174 176
175 delete stored_password_mock_ptr_; 177 delete stored_password_mock_ptr_;
176 stored_password_mock_ptr_ = nullptr; 178 stored_password_mock_ptr_ = nullptr;
177 libsecret_loaded_ = true; 179 libsecret_loaded_ = true;
178 180
179 return true; 181 return true;
180 } 182 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 234
233 TEST_F(LibsecretTest, LibsecretMigratesFromSchemaV1ToV2) { 235 TEST_F(LibsecretTest, LibsecretMigratesFromSchemaV1ToV2) {
234 KeyStorageLibsecret libsecret; 236 KeyStorageLibsecret libsecret;
235 MockLibsecretLoader::ResetForOSCrypt(); 237 MockLibsecretLoader::ResetForOSCrypt();
236 MockLibsecretLoader::SetDeprecatedOSCryptPassword("swallow"); 238 MockLibsecretLoader::SetDeprecatedOSCryptPassword("swallow");
237 std::string password = libsecret.GetKey(); 239 std::string password = libsecret.GetKey();
238 EXPECT_EQ("swallow", password); 240 EXPECT_EQ("swallow", password);
239 } 241 }
240 242
241 } // namespace 243 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/password_manager/native_backend_libsecret_unittest.cc ('k') | components/os_crypt/libsecret_util_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698